summaryrefslogtreecommitdiff
path: root/src/ipcpd/ipcp-data.c
blob: 2d1d2752eb6bbaa1e1a7b8f41305d41f1e91a858 (plain)
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
 * Ouroboros - Copyright (C) 2016
 *
 * IPC process utilities
 *
 *    Dimitri Staessens <dimitri.staessens@intec.ugent.be>
 *    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 version 2 as
 * published by the Free Software Foundation.
 *
 * 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.
 */

#define OUROBOROS_PREFIX "ipcp-utils"

#include <ouroboros/config.h>
#include <ouroboros/list.h>
#include <ouroboros/time_utils.h>
#include <ouroboros/logs.h>
#include <ouroboros/errno.h>

#include "ipcp-data.h"
#include "ipcp.h"

#include <string.h>
#include <stdlib.h>
#include <assert.h>

struct reg_entry {
        struct list_head list;
        char *           name;
};

struct dir_entry {
        struct list_head list;
        char *           name;
        uint64_t         addr;
};

static struct reg_entry * reg_entry_create(char * name)
{
        struct reg_entry * entry = malloc(sizeof(*entry));
        if (entry == NULL)
                return NULL;

        assert(name);

        entry->name = name;
        if (entry->name == NULL)
                return NULL;

        return entry;
}

static void reg_entry_destroy(struct reg_entry * entry)
{
        assert(entry);

        if (entry->name != NULL)
                free(entry->name);

        free(entry);
}

static struct dir_entry * dir_entry_create(char *   name,
                                           uint64_t addr)
{
        struct dir_entry * entry = malloc(sizeof(*entry));
        if (entry == NULL)
                return NULL;

        assert(name);

        entry->addr    = addr;
        entry->name = name;
        if (entry->name == NULL)
                return NULL;

        return entry;
}

static void dir_entry_destroy(struct dir_entry * entry)
{
        assert(entry);

        if (entry->name != NULL)
                free(entry->name);

        free(entry);
}

struct ipcp_data * ipcp_data_create()
{
        struct ipcp_data * data = malloc(sizeof(*data));
        if (data == NULL)
                return NULL;

        data->type = 0;

        return data;
}

struct ipcp_data * ipcp_data_init(struct ipcp_data * dst,
                                  enum ipcp_type     ipcp_type)
{
        if (dst == NULL)
                return NULL;

        dst->type  = ipcp_type;
        dst->dif_name = NULL;

        /* init the lists */
        list_head_init(&dst->registry);
        list_head_init(&dst->directory);
        list_head_init(&dst->dir_queries);

        /* init the locks */
        pthread_rwlock_init(&dst->reg_lock, NULL);
        pthread_rwlock_init(&dst->dir_lock, NULL);
        pthread_mutex_init(&dst->dir_queries_lock, NULL);

        return dst;
}

static void clear_registry(struct ipcp_data * data)
{
        struct list_head * h;
        struct list_head * t;

        assert(data);

        list_for_each_safe(h, t, &data->registry) {
                struct reg_entry * e = list_entry(h, struct reg_entry, list);
                list_del(&e->list);
                reg_entry_destroy(e);
        }
}

static void clear_directory(struct ipcp_data * data)
{
        struct list_head * h;
        struct list_head * t;

        assert(data);

        list_for_each_safe(h, t, &data->directory) {
                struct dir_entry * e = list_entry(h, struct dir_entry, list);
                list_del(&e->list);
                dir_entry_destroy(e);
        }
}

static void clear_dir_queries(struct ipcp_data * data)
{
        struct list_head * h;
        struct list_head * t;

        assert(data);

        list_for_each_safe(h, t, &data->dir_queries) {
                struct dir_query * e = list_entry(h, struct dir_query, next);
                list_del(&e->next);
                ipcp_data_dir_query_destroy(e);
        }
}

