# Address

<a id="address"></a>

# Address

```cpp
#include <icy/net/address.h>

class Address
```

[Defined in src/net/include/icy/net/address.h:29](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L29)

Represents an IPv4 or IPv6 socket address with host and port.

## Friends

| Name | Description |
|------|-------------|
| [`operator<<`](#operator-173) `inline` |  |

---

<a id="operator-173"></a>

### operator<<

`inline`

```cpp
friend inline std::ostream & operator<<(std::ostream & stream, const Address & addr)
```

[Defined in src/net/include/icy/net/address.h:132](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L132)

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Address`](#address-1)  | Creates a wildcard (all zero) IPv4 [Address](#address). |
|  | [`Address`](#address-2)  | Creates an [Address](#address) from an IP address and a port number. |
|  | [`Address`](#address-3)  | Creates an [Address](#address) by copying another one. |
|  | [`Address`](#address-4)  | Creates an [Address](#address) from a native socket address. |
|  | [`Address`](#address-5)  | Creates an [Address](#address) from an IP address and a service name or port number. |
|  | [`Address`](#address-6) `explicit` | Creates an [Address](#address) from an IP address or host name and a port number/service name. Host name/address and port number must be separated by a colon. In case of an IPv6 address, the address part must be enclosed in brackets. |
|  | [`~Address`](#address-7) `noexcept` | Destroys the [Address](#address). |
| [`Address`](#address) & | [`operator=`](#operator-174)  | Assigns another [Address](#address). |
| `void` | [`swap`](#swap-10)  | Swaps the [Address](#address) with another one. |
| `std::string` | [`host`](#host) `const` | Returns the host IP address. |
| `uint16_t` | [`port`](#port) `const` | Returns the port number. |
| `socklen_t` | [`length`](#length) `const` | Returns the length of the internal native socket address. |
| `const struct sockaddr *` | [`addr`](#addr-1) `const` | Returns a pointer to the internal native socket address. |
| `int` | [`af`](#af) `const` | Returns the address family (AF_INET or AF_INET6) of the address. |
| `std::string` | [`toString`](#tostring-7) `const` | Returns a string representation of the address. |
| [`Address::Family`](#family-1) | [`family`](#family) `const` | Returns the address family of the host's address. |
| `bool` | [`valid`](#valid) `const` | Returns true when the port is set and the address is valid ie. not wildcard. |
| `bool` | [`operator<`](#operator-175) `const` | Compares two addresses for ordering (by family then port). |
| `bool` | [`operator==`](#operator-176) `const` | Returns true if the host and port of both addresses are equal. |
| `bool` | [`operator!=`](#operator-177) `const` | Returns true if the host or port of the addresses differ. |

---

<a id="address-1"></a>

### Address

```cpp
Address()
```

[Defined in src/net/include/icy/net/address.h:40](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L40)

Creates a wildcard (all zero) IPv4 [Address](#address).

---

<a id="address-2"></a>

### Address

```cpp
Address(const std::string & host, uint16_t port)
```

[Defined in src/net/include/icy/net/address.h:46](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L46)

Creates an [Address](#address) from an IP address and a port number.

The IP address must either be a domain name, or it must be in dotted decimal (IPv4) or hex string (IPv6) format.

---

<a id="address-3"></a>

### Address

```cpp
Address(const Address & addr)
```

[Defined in src/net/include/icy/net/address.h:49](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L49)

Creates an [Address](#address) by copying another one.

---

<a id="address-4"></a>

### Address

```cpp
Address(const struct sockaddr * addr, socklen_t length)
```

[Defined in src/net/include/icy/net/address.h:52](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L52)

Creates an [Address](#address) from a native socket address.

---

<a id="address-5"></a>

### Address

```cpp
Address(const std::string & host, const std::string & port)
```

[Defined in src/net/include/icy/net/address.h:62](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L62)

Creates an [Address](#address) from an IP address and a service name or port number.

The IP address must either be a domain name, or it must be in dotted decimal (IPv4) or hex string (IPv6) format.

The given port must either be a decimal port number, or a service name.

---

<a id="address-6"></a>

### Address

`explicit`

```cpp
explicit Address(const std::string & hostAndPort)
```

[Defined in src/net/include/icy/net/address.h:73](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L73)

Creates an [Address](#address) from an IP address or host name and a port number/service name. Host name/address and port number must be separated by a colon. In case of an IPv6 address, the address part must be enclosed in brackets.

Examples: 192.168.1.10:80

0state.com:8080

---

<a id="address-7"></a>

### ~Address

`noexcept`

```cpp
~Address() noexcept
```

[Defined in src/net/include/icy/net/address.h:76](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L76)

Destroys the [Address](#address).

---

<a id="operator-174"></a>

### operator=

```cpp
Address & operator=(const Address & addr)
```

[Defined in src/net/include/icy/net/address.h:79](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L79)

Assigns another [Address](#address).

---

<a id="swap-10"></a>

### swap

```cpp
void swap(Address & addr)
```

[Defined in src/net/include/icy/net/address.h:82](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L82)

Swaps the [Address](#address) with another one.

---

<a id="host"></a>

### host

`const`

```cpp
std::string host() const
```

[Defined in src/net/include/icy/net/address.h:85](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L85)

Returns the host IP address.

---

<a id="port"></a>

### port

`const`

```cpp
uint16_t port() const
```

[Defined in src/net/include/icy/net/address.h:88](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L88)

Returns the port number.

---

<a id="length"></a>

### length

`const`

```cpp
socklen_t length() const
```

[Defined in src/net/include/icy/net/address.h:91](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L91)

Returns the length of the internal native socket address.

---

<a id="addr-1"></a>

### addr

`const`

```cpp
const struct sockaddr * addr() const
```

[Defined in src/net/include/icy/net/address.h:94](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L94)

Returns a pointer to the internal native socket address.

---

<a id="af"></a>

### af

`const`

```cpp
int af() const
```

[Defined in src/net/include/icy/net/address.h:97](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L97)

Returns the address family (AF_INET or AF_INET6) of the address.

---

<a id="tostring-7"></a>

### toString

`const`

```cpp
std::string toString() const
```

[Defined in src/net/include/icy/net/address.h:100](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L100)

Returns a string representation of the address.

---

<a id="family"></a>

### family

`const`

```cpp
Address::Family family() const
```

[Defined in src/net/include/icy/net/address.h:103](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L103)

Returns the address family of the host's address.

---

<a id="valid"></a>

### valid

`const`

```cpp
bool valid() const
```

[Defined in src/net/include/icy/net/address.h:107](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L107)

Returns true when the port is set and the address is valid ie. not wildcard.

---

<a id="operator-175"></a>

### operator<

`const`

```cpp
bool operator<(const Address & addr) const
```

[Defined in src/net/include/icy/net/address.h:122](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L122)

Compares two addresses for ordering (by family then port). 
#### Returns
true if this address is less than `addr`.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `addr` | const [`Address`](#address) & | The address to compare against. |

---

<a id="operator-176"></a>

### operator==

`const`

```cpp
bool operator==(const Address & addr) const
```

[Defined in src/net/include/icy/net/address.h:126](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L126)

Returns true if the host and port of both addresses are equal.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `addr` | const [`Address`](#address) & | The address to compare against. |

---

<a id="operator-177"></a>

### operator!=

`const`

```cpp
bool operator!=(const Address & addr) const
```

[Defined in src/net/include/icy/net/address.h:130](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L130)

Returns true if the host or port of the addresses differ.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `addr` | const [`Address`](#address) & | The address to compare against. |

## Public Static Methods

| Return | Name | Description |
|--------|------|-------------|
| `uint16_t` | [`resolveService`](#resolveservice) `static` | Resolves a service name or decimal port string to a port number. |
| `bool` | [`validateIP`](#validateip) `static` | Returns true if the given string is a valid IPv4 or IPv6 address. |

---

<a id="resolveservice"></a>

### resolveService

`static`

```cpp
static uint16_t resolveService(const std::string & service)
```

[Defined in src/net/include/icy/net/address.h:112](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L112)

Resolves a service name or decimal port string to a port number. 
#### Returns
The resolved port number in host byte order.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `service` | `const std::string &` | Service name (e.g. "http") or decimal port string (e.g. "80"). |

---

<a id="validateip"></a>

### validateIP

`static`

```cpp
static bool validateIP(std::string_view address)
```

[Defined in src/net/include/icy/net/address.h:117](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L117)

Returns true if the given string is a valid IPv4 or IPv6 address. 
#### Returns
true if the address parses as a valid IP address, false otherwise.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `address` | `std::string_view` | The string to validate. |

## Protected Methods

| Return | Name | Description |
|--------|------|-------------|
| `void` | [`init`](#init-9)  |  |

---

<a id="init-9"></a>

### init

```cpp
void init(const std::string & host, uint16_t port)
```

[Defined in src/net/include/icy/net/address.h:139](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L139)

## Public Types

| Name | Description |
|------|-------------|
| [`Family`](#family-1)  | Possible address families for IP addresses. |

---

<a id="family-1"></a>

### Family

```cpp
enum Family
```

[Defined in src/net/include/icy/net/address.h:33](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L33)

Possible address families for IP addresses.

| Value | Description |
|-------|-------------|
| `IPv4` |  |
| `IPv6` |  |
## Private Attributes

| Return | Name | Description |
|--------|------|-------------|
| `std::shared_ptr< AddressBase >` | [`_base`](#_base)  |  |

---

<a id="_base"></a>

### _base

```cpp
std::shared_ptr< AddressBase > _base
```

[Defined in src/net/include/icy/net/address.h:142](https://github.com/auscaster/icey/blob/main/src/net/include/icy/net/address.h#L142)
