NameResolver.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_NAME_RESOLVER_H_
  23. #define _D_NAME_RESOLVER_H_
  24. #include "common.h"
  25. #include "SharedHandle.h"
  26. #include <netdb.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include <ares.h>
  34. #ifdef __cplusplus
  35. } /* end of extern "C" */
  36. #endif
  37. void callback(void* arg, int status, struct hostent* host);
  38. class NameResolver {
  39. friend void callback(void* arg, int status, struct hostent* host);
  40. public:
  41. enum STATUS {
  42. STATUS_READY,
  43. STATUS_QUERYING,
  44. STATUS_SUCCESS,
  45. STATUS_ERROR,
  46. };
  47. private:
  48. STATUS status;
  49. ares_channel channel;
  50. struct in_addr addr;
  51. string error;
  52. public:
  53. NameResolver():
  54. status(STATUS_READY)
  55. {
  56. ares_init(&channel);
  57. }
  58. ~NameResolver() {
  59. ares_destroy(channel);
  60. }
  61. void resolve(const string& name) {
  62. status = STATUS_QUERYING;
  63. ares_gethostbyname(channel, name.c_str(), AF_INET, callback, this);
  64. }
  65. string getAddrString() const {
  66. return inet_ntoa(addr);
  67. }
  68. const struct in_addr& getAddr() const {
  69. return addr;
  70. }
  71. const string& getError() const {
  72. return error;
  73. }
  74. STATUS getStatus() const {
  75. return status;
  76. }
  77. int getFds(fd_set* rfdsPtr, fd_set* wfdsPtr) const {
  78. return ares_fds(channel, rfdsPtr, wfdsPtr);
  79. }
  80. void process(fd_set* rfdsPtr, fd_set* wfdsPtr) {
  81. ares_process(channel, rfdsPtr, wfdsPtr);
  82. }
  83. bool operator==(const NameResolver& resolver) {
  84. return this == &resolver;
  85. }
  86. };
  87. typedef SharedHandle<NameResolver> NameResolverHandle;
  88. #endif // _D_NAME_RESOLVER_H_