Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * resolver.h - Fawkes network name resolver 00004 * 00005 * Created: Tue Nov 14 14:25:52 2006 00006 * Copyright 2006-2009 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #ifndef __NETCOMM_UTILS_RESOLVER_H_ 00025 #define __NETCOMM_UTILS_RESOLVER_H_ 00026 00027 #include <core/utils/lock_hashmap.h> 00028 #include <core/utils/lock_map.h> 00029 #include <utils/misc/string_compare.h> 00030 00031 #include <sys/socket.h> 00032 #include <netinet/in.h> 00033 #include <cstddef> 00034 00035 #include <ctime> 00036 #include <string> 00037 #include <utility> 00038 00039 namespace fawkes { 00040 #if 0 /* just to make Emacs auto-indent happy */ 00041 } 00042 #endif 00043 00044 class AvahiThread; 00045 class NetworkNameResolverThread; 00046 class HostInfo; 00047 00048 class NetworkNameResolver 00049 { 00050 friend class NetworkNameResolverThread; 00051 00052 public: 00053 NetworkNameResolver(AvahiThread *avahi_thread = NULL); 00054 ~NetworkNameResolver(); 00055 00056 bool resolve_name(const char *name, struct sockaddr **addr, socklen_t *addrlen); 00057 bool resolve_name_blocking(const char *name, struct sockaddr **addr, socklen_t *addrlen); 00058 bool resolve_address(struct sockaddr *addr, socklen_t addr_len, std::string &name); 00059 00060 void flush_cache(); 00061 void set_cache_timeout(unsigned int sec); 00062 unsigned int cache_timeout(); 00063 00064 const char * hostname(); 00065 const char * short_hostname(); 00066 00067 private: 00068 void name_resolved(char *name, struct sockaddr *addr, socklen_t addrlen); 00069 void addr_resolved(struct sockaddr *addr, socklen_t addrlen, char *name, bool namefound); 00070 void name_resolution_failed(char *name); 00071 void address_resolution_failed(struct sockaddr *addr, socklen_t addrlen); 00072 00073 private: 00074 NetworkNameResolverThread *resolver_thread; 00075 HostInfo *__host_info; 00076 unsigned int __cache_timeout; 00077 00078 LockHashMap<uint32_t, std::pair<char *, time_t> > addr2name_cache; 00079 LockHashMap<char *, 00080 std::pair<struct sockaddr *, time_t>, 00081 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2) 00082 std::tr1::hash<char *>, 00083 #else 00084 __gnu_cxx::hash<char *>, 00085 #endif 00086 StringEquality > name2addr_cache; 00087 00088 LockHashMap<uint32_t, std::pair<char *, time_t> >::iterator a2ncit; 00089 LockHashMap<char *, std::pair<struct sockaddr *, time_t>, 00090 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2) 00091 std::tr1::hash<char *>, 00092 #else 00093 __gnu_cxx::hash<char *>, 00094 #endif 00095 StringEquality >::iterator n2acit; 00096 }; 00097 00098 } // end namespace fawkes 00099 00100 #endif