void ipcp_data_destroy(struct ipcp_data * data)
{
        if (data == NULL)
                return;

        /* clear the lists */
        pthread_rwlock_wrlock(&data->reg_lock);
        clear_registry(data);
        pthread_rwlock_unlock(&data->reg_lock);

        pthread_rwlock_wrlock(&data->dir_lock);
        clear_directory(data);
        pthread_rwlock_unlock(&data->dir_lock);

        pthread_mutex_lock(&data->dir_queries_lock);
        clear_dir_queries(data);
        pthread_mutex_unlock(&data->dir_queries_lock);

        if (data->dif_name != NULL)
                free(data->dif_name);

        pthread_rwlock_destroy(&data->dir_lock);
        pthread_rwlock_destroy(&data->reg_lock);
        pthread_mutex_destroy(&data->dir_queries_lock);

        free(data);
}

static struct reg_entry * find_reg_entry_by_name(struct ipcp_data * data,
                                                 const char *       name)
{
        struct list_head * h;

        assert(data);
        assert(name);

        list_for_each(h, &data->registry) {
                struct reg_entry * e = list_entry(h, struct reg_entry, list);
                if (!strcmp(e->name, name))
                        return e;
        }

        return NULL;
}

static struct dir_entry * find_dir_entry(struct ipcp_data * data,
                                         const char *       name,
                                         uint64_t           addr)
{
        struct list_head * h;
        list_for_each(h, &data->directory) {
                struct dir_entry * e = list_entry(h, struct dir_entry, list);
                if (e->addr == addr && !strcmp(e->name, name))
                        return e;
        }

        return NULL;
}

static struct dir_entry * find_dir_entry_any(struct ipcp_data * data,
                                             const char *       name)
{
        struct list_head * h;
        list_for_each(h, &data->directory) {
                struct dir_entry * e = list_entry(h, struct dir_entry, list);
                if (!strcmp(e->name, name))
                        return e;
        }

        return NULL;
}

int ipcp_data_reg_add_entry(struct ipcp_data * data,
                            char *             name)
{
        struct reg_entry * entry;

        if (data == NULL || name == NULL)
                return -1;

        pthread_rwlock_wrlock(&data->reg_lock);

        if (find_reg_entry_by_name(data, name)) {
                pthread_rwlock_unlock(&data->reg_lock);
                return -1;
        }

        entry = reg_entry_create(name);
        if (entry == NULL) {
                pthread_rwlock_unlock(&data->reg_lock);
                return -1;
        }

        list_add(&entry->list, &data->registry);

        pthread_rwlock_unlock(&data->reg_lock);

        return 0;
}

int ipcp_data_reg_del_entry(struct ipcp_data * data,
                            const char *       name)
{
        struct reg_entry * e;
        if (data == NULL)
                return -1;

        pthread_rwlock_wrlock(&data->reg_lock);

        e = find_reg_entry_by_name(data, name);
        if (e == NULL) {
                pthread_rwlock_unlock(&data->reg_lock);
                return 0; /* nothing to do */
        }

        list_del(&e->list);

        pthread_rwlock_unlock(&data->reg_lock);

        reg_entry_destroy(e);

        return 0;
}

bool ipcp_data_reg_has(struct ipcp_data * data,
                       const char *       name)
{
        bool ret = false;

        if (data == NULL || name == NULL)
                return false;

        pthread_rwlock_rdlock(&data->reg_lock);

        ret = (find_reg_entry_by_name(data, name) != NULL);

        pthread_rwlock_unlock(&data->reg_lock);

        return ret;
}

int ipcp_data_dir_add_entry(struct ipcp_data * data,
                            char *             name,
                            uint64_t           addr)
{
        struct dir_entry * entry;
        char * entry_name;

        if (data == NULL || name == NULL)
                return -1;

        pthread_rwlock_wrlock(&data->dir_lock);

        if (find_dir_entry(data, name, addr) != NULL) {
                pthread_rwlock_unlock(&data->dir_lock);
                return -1;
        }

        entry_name = strdup(name);
        if (entry_name == NULL) {
                pthread_rwlock_unlock(&data->dir_lock);
                return -1;
        }

        entry = dir_entry_create(entry_name, addr);
        if (entry == NULL) {
                pthread_rwlock_unlock(&data->dir_lock);
                return -1;
        }

        list_add(&entry->list,&data->directory);

        pthread_rwlock_unlock(&data->dir_lock);

        LOG_DBG("Added directory entry for %s.", entry_name);

        return 0;
}

