43 #include <arpa/inet.h> 45 class EMANE::INETAddr::Implementation
49 Implementation{
"0.0.0.0",0}{}
51 Implementation(
const std::string & sEndPoint):
59 Implementation(
const std::string & sAddress,
60 std::uint16_t u16Port):
66 set(sAddress,u16Port);
69 Implementation(
const sockaddr_in & sockaddr):
70 pSockAddr_{
reinterpret_cast<struct sockaddr*
>(
new sockaddr_in)},
71 length_{
sizeof(sockaddr_in)},
75 memcpy(pSockAddr_.get(),
79 u16Port_ =
reinterpret_cast<sockaddr_in*
>(pSockAddr_.get())->
sin_port;
82 Implementation(
const sockaddr_in6 & sockaddr):
83 pSockAddr_{
reinterpret_cast<struct sockaddr*
>(
new sockaddr_in6)},
84 length_{
sizeof(sockaddr_in6)},
88 memcpy(pSockAddr_.get(),
92 u16Port_ =
reinterpret_cast<sockaddr_in6*
>(pSockAddr_.get())->sin6_port;
95 void set(
const std::string & sAddress,std::uint16_t u16Port)
97 addrinfo * pAddrInfo{};
100 memset(&hints,0,
sizeof(hints));
101 hints.ai_family = AF_UNSPEC;
102 hints.ai_socktype = 0;
107 if((iRet = getaddrinfo(sAddress.c_str(),
108 std::to_string(u16Port).c_str(),
112 throw INETAddrException(gai_strerror(iRet));
115 if(pAddrInfo->ai_family == AF_INET)
117 pSockAddr_.reset(reinterpret_cast<sockaddr*>(
new sockaddr_in));
118 memcpy(pSockAddr_.get(),
119 reinterpret_cast<sockaddr_in*
>(pAddrInfo->ai_addr),
120 sizeof(sockaddr_in));
122 else if(pAddrInfo->ai_family == AF_INET6)
124 pSockAddr_.reset(reinterpret_cast<sockaddr*>(
new sockaddr_in6));
125 memcpy(pSockAddr_.get(),
126 reinterpret_cast<sockaddr_in6*
>(pAddrInfo->ai_addr),
127 sizeof(sockaddr_in6));
131 freeaddrinfo(pAddrInfo);
132 throw INETAddrException(
"Unknown family");
135 iFamily_ = pAddrInfo->ai_family;
136 length_ = pAddrInfo->ai_addrlen;
139 freeaddrinfo(pAddrInfo);
142 void set(
const std::string & sEndPoint)
144 if(sEndPoint.empty())
146 throw INETAddrException(
"Empty endpoint string");
149 size_t pos = sEndPoint.rfind(
"/");
151 if(pos == std::string::npos)
155 if(std::count(sEndPoint.begin(),
158 sEndPoint.front() ==
'[')
160 pos = sEndPoint.rfind(
":");
164 std::string sAddress{sEndPoint};
165 std::uint16_t u16Port{};
167 if(pos != std::string::npos)
169 sAddress = sEndPoint.substr(0,pos);
170 u16Port = Utils::ParameterConvert(sEndPoint.substr(pos+1)).toUINT16();
174 if(!sAddress.empty())
176 if(sAddress.front() ==
'[' &&
177 sAddress.back() ==
']')
179 sAddress = sAddress.substr(1,sAddress.size() - 2);
184 set(sAddress,u16Port);
188 Implementation(
const Implementation & address)
193 Implementation &
operator=(
const Implementation & address)
201 return iFamily_ == AF_INET6;
206 return iFamily_ == AF_INET;
218 return reinterpret_cast<sockaddr_in*
>(pSockAddr_.get())->
sin_addr.s_addr == INADDR_ANY;
222 return IN6_IS_ADDR_UNSPECIFIED(&reinterpret_cast<sockaddr_in6 *>(pSockAddr_.get())->sin6_addr);
231 return reinterpret_cast<sockaddr_in6 *
>(pSockAddr_.get())->sin6_addr.s6_addr[0] == 0xFF;
235 return ntohl(reinterpret_cast<sockaddr_in*>(pSockAddr_.get())->
sin_addr.s_addr) >= 0xE0000000 &&
236 ntohl(reinterpret_cast<sockaddr_in*>(pSockAddr_.get())->
sin_addr.s_addr) <= 0xEFFFFFFF;
242 return pSockAddr_.get();
255 std::string
str(
bool bWithPort)
const 257 std::string sCharacterString{};
261 char buf[INET_ADDRSTRLEN];
264 &reinterpret_cast<sockaddr_in*>(pSockAddr_.get())->
sin_addr,
270 sCharacterString = std::string{buf} +
":" + std::to_string(u16Port_);
274 sCharacterString = std::string{buf};
279 char buf[INET6_ADDRSTRLEN];
282 &reinterpret_cast<sockaddr_in6 *>(pSockAddr_.get())->sin6_addr,
288 sCharacterString = std::string{
"["} + buf +
"]:" + std::to_string(u16Port_);
292 sCharacterString = std::string{buf};
296 return sCharacterString;
300 std::unique_ptr<sockaddr> pSockAddr_;
303 std::uint16_t u16Port_;
305 void copy(
const Implementation & address)
307 iFamily_ = address.iFamily_;
308 length_ = address.length_;
309 u16Port_ = address.u16Port_;
311 if(address.iFamily_ == AF_INET)
313 pSockAddr_.reset(reinterpret_cast<sockaddr*>(
new sockaddr_in));
314 memcpy(pSockAddr_.get(),
315 reinterpret_cast<sockaddr_in*
>(address.pSockAddr_.get()),
316 sizeof(sockaddr_in));
320 pSockAddr_.reset(reinterpret_cast<sockaddr*>(
new sockaddr_in6));
321 memcpy(pSockAddr_.get(),
322 reinterpret_cast<sockaddr_in6*
>(address.pSockAddr_.get()),
323 sizeof(sockaddr_in6));
329 pImpl_{
new Implementation{sockaddr}}{}
332 pImpl_{
new Implementation{sockaddr}}{}
335 pImpl_{
new Implementation{}}{}
338 pImpl_{
new Implementation{sEndPoint}}{}
341 std::uint16_t u16Port):
342 pImpl_{
new Implementation{sAddress,u16Port}}{}
346 pImpl_.reset(
new Implementation(*address.pImpl_.get()));
351 pImpl_.reset(
new Implementation(*address.pImpl_.get()));
357 pImpl_.swap(address.pImpl_);
362 pImpl_.swap(address.pImpl_);
369 std::uint16_t u16Port)
371 pImpl_->set(sAddress,u16Port);
376 pImpl_->set(sEndPoint);
381 return pImpl_->isIPv6();
386 return pImpl_->isIPv4();
391 return pImpl_->isAny();
396 return pImpl_->getFamily();
401 return pImpl_->getSockAddr();
406 return pImpl_->getAddrLength();
411 return pImpl_->str(bWithPort);
416 return pImpl_->getPort();
socklen_t getAddrLength() const
std::uint16_t getPort() const
sockaddr * getSockAddr() const
std::string str(bool bWithPort=true) const
void set(const std::string &sAddress, std::uint16_t u16Port)
INETAddr & operator=(const INETAddr &address)