12 #ifndef NETWORK_CORE_ADDRESS_H 13 #define NETWORK_CORE_ADDRESS_H 17 #include "../../string_func.h" 18 #include "../../core/smallmap_type.hpp" 53 address_length(address_length),
55 resolved(address_length != 0)
57 *this->hostname =
'\0';
66 address_length(address_length),
67 resolved(address_length != 0)
69 *this->hostname =
'\0';
70 memset(&this->address, 0,
sizeof(this->address));
71 memcpy(&this->address, address, address_length);
80 NetworkAddress(
const char *hostname =
"", uint16 port = 0,
int family = AF_UNSPEC) :
85 if (
StrEmpty(hostname)) hostname =
"";
86 if (*hostname ==
'[') hostname++;
88 char *tmp = strrchr(this->hostname,
']');
89 if (tmp != NULL) *tmp =
'\0';
91 memset(&this->address, 0,
sizeof(this->address));
92 this->address.ss_family = family;
102 memcpy(
this, &address,
sizeof(*
this));
144 if (r == 0) r = this->address.ss_family - address.
address.ss_family;
145 if (r == 0) r = memcmp(&this->address, &address.
address, this->address_length);
NetworkAddress(const NetworkAddress &address)
Make a clone of another address.
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.
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.
NetworkAddress(const char *hostname="", uint16 port=0, int family=AF_UNSPEC)
Create a network address based on a unresolved host and port.
SmallMap< NetworkAddress, SOCKET, 4 > SocketList
Type for a mapping between address and socket.
int GetAddressLength()
Get the (valid) length of the address.
SmallVector< NetworkAddress, 4 > NetworkAddressList
Type for a list of addresses.
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'.
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.
bool IsInNetmask(char *netmask)
Checks whether this IP address is contained by the given netmask.
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.