Fawkes API
Fawkes Development Version
|
Network name and address resolver. More...
#include <>>
Public Member Functions | |
NetworkNameResolver (AvahiThread *avahi_thread=NULL) | |
Constructor. More... | |
~NetworkNameResolver () | |
Destructor. More... | |
bool | resolve_name (const char *name, struct sockaddr **addr, socklen_t *addrlen) |
Resolve name. More... | |
bool | resolve_name_blocking (const char *name, struct sockaddr **addr, socklen_t *addrlen) |
Resolve name and wait for the result. More... | |
bool | resolve_address (struct sockaddr *addr, socklen_t addr_len, std::string &name) |
Resolve address. More... | |
void | flush_cache () |
Flush cache. More... | |
void | set_cache_timeout (unsigned int sec) |
Set cache timeout. More... | |
unsigned int | cache_timeout () |
Get cache timeout. More... | |
const char * | hostname () |
Get long hostname. More... | |
const char * | short_hostname () |
Get short hostname. More... | |
Friends | |
class | NetworkNameResolverThread |
Network name and address resolver.
This class implements a facility to resolve host names to addresses and vice versa. It provides a simplified interface that only supports IPv4 and will return only the first answer received. It has optional support for using mDNS via Avahi for lookups on the local network in the .local domain.
Quite some effort has been done to ensure the speediness of this implementation. It is assumed that a fast lookup is the most important thing for the resolver. This means especially that under some circumstances no result can be supplied on the first call but just on a subsequent call (it is not defined if or when a result will be available). So it is a good choice to always call the resolver and let it do the right thing and not cache names and addresses in your own application. This also makes the cache more efficient since multiple threads may use this single resolver. The resolver itself holds a resolver thread that will do lookups concurrently which will update the cache with results thus that subsequent calls will provide the correct information from the cache.
The resolver uses an internal cache of name to address and addres to name mapping. If a valid lookup has happened this mapping is assumed to be authoritative and that it will not change. If you want to flush the cache from time to time you may use flush_cache() to do so.
In general resolve_name() and resolve_address() immediately return. If no answer is in the cache for resolve_address() it will just provide the textual representation of the IP address. This is different for resolve_name(). If no answer is available if will order a concurrent lookup and return a lookup failure. Subsequent calls may succeed if the cache was successfully updated by the concurrent resolver thread. If you need the answer to be able to proceed use resolve_name_blocking(). This will wait until an answer is available via the host lookup facilities of the system or optional via mDNS.
Definition at line 48 of file resolver.h.
fawkes::NetworkNameResolver::NetworkNameResolver | ( | AvahiThread * | avahi_thread = NULL | ) |
Constructor.
This constructor us available if Avahi is available at compile time.
avahi_thread | Optional avahi thread, Avahi is not used if NULL |
Definition at line 89 of file resolver.cpp.
References fawkes::Thread::start().
fawkes::NetworkNameResolver::~NetworkNameResolver | ( | ) |
Destructor.
Definition at line 105 of file resolver.cpp.
References fawkes::Thread::cancel(), flush_cache(), and fawkes::Thread::join().
unsigned int fawkes::NetworkNameResolver::cache_timeout | ( | ) |
Get cache timeout.
Definition at line 131 of file resolver.cpp.
void fawkes::NetworkNameResolver::flush_cache | ( | ) |
Flush cache.
Flushes the caches for name to address and address to name mappings.
Definition at line 141 of file resolver.cpp.
References fawkes::LockHashMap< KeyType, ValueType, HashFunction, EqualKey >::lock(), fawkes::LockHashMap< KeyType, ValueType, HashFunction, EqualKey >::unlock(), and fawkes::HostInfo::update().
Referenced by ~NetworkNameResolver().
const char * fawkes::NetworkNameResolver::hostname | ( | ) |
Get long hostname.
Definition at line 361 of file resolver.cpp.
References fawkes::HostInfo::name().
bool fawkes::NetworkNameResolver::resolve_address | ( | struct sockaddr * | addr, |
socklen_t | addr_len, | ||
std::string & | name | ||
) |
Resolve address.
This will lookup an address from the cache and return the value if available. If there is no entry in the cache this will order a concurrent lookup of the address and return the textual representation of the address.
addr | address to resolve |
addr_len | length of addr in bytes |
name | contains a pointer to the name upon return. Note that this record resides in the cache and may not be freed. |
Definition at line 244 of file resolver.cpp.
References fawkes::LockHashMap< KeyType, ValueType, HashFunction, EqualKey >::lock(), fawkes::NetworkNameResolverThread::resolve_address(), and fawkes::LockHashMap< KeyType, ValueType, HashFunction, EqualKey >::unlock().
bool fawkes::NetworkNameResolver::resolve_name | ( | const char * | name, |
struct sockaddr ** | addr, | ||
socklen_t * | addrlen | ||
) |
Resolve name.
This will lookup a name from the cache and return the value if available. If there is no entry in the cache this will order a concurrent lookup of the name an return a failure.
name | name to resolve |
addr | contains a pointer to the address record upon return, this record is in the cache, so you may not free the resulting address! The address is always of type struct sockaddr_in (IPv4) at the moment. |
addrlen | contains the length of addr in bytes upon return |
Definition at line 177 of file resolver.cpp.
References fawkes::LockHashMap< KeyType, ValueType, HashFunction, EqualKey >::lock(), fawkes::NetworkNameResolverThread::resolve_name(), and fawkes::LockHashMap< KeyType, ValueType, HashFunction, EqualKey >::unlock().
Referenced by resolve_name_blocking().
bool fawkes::NetworkNameResolver::resolve_name_blocking | ( | const char * | name, |
struct sockaddr ** | addr, | ||
socklen_t * | addrlen | ||
) |
Resolve name and wait for the result.
This will lookup a name from the cache and return the value if available. If there is no entry in the cache this will order a concurrent lookup of the name and wait for the result.
name | name to resolve |
addr | contains a pointer to the address record upon return, this record is in the cache, so you may not free the resulting address! The address is always of type struct sockaddr_in (IPv4) at the moment. |
addrlen | contains the length of addr in bytes upon return |
Definition at line 213 of file resolver.cpp.
References resolve_name(), and fawkes::NetworkNameResolverThread::resolve_name_immediately().
Referenced by PlayerClientThread::init().
void fawkes::NetworkNameResolver::set_cache_timeout | ( | unsigned int | sec | ) |
Set cache timeout.
The apply only applies to consecutive lookups, existing entries will expire with the old timeout.
sec | the timeout in seconds determines after which time successful resolutions are purged from the cache. |
Definition at line 121 of file resolver.cpp.
const char * fawkes::NetworkNameResolver::short_hostname | ( | ) |
Get short hostname.
Definition at line 371 of file resolver.cpp.
References fawkes::HostInfo::short_name().
Referenced by FountainThread::init(), and fawkes::NetworkService::NetworkService().