Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

AsyncResolver.h

Go to the documentation of this file.
00001 /*  AsyncResolver.h - simple asynchronous DNS resolver stub
00002     Copyright (C) 2004 Mark Weaver
00003     Written by Mark Weaver <mark@npsl.co.uk>
00004 
00005     Part of the Open-Win32 library.
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the
00018     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA  02111-1307, USA.
00020 */
00021 
00022 /* Copyright 1998 by the Massachusetts Institute of Technology.
00023  *
00024  * Permission to use, copy, modify, and distribute this
00025  * software and its documentation for any purpose and without
00026  * fee is hereby granted, provided that the above copyright
00027  * notice appear in all copies and that both that copyright
00028  * notice and this permission notice appear in supporting
00029  * documentation, and that the name of M.I.T. not be used in
00030  * advertising or publicity pertaining to distribution of the
00031  * software without specific, written prior permission.
00032  * M.I.T. makes no representations about the suitability of
00033  * this software for any purpose.  It is provided "as is"
00034  * without express or implied warranty.
00035  */
00036 
00041 #ifndef OW32_AsyncResolver_h
00042 #define OW32_AsyncResolver_h
00043 
00044 #if defined(_MSC_VER) && !defined(_DEBUG)
00045 #pragma warning(disable: 4702)
00046 #endif
00047 #include <map>
00048 #include <vector>
00049 #if defined(_MSC_VER) && !defined(_DEBUG)
00050 #pragma warning(default: 4702)
00051 #endif
00052 
00053 #include <OW32/windows.h>
00054 #include <OW32/SyncObjects.h>
00055 
00056 #include <OW32/AsyncResolverSearch.h>
00057 #include <OW32/AsyncResolverOptions.h>
00058 #include <OW32/AsyncResolverRandom.h>
00059 #include <OW32/AsyncResolverHostCallback.h>
00060 #include <OW32/AsyncResolverHostsDB.h>
00061 
00062 #include <OW32/FileWatch.h>
00063 #include <OW32/FileWatchCallback.h>
00064 
00065 namespace OW32
00066 {
00067 
00068 class OW32_LIB_EXPORT CAsyncResolver;
00069 class OW32_LIB_EXPORT CAsyncResolverServer;
00070 class OW32_LIB_EXPORT CFileWatcher;
00071 
00072 #ifdef _MSC_VER
00073 #pragma warning(disable: 4251)
00074 #endif
00075 
00079 class OW32_LIB_EXPORT CAsyncResolver :
00080     public CIoTimeoutCallback,
00081     public CIoTimeoutDrainCallback,
00082     private CFileWatchCallback
00083 {
00084     // options and stuff
00085 public:
00089     CAsyncResolver() {}
00090 
00103     AsyncResolverError initialise(
00104         const CAsyncResolverOptions *pOptions, 
00105         HANDLE                      hIoPort,
00106         CFileWatcher                *pFileWatcher = NULL,
00107         const char                  *random_data = NULL);
00108 
00112     ~CAsyncResolver();
00113 
00118     AsyncResolverError terminate();
00119 
00123     HANDLE getIoCompletionPort() { return m_hIoCompletionPort; }
00124 
00135     void gethostbyname(CAsyncResolverHostCallback *pCallback, void *context, const char *name, int family);
00136 
00151     void gethostbyaddr(CAsyncResolverHostCallback *pCallback, void *context, const void *addr, int addrlen, int family);
00152 
00164     void search(CAsyncResolverCallback *pCallback, void *context, const char *name, int dnsclass, int type);
00165 
00173     void sendQuery(CAsyncResolverQuery *query);
00174 
00187     static AsyncResolverError expandName(
00188             const unsigned char *encoded, 
00189             const unsigned char *abuf, size_t alen, 
00190             char **s, size_t *enclen);
00191 
00201     AsyncResolverError getHostsDB(CAsyncResolverHostsDBPtr &hostsDB);
00202 
00212     static AsyncResolverError getHostEnt(FILE *fp, struct hostent **host);
00213 
00221     static void freeHostEnt(struct hostent *host);
00222 
00231     static const TCHAR *describeError(AsyncResolverError code);
00232 
00242     static AsyncResolverError readLine(FILE *fp, char **buf, size_t *bufsize);
00243 
00245     unsigned int random(unsigned int n);
00246 
00248     const CAsyncResolverOptions *getOptions() const { return m_pOptions; }
00249 
00251     void OnAnswer(size_t serverIndex, bool fTCP, BYTE *abuf, size_t alen);
00252 
00254     void OnError(size_t serverIndex, bool fTCP, AsyncResolverError status, CAsyncResolverQuery *query);
00255 
00256 // CIoTimeoutCallback:
00261     virtual void onTimeout(LPVOID key);
00262 
00263 // CIoTimeoutDrainCallback
00268     virtual bool drainTimeout(CIoTimeoutCallback *pCallback, LPVOID key);
00269 
00270 private:
00271 // CFileWatchCallback
00278     virtual void onChanged(CFileWatch *watch, const FILE_NOTIFY_INFORMATION *notifyInfo);
00279 
00287     virtual void onFailure(CFileWatch *watch, DWORD error);
00288 
00289 private:
00296     bool removeQuery(CAsyncResolverQuery *query);
00297 
00298     // prohibit copy and assignment
00299     CAsyncResolver(const CAsyncResolver &);
00300     CAsyncResolver& operator=(const CAsyncResolver &);
00301 
00303     void nextServer(CAsyncResolverQuery *query);
00304 
00306     typedef std::multimap<int, CAsyncResolverQueryPtr> querymap_t;
00308     querymap_t                          m_queryMap;
00310     CCriticalSection                    m_queryMapLock;
00311 
00313     CAsyncResolverRandom                m_random;
00315     CCriticalSection                    m_randomLock;
00316 
00318     const CAsyncResolverOptions         *m_pOptions;
00320     std::vector<CAsyncResolverServer *> m_servers;
00322     bool                                m_fInDestruction;
00323 
00325     CCriticalSection                    m_hostsDBLock;
00327     CAsyncResolverHostsDBPtr            m_hostsDB;
00329     CFileWatchPtr                       m_fileWatch;
00330 
00332     CFileWatcher                        *m_pFileWatcher;
00333 
00335     HANDLE                              m_hIoCompletionPort;
00336 };
00337 
00338 #ifdef _MSC_VER
00339 #pragma warning(default: 4251)
00340 #endif
00341 
00342 }; // namespace OW32
00343 
00344 #endif // OW32_AsyncResolver_h

Generated on Sun Jun 5 01:29:17 2005 for OW32 by  doxygen 1.3.9.1