pex: move rx header check to callback function
[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 };
28
29 #define PEX_ID_LEN 8
30
31 struct pex_hdr {
32 uint8_t version;
33 uint8_t opcode;
34 uint16_t len;
35 uint8_t id[PEX_ID_LEN];
36 };
37
38 struct pex_ext_hdr {
39 uint64_t nonce;
40 uint8_t auth_id[PEX_ID_LEN];
41 };
42
43 #define PEER_EP_F_IPV6 (1 << 0)
44 #define PEER_EP_F_LOCAL (1 << 1)
45
46 struct pex_peer_endpoint {
47 uint16_t flags;
48 uint16_t port;
49 uint8_t peer_id[PEX_ID_LEN];
50 uint8_t addr[16];
51 };
52
53 struct pex_hello {
54 uint16_t flags;
55 uint8_t local_addr[16];
56 };
57
58 struct pex_update_request {
59 uint64_t req_id; /* must be first */
60 uint64_t cur_version;
61 };
62
63 struct pex_update_response {
64 uint64_t req_id; /* must be first */
65 uint32_t data_len;
66 uint8_t e_key[CURVE25519_KEY_SIZE];
67 };
68
69 struct pex_update_response_data {
70 uint64_t req_id; /* must be first */
71 uint32_t offset;
72 };
73
74 struct pex_update_response_no_data {
75 uint64_t req_id; /* must be first */
76 uint64_t cur_version;
77 };
78
79 struct pex_msg_update_send_ctx {
80 const uint8_t *pubkey;
81 const uint8_t *auth_key;
82 uint64_t req_id;
83 bool ext;
84
85 void *data;
86 void *cur;
87 int rem;
88 };
89
90 struct pex_msg_local_control {
91 int msg_type;
92 uint8_t auth_id[PEX_ID_LEN];
93 union network_endpoint ep;
94 int timeout;
95 };
96
97 struct pex_hdr *pex_rx_accept(void *data, size_t len, bool ext);
98
99 typedef void (*pex_recv_cb_t)(void *data, size_t len, struct sockaddr_in6 *addr);
100 typedef void (*pex_recv_control_cb_t)(struct pex_msg_local_control *msg, int len);
101
102 int pex_open(void *addr, size_t addr_len, pex_recv_cb_t cb, bool server);
103 int pex_unix_open(const char *path, pex_recv_control_cb_t cb);
104 void pex_close(void);
105
106 uint64_t pex_network_hash(const uint8_t *auth_key, uint64_t req_id);
107 struct pex_hdr *__pex_msg_init(const uint8_t *pubkey, uint8_t opcode);
108 struct pex_hdr *__pex_msg_init_ext(const uint8_t *pubkey, const uint8_t *auth_key,
109 uint8_t opcode, bool ext);
110 int __pex_msg_send(int fd, const void *addr, void *ip_hdr, size_t ip_hdrlen);
111 void *pex_msg_append(size_t len);
112
113 struct pex_update_request *
114 pex_msg_update_request_init(const uint8_t *pubkey, const uint8_t *priv_key,
115 const uint8_t *auth_key, union network_endpoint *addr,
116 uint64_t cur_version, bool ext);
117 void *pex_msg_update_response_recv(const void *data, int len, enum pex_opcode op,
118 int *data_len, uint64_t *timestamp);
119
120 void pex_msg_update_response_init(struct pex_msg_update_send_ctx *ctx,
121 const uint8_t *pubkey, const uint8_t *auth_key,
122 const uint8_t *peer_key, bool ext,
123 struct pex_update_request *req,
124 const void *data, int len);
125 bool pex_msg_update_response_continue(struct pex_msg_update_send_ctx *ctx);
126
127 #endif