24 #include <arpa/inet.h>
25 #include <core/exceptions/system.h>
26 #include <netcomm/service_discovery/service.h>
27 #include <netcomm/utils/resolver.h>
28 #include <netinet/in.h>
29 #include <sys/types.h>
61 unsigned short int port)
69 _modified_name = NULL;
71 memset(&_addr, 0,
sizeof(_addr));
89 unsigned short int port,
90 const struct sockaddr * addr,
91 const socklen_t addr_size,
92 std::list<std::string> &txt)
100 _modified_name = NULL;
102 if (addr_size >
sizeof(_addr)) {
103 throw Exception(
"Address size too large");
105 memcpy(&_addr, addr, addr_size);
119 _name = strdup(
name);
120 _type = strdup(
type);
125 _modified_name = NULL;
127 memset(&_addr, 0,
sizeof(_addr));
145 unsigned short int port)
147 std::string s =
name;
148 std::string::size_type hpos = s.find(
"%h");
149 if (nnresolver && (hpos != std::string::npos)) {
152 _name = strdup(s.c_str());
153 _type = strdup(
type);
158 _modified_name = NULL;
160 memset(&_addr, 0,
sizeof(_addr));
171 _name = strdup(
name);
172 _type = strdup(
type);
175 _modified_name = NULL;
180 memset(&_addr, 0,
sizeof(_addr));
194 if (_modified_name != NULL)
195 free(_modified_name);
204 _name = strdup(s->_name);
205 _type = strdup(s->_type);
207 if (s->_domain != NULL) {
208 _domain = strdup(s->_domain);
212 if (s->_host != NULL) {
213 _host = strdup(s->_host);
218 _modified_name = NULL;
219 if (s->_modified_name != NULL) {
220 _modified_name = strdup(s->_modified_name);
223 memcpy(&_addr, &s->_addr,
sizeof(_addr));
234 _name = strdup(s._name);
235 _type = strdup(s._type);
237 if (s._domain != NULL) {
238 _domain = strdup(s._domain);
242 if (s._host != NULL) {
243 _host = strdup(s._host);
248 _modified_name = NULL;
249 if (s._modified_name != NULL) {
250 _modified_name = strdup(s._modified_name);
253 memcpy(&_addr, &s._addr,
sizeof(_addr));
273 if (_modified_name != NULL)
274 free(_modified_name);
280 _modified_name = NULL;
282 _name = strdup(s._name);
283 _type = strdup(s._type);
285 if (s._domain != NULL) {
286 _domain = strdup(s._domain);
290 if (s._host != NULL) {
291 _host = strdup(s._host);
296 _modified_name = NULL;
297 if (s._modified_name != NULL) {
298 _modified_name = strdup(s._modified_name);
301 memcpy(&_addr, &s._addr,
sizeof(_addr));
316 va_start(arg, format);
318 if (vasprintf(&tmp, format, arg) == -1) {
342 _name = strdup(new_name);
363 free(_modified_name);
364 _modified_name = strdup(new_name);
376 return _modified_name;
422 if (_addr.ss_family == AF_INET) {
423 char ipaddr[INET_ADDRSTRLEN];
424 struct sockaddr_in *saddr = (
struct sockaddr_in *)&_addr;
425 if (inet_ntop(AF_INET, &(saddr->sin_addr), ipaddr,
sizeof(ipaddr)) != NULL) {
428 throw Exception(
"Failed to convert IPv4 address to string");
430 }
else if (_addr.ss_family == AF_INET6) {
431 char ipaddr[INET6_ADDRSTRLEN];
432 struct sockaddr_in6 *saddr = (
struct sockaddr_in6 *)&_addr;
433 if (inet_ntop(AF_INET6, &(saddr->sin6_addr), ipaddr,
sizeof(ipaddr)) != NULL) {
436 throw Exception(
"Failed to convert IPv6 address to string");
439 throw Exception(
"Unknown address family");
446 const std::list<std::string> &
459 return ((strcmp(_name, s._name) == 0) && (strcmp(_type, s._type) == 0));
469 return ((strcmp(_name, s->_name) == 0) && (strcmp(_type, s->_type) == 0));
480 int typediff = strcmp(_type, s._type);
482 return (strcmp(_name, s._name) < 0);
484 return (typediff < 0);
Base class for exceptions in Fawkes.
Network name and address resolver.
const char * short_hostname()
Get short hostname.
Representation of a service announced or found via service discovery (i.e.
std::string addr_string() const
Get IP address of entry as string.
~NetworkService()
Destructor.
bool operator==(const NetworkService &s) const
Equal operator for NetworkService reference.
void set_modified_name(const char *new_name) const
Set modified name of service.
const char * modified_name() const
Get modified name of service.
const char * type() const
Get type of service.
void set_txt(std::list< std::string > &txtlist)
Set TXT records all at once.
void add_txt(const char *format,...)
Add a TXT record.
unsigned short int port() const
Get port of service.
NetworkService & operator=(const NetworkService &s)
Assignment operator.
void set_name(const char *new_name)
Set name of service.
NetworkService(const char *name, const char *type, const char *domain, const char *host, unsigned short int port)
Constructor.
const char * name() const
Get name of service.
const char * host() const
Get host of service.
bool operator<(const NetworkService &s) const
Less than operator.
const char * domain() const
Get domain of service.
const std::list< std::string > & txt() const
Get TXT record list of service.
System ran out of memory and desired operation could not be fulfilled.
Fawkes library namespace.