9 static const char rcsid[] __attribute__((used)) =
"$Id: IPAddressPort.cpp 1664 2016-04-04 03:57:40Z sella $";
11 #include "IPAddressPort.h"
12 #include "../util/String.h"
16 #define HASH_ALGO_INT1
18 using namespace sella::net;
19 using namespace sella::util;
21 IPAddressPort::IPAddressPort(
const IPAddress &address,
const Port &port)
throw () :
27 IPAddressPort::IPAddressPort(
const IPAddress &address,
const TCPPort &port)
throw () :
30 init(address, (
Port) port);
33 IPAddressPort::IPAddressPort(
const IPAddress &address,
const UDPPort &port)
throw () :
36 init(address, (
Port) port);
39 IPAddressPort::IPAddressPort(
const IPAddress &address, uint16_t port)
throw (PortException) :
47 IPAddressPort::IPAddressPort(
const std::string &address, uint16_t port)
throw (AddressException, PortException) :
50 std::vector<std::string> matches;
52 std::string aaa = address;
53 std::string pat =
"^\\s*(\\[?([0-9\\.]+)\\]?|\\[([0-9A-Z:]+)\\]):([0-9]+)\\s*$";
55 if (String::regex_imatch(aaa, pat, matches)) {
56 const IPAddress a((matches.at(3).empty()) ? matches.at(2) : matches.at(3));
63 const Port p(matches.at(4));
67 }
else if (String::regex_imatch(address,
"^\\s*\\[?([0-9\\.]+|[0-9A-Z:]+)\\]?\\s*$", matches)) {
80 IPAddressPort::IPAddressPort(
const struct sockaddr_storage *ss)
throw (AddressException, PortException) :
89 IPAddressPort::IPAddressPort(
const struct sockaddr *sa)
throw (AddressException, PortException) :
98 IPAddressPort::IPAddressPort(
const IPAddressPort &other)
throw () :
99 address(other.address),
102 scache(other.scache),
106 IPAddressPort::IPAddressPort(
const IPAddressPort &&other) throw () :
107 address(std::move(other.address)),
108 port(std::move(other.port)),
110 scache(std::move(other.scache)),
114 IPAddressPort::~IPAddressPort() throw () { }
117 if (
this != &other) {
118 this->address = other.address;
119 this->port = other.port;
121 this->scache = other.scache;
122 this->hcache = other.hcache;
129 if (
this != &other) {
130 this->address = std::move(other.address);
131 this->port = std::move(other.port);
133 this->scache = std::move(other.scache);
134 this->hcache = other.hcache;
140 bool IPAddressPort::operator == (
const IPAddressPort &other)
const throw () {
141 return (this->port == other.port) ? ((this->address == other.address) ?
true :
false) :
false;
144 bool IPAddressPort::operator != (
const IPAddressPort &other)
const throw () {
145 return !(*
this == other);
148 bool IPAddressPort::operator >= (
const IPAddressPort &other)
const throw () {
149 return (this->address < other.address) ?
false : ((this->address > other.address) ?
true : (this->port >= other.port) ?
true :
false);
152 bool IPAddressPort::operator <= (
const IPAddressPort &other)
const throw () {
153 return (this->address > other.address) ?
false : ((this->address < other.address) ?
true : (this->port <= other.port) ?
true :
false);
156 bool IPAddressPort::operator > (
const IPAddressPort &other)
const throw () {
157 return (this->address > other.address) ?
true : ((this->address < other.address) ?
false : (this->port > other.port) ?
true :
false);
160 bool IPAddressPort::operator < (
const IPAddressPort &other)
const throw () {
161 return (this->address < other.address) ?
true : ((this->address > other.address) ?
false : (this->port < other.port) ?
true :
false);
164 int IPAddressPort::getFamily(
void)
const throw () {
165 return address.getFamily();
168 const IPAddress& IPAddressPort::getIPAddress(
void)
const throw () {
169 return this->address;
172 const Port& IPAddressPort::getPort(
void)
const throw () {
176 const struct sockaddr_storage& IPAddressPort::getSockaddrStorage(
void)
const throw () {
180 const struct sockaddr& IPAddressPort::getSockaddr(
void)
const throw () {
184 const struct sockaddr_in& IPAddressPort::getSockaddrIn(
void)
const throw () {
188 const struct sockaddr_in6& IPAddressPort::getSockaddrIn6(
void)
const throw () {
192 bool IPAddressPort::hasIPAddress(
void)
const throw () {
193 return (!address.empty());
196 bool IPAddressPort::hasPort(
void)
const throw () {
197 return (!port.empty());
200 const std::string& IPAddressPort::str(
void)
const throw (AddressException) {
201 if (scache.empty()) {
202 if (this->address.getFamily() == AF_INET) {
203 scache.append(this->address.str()).append(
":").append(this->port.str());
205 scache.append(
"[").append(this->address.str()).append(
"]:").append(this->port.str());
212 const char* IPAddressPort::c_str(
void)
const throw (AddressException) {
213 return str().c_str();
216 uint32_t IPAddressPort::getU32(
void)
const throw () {
217 return (address.getU32() ^ port.getU32());
220 uint32_t IPAddressPort::getHash(
void)
const throw () {
225 #if defined HASH_ALGO_INT1
226 uint32_t k = getU32();
228 hcache = ((k & 0xF0F0F0F0) >> 4) | ((k & 0x0F0F0F0F) << 4);
229 #elif defined HASH_ALGO_INT2
230 uint32_t k = getU32();
232 hcache = (k<<16)^(k>>16)^k;
233 #elif defined HASH_ALGO_INT3
236 #error No hash algo selected.
242 bool IPAddressPort::empty(
void)
const throw () {
243 return (port.empty() && address.empty());
246 const IPAddressPort::shared IPAddressPort::shared_ptr(
const IPAddress &address,
const Port &port)
throw () {
247 return std::make_shared<IPAddressPort>(address, port);
250 const IPAddressPort::shared IPAddressPort::shared_ptr(
const IPAddress &address,
const TCPPort &port)
throw () {
251 return std::make_shared<IPAddressPort>(address, port);
254 const IPAddressPort::shared IPAddressPort::shared_ptr(
const IPAddress &address,
const UDPPort &port)
throw () {
255 return std::make_shared<IPAddressPort>(address, port);
258 const IPAddressPort::shared IPAddressPort::shared_ptr(
const IPAddress &address, uint16_t port)
throw (PortException) {
259 return std::make_shared<IPAddressPort>(address, port);
262 const IPAddressPort::shared IPAddressPort::shared_ptr(
const std::string &address, uint16_t port)
throw (AddressException, PortException) {
263 return std::make_shared<IPAddressPort>(address, port);
266 const IPAddressPort::shared IPAddressPort::shared_ptr(
const struct sockaddr_storage *ss)
throw (AddressException, PortException) {
267 return std::make_shared<IPAddressPort>(ss);
270 const IPAddressPort::shared IPAddressPort::shared_ptr(
const struct sockaddr *sa)
throw (AddressException, PortException) {
271 return std::make_shared<IPAddressPort>(sa);
274 void IPAddressPort::init(
const IPAddress &address,
const Port &port)
throw () {
275 this->address = address;
280 u.sa.sa_family = address.getFamily();
281 if (u.sa.sa_family == AF_INET) {
282 memcpy(&(u.sa_in.sin_addr), &(address.getIPv4AddressStructure()),
sizeof(
struct in_addr));
284 memcpy(&(u.sa_in6.sin6_addr), &(address.getIPv6AddressStructure()),
sizeof(
struct in6_addr));
286 u.sa_in.sin_port = htons(port.get());