gloox  1.0
dns.h
1 /*
2  Copyright (c) 2005-2009 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 #ifndef DNS_H__
15 #define DNS_H__
16 
17 #include "macros.h"
18 #include "logsink.h"
19 
20 #ifdef __MINGW32__
21 # include <windows.h>
22 # include <windns.h>
23 #endif
24 
25 #ifdef HAVE_ARPA_NAMESER_H
26 # include <arpa/nameser.h>
27 #endif
28 
29 #ifdef __APPLE__
30 # include <arpa/nameser_compat.h>
31 #endif
32 
33 #ifndef NS_MAXDNAME
34 # define NS_MAXDNAME 1025
35 #endif
36 
37 #ifndef NS_PACKETSZ
38 # define NS_PACKETSZ 512
39 #endif
40 
41 #ifdef HAVE_GETADDRINFO
42 # include <sys/types.h>
43 # include <sys/socket.h>
44 # include <netdb.h>
45 #endif
46 
47 #include <string>
48 #include <map>
49 
50 namespace gloox
51 {
52 
61  class GLOOX_API DNS
62  {
63  public:
64 
68  typedef std::map<std::string, int> HostMap;
69 
79  static HostMap resolve( const std::string& service, const std::string& proto,
80  const std::string& domain, const LogSink& logInstance );
81 
90  static HostMap resolve( const std::string& domain, const LogSink& logInstance )
91  { return resolve( "xmpp-client", "tcp", domain, logInstance ); }
92 
100  static int connect( const std::string& host, const LogSink& logInstance );
101 
110  static int connect( const std::string& host, int port, const LogSink& logInstance );
111 
117  static int getSocket( const LogSink& logInstance );
118 
124  static void closeSocket( int fd, const LogSink& logInstance );
125 
126  private:
127 #ifdef HAVE_GETADDRINFO
128 
137  static void resolve( struct addrinfo** res, const std::string& service, const std::string& proto,
138  const std::string& domain, const LogSink& logInstance );
139 
147  static void resolve( struct addrinfo** res, const std::string& domain, const LogSink& logInstance )
148  { resolve( res, "xmpp-client", "tcp", domain, logInstance ); }
149 
156  static int connect( struct addrinfo* res, const LogSink& logInstance );
157 #endif
158 
165  static int getSocket( int af, int socktype, int proto, const LogSink& logInstance );
166 
167  static HostMap defaultHostMap( const std::string& domain, const LogSink& logInstance );
168  static void cleanup( const LogSink& logInstance );
169 
170  struct buffer
171  {
172  unsigned char buf[NS_PACKETSZ];
173  int len;
174  };
175  };
176 
177 }
178 
179 #endif // DNS_H__