00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
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
00261 virtual void onTimeout(LPVOID key);
00262
00263
00268 virtual bool drainTimeout(CIoTimeoutCallback *pCallback, LPVOID key);
00269
00270 private:
00271
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
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 };
00343
00344 #endif // OW32_AsyncResolver_h