# NetworkAddress Class Reference

Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string and then back again to pass it to functions. More...

`#include <address.h>`

Inheritance diagram for NetworkAddress:

## Public Member Functions
| NetworkAddress (struct sockaddr_storage &address, int address_length)
| Create a network address based on a resolved IP and port.
| NetworkAddress (sockaddr *address, int address_length)
| Create a network address based on a resolved IP and port.
| NetworkAddress (const char *hostname="", uint16 port=0, int family=AF_UNSPEC)
| Create a network address based on a unresolved host and port.
| NetworkAddress (const NetworkAddress &address)
| Make a clone of another address.
const char * | GetHostname ()
| Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
void | GetAddressAsString (char *buffer, const char *last, bool with_family=true)
| Get the address as a string, e.g.
const char * | GetAddressAsString (bool with_family=true)
| Get the address as a string, e.g.
const sockaddr_storage * | GetAddress ()
| Get the address in it's internal representation.
int | GetAddressLength ()
| Get the (valid) length of the address.
uint16 | GetPort () const
| Get the port.
void | SetPort (uint16 port)
| Set the port.
bool | IsResolved () const
| Check whether the IP address has been resolved already.
bool | IsFamily (int family)
| Checks of this address is of the given family.
bool | IsInNetmask (char *netmask)
| Checks whether this IP address is contained by the given netmask.
int | CompareTo (NetworkAddress &address)
| Compare the address of this class with the address of another.
bool | operator== (NetworkAddress &address)
| Compare the address of this class with the address of another.
bool | operator== (NetworkAddress &address) const
| Compare the address of this class with the address of another.
bool | operator!= (NetworkAddress address) const
| Compare the address of this class with the address of another.
bool | operator< (NetworkAddress &address)
| Compare the address of this class with the address of another.
SOCKET | Connect ()
| Connect to the given address.
void | Listen (int socktype, SocketList *sockets)
| Make the given socket listen.
## Static Public Member Functions
static const char * | SocketTypeAsString (int socktype)
| Convert the socket type into a string.
static const char * | AddressFamilyAsString (int family)
| Convert the address family into a string.
## Private Types
typedef SOCKET(* | LoopProc )(addrinfo *runp)
| Helper function to resolve something to a socket.
## Private Member Functions
SOCKET | Resolve (int family, int socktype, int flags, SocketList *sockets, LoopProc func)
| Resolve this address into a socket.
## Private Attributes
char | hostname [NETWORK_HOSTNAME_LENGTH]
| The hostname.
int | address_length
| The length of the resolved address.
sockaddr_storage | address
| The resolved address.

## Detailed Description

Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string and then back again to pass it to functions.

It furthermore allows easier delaying of the hostname lookup.

Definition at line 31 of file address.h.

## Member Typedef Documentation

typedef SOCKET(* NetworkAddress::LoopProc)(addrinfo *runp)`[private]`

Helper function to resolve something to a socket.

Parameters:

  | runp | information about the socket to try not

Returns:
  the opened socket or INVALID_SOCKET

Definition at line 42 of file address.h.

## Constructor & Destructor Documentation

NetworkAddress::NetworkAddress | ( | struct sockaddr_storage & | address,
|  | int | address_length |
| ) |  |  | `[inline]`

Create a network address based on a resolved IP and port.

Parameters:

  | address | the IP address with port

Definition at line 59 of file address.h.

References hostname.

Referenced by IsInNetmask().

NetworkAddress::NetworkAddress | ( | sockaddr * | address,
|  | int | address_length |
| ) |  |  | `[inline]`

Create a network address based on a resolved IP and port.

Parameters:

  | address | the IP address with port

Definition at line 70 of file address.h.

References hostname.

NetworkAddress::NetworkAddress | ( | const char * | hostname = `""`,
|  | uint16 | port = `0`,
|  | int | family = `AF_UNSPEC` |
| ) |  |  | `[inline]`

Create a network address based on a unresolved host and port.

Parameters:

  | ip | the unresolved hostname
  | port | the port
  | family | the address family

Definition at line 84 of file address.h.

References address, hostname, lastof, SetPort(), strecpy(), and StrEmpty().

