-
Notifications
You must be signed in to change notification settings - Fork 1
/
dmap-handler.c
329 lines (253 loc) · 6.83 KB
/
dmap-handler.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
328
329
#include "dmap-handler.h"
#include "dmap-neighbor.h"
#include "dmap-trace-helpers.h"
#include "dmap-hash.h"
#include "dmap-sha256.h"
static int dmap_handle_hello(struct dmap *map, struct dmap_req_hello *req,
struct dmap_resp_hello *resp)
{
int r;
r = dmap_check_address(&req->source);
if (r)
return r;
TRACE("hello %s:%d %s",
req->source.host, req->source.port, req->source.id_str);
r = dmap_add_neighbor(map, &req->source, true);
if (r) {
if (r == -EEXIST)
r = 0;
else
return r;
}
dmap_get_address(map, &resp->addr);
return 0;
}
static int dmap_handle_ping(struct dmap *map, struct dmap_req_ping *req,
struct dmap_resp_ping *resp)
{
struct dmap_neighbor *neighbor;
int r;
r = dmap_check_address(&req->source);
if (r)
return r;
TRACE("ping %s:%d %s",
req->source.host, req->source.port, req->source.id_str);
neighbor = dmap_lookup_neighbor(map, &req->source);
if (!neighbor)
return -ENOTTY;
dmap_neighbor_put(neighbor);
return 0;
}
static int dmap_handle_bye(struct dmap *map, struct dmap_req_bye *req,
struct dmap_resp_bye *resp)
{
struct dmap_neighbor *neighbor;
int r;
r = dmap_check_address(&req->source);
if (r)
return r;
TRACE("bye %s:%d %s",
req->source.host, req->source.port, req->source.id_str);
neighbor = dmap_lookup_neighbor(map, &req->source);
if (!neighbor)
return -ENOTTY;
r = dmap_erase_neighbor(map, neighbor);
dmap_neighbor_put(neighbor);
return r;
}
static int dmap_handle_set_key(struct dmap *map, struct dmap_req_set_key *req,
struct dmap_resp_set_key *resp)
{
unsigned char hash[32];
struct dmap_neighbor *neighbor;
int r;
sha256(req->key, sizeof(req->key), hash);
neighbor = dmap_select_neighbor(map, hash);
if (WARN_ON(neighbor == NULL))
return -ENOTTY;
if (dmap_is_self_neighbor(map, neighbor))
r = dmap_hash_insert(&map->hash, req->key, sizeof(req->key),
req->value, sizeof(req->value));
else
r = dmap_neighbor_set_key(neighbor, req, resp);
TRACE("set key: %16phN r: %d", req->key, hash, r);
return r;
}
static int dmap_handle_get_key(struct dmap *map, struct dmap_req_get_key *req,
struct dmap_resp_get_key *resp)
{
struct dmap_neighbor *neighbor;
unsigned char hash[32];
int r;
size_t value_len;
sha256(req->key, sizeof(req->key), hash);
neighbor = dmap_select_neighbor(map, hash);
if (WARN_ON(neighbor == NULL))
return -ENOTTY;
if (dmap_is_self_neighbor(map, neighbor))
r = dmap_hash_get(&map->hash, req->key, sizeof(req->key),
resp->value, sizeof(resp->value), &value_len);
else
r = dmap_neighbor_get_key(neighbor, req, resp);
TRACE("get key: %16phN value: %16phN r: %d", req->key, resp->value);
return r;
}
static int dmap_handle_del_key(struct dmap *map, struct dmap_req_del_key *req,
struct dmap_resp_del_key *resp)
{
struct dmap_neighbor *neighbor;
unsigned char hash[32];
int r;
sha256(req->key, sizeof(req->key), hash);
neighbor = dmap_select_neighbor(map, hash);
if (WARN_ON(neighbor == NULL))
return -ENOTTY;
if (dmap_is_self_neighbor(map, neighbor))
r = dmap_hash_delete(&map->hash, req->key, sizeof(req->key));
else
r = dmap_neighbor_del_key(neighbor, req, resp);
TRACE("del key: %16phN r: %d", req->key, r);
return r;
}
static int dmap_handle_upd_key(struct dmap *map, struct dmap_req_upd_key *req,
struct dmap_resp_upd_key *resp)
{
unsigned char hash[32];
struct dmap_neighbor *neighbor;
int r;
sha256(req->key, sizeof(req->key), hash);
neighbor = dmap_select_neighbor(map, hash);
if (WARN_ON(neighbor == NULL))
return -ENOTTY;
if (dmap_is_self_neighbor(map, neighbor))
r = dmap_hash_update(&map->hash, req->key, sizeof(req->key),
req->value, sizeof(req->value));
else
r = dmap_neighbor_upd_key(neighbor, req, resp);
TRACE("update key: %16phN r: %d", req->key, hash, r);
return r;
}
static int dmap_handle_cmpxchg_key(struct dmap *map,
struct dmap_req_cmpxchg_key *req,
struct dmap_resp_cmpxchg_key *resp)
{
unsigned char hash[32];
struct dmap_neighbor *neighbor;
size_t value_len;
int r;
sha256(req->key, sizeof(req->key), hash);
neighbor = dmap_select_neighbor(map, hash);
if (WARN_ON(neighbor == NULL))
return -ENOTTY;
if (dmap_is_self_neighbor(map, neighbor))
r = dmap_hash_cmpxchg(&map->hash, req->key, sizeof(req->key),
req->exchange, sizeof(req->exchange),
req->comparand, sizeof(req->comparand),
resp->value, sizeof(resp->value), &value_len);
else
r = dmap_neighbor_cmpxchg_key(neighbor, req, resp);
TRACE("cmpxchg key: %16phN r: %d", req->key, hash, r);
return r;
}
int dmap_handle_request(struct dmap *map, u32 type, void *req_body, u32 req_len,
void *resp_body, u32 *resp_len)
{
int r;
switch (type) {
case DMAP_PACKET_HELLO: {
struct dmap_req_hello *req = req_body;
struct dmap_resp_hello *resp = resp_body;
if (req_len != sizeof(*req)) {
r = -EINVAL;
break;
}
r = dmap_handle_hello(map, req, resp);
*resp_len = sizeof(*resp);
break;
}
case DMAP_PACKET_PING: {
struct dmap_req_ping *req = req_body;
struct dmap_resp_ping *resp = resp_body;
if (req_len != sizeof(*req)) {
r = -EINVAL;
break;
}
r = dmap_handle_ping(map, req, resp);
*resp_len = sizeof(*resp);
break;
}
case DMAP_PACKET_BYE: {
struct dmap_req_bye *req = req_body;
struct dmap_resp_bye *resp = resp_body;
if (req_len != sizeof(*req)) {
r = -EINVAL;
break;
}
r = dmap_handle_bye(map, req, resp);
*resp_len = sizeof(*resp);
break;
}
case DMAP_PACKET_SET_KEY: {
struct dmap_req_set_key *req = req_body;
struct dmap_resp_set_key *resp = resp_body;
if (req_len != sizeof(*req)) {
r = -EINVAL;
break;
}
r = dmap_handle_set_key(map, req, resp);
*resp_len = sizeof(*resp);
break;
}
case DMAP_PACKET_GET_KEY: {
struct dmap_req_get_key *req = req_body;
struct dmap_resp_get_key *resp = resp_body;
if (req_len != sizeof(*req)) {
r = -EINVAL;
break;
}
r = dmap_handle_get_key(map, req, resp);
*resp_len = sizeof(*resp);
break;
}
case DMAP_PACKET_DEL_KEY: {
struct dmap_req_del_key *req = req_body;
struct dmap_resp_del_key *resp = resp_body;
if (req_len != sizeof(*req)) {
r = -EINVAL;
break;
}
r = dmap_handle_del_key(map, req, resp);
*resp_len = sizeof(*resp);
break;
}
case DMAP_PACKET_UPD_KEY: {
struct dmap_req_upd_key *req = req_body;
struct dmap_resp_upd_key *resp = resp_body;
if (req_len != sizeof(*req)) {
r = -EINVAL;
break;
}
r = dmap_handle_upd_key(map, req, resp);
*resp_len = sizeof(*resp);
break;
}
case DMAP_PACKET_CMPXCHG_KEY: {
struct dmap_req_cmpxchg_key *req = req_body;
struct dmap_resp_cmpxchg_key *resp = resp_body;
if (req_len != sizeof(*req)) {
r = -EINVAL;
break;
}
r = dmap_handle_cmpxchg_key(map, req, resp);
*resp_len = sizeof(*resp);
break;
}
default:
r = -EINVAL;
TRACE_ERR(r, "unsupported request type %d", type);
break;
}
if (r)
*resp_len = 0;
return r;
}