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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
26 #define close closesocket
29 #if APR_HAVE_SOCKADDR_UN
30 #define GENERIC_INADDR_ANY_LEN sizeof(struct sockaddr_un)
32 #define GENERIC_INADDR_ANY_LEN 16
35 /* big enough for IPv4, IPv6 and optionaly sun_path */
36 static char generic_inaddr_any[GENERIC_INADDR_ANY_LEN] = {0};
38 static apr_status_t socket_cleanup(void *sock)
40 apr_socket_t *thesocket = sock;
41 int sd = thesocket->socketdes;
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);
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().
52 thesocket->socketdes = -1;
58 /* Restore, close() was not successful. */
59 thesocket->socketdes = sd;
65 static apr_status_t socket_child_cleanup(void *sock)
67 apr_socket_t *thesocket = sock;
68 if (close(thesocket->socketdes) == 0) {
69 thesocket->socketdes = -1;
77 static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol)
80 sock->protocol = protocol;
81 apr_sockaddr_vars_set(sock->local_addr, family, 0);
82 apr_sockaddr_vars_set(sock->remote_addr, family, 0);
84 #if defined(BEOS) && !defined(BEOS_BONE)
85 /* BeOS pre-BONE has TCP_NODELAY on by default and it can't be
88 sock->options |= APR_TCP_NODELAY;
92 static void alloc_socket(apr_socket_t **new, apr_pool_t *p)
94 *new = (apr_socket_t *)apr_pcalloc(p, sizeof(apr_socket_t));
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);
110 apr_status_t apr_socket_protocol_get(apr_socket_t *sock, int *protocol)
112 *protocol = sock->protocol;
116 apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type,
117 int protocol, apr_pool_t *cont)
119 int family = ofamily, flags = 0;
120 int oprotocol = protocol;
122 #ifdef HAVE_SOCK_CLOEXEC
123 flags |= SOCK_CLOEXEC;
126 if (family == APR_UNSPEC) {
133 #if APR_HAVE_SOCKADDR_UN
134 if (family == APR_UNIX) {
138 alloc_socket(new, cont);
141 (*new)->socketdes = socket(family, type|flags, protocol);
143 /* For some reason BeOS R5 has an unconventional protocol numbering,
144 * so we need to translate here. */
147 (*new)->socketdes = socket(family, type|flags, 0);
150 (*new)->socketdes = socket(family, type|flags, IPPROTO_TCP);
153 (*new)->socketdes = socket(family, type|flags, IPPROTO_UDP);
157 errno = EPROTONOSUPPORT;
158 (*new)->socketdes = -1;
164 if ((*new)->socketdes < 0 && ofamily == APR_UNSPEC) {
166 (*new)->socketdes = socket(family, type|flags, protocol);
170 if ((*new)->socketdes < 0) {
173 set_socket_vars(*new, family, type, oprotocol);
175 #ifndef HAVE_SOCK_CLOEXEC
180 if ((flags = fcntl((*new)->socketdes, F_GETFD)) == -1) {
182 close((*new)->socketdes);
183 (*new)->socketdes = -1;
188 if (fcntl((*new)->socketdes, F_SETFD, flags) == -1) {
190 close((*new)->socketdes);
191 (*new)->socketdes = -1;
197 (*new)->timeout = -1;
199 apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
200 socket_child_cleanup);
205 apr_status_t apr_socket_shutdown(apr_socket_t *thesocket,
206 apr_shutdown_how_e how)
208 return (shutdown(thesocket->socketdes, how) == -1) ? errno : APR_SUCCESS;
211 apr_status_t apr_socket_close(apr_socket_t *thesocket)
213 return apr_pool_cleanup_run(thesocket->pool, thesocket, socket_cleanup);
216 apr_status_t apr_socket_bind(apr_socket_t *sock, apr_sockaddr_t *sa)
218 if (bind(sock->socketdes,
219 (struct sockaddr *)&sa->sa, sa->salen) == -1) {
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) {
228 sock->local_port_unknown = 1;
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 */
239 apr_status_t apr_socket_listen(apr_socket_t *sock, apr_int32_t backlog)
241 if (listen(sock->socketdes, backlog) == -1)
247 apr_status_t apr_socket_accept(apr_socket_t **new, apr_socket_t *sock,
248 apr_pool_t *connection_context)
253 sa.salen = sizeof(sa.sa);
257 int flags = SOCK_CLOEXEC;
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.
264 if (apr_is_option_set(sock, APR_SO_NONBLOCK) == 1) {
265 flags |= SOCK_NONBLOCK;
268 s = accept4(sock->socketdes, (struct sockaddr *)&sa.sa, &sa.salen, flags);
271 s = accept(sock->socketdes, (struct sockaddr *)&sa.sa, &sa.salen);
279 /* 0 is an invalid socket for TPF */
283 alloc_socket(new, connection_context);
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
290 set_socket_vars(*new, sa.sa.sin.sin_family, SOCK_STREAM, sock->protocol);
293 (*new)->connected = 1;
295 (*new)->timeout = -1;
297 (*new)->remote_addr_unknown = 0;
299 (*new)->socketdes = s;
301 /* Copy in peer's address. */
302 (*new)->remote_addr->sa = sa.sa;
303 (*new)->remote_addr->salen = sa.salen;
305 *(*new)->local_addr = *sock->local_addr;
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;
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;
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;
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);
328 if (sock->local_addr->sa.sin.sin_family != AF_UNIX)
330 (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port);
331 if (sock->local_port_unknown) {
332 /* not likely for a listening socket, but theoretically possible :) */
333 (*new)->local_port_unknown = 1;
336 #if APR_TCP_NODELAY_INHERITED
337 if (apr_is_option_set(sock, APR_TCP_NODELAY) == 1) {
338 apr_set_option(*new, APR_TCP_NODELAY, 1);
340 #endif /* TCP_NODELAY_INHERITED */
341 #if APR_O_NONBLOCK_INHERITED
342 if (apr_is_option_set(sock, APR_SO_NONBLOCK) == 1) {
343 apr_set_option(*new, APR_SO_NONBLOCK, 1);
345 #endif /* APR_O_NONBLOCK_INHERITED */
347 if (sock->local_interface_unknown ||
348 !memcmp(sock->local_addr->ipaddr_ptr,
350 sock->local_addr->ipaddr_len)) {
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.
354 * If the listening socket was not bound to a specific interface, we
355 * don't know the local_addr of the connected socket.
357 (*new)->local_interface_unknown = 1;
365 if ((flags = fcntl((*new)->socketdes, F_GETFD)) == -1) {
367 close((*new)->socketdes);
368 (*new)->socketdes = -1;
373 if (fcntl((*new)->socketdes, F_SETFD, flags) == -1) {
375 close((*new)->socketdes);
376 (*new)->socketdes = -1;
383 apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
388 apr_status_t apr_socket_connect(apr_socket_t *sock, apr_sockaddr_t *sa)
393 rc = connect(sock->socketdes,
394 (const struct sockaddr *)&sa->sa.sin,
396 } while (rc == -1 && errno == EINTR);
398 /* we can see EINPROGRESS the first time connect is called on a non-blocking
399 * socket; if called again, we can see EALREADY
401 if ((rc == -1) && (errno == EINPROGRESS || errno == EALREADY)
402 && (sock->timeout > 0)) {
403 rc = apr_wait_for_io_or_timeout(NULL, sock, 0);
404 if (rc != APR_SUCCESS) {
411 apr_socklen_t len = sizeof(error);
412 if ((rc = getsockopt(sock->socketdes, SOL_SOCKET, SO_ERROR,
413 (char *)&error, &len)) < 0) {
420 #endif /* SO_ERROR */
423 if (memcmp(sa->ipaddr_ptr, generic_inaddr_any, sa->ipaddr_len)) {
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. */
427 sock->remote_addr_unknown = 0;
429 /* Copy the address structure details in. */
430 sock->remote_addr->sa = sa->sa;
431 sock->remote_addr->salen = sa->salen;
432 /* Adjust ipaddr_ptr et al. */
433 apr_sockaddr_vars_set(sock->remote_addr, sa->family, sa->port);
436 if (sock->local_addr->port == 0) {
437 /* connect() got us an ephemeral port */
438 sock->local_port_unknown = 1;
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;
447 if (!memcmp(sock->local_addr->ipaddr_ptr,
449 sock->local_addr->ipaddr_len)) {
450 /* not bound to specific local interface; connect() had to assign
453 sock->local_interface_unknown = 1;
456 if (rc == -1 && errno != EISCONN) {
466 apr_status_t apr_socket_type_get(apr_socket_t *sock, int *type)
472 apr_status_t apr_socket_data_get(void **data, const char *key, apr_socket_t *sock)
474 sock_userdata_t *cur = sock->userdata;
479 if (!strcmp(cur->key, key)) {
489 apr_status_t apr_socket_data_set(apr_socket_t *sock, void *data, const char *key,
490 apr_status_t (*cleanup) (void *))
492 sock_userdata_t *new = apr_palloc(sock->pool, sizeof(sock_userdata_t));
494 new->key = apr_pstrdup(sock->pool, key);
496 new->next = sock->userdata;
497 sock->userdata = new;
500 apr_pool_cleanup_register(sock->pool, data, cleanup, cleanup);
506 apr_status_t apr_os_sock_get(apr_os_sock_t *thesock, apr_socket_t *sock)
508 *thesock = sock->socketdes;
512 apr_status_t apr_os_sock_make(apr_socket_t **apr_sock,
513 apr_os_sock_info_t *os_sock_info,
516 alloc_socket(apr_sock, cont);
517 set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, os_sock_info->protocol);
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);
528 (*apr_sock)->local_port_unknown = (*apr_sock)->local_interface_unknown = 1;
530 if (os_sock_info->remote) {
532 (*apr_sock)->connected = 1;
534 memcpy(&(*apr_sock)->remote_addr->sa.sin,
535 os_sock_info->remote,
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);
541 (*apr_sock)->remote_addr_unknown = 1;
544 (*apr_sock)->inherit = 0;
545 apr_pool_cleanup_register((*apr_sock)->pool, (void *)(*apr_sock),
546 socket_cleanup, socket_cleanup);
550 apr_status_t apr_os_sock_put(apr_socket_t **sock, apr_os_sock_t *thesock,
553 /* XXX Bogus assumption that *sock points at anything legit */
554 if ((*sock) == NULL) {
555 alloc_socket(sock, cont);
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 */
559 set_socket_vars(*sock, APR_INET, SOCK_STREAM, 0);
560 (*sock)->timeout = -1;
562 (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
563 (*sock)->remote_addr_unknown = 1;
564 (*sock)->socketdes = *thesock;
568 APR_POOL_IMPLEMENT_ACCESSOR(socket)
570 APR_IMPLEMENT_INHERIT_SET(socket, inherit, pool, socket_cleanup)
572 APR_IMPLEMENT_INHERIT_UNSET(socket, inherit, pool, socket_cleanup)