21#ifndef HEADER_SOCKET_ADDRESS_HPP
22#define HEADER_SOCKET_ADDRESS_HPP
31# include <netinet/in.h>
32# include <sys/socket.h>
49 std::array<char,
sizeof(sockaddr_storage)> m_sockaddr;
54 static bool g_ignore_error_message;
64 SocketAddress(
const std::string& str, uint16_t port_number = 0,
65 short family = AF_UNSPEC)
67 init(str, port_number, family);
76 void init(
const std::string& str, uint16_t port_number = 0,
77 short family = AF_UNSPEC);
85 if (m_family == AF_INET &&
getIP() == 0)
95 sockaddr_in* in = (sockaddr_in*)m_sockaddr.data();
96 in->sin_family = AF_INET;
99 uint32_t
getIP()
const;
103 void setIP(uint32_t ip);
107 std::string toString(
bool show_port =
true)
const;
109 sockaddr* getSockaddr()
const {
return (sockaddr*)m_sockaddr.data(); }
111 socklen_t getSocklen()
const
113 if (m_family == AF_INET)
114 return sizeof(sockaddr_in);
115 else if (m_family == AF_INET6)
116 return sizeof(sockaddr_in6);
120 void setSockAddrIn(
short family, sockaddr* new_sockaddr, socklen_t len)
124 memcpy(m_sockaddr.data(), new_sockaddr, len);
127 short getFamily()
const {
return m_family; }
129 void setIPv6(
const uint8_t* bytes, uint16_t port = 0)
133 sockaddr_in6* in6 = (sockaddr_in6*)m_sockaddr.data();
134 in6->sin6_family = AF_INET6;
135 memcpy(in6->sin6_addr.s6_addr, bytes, 16);
145 if (m_family == AF_INET6)
156 void convertForIPv6Socket(
bool ipv6);
158 ENetAddress toENetAddress()
const;
Describes a IPv4 or IPv6 address in sockaddr_in(6) format, suitable in using with sendto.
Definition: socket_address.hpp:47
bool isLAN() const
Returns if this IP address belongs to a LAN, i.e.
Definition: socket_address.cpp:495
void setPort(uint16_t port)
Set the port.
Definition: socket_address.cpp:309
void setIP(uint32_t ip)
Sets the ip address.
Definition: socket_address.cpp:296
uint32_t getIP() const
Returns the IPv4 address in decimal, it will handle IPv4 mapped IPv6 address too.
Definition: socket_address.cpp:260
bool isLoopback() const
Returns if this IP is loopback (ie for IPv4 127.0.0.0/8, IPv6 ::1/128)
Definition: socket_address.cpp:452
static void unitTesting()
Unit testing.
Definition: socket_address.cpp:623
void init(const std::string &str, uint16_t port_number=0, short family=AF_UNSPEC)
String initialization (Can be IPv4, IPv6 or domain).
Definition: socket_address.cpp:120
uint16_t getPort() const
Returns the port number.
Definition: socket_address.cpp:279
void clear()
Resets ip and port to 0.
Definition: socket_address.hpp:91
bool operator!=(const SocketAddress &other) const
Compares if ip address and port are not identical.
Definition: socket_address.cpp:552
bool isPublicAddressLocalhost() const
Returns this IP address is localhost which its equal to any interface address.
Definition: socket_address.cpp:326
bool operator==(const SocketAddress &other) const
Compares if ip address and port are identical.
Definition: socket_address.cpp:532
bool isIPv6() const
Return true if this is an IPv6 address, if it's an IPv4 mapped IPv6 address it will return false (::f...
Definition: socket_address.hpp:143
Declares the general types that are used by the network.