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 static char generic_inaddr_any[16] = {0}; /* big enough for IPv4 or IPv6 */
31 static apr_status_t socket_cleanup(void *sock)
33 apr_socket_t *thesocket = sock;
34 int sd = thesocket->socketdes;
36 /* Set socket descriptor to -1 before close(), so that there is no
37 * chance of returning an already closed FD from apr_os_sock_get().
39 thesocket->socketdes = -1;
45 /* Restore, close() was not successful. */
46 thesocket->socketdes = sd;
52 static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol)
55 sock->protocol = protocol;
56 apr_sockaddr_vars_set(sock->local_addr, family, 0);
57 apr_sockaddr_vars_set(sock->remote_addr, family, 0);
59 #if defined(BEOS) && !defined(BEOS_BONE)
60 /* BeOS pre-BONE has TCP_NODELAY on by default and it can't be
63 sock->options |= APR_TCP_NODELAY;
67 static void alloc_socket(apr_socket_t **new, apr_pool_t *p)
69 *new = (apr_socket_t *)apr_pcalloc(p, sizeof(apr_socket_t));
71 (*new)->local_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
72 sizeof(apr_sockaddr_t));
73 (*new)->local_addr->pool = p;
74 (*new)->remote_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
75 sizeof(apr_sockaddr_t));
76 (*new)->remote_addr->pool = p;
77 (*new)->remote_addr_unknown = 1;
78 #ifndef WAITIO_USES_POLL
79 /* Create a pollset with room for one descriptor. */
80 /* ### check return codes */
81 (void) apr_pollset_create(&(*new)->pollset, 1, p, 0);
85 apr_status_t apr_socket_protocol_get(apr_socket_t *sock, int *protocol)
87 *protocol = sock->protocol;
91 apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type,
92 int protocol, apr_pool_t *cont)
94 int family = ofamily, flags = 0;
96 #ifdef HAVE_SOCK_CLOEXEC
97 flags |= SOCK_CLOEXEC;
100 if (family == APR_UNSPEC) {
108 alloc_socket(new, cont);
111 (*new)->socketdes = socket(family, type|flags, protocol);
113 /* For some reason BeOS R5 has an unconventional protocol numbering,
114 * so we need to translate here. */
117 (*new)->socketdes = socket(family, type|flags, 0);
120 (*new)->socketdes = socket(family, type|flags, IPPROTO_TCP);
123 (*new)->socketdes = socket(family, type|flags, IPPROTO_UDP);
127 errno = EPROTONOSUPPORT;
128 (*new)->socketdes = -1;
134 if ((*new)->socketdes < 0 && ofamily == APR_UNSPEC) {
136 (*new)->socketdes = socket(family, type|flags, protocol);
140 if ((*new)->socketdes < 0) {
143 set_socket_vars(*new, family, type, protocol);
145 #ifndef HAVE_SOCK_CLOEXEC
149 if ((flags = fcntl((*new)->socketdes, F_GETFD)) == -1)
153 if (fcntl((*new)->socketdes, F_SETFD, flags) == -1)
158 (*new)->timeout = -1;
160 apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
166 apr_status_t apr_socket_shutdown(apr_socket_t *thesocket,
167 apr_shutdown_how_e how)
169 return (shutdown(thesocket->socketdes, how) == -1) ? errno : APR_SUCCESS;
172 apr_status_t apr_socket_close(apr_socket_t *thesocket)
174 return apr_pool_cleanup_run(thesocket->pool, thesocket, socket_cleanup);
177 apr_status_t apr_socket_bind(apr_socket_t *sock, apr_sockaddr_t *sa)
179 if (bind(sock->socketdes,
180 (struct sockaddr *)&sa->sa, sa->salen) == -1) {
184 sock->local_addr = sa;
185 /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
186 if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing w/ 0 */
187 sock->local_port_unknown = 1; /* kernel got us an ephemeral port */
193 apr_status_t apr_socket_listen(apr_socket_t *sock, apr_int32_t backlog)
195 if (listen(sock->socketdes, backlog) == -1)
201 apr_status_t apr_socket_accept(apr_socket_t **new, apr_socket_t *sock,
202 apr_pool_t *connection_context)
207 sa.salen = sizeof(sa.sa);
210 s = accept4(sock->socketdes, (struct sockaddr *)&sa.sa, &sa.salen, SOCK_CLOEXEC);
212 s = accept(sock->socketdes, (struct sockaddr *)&sa.sa, &sa.salen);
220 /* 0 is an invalid socket for TPF */
224 alloc_socket(new, connection_context);
226 /* Set up socket variables -- note that it may be possible for
227 * *new to be an AF_INET socket when sock is AF_INET6 in some
228 * dual-stack configurations, so ensure that the remote_/local_addr
229 * structures are adjusted for the family of the accepted
231 set_socket_vars(*new, sa.sa.sin.sin_family, SOCK_STREAM, sock->protocol);
234 (*new)->connected = 1;
236 (*new)->timeout = -1;
238 (*new)->remote_addr_unknown = 0;
240 (*new)->socketdes = s;
242 /* Copy in peer's address. */
243 (*new)->remote_addr->sa = sa.sa;
244 (*new)->remote_addr->salen = sa.salen;
246 *(*new)->local_addr = *sock->local_addr;
248 /* The above assignment just overwrote the pool entry. Setting the local_addr
249 pool for the accepted socket back to what it should be. Otherwise all
250 allocations for this socket will come from a server pool that is not
251 freed until the process goes down.*/
252 (*new)->local_addr->pool = connection_context;
254 /* fix up any pointers which are no longer valid */
255 if (sock->local_addr->sa.sin.sin_family == AF_INET) {
256 (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin.sin_addr;
259 else if (sock->local_addr->sa.sin.sin_family == AF_INET6) {
260 (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin6.sin6_addr;
263 (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port);
264 if (sock->local_port_unknown) {
265 /* not likely for a listening socket, but theoretically possible :) */
266 (*new)->local_port_unknown = 1;
269 #if APR_TCP_NODELAY_INHERITED
270 if (apr_is_option_set(sock, APR_TCP_NODELAY) == 1) {
271 apr_set_option(*new, APR_TCP_NODELAY, 1);
273 #endif /* TCP_NODELAY_INHERITED */
274 #if APR_O_NONBLOCK_INHERITED
275 if (apr_is_option_set(sock, APR_SO_NONBLOCK) == 1) {
276 apr_set_option(*new, APR_SO_NONBLOCK, 1);
278 #endif /* APR_O_NONBLOCK_INHERITED */
280 if (sock->local_interface_unknown ||
281 !memcmp(sock->local_addr->ipaddr_ptr,
283 sock->local_addr->ipaddr_len)) {
284 /* If the interface address inside the listening socket's local_addr wasn't
285 * up-to-date, we don't know local interface of the connected socket either.
287 * If the listening socket was not bound to a specific interface, we
288 * don't know the local_addr of the connected socket.
290 (*new)->local_interface_unknown = 1;
297 if ((flags = fcntl((*new)->socketdes, F_GETFD)) == -1)
301 if (fcntl((*new)->socketdes, F_SETFD, flags) == -1)
307 apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
312 apr_status_t apr_socket_connect(apr_socket_t *sock, apr_sockaddr_t *sa)
317 rc = connect(sock->socketdes,
318 (const struct sockaddr *)&sa->sa.sin,
320 } while (rc == -1 && errno == EINTR);
322 /* we can see EINPROGRESS the first time connect is called on a non-blocking
323 * socket; if called again, we can see EALREADY
325 if ((rc == -1) && (errno == EINPROGRESS || errno == EALREADY)
326 && (sock->timeout > 0)) {
327 rc = apr_wait_for_io_or_timeout(NULL, sock, 0);
328 if (rc != APR_SUCCESS) {
335 apr_socklen_t len = sizeof(error);
336 if ((rc = getsockopt(sock->socketdes, SOL_SOCKET, SO_ERROR,
337 (char *)&error, &len)) < 0) {
344 #endif /* SO_ERROR */
348 if (memcmp(sa->ipaddr_ptr, generic_inaddr_any, sa->ipaddr_len)) {
349 /* A real remote address was passed in. If the unspecified
350 * address was used, the actual remote addr will have to be
351 * determined using getpeername() if required. */
352 sock->remote_addr_unknown = 0;
354 /* Copy the address structure details in. */
355 sock->remote_addr->sa = sa->sa;
356 sock->remote_addr->salen = sa->salen;
357 /* Adjust ipaddr_ptr et al. */
358 apr_sockaddr_vars_set(sock->remote_addr, sa->family, sa->port);
361 if (sock->local_addr->port == 0) {
362 /* connect() got us an ephemeral port */
363 sock->local_port_unknown = 1;
365 if (!memcmp(sock->local_addr->ipaddr_ptr,
367 sock->local_addr->ipaddr_len)) {
368 /* not bound to specific local interface; connect() had to assign
371 sock->local_interface_unknown = 1;
374 if (rc == -1 && errno != EISCONN) {
384 apr_status_t apr_socket_type_get(apr_socket_t *sock, int *type)
390 apr_status_t apr_socket_data_get(void **data, const char *key, apr_socket_t *sock)
392 sock_userdata_t *cur = sock->userdata;
397 if (!strcmp(cur->key, key)) {
407 apr_status_t apr_socket_data_set(apr_socket_t *sock, void *data, const char *key,
408 apr_status_t (*cleanup) (void *))
410 sock_userdata_t *new = apr_palloc(sock->pool, sizeof(sock_userdata_t));
412 new->key = apr_pstrdup(sock->pool, key);
414 new->next = sock->userdata;
415 sock->userdata = new;
418 apr_pool_cleanup_register(sock->pool, data, cleanup, cleanup);
424 apr_status_t apr_os_sock_get(apr_os_sock_t *thesock, apr_socket_t *sock)
426 *thesock = sock->socketdes;
430 apr_status_t apr_os_sock_make(apr_socket_t **apr_sock,
431 apr_os_sock_info_t *os_sock_info,
434 alloc_socket(apr_sock, cont);
435 set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, os_sock_info->protocol);
436 (*apr_sock)->timeout = -1;
437 (*apr_sock)->socketdes = *os_sock_info->os_sock;
438 if (os_sock_info->local) {
439 memcpy(&(*apr_sock)->local_addr->sa.sin,
441 (*apr_sock)->local_addr->salen);
442 /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
443 (*apr_sock)->local_addr->port = ntohs((*apr_sock)->local_addr->sa.sin.sin_port);
446 (*apr_sock)->local_port_unknown = (*apr_sock)->local_interface_unknown = 1;
448 if (os_sock_info->remote) {
450 (*apr_sock)->connected = 1;
452 memcpy(&(*apr_sock)->remote_addr->sa.sin,
453 os_sock_info->remote,
454 (*apr_sock)->remote_addr->salen);
455 /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
456 (*apr_sock)->remote_addr->port = ntohs((*apr_sock)->remote_addr->sa.sin.sin_port);
459 (*apr_sock)->remote_addr_unknown = 1;
462 (*apr_sock)->inherit = 0;
463 apr_pool_cleanup_register((*apr_sock)->pool, (void *)(*apr_sock),
464 socket_cleanup, socket_cleanup);
468 apr_status_t apr_os_sock_put(apr_socket_t **sock, apr_os_sock_t *thesock,
471 /* XXX Bogus assumption that *sock points at anything legit */
472 if ((*sock) == NULL) {
473 alloc_socket(sock, cont);
474 /* XXX IPv6 figure out the family here! */
475 /* XXX figure out the actual socket type here */
476 /* *or* just decide that apr_os_sock_put() has to be told the family and type */
477 set_socket_vars(*sock, APR_INET, SOCK_STREAM, 0);
478 (*sock)->timeout = -1;
480 (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
481 (*sock)->remote_addr_unknown = 1;
482 (*sock)->socketdes = *thesock;
486 APR_POOL_IMPLEMENT_ACCESSOR(socket)
488 APR_IMPLEMENT_INHERIT_SET(socket, inherit, pool, socket_cleanup)
490 APR_IMPLEMENT_INHERIT_UNSET(socket, inherit, pool, socket_cleanup)