NetworkAddress::NetworkAddress | ( | const NetworkAddress & | address | ) | `[inline]`

Make a clone of another address.

Parameters:

  | address | the address to clone

Definition at line 103 of file address.h.

## Member Function Documentation

const char * NetworkAddress::AddressFamilyAsString | ( | int | family | ) | `[static]`

Convert the address family into a string.

Parameters:

  | family | the family to convert

Returns:
  the string representation

Note:
  only works for AF_INET, AF_INET6 and AF_UNSPEC

Definition at line 352 of file address.cpp.

Referenced by ConnectLoopProc(), ListenLoopProc(), and Resolve().

int NetworkAddress::CompareTo | ( | NetworkAddress & | address | ) | `[inline]`

Compare the address of this class with the address of another.

Parameters:

  | address | the other address.

Returns:
  < 0 if address is less, 0 if equal and > 0 if address is more

Definition at line 189 of file address.h.

References address, GetAddressLength(), and GetPort().

Referenced by operator!=(), operator<(), and operator==().

SOCKET NetworkAddress::Connect | ( |  | ) |

Connect to the given address.

Returns:
  the connected socket or INVALID_SOCKET.

Definition at line 269 of file address.cpp.

References ConnectLoopProc(), GetAddressAsString(), and Resolve().

Referenced by TCPConnecter::Connect().

const sockaddr_storage * NetworkAddress::GetAddress | ( |  | ) |

Get the address in it's internal representation.

Returns:
  the address

Definition at line 97 of file address.cpp.

References address, IsResolved(), Resolve(), and ResolveLoopProc().

Referenced by GetAddressAsString(), GetAddressLength(), IsInNetmask(), and NetworkUDPSocketHandler::SendPacket().

const char * NetworkAddress::GetAddressAsString | ( | bool | with_family = `true` | ) |

Get the address as a string, e.g.

127.0.0.1:12345.

Parameters:

  | with_family | whether to add the family (e.g. IPvX).

Returns:
  the address

Note:
  NOT thread safe

Definition at line 78 of file address.cpp.

References GetAddressAsString(), lastof, and NETWORK_HOSTNAME_LENGTH.

void NetworkAddress::GetAddressAsString | ( | char * | buffer,
|  | const char * | last,
|  | bool | with_family = `true` |
| ) |  |  |

Get the address as a string, e.g.

127.0.0.1:12345.

Parameters:

  | buffer | the buffer to write to
  | last | the last element in the buffer
  | with_family | whether to add the family (e.g. IPvX).

Definition at line 60 of file address.cpp.

References address, GetAddress(), GetHostname(), GetPort(), seprintf(), and strecpy().

Referenced by Connect(), GetAddressAsString(), NetworkContentSocketHandler::HandlePacket(), NetworkUDPSocketHandler::HandleUDPPacket(), NetworkUDPQueryServerThread(), NetworkUDPSocketHandler::ReceivePackets(), and NetworkUDPSocketHandler::SendPacket().

int NetworkAddress::GetAddressLength | ( |  | ) | `[inline]`

Get the (valid) length of the address.

Returns:
  the length

Definition at line 141 of file address.h.

References address_length, GetAddress(), and IsResolved().

Referenced by CompareTo(), IsInNetmask(), NetworkFindClientInfoFromIP(), and NetworkUDPSocketHandler::SendPacket().

const char * NetworkAddress::GetHostname | ( |  | ) |

Get the hostname; in case it wasn't given the IPv4 dotted representation is given.

Returns:
  the hostname

Definition at line 19 of file address.cpp.

References address, address_length, hostname, and StrEmpty().

Referenced by GetAddressAsString(), NetworkGameListAddItem(), NetworkUDPQueryServerThread(), and NetworkHTTPContentConnecter::OnConnect().

uint16 NetworkAddress::GetPort | ( |  | ) | const

Get the port.

Returns:
  the port

Definition at line 28 of file address.cpp.

References address.

Referenced by CompareTo(), GetAddressAsString(), Listen(), NetworkGameWindow::OnClick(), and Resolve().

bool NetworkAddress::IsFamily | ( | int | family | ) |

Checks of this address is of the given family.

