unet-cli: strip initial newline in usage message
[project/unetd.git] / pex-msg.h
1 #ifndef __PEX_MSG_H
2 #define __PEX_MSG_H
3
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include "curve25519.h"
8 #include "siphash.h"
9 #include "utils.h"
10
11 #define UNETD_GLOBAL_PEX_PORT 51819
12 #define PEX_BUF_SIZE 1024
13 #define PEX_RX_BUF_SIZE 16384
14 #define UNETD_NET_DATA_SIZE_MAX (128 * 1024)
15
16 enum pex_opcode {
17 PEX_MSG_HELLO,
18 PEX_MSG_NOTIFY_PEERS,
19 PEX_MSG_QUERY,
20 PEX_MSG_PING,
21 PEX_MSG_PONG,
22 PEX_MSG_UPDATE_REQUEST,
23 PEX_MSG_UPDATE_RESPONSE,
24 PEX_MSG_UPDATE_RESPONSE_DATA,
25 PEX_MSG_UPDATE_RESPONSE_NO_DATA,
26 PEX_MSG_ENDPOINT_NOTIFY,
27 PEX_MSG_ENDPOINT_PORT_NOTIFY,
28 };
29
30 #define PEX_ID_LEN 8
31
32 struct pex_hdr {
33 uint8_t version;
34 uint8_t opcode;
35 uint16_t len;
36 uint8_t id[PEX_ID_LEN];
37 };
38
39 struct pex_ext_hdr {
40 uint64_t nonce;
41 uint8_t auth_id[PEX_ID_LEN];
42 };
43
44 #define PEER_EP_F_IPV6 (1 << 0)
45 #define PEER_EP_F_LOCAL (1 << 1)
46
47 struct pex_peer_endpoint {
48 uint16_t flags;
49 uint16_t port;
50 uint8_t peer_id[PEX_ID_LEN];
51 uint8_t addr[16];
52 };
53
54 struct pex_hello {
55 uint16_t flags;
56 uint8_t local_addr[16];
57 };
58
59 struct pex_update_request {
60 uint64_t req_id; /* must be first */
61 uint64_t cur_version;
62 };
63
64 struct pex_update_response {
65 uint64_t req_id; /* must be first */
66 uint32_t data_len;
67 uint8_t e_key[CURVE25519_KEY_SIZE];
68 };
69
70 struct pex_update_response_data {
71 uint64_t req_id; /* must be first */
72 uint32_t offset;
73 };
74
75 struct pex_update_response_no_data {
76 uint64_t req_id; /* must be first */
77 uint64_t cur_version;
78 };
79
80 struct pex_endpoint_port_notify {
81 uint16_t port;
82 };
83
84 struct pex_msg_update_send_ctx {
85 const uint8_t *pubkey;
86 const uint8_t *auth_key;
87 uint64_t req_id;
88 bool ext;
89
90 void *data;
91 void *cur;
92 int rem;
93 };
94
95 struct pex_msg_local_control {
96 int msg_type;
97 uint8_t auth_id[PEX_ID_LEN];
98 union network_endpoint ep;
99 int timeout;
100 };
101
102 struct pex_hdr *pex_rx_accept(void *data, size_t len, bool ext);
103
104 typedef void (*pex_recv_cb_t)(void *data, size_t len, struct sockaddr_in6 *addr);
105 typedef void (*pex_recv_control_cb_t)(struct pex_msg_local_control *msg, int len);
106
107 int pex_open(void *addr, size_t addr_len, pex_recv_cb_t cb, bool server);
108 int pex_unix_open(const char *path, pex_recv_control_cb_t cb);
109 void pex_close(void);
110
111 int pex_socket(void);
112 int pex_raw_socket(int family);
113 uint64_t pex_network_hash(const uint8_t *auth_key, uint64_t req_id);
114 struct pex_hdr *__pex_msg_init(const uint8_t *pubkey, uint8_t opcode);
115 struct pex_hdr *__pex_msg_init_ext(const uint8_t *pubkey, const uint8_t *auth_key,
116 uint8_t opcode, bool ext);
117 int __pex_msg_send(int fd, const void *addr, void *ip_hdr, size_t ip_hdrlen);
118 void *pex_msg_append(size_t len);
119
120 struct pex_update_request *
121 pex_msg_update_request_init(const uint8_t *pubkey, const uint8_t *priv_key,
122 const uint8_t *auth_key, union network_endpoint *addr,
123 uint64_t cur_version, bool ext);
124 void *pex_msg_update_response_recv(const void *data, int len, enum pex_opcode op,
125 int *data_len, uint64_t *timestamp);
126
127 void pex_msg_update_response_init(struct pex_msg_update_send_ctx *ctx,
128 const uint8_t *pubkey, const uint8_t *auth_key,
129 const uint8_t *peer_key, bool ext,
130 struct pex_update_request *req,
131 const void *data, int len);
132 bool pex_msg_update_response_continue(struct pex_msg_update_send_ctx *ctx);
133
134 #endif