29 #include <arpa/inet.h>
37 #if (defined HAVE_LIBNL3) || (defined HAVE_LIBNL)
48 : _address(VADDR_ANY), _service(), _scope(), _family(AF_UNSPEC)
53 : _address(VADDR_ANY), _service(), _scope(), _family(family)
61 : _address(address), _service(), _scope(), _family(family)
69 : _address(address), _service(service), _scope(), _family(family)
73 vaddress::vaddress(
const std::string &address,
const std::string &service,
const std::string &scope, sa_family_t family)
74 : _address(address), _service(service), _scope(scope), _family(family)
84 if (_address < dhs._address)
return true;
85 if (_scope < dhs._scope)
return true;
92 if (_address != obj._address)
return true;
93 if (_scope != obj._scope)
return true;
99 if (_address != obj._address)
return false;
100 if (_scope != obj._scope)
return false;
109 const std::string n =
name();
113 if (pos == std::string::npos)
return false;
114 if ((n.length() - 1) == pos)
return true;
127 if (_family != AF_UNSPEC)
130 struct addrinfo hints;
131 memset(&hints, 0,
sizeof(
struct addrinfo));
132 hints.ai_family = PF_UNSPEC;
133 hints.ai_socktype = SOCK_DGRAM;
135 struct addrinfo *res;
143 address = this->
address().c_str();
146 service = this->
service().c_str();
149 if ((ret = ::getaddrinfo(address, service, &hints, &res)) != 0)
151 throw address_exception(
"getaddrinfo(): " + std::string(gai_strerror(ret)));
154 sa_family_t fam =
static_cast<sa_family_t
>(res->ai_family);
162 if (_scope.length() == 0)
throw scope_not_set();
168 if (_address.length() == 0)
throw address_not_set();
169 if (
isAny())
throw address_not_set();
175 struct addrinfo hints;
177 struct addrinfo *res;
180 memset(&hints, 0,
sizeof(
struct addrinfo));
181 hints.ai_family = PF_UNSPEC;
182 hints.ai_socktype = SOCK_STREAM;
184 if ((ret = ::getaddrinfo(_address.c_str(), NULL, &hints, &res)) != 0)
186 throw address_exception(
"getaddrinfo(): " + std::string(gai_strerror(ret)));
189 if ((ret = ::getnameinfo(res->ai_addr, res->ai_addrlen, (
char*)&addr_str, 256, NULL, 256, NI_NAMEREQD | NI_NOFQDN)) != 0)
192 throw address_exception(
"getnameinfo(): " + std::string(gai_strerror(ret)));
197 return std::string(addr_str);
202 if (_service.length() == 0)
throw service_not_set();
208 std::stringstream ss;
220 std::stringstream ss;
226 ss <<
"[" << address <<
"]:" +
service;
233 ss <<
" (" << scope <<
")";