Apache HTTPD
sockets.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "apr_arch_networkio.h"
18#include "apr_network_io.h"
19#include "apr_strings.h"
20#include "apr_support.h"
21#include "apr_portable.h"
22#include "apr_arch_inherit.h"
23
24#ifdef BEOS_R5
25#undef close
26#define close closesocket
27#endif /* BEOS_R5 */
28
29#if APR_HAVE_SOCKADDR_UN
30#define GENERIC_INADDR_ANY_LEN sizeof(struct sockaddr_un)
31#else
32#define GENERIC_INADDR_ANY_LEN 16
33#endif
34
35/* big enough for IPv4, IPv6 and optionaly sun_path */
37
39{
41 int sd = thesocket->socketdes;
42
43#if APR_HAVE_SOCKADDR_UN
44 if (thesocket->bound && thesocket->local_addr->family == APR_UNIX) {
45 /* XXX: Check for return values ? */
46 unlink(thesocket->local_addr->hostname);
47 }
48#endif
49 /* Set socket descriptor to -1 before close(), so that there is no
50 * chance of returning an already closed FD from apr_os_sock_get().
51 */
52 thesocket->socketdes = -1;
53
54 if (close(sd) == 0) {
55 return APR_SUCCESS;
56 }
57 else {
58 /* Restore, close() was not successful. */
59 thesocket->socketdes = sd;
60
61 return errno;
62 }
63}
64
66{
68 if (close(thesocket->socketdes) == 0) {
69 thesocket->socketdes = -1;
70 return APR_SUCCESS;
71 }
72 else {
73 return errno;
74 }
75}
76
78{
79 sock->type = type;
83 sock->options = 0;
84#if defined(BEOS) && !defined(BEOS_BONE)
85 /* BeOS pre-BONE has TCP_NODELAY on by default and it can't be
86 * switched off!
87 */
89#endif
90}
91
93{
94 *new = (apr_socket_t *)apr_pcalloc(p, sizeof(apr_socket_t));
95 (*new)->pool = p;
96 (*new)->local_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
97 sizeof(apr_sockaddr_t));
98 (*new)->local_addr->pool = p;
99 (*new)->remote_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
100 sizeof(apr_sockaddr_t));
101 (*new)->remote_addr->pool = p;
102 (*new)->remote_addr_unknown = 1;
103#ifndef WAITIO_USES_POLL
104 /* Create a pollset with room for one descriptor. */
105 /* ### check return codes */
106 (void) apr_pollset_create(&(*new)->pollset, 1, p, 0);
107#endif
108}
109
115
118{
119 int family = ofamily, flags = 0;
120 int oprotocol = protocol;
121
122#ifdef HAVE_SOCK_CLOEXEC
124#endif
125
126 if (family == APR_UNSPEC) {
127#if APR_HAVE_IPV6
129#else
131#endif
132 }
133#if APR_HAVE_SOCKADDR_UN
134 if (family == APR_UNIX) {
135 protocol = 0;
136 }
137#endif
138 alloc_socket(new, cont);
139
140#ifndef BEOS_R5
141 (*new)->socketdes = socket(family, type|flags, protocol);
142#else
143 /* For some reason BeOS R5 has an unconventional protocol numbering,
144 * so we need to translate here. */
145 switch (protocol) {
146 case 0:
147 (*new)->socketdes = socket(family, type|flags, 0);
148 break;
149 case APR_PROTO_TCP:
150 (*new)->socketdes = socket(family, type|flags, IPPROTO_TCP);
151 break;
152 case APR_PROTO_UDP:
153 (*new)->socketdes = socket(family, type|flags, IPPROTO_UDP);
154 break;
155 case APR_PROTO_SCTP:
156 default:
158 (*new)->socketdes = -1;
159 break;
160 }
161#endif /* BEOS_R5 */
162
163#if APR_HAVE_IPV6
164 if ((*new)->socketdes < 0 && ofamily == APR_UNSPEC) {
166 (*new)->socketdes = socket(family, type|flags, protocol);
167 }
168#endif
169
170 if ((*new)->socketdes < 0) {
171 return errno;
172 }
174
175#ifndef HAVE_SOCK_CLOEXEC
176 {
177 int flags;
178 apr_status_t rv;
179
180 if ((flags = fcntl((*new)->socketdes, F_GETFD)) == -1) {
181 rv = errno;
182 close((*new)->socketdes);
183 (*new)->socketdes = -1;
184 return rv;
185 }
186
187 flags |= FD_CLOEXEC;
188 if (fcntl((*new)->socketdes, F_SETFD, flags) == -1) {
189 rv = errno;
190 close((*new)->socketdes);
191 (*new)->socketdes = -1;
192 return rv;
193 }
194 }
195#endif
196
197 (*new)->timeout = -1;
198 (*new)->inherit = 0;
199 apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
201
202 return APR_SUCCESS;
203}
204
210
215
217{
218 if (bind(sock->socketdes,
219 (struct sockaddr *)&sa->sa, sa->salen) == -1) {
220 return errno;
221 }
222 else {
223 sock->local_addr = sa;
224 /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
225#if APR_HAVE_SOCKADDR_UN
226 if (sock->local_addr->family == APR_UNIX) {
227 sock->bound = 1;
229 }
230 else
231#endif
232 if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing w/ 0 */
233 sock->local_port_unknown = 1; /* kernel got us an ephemeral port */
234 }
235 return APR_SUCCESS;
236 }
237}
238
240{
241 if (listen(sock->socketdes, backlog) == -1)
242 return errno;
243 else
244 return APR_SUCCESS;
245}
246
249{
250 int s;
252
253 sa.salen = sizeof(sa.sa);
254
255#ifdef HAVE_ACCEPT4
256 {
257 int flags = SOCK_CLOEXEC;
258
259#if defined(SOCK_NONBLOCK) && APR_O_NONBLOCK_INHERITED
260 /* With FreeBSD accept4() (avail in 10+), O_NONBLOCK is not inherited
261 * (unlike Linux). Mimic the accept() behavior here in a way that
262 * may help other platforms.
263 */
266 }
267#endif
268 s = accept4(sock->socketdes, (struct sockaddr *)&sa.sa, &sa.salen, flags);
269 }
270#else
271 s = accept(sock->socketdes, (struct sockaddr *)&sa.sa, &sa.salen);
272#endif
273
274 if (s < 0) {
275 return errno;
276 }
277#ifdef TPF
278 if (s == 0) {
279 /* 0 is an invalid socket for TPF */
280 return APR_EINTR;
281 }
282#endif
284
285 /* Set up socket variables -- note that it may be possible for
286 * *new to be an AF_INET socket when sock is AF_INET6 in some
287 * dual-stack configurations, so ensure that the remote_/local_addr
288 * structures are adjusted for the family of the accepted
289 * socket: */
290 set_socket_vars(*new, sa.sa.sin.sin_family, SOCK_STREAM, sock->protocol);
291
292#ifndef HAVE_POLL
293 (*new)->connected = 1;
294#endif
295 (*new)->timeout = -1;
296
297 (*new)->remote_addr_unknown = 0;
298
299 (*new)->socketdes = s;
300
301 /* Copy in peer's address. */
302 (*new)->remote_addr->sa = sa.sa;
303 (*new)->remote_addr->salen = sa.salen;
304
305 *(*new)->local_addr = *sock->local_addr;
306
307 /* The above assignment just overwrote the pool entry. Setting the local_addr
308 pool for the accepted socket back to what it should be. Otherwise all
309 allocations for this socket will come from a server pool that is not
310 freed until the process goes down.*/
311 (*new)->local_addr->pool = connection_context;
312
313 /* fix up any pointers which are no longer valid */
314 if (sock->local_addr->sa.sin.sin_family == AF_INET) {
315 (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin.sin_addr;
316 }
317#if APR_HAVE_IPV6
318 else if (sock->local_addr->sa.sin.sin_family == AF_INET6) {
319 (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin6.sin6_addr;
320 }
321#endif
322#if APR_HAVE_SOCKADDR_UN
323 else if (sock->local_addr->sa.sin.sin_family == AF_UNIX) {
324 *(*new)->remote_addr = *sock->local_addr;
325 (*new)->local_addr->ipaddr_ptr = &((*new)->local_addr->sa.unx.sun_path);
326 (*new)->remote_addr->ipaddr_ptr = &((*new)->remote_addr->sa.unx.sun_path);
327 }
328 if (sock->local_addr->sa.sin.sin_family != AF_UNIX)
329#endif
330 (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port);
332 /* not likely for a listening socket, but theoretically possible :) */
333 (*new)->local_port_unknown = 1;
334 }
335
336#if APR_TCP_NODELAY_INHERITED
339 }
340#endif /* TCP_NODELAY_INHERITED */
341#if APR_O_NONBLOCK_INHERITED
344 }
345#endif /* APR_O_NONBLOCK_INHERITED */
346
351 /* If the interface address inside the listening socket's local_addr wasn't
352 * up-to-date, we don't know local interface of the connected socket either.
353 *
354 * If the listening socket was not bound to a specific interface, we
355 * don't know the local_addr of the connected socket.
356 */
357 (*new)->local_interface_unknown = 1;
358 }
359
360#ifndef HAVE_ACCEPT4
361 {
362 int flags;
363 apr_status_t rv;
364
365 if ((flags = fcntl((*new)->socketdes, F_GETFD)) == -1) {
366 rv = errno;
367 close((*new)->socketdes);
368 (*new)->socketdes = -1;
369 return rv;
370 }
371
372 flags |= FD_CLOEXEC;
373 if (fcntl((*new)->socketdes, F_SETFD, flags) == -1) {
374 rv = errno;
375 close((*new)->socketdes);
376 (*new)->socketdes = -1;
377 return rv;
378 }
379 }
380#endif
381
382 (*new)->inherit = 0;
383 apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
385 return APR_SUCCESS;
386}
387
389{
390 int rc;
391
392 do {
394 (const struct sockaddr *)&sa->sa.sin,
395 sa->salen);
396 } while (rc == -1 && errno == EINTR);
397
398 /* we can see EINPROGRESS the first time connect is called on a non-blocking
399 * socket; if called again, we can see EALREADY
400 */
401 if ((rc == -1) && (errno == EINPROGRESS || errno == EALREADY)
402 && (sock->timeout > 0)) {
404 if (rc != APR_SUCCESS) {
405 return rc;
406 }
407
408#ifdef SO_ERROR
409 {
410 int error;
411 apr_socklen_t len = sizeof(error);
413 (char *)&error, &len)) < 0) {
414 return errno;
415 }
416 if (error) {
417 return error;
418 }
419 }
420#endif /* SO_ERROR */
421 }
422
424 /* A real remote address was passed in. If the unspecified
425 * address was used, the actual remote addr will have to be
426 * determined using getpeername() if required. */
428
429 /* Copy the address structure details in. */
430 sock->remote_addr->sa = sa->sa;
432 /* Adjust ipaddr_ptr et al. */
434 }
435
436 if (sock->local_addr->port == 0) {
437 /* connect() got us an ephemeral port */
439 }
440#if APR_HAVE_SOCKADDR_UN
441 if (sock->local_addr->sa.sin.sin_family == AF_UNIX) {
442 /* Assign connect address as local. */
443 sock->local_addr = sa;
444 }
445 else
446#endif
450 /* not bound to specific local interface; connect() had to assign
451 * one for the socket
452 */
454 }
455
456 if (rc == -1 && errno != EISCONN) {
457 return errno;
458 }
459
460#ifndef HAVE_POLL
461 sock->connected=1;
462#endif
463 return APR_SUCCESS;
464}
465
471
473{
475
476 *data = NULL;
477
478 while (cur) {
479 if (!strcmp(cur->key, key)) {
480 *data = cur->data;
481 break;
482 }
483 cur = cur->next;
484 }
485
486 return APR_SUCCESS;
487}
488
490 apr_status_t (*cleanup) (void *))
491{
493
494 new->key = apr_pstrdup(sock->pool, key);
495 new->data = data;
496 new->next = sock->userdata;
497 sock->userdata = new;
498
499 if (cleanup) {
501 }
502
503 return APR_SUCCESS;
504}
505
511
515{
518 (*apr_sock)->timeout = -1;
519 (*apr_sock)->socketdes = *os_sock_info->os_sock;
520 if (os_sock_info->local) {
521 memcpy(&(*apr_sock)->local_addr->sa.sin,
523 (*apr_sock)->local_addr->salen);
524 /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
525 (*apr_sock)->local_addr->port = ntohs((*apr_sock)->local_addr->sa.sin.sin_port);
526 }
527 else {
528 (*apr_sock)->local_port_unknown = (*apr_sock)->local_interface_unknown = 1;
529 }
530 if (os_sock_info->remote) {
531#ifndef HAVE_POLL
532 (*apr_sock)->connected = 1;
533#endif
534 memcpy(&(*apr_sock)->remote_addr->sa.sin,
536 (*apr_sock)->remote_addr->salen);
537 /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
538 (*apr_sock)->remote_addr->port = ntohs((*apr_sock)->remote_addr->sa.sin.sin_port);
539 }
540 else {
541 (*apr_sock)->remote_addr_unknown = 1;
542 }
543
544 (*apr_sock)->inherit = 0;
545 apr_pool_cleanup_register((*apr_sock)->pool, (void *)(*apr_sock),
547 return APR_SUCCESS;
548}
549
552{
553 /* XXX Bogus assumption that *sock points at anything legit */
554 if ((*sock) == NULL) {
556 /* XXX IPv6 figure out the family here! */
557 /* XXX figure out the actual socket type here */
558 /* *or* just decide that apr_os_sock_put() has to be told the family and type */
560 (*sock)->timeout = -1;
561 }
562 (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
563 (*sock)->remote_addr_unknown = 1;
564 (*sock)->socketdes = *thesock;
565 return APR_SUCCESS;
566}
567
569
571
const char apr_size_t len
Definition ap_regex.h:187
#define listen
#define accept
#define getsockopt
#define socket
#define shutdown
#define connect
#define bind
APR Network library.
APR Portability Routines.
APR Strings library.
APR Support functions.
#define APR_EINTR
Definition apr_errno.h:737
apr_pool_t const char apr_dbd_t const char ** error
Definition apr_dbd.h:143
const char apr_ssize_t int flags
Definition apr_encode.h:168
apr_redis_t * rc
Definition apr_redis.h:173
#define APR_PROTO_UDP
#define APR_PROTO_SCTP
#define APR_PROTO_TCP
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char * key
void * data
void const char apr_status_t(* cleanup)(void *))
int type
apr_pool_t * cont
Definition apr_getopt.h:103
apr_sockaddr_t * sockaddr
int family
apr_socket_t * sock
apr_sockaddr_t * sa
apr_int32_t backlog
apr_shutdown_how_e how
#define APR_UNIX
#define APR_UNSPEC
#define APR_INET
int int int protocol
apr_shutdown_how_e
#define APR_POOL_IMPLEMENT_ACCESSOR(type)
Definition apr_pools.h:91
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
int apr_os_sock_t
apr_os_sock_t * thesock
apr_os_sock_info_t * os_sock_info
#define APR_TCP_NODELAY
#define APR_SO_NONBLOCK
const char * s
Definition apr_strings.h:95
apr_status_t apr_wait_for_io_or_timeout(apr_file_t *f, apr_socket_t *s, int for_read)
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
#define APR_IMPLEMENT_INHERIT_UNSET(name, flag, pool, cleanup)
#define APR_IMPLEMENT_INHERIT_SET(name, flag, pool, cleanup)
void apr_sockaddr_vars_set(apr_sockaddr_t *, int, apr_port_t)
Definition sockaddr.c:180
static void alloc_socket(apr_socket_t **new, apr_pool_t *p)
Definition sockets.c:58
static apr_status_t socket_cleanup(void *sock)
Definition sockets.c:33
static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol)
Definition sockets.c:50
apr_os_sock_t * os_sock
struct sockaddr * remote
struct sockaddr * local
union apr_sockaddr_t::@55 sa
apr_pool_t * pool
struct sockaddr_in sin
apr_int32_t family
apr_socklen_t salen
apr_pool_t * pool
sock_userdata_t * userdata
apr_interval_time_t timeout
apr_sockaddr_t * remote_addr
apr_int32_t options
apr_sockaddr_t * local_addr
#define apr_is_option_set(skt, option)
#define apr_set_option(skt, option, on)
apr_status_t apr_os_sock_get(apr_os_sock_t *thesock, apr_socket_t *sock)
Definition sockets.c:506
apr_status_t apr_socket_accept(apr_socket_t **new, apr_socket_t *sock, apr_pool_t *connection_context)
Definition sockets.c:247
apr_status_t apr_socket_listen(apr_socket_t *sock, apr_int32_t backlog)
Definition sockets.c:239
apr_status_t apr_socket_bind(apr_socket_t *sock, apr_sockaddr_t *sa)
Definition sockets.c:216
apr_status_t apr_os_sock_make(apr_socket_t **apr_sock, apr_os_sock_info_t *os_sock_info, apr_pool_t *cont)
Definition sockets.c:512
apr_status_t apr_os_sock_put(apr_socket_t **sock, apr_os_sock_t *thesock, apr_pool_t *cont)
Definition sockets.c:550
#define GENERIC_INADDR_ANY_LEN
Definition sockets.c:32
apr_status_t apr_socket_close(apr_socket_t *thesocket)
Definition sockets.c:211
apr_status_t apr_socket_type_get(apr_socket_t *sock, int *type)
Definition sockets.c:466
apr_status_t apr_socket_connect(apr_socket_t *sock, apr_sockaddr_t *sa)
Definition sockets.c:388
static apr_status_t socket_child_cleanup(void *sock)
Definition sockets.c:65
apr_status_t apr_socket_protocol_get(apr_socket_t *sock, int *protocol)
Definition sockets.c:110
apr_status_t apr_socket_shutdown(apr_socket_t *thesocket, apr_shutdown_how_e how)
Definition sockets.c:205
static char generic_inaddr_any[16]
Definition sockets.c:36
apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type, int protocol, apr_pool_t *cont)
Definition sockets.c:116
apr_status_t apr_socket_data_get(void **data, const char *key, apr_socket_t *sock)
Definition sockets.c:472
apr_status_t apr_socket_data_set(apr_socket_t *sock, void *data, const char *key, apr_status_t(*cleanup)(void *))
Definition sockets.c:489