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
|
/* $OpenBSD: transport.c,v 1.2 1998/11/15 00:44:03 niklas Exp $ */
/*
* Copyright (c) 1998 Niklas Hallqvist. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Ericsson Radio Systems.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* 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/param.h>
#include <sys/queue.h>
#include "conf.h"
#include "exchange.h"
#include "log.h"
#include "message.h"
#include "sa.h"
#include "timer.h"
#include "transport.h"
LIST_HEAD (transport_list, transport) transport_list;
LIST_HEAD (transport_method_list, transport_vtbl) transport_method_list;
/* Initialize the transport maintenance module. */
void
transport_init (void)
{
LIST_INIT (&transport_list);
LIST_INIT (&transport_method_list);
}
/* Register another transport T. */
void
transport_add (struct transport *t)
{
TAILQ_INIT (&t->sendq);
LIST_INSERT_HEAD (&transport_list, t, link);
}
/* Register another transport method T. */
void
transport_method_add (struct transport_vtbl *t)
{
LIST_INSERT_HEAD (&transport_method_list, t, link);
}
/* Apply a function FUNC on all registered transports. */
void
transport_map (void (*func) (struct transport *))
{
struct transport *t;
for (t = LIST_FIRST (&transport_list); t; t = LIST_NEXT (t, link))
(*func) (t);
}
/*
* Build up a file desciptor 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)
{
int n;
int max = -1;
struct transport *t;
for (t = LIST_FIRST (&transport_list); t; t = LIST_NEXT (t, link))
if (t->flags & TRANSPORT_LISTEN)
{
n = (*t->vtbl->fd_set) (t, fds);
if (n > max)
max = n;
}
return max + 1;
}
/*
* Build up a file desciptor 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)
{
int n;
int max = -1;
struct transport *t;
for (t = LIST_FIRST (&transport_list); t; t = LIST_NEXT (t, link))
{
if (TAILQ_FIRST (&t->sendq))
{
n = (*t->vtbl->fd_set) (t, fds);
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->vtbl->handle_message) (t);
}
/*
* Send the first queued message on the transports whose file descriptor
* is in FDS.
*/
void
transport_send_messages (fd_set *fds)
{
struct transport *t;
struct message *msg;
struct timeval expiration;
struct sa *sa, *next_sa;
int expiry;
for (t = LIST_FIRST (&transport_list); t; t = LIST_NEXT (t, link))
if (TAILQ_FIRST (&t->sendq) && (*t->vtbl->fd_isset) (t, fds))
{
msg = TAILQ_FIRST (&t->sendq);
TAILQ_REMOVE (&t->sendq, msg, link);
/*
* We disregard the potential error message here, hoping that the
* retransmit will go better.
* XXX Consider a retry/fatal error discriminator.
*/
t->vtbl->send_message (msg);
/*
* 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.
*/
if (msg->xmits == 0)
message_post_send (msg);
msg->xmits++;
if ((msg->flags & MSG_NO_RETRANS) == 0)
{
/* XXX make this a configurable parameter. */
if (msg->xmits > conf_get_num ("General", "retransmits"))
{
log_print ("transport_send_messages: giving up on message %p",
msg);
msg->exchange->last_sent = 0;
/*
* As this exchange never went to a normal end, remove the
* SA's being negotiated too.
*/
for (sa = TAILQ_FIRST (&msg->exchange->sa_list); sa;
sa = next_sa)
{
next_sa = TAILQ_NEXT (sa, next);
sa_free (sa);
}
exchange_free (msg->exchange);
message_free (msg);
return;
};
gettimeofday (&expiration, 0);
/* XXX Calculate from round trip timings and a backoff func. */
expiry = msg->xmits * 2 + 5;
expiration.tv_sec += expiry;
log_debug (LOG_TRANSPORT, 30,
"transport_send_messages: "
"message %p scheduled for retransmission %d in %d secs",
msg, msg->xmits, expiry);
msg->retrans = timer_add_event ("message_send",
(void (*) (void *))message_send,
msg, &expiration);
if (!msg->retrans)
{
/* If we can make no retransmission, we can't.... */
message_free (msg);
return;
}
msg->exchange->last_sent = msg;
}
else if ((msg->flags & MSG_KEEP) == 0)
message_free (msg);
}
}
/*
* 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;
}
|