lokinet/llarp/net/address_info.hpp

96 lines
1.9 KiB
C++
Raw Normal View History

#pragma once
2018-12-11 23:58:58 +00:00
#include <llarp/crypto/types.hpp>
#include "ip_address.hpp"
#include "net.h"
#include <llarp/util/bencode.hpp>
#include <llarp/util/mem.h>
2018-08-30 18:48:43 +00:00
#include <string>
2018-12-11 23:58:58 +00:00
#include <vector>
2018-08-30 18:48:43 +00:00
#include <oxenc/variant.h>
2018-08-30 18:48:43 +00:00
/**
* address_info.hpp
*
* utilities for handling addresses on the llarp network
*/
/// address information model
namespace llarp
{
2019-05-24 02:01:36 +00:00
struct AddressInfo
2018-08-30 18:48:43 +00:00
{
uint16_t rank;
std::string dialect;
llarp::PubKey pubkey;
2019-12-10 13:37:41 +00:00
in6_addr ip = {};
2018-08-30 18:48:43 +00:00
uint16_t port;
2022-05-26 15:59:44 +00:00
uint64_t version = llarp::constants::proto_version;
2018-08-30 18:48:43 +00:00
2019-05-24 02:01:36 +00:00
bool
BDecode(llarp_buffer_t* buf)
2018-09-06 11:46:19 +00:00
{
2019-05-24 02:01:36 +00:00
return bencode_decode_dict(*this, buf);
2018-09-06 11:46:19 +00:00
}
2018-08-30 18:48:43 +00:00
bool
2019-05-24 02:01:36 +00:00
BEncode(llarp_buffer_t* buf) const;
2018-08-30 18:48:43 +00:00
bool
2019-05-24 02:01:36 +00:00
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf);
2018-08-31 12:46:54 +00:00
2020-05-06 20:38:44 +00:00
/// Return an IpAddress representing the address portion of this AddressInfo
IpAddress
toIpAddress() const;
/// Updates our ip and port to reflect that of the given SockAddr
2020-05-06 20:38:44 +00:00
void
fromSockAddr(const SockAddr& address);
2020-05-06 20:38:44 +00:00
/// get this as an explicit v4 or explicit v6
net::ipaddr_t
IP() const;
/// get this as an v4 or throw if it is not one
inline net::ipv4addr_t
IPv4() const
{
auto ip = IP();
if (auto* ptr = std::get_if<net::ipv4addr_t>(&ip))
return *ptr;
throw std::runtime_error{"no ipv4 address found in address info"};
}
std::string
ToString() const;
2018-08-30 18:48:43 +00:00
};
2019-08-19 22:25:40 +00:00
void
to_json(nlohmann::json& j, const AddressInfo& a);
2018-12-12 00:26:37 +00:00
bool
operator==(const AddressInfo& lhs, const AddressInfo& rhs);
bool
operator<(const AddressInfo& lhs, const AddressInfo& rhs);
template <>
constexpr inline bool IsToStringFormattable<AddressInfo> = true;
2018-08-30 18:48:43 +00:00
} // namespace llarp
namespace std
{
template <>
struct hash<llarp::AddressInfo>
{
size_t
operator()(const llarp::AddressInfo& addr) const
{
return std::hash<llarp::PubKey>{}(addr.pubkey);
}
};
} // namespace std