summaryrefslogtreecommitdiff
path: root/src/lib/ipcp.c
blob: 445160f0dc16df0eee62d6a2d65697a9310122d2 (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
/*
 * Ouroboros - Copyright (C) 2016
 *
 * The API to instruct IPCPs
 *
 *    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 "lib-ipcp"

#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199506L
#endif

#include <ouroboros/ipcp.h>
#include <ouroboros/common.h>
#include <ouroboros/logs.h>
#include <ouroboros/config.h>
#include <ouroboros/utils.h>
#include <ouroboros/sockets.h>

#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

static int send_ipcp_msg(pid_t pid,
                         ipcp_msg_t * msg)
{
       int sockfd = 0;
       buffer_t buf;
       char * sock_path;

       sock_path = ipcp_sock_path(pid);
       if (sock_path == NULL)
               return -1;

       sockfd = client_socket_open(sock_path);
       if (sockfd < 0) {
               free(sock_path);
               return -1;
       }

       buf.size = ipcp_msg__get_packed_size(msg);
       if (buf.size == 0) {
               close(sockfd);
               free(sock_path);
               return -1;
       }

       buf.data = malloc(buf.size);
       if (buf.data == NULL) {
               close(sockfd);
               free(sock_path);
               return -ENOMEM;
       }

       ipcp_msg__pack(msg, buf.data);

       if (write(sockfd, buf.data, buf.size) == -1) {
               free(sock_path);
               free(buf.data);
               close(sockfd);
               return -1;
       }

       free(buf.data);
       free(sock_path);
       close(sockfd);
       return 0;
}

pid_t ipcp_create(rina_name_t name,
                  char * ipcp_type)
{
        pid_t pid = 0;
        char * api_id = NULL;
        size_t len = 0;
        char * ipcp_dir = "bin/ipcpd";
        char * full_name = NULL;

        if (ipcp_type == NULL)
                return -1;

        pid = fork();
        if (pid == -1) {
                LOG_ERR("Failed to fork");
                return pid;
        }

        if (pid != 0) {
                return pid;
        }

        api_id = malloc(n_digits(name.api_id) + 1);
        if (!api_id) {
                LOG_ERR("Failed to malloc");
                exit(EXIT_FAILURE);
        }
        sprintf(api_id, "%d", name.api_id);

        len += strlen(INSTALL_DIR);
        len += strlen(ipcp_dir);
        len += 2;
        full_name = malloc(len);
        if (full_name == NULL) {
                LOG_ERR("Failed to malloc");
                free(api_id);
                exit(EXIT_FAILURE);
        }

        strcpy(full_name, INSTALL_DIR);
        strcat(full_name, "/");
        strcat(full_name, ipcp_dir);

        char * argv[] = {full_name,
                         name.ap_name, api_id,
                         ipcp_type, 0};

        char * envp[] = {0};

        execve(argv[0], &argv[0], envp);

        LOG_DBG("%s", strerror(errno));
        LOG_ERR("Failed to load IPCP daemon");
        LOG_ERR("Make sure to run the installed version");
        free(api_id);
        free(full_name);
        exit(EXIT_FAILURE);
}

int ipcp_destroy(pid_t pid)
{
        int status;

        if (kill(pid, SIGTERM)) {
                LOG_ERR("Failed to destroy IPCP");
                return -1;
        }

        if (waitpid(pid, &status, 0) < 0) {
                LOG_ERR("Failed to destroy IPCP");
                return -1;
        }

        return 0;
}

int ipcp_reg(pid_t pid,
             char ** difs,
             size_t difs_size)
{
        ipcp_msg_t msg = IPCP_MSG__INIT;

        if (difs == NULL ||
            difs_size == 0 ||
            difs[0] == NULL)
                return -EINVAL;

        msg.code = IPCP_MSG_CODE__IPCP_REG;
        msg.dif_name = difs;
        msg.n_dif_name = difs_size;

        if (send_ipcp_msg(pid, &msg)) {
                LOG_ERR("Failed to send message to daemon");
                return -1;
        }

        return 0;
}

int ipcp_unreg(pid_t pid,
               char ** difs,
               size_t difs_size)
{
        ipcp_msg_t msg = IPCP_MSG__INIT;

        if (difs == NULL ||
            difs_size == 0 ||
            difs[0] == NULL)
                return -EINVAL;

        msg.code = IPCP_MSG_CODE__IPCP_UNREG;
        msg.dif_name = difs;
        msg.n_dif_name = difs_size;

        if (send_ipcp_msg(pid, &msg)) {
                LOG_ERR("Failed to send message to daemon");
                return -1;
        }

        return 0;
}

int ipcp_bootstrap(pid_t pid,
                   struct dif_config * conf)
{
        ipcp_msg_t msg = IPCP_MSG__INIT;

        msg.code = IPCP_MSG_CODE__IPCP_BOOTSTRAP;

        if (send_ipcp_msg(pid, &msg)) {
                LOG_ERR("Failed to send message to daemon");
                return -1;
        }

        return 0;
}

int ipcp_enroll(pid_t pid,
                char * dif_name,
                char * member_name,
                char ** n_1_difs,
                ssize_t n_1_difs_size)
{
        ipcp_msg_t msg = IPCP_MSG__INIT;

        if (n_1_difs == NULL ||
            n_1_difs_size == 0 ||
            n_1_difs[0] == NULL ||
            dif_name == NULL ||
            member_name == NULL)
                return -EINVAL;

        msg.code = IPCP_MSG_CODE__IPCP_ENROLL;
        msg.dif_name = malloc(sizeof(*(msg.dif_name)));
        if (msg.dif_name == NULL) {
                LOG_ERR("Failed to malloc");
                return -1;
        }
        msg.dif_name[0] = dif_name;
        msg.ap_name = member_name;
        msg.n_1_dif_name = n_1_difs;
        msg.n_n_1_dif_name = n_1_difs_size;

        if (send_ipcp_msg(pid, &msg)) {
                LOG_ERR("Failed to send message to daemon");
                free(msg.dif_name);
                return -1;
        }

        free(msg.dif_name);
        return 0;
}