int DNS::connect( const std::string& host, const LogSink& logInstance ) { struct addrinfo* results = 0; resolve( &results, host, logInstance ); if( !results ) { logInstance.err( LogAreaClassDns, "host not found: " + host ); return -ConnDnsError; } struct addrinfo* runp = results; while( runp ) { int fd = DNS::connect( runp, logInstance ); if( fd >= 0 ) return fd; runp = runp->ai_next; } freeaddrinfo( results ); return -ConnConnectionRefused; }
void DNS::resolve( struct addrinfo** res, const std::string& service, const std::string& proto, const std::string& domain, const LogSink& logInstance ) { logInstance.dbg( LogAreaClassDns, "Resolving: _" + service + "._" + proto + "." + domain ); struct addrinfo hints; if( proto == "tcp" ) hints.ai_socktype = SOCK_STREAM; else if( proto == "udp" ) hints.ai_socktype = SOCK_DGRAM; else { logInstance.err( LogAreaClassDns, "Unknown/Invalid protocol: " + proto ); } memset( &hints, '\0', sizeof( hints ) ); hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME; hints.ai_socktype = SOCK_STREAM; int e = getaddrinfo( domain.c_str(), service.c_str(), &hints, res ); if( e ) logInstance.err( LogAreaClassDns, "getaddrinfo() failed" ); }