2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-12-11 23:58:58 +00:00
|
|
|
|
2021-03-09 22:24:35 +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
|
|
|
|
2022-05-20 17:10:04 +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;
|
|
|
|
|
2021-02-23 10:48:50 +00:00
|
|
|
/// Updates our ip and port to reflect that of the given SockAddr
|
2020-05-06 20:38:44 +00:00
|
|
|
void
|
2021-02-22 15:01:05 +00:00
|
|
|
fromSockAddr(const SockAddr& address);
|
2020-05-06 20:38:44 +00:00
|
|
|
|
2022-05-20 17:10:04 +00:00
|
|
|
/// get this as an explicit v4 or explicit v6
|
2022-07-28 16:07:38 +00:00
|
|
|
net::ipaddr_t
|
2022-05-20 17:10:04 +00:00
|
|
|
IP() const;
|
|
|
|
|
2022-07-28 16:07:38 +00: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 00:41:14 +00:00
|
|
|
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);
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
template <>
|
|
|
|
constexpr inline bool IsToStringFormattable<AddressInfo> = true;
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
} // namespace llarp
|
2021-03-09 18:39:40 +00: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
|