pex: add support for sending endpoint notification from the wg port via raw socket
[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
10 #define UNETD_GLOBAL_PEX_PORT 51819
11 #define PEX_BUF_SIZE 1024
12 #define UNETD_NET_DATA_SIZE_MAX (128 * 1024)
13
14 enum pex_opcode {
15 PEX_MSG_HELLO,
16 PEX_MSG_NOTIFY_PEERS,
17 PEX_MSG_QUERY,
18 PEX_MSG_PING,
19 PEX_MSG_PONG,
20 PEX_MSG_UPDATE_REQUEST,
21 PEX_MSG_UPDATE_RESPONSE,
22 PEX_MSG_UPDATE_RESPONSE_DATA,
23 PEX_MSG_UPDATE_RESPONSE_NO_DATA,
24 PEX_MSG_ENDPOINT_NOTIFY,
25 };
26
27 #define PEX_ID_LEN 8
28
29 struct pex_hdr {
30 uint8_t version;
31 uint8_t opcode;
32 uint16_t len;
33 uint8_t id[PEX_ID_LEN];
34 };
35
36 struct pex_ext_hdr {
37 uint64_t nonce;
38 uint8_t auth_id[PEX_ID_LEN];
39 };
40
41 #define PEER_EP_F_IPV6 (1 << 0)
42 #define PEER_EP_F_LOCAL (1 << 1)
43
44 struct pex_peer_endpoint {
45 uint16_t flags;
46 uint16_t port;
47 uint8_t peer_id[PEX_ID_LEN];
48 uint8_t addr[16];
49 };
50
51 struct pex_hello {
52 uint16_t flags;
53 uint8_t local_addr[16];
54 };
55
56 struct pex_update_request {
57 uint64_t req_id; /* must be first */
58 uint64_t cur_version;
59 };
60
61 struct pex_update_response {
62 uint64_t req_id; /* must be first */
63 uint32_t data_len;
64 uint8_t e_key[CURVE25519_KEY_SIZE];
65 };
66
67 struct pex_update_response_data {
68 uint64_t req_id; /* must be first */
69 uint32_t offset;
70 };
71
72 struct pex_update_response_no_data {
73 uint64_t req_id; /* must be first */
74 uint64_t cur_version;
75 };
76
77 struct pex_msg_update_send_ctx {
78 const uint8_t *pubkey;
79 const uint8_t *auth_key;
80 uint64_t req_id;
81 bool ext;
82
83 void *data;
84 void *cur;
85 int rem;
86 };
87
88 typedef void (*pex_recv_cb_t)(struct pex_hdr *hdr, struct sockaddr_in6 *addr);
89
90 int pex_open(void *addr, size_t addr_len, pex_recv_cb_t cb, bool server);
91 void pex_close(void);
92
93 uint64_t pex_network_hash(const uint8_t *auth_key, uint64_t req_id);
94 struct pex_hdr *__pex_msg_init(const uint8_t *pubkey, uint8_t opcode);
95 struct pex_hdr *__pex_msg_init_ext(const uint8_t *pubkey, const uint8_t *auth_key,
96 uint8_t opcode, bool ext);
97 int __pex_msg_send(int fd, const void *addr, void *ip_hdr, size_t ip_hdrlen);
98 void *pex_msg_append(size_t len);
99
100 struct pex_update_request *
101 pex_msg_update_request_init(const uint8_t *pubkey, const uint8_t *priv_key,
102 const uint8_t *auth_key, union network_endpoint *addr,
103 uint64_t cur_version, bool ext);
104 void *pex_msg_update_response_recv(const void *data, int len, enum pex_opcode op,
105 int *data_len, uint64_t *timestamp);
106
107 void pex_msg_update_response_init(struct pex_msg_update_send_ctx *ctx,
108 const uint8_t *pubkey, const uint8_t *auth_key,
109 const uint8_t *peer_key, bool ext,
110 struct pex_update_request *req,
111 const void *data, int len);
112 bool pex_msg_update_response_continue(struct pex_msg_update_send_ctx *ctx);
113
114 #endif