---
title: llarp::IpAddress
summary: A struct that can represent either an IPv4 or IPv6 address. 

---

# llarp::IpAddress



A struct that can represent either an IPv4 or IPv6 address.  [More...](#detailed-description)


`#include <ip_address.hpp>`

## Public Functions

|                | Name           |
| -------------- | -------------- |
| | **[IpAddress](Classes/structllarp_1_1IpAddress.md#function-ipaddress)**() =default<br>Empty constructor.  |
| | **[IpAddress](Classes/structllarp_1_1IpAddress.md#function-ipaddress)**([IpAddress](Classes/structllarp_1_1IpAddress.md) && ) =default<br>move construtor  |
| | **[IpAddress](Classes/structllarp_1_1IpAddress.md#function-ipaddress)**(const [IpAddress](Classes/structllarp_1_1IpAddress.md) & )<br>copy construct  |
| | **[IpAddress](Classes/structllarp_1_1IpAddress.md#function-ipaddress)**(std::string_view str)<br>Constructor.  |
| | **[IpAddress](Classes/structllarp_1_1IpAddress.md#function-ipaddress)**(std::string_view str, std::optional< uint16_t > port)<br>Constructor.  |
| | **[IpAddress](Classes/structllarp_1_1IpAddress.md#function-ipaddress)**(const [SockAddr](Classes/structllarp_1_1SockAddr.md) & addr)<br>Construct from a [SockAddr]().  |
| [IpAddress](Classes/structllarp_1_1IpAddress.md) & | **[operator=](Classes/structllarp_1_1IpAddress.md#function-operator=)**(const sockaddr & other) |
| [IpAddress](Classes/structllarp_1_1IpAddress.md) & | **[operator=](Classes/structllarp_1_1IpAddress.md#function-operator=)**([IpAddress](Classes/structllarp_1_1IpAddress.md) && other)<br>move assignment  |
| [IpAddress](Classes/structllarp_1_1IpAddress.md) & | **[operator=](Classes/structllarp_1_1IpAddress.md#function-operator=)**(const [IpAddress](Classes/structllarp_1_1IpAddress.md) & other)<br>copy assignment  |
| std::optional< uint16_t > | **[getPort](Classes/structllarp_1_1IpAddress.md#function-getport)**() const<br>Return the port.  |
| bool | **[hasPort](Classes/structllarp_1_1IpAddress.md#function-hasport)**() const<br>Return true if we have a port set otherwise return false.  |
| void | **[setPort](Classes/structllarp_1_1IpAddress.md#function-setport)**(std::optional< uint16_t > port)<br>Set the port.  |
| void | **[setAddress](Classes/structllarp_1_1IpAddress.md#function-setaddress)**(std::string_view str)<br>Set the IP address.  |
| void | **[setAddress](Classes/structllarp_1_1IpAddress.md#function-setaddress)**(std::string_view str, std::optional< uint16_t > port) |
| bool | **[isIPv4](Classes/structllarp_1_1IpAddress.md#function-isipv4)**()<br>Returns true if this is an IPv4 address (or an IPv6 address representing an IPv4 address)  |
| bool | **[isEmpty](Classes/structllarp_1_1IpAddress.md#function-isempty)**() const<br>Returns true if this represents a valid [IpAddress](Classes/structllarp_1_1IpAddress.md), false otherwise.  |
| [SockAddr](Classes/structllarp_1_1SockAddr.md) | **[createSockAddr](Classes/structllarp_1_1IpAddress.md#function-createsockaddr)**() const<br>Creates an instance of [SockAddr]() representing this [IpAddress](Classes/structllarp_1_1IpAddress.md).  |
| bool | **[isBogon](Classes/structllarp_1_1IpAddress.md#function-isbogon)**() const<br>Returns true if this [IpAddress](Classes/structllarp_1_1IpAddress.md) is a bogon, false otherwise.  |
| std::string | **[ToString](Classes/structllarp_1_1IpAddress.md#function-tostring)**() const<br>Returns a string representing this [IpAddress](Classes/structllarp_1_1IpAddress.md).  |
| std::string | **[toHost](Classes/structllarp_1_1IpAddress.md#function-tohost)**() const |
| [huint32_t](Classes/structllarp_1_1huint__t.md) | **[toIP](Classes/structllarp_1_1IpAddress.md#function-toip)**() const |
| [huint128_t](Classes/structllarp_1_1huint__t.md) | **[toIP6](Classes/structllarp_1_1IpAddress.md#function-toip6)**() const |
| bool | **[operator<](Classes/structllarp_1_1IpAddress.md#function-operator<)**(const [IpAddress](Classes/structllarp_1_1IpAddress.md) & other) const |
| bool | **[operator==](Classes/structllarp_1_1IpAddress.md#function-operator==)**(const [IpAddress](Classes/structllarp_1_1IpAddress.md) & other) const |

## Detailed Description

```cpp
struct llarp::IpAddress;
```

A struct that can represent either an IPv4 or IPv6 address. 

It is meant for representation purposes only (e.g. serialization/deserialization). In addition, it also optionally stores a port.

As a convenience, it can produce a [SockAddr](Classes/structllarp_1_1SockAddr.md) for dealing with network libraries which depend sockaddr structs. However, it does not keep this as a member variable and isn't responsible for its lifetime/memory/etc. 

## Public Functions Documentation

### function IpAddress

```cpp
IpAddress() =default
```

Empty constructor. 

### function IpAddress

```cpp
IpAddress(
    IpAddress && 
) =default
```

move construtor 

### function IpAddress

```cpp
IpAddress(
    const IpAddress & 
)
```

copy construct 

### function IpAddress

```cpp
IpAddress(
    std::string_view str
)
```

Constructor. 

**Parameters**: 

  * **str** is a string representing an IP address and optionally a port 


**Exceptions**: 

  * **std::invalid_argument** if str cannot be parsed 


Takes a string which can be an IPv4 or IPv6 address optionally followed by a colon and a port.

Examples:

127.0.0.1 1.2.3.4:53

Note that an IPv6 + port representation must be done in an unambiguous way, e.g. wrap the IP portion of the string in brackets: [IPv6Addr]:port


### function IpAddress

```cpp
IpAddress(
    std::string_view str,
    std::optional< uint16_t > port
)
```

Constructor. 

**Parameters**: 

  * **str** is a string representing an IP address and optionally a port 


**Exceptions**: 

  * **std::invalid_argument** if str cannot be parsed 


Takes an IP address (as above) and a port. The string may not contain a port.


### function IpAddress

```cpp
IpAddress(
    const SockAddr & addr
)
```

Construct from a [SockAddr](). 

**Parameters**: 

  * **addr** is an [SockAddr](Classes/structllarp_1_1SockAddr.md) to initialize from. 


### function operator=

```cpp
IpAddress & operator=(
    const sockaddr & other
)
```


### function operator=

```cpp
IpAddress & operator=(
    IpAddress && other
)
```

move assignment 

### function operator=

```cpp
IpAddress & operator=(
    const IpAddress & other
)
```

copy assignment 

### function getPort

```cpp
std::optional< uint16_t > getPort() const
```

Return the port. 

**Return**: the port, if present 

Returns -1 if no port has been provided.


### function hasPort

```cpp
bool hasPort() const
```

Return true if we have a port set otherwise return false. 

### function setPort

```cpp
void setPort(
    std::optional< uint16_t > port
)
```

Set the port. 

**Parameters**: 

  * **port** 


### function setAddress

```cpp
void setAddress(
    std::string_view str
)
```

Set the IP address. 

Follows the same logic as the constructor with the same signature, but doesn't overwrite the port if the port isn't present in the string. 


### function setAddress

```cpp
void setAddress(
    std::string_view str,
    std::optional< uint16_t > port
)
```


### function isIPv4

```cpp
bool isIPv4()
```

Returns true if this is an IPv4 address (or an IPv6 address representing an IPv4 address) 

**Return**: true if this is an IPv4 address, false otherwise 

TODO: could return an int (e.g. 4 or 6) or an enum


### function isEmpty

```cpp
bool isEmpty() const
```

Returns true if this represents a valid [IpAddress](Classes/structllarp_1_1IpAddress.md), false otherwise. 

**Return**: whether or not this [IpAddress](Classes/structllarp_1_1IpAddress.md) is empty 

### function createSockAddr

```cpp
SockAddr createSockAddr() const
```

Creates an instance of [SockAddr]() representing this [IpAddress](Classes/structllarp_1_1IpAddress.md). 

**Return**: an instance of a [SockAddr](Classes/structllarp_1_1SockAddr.md) created from this [IpAddress](Classes/structllarp_1_1IpAddress.md)

### function isBogon

```cpp
bool isBogon() const
```

Returns true if this [IpAddress](Classes/structllarp_1_1IpAddress.md) is a bogon, false otherwise. 

**Return**: whether or not this [IpAddress](Classes/structllarp_1_1IpAddress.md) is a bogon 

### function ToString

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

Returns a string representing this [IpAddress](Classes/structllarp_1_1IpAddress.md). 

**Return**: string representation of this [IpAddress](Classes/structllarp_1_1IpAddress.md)

### function toHost

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


### function toIP

```cpp
huint32_t toIP() const
```


### function toIP6

```cpp
huint128_t toIP6() const
```


### function operator<

```cpp
bool operator<(
    const IpAddress & other
) const
```


### function operator==

```cpp
bool operator==(
    const IpAddress & other
) const
```


-------------------------------

Updated on 2023-01-27 at 23:09:26 +0000