forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccept.c
322 lines (283 loc) · 9.86 KB
/
accept.c
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
/****************************************************************************
* net/socket/accept.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <fcntl.h>
#include <nuttx/cancelpt.h>
#include <nuttx/fs/fs.h>
#include <nuttx/kmalloc.h>
#include <arch/irq.h>
#include "socket/socket.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: psock_accept
*
* Description:
* The psock_accept function is used with connection-based socket types
* (SOCK_STREAM, SOCK_SEQPACKET and SOCK_RDM). It extracts the first
* connection request on the queue of pending connections, creates a new
* connected socket with mostly the same properties as 'sockfd', and
* allocates a new socket descriptor for the socket, which is returned. The
* newly created socket is no longer in the listening state. The original
* socket 'sockfd' is unaffected by this call. Per file descriptor flags
* are not inherited across an psock_accept.
*
* The 'sockfd' argument is a socket descriptor that has been created with
* socket(), bound to a local address with bind(), and is listening for
* connections after a call to listen().
*
* On return, the 'addr' structure is filled in with the address of the
* connecting entity. The 'addrlen' argument initially contains the size
* of the structure pointed to by 'addr'; on return it will contain the
* actual length of the address returned.
*
* If no pending connections are present on the queue, and the socket is
* not marked as non-blocking, psock_accept blocks the caller until a
* connection is present. If the socket is marked non-blocking and no
* pending connections are present on the queue, psock_accept returns
* EAGAIN.
*
* Input Parameters:
* psock Reference to the listening socket structure
* addr Receives the address of the connecting client
* addrlen Input: allocated size of 'addr', Return: returned size
* of 'addr'
* newsock Location to return the accepted socket information.
* flags The flags used for initialization
*
* Returned Value:
* Returns 0 (OK) on success. On failure, it returns a negated errno value
* to indicate the nature of the error.
*
* EAGAIN or EWOULDBLOCK
* The socket is marked non-blocking and no connections are present to
* be accepted.
* EOPNOTSUPP
* The referenced socket is not of type SOCK_STREAM.
* EINTR
* The system call was interrupted by a signal that was caught before
* a valid connection arrived.
* ECONNABORTED
* A connection has been aborted.
* EINVAL
* Socket is not listening for connections.
* EMFILE
* The per-process limit of open file descriptors has been reached.
* ENFILE
* The system maximum for file descriptors has been reached.
* EFAULT
* The addr parameter is not in a writable part of the user address space.
* ENOBUFS or ENOMEM
* Not enough free memory.
* EPROTO
* Protocol error.
* EPERM
* Firewall rules forbid connection.
*
****************************************************************************/
int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
FAR socklen_t *addrlen, FAR struct socket *newsock,
int flags)
{
FAR struct socket_conn_s *conn;
int ret;
DEBUGASSERT(psock != NULL && psock->s_conn != NULL && newsock != NULL);
/* May sure that the socket has been opened with socket() */
if (psock == NULL || psock->s_conn == NULL)
{
nerr("ERROR: Socket invalid or not opened\n");
return -EINVAL;
}
/* Is the socket listening for a connection? */
conn = psock->s_conn;
if (!_SS_ISLISTENING(conn->s_flags))
{
nerr("ERROR: Socket is not listening for a connection.\n");
return -EINVAL;
}
/* Let the address family's accept() method handle the operation */
DEBUGASSERT(psock->s_sockif != NULL);
if (psock->s_sockif->si_accept == NULL)
{
return -EOPNOTSUPP;
}
net_lock();
ret = psock->s_sockif->si_accept(psock, addr, addrlen, newsock, flags);
if (ret >= 0)
{
/* Mark the new socket as connected. */
conn = newsock->s_conn;
conn->s_flags |= _SF_CONNECTED;
conn->s_flags &= ~_SF_CLOSED;
if (flags & SOCK_NONBLOCK)
{
conn->s_flags |= _SF_NONBLOCK;
}
}
else
{
nerr("ERROR: si_accept failed: %d\n", ret);
}
net_unlock();
return ret;
}
/****************************************************************************
* Name: accept4
*
* Description:
* The accept4 function is used with connection-based socket types
* (SOCK_STREAM, SOCK_SEQPACKET and SOCK_RDM). It extracts the first
* connection request on the queue of pending connections, creates a new
* connected socket with mostly the same properties as 'sockfd', and
* allocates a new socket descriptor for the socket, which is returned. The
* newly created socket is no longer in the listening state. The original
* socket 'sockfd' is unaffected by this call. Per file descriptor flags
* are not inherited across an accept.
*
* The 'sockfd' argument is a socket descriptor that has been created with
* socket(), bound to a local address with bind(), and is listening for
* connections after a call to listen().
*
* On return, the 'addr' structure is filled in with the address of the
* connecting entity. The 'addrlen' argument initially contains the size
* of the structure pointed to by 'addr'; on return it will contain the
* actual length of the address returned.
*
* If no pending connections are present on the queue, and the socket is
* not marked as non-blocking, accept blocks the caller until a connection
* is present. If the socket is marked non-blocking and no pending
* connections are present on the queue, accept returns EAGAIN.
*
* Input Parameters:
* sockfd The listening socket descriptor
* addr Receives the address of the connecting client
* addrlen Input: allocated size of 'addr',
* Return: returned size of 'addr'
* flags The flags used for initialization
*
* Returned Value:
* Returns -1 on error. If it succeeds, it returns a non-negative integer
* that is a descriptor for the accepted socket.
*
* EAGAIN or EWOULDBLOCK
* The socket is marked non-blocking and no connections are present to
* be accepted.
* EBADF
* The descriptor is invalid.
* ENOTSOCK
* The descriptor references a file, not a socket.
* EOPNOTSUPP
* The referenced socket is not of type SOCK_STREAM.
* EINTR
* The system call was interrupted by a signal that was caught before
* a valid connection arrived.
* ECONNABORTED
* A connection has been aborted.
* EINVAL
* Socket is not listening for connections.
* EMFILE
* The per-process limit of open file descriptors has been reached.
* ENFILE
* The system maximum for file descriptors has been reached.
* EFAULT
* The addr parameter is not in a writable part of the user address
* space.
* ENOBUFS or ENOMEM
* Not enough free memory.
* EPROTO
* Protocol error.
* EPERM
* Firewall rules forbid connection.
*
****************************************************************************/
int accept4(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen,
int flags)
{
FAR struct socket *psock = NULL;
FAR struct socket *newsock;
int oflags = O_RDWR;
int errcode;
int newfd;
int ret;
/* accept4() is a cancellation point */
enter_cancellation_point();
if (flags & ~(SOCK_NONBLOCK | SOCK_CLOEXEC))
{
errcode = EINVAL;
goto errout;
}
/* Get the underlying socket structure */
ret = sockfd_socket(sockfd, &psock);
/* Verify that the sockfd corresponds to valid, allocated socket */
if (ret < 0)
{
errcode = -ret;
goto errout;
}
newsock = kmm_zalloc(sizeof(*newsock));
if (newsock == NULL)
{
errcode = ENOMEM;
goto errout;
}
ret = psock_accept(psock, addr, addrlen, newsock, flags);
if (ret < 0)
{
errcode = -ret;
goto errout_with_alloc;
}
/* Allocate a socket descriptor for the new connection now (so that it
* cannot fail later)
*/
if (flags & SOCK_CLOEXEC)
{
oflags |= O_CLOEXEC;
}
if (flags & SOCK_NONBLOCK)
{
oflags |= O_NONBLOCK;
}
newfd = sockfd_allocate(newsock, oflags);
if (newfd < 0)
{
errcode = ENFILE;
goto errout_with_psock;
}
leave_cancellation_point();
return newfd;
errout_with_psock:
psock_close(newsock);
errout_with_alloc:
kmm_free(newsock);
errout:
leave_cancellation_point();
set_errno(errcode);
return ERROR;
}