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