Parameters:

  | family | the family to check against

Returns:
  true if it is of the given family

Definition at line 110 of file address.cpp.

References address, IsResolved(), Resolve(), and ResolveLoopProc().

Referenced by NetworkUDPSocketHandler::SendPacket().

bool NetworkAddress::IsInNetmask | ( | char * | netmask | ) |

Checks whether this IP address is contained by the given netmask.

Parameters:

  | netmask | the netmask in CIDR notation to test against.

Note:
  netmask without /n assumes all bits need to match.

Returns:
  true if this IP is within the netmask.

Definition at line 118 of file address.cpp.

References address, GetAddress(), GetAddressLength(), IsResolved(), and NetworkAddress().

bool NetworkAddress::IsResolved | ( |  | ) | const`[inline]`

Check whether the IP address has been resolved already.

Returns:
  true iff the port has been resolved

Definition at line 164 of file address.h.

References address_length.

Referenced by GetAddress(), GetAddressLength(), IsFamily(), and IsInNetmask().

void NetworkAddress::Listen | ( | int | socktype,
|  | SocketList * | sockets |
| ) |  |  |

Make the given socket listen.

Parameters:

  | socktype | the type of socket (TCP, UDP, etc)
  | sockets | the list of sockets to add the sockets to

Definition at line 327 of file address.cpp.

References address, address_length, GetPort(), hostname, ListenLoopProc(), Resolve(), and StrEmpty().

bool NetworkAddress::operator!= | ( | NetworkAddress | address | ) | const`[inline]`

Compare the address of this class with the address of another.

Parameters:

  | address | the other address.

Returns:
  true if both do not match.

Definition at line 222 of file address.h.

References CompareTo().

bool NetworkAddress::operator< | ( | NetworkAddress & | address | ) | `[inline]`

Compare the address of this class with the address of another.

Parameters:

  | address | the other address.

Definition at line 231 of file address.h.

References CompareTo().

bool NetworkAddress::operator== | ( | NetworkAddress & | address | ) | const`[inline]`

Compare the address of this class with the address of another.

Parameters:

  | address | the other address.

Returns:
  true if both match.

Definition at line 213 of file address.h.

References CompareTo().

bool NetworkAddress::operator== | ( | NetworkAddress & | address | ) | `[inline]`

Compare the address of this class with the address of another.

Parameters:

  | address | the other address.

Returns:
  true if both match.

Definition at line 203 of file address.h.

References CompareTo().

SOCKET NetworkAddress::Resolve | ( | int | family,
|  | int | socktype,
|  | int | flags,
|  | SocketList * | sockets,
|  | LoopProc | func |
| ) |  |  | `[private]`

Resolve this address into a socket.

Parameters:

  | family | the type of 'protocol' (IPv4, IPv6)
  | socktype | the type of socket (TCP, UDP, etc)
  | flags | the flags to send to getaddrinfo
  | sockets | the list of sockets to add the sockets to
  | func | the inner working while looping over the address info

Returns:
  the resolved socket or INVALID_SOCKET.

Definition at line 172 of file address.cpp.

References address, address_length, AddressFamilyAsString(), SmallVector< T, S >::End(), SmallMap< T, U, S >::Find(), FS2OTTD(), GetPort(), hostname, lastof, ResolveLoopProc(), seprintf(), SocketTypeAsString(), strecpy(), and StrEmpty().

Referenced by Connect(), GetAddress(), IsFamily(), and Listen().

void NetworkAddress::SetPort | ( | uint16 | port | ) |

Set the port.

Parameters:

  | port | set the port number

Definition at line 43 of file address.cpp.

References address.

Referenced by NetworkAddress().

const char * NetworkAddress::SocketTypeAsString | ( | int | socktype | ) | `[static]`

Convert the socket type into a string.

Parameters:

  | socktype | the socket type to convert

Returns:
  the string representation

Note:
  only works for SOCK_STREAM and SOCK_DGRAM

Definition at line 343 of file address.cpp.

Referenced by ConnectLoopProc(), ListenLoopProc(), and Resolve().

The documentation for this class was generated from the following files:

- src/network/core/address.h
- src/network/core/address.cpp