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
|
/*
* Ouroboros - Copyright (C) 2016 - 2023
*
* Local IPC process
*
* Dimitri Staessens <dimitri@ouroboros.rocks>
* Sander Vrijders <sander@ouroboros.rocks>
*
* 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., http://www.fsf.org/about/contact/.
*/
#if defined(__linux__) || defined(__CYGWIN__)
#define _DEFAULT_SOURCE
#else
#define _POSIX_C_SOURCE 200112L
#endif
#include "config.h"
#define OUROBOROS_PREFIX "ipcpd-local"
#include <ouroboros/hash.h>
#include <ouroboros/logs.h>
#include <ouroboros/errno.h>
#include <ouroboros/dev.h>
#include <ouroboros/fqueue.h>
#include <ouroboros/ipcp.h>
#include <ouroboros/ipcp-dev.h>
#include <ouroboros/local-dev.h>
#include "ipcp.h"
#include "shim-data.h"
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/wait.h>
#include <assert.h>
#define THIS_TYPE IPCP_LOCAL
struct ipcp ipcpi;
struct {
struct shim_data * shim_data;
int in_out[SYS_MAX_FLOWS];
fset_t * flows;
fqueue_t * fq;
pthread_rwlock_t lock;
pthread_t packet_loop;
} local_data;
static int local_data_init(void)
{
int i;
for (i = 0; i < SYS_MAX_FLOWS; ++i)
local_data.in_out[i] = -1;
local_data.flows = fset_create();
if (local_data.flows == NULL)
goto fail_fset;
local_data.fq = fqueue_create();
if (local_data.fq == NULL)
goto fail_fqueue;
local_data.shim_data = shim_data_create();
if (local_data.shim_data == NULL)
goto fail_shim_data;
if (pthread_rwlock_init(&local_data.lock, NULL) < 0)
goto fail_rwlock_init;
return 0;
fail_rwlock_init:
shim_data_destroy(local_data.shim_data);
fail_shim_data:
fqueue_destroy(local_data.fq);
fail_fqueue:
fset_destroy(local_data.flows);
fail_fset:
return -ENOMEM;
}
static void local_data_fini(void){
pthread_rwlock_destroy(&local_data.lock);
shim_data_destroy(local_data.shim_data);
fqueue_destroy(local_data.fq);
fset_destroy(local_data.flows);
}
static void * local_ipcp_packet_loop(void * o)
{
(void) o;
ipcp_lock_to_core();
while (true) {
int fd;
ssize_t idx;
fevent(local_data.flows, local_data.fq, NULL);
while ((fd = fqueue_next(local_data.fq)) >= 0) {
if (fqueue_type(local_data.fq) != FLOW_PKT)
continue;
idx = local_flow_read(fd);
if (idx < 0)
continue;
assert(idx < (SHM_BUFFER_SIZE));
pthread_rwlock_rdlock(&local_data.lock);
fd = local_data.in_out[fd];
pthread_rwlock_unlock(&local_data.lock);
if (fd != -1)
local_flow_write(fd, idx);
}
}
return (void *) 0;
}
static int local_ipcp_bootstrap(const struct ipcp_config * conf)
{
assert(conf);
assert(conf->type == THIS_TYPE);
ipcpi.dir_hash_algo = conf->layer_info.dir_hash_algo;
strcpy(ipcpi.layer_name,conf->layer_info.layer_name);
if (pthread_create(&local_data.packet_loop, NULL,
local_ipcp_packet_loop, NULL)) {
log_err("Failed to create pthread: %s", strerror(errno));
ipcp_set_state(IPCP_INIT);
return -1;
}
return 0;
}
static int local_ipcp_reg(const uint8_t * hash)
{
if (shim_data_reg_add_entry(local_data.shim_data, hash)) {
log_err("Failed to add " HASH_FMT32 " to local registry.",
HASH_VAL32(hash));
return -1;
}
return 0;
}
static int local_ipcp_unreg(const uint8_t * hash)
{
shim_data_reg_del_entry(local_data.shim_data, hash);
log_info("Unregistered " HASH_FMT32 ".", HASH_VAL32(hash));
return 0;
}
static int local_ipcp_query(const uint8_t * hash)
{
int ret;
ret = (shim_data_reg_has(local_data.shim_data, hash) ? 0 : -1);
return ret;
}
static int local_ipcp_flow_alloc(int fd,
const uint8_t * dst,
qosspec_t qs,
const void * data,
size_t len)
{
int out_fd = -1;
log_dbg("Allocating flow to " HASH_FMT32 " on fd %d.",
HASH_VAL32(dst), fd);
assert(dst);
out_fd = ipcp_wait_flow_req_arr(dst, qs, IPCP_LOCAL_MPL, data, len);
if (out_fd < 0) {
log_dbg("Flow allocation failed: %d", out_fd);
return -1;
}
pthread_rwlock_wrlock(&local_data.lock);
local_data.in_out[fd] = out_fd;
local_data.in_out[out_fd] = fd;
pthread_rwlock_unlock(&local_data.lock);
fset_add(local_data.flows, fd);
log_info("Pending local allocation request on fd %d.", fd);
return 0;
}
static int local_ipcp_flow_alloc_resp(int fd,
int response,
const void * data,
size_t len)
{
int out_fd;
time_t mpl = IPCP_LOCAL_MPL;
if (ipcp_wait_flow_resp(fd) < 0)
return -1;
pthread_rwlock_wrlock(&local_data.lock);
if (response) {
if (local_data.in_out[fd] != -1)
local_data.in_out[local_data.in_out[fd]] = fd;
local_data.in_out[fd] = -1;
pthread_rwlock_unlock(&local_data.lock);
return 0;
}
out_fd = local_data.in_out[fd];
if (out_fd == -1) {
pthread_rwlock_unlock(&local_data.lock);
return -1;
}
pthread_rwlock_unlock(&local_data.lock);
fset_add(local_data.flows, fd);
if (ipcp_flow_alloc_reply(out_fd, response, mpl, data, len) < 0)
return -1;
log_info("Flow allocation completed, fds (%d, %d).", out_fd, fd);
return 0;
}
static int local_ipcp_flow_dealloc(int fd)
{
assert(!(fd < 0));
ipcp_flow_fini(fd);
pthread_rwlock_wrlock(&local_data.lock);
fset_del(local_data.flows, fd);
local_data.in_out[fd] = -1;
pthread_rwlock_unlock(&local_data.lock);
flow_dealloc(fd);
log_info("Flow with fd %d deallocated.", fd);
return 0;
}
static struct ipcp_ops local_ops = {
.ipcp_bootstrap = local_ipcp_bootstrap,
.ipcp_enroll = NULL,
.ipcp_connect = NULL,
.ipcp_disconnect = NULL,
.ipcp_reg = local_ipcp_reg,
.ipcp_unreg = local_ipcp_unreg,
.ipcp_query = local_ipcp_query,
.ipcp_flow_alloc = local_ipcp_flow_alloc,
.ipcp_flow_join = NULL,
.ipcp_flow_alloc_resp = local_ipcp_flow_alloc_resp,
.ipcp_flow_dealloc = local_ipcp_flow_dealloc
};
int main(int argc,
char * argv[])
{
if (local_data_init() < 0) {
log_err("Failed to init local data.");
goto fail_data_init;
}
if (ipcp_init(argc, argv, &local_ops, THIS_TYPE) < 0)
goto fail_init;
if (ipcp_start() < 0) {
log_err("Failed to start IPCP.");
goto fail_start;
}
ipcp_sigwait();
if (ipcp_get_state() == IPCP_SHUTDOWN) {
pthread_cancel(local_data.packet_loop);
pthread_join(local_data.packet_loop, NULL);
}
ipcp_stop();
ipcp_fini();
local_data_fini();
exit(EXIT_SUCCESS);
fail_start:
ipcp_fini();
fail_init:
local_data_fini();
fail_data_init:
exit(EXIT_FAILURE);
}
|