summaryrefslogtreecommitdiff
path: root/src/irmd/registry.c
blob: 7745efaadf4bcbe8fcb456ef2772b72763c9a56a (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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*
 * Ouroboros - Copyright (C) 2016
 *
 * The IPC Resource Manager - Registry
 *
 *    Dimitri Staessens <dimitri.staessens@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 "registry.h"

#include <ouroboros/config.h>

#define OUROBOROS_PREFIX "registry"

#include <ouroboros/errno.h>
#include <ouroboros/logs.h>
#include <ouroboros/irm_config.h>

#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>

#define reg_entry_has_auto_binding(e)  (reg_entry_get_auto_info(e) != NULL)
#define reg_entry_has_api(e, api)      (reg_entry_get_reg_api(e, api) != NULL)
#define reg_entry_has_binding(e, name) (reg_entry_get_binding(e, name) != NULL)


struct reg_binding {
        struct list_head next;
        char *           apn;
        uint32_t         flags;
        char **          argv;
};

struct reg_dif {
        struct list_head next;
        char *           dif_name;
        enum ipcp_type   type;
};

enum api_state {
        REG_I_NULL = 0,
        REG_I_INIT,
        REG_I_SLEEP,
        REG_I_WAKE,
        REG_I_DESTROY
};

struct reg_api {
        struct list_head next;
        pid_t            api;

        /* the api will block on this */
        enum api_state   state;
        pthread_cond_t   state_cond;
        pthread_mutex_t  state_lock;
};

static struct reg_api * reg_api_create(pid_t api)
{
        struct reg_api * i;
        i = malloc(sizeof(*i));
        if (i == NULL)
                return NULL;

        i->api   = api;
        i->state = REG_I_INIT;

        pthread_mutex_init(&i->state_lock, NULL);
        pthread_cond_init(&i->state_cond, NULL);

        INIT_LIST_HEAD(&i->next);

        return i;
}

static void reg_api_destroy(struct reg_api * i)
{
        pthread_mutex_lock(&i->state_lock);

        if (i->state != REG_I_NULL)
                i->state = REG_I_DESTROY;

        pthread_cond_signal(&i->state_cond);

        pthread_mutex_unlock(&i->state_lock);

        pthread_cleanup_push((void(*)(void *)) pthread_mutex_unlock,
                             (void *) &i->state_lock);

        while (i->state != REG_I_NULL)
                pthread_cond_wait(&i->state_cond, &i->state_lock);

        pthread_cleanup_pop(true);

        pthread_cond_destroy(&i->state_cond);
        pthread_mutex_destroy(&i->state_lock);

        free(i);
}

static void cleanup_sleeper(void * o) {
        struct reg_api * i = (struct reg_api *) o;
        i->state = REG_I_NULL;
        pthread_cond_signal(&i->state_cond);
        pthread_mutex_unlock(&i->state_lock);
}

void reg_api_sleep(struct reg_api * i)
{
        if (i == NULL)
                return;

        pthread_mutex_lock(&i->state_lock);
        if (i->state != REG_I_INIT) {
                pthread_mutex_unlock(&i->state_lock);
                return;
        }

        i->state = REG_I_SLEEP;

        pthread_cleanup_push(cleanup_sleeper, (void *) i);

        while (i->state == REG_I_SLEEP)
                pthread_cond_wait(&i->state_cond, &i->state_lock);

        pthread_cleanup_pop(true);
}

void reg_api_wake(struct reg_api * i)
{
        pthread_mutex_lock(&i->state_lock);

        if (i->state == REG_I_NULL) {
                pthread_mutex_unlock(&i->state_lock);
                return;
        }

        i->state = REG_I_WAKE;

        pthread_cond_broadcast(&i->state_cond);

        while (i->state == REG_I_WAKE)
                pthread_cond_wait(&i->state_cond, &i->state_lock);

        pthread_mutex_unlock(&i->state_lock);
}

static struct reg_binding * reg_binding_create(char *   apn,
                                        uint32_t flags,
                                        char **  argv)
{
        struct reg_binding * b = malloc(sizeof(*b));
        if (b == NULL)
                return NULL;

        INIT_LIST_HEAD(&b->next);

        b->apn   = apn;
        b->flags = flags;
        b->argv  = argv;

        return b;
}

static void reg_binding_destroy(struct reg_binding * b)
{
        if (b == NULL)
                return;

        if (b->argv != NULL) {
                char ** t = b->argv;
                while (*t != NULL)
                        free(*t++);
                free(b->argv);
        }

        free(b->apn);
        free(b);
}

static struct reg_entry * reg_entry_create()
{
        struct reg_entry * e = malloc(sizeof(*e));
        if (e == NULL)
                return NULL;

        e->name         = NULL;
        e->state        = REG_NAME_NULL;

        e->req_ae_name  = NULL;
        e->response     = -1;

        return e;
}

static struct reg_entry * reg_entry_init(struct reg_entry * e,
                                         char *             name)
{
        if (e == NULL || name == NULL)
                return NULL;

        INIT_LIST_HEAD(&e->next);
        INIT_LIST_HEAD(&e->difs);
        INIT_LIST_HEAD(&e->bindings);
        INIT_LIST_HEAD(&e->reg_apis);

        e->name = name;

        if (pthread_cond_init(&e->state_cond, NULL))
                return NULL;

        if (pthread_mutex_init(&e->state_lock, NULL))
                return NULL;

        e->state = REG_NAME_IDLE;

        return e;
}

static void reg_entry_destroy(struct reg_entry * e)
{
        struct list_head * pos = NULL;
        struct list_head * n   = NULL;

        if (e == NULL)
                return;

        pthread_mutex_lock(&e->state_lock);

        e->state = REG_NAME_DESTROY;

        pthread_cond_broadcast(&e->state_cond);
        pthread_mutex_unlock(&e->state_lock);

        pthread_mutex_destroy(&e->state_lock);

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

        list_for_each_safe(pos, n, &e->reg_apis) {
                struct reg_api * i = list_entry(pos, struct reg_api, next);
                reg_api_destroy(i);
        }

        list_for_each_safe(pos, n, &e->bindings) {
                struct reg_binding * b =
                        list_entry(pos, struct reg_binding, next);
                reg_binding_destroy(b);
        }

        list_for_each_safe(pos, n, &e->difs) {
                struct reg_dif * d =
                        list_entry(pos, struct reg_dif, next);
                free(d->dif_name);
                free(d);
        }

        free(e);
}

bool reg_entry_is_local_in_dif(struct reg_entry * e,
                               char *             dif_name)
{
        struct list_head * pos = NULL;

        list_for_each(pos, &e->difs) {
                struct reg_dif * d =
                        list_entry(pos, struct reg_dif, next);

                if (!strcmp(dif_name, d->dif_name))
                        return true;
        }

        return false;
}

int reg_entry_add_local_in_dif(struct reg_entry * e,
                               char *             dif_name,
                               enum ipcp_type     type)
{
        if (!reg_entry_is_local_in_dif(e, dif_name)) {
                struct reg_dif * rdn = malloc(sizeof(*rdn));
                rdn->dif_name = strdup(dif_name);
                if (rdn->dif_name == NULL)
                        return -1;
                rdn->type = type;
                list_add(&rdn->next, &e->difs);
                return 0;
        }

        return 0; /* already registered. Is ok */
}

void reg_entry_del_local_from_dif(struct reg_entry * e,
                                  char *             dif_name)
{
        struct list_head * pos = NULL;
        struct list_head * n   = NULL;

        list_for_each_safe(pos, n, &e->difs) {
                struct reg_dif * d =
                        list_entry(pos, struct reg_dif, next);

                if (!strcmp(dif_name, d->dif_name)) {
                        list_del(&d->next);
                        free(d);
                }
        }
}

struct reg_binding * reg_entry_get_binding(struct reg_entry * e,
                                           char *             apn)
{
        struct list_head * pos = NULL;

        list_for_each(pos, &e->bindings) {
                struct reg_binding * n =
                        list_entry(pos, struct reg_binding, next);

                if (strcmp(apn, n->apn) == 0)
                        return n;
        }

        return NULL;
}

void reg_entry_del_binding(struct reg_entry * e,
                           char *             apn)
{
        struct reg_binding * b = reg_entry_get_binding(e, apn);
        if (b == NULL)
                return;

        list_del(&b->next);
        free(b);
}

struct reg_binding * reg_entry_add_binding(struct reg_entry * e,
                                           char *             apn,
                                           uint32_t           flags,
                                           char **            argv)
{
        struct reg_binding * b;
        if ((b = reg_entry_get_binding(e, apn)) != NULL) {
                LOG_DBG("Updating AP name %s binding with %s.",
                        apn, e->name);
                reg_entry_del_binding(e, b->apn);
        }

        if (flags & BIND_AP_AUTO) {
                b = reg_binding_create(apn, flags, argv);
                if (e->state == REG_NAME_IDLE)
                        e->state = REG_NAME_AUTO_ACCEPT;
        } else {
                flags &= ~BIND_AP_AUTO;
                b = reg_binding_create(apn, flags, NULL);
        }

        list_add(&b->next, &e->bindings);

        return b;
}

char ** reg_entry_get_auto_info(struct reg_entry * e)
{
        struct list_head * pos = NULL;

        list_for_each(pos, &e->bindings) {
                struct reg_binding * b =
                        list_entry(pos, struct reg_binding, next);
                if (b->flags & BIND_AP_AUTO)
                    return b->argv;
        }

        return NULL;
}

struct reg_api * reg_entry_get_reg_api(struct reg_entry * e,
                                       pid_t              api)
{
        struct list_head * pos = NULL;

        list_for_each(pos, &e->reg_apis) {
                struct reg_api * r =
                        list_entry(pos, struct reg_api, next);

                if (r->api == api)
                        return r;
        }

        return NULL;
}

pid_t reg_entry_resolve_api(struct reg_entry * e)
{
        struct list_head * pos = NULL;

        /* FIXME: now just returns the first accepting instance */
        list_for_each(pos, &e->reg_apis) {
                struct reg_api * r =
                        list_entry(pos, struct reg_api, next);
                return r->api;
        }

        return -1;
}

struct reg_entry * registry_get_entry_by_name(struct list_head * registry,
                                              char *             name)
{
        struct list_head * pos = NULL;

        list_for_each(pos, registry) {
                struct reg_entry * e =
                        list_entry(pos, struct reg_entry, next);

                if (strcmp(name, e->name) == 0)
                        return e;
        }

        return NULL;
}

struct reg_entry * registry_get_entry_by_apn(struct list_head * registry,
                                             char *             apn)
{
        struct list_head * pos = NULL;

        list_for_each(pos, registry) {
                struct list_head * p = NULL;
                struct reg_entry * e =
                        list_entry(pos, struct reg_entry, next);

                list_for_each(p, &e->bindings) {
                        struct reg_binding * b =
                                list_entry(p, struct reg_binding, next);

                        if (strcmp(b->apn, apn) == 0)
                                return e;
                }
        }

        return NULL;
}

struct reg_entry * registry_get_entry_by_api(struct list_head * registry,
                                             pid_t              api)
{
        struct list_head * pos = NULL;

        list_for_each(pos, registry) {
                struct list_head * p = NULL;
                struct reg_entry * e =
                        list_entry(pos, struct reg_entry, next);

                list_for_each(p, &e->reg_apis) {
                        struct reg_api * r =
                                list_entry(p, struct reg_api, next);

                        if (r->api == api)
                                return e;
                }
        }

        return NULL;
}

struct reg_entry * registry_assign(struct list_head * registry,
                                   char *             name)
{
        struct reg_entry * e = NULL;

        if (name == NULL)
                return NULL;

        if (registry_has_name(registry, name)) {
                LOG_DBG("Name %s already registered.", name);
                return NULL;
        }

        e = reg_entry_create();
        if (e == NULL) {
                LOG_DBG("Could not create registry entry.");
                return NULL;
        }

        e = reg_entry_init(e, name);
        if (e == NULL) {
                LOG_DBG("Could not initialize registry entry.");
                reg_entry_destroy(e);
                return NULL;
        }

        list_add(&e->next, registry);

        return e;
}

void registry_deassign(struct list_head * registry,
                       char *             name)
{
        struct reg_entry * e = registry_get_entry_by_name(registry, name);
        if (e == NULL)
                return;

        list_del(&e->next);
        reg_entry_destroy(e);

        return;
}

int registry_add_binding(struct list_head * registry,
                         char *             name,
                         char *             apn,
                         uint32_t           flags,
                         char **            argv)
{
        struct reg_entry * e;

        if (name == NULL || apn == NULL)
                return -EINVAL;

        e = registry_get_entry_by_name(registry, name);
        if (e == NULL) {
                LOG_DBG("Adding new name to registry: %s.", name);
                e = registry_assign(registry, name);
        }

        if (e->state == REG_NAME_NULL) {
                LOG_DBG("Tried to add binding in NULL state.");
                return -1;
        }

        if(reg_entry_add_binding(e, apn, flags, argv) == NULL)
                return -1;

        return 0;
}


void registry_del_binding(struct list_head * registry,
                          char *             name,
                          char *             apn)
{
        struct reg_entry *   e = NULL;

        if (name == NULL || apn == NULL)
                return;

        e = registry_get_entry_by_name(registry, name);
        if (e == NULL) {
                LOG_DBG("Name %s not found in registry.", name);
                return;
        }

        reg_entry_del_binding(e, apn);

        if (e->state == REG_NAME_AUTO_ACCEPT && !reg_entry_has_auto_binding(e))
                e->state = REG_NAME_IDLE;

        return;
}


struct reg_api * registry_add_api_name(struct list_head * registry,
                                       pid_t              api,
                                       char *             name)
{
        struct reg_entry * e = NULL;
        struct reg_api *   i = NULL;

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

        e = registry_get_entry_by_name(registry, name);
        if (e == NULL) {
                LOG_DBG("Name %s not found in registry.", name);
                return NULL;
        }

        if (e->state == REG_NAME_NULL) {
                LOG_DBG("Tried to add instance in NULL state.");
                return NULL;
        }

        if (reg_entry_has_api(e, api)) {
                LOG_DBG("Instance already registered with this name.");
                return NULL;
        }

        i = reg_api_create(api);
        if (i == NULL) {
                LOG_DBG("Failed to create reg_instance");
                return NULL;
        }

        if (e->state == REG_NAME_IDLE || e->state == REG_NAME_AUTO_ACCEPT
           || e->state == REG_NAME_AUTO_EXEC) {
                e->state = REG_NAME_FLOW_ACCEPT;
                pthread_cond_signal(&e->state_cond);
        }

        list_add(&i->next, &e->reg_apis);

        return i;
}

void registry_del_api(struct list_head * registry,
                      pid_t              api)
{
        struct reg_entry * e = NULL;
        struct reg_api * i   = NULL;

        if ( api == -1)
                return;

        e = registry_get_entry_by_api(registry, api);
        if (e == NULL) {
                LOG_DBG("Instance %d not found.", api);
                return;
        }

        i = reg_entry_get_reg_api(e, api);
        if (i == NULL) {
                LOG_DBG("Instance %d is not accepting flows for %s.",
                         api, e->name);
                return;
        }

        list_del(&i->next);

        reg_api_destroy(i);

        if (list_empty(&e->reg_apis)) {
                if (reg_entry_has_auto_binding(e))
                        e->state = REG_NAME_AUTO_ACCEPT;
                else
                        e->state = REG_NAME_IDLE;
        } else {
                e->state = REG_NAME_FLOW_ACCEPT;
        }

        pthread_cond_signal(&e->state_cond);

        return;
}

void registry_sanitize_apis(struct list_head * registry)
{
        struct list_head * pos = NULL;
        struct list_head * n   = NULL;

        struct list_head * pos2 = NULL;
        struct list_head * n2   = NULL;

        list_for_each_safe(pos, n, registry) {
                struct reg_entry * e = list_entry(pos, struct reg_entry, next);
                list_for_each_safe(pos2, n2, &e->reg_apis) {
                        struct reg_api * r
                                = list_entry(pos2, struct reg_api, next);
                        if (kill(r->api, 0) < 0) {
                                LOG_DBG("Process %d gone, binding removed.",
                                        r->api);
                                registry_del_api(registry, r->api);
                        }
                }
        }
}

char * registry_get_dif_for_dst(struct list_head * registry,
                                char *             dst_name)
{
        struct list_head * pos = NULL;
        struct reg_entry * re =
                registry_get_entry_by_name(registry, dst_name);

        if (re != NULL) { /* local AP */
                list_for_each(pos, &re->difs) {
                        struct reg_dif  * rd =
                                list_entry(pos, struct reg_dif, next);
                        if (rd->type == IPCP_LOCAL)
                                return rd->dif_name;
                }

                list_for_each(pos, &re->difs) {
                        struct reg_dif * rd =
                                list_entry(pos, struct reg_dif, next);
                        if (rd->type == IPCP_NORMAL)
                                return rd->dif_name;
                }

                list_for_each(pos, &re->difs) {
                        struct reg_dif * rd =
                                list_entry(pos, struct reg_dif, next);
                        if (rd->type == IPCP_SHIM_UDP)
                                return rd->dif_name;
                }

                LOG_DBG("Could not find DIF for %s.", dst_name);

                return NULL;
        } else {
                LOG_DBG("No local ap %s found.", dst_name);
                return NULL;
        }
}

int registry_add_name_to_dif(struct list_head * registry,
                             char *             name,
                             char *             dif_name,
                             enum ipcp_type     type)
{
        struct reg_entry * re = registry_get_entry_by_name(registry, name);
        if (re == NULL)
                return -1;

        return reg_entry_add_local_in_dif(re, dif_name, type);
}

void registry_del_name_from_dif(struct list_head * registry,
                                char *             name,
                                char *             dif_name)
{
        struct reg_entry * re = registry_get_entry_by_name(registry, name);
        if (re == NULL)
                return;

        reg_entry_del_local_from_dif(re, dif_name);
}

void registry_destroy(struct list_head * registry)
{
        struct list_head * h = NULL;
        struct list_head * t = NULL;

        if (registry == NULL)
                return;

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