summaryrefslogtreecommitdiff
path: root/src/irmd/main.c
blob: 547286e8401998c48c864ecc50bf0387d125bd3d (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
/*
 * 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/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 <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <errno.h>

struct name_to_pid_entry {
        struct list_head  next;
        pid_t             pid;
        instance_name_t * api;
};

struct irm {
        struct list_head name_to_pid;
};

static pid_t find_pid_by_name(struct irm *      instance,
                              instance_name_t * api)
{
        struct list_head * pos;

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

                LOG_DBG("name is %s", api->name);

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

        return 0;
}

static void create_ipcp(struct irm *      instance,
                        instance_name_t * api,
                        char *            ipcp_type)
{
        pid_t pid;
        struct name_to_pid_entry * tmp;

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

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

        INIT_LIST_HEAD(&tmp->next);

        tmp->pid = pid;
        tmp->api = instance_name_dup(api);
        if (tmp->api == NULL) {
                free(tmp);
                return;
        }

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

        list_add(&tmp->next, &instance->name_to_pid);
}

static void destroy_ipcp(struct irm *      instance,
                         instance_name_t * api)
{
        pid_t pid = 0;
        struct list_head * pos;
        struct list_head * n;

        pid = find_pid_by_name(instance, api);
        if (pid == 0) {
                LOG_ERR("No such IPCP");
                return;
        }

        LOG_DBG("Destroying ipcp with pid %d", pid);

        if (ipcp_destroy(pid))
                LOG_ERR("Could not destroy IPCP");

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

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

static void bootstrap_ipcp(struct irm *        instance,
                           instance_name_t *   api,
                           struct dif_config * conf)
{
        pid_t pid = 0;

        pid = find_pid_by_name(instance, api);
        if (pid == 0) {
                LOG_ERR("No such IPCP");
                return;
        }

        if (ipcp_bootstrap(pid, conf))
                LOG_ERR("Could not bootstrap IPCP");
}

static void enroll_ipcp(struct irm *       instance,
                        instance_name_t  * api,
                        char *             dif_name)
{
        pid_t   pid = 0;
        char *  member;
        char ** n_1_difs = NULL;
        ssize_t n_1_difs_size = 0;

        pid = find_pid_by_name(instance, api);
        if (pid == 0) {
                LOG_ERR("No such IPCP");
                return;
        }

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

        n_1_difs_size = da_resolve_dap(member, n_1_difs);
        if (n_1_difs_size != 0)
                if (ipcp_enroll(pid, dif_name, member,
                                n_1_difs, n_1_difs_size))
                        LOG_ERR("Could not enroll IPCP");
}

static void reg_ipcp(struct irm *      instance,
                     instance_name_t * api,
                     char **           difs,
                     size_t            difs_size)
{
        pid_t pid = 0;

        pid = find_pid_by_name(instance, api);
        if (pid == 0) {
                LOG_ERR("No such IPCP");
                return;
        }

        if (ipcp_reg(pid, difs, difs_size))
                LOG_ERR("Could not register IPCP to N-1 DIF(s)");
}

static void unreg_ipcp(struct irm *       instance,
                       instance_name_t  * api,
                       char **            difs,
                       size_t             difs_size)
{
        pid_t pid = 0;

        pid = find_pid_by_name(instance, api);
        if (pid == 0) {
                LOG_ERR("No such IPCP");
                return;
        }

        if (ipcp_unreg(pid, difs, difs_size))
                LOG_ERR("Could not unregister IPCP from N-1 DIF(s)");
}

/* FIXME: Close sockfd on closing and release irm */
int main()
{
        struct irm * instance = NULL;
        int          sockfd;
        uint8_t      buf[IRM_MSG_BUF_SIZE];

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

        INIT_LIST_HEAD(&instance->name_to_pid);

        sockfd = server_socket_open(IRM_SOCK_PATH);
        if (sockfd < 0)
                return -1;

        while (true) {
                int cli_sockfd;
                irm_msg_t * msg;
                ssize_t count;
                instance_name_t api;

                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) {
                        msg = irm_msg__unpack(NULL, count, buf);
                        if (msg == NULL)
                                continue;

                        api.name = msg->ap_name;
                        api.id   = msg->api_id;

                        switch (msg->code) {
                        case IRM_MSG_CODE__IRM_CREATE_IPCP:
                                create_ipcp(instance, &api, msg->ipcp_type);
                                break;
                        case IRM_MSG_CODE__IRM_DESTROY_IPCP:
                                destroy_ipcp(instance, &api);
                                break;
                        case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP:
                                bootstrap_ipcp(instance, &api, NULL);
                                break;
                        case IRM_MSG_CODE__IRM_ENROLL_IPCP:
                                if (msg->n_dif_name != 1)
                                        continue;
                                enroll_ipcp(instance, &api, msg->dif_name[0]);
                                break;
                        case IRM_MSG_CODE__IRM_REG_IPCP:
                                reg_ipcp(instance, &api,
                                         msg->dif_name,
                                         msg->n_dif_name);
                                break;
                        case IRM_MSG_CODE__IRM_UNREG_IPCP:
                                unreg_ipcp(instance, &api,
                                           msg->dif_name,
                                           msg->n_dif_name);
                                break;
                        default:
                                LOG_ERR("Don't know that message code");
                                break;
                        }

                        irm_msg__free_unpacked(msg, NULL);
                }

                close(cli_sockfd);
        }

        return 0;
}