finalize CSockAddress

pull/14/head
Tom Early 6 years ago
parent 093feae9a2
commit 304a8a7275

@ -18,10 +18,8 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#include <string.h> #include <iostream>
#include <strings.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <string>
class CSockAddress class CSockAddress
{ {
@ -45,32 +43,9 @@ public:
} }
} }
CSockAddress(const int family, const unsigned short port, const char *address) CSockAddress(const int family, const unsigned short port = 0, const char *address = NULL)
{ {
Clear(); Initialize(family, port, address);
if (AF_INET==family && address) {
auto addr4 = (struct sockaddr_in *)&addr;
addr4->sin_family = AF_INET;
addr4->sin_port = htons(port);
if (0 == strncasecmp(address, "loc", 3))
inet_pton(AF_INET, "127.0.0.1", &(addr4->sin_addr));
else if (0 == strncasecmp(address, "any", 3))
inet_pton(AF_INET, "0.0.0.0", &(addr4->sin_addr));
else
inet_pton(AF_INET, address, &(addr4->sin_addr));
inet_ntop(AF_INET, &(addr4->sin_addr), straddr, INET6_ADDRSTRLEN);
} else if (AF_INET6==family && address) {
auto addr6 = (struct sockaddr_in6 *)&addr;
addr6->sin6_family = AF_INET6;
addr6->sin6_port = htons(port);
if (0 == strncasecmp(address, "loc", 3))
inet_pton(AF_INET6, "::1", &(addr6->sin6_addr));
else if (0 == strncasecmp(address, "any", 3))
inet_pton(AF_INET6, "::", &(addr6->sin6_addr));
else
inet_pton(AF_INET6, address, &(addr6->sin6_addr));
inet_ntop(AF_INET6, &(addr6->sin6_addr), straddr, INET6_ADDRSTRLEN);
}
} }
~CSockAddress() {} ~CSockAddress() {}
@ -78,7 +53,7 @@ public:
void Initialize(const int family, const uint16_t port = 0U, const char *address = NULL) void Initialize(const int family, const uint16_t port = 0U, const char *address = NULL)
{ {
Clear(); Clear();
addr.ss_family = (sa_family_t)family; addr.ss_family = family;
if (AF_INET == family) { if (AF_INET == family) {
auto addr4 = (struct sockaddr_in *)&addr; auto addr4 = (struct sockaddr_in *)&addr;
addr4->sin_port = htons(port); addr4->sin_port = htons(port);
@ -87,11 +62,13 @@ public:
inet_pton(AF_INET, "127.0.0.1", &(addr4->sin_addr)); inet_pton(AF_INET, "127.0.0.1", &(addr4->sin_addr));
else if (0 == strncasecmp(address, "any", 3)) else if (0 == strncasecmp(address, "any", 3))
inet_pton(AF_INET, "0.0.0.0", &(addr4->sin_addr)); inet_pton(AF_INET, "0.0.0.0", &(addr4->sin_addr));
else else if (address) {
inet_pton(AF_INET, address, &(addr4->sin_addr)); if (1 > inet_pton(AF_INET, address, &(addr4->sin_addr)))
inet_ntop(AF_INET, &(addr4->sin_addr), straddr, INET6_ADDRSTRLEN); std::cerr << "Address Initialization Error: '" << address << "' is not a valdid IPV4 address!" << std::endl;
} }
} else { }
inet_ntop(AF_INET, &(addr4->sin_addr), straddr, INET6_ADDRSTRLEN);
} else if (AF_INET6 == family) {
auto addr6 = (struct sockaddr_in6 *)&addr; auto addr6 = (struct sockaddr_in6 *)&addr;
addr6->sin6_port = htons(port); addr6->sin6_port = htons(port);
if (address) { if (address) {
@ -99,11 +76,14 @@ public:
inet_pton(AF_INET6, "::1", &(addr6->sin6_addr)); inet_pton(AF_INET6, "::1", &(addr6->sin6_addr));
else if (0 == strncasecmp(address, "any", 3)) else if (0 == strncasecmp(address, "any", 3))
inet_pton(AF_INET6, "::", &(addr6->sin6_addr)); inet_pton(AF_INET6, "::", &(addr6->sin6_addr));
else else if (address) {
inet_pton(AF_INET6, address, &(addr6->sin6_addr)); if (1 > inet_pton(AF_INET6, address, &(addr6->sin6_addr)))
inet_ntop(AF_INET6, &(addr6->sin6_addr), straddr, INET6_ADDRSTRLEN); std::cerr << "Address Initialization Error: '" << address << "' is not a valid IPV6 address!" << std::endl;
} }
} }
inet_ntop(AF_INET6, &(addr6->sin6_addr), straddr, INET6_ADDRSTRLEN);
} else
std::cerr << "Error: Wrong address family type:" << family << " for [" << (address ? address : "NULL") << "']:" << port << std::endl;
} }
CSockAddress &operator=(const CSockAddress &from) CSockAddress &operator=(const CSockAddress &from)
@ -149,9 +129,11 @@ public:
if (AF_INET == addr.ss_family) { if (AF_INET == addr.ss_family) {
auto addr4 = (struct sockaddr_in *)&addr; auto addr4 = (struct sockaddr_in *)&addr;
addr4->sin_addr.s_addr = 0U; addr4->sin_addr.s_addr = 0U;
strcpy(straddr, "0.0.0.0");
} else { } else {
auto addr6 = (struct sockaddr_in6 *)&addr; auto addr6 = (struct sockaddr_in6 *)&addr;
memset(&(addr6->sin6_addr.s6_addr), 0, 16); memset(&(addr6->sin6_addr.s6_addr), 0, 16);
strcpy(straddr, "::");
} }
} }
@ -170,10 +152,11 @@ public:
if (AF_INET == addr.ss_family) { if (AF_INET == addr.ss_family) {
auto addr4 = (struct sockaddr_in *)&addr; auto addr4 = (struct sockaddr_in *)&addr;
return ntohs(addr4->sin_port); return ntohs(addr4->sin_port);
} else { } else if (AF_INET6 == addr.ss_family) {
auto addr6 = (struct sockaddr_in6 *)&addr; auto addr6 = (struct sockaddr_in6 *)&addr;
return ntohs(addr6->sin6_port); return ntohs(addr6->sin6_port);
} } else
return 0;
} }
void SetPort(const uint16_t newport) void SetPort(const uint16_t newport)
@ -181,7 +164,7 @@ public:
if (AF_INET == addr.ss_family) { if (AF_INET == addr.ss_family) {
auto addr4 = (struct sockaddr_in *)&addr; auto addr4 = (struct sockaddr_in *)&addr;
addr4->sin_port = newport; addr4->sin_port = newport;
} else { } else if (AF_INET6 == addr.ss_family) {
auto addr6 = (struct sockaddr_in6 *)&addr; auto addr6 = (struct sockaddr_in6 *)&addr;
addr6->sin6_port = newport; addr6->sin6_port = newport;
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.