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
|
/****************************************************************************
* net/uip/uip-tcpbacklog.c
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "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
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <net/uip/uipopt.h>
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) && defined(CONFIG_NET_TCPBACKLOG)
#include <sys/types.h>
#include <stdlib.h>
#include <queue.h>
#include <debug.h>
#include <net/uip/uip.h>
#include <net/uip/uip-tcp.h>
#include "uip-internal.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Function: uip_backlogcreate
*
* Description:
* Called from the listen() logic to setup the backlog as specified in the
* the listen arguments *.
*
* Assumptions:
* Called from normal user code. Interrupts may be disabled.
*
****************************************************************************/
int uip_backlogcreate(FAR struct uip_conn *conn, int nblg)
{
FAR struct uip_backlog_s *bls = NULL;
FAR struct uip_blcontainer_s *blc;
irqstate_t flags;
int size;
int offset;
int i;
#ifdef CONFIG_DEBUG
if (!conn)
{
return -EINVAL;
}
#endif
/* Then allocate the backlog as requested */
if (nblg > 0)
{
/* Align the list of backlog structures to 32-bit boundaries. This
* may be excessive on 24-16-bit address machines; and insufficient
* on 64-bit address machines -- REVISIT
*/
offset = (sizeof(struct uip_backlog_s) + 3) & ~3;
/* Then determine the full size of the allocation include the
* uip_backlog_s, a pre-allocated array of struct uip_blcontainer_s
* and alignement padding
*/
size = offset + nblg * sizeof(struct uip_blcontainer_s);
/* Then allocate that much */
bls = (FAR struct uip_backlog_s *)zalloc(size);
if (!bls)
{
return -ENOMEM;
}
/* Then add all of the pre-allocated containers to the the free list */
blc = (FAR struct uip_blcontainer_s*)(((FAR ubyte*)bls) + offset);
for (i = 0; i < nblg; i++)
{
dq_addfirst(&blc->bc_node, &bls->bl_free);
}
}
/* Destroy any existing backlog (shouldn't be any) */
flags = irqsave();
uip_backlogdestroy(conn);
/* Now install the backlog tear-off in the connection. NOTE that bls may
* actually be NULL if nblg is <= 0; In that case, we are disabling backlog
* support. Since interrupts are disabled, destroying the old backlog and
* replace it with the new is an atomic operation
*/
conn->backlog = bls;
irqrestore(flags);
return OK;
}
/****************************************************************************
* Function: uip_backlogdestroy
*
* Description:
* (1) Called from uip_tcpfree() whenever a connection is freed.
* (2) Called from uip_backlogcreate() to destroy any old backlog
*
* NOTE: This function may re-enter uip_tcpfree when a connection that
* is freed that has pending connections.
*
* Assumptions:
* The caller has disabled interrupts so that there can be no conflict
* with ongoing, interrupt driven activity
*
****************************************************************************/
int uip_backlogdestroy(FAR struct uip_conn *conn)
{
FAR struct uip_backlog_s *blg;
FAR struct uip_blcontainer_s *blc;
FAR struct uip_conn *blconn;
#ifdef CONFIG_DEBUG
if (!conn)
{
return -EINVAL;
}
#endif
/* Make sure that the connection has a backlog to be destroyed */
if (conn->backlog)
{
/* Remove the backlog structure reference from the connection */
blg = conn->backlog;
conn->backlog = NULL;
/* Handle any pending connections in the backlog */
while ((blc = (FAR struct uip_blcontainer_s*)dq_remfirst(&blg->bl_pending)) != NULL)
{
blconn = blc->bc_conn;
if (blconn)
{
/* REVISIT -- such connections really need to be gracefully closed */
blconn->blparent = NULL;
blconn->backlog = NULL;
uip_tcpfree(blconn);
}
}
/* Then free the entire backlog structure */
free(blg);
}
return OK;
}
/****************************************************************************
* Function: uip_backlogadd
*
* Description:
* Called uip_listen when a new connection is made with a listener socket
* but when there is not accept() in place to receive the connection. This
* function adds the new connection to the backlog.
*
* Assumptions:
* Called from the interrupt level with interrupts disabled
*
****************************************************************************/
int uip_backlogadd(FAR struct uip_conn *conn, FAR struct uip_conn *blconn)
{
FAR struct uip_backlog_s *bls;
FAR struct uip_blcontainer_s *blc;
int ret = -EINVAL;
#ifdef CONFIG_DEBUG
if (!conn)
{
return -EINVAL;
}
#endif
bls = conn->backlog;
if (conn->backlog && blconn)
{
/* Allocate a container for the connection from the free list */
blc = (FAR struct uip_blcontainer_s *)dq_remfirst(&bls->bl_free);
if (!blc)
{
ret = -ENOMEM;
}
else
{
/* Save the connection reference in the container and put the
* container at the end of the pending connection list (FIFO).
*/
blc->bc_conn = blconn;
dq_addlast(&blc->bc_node, &bls->bl_pending);
ret = OK;
}
}
return ret;
}
/****************************************************************************
* Function: uip_backlogremove
*
* Description:
* Called from accept(). Before waiting for a new connection, accept will
* call this API to see if there are pending connections in the backlog.
*
* Assumptions:
* Called from normal user code, but with interrupts disabled,
*
****************************************************************************/
struct uip_conn *uip_backlogremove(FAR struct uip_conn *conn)
{
FAR struct uip_backlog_s *bls;
FAR struct uip_blcontainer_s *blc;
FAR struct uip_conn *blconn = NULL;
#ifdef CONFIG_DEBUG
if (!conn)
{
return NULL;
}
#endif
bls = conn->backlog;
if (bls && blconn)
{
/* Remove the a container at the head of the pending connection list
* (FIFO)
*/
blc = (FAR struct uip_blcontainer_s *)dq_remfirst(&bls->bl_pending);
if (blc)
{
/* Extract the connection reference from the container and put
* container in the free list
*/
blconn = blc->bc_conn;
blc->bc_conn = NULL;
dq_addlast(&blc->bc_node, &bls->bl_free);
}
}
return blconn;
}
/****************************************************************************
* Function: uip_backlogdelete
*
* Description:
* Called from uip_tcpfree() when a connection is freed that this also
* retained in the pending connectino list of a listener. We simply need
* to remove the defunct connecton from the list.
*
* Assumptions:
* Called from the interrupt level with interrupts disabled
*
****************************************************************************/
int uip_backlogdelete(FAR struct uip_conn *conn, FAR struct uip_conn *blconn)
{
FAR struct uip_backlog_s *bls;
FAR struct uip_blcontainer_s *blc;
#ifdef CONFIG_DEBUG
if (!conn)
{
return NULL;
}
#endif
bls = conn->backlog;
if (bls)
{
/* Find the container hold the connection */
for (blc = (FAR struct uip_blcontainer_s *)dq_peek(&bls->bl_pending);
blc;
blc = (FAR struct uip_blcontainer_s *)dq_next(&blc->bc_node))
{
if (blc->bc_conn == blconn)
{
/* Remove the a container from the list of pending connections */
dq_rem(&blc->bc_node, &bls->bl_pending);
return OK;
}
}
return -EINVAL;
}
return OK;
}
#endif /* CONFIG_NET && CONFIG_NET_TCP && CONFIG_NET_TCPBACKLOG */
|