---
title: llarp/net/ip_address.hpp

---

# llarp/net/ip_address.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[std](Namespaces/namespacestd.md)** <br>STL namespace.  |

## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[llarp::IpAddress](Classes/structllarp_1_1IpAddress.md)** <br>A struct that can represent either an IPv4 or IPv6 address.  |
| struct | **[std::hash< llarp::IpAddress >](Classes/structstd_1_1hash_3_01llarp_1_1IpAddress_01_4.md)**  |




## Source code

```cpp
#pragma once

#include "sock_addr.hpp"

#include <optional>
#include <string_view>
#include <string>

#include "net_int.hpp"

#include <llarp/util/formattable.hpp>

namespace llarp
{
  struct [[deprecated("use llarp::SockAddr instead")]] IpAddress
  {
    IpAddress() = default;
    IpAddress(IpAddress &&) = default;
    IpAddress(const IpAddress&);

    IpAddress(std::string_view str);

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

    IpAddress(const SockAddr& addr);

    IpAddress& operator=(const sockaddr& other);

    IpAddress& operator=(IpAddress&& other);

    IpAddress& operator=(const IpAddress& other);

    std::optional<uint16_t> getPort() const;

    bool hasPort() const;

    void setPort(std::optional<uint16_t> port);

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

    bool isIPv4();

    bool isEmpty() const;

    SockAddr createSockAddr() const;

    bool isBogon() const;

    std::string ToString() const;

    std::string toHost() const;

    huint32_t toIP() const;

    huint128_t toIP6() const;

    // TODO: other utility functions left over from Addr which may be useful
    // IsBogon() const;
    // isPrivate() const;
    // std::hash
    // to string / stream / etc

    bool operator<(const IpAddress& other) const;

    bool operator==(const IpAddress& other) const;

   private:
    bool m_empty = true;
    std::string m_ipAddress;
    std::optional<uint16_t> m_port = std::nullopt;
  };

  template <>
  constexpr inline bool IsToStringFormattable<IpAddress> = true;

}  // namespace llarp

namespace std
{
  template <>
  struct hash<llarp::IpAddress>
  {
    std::size_t
    operator()(const llarp::IpAddress& address) const noexcept
    {
      return std::hash<std::string>{}(address.ToString());
    }
  };
}  // namespace std
```


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

Updated on 2024-11-10 at 05:10:02 +0000
