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
|
/*
* 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
#define ALLOC_TIMEOUT 10 /* ms */
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){
shim_data_destroy(local_data.shim_data);
fset_destroy(local_data.flows);
fqueue_destroy(local_data.fq);
pthread_rwlock_destroy(&local_data.lock);
}
static void * ipcp_local_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 ipcp_local_bootstrap(const struct ipcp_config * conf)
{
assert(conf);
assert(conf->type == THIS_TYPE);
ipcpi.dir_hash_algo = conf->layer_info.dir_hash_algo;
ipcpi.layer_name = strdup(conf->layer_info.layer_name);
if (ipcpi.layer_name == NULL) {
log_err("Failed to set layer name");
return -ENOMEM;
}
ipcp_set_state(IPCP_OPERATIONAL);
if (pthread_create(&local_data.packet_loop, NULL,
ipcp_local_packet_loop, NULL)) {
ipcp_set_state(IPCP_INIT);
return -1;
}
log_info("Bootstrapped local IPCP with pid %d.", getpid());
return 0;
}
static int ipcp_local_reg(const uint8_t * hash)
{
if (shim_data_reg_add_entry(local_data.shim_data, hash)) {
log_dbg("Failed to add " HASH_FMT " to local registry.",
HASH_VAL(hash));
return -1;
}
log_info("Registered " HASH_FMT ".", HASH_VAL(hash));
return 0;
}
static int ipcp_local_unreg(const uint8_t * hash)
{
shim_data_reg_del_entry(local_data.shim_data, hash);
log_info("Unregistered " HASH_FMT ".", HASH_VAL(hash));
return 0;
}
static int ipcp_local_query(const uint8_t * hash)
{
int ret;
ret = (shim_data_reg_has(local_data.shim_data, hash) ? 0 : -1);
return ret;
}
static int ipcp_local_flow_alloc(int fd,
const uint8_t * dst,
qosspec_t qs,
const void * data,
size_t len)
{
struct timespec ts = {0, ALLOC_TIMEOUT * MILLION};
struct timespec abstime;
int out_fd = -1;
time_t mpl = IPCP_LOCAL_MPL;
log_dbg("Allocating flow to " HASH_FMT " on fd %d.", HASH_VAL(dst), fd);
assert(dst);
clock_gettime(PTHREAD_COND_CLOCK, &abstime);
pthread_mutex_lock(&ipcpi.alloc_lock);
while (ipcpi.alloc_id != -1 && ipcp_get_state() == IPCP_OPERATIONAL) {
ts_add(&abstime, &ts, &abstime);
pthread_cond_timedwait(&ipcpi.alloc_cond,
&ipcpi.alloc_lock,
&abstime);
}
if (ipcp_get_state() != IPCP_OPERATIONAL) {
log_dbg("Won't allocate over non-operational IPCP.");
pthread_mutex_unlock(&ipcpi.alloc_lock);
return -1;
}
assert(ipcpi.alloc_id == -1);
out_fd = ipcp_flow_req_arr(dst, ipcp_dir_hash_len(), qs, mpl,
data, len);
if (out_fd < 0) {
pthread_mutex_unlock(&ipcpi.alloc_lock);
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);
ipcpi.alloc_id = out_fd;
pthread_cond_broadcast(&ipcpi.alloc_cond);
pthread_mutex_unlock(&ipcpi.alloc_lock);
fset_add(local_data.flows, fd);
log_info("Pending local allocation request on fd %d.", fd);
return 0;
}
static int ipcp_local_flow_alloc_resp(int fd,
int response,
const void * data,
size_t len)
{
struct timespec ts = {0, ALLOC_TIMEOUT * MILLION};
struct timespec abstime;
int out_fd = -1;
time_t mpl = IPCP_LOCAL_MPL;
clock_gettime(PTHREAD_COND_CLOCK, &abstime);
pthread_mutex_lock(&ipcpi.alloc_lock);
while (ipcpi.alloc_id != fd && ipcp_get_state() == IPCP_OPERATIONAL) {
ts_add(&abstime, &ts, &abstime);
pthread_cond_timedwait(&ipcpi.alloc_cond,
&ipcpi.alloc_lock,
&abstime);
}
if (ipcp_get_state() != IPCP_OPERATIONAL) {
pthread_mutex_unlock(&ipcpi.alloc_lock);
return -1;
}
ipcpi.alloc_id = -1;
pthread_cond_broadcast(&ipcpi.alloc_cond);
pthread_mutex_unlock(&ipcpi.alloc_lock);
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 ipcp_local_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 = ipcp_local_bootstrap,
.ipcp_enroll = NULL,
.ipcp_connect = NULL,
.ipcp_disconnect = NULL,
.ipcp_reg = ipcp_local_reg,
.ipcp_unreg = ipcp_local_unreg,
.ipcp_query = ipcp_local_query,
.ipcp_flow_alloc = ipcp_local_flow_alloc,
.ipcp_flow_join = NULL,
.ipcp_flow_alloc_resp = ipcp_local_flow_alloc_resp,
.ipcp_flow_dealloc = ipcp_local_flow_dealloc
};
int main(int argc,
char * argv[])
{
if (ipcp_init(argc, argv, &local_ops, THIS_TYPE) < 0)
goto fail_init;
if (local_data_init() < 0) {
log_err("Failed to init local data.");
goto fail_data_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:
local_data_fini();
fail_data_init:
ipcp_fini();
fail_init:
exit(EXIT_FAILURE);
}
|