forked from codyps/peerduct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes_dat.c
More file actions
242 lines (188 loc) · 5.19 KB
/
nodes_dat.c
File metadata and controls
242 lines (188 loc) · 5.19 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "nodes_dat.h"
/* endian.h usage */
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif
#include <endian.h> /* le32toh, le16toh */
#include <arpa/inet.h> /* htons */
int nd_parse_init(struct nd_parse_ctx *npc)
{
npc->head_pos = 0;
npc->header_parsed = false;
/* head ?? */
npc->version = 0;
npc->peers = NULL;
npc->tail = &(npc->peers);
npc->nr_consumed = 0;
npc->nr_total = 0;
npc->error = 0;
/* cur_e ?? */
npc->cur_e_pos = 0;
return 0;
}
#define le16ton(x) htons(le16toh(x))
#define le32ton(x) htonl(le32toh(x))
static struct peer *mk_kad_peer(struct nd_entry *entry)
{
struct kad_peer *kp = malloc(sizeof(*kp));
if (!kp)
return NULL;
memcpy(kp->client_id, entry->client_id, sizeof(kp->client_id));
memcpy(kp->kad_udp_key, entry->kad_udp_key, sizeof(kp->kad_udp_key));
kp->version = entry->version;
kp->verified = entry->verified;
kp->udp.sin_family = AF_INET;
kp->tcp.sin_family = AF_INET;
kp->udp.sin_port = le16ton(entry->udp_port);
kp->tcp.sin_port = le16ton(entry->tcp_port);
/* stored as be */
kp->udp.sin_addr.s_addr = entry->ip_addr;
kp->tcp.sin_addr.s_addr = entry->ip_addr;
kp->peer.type = PT_KAD;
kp->peer.next = NULL;
return &kp->peer;
}
static struct peer *mk_kad_peer_v0(struct nd_entry_v0 *entry)
{
struct kad_peer *kp = malloc(sizeof(*kp));
if (!kp)
return NULL;
memcpy(kp->client_id, entry->client_id, sizeof(kp->client_id));
memset(kp->kad_udp_key, 0, sizeof(kp->kad_udp_key));
kp->version = 0;
kp->verified = 0;
kp->udp.sin_family = AF_INET;
kp->tcp.sin_family = AF_INET;
kp->udp.sin_port = le16ton(entry->udp_port);
kp->tcp.sin_port = le16ton(entry->tcp_port);
/* stored as BE aka Network byte order.
* sockaddr_in expects network byte order.
* all is well. */
kp->udp.sin_addr.s_addr = entry->ip_addr;
kp->tcp.sin_addr.s_addr = entry->ip_addr;
kp->peer.type = PT_KAD;
kp->peer.next = NULL;
return &kp->peer;
}
/* do not call if npc->header_parsed is true */
static int header_parser(struct nd_parse_ctx *npc, void *buf, size_t len)
{
size_t consumed = 0;
if (npc->head_pos < sizeof(uint32_t)) {
size_t cpy_sz = MIN(len, sizeof(uint32_t) - npc->head_pos);
memcpy( (char *)&(npc->head) + npc->head_pos, buf, cpy_sz);
npc->head_pos += cpy_sz;
consumed += cpy_sz;
if (npc->head_pos >= sizeof(uint32_t)) {
if (npc->head.zero) {
npc->version = 0;
npc->nr_total = le32toh(npc->head.zero);
npc->header_parsed = true;
return consumed;
} else {
buf += cpy_sz;
len -= cpy_sz;
}
}
}
/* this is not an else case of the first if as the value being compared
* against could be changed within the first if. */
if (npc->head_pos >= sizeof(uint32_t)) {
size_t cpy_sz = MIN(len, sizeof(npc->head) - npc->head_pos);
memcpy( (char *)&(npc->head) + npc->head_pos, buf, cpy_sz);
npc->head_pos += cpy_sz;
consumed += cpy_sz;
if (npc->head_pos >= sizeof(npc->head)) {
npc->version = le32toh(npc->head.version);
npc->nr_total = le32toh(npc->head.nr);
npc->header_parsed = true;
return consumed;
}
}
return consumed;
}
int nd_parse_proc(struct nd_parse_ctx *npc, void *buf, size_t len)
{
size_t consumed = 0; /* number of bytes in buf consumed by
the current function call */
if (npc->error < 0)
return npc->error;
if (len == 0)
return consumed;
if (!npc->header_parsed) {
int r = header_parser(npc, buf, len);
if (!npc->header_parsed)
return r;
buf += r;
len -= r;
consumed += r;
}
/* if we pass the above if, the header must be parsed.
* Thus, version and nr_total will be populated. */
size_t goal_len;
if (npc->version == 0)
goal_len = sizeof(npc->cur_e_v0);
else if (npc->version == 2)
goal_len = sizeof(npc->cur_e);
else {
npc->error = -EINVAL;
return npc->error;
}
uint32_t nr_total = npc->nr_total;
uint32_t nr;
size_t pos = npc->cur_e_pos;
for (nr = npc->nr_consumed; nr < nr_total; nr++) {
char *start_cpy = (char *)&(npc->cur_e) + pos;
size_t rem = goal_len - pos;
size_t cpy_len = MIN(rem, len);
memcpy(start_cpy, buf, cpy_len);
pos += cpy_len;
len -= cpy_len;
buf += cpy_len;
if (likely(pos == goal_len)) {
/* we filled this entry, deserialize it
* and continue */
struct peer *p = npc->version ?
mk_kad_peer(&npc->cur_e) :
mk_kad_peer_v0(&npc->cur_e_v0);
if (!p) {
/* failed allocation? set npc->error and
* return the consumed bytes. Error will be
* returned on next call */
npc->error = -errno;
npc->nr_consumed = nr;
npc->cur_e_pos = 0;
return consumed;
}
pos = 0;
*(npc->tail) = p;
npc->tail = &(p->next);
consumed += cpy_len;
} else {
/* entry not filled but data done.
* update things for next time */
npc->cur_e_pos = pos;
npc->nr_consumed = nr;
return consumed + cpy_len;
}
}
/* we've read all the nodes in the file */
return consumed;
}
struct peer *nd_parse_get_peers(struct nd_parse_ctx *npc)
{
struct peer *p = npc->peers;
npc->peers = NULL;
npc->tail = &(npc->peers);
return p;
}
void nd_parse_destroy(struct nd_parse_ctx *npc)
{
free_kad_peers(npc->peers);
npc->peers = NULL;
npc->tail = &(npc->peers);
}