24 #include <netcomm/service_discovery/service.h> 25 #include <netcomm/utils/resolver.h> 26 #include <core/exceptions/system.h> 28 #include <sys/types.h> 29 #include <arpa/inet.h> 30 #include <netinet/in.h> 60 NetworkService::NetworkService(
const char *name,
64 unsigned short int port)
68 _domain = strdup(domain);
72 _modified_name = NULL;
74 memset(&_addr, 0,
sizeof(_addr));
89 NetworkService::NetworkService(
const char *name,
93 unsigned short int port,
94 const struct sockaddr *addr,
95 const socklen_t addr_size,
96 std::list<std::string> &txt)
100 _type = strdup(type);
101 _domain = strdup(domain);
102 _host = strdup(host);
105 _modified_name = NULL;
107 if (addr_size >
sizeof(_addr)) {
108 throw Exception(
"Address size too large");
110 memcpy(&_addr, addr, addr_size);
123 NetworkService::NetworkService(
const char *name,
125 unsigned short int port)
127 _name = strdup(name);
128 _type = strdup(type);
133 _modified_name = NULL;
135 memset(&_addr, 0,
sizeof(_addr));
154 unsigned short int port)
156 std::string s = name;
157 std::string::size_type hpos = s.find(
"%h");
158 if (nnresolver && (hpos != std::string::npos)) {
161 _name = strdup(s.c_str());
162 _type = strdup(type);
167 _modified_name = NULL;
169 memset(&_addr, 0,
sizeof(_addr));
178 NetworkService::NetworkService(
const char *name,
182 _name = strdup(name);
183 _type = strdup(type);
184 _domain = strdup(domain);
186 _modified_name = NULL;
191 memset(&_addr, 0,
sizeof(_addr));
196 NetworkService::~NetworkService()
198 if ( _name != NULL) free( _name );
199 if ( _type != NULL) free( _type );
200 if ( _domain != NULL) free( _domain );
201 if ( _host != NULL) free( _host );
202 if ( _modified_name != NULL) free( _modified_name );
212 _name = strdup(s->_name);
213 _type = strdup(s->_type);
215 if ( s->_domain != NULL ) {
216 _domain = strdup(s->_domain);
220 if ( s->_host != NULL ) {
221 _host = strdup(s->_host);
226 _modified_name = NULL;
227 if (s->_modified_name != NULL) {
228 _modified_name = strdup(s->_modified_name);
231 memcpy(&_addr, &s->_addr,
sizeof(_addr));
243 _name = strdup(s._name);
244 _type = strdup(s._type);
246 if ( s._domain != NULL ) {
247 _domain = strdup(s._domain);
251 if ( s._host != NULL ) {
252 _host = strdup(s._host);
257 _modified_name = NULL;
258 if (s._modified_name != NULL) {
259 _modified_name = strdup(s._modified_name);
262 memcpy(&_addr, &s._addr,
sizeof(_addr));
273 NetworkService::add_txt(
const char *format, ...)
276 va_start(arg, format);
278 if (vasprintf(&tmp, format, arg) == -1) {
291 NetworkService::set_txt(std::list<std::string> &txtlist)
301 NetworkService::set_name(
const char *new_name)
304 _name = strdup(new_name);
312 NetworkService::name()
const 324 NetworkService::set_modified_name(
const char *new_name)
const 326 if (_modified_name) free(_modified_name);
327 _modified_name = strdup(new_name);
338 NetworkService::modified_name()
const 340 return _modified_name;
348 NetworkService::type()
const 358 NetworkService::domain()
const 368 NetworkService::host()
const 378 NetworkService::port()
const 389 NetworkService::addr_string()
const 391 if (_addr.ss_family == AF_INET) {
392 char ipaddr[INET_ADDRSTRLEN];
393 struct sockaddr_in *saddr = (
struct sockaddr_in *)&_addr;
394 if (inet_ntop(AF_INET, &(saddr->sin_addr), ipaddr,
sizeof(ipaddr)) != NULL) {
397 throw Exception(
"Failed to convert IPv4 address to string");
399 }
else if (_addr.ss_family == AF_INET6) {
400 char ipaddr[INET6_ADDRSTRLEN];
401 struct sockaddr_in6 *saddr = (
struct sockaddr_in6 *)&_addr;
402 if (inet_ntop(AF_INET6, &(saddr->sin6_addr), ipaddr,
sizeof(ipaddr)) != NULL) {
405 throw Exception(
"Failed to convert IPv6 address to string");
408 throw Exception(
"Unknown address family");
416 const std::list<std::string> &
417 NetworkService::txt()
const 430 return ( (strcmp(_name, s._name) == 0) &&
431 (strcmp(_type, s._type) == 0) );
442 return ( (strcmp(_name, s->_name) == 0) &&
443 (strcmp(_type, s->_type) == 0) );
455 int typediff = strcmp(_type, s._type);
456 if ( typediff == 0 ) {
457 return (strcmp(_name, s._name) < 0);
459 return (typediff < 0);
Fawkes library namespace.
const char * short_hostname()
Get short hostname.
Base class for exceptions in Fawkes.
Representation of a service announced or found via service discovery (i.e.
Network name and address resolver.
System ran out of memory and desired operation could not be fulfilled.