lokinet/llarp/net/address_info.hpp

96 lines
1.9 KiB
C++
Raw Normal View History

#pragma once
2018-12-12 00:58:58 +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
#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;
/// Updates our ip and port to reflect that of the given SockAddr
2020-05-06 22:38:44 +02:00
void
fromSockAddr(const SockAddr& address);
2020-05-06 22:38:44 +02: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 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);
template <>
constexpr inline bool IsToStringFormattable<AddressInfo> = true;
2018-08-30 20:48:43 +02: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