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