10 #include <netinet/in.h>
18 class message_factory;
20 using buffer = std::vector<uint8_t>;
22 enum class network_access_type {
33 namespace message_type {
34 static uint16_t constexpr binding_request = 0x001;
35 static uint16_t constexpr binding_response = 0x0101;
36 static uint16_t constexpr binding_error_response = 0x0111;
37 static uint16_t constexpr shared_secret_request = 0x0002;
38 static uint16_t constexpr shared_secret_response = 0x0102;
39 static uint16_t constexpr shared_secret_error_response = 0x0112;
42 namespace attribute_type {
43 static uint16_t constexpr mapped_address = 0x001;
44 static uint16_t constexpr response_address = 0x0002;
45 static uint16_t constexpr change_request = 0x0003;
46 static uint16_t constexpr source_address = 0x004;
47 static uint16_t constexpr changed_address = 0x005;
48 static uint16_t constexpr username = 0x0006;
49 static uint16_t constexpr password = 0x0007;
50 static uint16_t constexpr message_integrity = 0x0008;
51 static uint16_t constexpr error_code = 0x0009;
52 static uint16_t constexpr unknown_attributes = 0x000a;
53 static uint16_t constexpr reflected_from = 0x000b;
59 std::vector<uint8_t> value;
62 namespace attributes {
66 inline sockaddr_storage addr()
const {
70 sockaddr_storage m_addr;
84 uint16_t message_type;
85 uint16_t message_length;
86 std::array<uint8_t, 16> transaction_id;
94 buffer encode()
const;
95 inline std::vector<attribute>
const & attributes()
const {
99 attribute const * find_attribute(uint16_t attr_type)
const;
103 std::vector<attribute> m_attrs;
108 static message * create_binding_request();
113 static uint16_t decode_u16(buffer
const & buff,
size_t * offset);
114 static uint32_t decode_u32(buffer
const & buff,
size_t * offset);
115 static attribute decode_attr(buffer
const & buff,
size_t * offset);
116 static message * decode_message(buffer
const & buff,
size_t * offset);
121 static void encode_u16(buffer & buff, uint16_t n);
122 static void encode_u32(buffer & buff, uint32_t n);
126 server(std::string
const& h, uint16_t p)
127 : hostname(h), port(p)
129 std::string hostname;
133 enum class protocol {
140 bool is_valid() {
return !public_ip.empty(); }
141 void invalidate() { public_ip.clear(); }
142 std::string public_ip;
150 bool bind(std::string
const & hostname,
152 std::string
const & interface,
154 uint16_t bind_timeout,
155 uint16_t cache_timeout,
158 network_access_type discover_network_access_type(
server const & srv);
160 inline void set_verbose(
bool b) {
164 void verbose(
char const * format, ...) __attribute__((format(printf, 2, 3)));
165 void create_udp_socket(
int inet_family);
167 std::unique_ptr<message> send_binding_request(std::chrono::milliseconds wait_time);
169 std::unique_ptr<message> send_binding_request(sockaddr_storage
const & addr,
170 std::chrono::milliseconds wait_time);
172 message * send_message(sockaddr_storage
const & remote_adr,
message const & req,
173 std::chrono::milliseconds wait_time,
int * local_iface_index =
nullptr);
178 std::string m_interface;
179 uint16_t m_bind_timeout;
180 uint16_t m_cache_timeout;
181 std::chrono::time_point<std::chrono::steady_clock> m_last_cache_time;
187 std::string sockaddr_to_string(sockaddr_storage
const & addr);