Fawkes API  Fawkes Development Version
resolver.h
1 
2 /***************************************************************************
3  * resolver.h - Fawkes network name resolver
4  *
5  * Created: Tue Nov 14 14:25:52 2006
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __NETCOMM_UTILS_RESOLVER_H_
25 #define __NETCOMM_UTILS_RESOLVER_H_
26 
27 #include <core/utils/lock_hashmap.h>
28 #include <core/utils/lock_map.h>
29 #include <utils/misc/string_compare.h>
30 
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <cstddef>
34 
35 #include <ctime>
36 #include <string>
37 #include <utility>
38 
39 namespace fawkes {
40 #if 0 /* just to make Emacs auto-indent happy */
41 }
42 #endif
43 
44 class AvahiThread;
45 class NetworkNameResolverThread;
46 class HostInfo;
47 
49 {
50  friend class NetworkNameResolverThread;
51 
52  public:
53  NetworkNameResolver(AvahiThread *avahi_thread = NULL);
55 
56  bool resolve_name(const char *name, struct sockaddr **addr, socklen_t *addrlen);
57  bool resolve_name_blocking(const char *name, struct sockaddr **addr, socklen_t *addrlen);
58  bool resolve_address(struct sockaddr *addr, socklen_t addr_len, std::string &name);
59 
60  void flush_cache();
61  void set_cache_timeout(unsigned int sec);
62  unsigned int cache_timeout();
63 
64  const char * hostname();
65  const char * short_hostname();
66 
67  private:
68  void name_resolved(char *name, struct sockaddr *addr, socklen_t addrlen);
69  void addr_resolved(struct sockaddr *addr, socklen_t addrlen, char *name, bool namefound);
70  void name_resolution_failed(char *name);
71  void address_resolution_failed(struct sockaddr *addr, socklen_t addrlen);
72 
73  private:
74  NetworkNameResolverThread *resolver_thread;
75  HostInfo *__host_info;
76  unsigned int __cache_timeout;
77 
79  LockHashMap<char *,
80  std::pair<struct sockaddr *, time_t>,
81 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
82  std::tr1::hash<char *>,
83 #else
84  __gnu_cxx::hash<char *>,
85 #endif
86  StringEquality > name2addr_cache;
87 
90 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
91  std::tr1::hash<char *>,
92 #else
93  __gnu_cxx::hash<char *>,
94 #endif
95  StringEquality >::iterator n2acit;
96 };
97 
98 } // end namespace fawkes
99 
100 #endif
String equality checker.
Fawkes library namespace.
Worker thread for NetworkNameResolver.
Host information.
Definition: hostinfo.h:31
Avahi main thread.
Definition: avahi_thread.h:55
Network name and address resolver.
Definition: resolver.h:48
Hash map with a lock.
Definition: lock_hashmap.h:47