gloox  1.0.1
dns.cpp
1 /*
2  Copyright (c) 2005-2012 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "config.h"
15 
16 #include "gloox.h"
17 #include "dns.h"
18 #include "util.h"
19 
20 #ifndef _WIN32_WCE
21 # include <sys/types.h>
22 #endif
23 
24 #include <stdio.h>
25 
26 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
27 # include <netinet/in.h>
28 # include <arpa/nameser.h>
29 # include <resolv.h>
30 # include <netdb.h>
31 # include <arpa/inet.h>
32 # include <sys/socket.h>
33 # include <sys/un.h>
34 # include <unistd.h>
35 # include <errno.h>
36 #endif
37 
38 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
39 # include <winsock.h>
40 #elif defined( _WIN32_WCE )
41 # include <winsock2.h>
42 #endif
43 
44 #ifdef HAVE_WINDNS_H
45 # include <windns.h>
46 #endif
47 
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)
53 
54 #ifndef T_SRV
55 # define T_SRV 33
56 #endif
57 
58 // mingw
59 #ifndef DNS_TYPE_SRV
60 # define DNS_TYPE_SRV 33
61 #endif
62 
63 #ifndef NS_CMPRSFLGS
64 # define NS_CMPRSFLGS 0xc0
65 #endif
66 
67 #ifndef C_IN
68 # define C_IN 1
69 #endif
70 
71 #ifndef INVALID_SOCKET
72 # define INVALID_SOCKET -1
73 #endif
74 
75 #define XMPP_PORT 5222
76 
77 namespace gloox
78 {
79 
80 #if defined( HAVE_RES_QUERYDOMAIN ) && defined( HAVE_DN_SKIPNAME ) && defined( HAVE_RES_QUERY )
81  DNS::HostMap DNS::resolve( const std::string& service, const std::string& proto,
82  const std::string& domain, const LogSink& logInstance )
83  {
84  buffer srvbuf;
85  bool error = false;
86 
87  const std::string dname = "_" + service + "._" + proto;
88 
89  if( !domain.empty() )
90  srvbuf.len = res_querydomain( dname.c_str(), const_cast<char*>( domain.c_str() ),
91  C_IN, T_SRV, srvbuf.buf, NS_PACKETSZ );
92  else
93  srvbuf.len = res_query( dname.c_str(), C_IN, T_SRV, srvbuf.buf, NS_PACKETSZ );
94 
95  if( srvbuf.len < 0 )
96  return defaultHostMap( domain, logInstance );
97 
98  HEADER* hdr = (HEADER*)srvbuf.buf;
99  unsigned char* here = srvbuf.buf + NS_HFIXEDSZ;
100 
101  if( srvbuf.len < NS_HFIXEDSZ )
102  error = true;
103 
104  if( hdr->rcode >= 1 && hdr->rcode <= 5 )
105  error = true;
106 
107  if( ntohs( hdr->ancount ) == 0 )
108  error = true;
109 
110  if( ntohs( hdr->ancount ) > NS_PACKETSZ )
111  error = true;
112 
113  int cnt;
114  for( cnt = ntohs( hdr->qdcount ); cnt > 0; --cnt )
115  {
116  int strlen = dn_skipname( here, srvbuf.buf + srvbuf.len );
117  here += strlen + NS_QFIXEDSZ;
118  }
119 
120  unsigned char* srv[NS_PACKETSZ];
121  int srvnum = 0;
122  for( cnt = ntohs( hdr->ancount ); cnt > 0; --cnt )
123  {
124  int strlen = dn_skipname( here, srvbuf.buf + srvbuf.len );
125  here += strlen;
126  srv[srvnum++] = here;
127  here += SRV_FIXEDSZ;
128  here += dn_skipname( here, srvbuf.buf + srvbuf.len );
129  }
130 
131  if( error )
132  {
133  return defaultHostMap( domain, logInstance );
134  }
135 
136  // (q)sort here
137 
138  HostMap servers;
139  for( cnt = 0; cnt < srvnum; ++cnt )
140  {
141  char srvname[NS_MAXDNAME];
142  srvname[0] = '\0';
143 
144  if( dn_expand( srvbuf.buf, srvbuf.buf + NS_PACKETSZ,
145  srv[cnt] + SRV_SERVER, srvname, NS_MAXDNAME ) < 0
146  || !(*srvname) )
147  continue;
148 
149  unsigned char* c = srv[cnt] + SRV_PORT;
150  servers.insert( std::make_pair( (char*)srvname, ntohs( c[1] << 8 | c[0] ) ) );
151  }
152 
153  if( !servers.size() )
154  return defaultHostMap( domain, logInstance );
155 
156  return servers;
157  }
158 
159 #elif defined( _WIN32 ) && defined( HAVE_WINDNS_H )
160  DNS::HostMap DNS::resolve( const std::string& service, const std::string& proto,
161  const std::string& domain, const LogSink& logInstance )
162  {
163  const std::string dname = "_" + service + "._" + proto + "." + domain;
164  bool error = false;
165 
166  DNS::HostMap servers;
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 )
170  {
171  // NOTE: DnsQuery_UTF8 and DnsQuery_A really should have been defined with
172  // PDNS_RECORDA instead of PDNS_RECORD, since that's what it is (even with _UNICODE defined).
173  // We'll correct for that mistake with a cast.
174  DNS_RECORDA* pRec = (DNS_RECORDA*)pRecord;
175  do
176  {
177  if( pRec->wType == DNS_TYPE_SRV )
178  {
179  servers[pRec->Data.SRV.pNameTarget] = pRec->Data.SRV.wPort;
180  }
181  pRec = pRec->pNext;
182  }
183  while( pRec != NULL );
184  DnsRecordListFree( pRecord, DnsFreeRecordList );
185  }
186  else
187  {
188  logInstance.warn( LogAreaClassDns, "DnsQuery_UTF8() failed: " + util::int2string( status ) );
189  error = true;
190  }
191 
192  if( error || !servers.size() )
193  {
194  servers = defaultHostMap( domain, logInstance );
195  }
196 
197  return servers;
198  }
199 
200 #else
201  DNS::HostMap DNS::resolve( const std::string& /*service*/, const std::string& /*proto*/,
202  const std::string& domain, const LogSink& logInstance )
203  {
204  logInstance.warn( LogAreaClassDns, "Notice: gloox does not support SRV "
205  "records on this platform. Using A records instead." );
206  return defaultHostMap( domain, logInstance );
207  }
208 #endif
209 
210  DNS::HostMap DNS::defaultHostMap( const std::string& domain, const LogSink& logInstance )
211  {
212  HostMap server;
213 
214  logInstance.warn( LogAreaClassDns, "Notice: no SRV record found for "
215  + domain + ", using default port." );
216 
217  if( !domain.empty() )
218  server[domain] = XMPP_PORT;
219 
220  return server;
221  }
222 
223 #ifdef HAVE_GETADDRINFO
224  void DNS::resolve( struct addrinfo** res, const std::string& service, const std::string& proto,
225  const std::string& domain, const LogSink& logInstance )
226  {
227  logInstance.dbg( LogAreaClassDns, "Resolving: _" + service + "._" + proto + "." + domain );
228  struct addrinfo hints;
229  if( proto == "tcp" )
230  hints.ai_socktype = SOCK_STREAM;
231  else if( proto == "udp" )
232  hints.ai_socktype = SOCK_DGRAM;
233  else
234  {
235  logInstance.err( LogAreaClassDns, "Unknown/Invalid protocol: " + proto );
236  }
237  memset( &hints, '\0', sizeof( hints ) );
238  hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME;
239  hints.ai_socktype = SOCK_STREAM;
240  int e = getaddrinfo( domain.c_str(), service.c_str(), &hints, res );
241  if( e )
242  logInstance.err( LogAreaClassDns, "getaddrinfo() failed" );
243  }
244 
245  int DNS::connect( const std::string& host, const LogSink& logInstance )
246  {
247  struct addrinfo* results = 0;
248 
249  resolve( &results, host, logInstance );
250  if( !results )
251  {
252  logInstance.err( LogAreaClassDns, "host not found: " + host );
253  return -ConnDnsError;
254  }
255 
256  struct addrinfo* runp = results;
257  while( runp )
258  {
259  int fd = DNS::connect( runp, logInstance );
260  if( fd >= 0 )
261  return fd;
262 
263  runp = runp->ai_next;
264  }
265 
266  freeaddrinfo( results );
267 
268  return -ConnConnectionRefused;
269  }
270 
271  int DNS::connect( struct addrinfo* res, const LogSink& logInstance )
272  {
273  if( !res )
274  return -1;
275 
276  int fd = getSocket( res->ai_family, res->ai_socktype, res->ai_protocol, logInstance );
277  if( fd < 0 )
278  return fd;
279 
280  if( ::connect( fd, res->ai_addr, res->ai_addrlen ) == 0 )
281  {
282  char ip[NI_MAXHOST];
283  char port[NI_MAXSERV];
284 
285  if( getnameinfo( res->ai_addr, sizeof( sockaddr ),
286  ip, sizeof( ip ),
287  port, sizeof( port ),
288  NI_NUMERICHOST | NI_NUMERICSERV ) )
289  {
290  //FIXME do we need to handle this? How? Can it actually happen at all?
291 // printf( "could not get numeric hostname");
292  }
293 
294  if( res->ai_canonname )
295  logInstance.dbg( LogAreaClassDns, "Connecting to " + std::string( res->ai_canonname )
296  + " (" + ip + "), port " + port );
297  else
298  logInstance.dbg( LogAreaClassDns, "Connecting to " + ip + ":" + port );
299 
300  return fd;
301  }
302 
303  std::string message = "connect() failed. "
304 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
305  "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
306 #else
307  "errno: " + util::int2string( errno ) + ": " + strerror( errno );
308 #endif
309  logInstance.dbg( LogAreaClassDns, message );
310 
311  closeSocket( fd, logInstance );
312  return -ConnConnectionRefused;
313  }
314 
315 #else
316 
317  int DNS::connect( const std::string& host, const LogSink& logInstance )
318  {
319  HostMap hosts = resolve( host, logInstance );
320  if( hosts.size() == 0 )
321  return -ConnDnsError;
322 
323  HostMap::const_iterator it = hosts.begin();
324  for( ; it != hosts.end(); ++it )
325  {
326  int fd = DNS::connect( (*it).first, (*it).second, logInstance );
327  if( fd >= 0 )
328  return fd;
329  }
330 
331  return -ConnConnectionRefused;
332  }
333 #endif
334 
335  int DNS::getSocket( const LogSink& logInstance )
336  {
337 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
338  WSADATA wsaData;
339  if( WSAStartup( MAKEWORD( 1, 1 ), &wsaData ) != 0 )
340  {
341  logInstance.dbg( LogAreaClassDns, "WSAStartup() failed. WSAGetLastError: "
342  + util::int2string( ::WSAGetLastError() ) );
343  return -ConnDnsError;
344  }
345 #endif
346 
347  int protocol = IPPROTO_TCP;
348  struct protoent* prot;
349  if( ( prot = getprotobyname( "tcp" ) ) != 0 )
350  {
351  protocol = prot->p_proto;
352  }
353  else
354  {
355  std::string message = "getprotobyname( \"tcp\" ) failed. "
356 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
357  "WSAGetLastError: " + util::int2string( ::WSAGetLastError() )
358 #else
359  "errno: " + util::int2string( errno ) + ": " + strerror( errno );
360 #endif
361  + ". Falling back to IPPROTO_TCP: " + util::int2string( IPPROTO_TCP );
362  logInstance.dbg( LogAreaClassDns, message );
363 
364  // Do not return an error. We'll fall back to IPPROTO_TCP.
365  }
366 
367  return getSocket( PF_INET, SOCK_STREAM, protocol, logInstance );
368  }
369 
370  int DNS::getSocket( int af, int socktype, int proto, const LogSink& logInstance )
371  {
372 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
373  SOCKET fd;
374 #else
375  int fd;
376 #endif
377  if( ( fd = socket( af, socktype, proto ) ) == INVALID_SOCKET )
378  {
379  std::string message = "getSocket( "
380  + util::int2string( af ) + ", "
381  + util::int2string( socktype ) + ", "
382  + util::int2string( proto )
383  + " ) failed. "
384 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
385  "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
386 #else
387  "errno: " + util::int2string( errno ) + ": " + strerror( errno );
388 #endif
389  logInstance.dbg( LogAreaClassDns, message );
390 
391  cleanup( logInstance );
392  return -ConnConnectionRefused;
393  }
394 
395 #ifdef HAVE_SETSOCKOPT
396  int timeout = 5000;
397  int reuseaddr = 1;
398  setsockopt( fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof( timeout ) );
399  setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseaddr, sizeof( reuseaddr ) );
400 #endif
401 
402  return (int)fd;
403  }
404 
405  int DNS::connect( const std::string& host, int port, const LogSink& logInstance )
406  {
407  int fd = getSocket( logInstance );
408  if( fd < 0 )
409  return fd;
410 
411  struct hostent* h;
412  if( ( h = gethostbyname( host.c_str() ) ) == 0 )
413  {
414  logInstance.dbg( LogAreaClassDns, "gethostbyname() failed for " + host + "." );
415  cleanup( logInstance );
416  closeSocket( fd, logInstance );
417  return -ConnDnsError;
418  }
419 
420  struct sockaddr_in target;
421  target.sin_family = AF_INET;
422  target.sin_port = htons( static_cast<unsigned short int>( port ) );
423 
424  if( h->h_length != sizeof( struct in_addr ) )
425  {
426  logInstance.dbg( LogAreaClassDns, "gethostbyname() returned unexpected structure." );
427  cleanup( logInstance );
428  closeSocket( fd, logInstance );
429  return -ConnDnsError;
430  }
431  else
432  {
433  memcpy( &target.sin_addr, h->h_addr, sizeof( struct in_addr ) );
434  }
435 
436  logInstance.dbg( LogAreaClassDns, "Connecting to " + host
437  + " (" + inet_ntoa( target.sin_addr ) + ":" + util::int2string( port ) + ")" );
438 
439  memset( target.sin_zero, '\0', 8 );
440  if( ::connect( fd, (struct sockaddr *)&target, sizeof( struct sockaddr ) ) == 0 )
441  {
442  logInstance.dbg( LogAreaClassDns, "Connected to " + host + " ("
443  + inet_ntoa( target.sin_addr ) + ":" + util::int2string( port ) + ")" );
444  return fd;
445  }
446 
447  std::string message = "Connection to " + host + " ("
448  + inet_ntoa( target.sin_addr ) + ":" + util::int2string( port ) + ") failed. "
449 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
450  "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
451 #else
452  "errno: " + util::int2string( errno ) + ": " + strerror( errno );
453 #endif
454  logInstance.dbg( LogAreaClassDns, message );
455 
456  closeSocket( fd, logInstance );
457  return -ConnConnectionRefused;
458  }
459 
460  void DNS::closeSocket( int fd, const LogSink& logInstance )
461  {
462 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
463  int result = closesocket( fd );
464 #else
465  int result = close( fd );
466 #endif
467 
468  if( result != 0 )
469  {
470  std::string message = "closeSocket() failed. "
471 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
472  "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
473 #else
474  "errno: " + util::int2string( errno ) + ": " + strerror( errno );
475 #endif
476  logInstance.dbg( LogAreaClassDns, message );
477  }
478  }
479 
480  void DNS::cleanup( const LogSink& logInstance )
481  {
482 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
483  if( WSACleanup() != 0 )
484  {
485  logInstance.dbg( LogAreaClassDns, "WSACleanup() failed. WSAGetLastError: "
486  + util::int2string( ::WSAGetLastError() ) );
487  }
488 #else
489  (void)logInstance;
490 #endif
491  }
492 
493 }