/* */ #ifndef _D_NAME_RESOLVER_H_ #define _D_NAME_RESOLVER_H_ #include "common.h" #include "SharedHandle.h" #include #include #include #include #ifdef __cplusplus extern "C" { #endif #include #ifdef __cplusplus } /* end of extern "C" */ #endif void callback(void* arg, int status, struct hostent* host); class NameResolver { friend void callback(void* arg, int status, struct hostent* host); public: enum STATUS { STATUS_READY, STATUS_QUERYING, STATUS_SUCCESS, STATUS_ERROR, }; private: STATUS status; ares_channel channel; struct in_addr addr; string error; public: NameResolver(): status(STATUS_READY) { ares_init(&channel); } ~NameResolver() { ares_destroy(channel); } void resolve(const string& name) { status = STATUS_QUERYING; ares_gethostbyname(channel, name.c_str(), AF_INET, callback, this); } string getAddrString() const { return inet_ntoa(addr); } const struct in_addr& getAddr() const { return addr; } const string& getError() const { return error; } STATUS getStatus() const { return status; } int getFds(fd_set* rfdsPtr, fd_set* wfdsPtr) const { return ares_fds(channel, rfdsPtr, wfdsPtr); } void process(fd_set* rfdsPtr, fd_set* wfdsPtr) { ares_process(channel, rfdsPtr, wfdsPtr); } bool operator==(const NameResolver& resolver) { return this == &resolver; } }; typedef SharedHandle NameResolverHandle; #endif // _D_NAME_RESOLVER_H_