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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
/*
* Ouroboros - Copyright (C) 2016
*
* The Common Distributed Application Protocol
*
* Sander Vrijders <sander.vrijders@intec.ugent.be>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <ouroboros/config.h>
#include <ouroboros/cdap.h>
#include <ouroboros/bitmap.h>
#include <ouroboros/common.h>
#include <ouroboros/dev.h>
#include <stdlib.h>
#include <pthread.h>
#include "cdap.pb-c.h"
typedef Cdap cdap_t;
typedef Opcode opcode_t;
#define IDS_SIZE 256
#define BUF_SIZE 2048
struct cdap {
int fd;
struct bmp * ids;
pthread_mutex_t ids_lock;
pthread_t reader;
struct cdap_ops * ops;
};
static ssize_t cdap_msg_to_buffer(cdap_t * msg,
buffer_t ** val)
{
int i;
size_t len;
len = msg->n_value;
*val = malloc(len * sizeof(**val));
if (*val == NULL)
return -1;
for (i = 0; i < len; i++) {
if (msg->value[i].data == NULL) {
free(*val);
return -1;
}
(*val)[i].data = msg->value[i].data;
(*val)[i].len = msg->value[i].len;
}
return len;
}
static int next_invoke_id(struct cdap * instance)
{
int ret;
pthread_mutex_lock(&instance->ids_lock);
ret = bmp_allocate(instance->ids);
pthread_mutex_unlock(&instance->ids_lock);
return ret;
}
static int release_invoke_id(struct cdap * instance,
int id)
{
int ret;
pthread_mutex_lock(&instance->ids_lock);
ret = bmp_release(instance->ids, id);
pthread_mutex_unlock(&instance->ids_lock);
return ret;
}
static void * sdu_reader(void * o)
{
struct cdap * instance = (struct cdap *) o;
cdap_t * msg;
uint8_t buf[BUF_SIZE];
ssize_t len;
ssize_t length;
buffer_t * val;
while (true) {
len = flow_read(instance->fd, buf, BUF_SIZE);
if (len < 0)
return (void *) -1;
msg = cdap__unpack(NULL, len, buf);
if (msg == NULL)
continue;
switch (msg->opcode) {
case OPCODE__READ:
if (msg->name != NULL)
instance->ops->cdap_read(instance,
msg->name);
break;
case OPCODE__WRITE:
length = cdap_msg_to_buffer(msg, &val);
if (msg->name != NULL &&
msg->value != NULL &&
len > 0) {
instance->ops->cdap_write(instance,
msg->name,
val,
length,
msg->flags);
free(val);
}
break;
case OPCODE__CREATE:
length = cdap_msg_to_buffer(msg, &val);
if (msg->name != NULL &&
length == 1) {
instance->ops->cdap_create(instance,
msg->name,
val[0]);
free(val);
}
break;
case OPCODE__DELETE:
length = cdap_msg_to_buffer(msg, &val);
if (msg->name != NULL &&
length == 1) {
instance->ops->cdap_create(instance,
msg->name,
val[0]);
free(val);
}
break;
case OPCODE__START:
if (msg->name != NULL)
instance->ops->cdap_start(instance,
msg->name);
break;
case OPCODE__STOP:
if (msg->name != NULL)
instance->ops->cdap_stop(instance,
msg->name);
break;
case OPCODE__REPLY:
length = cdap_msg_to_buffer(msg, &val);
if (msg->name != NULL &&
length > 0) {
instance->ops->cdap_reply(instance,
msg->invoke_id,
msg->result,
val,
length);
release_invoke_id(instance, msg->invoke_id);
free(val);
}
break;
default:
break;
}
cdap__free_unpacked(msg, NULL);
}
return (void *) 0;
}
struct cdap * cdap_create(struct cdap_ops * ops,
int fd)
{
struct cdap * instance = NULL;
int flags;
if (ops == NULL || fd < 0 ||
ops->cdap_reply == NULL ||
ops->cdap_read == NULL ||
ops->cdap_write == NULL ||
ops->cdap_create == NULL ||
ops->cdap_delete == NULL ||
ops->cdap_start == NULL ||
ops->cdap_stop == NULL)
return NULL;
flags = flow_cntl(fd, FLOW_F_GETFL, 0);
if (flags & FLOW_O_NONBLOCK)
return NULL;
instance = malloc(sizeof(*instance));
if (instance == NULL)
return NULL;
if (pthread_mutex_init(&instance->ids_lock, NULL)) {
free(instance);
return NULL;
}
instance->ops = ops;
instance->fd = fd;
instance->ids = bmp_create(IDS_SIZE, 0);
if (instance->ids == NULL) {
free(instance);
return NULL;
}
pthread_create(&instance->reader,
NULL,
sdu_reader,
(void *) instance);
return instance;
}
int cdap_destroy(struct cdap * instance)
{
if (instance == NULL)
return -1;
pthread_cancel(instance->reader);
if (flow_dealloc(instance->fd))
return -1;
pthread_mutex_lock(&instance->ids_lock);
bmp_destroy(instance->ids);
pthread_mutex_unlock(&instance->ids_lock);
pthread_join(instance->reader,
NULL);
free(instance);
return 0;
}
static int write_msg(struct cdap * instance,
cdap_t * msg)
{
buffer_t buf;
int ret;
buf.len = cdap__get_packed_size(msg);
if (buf.len == 0)
return -1;
buf.data = malloc(BUF_SIZE);
if (buf.data == NULL)
return -1;
cdap__pack(msg, buf.data);
ret = flow_write(instance->fd, buf.data, buf.len);
free(buf.data);
return ret;
}
static int buffer_to_cdap_msg(cdap_t * msg,
buffer_t * val,
size_t len)
{
int i;
msg->value = malloc(len * sizeof(*msg->value));
if (msg->value == NULL)
return -1;
msg->n_value = len;
for (i = 0; i < len; i++) {
if (val[i].data == NULL) {
free(msg->value);
return -1;
}
msg->value[i].data = val[i].data;
msg->value[i].len = val[i].len;
}
return 0;
}
static int send_read_or_start_or_stop(struct cdap * instance,
char * name,
opcode_t code)
{
int id;
cdap_t msg = CDAP__INIT;
if (instance == NULL || name == NULL)
return -1;
id = next_invoke_id(instance);
if (!bmp_is_id_valid(instance->ids, id))
return -1;
msg.opcode = code;
msg.invoke_id = id;
msg.name = name;
return write_msg(instance, &msg);
}
static int send_create_or_delete(struct cdap * instance,
char * name,
buffer_t val,
opcode_t code)
{
int id;
cdap_t msg = CDAP__INIT;
int ret;
if (instance == NULL || name == NULL)
return -1;
id = next_invoke_id(instance);
if (!bmp_is_id_valid(instance->ids, id))
return -1;
msg.opcode = code;
msg.name = name;
msg.invoke_id = id;
if (buffer_to_cdap_msg(&msg, &val, 1)) {
release_invoke_id(instance, id);
return -1;
}
ret = write_msg(instance, &msg);
free(msg.value);
return ret;
}
int cdap_send_read(struct cdap * instance,
char * name)
{
return send_read_or_start_or_stop(instance, name, OPCODE__READ);
}
int cdap_send_write(struct cdap * instance,
char * name,
buffer_t * val,
size_t len,
uint32_t flags)
{
int id;
int ret;
cdap_t msg = CDAP__INIT;
if (instance == NULL || name == NULL ||
val == NULL || len < 1)
return -1;
id = next_invoke_id(instance);
if (!bmp_is_id_valid(instance->ids, id))
return -1;
msg.opcode = OPCODE__WRITE;
msg.name = name;
msg.has_flags = true;
msg.flags = flags;
msg.invoke_id = id;
if (buffer_to_cdap_msg(&msg, val, len)) {
release_invoke_id(instance, id);
return -1;
}
ret = write_msg(instance, &msg);
free(msg.value);
return ret;
}
int cdap_send_create(struct cdap * instance,
char * name,
buffer_t val)
{
return send_create_or_delete(instance, name, val, OPCODE__CREATE);
}
int cdap_send_delete(struct cdap * instance,
char * name,
buffer_t val)
{
return send_create_or_delete(instance, name, val, OPCODE__DELETE);
}
int cdap_send_start(struct cdap * instance,
char * name)
{
return send_read_or_start_or_stop(instance, name, OPCODE__START);
}
int cdap_send_stop(struct cdap * instance,
char * name)
{
return send_read_or_start_or_stop(instance, name, OPCODE__STOP);
}
int cdap_send_reply(struct cdap * instance,
int invoke_id,
int result,
buffer_t * val,
size_t len)
{
cdap_t msg = CDAP__INIT;
if (instance == NULL || val == NULL)
return -1;
msg.invoke_id = invoke_id;
msg.has_result = true;
msg.result = result;
if (buffer_to_cdap_msg(&msg, val, len))
return -1;
return write_msg(instance, &msg);
}
|