2021-03-09 23:24:35 +01:00
|
|
|
#pragma once
|
2018-12-12 00:58:58 +01:00
|
|
|
|
2021-03-09 23:24:35 +01: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 20:48:43 +02:00
|
|
|
|
|
|
|
#include <string>
|
2018-12-12 00:58:58 +01:00
|
|
|
#include <vector>
|
2018-08-30 20:48:43 +02:00
|
|
|
|
2022-05-20 19:10:04 +02:00
|
|
|
#include <oxenc/variant.h>
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
/**
|
|
|
|
* address_info.hpp
|
|
|
|
*
|
|
|
|
* utilities for handling addresses on the llarp network
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// address information model
|
|
|
|
namespace llarp
|
|
|
|
{
|
2019-05-24 04:01:36 +02:00
|
|
|
struct AddressInfo
|
2018-08-30 20:48:43 +02:00
|
|
|
{
|
|
|
|
uint16_t rank;
|
|
|
|
std::string dialect;
|
|
|
|
llarp::PubKey pubkey;
|
2019-12-10 14:37:41 +01:00
|
|
|
in6_addr ip = {};
|
2018-08-30 20:48:43 +02:00
|
|
|
uint16_t port;
|
2022-05-26 17:59:44 +02:00
|
|
|
uint64_t version = llarp::constants::proto_version;
|
2018-08-30 20:48:43 +02:00
|
|
|
|
2019-05-24 04:01:36 +02:00
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
2018-09-06 13:46:19 +02:00
|
|
|
{
|
2019-05-24 04:01:36 +02:00
|
|
|
return bencode_decode_dict(*this, buf);
|
2018-09-06 13:46:19 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
bool
|
2019-05-24 04:01:36 +02:00
|
|
|
BEncode(llarp_buffer_t* buf) const;
|
2018-08-30 20:48:43 +02:00
|
|
|
|
|
|
|
bool
|
2019-05-24 04:01:36 +02:00
|
|
|
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf);
|
2018-08-31 14:46:54 +02:00
|
|
|
|
2020-05-06 22:38:44 +02:00
|
|
|
/// Return an IpAddress representing the address portion of this AddressInfo
|
|
|
|
IpAddress
|
|
|
|
toIpAddress() const;
|
|
|
|
|
2021-02-23 11:48:50 +01:00
|
|
|
/// Updates our ip and port to reflect that of the given SockAddr
|
2020-05-06 22:38:44 +02:00
|
|
|
void
|
2021-02-22 16:01:05 +01:00
|
|
|
fromSockAddr(const SockAddr& address);
|
2020-05-06 22:38:44 +02:00
|
|
|
|
2022-05-20 19:10:04 +02:00
|
|
|
/// get this as an explicit v4 or explicit v6
|
2022-07-28 18:07:38 +02:00
|
|
|
net::ipaddr_t
|
2022-05-20 19:10:04 +02:00
|
|
|
IP() const;
|
|
|
|
|
2022-07-28 18:07:38 +02:00
|
|
|
/// 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"};
|
|
|
|
}
|
|
|
|
|
2022-07-16 02:41:14 +02:00
|
|
|
std::string
|
|
|
|
ToString() const;
|
2018-08-30 20:48:43 +02:00
|
|
|
};
|
|
|
|
|
2019-08-20 00:25:40 +02:00
|
|
|
void
|
|
|
|
to_json(nlohmann::json& j, const AddressInfo& a);
|
|
|
|
|
2018-12-12 01:26:37 +01:00
|
|
|
bool
|
|
|
|
operator==(const AddressInfo& lhs, const AddressInfo& rhs);
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const AddressInfo& lhs, const AddressInfo& rhs);
|
|
|
|
|
2022-07-16 02:41:14 +02:00
|
|
|
template <>
|
|
|
|
constexpr inline bool IsToStringFormattable<AddressInfo> = true;
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
} // namespace llarp
|
2021-03-09 19:39:40 +01:00
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct hash<llarp::AddressInfo>
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const llarp::AddressInfo& addr) const
|
|
|
|
{
|
|
|
|
return std::hash<llarp::PubKey>{}(addr.pubkey);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace std
|