-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsn_multiple_wire.c
327 lines (286 loc) · 8.33 KB
/
sn_multiple_wire.c
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* sn_multiple_wire.c
*
* Created on: Mar 25, 2012
* Author: Costin Lupu
*/
#include "n2n.h"
#include "sn_multiple_wire.h"
static int alloc_array( void **array_ptr, unsigned int num, unsigned int item_size )
{
if (num > 0)
{
*array_ptr = calloc(num, item_size);
if (!*array_ptr)
{
traceError("not enough memory for allocating array");
return -1;
}
}
else
{
*array_ptr = NULL;
}
return 0;
}
static void free_array( void **array_ptr )
{
if(array_ptr && *array_ptr)
{
free(*array_ptr);
*array_ptr = NULL;
}
}
int alloc_communities( snm_comm_name_t **comm_ptr, unsigned int comm_num )
{
return alloc_array((void **) comm_ptr, comm_num, sizeof(snm_comm_name_t));
}
void free_communities( snm_comm_name_t **comm_ptr )
{
free_array((void **) comm_ptr);
}
int alloc_supernodes( n2n_sock_t **sn_ptr, unsigned int sn_num )
{
return alloc_array((void **) sn_ptr, sn_num, sizeof(n2n_sock_t));
}
void free_supernodes( n2n_sock_t **sn_ptr )
{
free_array((void **) sn_ptr);
}
int encode_SNM_comm( uint8_t *base,
size_t *idx,
const snm_comm_name_t *comm_name )
{
int retval = 0;
retval += encode_uint8(base, idx, comm_name->size);
retval += encode_buf(base, idx, comm_name->name, comm_name->size);
return retval;
}
int decode_SNM_comm( snm_comm_name_t *comm_name,
const uint8_t *base,
size_t *rem,
size_t *idx )
{
int retval = 0;
memset(comm_name, 0, N2N_COMMUNITY_SIZE);
retval += decode_uint8(&comm_name->size, base, rem, idx);
retval += decode_buf(comm_name->name, comm_name->size, base, rem, idx);
return retval;
}
int encode_SNM_hdr( uint8_t *base,
size_t *idx,
const snm_hdr_t *hdr )
{
int retval = 0;
retval += encode_uint8(base, idx, hdr->type);
retval += encode_uint8(base, idx, hdr->flags);
retval += encode_uint16(base, idx, hdr->seq_num);
return retval;
}
int decode_SNM_hdr( snm_hdr_t *hdr,
const uint8_t *base,
size_t *rem,
size_t *idx )
{
int retval = 0;
retval += decode_uint8(&hdr->type, base, rem, idx);
retval += decode_uint8(&hdr->flags, base, rem, idx);
retval += decode_uint16(&hdr->seq_num, base, rem, idx);
return retval;
}
int encode_SNM_REQ( uint8_t *base,
size_t *idx,
const snm_hdr_t *hdr,
const n2n_SNM_REQ_t *req )
{
int i, retval = 0;
retval += encode_SNM_hdr(base, idx, hdr);
if (GET_N(hdr->flags))
{
retval += encode_uint16(base, idx, req->comm_num);
for (i = 0; i < req->comm_num; i++)
{
retval += encode_SNM_comm(base, idx, &req->comm_ptr[i]);
}
}
return retval;
}
int decode_SNM_REQ( n2n_SNM_REQ_t *pkt,
const snm_hdr_t *hdr,
const uint8_t *base,
size_t *rem,
size_t *idx )
{
int i, retval = 0;
memset(pkt, 0, sizeof(n2n_SNM_REQ_t));
if (GET_N(hdr->flags))
{
retval += decode_uint16(&pkt->comm_num, base, rem, idx);
if (alloc_communities(&pkt->comm_ptr, pkt->comm_num))
{
return -1;
}
for (i = 0; i < pkt->comm_num; i++)
{
retval += decode_SNM_comm(&pkt->comm_ptr[i], base, rem, idx);
}
}
return retval;
}
int encode_SNM_INFO( uint8_t *base,
size_t *idx,
const snm_hdr_t *hdr,
const n2n_SNM_INFO_t *info )
{
int i, retval = 0;
retval += encode_SNM_hdr(base, idx, hdr);
retval += encode_uint16(base, idx, info->sn_num);
retval += encode_uint16(base, idx, info->comm_num);
if (GET_S(hdr->flags) || GET_A(hdr->flags)) /* SNM / ADV adresses */
{
for (i = 0; i < info->sn_num; i++)
{
retval += encode_sock(base, idx, &info->sn_ptr[i]);
}
}
if (GET_C(hdr->flags) || GET_N(hdr->flags))
{
for (i = 0; i < info->comm_num; i++)
{
retval += encode_SNM_comm(base, idx, &info->comm_ptr[i]);
}
}
return retval;
}
int decode_SNM_INFO( n2n_SNM_INFO_t *pkt,
const snm_hdr_t *hdr,
const uint8_t *base,
size_t * rem,
size_t * idx )
{
int i, retval = 0;
memset(pkt, 0, sizeof(n2n_SNM_INFO_t));
retval += decode_uint16(&pkt->sn_num, base, rem, idx);
retval += decode_uint16(&pkt->comm_num, base, rem, idx);
if (GET_S(hdr->flags) || GET_A(hdr->flags)) /* SNM / ADV adresses */
{
if (alloc_supernodes(&pkt->sn_ptr, pkt->sn_num))
{
return -1;
}
for (i = 0; i < pkt->sn_num; i++)
{
retval += decode_sock(&pkt->sn_ptr[i], base, rem, idx);
}
}
if (GET_C(hdr->flags) || GET_N(hdr->flags))
{
if (alloc_communities(&pkt->comm_ptr, pkt->comm_num))
{
free_supernodes(&pkt->sn_ptr);
return -1;
}
for (i = 0; i < pkt->comm_num; i++)
{
retval += decode_SNM_comm(&pkt->comm_ptr[i], base, rem, idx);
}
}
return retval;
}
int encode_SNM_ADV( uint8_t *base,
size_t *idx,
const snm_hdr_t *hdr,
const n2n_SNM_ADV_t *adv )
{
int i, retval = 0;
retval += encode_SNM_hdr(base, idx, hdr);
retval += encode_sock(base, idx, &adv->sn);
if (GET_N(hdr->flags))
{
retval += encode_uint16(base, idx, adv->comm_num);
for (i = 0; i < adv->comm_num; i++)
{
retval += encode_SNM_comm(base, idx, &adv->comm_ptr[i]);
}
}
return retval;
}
int decode_SNM_ADV( n2n_SNM_ADV_t *pkt,
const snm_hdr_t *hdr,
const uint8_t *base,
size_t *rem,
size_t *idx )
{
int i, retval = 0;
memset(pkt, 0, sizeof(n2n_SNM_ADV_t));
retval += decode_sock(&pkt->sn, base, rem, idx);
if (GET_N(hdr->flags))
{
retval += decode_uint16(&pkt->comm_num, base, rem, idx);
if (alloc_communities(&pkt->comm_ptr, pkt->comm_num))
{
return -1;
}
for (i = 0; i < pkt->comm_num; i++)
{
retval += decode_SNM_comm(&pkt->comm_ptr[i], base, rem, idx);
}
}
return retval;
}
void log_SNM_hdr( const snm_hdr_t *hdr )
{
traceEvent( TRACE_DEBUG, "HEADER type=%d S=%d C=%d N=%d A=%d E=%d Seq=%d", hdr->type,
GET_S(hdr->flags), GET_C(hdr->flags), GET_N(hdr->flags), GET_A(hdr->flags), GET_E(hdr->flags),
hdr->seq_num );
}
void log_SNM_REQ( const n2n_SNM_REQ_t *req )
{
int i;
traceEvent( TRACE_DEBUG, "REQ Communities=%d", req->comm_num );
if (req->comm_ptr)
{
for(i = 0; i < req->comm_num; i++)
{
traceEvent( TRACE_DEBUG, "\t[%d] len=%d name=%s", i,
req->comm_ptr[i].size, req->comm_ptr[i].name );
}
}
}
void log_SNM_INFO( const n2n_SNM_INFO_t *info )
{
int i;
n2n_sock_str_t sockbuf;
traceEvent( TRACE_DEBUG, "INFO Supernodes=%d Communities=%d",
info->sn_num, info->comm_num );
if (info->sn_ptr)
{
for(i = 0; i < info->sn_num; i++)
{
traceEvent( TRACE_DEBUG, "\t[S%d] %s", i, sock2str(sockbuf, &info->sn_ptr[i]) );
}
}
if (info->comm_ptr)
{
for(i = 0; i < info->comm_num; i++)
{
traceEvent( TRACE_DEBUG, "\t[C%d] len=%d name=%s", i, info->comm_ptr[i].size,
(info->comm_ptr[i].size > 0) ? (const char *) info->comm_ptr[i].name : "" );
}
}
}
void log_SNM_ADV( const n2n_SNM_ADV_t *adv )
{
int i;
n2n_sock_str_t sockbuf;
traceEvent( TRACE_DEBUG, "ADV Supernode=%s Communities=%d",
sock2str(sockbuf, &adv->sn), adv->comm_num );
if (adv->comm_ptr)
{
for(i = 0; i < adv->comm_num; i++)
{
traceEvent( TRACE_DEBUG, "\t[C%d] len=%d name=%s", i, adv->comm_ptr[i].size,
(adv->comm_ptr[i].size > 0) ? (const char *) adv->comm_ptr[i].name : "" );
}
}
}