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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/* $OpenBSD: transport.c,v 1.39 2021/01/28 01:18:44 mortimer Exp $ */
/* $EOM: transport.c,v 1.43 2000/10/10 12:36:39 provos Exp $ */
/*
* Copyright (c) 1998, 1999 Niklas Hallqvist. All rights reserved.
* Copyright (c) 2001, 2004 Håkan Olsson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This code was written under funding by Ericsson Radio Systems.
*/
#include <sys/queue.h>
#include <netdb.h>
#include <string.h>
#include "conf.h"
#include "exchange.h"
#include "log.h"
#include "message.h"
#include "sa.h"
#include "timer.h"
#include "transport.h"
#include "virtual.h"
/* If no retransmit limit is given, use this as a default. */
#define RETRANSMIT_DEFAULT 10
LIST_HEAD(transport_method_list, transport_vtbl) transport_method_list;
struct transport_list transport_list;
/* Call the reinit function of the various transports. */
void
transport_reinit(void)
{
struct transport_vtbl *method;
for (method = LIST_FIRST(&transport_method_list); method;
method = LIST_NEXT(method, link))
if (method->reinit)
method->reinit();
}
/* Initialize the transport maintenance module. */
void
transport_init(void)
{
LIST_INIT(&transport_list);
LIST_INIT(&transport_method_list);
}
/* Register another transport T. */
void
transport_setup(struct transport *t, int toplevel)
{
if (toplevel) {
/* Only the toplevel (virtual) transport has sendqueues. */
LOG_DBG((LOG_TRANSPORT, 70,
"transport_setup: virtual transport %p", t));
TAILQ_INIT(&t->sendq);
TAILQ_INIT(&t->prio_sendq);
t->refcnt = 0;
} else {
/* udp and udp_encap trp goes into the transport list. */
LOG_DBG((LOG_TRANSPORT, 70,
"transport_setup: added %p to transport list", t));
LIST_INSERT_HEAD(&transport_list, t, link);
t->refcnt = 1;
}
t->flags = 0;
}
/* Add a referer to transport T. */
void
transport_reference(struct transport *t)
{
t->refcnt++;
LOG_DBG((LOG_TRANSPORT, 95,
"transport_reference: transport %p now has %d references", t,
t->refcnt));
}
/*
* Remove a referer from transport T, removing all of T when no referers left.
*/
void
transport_release(struct transport *t)
{
LOG_DBG((LOG_TRANSPORT, 95,
"transport_release: transport %p had %d references", t,
t->refcnt));
if (--t->refcnt)
return;
LOG_DBG((LOG_TRANSPORT, 70, "transport_release: freeing %p", t));
t->vtbl->remove(t);
}
void
transport_report(void)
{
struct virtual_transport *v;
struct transport *t;
struct message *msg;
for (t = LIST_FIRST(&transport_list); t; t = LIST_NEXT(t, link)) {
LOG_DBG((LOG_REPORT, 0,
"transport_report: transport %p flags %x refcnt %d", t,
t->flags, t->refcnt));
/* XXX Report sth on the virtual transport? */
t->vtbl->report(t);
/*
* This is the reason message_dump_raw lives outside
* message.c.
*/
v = (struct virtual_transport *)t->virtual;
if ((v->encap_is_active && v->encap == t) ||
(!v->encap_is_active && v->main == t)) {
for (msg = TAILQ_FIRST(&t->virtual->prio_sendq); msg;
msg = TAILQ_NEXT(msg, link))
message_dump_raw("udp_report(prio)", msg,
LOG_REPORT);
for (msg = TAILQ_FIRST(&t->virtual->sendq); msg;
msg = TAILQ_NEXT(msg, link))
message_dump_raw("udp_report", msg,
LOG_REPORT);
}
}
}
int
transport_prio_sendqs_empty(void)
{
struct transport *t;
for (t = LIST_FIRST(&transport_list); t; t = LIST_NEXT(t, link))
if (TAILQ_FIRST(&t->virtual->prio_sendq))
return 0;
return 1;
}
/* Register another transport method T. */
void
transport_method_add(struct transport_vtbl *t)
{
LIST_INSERT_HEAD(&transport_method_list, t, link);
}
/*
* Build up a file descriptor set FDS with all transport descriptors we want
* to read from. Return the number of file descriptors select(2) needs to
* check in order to cover the ones we setup in here.
*/
int
transport_fd_set(fd_set * fds)
{
struct transport *t;
int n;
int max = -1;
for (t = LIST_FIRST(&transport_list); t; t = LIST_NEXT(t, link))
if (t->virtual->flags & TRANSPORT_LISTEN) {
n = t->vtbl->fd_set(t, fds, 1);
if (n > max)
max = n;
LOG_DBG((LOG_TRANSPORT, 95, "transport_fd_set: "
"transport %p (virtual %p) fd %d", t,
t->virtual, n));
}
return max + 1;
}
/*
* Build up a file descriptor set FDS with all the descriptors belonging to
* transport where messages are queued for transmittal. Return the number
* of file descriptors select(2) needs to check in order to cover the ones
* we setup in here.
*/
int
transport_pending_wfd_set(fd_set * fds)
{
struct transport *t;
int n;
int max = -1;
for (t = LIST_FIRST(&transport_list); t; t = LIST_NEXT(t, link)) {
if (TAILQ_FIRST(&t->virtual->sendq) ||
TAILQ_FIRST(&t->virtual->prio_sendq)) {
n = t->vtbl->fd_set(t, fds, 1);
LOG_DBG((LOG_TRANSPORT, 95,
"transport_pending_wfd_set: "
"transport %p (virtual %p) fd %d pending", t,
t->virtual, n));
if (n > max)
max = n;
}
}
return max + 1;
}
/*
* For each transport with a file descriptor in FDS, try to get an
* incoming message and start processing it.
*/
void
transport_handle_messages(fd_set *fds)
{
struct transport *t;
for (t = LIST_FIRST(&transport_list); t; t = LIST_NEXT(t, link)) {
if ((t->flags & TRANSPORT_LISTEN) &&
(*t->vtbl->fd_isset)(t, fds)) {
(*t->virtual->vtbl->handle_message)(t);
(*t->vtbl->fd_set)(t, fds, 0);
}
}
}
/*
* Send the first queued message on the transports found whose file
* descriptor is in FDS and has messages queued. Remove the fd bit from
* FDS as soon as one message has been sent on it so other transports
* sharing the socket won't get service without an intervening select
* call. Perhaps a fairness strategy should be implemented between
* such transports. Now early transports in the list will potentially
* be favoured to later ones sharing the file descriptor.
*/
void
transport_send_messages(fd_set * fds)
{
struct transport *t, *next;
struct message *msg;
struct exchange *exchange;
struct sockaddr *dst;
struct timespec expiration;
int expiry, ok_to_drop_message;
char peer[NI_MAXHOST], peersv[NI_MAXSERV];
/*
* Reference all transports first so noone will disappear while in
* use.
*/
for (t = LIST_FIRST(&transport_list); t; t = LIST_NEXT(t, link))
transport_reference(t->virtual);
for (t = LIST_FIRST(&transport_list); t; t = LIST_NEXT(t, link)) {
if ((TAILQ_FIRST(&t->virtual->sendq) ||
TAILQ_FIRST(&t->virtual->prio_sendq)) &&
t->vtbl->fd_isset(t, fds)) {
/* Remove fd bit. */
t->vtbl->fd_set(t, fds, 0);
/* Prefer a message from the prioritized sendq. */
if (TAILQ_FIRST(&t->virtual->prio_sendq)) {
msg = TAILQ_FIRST(&t->virtual->prio_sendq);
TAILQ_REMOVE(&t->virtual->prio_sendq, msg,
link);
} else {
msg = TAILQ_FIRST(&t->virtual->sendq);
TAILQ_REMOVE(&t->virtual->sendq, msg, link);
}
msg->flags &= ~MSG_IN_TRANSIT;
exchange = msg->exchange;
exchange->in_transit = 0;
/*
* We disregard the potential error message here,
* hoping that the retransmit will go better.
* XXX Consider a retry/fatal error discriminator.
*/
t->virtual->vtbl->send_message(msg, 0);
msg->xmits++;
/*
* This piece of code has been proven to be quite
* delicate. Think twice for before altering.
* Here's an outline:
*
* If this message is not the one which finishes an
* exchange, check if we have reached the number of
* retransmit before queuing it up for another.
*
* If it is a finishing message we still may have to
* keep it around for an on-demand retransmit when
* seeing a duplicate of our peer's previous message.
*/
if ((msg->flags & MSG_LAST) == 0) {
if (msg->flags & MSG_DONTRETRANSMIT)
exchange->last_sent = 0;
else if (msg->xmits > conf_get_num("General",
"retransmits", RETRANSMIT_DEFAULT)) {
t->virtual->vtbl->get_dst(t->virtual, &dst);
if (getnameinfo(dst, SA_LEN(dst), peer,
sizeof peer, peersv, sizeof peersv,
NI_NUMERICHOST | NI_NUMERICSERV)) {
strlcpy(peer, "<unknown>", sizeof peer);
strlcpy(peersv, "<?>", sizeof peersv);
}
log_print("transport_send_messages: "
"giving up on exchange %s, no "
"response from peer %s:%s",
exchange->name ? exchange->name :
"<unnamed>", peer, peersv);
exchange->last_sent = 0;
#ifdef notyet
exchange_free(exchange);
exchange = 0;
#endif
} else {
clock_gettime(CLOCK_MONOTONIC,
&expiration);
/*
* XXX Calculate from round trip
* timings and a backoff func.
*/
expiry = msg->xmits * 2 + 5;
expiration.tv_sec += expiry;
LOG_DBG((LOG_TRANSPORT, 30,
"transport_send_messages: "
"message %p scheduled for "
"retransmission %d in %d secs",
msg, msg->xmits, expiry));
if (msg->retrans)
timer_remove_event(msg->retrans);
msg->retrans
= timer_add_event("message_send_expire",
(void (*) (void *)) message_send_expire,
msg, &expiration);
/*
* If we cannot retransmit, we
* cannot...
*/
exchange->last_sent =
msg->retrans ? msg : 0;
}
} else
exchange->last_sent =
exchange->last_received ? msg : 0;
/*
* If this message is not referred to for later
* retransmission it will be ok for us to drop it
* after the post-send function. But as the post-send
* function may remove the exchange, we need to
* remember this fact here.
*/
ok_to_drop_message = exchange->last_sent == 0;
/*
* If this is not a retransmit call post-send
* functions that allows parallel work to be done
* while the network and peer does their share of
* the job. Note that a post-send function may take
* away the exchange we belong to, but only if no
* retransmits are possible.
*/
if (msg->xmits == 1)
message_post_send(msg);
if (ok_to_drop_message)
message_free(msg);
}
}
for (t = LIST_FIRST(&transport_list); t; t = next) {
next = LIST_NEXT(t, link);
transport_release(t->virtual);
}
}
/*
* Textual search after the transport method denoted by NAME, then create
* a transport connected to the peer with address ADDR, given in a transport-
* specific string format.
*/
struct transport *
transport_create(char *name, char *addr)
{
struct transport_vtbl *method;
for (method = LIST_FIRST(&transport_method_list); method;
method = LIST_NEXT(method, link))
if (strcmp(method->name, name) == 0)
return (*method->create) (addr);
return 0;
}
|