10 #ifndef NETWORK_CORE_ADDRESS_H 11 #define NETWORK_CORE_ADDRESS_H 15 #include "../../string_func.h" 16 #include "../../core/smallmap_type.hpp" 49 address_length(address_length),
51 resolved(address_length != 0)
53 *this->hostname =
'\0';
62 address_length(address_length),
63 resolved(address_length != 0)
65 *this->hostname =
'\0';
66 memset(&this->address, 0,
sizeof(this->address));
67 memcpy(&this->address, address, address_length);
76 NetworkAddress(
const char *hostname =
"", uint16 port = 0,
int family = AF_UNSPEC) :
81 if (
StrEmpty(hostname)) hostname =
"";
82 if (*hostname ==
'[') hostname++;
84 char *tmp = strrchr(this->hostname,
']');
85 if (tmp !=
nullptr) *tmp =
'\0';
87 memset(&this->address, 0,
sizeof(this->address));
88 this->address.ss_family = family;
131 if (r == 0) r = this->address.ss_family - address.
address.ss_family;
132 if (r == 0) r = memcmp(&this->address, &address.
address, this->address_length);
Network stuff has many things that needs to be included and/or implemented by default.
int CompareTo(NetworkAddress &address)
Compare the address of this class with the address of another.
sockaddr_storage address
The resolved address.
bool operator==(NetworkAddress &address)
Compare the address of this class with the address of another.
SmallMap< NetworkAddress, SOCKET > SocketList
Type for a mapping between address and socket.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
NetworkAddress(struct sockaddr_storage &address, int address_length)
Create a network address based on a resolved IP and port.
#define lastof(x)
Get the last element of an fixed size array.
Configuration options of the network stuff.
bool IsFamily(int family)
Checks of this address is of the given family.
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
NetworkAddress(const char *hostname="", uint16 port=0, int family=AF_UNSPEC)
Create a network address based on a unresolved host and port.
int GetAddressLength()
Get the (valid) length of the address.
const char * GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
static const char * SocketTypeAsString(int socktype)
Convert the socket type into a string.
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including '\0'.
bool IsInNetmask(const char *netmask)
Checks whether this IP address is contained by the given netmask.
void SetPort(uint16 port)
Set the port.
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
SOCKET Connect()
Connect to the given address.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
static const char * AddressFamilyAsString(int family)
Convert the address family into a string.
bool operator!=(NetworkAddress address) const
Compare the address of this class with the address of another.
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
uint16 GetPort() const
Get the port.
char hostname[NETWORK_HOSTNAME_LENGTH]
The hostname.
bool operator<(NetworkAddress &address)
Compare the address of this class with the address of another.
NetworkAddress(sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
bool resolved
Whether the address has been (tried to be) resolved.
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
void GetAddressAsString(char *buffer, const char *last, bool with_family=true)
Get the address as a string, e.g.
int address_length
The length of the resolved address.
bool IsResolved() const
Check whether the IP address has been resolved already.
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.