summaryrefslogtreecommitdiff
path: root/src/irmd/main.c
blob: 31dabebbaaf9f135292a9f29c21c2829e80ac817 (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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/*
 * Ouroboros - Copyright (C) 2016
 *
 * The IPC Resource Manager
 *
 *    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.
 */

#define OUROBOROS_PREFIX "irmd"

#include <ouroboros/config.h>
#include <ouroboros/logs.h>
#include <ouroboros/common.h>
#include <ouroboros/sockets.h>
#include <ouroboros/irm.h>
#include <ouroboros/ipcp.h>
#include <ouroboros/da.h>
#include <ouroboros/list.h>
#include <ouroboros/instance_name.h>
#include <ouroboros/utils.h>
#include <ouroboros/dif_config.h>
#include <ouroboros/shm_du_map.h>

#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <limits.h>

/* FIXME: this smells like part of namespace management */
#define ALL_DIFS "*"

struct ipcp_entry {
        struct list_head  next;
        instance_name_t * api;
        char *            dif_name;
};

/* currently supports only registering whatevercast groups of a single AP */
struct reg_name_entry {
        struct list_head next;

        /* generic whatevercast name */
        char *             name;

        /* FIXME: resolve name instead */
        instance_name_t  * api;
        uint32_t           reg_ap_id;
};

struct irm {
        /* FIXME: list of ipcps can be merged with registered names */
        struct list_head ipcps;
        struct list_head reg_names;

        struct shm_du_map * dum;
};

struct irm * instance = NULL;

static struct ipcp_entry * find_ipcp_entry_by_name(instance_name_t * api)
{
        struct ipcp_entry * tmp = NULL;
        struct list_head * pos = NULL;

        list_for_each(pos, &instance->ipcps) {
                struct ipcp_entry * tmp =
                        list_entry(pos, struct ipcp_entry, next);

                if (instance_name_cmp(api, tmp->api) == 0)
                        return tmp;
        }

        return tmp;
}

static instance_name_t * get_ipcp_by_name(char * ap_name)
{
        struct list_head * pos = NULL;

        list_for_each(pos, &instance->ipcps) {
                struct ipcp_entry * e =
                        list_entry(pos, struct ipcp_entry, next);

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

        return NULL;
}

static instance_name_t * get_ipcp_by_dif_name(char * dif_name)
{
        struct list_head * pos = NULL;

        list_for_each(pos, &instance->ipcps) {
                struct ipcp_entry * e =
                        list_entry(pos, struct ipcp_entry, next);

                if (e->dif_name == NULL)
                        continue;

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

        return NULL;
}

static struct reg_name_entry * reg_name_entry_create()
{
        struct reg_name_entry * e = malloc(sizeof(*e));
        if (e == NULL)
                return NULL;

        e->reg_ap_id = rand() % INT_MAX;
        e->name  = NULL;

        INIT_LIST_HEAD(&e->next);

        return e;
}

static struct reg_name_entry * reg_name_entry_init(struct reg_name_entry * e,
                                                   char *                  name,
                                                   instance_name_t       * api)
{
        if (e == NULL || name == NULL || api == NULL)
                return NULL;

        e->name = name;
        e->api  = api;

        return e;
}

static int reg_name_entry_destroy(struct reg_name_entry * e)
{
        if (e == NULL)
                return 0;

        free(e->name);
        instance_name_destroy(e->api);
        return 0;
}

static struct reg_name_entry * find_reg_name_entry_by_name(char * name)
{
        struct list_head * pos = NULL;

        list_for_each(pos, &instance->reg_names) {
                struct reg_name_entry * e =
                        list_entry(pos, struct reg_name_entry, next);

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

        return NULL;
}

static struct reg_name_entry * find_reg_name_entry_by_id(uint32_t reg_ap_id)
{
        struct list_head * pos = NULL;

        list_for_each(pos, &instance->reg_names) {
                struct reg_name_entry * e =
                        list_entry(pos, struct reg_name_entry, next);

                if (reg_ap_id == e->reg_ap_id)
                        return e;
        }

        return NULL;
}

/* FIXME: add only name when we have NSM solved */
static int reg_name_entry_add_name_instance(char * name, instance_name_t * api)
{
        struct reg_name_entry * e = find_reg_name_entry_by_name(name);
        if (e == NULL) {
                e = reg_name_entry_create();
                e = reg_name_entry_init(e, name, api);
                list_add(&e->next, &instance->reg_names);
                return 0;
        }

        /* already exists, we don't have NSM yet */
        return -1;
}

static int reg_name_entry_del_name(char * name)
{
        struct reg_name_entry * e = find_reg_name_entry_by_name(name);
        if (e == NULL)
                return 0;

        list_del(&e->next);

        reg_name_entry_destroy(e);

        return 0;
}

static pid_t create_ipcp(char *         ap_name,
                         enum ipcp_type ipcp_type)
{
        pid_t pid;
        struct ipcp_entry * tmp = NULL;

        pid = ipcp_create(ap_name, ipcp_type);
        if (pid == -1) {
                LOG_ERR("Failed to create IPCP");
                return -1;
        }

        tmp = malloc(sizeof(*tmp));
        if (tmp == NULL) {
                return -1;
        }

        INIT_LIST_HEAD(&tmp->next);

        tmp->api = instance_name_create();
        if (tmp->api == NULL) {
                free(tmp);
                return -1;
        }

        if(instance_name_init_from(tmp->api, ap_name, pid) == NULL) {
                instance_name_destroy(tmp->api);
                free(tmp);
                return -1;
        }

        tmp->dif_name = NULL;

        LOG_DBG("Created IPC process with pid %d", pid);

        list_add(&tmp->next, &instance->ipcps);
        return pid;
}

static int destroy_ipcp(instance_name_t * api)
{
        struct list_head * pos = NULL;
        struct list_head * n = NULL;

        if (api->id == 0)
                api = get_ipcp_by_name(api->name);

        if (api == NULL) {
                LOG_ERR("No such IPCP in the system.");
                return -1;
        }

        LOG_DBG("Destroying ipcp %s-%d", api->name, api->id);

        if (ipcp_destroy(api->id))
                LOG_ERR("Could not destroy IPCP");

        list_for_each_safe(pos, n, &(instance->ipcps)) {
                struct ipcp_entry * tmp =
                        list_entry(pos, struct ipcp_entry, next);

                if (instance_name_cmp(api, tmp->api) == 0)
                        list_del(&tmp->next);
        }

        return 0;
}

static int bootstrap_ipcp(instance_name_t *  api,
                          dif_config_msg_t * conf)
{
        struct ipcp_entry * entry = NULL;

        if (api->id == 0)
                api = get_ipcp_by_name(api->name);

        if (api == NULL) {
                LOG_ERR("No such IPCP in the system.");
                return -1;
        }

        entry = find_ipcp_entry_by_name(api);
        if (entry == NULL) {
                LOG_ERR("No such IPCP");
                return -1;
        }

        entry->dif_name = strdup(conf->dif_name);
        if (entry->dif_name == NULL) {
                LOG_ERR("Failed to strdup");
                return -1;
        }

        if (ipcp_bootstrap(entry->api->id, conf)) {
                LOG_ERR("Could not bootstrap IPCP");
                free(entry->dif_name);
                entry->dif_name = NULL;
                return -1;
        }

        return 0;
}

static int enroll_ipcp(instance_name_t  * api,
                       char *             dif_name)
{
        char *  member = NULL;
        char ** n_1_difs = NULL;
        ssize_t n_1_difs_size = 0;
        struct ipcp_entry * entry = NULL;

        entry = find_ipcp_entry_by_name(api);
        if (entry == NULL) {
                LOG_ERR("No such IPCP");
                return -1;
        }

        entry->dif_name = strdup(dif_name);
        if (entry->dif_name == NULL) {
                LOG_ERR("Failed to strdup");
                return -1;
        }

        member = da_resolve_daf(dif_name);
        if (member == NULL) {
                LOG_ERR("Could not find a member of that DIF");
                free(entry->dif_name);
                entry->dif_name = NULL;
                return -1;
        }

        n_1_difs_size = da_resolve_dap(member, n_1_difs);
        if (n_1_difs_size < 1) {
                LOG_ERR("Could not find N-1 DIFs");
                free(entry->dif_name);
                entry->dif_name = NULL;
                return -1;
        }

        if (ipcp_enroll(entry->api->id, member, n_1_difs[0])) {
                LOG_ERR("Could not enroll IPCP");
                free(entry->dif_name);
                entry->dif_name = NULL;
                return -1;
        }

        return 0;
}

static int reg_ipcp(instance_name_t * api,
                    char **           difs,
                    size_t            difs_size)
{
        if (ipcp_reg(api->id, difs, difs_size)) {
                LOG_ERR("Could not register IPCP to N-1 DIF(s)");
                return -1;
        }

        return 0;
}

static int unreg_ipcp(instance_name_t  * api,
                      char **            difs,
                      size_t             difs_size)
{

        if (ipcp_unreg(api->id, difs, difs_size)) {
                LOG_ERR("Could not unregister IPCP from N-1 DIF(s)");
                return -1;
        }

        return 0;
}

static int ap_unreg_id(uint32_t reg_ap_id,
                       pid_t    pid,
                       char **  difs,
                       size_t   len)
{
        int i;
        int ret = 0;
        struct reg_name_entry * rne    = NULL;
        struct list_head      * pos  = NULL;

        rne = find_reg_name_entry_by_id(reg_ap_id);
        if (rne == NULL)
                return 0; /* no such id */

        if (instance->ipcps.next == NULL) {
                LOG_ERR("No IPCPs in this system.");
                return 0;
        }

        if (strcmp(difs[0], ALL_DIFS) == 0) {
                  list_for_each(pos, &instance->ipcps) {
                        struct ipcp_entry * e =
                                list_entry(pos, struct ipcp_entry, next);

                        if (ipcp_name_unreg(e->api->id, rne->name)) {
                                LOG_ERR("Could not unregister %s in DIF %s.",
                                        rne->name, e->dif_name);
                                --ret;
                        }
                }
        } else {
                for (i = 0; i < len; ++i) {
                        if (ipcp_name_unreg(pid, rne->name)) {
                                LOG_ERR("Could not unregister %s in DIF %s.",
                                        rne->name, difs[i]);
                                --ret;
                        }
                }
        }

        reg_name_entry_del_name(rne->name);

        return ret;
}

static int ap_reg(char *  ap_name,
                  pid_t   ap_id,
                  char ** difs,
                  size_t  len)
{
        int i;
        int ret = 0;
        int reg_ap_id = 0;
        struct list_head * pos = NULL;
        struct reg_name_entry * rne = NULL;

        instance_name_t * api   = NULL;
        instance_name_t * ipcpi = NULL;

        if (instance->ipcps.next == NULL)
                LOG_ERR("No IPCPs in this system.");

        /* check if this ap_name is already registered */
        rne = find_reg_name_entry_by_name(ap_name);
        if (rne != NULL)
                return -1; /* can only register one instance for now */

        rne = reg_name_entry_create();
        if (rne == NULL)
                return -1;

        api = instance_name_create();
        if (instance_name_init_from(api, ap_name, ap_id) == NULL) {
                instance_name_destroy(api);
                return -1;
        }

        /*
         * for now, the whatevercast name is the same as the ap_name and
         * contains a single instance only
         */

        if (reg_name_entry_init(rne, strdup(ap_name), api) == NULL) {
                reg_name_entry_destroy(rne);
                instance_name_destroy(api);
                return -1;
        }

        if (strcmp(difs[0], ALL_DIFS) == 0) {
                list_for_each(pos, &instance->ipcps) {
                        struct ipcp_entry * e =
                                list_entry(pos, struct ipcp_entry, next);

                        if (ipcp_name_reg(e->api->id, ap_name)) {
                                LOG_ERR("Could not register %s in DIF %s.",
                                        api->name, e->dif_name);
                        } else {
                                ++ret;
                        }
                }
        } else {
                for (i = 0; i < len; ++i) {
                        ipcpi = get_ipcp_by_dif_name(difs[i]);
                        if (ipcpi == NULL) {
                                LOG_ERR("%s: No such DIF.", difs[i]);
                                continue;
                        }

                        if (ipcp_name_reg(ipcpi->id, api->name)) {
                                LOG_ERR("Could not register %s in DIF %s.",
                                        api->name, difs[i]);
                        } else {
                                ++ret;
                        }
                }
        }

        if (ret ==  0) {
                instance_name_destroy(api);
                return -1;
        }
        /* for now, we register single instances */
        reg_name_entry_add_name_instance(strdup(ap_name),
                                         instance_name_dup(api));
        instance_name_destroy(api);

        return reg_ap_id;
}

static int ap_unreg(char *  ap_name,
                    pid_t   ap_id,
                    char ** difs,
                    size_t  len)
{
        struct reg_name_entry * tmp = NULL;

        instance_name_t * api = instance_name_create();
        if (api == NULL)
                return -1;

        if (instance_name_init_from(api, ap_name, ap_id) == NULL) {
                instance_name_destroy(api);
                return -1;
        }

        /* check if ap_name is registered */
        tmp = find_reg_name_entry_by_name(api->name);
        if (tmp == NULL) {
                instance_name_destroy(api);
                return 0;
        } else {
                return ap_unreg_id(tmp->reg_ap_id, api->id, difs, len);
        }
}


static int flow_accept(int fd,
                       char * ap_name,
                       char * ae_name)
{
        return -1;
}

static int flow_alloc_resp(int fd,
                           int result)
{

        return -1;
}

static int flow_alloc(char * dst_ap_name,
                      char * src_ap_name,
                      char * src_ae_name,
                      struct qos_spec * qos,
                      int oflags)
{
        return -1;
}

static int flow_alloc_res(int fd)
{

        return -1;
}

static int flow_dealloc(int fd)
{
        return -1;
}

static int flow_cntl(int fd,
                     int oflags)
{
        return -1;
}

static int flow_req_arr(uint32_t reg_api_id,
                        char *   ap_name,
                        char *   ae_name)
{
        return -1;
}

static int flow_alloc_reply(uint32_t port_id,
                            int      result)
{
        return -1;
}

static int flow_dealloc_ipcp(uint32_t port_id)
{
        return -1;
}

void irmd_sig_handler(int sig, siginfo_t * info, void * c)
{
        switch(sig) {
        case SIGINT:
        case SIGTERM:
        case SIGHUP:
                shm_du_map_close(instance->dum);
                free(instance);
                exit(0);
        default:
                return;
        }
}

int main()
{
        int     sockfd;
        uint8_t buf[IRM_MSG_BUF_SIZE];

        struct sigaction sig_act;

        /* init sig_act */
        memset(&sig_act, 0, sizeof sig_act);

        /* install signal traps */
        sig_act.sa_sigaction = &irmd_sig_handler;
        sig_act.sa_flags     = SA_SIGINFO;

        sigaction(SIGINT,  &sig_act, NULL);
        sigaction(SIGTERM, &sig_act, NULL);
        sigaction(SIGHUP,  &sig_act, NULL);

        if (access("/dev/shm/" SHM_DU_MAP_FILENAME, F_OK) != -1)
                unlink("/dev/shm/" SHM_DU_MAP_FILENAME);

        instance = malloc(sizeof(*instance));
        if (instance == NULL)
                return -1;

        if ((instance->dum = shm_du_map_create()) == NULL) {
                free(instance);
                return -1;
        }

        INIT_LIST_HEAD(&instance->ipcps);
        INIT_LIST_HEAD(&instance->reg_names);

        sockfd = server_socket_open(IRM_SOCK_PATH);
        if (sockfd < 0) {
                shm_du_map_close(instance->dum);
                free(instance);
                return -1;
        }

        while (true) {
                int cli_sockfd;
                irm_msg_t * msg;
                ssize_t count;
                instance_name_t api;
                buffer_t buffer;
                irm_msg_t ret_msg = IRM_MSG__INIT;

                ret_msg.code = IRM_MSG_CODE__IRM_REPLY;

                cli_sockfd = accept(sockfd, 0, 0);
                if (cli_sockfd < 0) {
                        LOG_ERR("Cannot accept new connection");
                        continue;
                }

                count = read(cli_sockfd, buf, IRM_MSG_BUF_SIZE);
                if (count <= 0) {
                        LOG_ERR("Failed to read from socket");
                        close(cli_sockfd);
                        continue;
                }

                msg = irm_msg__unpack(NULL, count, buf);
                if (msg == NULL) {
                        close(cli_sockfd);
                        continue;
                }

                api.name = msg->ap_name;
                if (msg->has_api_id == true)
                        api.id = msg->api_id;

                switch (msg->code) {
                case IRM_MSG_CODE__IRM_CREATE_IPCP:
                        ret_msg.has_result = true;
                        ret_msg.result = create_ipcp(msg->ap_name,
                                                     msg->ipcp_type);
                        break;
                case IRM_MSG_CODE__IRM_DESTROY_IPCP:
                        ret_msg.has_result = true;
                        ret_msg.result = destroy_ipcp(&api);
                        break;
                case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP:
                        ret_msg.has_result = true;
                        ret_msg.result = bootstrap_ipcp(&api, msg->conf);
                        break;
                case IRM_MSG_CODE__IRM_ENROLL_IPCP:
                        ret_msg.has_result = true;
                        ret_msg.result = enroll_ipcp(&api,
                                                     msg->dif_name[0]);
                        break;
                case IRM_MSG_CODE__IRM_REG_IPCP:
                        ret_msg.has_result = true;
                        ret_msg.result = reg_ipcp(&api,
                                                  msg->dif_name,
                                                  msg->n_dif_name);
                        break;
                case IRM_MSG_CODE__IRM_UNREG_IPCP:
                        ret_msg.has_result = true;
                        ret_msg.result = unreg_ipcp(&api,
                                                    msg->dif_name,
                                                    msg->n_dif_name);
                        break;
                case IRM_MSG_CODE__IRM_AP_REG:
                        ret_msg.has_fd = true;
                        ret_msg.fd = ap_reg(msg->ap_name,
                                            msg->pid,
                                            msg->dif_name,
                                            msg->n_dif_name);
                        break;
                case IRM_MSG_CODE__IRM_AP_UNREG:
                        ret_msg.has_result = true;
                        ret_msg.result = ap_unreg(msg->ap_name,
                                                  msg->pid,
                                                  msg->dif_name,
                                                  msg->n_dif_name);
                        break;
                case IRM_MSG_CODE__IRM_FLOW_ACCEPT:
                        ret_msg.has_fd = true;
                        ret_msg.fd = flow_accept(msg->fd,
                                                 ret_msg.ap_name,
                                                 ret_msg.ae_name);
                        break;
                case IRM_MSG_CODE__IRM_FLOW_ALLOC_RESP:
                        ret_msg.has_result = true;
                        ret_msg.result = flow_alloc_resp(msg->fd,
                                                         msg->result);
                        break;
                case IRM_MSG_CODE__IRM_FLOW_ALLOC:
                        ret_msg.has_fd = true;
                        ret_msg.fd = flow_alloc(msg->dst_name,
                                                msg->ap_name,
                                                msg->ae_name,
                                                NULL,
                                                msg->oflags);
                        break;
                case IRM_MSG_CODE__IRM_FLOW_ALLOC_RES:
                        ret_msg.has_response = true;
                        ret_msg.response = flow_alloc_res(msg->fd);
                        break;
                case IRM_MSG_CODE__IRM_FLOW_DEALLOC:
                        ret_msg.has_result = true;
                        ret_msg.result = flow_dealloc(msg->fd);
                        break;
                case IRM_MSG_CODE__IRM_FLOW_CONTROL:
                        ret_msg.has_result = true;
                        ret_msg.result = flow_cntl(msg->fd,
                                                   msg->oflags);
                        break;
                case IRM_MSG_CODE__IPCP_FLOW_REQ_ARR:
                        ret_msg.has_port_id = true;
                        ret_msg.port_id = flow_req_arr(msg->port_id,
                                                       msg->ap_name,
                                                       msg->ae_name);
                        break;
                case IRM_MSG_CODE__IPCP_FLOW_ALLOC_REPLY:
                        ret_msg.has_result = true;
                        ret_msg.result = flow_alloc_reply(msg->port_id,
                                                          msg->response);
                        break;
                case IRM_MSG_CODE__IPCP_FLOW_DEALLOC:
                        ret_msg.has_result = true;
                        ret_msg.result = flow_dealloc_ipcp(msg->port_id);
                        break;
                default:
                        LOG_ERR("Don't know that message code");
                        break;
                }

                irm_msg__free_unpacked(msg, NULL);

                buffer.size = irm_msg__get_packed_size(&ret_msg);
                if (buffer.size == 0) {
                        LOG_ERR("Failed to send reply message");
                        close(cli_sockfd);
                        continue;
                }

                buffer.data = malloc(buffer.size);
                if (buffer.data == NULL) {
                        close(cli_sockfd);
                        continue;
                }

                irm_msg__pack(&ret_msg, buffer.data);

                if (write(cli_sockfd, buffer.data, buffer.size) == -1) {
                        free(buffer.data);
                        close(cli_sockfd);
                        continue;
                }

                free(buffer.data);
                close(cli_sockfd);
        }

        return 0;
}