21 # include <sys/types.h>
26 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
27 # include <netinet/in.h>
28 # include <arpa/nameser.h>
31 # include <arpa/inet.h>
32 # include <sys/socket.h>
38 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
40 #elif defined( _WIN32_WCE )
41 # include <winsock2.h>
48 #define SRV_COST (RRFIXEDSZ+0)
49 #define SRV_WEIGHT (RRFIXEDSZ+2)
50 #define SRV_PORT (RRFIXEDSZ+4)
51 #define SRV_SERVER (RRFIXEDSZ+6)
52 #define SRV_FIXEDSZ (RRFIXEDSZ+6)
60 # define DNS_TYPE_SRV 33
64 # define NS_CMPRSFLGS 0xc0
71 #ifndef INVALID_SOCKET
72 # define INVALID_SOCKET -1
75 #define XMPP_PORT 5222
80 #if defined( HAVE_RES_QUERYDOMAIN ) && defined( HAVE_DN_SKIPNAME ) && defined( HAVE_RES_QUERY )
82 const std::string& domain,
const LogSink& logInstance )
87 const std::string dname =
"_" + service +
"._" + proto;
90 srvbuf.len = res_querydomain( dname.c_str(),
const_cast<char*
>( domain.c_str() ),
91 C_IN, T_SRV, srvbuf.buf, NS_PACKETSZ );
93 srvbuf.len = res_query( dname.c_str(), C_IN, T_SRV, srvbuf.buf, NS_PACKETSZ );
96 return defaultHostMap( domain, logInstance );
98 HEADER* hdr = (HEADER*)srvbuf.buf;
99 unsigned char* here = srvbuf.buf + NS_HFIXEDSZ;
101 if( ( hdr->tc ) || ( srvbuf.len < NS_HFIXEDSZ ) )
104 if( hdr->rcode >= 1 && hdr->rcode <= 5 )
107 if( ntohs( hdr->ancount ) == 0 )
110 if( ntohs( hdr->ancount ) > NS_PACKETSZ )
114 for( cnt = ntohs( hdr->qdcount ); cnt > 0; --cnt )
116 int strlen = dn_skipname( here, srvbuf.buf + srvbuf.len );
117 here += strlen + NS_QFIXEDSZ;
120 unsigned char* srv[NS_PACKETSZ];
122 for( cnt = ntohs( hdr->ancount ); cnt > 0; --cnt )
124 int strlen = dn_skipname( here, srvbuf.buf + srvbuf.len );
126 srv[srvnum++] = here;
128 here += dn_skipname( here, srvbuf.buf + srvbuf.len );
133 return defaultHostMap( domain, logInstance );
139 for( cnt = 0; cnt < srvnum; ++cnt )
141 char srvname[NS_MAXDNAME];
144 if( dn_expand( srvbuf.buf, srvbuf.buf + NS_PACKETSZ,
145 srv[cnt] + SRV_SERVER, srvname, NS_MAXDNAME ) < 0
149 unsigned char* c = srv[cnt] + SRV_PORT;
150 servers.insert( std::make_pair( (
char*)srvname, ntohs( c[1] << 8 | c[0] ) ) );
153 if( !servers.size() )
154 return defaultHostMap( domain, logInstance );
159 #elif defined( _WIN32 ) && defined( HAVE_WINDNS_H )
161 const std::string& domain,
const LogSink& logInstance )
163 const std::string dname =
"_" + service +
"._" + proto +
"." + domain;
167 DNS_RECORD* pRecord = NULL;
168 DNS_STATUS status = DnsQuery_UTF8( dname.c_str(), DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &pRecord, NULL );
169 if( status == ERROR_SUCCESS )
171 DNS_RECORD* pRec = pRecord;
174 if( pRec->wType == DNS_TYPE_SRV )
176 servers[pRec->Data.SRV.pNameTarget] = pRec->Data.SRV.wPort;
180 while( pRec != NULL );
181 DnsRecordListFree( pRecord, DnsFreeRecordList );
185 logInstance.warn(
LogAreaClassDns,
"DnsQuery_UTF8() failed: " + util::int2string( status ) );
189 if( error || !servers.size() )
191 servers = defaultHostMap( domain, logInstance );
199 const std::string& domain,
const LogSink& logInstance )
202 "records on this platform. Using A records instead." );
203 return defaultHostMap( domain, logInstance );
212 + domain +
", using default port." );
214 if( !domain.empty() )
215 server[domain] = XMPP_PORT;
220 #ifdef HAVE_GETADDRINFO
221 void DNS::resolve(
struct addrinfo** res,
const std::string& service,
const std::string& proto,
222 const std::string& domain,
const LogSink& logInstance )
224 logInstance.dbg(
LogAreaClassDns,
"Resolving: _" + service +
"._" + proto +
"." + domain );
225 struct addrinfo hints;
227 hints.ai_socktype = SOCK_STREAM;
228 else if( proto ==
"udp" )
229 hints.ai_socktype = SOCK_DGRAM;
232 logInstance.err(
LogAreaClassDns,
"Unknown/Invalid protocol: " + proto );
234 memset( &hints,
'\0',
sizeof( hints ) );
235 hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME;
236 hints.ai_socktype = SOCK_STREAM;
237 int e = getaddrinfo( domain.c_str(), service.c_str(), &hints, res );
242 int DNS::connect(
const std::string& host,
const LogSink& logInstance )
244 struct addrinfo* results = 0;
246 resolve( &results, host, logInstance );
253 struct addrinfo* runp = results;
260 runp = runp->ai_next;
263 freeaddrinfo( results );
268 int DNS::connect(
struct addrinfo* res,
const LogSink& logInstance )
273 int fd =
getSocket( res->ai_family, res->ai_socktype, res->ai_protocol, logInstance );
277 if( ::
connect( fd, res->ai_addr, res->ai_addrlen ) == 0 )
280 char port[NI_MAXSERV];
282 if( getnameinfo( res->ai_addr,
sizeof( sockaddr ),
284 port,
sizeof( port ),
285 NI_NUMERICHOST | NI_NUMERICSERV ) )
291 if( res->ai_canonname )
292 logInstance.dbg(
LogAreaClassDns,
"Connecting to " + std::string( res->ai_canonname )
293 +
" (" + ip +
"), port " + port );
295 logInstance.dbg(
LogAreaClassDns,
"Connecting to " + ip +
":" + port );
300 std::string message =
"connect() failed. "
301 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
302 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
304 "errno: " + util::int2string( errno );
317 if( hosts.size() == 0 )
320 HostMap::const_iterator it = hosts.begin();
321 for( ; it != hosts.end(); ++it )
323 int fd =
DNS::connect( (*it).first, (*it).second, logInstance );
334 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
336 if( WSAStartup( MAKEWORD( 1, 1 ), &wsaData ) != 0 )
339 + util::int2string( ::WSAGetLastError() ) );
344 int protocol = IPPROTO_TCP;
345 struct protoent* prot;
346 if( ( prot = getprotobyname(
"tcp" ) ) != 0 )
348 protocol = prot->p_proto;
352 std::string message =
"getprotobyname( \"tcp\" ) failed. "
353 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
354 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() )
356 "errno: " + util::int2string( errno );
358 +
". Falling back to IPPROTO_TCP: " + util::int2string( IPPROTO_TCP );
364 return getSocket( PF_INET, SOCK_STREAM, protocol, logInstance );
369 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
374 if( ( fd = socket( af, socktype, proto ) ) == INVALID_SOCKET )
376 std::string message =
"getSocket( "
377 + util::int2string( af ) +
", "
378 + util::int2string( socktype ) +
", "
379 + util::int2string( proto )
381 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
382 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
384 "errno: " + util::int2string( errno );
388 cleanup( logInstance );
392 #ifdef HAVE_SETSOCKOPT
394 setsockopt( fd, SOL_SOCKET, SO_SNDTIMEO, (
char*)&timeout,
sizeof( timeout ) );
395 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (
char*)&timeout,
sizeof( timeout ) );
408 if( ( h = gethostbyname( host.c_str() ) ) == 0 )
411 cleanup( logInstance );
415 struct sockaddr_in target;
416 target.sin_family = AF_INET;
417 target.sin_port = htons( static_cast<unsigned short int>( port ) );
419 if( h->h_length !=
sizeof(
struct in_addr ) )
422 cleanup( logInstance );
427 memcpy( &target.sin_addr, h->h_addr,
sizeof(
struct in_addr ) );
431 +
" (" + inet_ntoa( target.sin_addr ) +
":" + util::int2string( port ) +
")" );
433 memset( target.sin_zero,
'\0', 8 );
434 if( ::
connect( fd, (
struct sockaddr *)&target,
sizeof(
struct sockaddr ) ) == 0 )
437 + inet_ntoa( target.sin_addr ) +
":" + util::int2string( port ) +
")" );
441 std::string message =
"Connection to " + host +
" ("
442 + inet_ntoa( target.sin_addr ) +
":" + util::int2string( port ) +
") failed. "
443 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
444 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
446 "errno: " + util::int2string( errno );
456 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
457 int result = closesocket( fd );
459 int result = close( fd );
464 std::string message =
"closeSocket() failed. "
465 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
466 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
468 "errno: " + util::int2string( errno );
474 void DNS::cleanup(
const LogSink& logInstance )
476 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
477 if( WSACleanup() != 0 )
480 + util::int2string( ::WSAGetLastError() ) );