Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
beef9999 committed Oct 12, 2023
1 parent 65ee4da commit cbbe9a6
Showing 1 changed file with 0 additions and 37 deletions.
37 changes: 0 additions & 37 deletions net/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,29 @@ namespace net {
in6_addr addr = {};
struct { uint16_t _1, _2, _3, _4, _5, _6; uint8_t a, b, c, d; };
};

// For compatibility, the default constructor is still 0.0.0.0 (IPv4)
IPAddr() {
map_v4(htonl(INADDR_ANY));
}

// V6 constructor (Internet Address)
explicit IPAddr(in6_addr internet_addr) {
addr = internet_addr;
}

// V6 constructor (Network byte order)
IPAddr(uint32_t nl1, uint32_t nl2, uint32_t nl3, uint32_t nl4) {
addr._in_addr_field[0] = nl1;
addr._in_addr_field[1] = nl2;
addr._in_addr_field[2] = nl3;
addr._in_addr_field[3] = nl4;
}

// V4 constructor (Internet Address)
explicit IPAddr(in_addr internet_addr) {
map_v4(internet_addr);
}

// V4 constructor (Network byte order)
explicit IPAddr(uint32_t nl) {
map_v4(nl);
}

// String constructor
explicit IPAddr(const char* s) {
if (inet_pton(AF_INET6, s, &addr) > 0) {
Expand All @@ -91,7 +85,6 @@ namespace net {
// Invalid string, make it a default value
*this = IPAddr();
}

// Check if it's actually an IPv4 address mapped in IPV6
bool is_ipv4() const {
if (ntohl(addr._in_addr_field[2]) != 0x0000ffff) {
Expand All @@ -102,42 +95,34 @@ namespace net {
}
return true;
}

// We regard the default IPv4 0.0.0.0 as undefined
bool undefined() const {
return *this == V4Any();
}

// Should ONLY be used for IPv4 address
uint32_t to_nl() const {
return addr._in_addr_field[3];
}

bool is_loopback() const {
return is_ipv4() ? (*this == V4Loopback()) : (*this == V6Loopback());
}

bool is_broadcast() const {
// IPv6 does not support broadcast
return is_ipv4() && (*this == V4Broadcast());
}

bool is_link_local() const {
if (is_ipv4()) {
return (to_nl() & htonl(0xffff0000)) == htonl(0xa9fe0000);
} else {
return (addr._in_addr_field[0] & htonl(0xffc00000)) == htonl(0xfe800000);
}
}

bool operator==(const IPAddr& rhs) const {
return memcmp(this, &rhs, sizeof(rhs)) == 0;
}

bool operator!=(const IPAddr& rhs) const {
return !(*this == rhs);
}

std::string to_string() const {
std::string str;
char text[INET6_ADDRSTRLEN];
Expand All @@ -151,27 +136,19 @@ namespace net {
str.assign(text, strlen(text));
return str;
}

public:
static IPAddr V6None() {
return IPAddr(htonl(0xffffffff), htonl(0xffffffff), htonl(0xffffffff), htonl(0xffffffff));
}

static IPAddr V6Any() { return IPAddr(in6addr_any); }

static IPAddr V6Loopback() { return IPAddr(in6addr_loopback); }

static IPAddr V4Broadcast() { return IPAddr(htonl(INADDR_BROADCAST)); }

static IPAddr V4Any() { return IPAddr(htonl(INADDR_ANY)); }

static IPAddr V4Loopback() { return IPAddr(htonl(INADDR_LOOPBACK)); }

private:
void map_v4(in_addr addr_) {
map_v4(addr_.s_addr);
}

void map_v4(uint32_t nl) {
addr._in_addr_field[0] = 0x00000000;
addr._in_addr_field[1] = 0x00000000;
Expand All @@ -185,23 +162,17 @@ namespace net {
struct __attribute__ ((packed)) EndPoint {
IPAddr addr;
uint16_t port = 0;

EndPoint() = default;

EndPoint(IPAddr ip, uint16_t port) : addr(ip), port(port) {}

bool is_ipv4() const {
return addr.is_ipv4();
};

bool operator==(const EndPoint& rhs) const {
return rhs.addr == addr && rhs.port == port;
}

bool operator!=(const EndPoint& rhs) const {
return !operator==(rhs);
}

bool undefined() const {
return addr.undefined() && port == 0;
}
Expand All @@ -211,7 +182,6 @@ namespace net {

struct sockaddr_storage {
sockaddr_storage() = default;

explicit sockaddr_storage(const EndPoint& ep) {
if (ep.is_ipv4()) {
auto* in4 = (sockaddr_in*) &store;
Expand All @@ -225,19 +195,15 @@ namespace net {
in6->sin6_addr = ep.addr.addr;
}
}

explicit sockaddr_storage(const sockaddr_in& addr) {
*((sockaddr_in*) &store) = addr;
}

explicit sockaddr_storage(const sockaddr_in6& addr) {
*((sockaddr_in6*) &store) = addr;
}

explicit sockaddr_storage(const sockaddr& addr) {
*((sockaddr*) &store) = addr;
}

EndPoint to_endpoint() const {
EndPoint ep;
if (store.ss_family == AF_INET6) {
Expand All @@ -251,11 +217,9 @@ namespace net {
}
return ep;
}

sockaddr* get_sockaddr() const {
return (sockaddr*) &store;
}

socklen_t get_socklen() const {
switch (store.ss_family) {
case AF_INET:
Expand All @@ -266,7 +230,6 @@ namespace net {
return 0;
}
}

// store must be zero initialized
::sockaddr_storage store = {};
};
Expand Down

0 comments on commit cbbe9a6

Please sign in to comment.