asyncdns.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2000-2007 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: asyncdns.h - JNL portable asynchronous DNS interface
  6. ** License: see jnetlib.h
  7. **
  8. ** Usage:
  9. ** 1. Create JNL_AsyncDNS object, optionally with the number of cache entries.
  10. ** 2. call resolve() to resolve a hostname into an address. The return value of
  11. ** resolve is 0 on success (host successfully resolved), 1 on wait (meaning
  12. ** try calling resolve() with the same hostname in a few hundred milliseconds
  13. ** or so), or -1 on error (i.e. the host can't resolve).
  14. ** 3. call reverse() to do reverse dns (ala resolve()).
  15. ** 4. enjoy.
  16. */
  17. #ifndef _ASYNCDNS_H_
  18. #define _ASYNCDNS_H_
  19. #include "netinc.h"
  20. struct cache_entry;
  21. #define JNL_AUTODNS ((JNL_AsyncDNS *)-1)
  22. enum
  23. {
  24. DNS_RESOLVE_UNRESOLVABLE = -1,
  25. DNS_RESOLVE_SUCCESS = 0,
  26. DNS_RESOLVE_WAIT = 1,
  27. };
  28. enum
  29. {
  30. DNS_REVERSE_UNRESOLVABLE = -1,
  31. DNS_REVERSE_SUCCESS = 0,
  32. DNS_REVERSE_WAIT = 1,
  33. };
  34. class JNL_AsyncDNS
  35. {
  36. public:
  37. JNL_AsyncDNS( int max_cache_entries = 64 );
  38. ~JNL_AsyncDNS();
  39. int resolve( const char *hostname, unsigned short port, addrinfo **addr, int sockettype ); // return 0 on success, 1 on wait, -1 on unresolvable
  40. static int resolvenow( const char *hostname, unsigned short port, addrinfo **addr, int sockettype ); // return 0 on success, -1 on unresolvable
  41. //int reverse(unsigned long addr, char *hostname, size_t hostnameSize); // return 0 on success, 1 on wait, -1 on unresolvable. hostname must be at least 256 bytes.
  42. private:
  43. cache_entry *m_cache;
  44. int m_cache_size;
  45. volatile int m_thread_kill;
  46. #ifdef _WIN32
  47. HANDLE m_thread;
  48. static unsigned long WINAPI _threadfunc( LPVOID _d );
  49. #else
  50. pthread_t m_thread;
  51. static unsigned int _threadfunc( void *_d );
  52. #endif
  53. void makesurethreadisrunning( void );
  54. };
  55. #endif //_ASYNCDNS_H_