-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.c
66 lines (50 loc) · 1.55 KB
/
data.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
#include "data.h"
#include "pack.h"
#include <assert.h>
int create_route_list_data(data_info *data, const route_list_info *route_list, const unsigned char *entry_client_id) {
assert(data);
assert(data->content);
assert(route_list);
assert(route_list->routes);
assert(route_list->count > 0 && route_list->count <= ROUTES_PER_PACKET);
assert(entry_client_id);
unsigned char *p = data->content;
size_t route_count = route_list->count;
const route_info *routes = route_list->routes;
for(size_t i = 0; i < route_count; i++) {
if(encrypt_route(p, &routes[i], entry_client_id))
return 1;
p += ROUTE_SIZE;
}
data->type = ROUTE_LIST_DATA;
data->length = p - data->content;
return 0;
}
int read_route_list_data(return_route_list_info *route_list, const data_info *data) {
assert(route_list);
assert(data);
assert(data->content);
assert(data->length <= OUTGOING_DATA_MAX);
assert(sizeof route_list->routes[0].id == sizeof(uint64_t));
assert(sizeof route_list->routes[0].dest_node_id == sizeof(uint64_t));
const unsigned char *p = data->content;
#ifndef NDEBUG
const unsigned char *end_ptr = p + data->length;
#endif
size_t route_count = data->length / ROUTE_SIZE;
return_route_info *route;
if(route_count == 0)
return 1;
route_list->count = route_count;
if(route_count * ROUTE_SIZE != data->length)
return 2;
for(size_t i = 0; i < route_count; i++) {
route = &route_list->routes[i];
route->id = read_uint64(&p);
route->dest_node_id = read_uint64(&p);
route->data = p;
p += INCOMING_ROUTE_LEN;
}
assert(p == end_ptr);
return 0;
}