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;
90 NetworkService::NetworkService(
const char *name,
94 unsigned short int port,
95 const struct sockaddr *addr,
96 const socklen_t addr_size,
97 std::list<std::string> &txt)
100 _name = strdup(name);
101 _type = strdup(type);
102 _domain = strdup(domain);
103 _host = strdup(host);
106 _modified_name = NULL;
108 _addr = (
struct sockaddr *)malloc(addr_size);
109 memcpy(_addr, addr, addr_size);
110 _addr_size = 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;
155 unsigned short int port)
157 std::string s = name;
158 std::string::size_type hpos = s.find(
"%h");
159 if (nnresolver && (hpos != std::string::npos)) {
162 _name = strdup(s.c_str());
163 _type = strdup(type);
168 _modified_name = NULL;
180 NetworkService::NetworkService(
const char *name,
184 _name = strdup(name);
185 _type = strdup(type);
186 _domain = strdup(domain);
188 _modified_name = NULL;
198 NetworkService::~NetworkService()
200 if ( _name != NULL) free( _name );
201 if ( _type != NULL) free( _type );
202 if ( _domain != NULL) free( _domain );
203 if ( _host != NULL) free( _host );
204 if ( _addr != NULL) free( _addr );
205 if ( _modified_name != NULL) free( _modified_name );
215 _name = strdup(s->_name);
216 _type = strdup(s->_type);
218 if ( s->_domain != NULL ) {
219 _domain = strdup(s->_domain);
223 if ( s->_host != NULL ) {
224 _host = strdup(s->_host);
229 _modified_name = NULL;
230 if (s->_modified_name != NULL) {
231 _modified_name = strdup(s->_modified_name);
247 _name = strdup(s._name);
248 _type = strdup(s._type);
250 if ( s._domain != NULL ) {
251 _domain = strdup(s._domain);
255 if ( s._host != NULL ) {
256 _host = strdup(s._host);
261 _modified_name = NULL;
262 if (s._modified_name != NULL) {
263 _modified_name = strdup(s._modified_name);
278 NetworkService::add_txt(
const char *format, ...)
281 va_start(arg, format);
283 if (vasprintf(&tmp, format, arg) == -1) {
296 NetworkService::set_txt(std::list<std::string> &txtlist)
306 NetworkService::set_name(
const char *new_name)
309 _name = strdup(new_name);
317 NetworkService::name()
const
329 NetworkService::set_modified_name(
const char *new_name)
const
331 if (_modified_name) free(_modified_name);
332 _modified_name = strdup(new_name);
343 NetworkService::modified_name()
const
345 return _modified_name;
353 NetworkService::type()
const
363 NetworkService::domain()
const
373 NetworkService::host()
const
383 NetworkService::port()
const
394 NetworkService::addr_string()
const
396 char ipaddr[INET_ADDRSTRLEN];
397 struct sockaddr_in *saddr = (
struct sockaddr_in *)_addr;
398 return std::string(inet_ntop(AF_INET, &(saddr->sin_addr), ipaddr,
sizeof(ipaddr)));
405 const std::list<std::string> &
406 NetworkService::txt()
const
419 return ( (strcmp(_name, s._name) == 0) &&
420 (strcmp(_type, s._type) == 0) );
431 return ( (strcmp(_name, s->_name) == 0) &&
432 (strcmp(_type, s->_type) == 0) );
444 int typediff = strcmp(_type, s._type);
445 if ( typediff == 0 ) {
446 return (strcmp(_name, s._name) < 0);
448 return (typediff < 0);
const char * short_hostname()
Get short hostname.
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.