lokinet/llarp/net/address_info.hpp

83 lines
1.6 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
/**
* 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;
2019-05-24 02:01:36 +00:00
uint64_t version = LLARP_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
2019-02-24 23:46:37 +00:00
std::ostream&
print(std::ostream& stream, int level, int spaces) 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);
2019-02-24 23:46:37 +00:00
inline std::ostream&
operator<<(std::ostream& out, const AddressInfo& a)
{
return a.print(out, -1, -1);
}
2018-12-12 00:26:37 +00:00
bool
operator==(const AddressInfo& lhs, const AddressInfo& rhs);
bool
operator<(const AddressInfo& lhs, const AddressInfo& rhs);
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