21 # include <sys/types.h>
27 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
28 # include <netinet/in.h>
29 # include <arpa/nameser.h>
32 # include <arpa/inet.h>
33 # include <sys/socket.h>
39 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
40 # include <winsock2.h>
41 # include <ws2tcpip.h>
42 #elif defined( _WIN32_WCE )
43 # include <winsock2.h>
50 #define SRV_COST (RRFIXEDSZ+0)
51 #define SRV_WEIGHT (RRFIXEDSZ+2)
52 #define SRV_PORT (RRFIXEDSZ+4)
53 #define SRV_SERVER (RRFIXEDSZ+6)
54 #define SRV_FIXEDSZ (RRFIXEDSZ+6)
62 # define DNS_TYPE_SRV 33
66 # define NS_CMPRSFLGS 0xc0
73 #ifndef INVALID_SOCKET
74 # define INVALID_SOCKET -1
77 #define XMPP_PORT 5222
82 #if defined( HAVE_RES_QUERYDOMAIN ) && defined( HAVE_DN_SKIPNAME ) && defined( HAVE_RES_QUERY )
84 const std::string& domain,
const LogSink& logInstance )
89 const std::string dname =
"_" + service +
"._" + proto;
92 srvbuf.len = res_querydomain( dname.c_str(),
const_cast<char*
>( domain.c_str() ),
93 C_IN, T_SRV, srvbuf.buf, NS_PACKETSZ );
95 srvbuf.len = res_query( dname.c_str(), C_IN, T_SRV, srvbuf.buf, NS_PACKETSZ );
98 return defaultHostMap( domain, logInstance );
100 HEADER* hdr =
reinterpret_cast<HEADER*
>( srvbuf.buf );
101 unsigned char* here = srvbuf.buf + NS_HFIXEDSZ;
103 if( srvbuf.len < NS_HFIXEDSZ )
106 if( hdr->rcode >= 1 && hdr->rcode <= 5 )
109 if( ntohs( hdr->ancount ) == 0 )
112 if( ntohs( hdr->ancount ) > NS_PACKETSZ )
116 for( cnt = ntohs( hdr->qdcount ); cnt > 0; --cnt )
118 int strlen = dn_skipname( here, srvbuf.buf + srvbuf.len );
119 here += strlen + NS_QFIXEDSZ;
122 unsigned char* srv[NS_PACKETSZ];
124 for( cnt = ntohs( hdr->ancount ); cnt > 0; --cnt )
126 int strlen = dn_skipname( here, srvbuf.buf + srvbuf.len );
128 srv[srvnum++] = here;
130 here += dn_skipname( here, srvbuf.buf + srvbuf.len );
135 return defaultHostMap( domain, logInstance );
141 for( cnt = 0; cnt < srvnum; ++cnt )
143 char srvname[NS_MAXDNAME];
146 if( dn_expand( srvbuf.buf, srvbuf.buf + NS_PACKETSZ,
147 srv[cnt] + SRV_SERVER, srvname, NS_MAXDNAME ) < 0
151 unsigned char* c = srv[cnt] + SRV_PORT;
152 servers.insert( std::make_pair(
static_cast<char*
>( srvname ), ntohs( c[1] << 8 | c[0] ) ) );
155 if( !servers.size() )
156 return defaultHostMap( domain, logInstance );
161 #elif defined( _WIN32 ) && defined( HAVE_WINDNS_H ) && !defined( __MINGW32__ )
163 const std::string& domain,
const LogSink& logInstance )
165 const std::string dname =
"_" + service +
"._" + proto +
"." + domain;
169 DNS_RECORD* pRecord = NULL;
170 DNS_STATUS status = DnsQuery_UTF8( dname.c_str(), DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &pRecord, NULL );
171 if( status == ERROR_SUCCESS )
176 DNS_RECORDA* pRec = (DNS_RECORDA*)pRecord;
179 if( pRec->wType == DNS_TYPE_SRV )
181 servers[pRec->Data.SRV.pNameTarget] = pRec->Data.SRV.wPort;
185 while( pRec != NULL );
186 DnsRecordListFree( pRecord, DnsFreeRecordList );
190 logInstance.
warn(
LogAreaClassDns,
"DnsQuery_UTF8() failed: " + util::int2string( status ) );
194 if( error || !servers.size() )
196 servers = defaultHostMap( domain, logInstance );
204 const std::string& domain,
const LogSink& logInstance )
206 logInstance.warn(
LogAreaClassDns,
"Notice: gloox does not support SRV "
207 "records on this platform. Using A records instead." );
208 return defaultHostMap( domain, logInstance );
212 DNS::HostMap DNS::defaultHostMap(
const std::string& domain,
const LogSink& logInstance )
217 + domain +
", using default port." );
219 if( !domain.empty() )
220 server[domain] = XMPP_PORT;
225 #ifdef HAVE_GETADDRINFO
226 void DNS::resolve(
struct addrinfo** res,
const std::string& service,
const std::string& proto,
227 const std::string& domain,
const LogSink& logInstance )
229 logInstance.dbg(
LogAreaClassDns,
"Resolving: _" + service +
"._" + proto +
"." + domain );
230 struct addrinfo hints;
232 hints.ai_socktype = SOCK_STREAM;
233 else if( proto ==
"udp" )
234 hints.ai_socktype = SOCK_DGRAM;
237 logInstance.err(
LogAreaClassDns,
"Unknown/Invalid protocol: " + proto );
239 memset( &hints,
'\0',
sizeof( hints ) );
240 hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME;
241 hints.ai_socktype = SOCK_STREAM;
242 int e = getaddrinfo( domain.c_str(), service.c_str(), &hints, res );
249 struct addrinfo* results = 0;
251 resolve( &results, host, logInstance );
258 struct addrinfo* runp = results;
265 runp = runp->ai_next;
268 freeaddrinfo( results );
278 int fd =
getSocket( res->ai_family, res->ai_socktype, res->ai_protocol, logInstance );
282 if( ::
connect( fd, res->ai_addr, res->ai_addrlen ) == 0 )
285 char port[NI_MAXSERV];
287 if( getnameinfo( res->ai_addr, res->ai_addrlen,
289 port,
sizeof( port ),
290 NI_NUMERICHOST | NI_NUMERICSERV ) )
296 if( res->ai_canonname )
297 logInstance.
dbg(
LogAreaClassDns, std::string(
"Connecting to " ).append( res->ai_canonname ).append(
" (" ).append( ip ).append(
"), port " ).append( port ) );
299 logInstance.
dbg(
LogAreaClassDns, std::string(
"Connecting to " ).append( ip ).append(
":" ).append( port ) );
304 std::string message =
"connect() failed. "
305 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
306 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
308 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
318 int DNS::connect(
const std::string& host,
const LogSink& logInstance )
321 if( hosts.size() == 0 )
324 HostMap::const_iterator it = hosts.begin();
325 for( ; it != hosts.end(); ++it )
327 int fd =
DNS::connect( (*it).first, (*it).second, logInstance );
338 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
340 if( WSAStartup( MAKEWORD( 1, 1 ), &wsaData ) != 0 )
343 + util::int2string( ::WSAGetLastError() ) );
348 int protocol = IPPROTO_TCP;
349 #if !defined( __APPLE__ ) // Sandboxing on Apple doesn't like you to use getprotobyname
350 struct protoent* prot;
351 if( ( prot = getprotobyname(
"tcp" ) ) != 0 )
353 protocol = prot->p_proto;
357 std::string message =
"getprotobyname( \"tcp\" ) failed. "
358 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
359 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() )
361 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
363 +
". Falling back to IPPROTO_TCP: " + util::int2string( IPPROTO_TCP );
368 #endif // !defined( __APPLE__ )
370 return getSocket( PF_INET, SOCK_STREAM, protocol, logInstance );
375 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
380 if( ( fd = socket( af, socktype, proto ) ) == INVALID_SOCKET )
382 std::string message =
"getSocket( "
383 + util::int2string( af ) +
", "
384 + util::int2string( socktype ) +
", "
385 + util::int2string( proto )
387 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
388 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
390 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
394 cleanup( logInstance );
398 #ifdef HAVE_SETSOCKOPT
401 setsockopt( fd, SOL_SOCKET, SO_SNDTIMEO,
reinterpret_cast<char*
>( &timeout ),
sizeof( timeout ) );
402 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char*
>( &reuseaddr ),
sizeof( reuseaddr ) );
405 return static_cast<int>( fd );
408 #ifdef HAVE_GETADDRINFO
411 struct addrinfo hints, *servinfo, *p;
415 memset( &hints, 0,
sizeof( hints ) );
416 hints.ai_family = AF_UNSPEC;
417 hints.ai_socktype = SOCK_STREAM;
419 if( ( rv = getaddrinfo( host.c_str(), util::int2string( port ).c_str(), &hints, &servinfo ) ) != 0 )
425 for( p = servinfo; p != 0; p = p->ai_next )
427 if( ( fd =
getSocket( p->ai_family, p->ai_socktype, p->ai_protocol, logInstance ) ) == -1 )
432 if( ::
connect( fd, p->ai_addr, p->ai_addrlen ) == -1 )
443 freeaddrinfo( servinfo );
444 std::string message =
"Connection to " + host +
":" + util::int2string( port ) +
" failed. "
445 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
446 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
448 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
454 freeaddrinfo( servinfo );
458 #else // HAVE_GETADDRINFO
466 if( ( h = gethostbyname( host.c_str() ) ) == 0 )
469 cleanup( logInstance );
474 struct sockaddr_in target;
475 target.sin_family = AF_INET;
476 target.sin_port = htons(
static_cast<unsigned short int>( port ) );
478 if( h->h_length !=
sizeof(
struct in_addr ) )
481 cleanup( logInstance );
487 memcpy( &target.sin_addr, h->h_addr,
sizeof(
struct in_addr ) );
491 +
" (" + inet_ntoa( target.sin_addr ) +
":" + util::int2string( port ) +
")" );
493 memset( target.sin_zero,
'\0', 8 );
494 if( ::
connect( fd,
reinterpret_cast<struct sockaddr *
>( &target ),
sizeof(
struct sockaddr ) ) == 0 )
497 + inet_ntoa( target.sin_addr ) +
":" + util::int2string( port ) +
")" );
501 std::string message =
"Connection to " + host +
" ("
502 + inet_ntoa( target.sin_addr ) +
":" + util::int2string( port ) +
") failed. "
503 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
504 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
506 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
513 #endif // HAVE_GETADDRINFO
517 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
518 int result = closesocket( fd );
520 int result = close( fd );
525 std::string message =
"closeSocket() failed. "
526 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
527 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
529 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
535 void DNS::cleanup(
const LogSink& logInstance )
537 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
538 if( WSACleanup() != 0 )
541 + util::int2string( ::WSAGetLastError() ) );