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> 64 unsigned short int port)
68 _domain = strdup(domain);
72 _modified_name = NULL;
74 memset(&_addr, 0,
sizeof(_addr));
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);
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));
182 _name = strdup(name);
183 _type = strdup(type);
184 _domain = strdup(domain);
186 _modified_name = NULL;
191 memset(&_addr, 0,
sizeof(_addr));
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));
276 va_start(arg, format);
278 if (vasprintf(&tmp, format, arg) == -1) {
304 _name = strdup(new_name);
326 if (_modified_name) free(_modified_name);
327 _modified_name = strdup(new_name);
340 return _modified_name;
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> &
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 * host() const
Get host of service.
std::string addr_string() const
Get IP address of entry as string.
unsigned short int port() const
Get port of service.
const char * short_hostname()
Get short hostname.
void set_name(const char *new_name)
Set name of service.
Base class for exceptions in Fawkes.
void add_txt(const char *format,...)
Add a TXT record.
NetworkService(const char *name, const char *type, const char *domain, const char *host, unsigned short int port)
Constructor.
const char * modified_name() const
Get modified name of service.
Representation of a service announced or found via service discovery (i.e.
Network name and address resolver.
void set_txt(std::list< std::string > &txtlist)
Set TXT records all at once.
bool operator<(const NetworkService &s) const
Less than operator.
const std::list< std::string > & txt() const
Get TXT record list of service.
bool operator==(const NetworkService &s) const
Equal operator for NetworkService reference.
const char * domain() const
Get domain of service.
~NetworkService()
Destructor.
const char * type() const
Get type of service.
void set_modified_name(const char *new_name) const
Set modified name of service.
System ran out of memory and desired operation could not be fulfilled.
const char * name() const
Get name of service.