int ipcp_data_dir_del_entry(struct ipcp_data * data,
                            const char *       name,
                            uint64_t           addr)
{
        struct dir_entry * e;
        if (data == NULL)
                return -1;

        pthread_rwlock_wrlock(&data->dir_lock);

        e = find_dir_entry(data, name, addr);
        if (e == NULL) {
                pthread_rwlock_unlock(&data->dir_lock);
                return 0; /* nothing to do */
        }

        list_del(&e->list);

        pthread_rwlock_unlock(&data->dir_lock);

        dir_entry_destroy(e);

        return 0;
}

bool ipcp_data_dir_has(struct ipcp_data * data,
                       const char *       name)
{
        bool ret = false;

        pthread_rwlock_rdlock(&data->dir_lock);

        ret = (find_dir_entry_any(data, name) != NULL);

        pthread_rwlock_unlock(&data->dir_lock);

        return ret;
}

uint64_t ipcp_data_dir_get_addr(struct ipcp_data * data,
                                const char *       name)
{
        struct dir_entry * entry;
        uint64_t           addr;

        pthread_rwlock_rdlock(&data->dir_lock);

        entry = find_dir_entry_any(data, name);

        if (entry == NULL) {
                pthread_rwlock_unlock(&data->dir_lock);
                return 0; /* undefined behaviour, 0 may be a valid address */
        }

        addr = entry->addr;

        pthread_rwlock_unlock(&data->dir_lock);

        return addr;
}

struct dir_query * ipcp_data_dir_query_create(char * name)
{
        struct dir_query * query;
        pthread_condattr_t cattr;

        query = malloc(sizeof(*query));
        if (query == NULL)
                return NULL;

        query->name = strdup(name);
        if (query->name == NULL) {
                free(query);
                return NULL;
        }

        query->state = QUERY_INIT;

        pthread_condattr_init(&cattr);
#ifndef __APPLE__
        pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
#endif
        pthread_cond_init(&query->cond, &cattr);
        pthread_mutex_init(&query->lock, NULL);

        list_head_init(&query->next);

        return query;
}

void ipcp_data_dir_query_respond(struct dir_query * query)
{
        assert(query);

        pthread_mutex_lock(&query->lock);

        if (query->state != QUERY_PENDING) {
                pthread_mutex_unlock(&query->lock);
                return;
        }

        query->state = QUERY_RESPONSE;
        pthread_cond_broadcast(&query->cond);

        while (query->state == QUERY_RESPONSE)
                pthread_cond_wait(&query->cond, &query->lock);

        pthread_mutex_unlock(&query->lock);
}

void ipcp_data_dir_query_destroy(struct dir_query * query)
{
        assert(query);

        pthread_mutex_lock(&query->lock);

        if (query->state == QUERY_DESTROY) {
                pthread_mutex_unlock(&query->lock);
                return;
        }

        if (query->state == QUERY_INIT)
                query->state = QUERY_DONE;

        if (query->state == QUERY_PENDING) {
                query->state = QUERY_DESTROY;
                pthread_cond_broadcast(&query->cond);
        }

        while (query->state != QUERY_DONE)
                pthread_cond_wait(&query->cond, &query->lock);

        pthread_mutex_unlock(&query->lock);

        pthread_cond_destroy(&query->cond);
        pthread_mutex_destroy(&query->lock);

        free(query->name);
        free(query);
}

int ipcp_data_dir_query_wait(struct dir_query * query,
                             const struct timespec * timeout)
{
        struct timespec abstime;
        int ret = 0;

        assert(query);
        assert(timeout);

        clock_gettime(PTHREAD_COND_CLOCK, &abstime);
        ts_add(&abstime, timeout, &abstime);

        pthread_mutex_lock(&query->lock);

        if (query->state != QUERY_INIT) {
                pthread_mutex_unlock(&query->lock);
                return -EINVAL;
        }

        query->state = QUERY_PENDING;

        while (query->state == QUERY_PENDING) {
                if ((ret = -pthread_cond_timedwait(&query->cond,
                                                   &query->lock,
                                                   &abstime)) == -ETIMEDOUT)
                        break;
        }

        if (query->state == QUERY_DESTROY)
                ret = -1;

        query->state = QUERY_DONE;
        pthread_cond_broadcast(&query->cond);

        pthread_mutex_unlock(&query->lock);

        return ret;
}