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
|
/*
* Ouroboros - Copyright (C) 2016 - 2017
*
* Handles the different AP connections
*
* Sander Vrijders <sander.vrijders@ugent.be>
* Dimitri Staessens <dimitri.staessens@ugent.be>
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define OUROBOROS_PREFIX "normal-ipcp"
#include <ouroboros/config.h>
#include <ouroboros/logs.h>
#include <ouroboros/dev.h>
#include <ouroboros/cacep.h>
#include <ouroboros/cdap.h>
#include <ouroboros/errno.h>
#include "ae.h"
#include "connmgr.h"
#include "enroll.h"
#include "fmgr.h"
#include "frct.h"
#include "ipcp.h"
#include "ribmgr.h"
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define FRCT_PROTO "frct"
struct ae_conn {
struct list_head next;
struct conn conn;
};
struct ae {
struct list_head next;
struct conn_info info;
struct list_head conn_list;
pthread_cond_t conn_cond;
pthread_mutex_t conn_lock;
};
struct {
pthread_t acceptor;
struct list_head aes;
pthread_mutex_t aes_lock;
} connmgr;
static int add_ae_conn(struct ae * ae,
int fd,
qosspec_t qs,
struct conn_info * rcv_info)
{
struct ae_conn * ae_conn = NULL;
ae_conn = malloc(sizeof(*ae_conn));
if (ae_conn == NULL) {
log_err("Not enough memory.");
return -1;
}
ae_conn->conn.conn_info = *rcv_info;
ae_conn->conn.flow_info.fd = fd;
ae_conn->conn.flow_info.qs = qs;
list_head_init(&ae_conn->next);
pthread_mutex_lock(&ae->conn_lock);
list_add(&ae_conn->next, &ae->conn_list);
pthread_cond_signal(&ae->conn_cond);
pthread_mutex_unlock(&ae->conn_lock);
return 0;
}
static struct ae * find_ae_by_name(char * name)
{
struct list_head * p = NULL;
list_for_each(p, &connmgr.aes) {
struct ae * ae = list_entry(p, struct ae, next);
if (strcmp(ae->info.ae_name, name) == 0)
return ae;
}
return NULL;
}
static void * flow_acceptor(void * o)
{
int fd;
qosspec_t qs;
struct conn_info rcv_info;
struct conn_info fail_info;
struct ae * ae = NULL;
(void) o;
memset(&fail_info, 0, sizeof(fail_info));
while (true) {
pthread_rwlock_rdlock(&ipcpi.state_lock);
if (ipcp_get_state() != IPCP_OPERATIONAL) {
pthread_rwlock_unlock(&ipcpi.state_lock);
log_info("Shutting down flow acceptor.");
return 0;
}
pthread_rwlock_unlock(&ipcpi.state_lock);
fd = flow_accept(&qs);
if (fd < 0) {
if (fd != -EIRMD)
log_warn("Flow accept failed: %d", fd);
continue;
}
if (flow_alloc_resp(fd, 0)) {
log_err("Failed to respond to flow alloc request.");
continue;
}
if (cacep_rcv(fd, &rcv_info)) {
log_err("Error establishing application connection.");
flow_dealloc(fd);
continue;
}
pthread_mutex_lock(&connmgr.aes_lock);
ae = find_ae_by_name(rcv_info.ae_name);
pthread_mutex_unlock(&connmgr.aes_lock);
if (ae != NULL) {
if (cacep_snd(fd, &ae->info)) {
log_err("Failed to respond to req.");
flow_dealloc(fd);
continue;
}
if (add_ae_conn(ae, fd, qs, &rcv_info)) {
log_err("Failed to add ae conn.");
flow_dealloc(fd);
continue;
}
} else {
cacep_snd(fd, &fail_info);
flow_dealloc(fd);
}
}
return (void *) 0;
}
int connmgr_init(void)
{
list_head_init(&connmgr.aes);
if (pthread_mutex_init(&connmgr.aes_lock, NULL))
return -1;
return 0;
}
int connmgr_start(void)
{
pthread_create(&connmgr.acceptor, NULL, flow_acceptor, NULL);
return 0;
}
void connmgr_stop(void)
{
pthread_cancel(connmgr.acceptor);
pthread_join(connmgr.acceptor, NULL);
}
void connmgr_fini(void)
{
struct list_head * p = NULL;
struct list_head * n = NULL;
pthread_mutex_lock(&connmgr.aes_lock);
list_for_each_safe(p, n, &connmgr.aes) {
struct ae * e = list_entry(p, struct ae, next);
connmgr_ae_destroy(e);
}
pthread_mutex_unlock(&connmgr.aes_lock);
pthread_mutex_destroy(&connmgr.aes_lock);
}
struct ae * connmgr_ae_create(struct conn_info info)
{
struct ae * ae;
ae = malloc(sizeof(*ae));
if (ae == NULL)
return NULL;
list_head_init(&ae->next);
list_head_init(&ae->conn_list);
ae->info = info;
if (pthread_mutex_init(&ae->conn_lock, NULL)) {
free(ae);
return NULL;
}
if (pthread_cond_init(&ae->conn_cond, NULL)) {
pthread_mutex_destroy(&ae->conn_lock);
free(ae);
return NULL;
}
pthread_mutex_lock(&connmgr.aes_lock);
list_add(&ae->next, &connmgr.aes);
pthread_mutex_unlock(&connmgr.aes_lock);
return ae;
}
void connmgr_ae_destroy(struct ae * ae)
{
struct list_head * p = NULL;
struct list_head * n = NULL;
assert(ae);
pthread_mutex_lock(&connmgr.aes_lock);
pthread_mutex_lock(&ae->conn_lock);
list_for_each_safe(p, n, &ae->conn_list) {
struct ae_conn * e = list_entry(p, struct ae_conn, next);
list_del(&e->next);
free(e);
}
pthread_mutex_unlock(&ae->conn_lock);
pthread_cond_destroy(&ae->conn_cond);
pthread_mutex_destroy(&ae->conn_lock);
list_del(&ae->next);
pthread_mutex_unlock(&connmgr.aes_lock);
free(ae);
}
int connmgr_alloc(struct ae * ae,
char * dst_name,
qosspec_t qs,
struct conn * conn)
{
assert(ae);
assert(dst_name);
assert(conn);
memset(&conn->conn_info, 0, sizeof(conn->conn_info));
conn->flow_info.fd = flow_alloc(dst_name, &qs);
if (conn->flow_info.fd < 0) {
log_err("Failed to allocate flow to %s.", dst_name);
return -1;
}
conn->flow_info.qs = qs;
if (flow_alloc_res(conn->flow_info.fd)) {
log_err("Flow allocation to %s failed.", dst_name);
flow_dealloc(conn->flow_info.fd);
return -1;
}
if (cacep_snd(conn->flow_info.fd, &ae->info)) {
log_err("Failed to create application connection.");
flow_dealloc(conn->flow_info.fd);
return -1;
}
if (cacep_rcv(conn->flow_info.fd, &conn->conn_info)) {
log_err("Failed to connect to application.");
flow_dealloc(conn->flow_info.fd);
return -1;
}
if (strcmp(ae->info.protocol, conn->conn_info.protocol) ||
ae->info.pref_version != conn->conn_info.pref_version ||
ae->info.pref_syntax != conn->conn_info.pref_syntax) {
flow_dealloc(conn->flow_info.fd);
return -1;
}
return 0;
}
int connmgr_wait(struct ae * ae,
struct conn * conn)
{
struct ae_conn * ae_conn;
assert(ae);
assert(conn);
pthread_mutex_lock(&ae->conn_lock);
pthread_cleanup_push((void(*)(void *))pthread_mutex_unlock,
(void *) &ae->conn_lock);
while (list_is_empty(&ae->conn_list))
pthread_cond_wait(&ae->conn_cond, &ae->conn_lock);
ae_conn = list_first_entry((&ae->conn_list), struct ae_conn, next);
if (ae_conn == NULL) {
pthread_mutex_unlock(&ae->conn_lock);
return -1;
}
*conn = ae_conn->conn;
list_del(&ae_conn->next);
free(ae_conn);
pthread_cleanup_pop(true);
return 0;
}
|