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_arch_inherit.h" 19#include "apr_network_io.h" 20#include "apr_general.h" 21#include "apr_portable.h" 22#include "apr_lib.h" 23#include "apr_strings.h" 24#include <errno.h> 25#include <string.h> 26#include <sys/socket.h> 27#include <netinet/tcp.h> 28#include <netinet/in.h> 29#include <arpa/inet.h> 30#include <netdb.h> 31#include "apr_arch_os2calls.h" 32 33static apr_status_t socket_cleanup(void *sock) 34{ 35 apr_socket_t *thesocket = sock; 36 37 if (thesocket->socketdes < 0) { 38 return APR_EINVALSOCK; 39 } 40 41 if (soclose(thesocket->socketdes) == 0) { 42 thesocket->socketdes = -1; 43 return APR_SUCCESS; 44 } 45 else { 46 return APR_OS2_STATUS(sock_errno()); 47 } 48} 49 50static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol) 51{ 52 sock->type = type; 53 sock->protocol = protocol; 54 apr_sockaddr_vars_set(sock->local_addr, family, 0); 55 apr_sockaddr_vars_set(sock->remote_addr, family, 0); 56} 57 58static void alloc_socket(apr_socket_t **new, apr_pool_t *p) 59{ 60 *new = (apr_socket_t *)apr_pcalloc(p, sizeof(apr_socket_t)); 61 (*new)->pool = p; 62 (*new)->local_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool, 63 sizeof(apr_sockaddr_t)); 64 (*new)->local_addr->pool = p; 65 66 (*new)->remote_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool, 67 sizeof(apr_sockaddr_t)); 68 (*new)->remote_addr->pool = p; 69 (*new)->remote_addr_unknown = 1; 70 71 /* Create a pollset with room for one descriptor. */ 72 /* ### check return codes */ 73 (void) apr_pollset_create(&(*new)->pollset, 1, p, 0); 74} 75 76APR_DECLARE(apr_status_t) apr_socket_protocol_get(apr_socket_t *sock, int *protocol) 77{ 78 *protocol = sock->protocol; 79 return APR_SUCCESS; 80} 81 82APR_DECLARE(apr_status_t) apr_socket_create(apr_socket_t **new, int family, int type, 83 int protocol, apr_pool_t *cont) 84{ 85 int downgrade = (family == AF_UNSPEC); 86 apr_pollfd_t pfd; 87 88 if (family == AF_UNSPEC) { 89#if APR_HAVE_IPV6 90 family = AF_INET6; 91#else 92 family = AF_INET; 93#endif 94 } 95 96 alloc_socket(new, cont); 97 98 (*new)->socketdes = socket(family, type, protocol); 99#if APR_HAVE_IPV6 100 if ((*new)->socketdes < 0 && downgrade) { 101 family = AF_INET; 102 (*new)->socketdes = socket(family, type, protocol); 103 } 104#endif 105 106 if ((*new)->socketdes < 0) { 107 return APR_OS2_STATUS(sock_errno()); 108 } 109 set_socket_vars(*new, family, type, protocol); 110 111 (*new)->timeout = -1; 112 (*new)->nonblock = FALSE; 113 apr_pool_cleanup_register((*new)->pool, (void *)(*new), 114 socket_cleanup, apr_pool_cleanup_null); 115 116 return APR_SUCCESS; 117} 118 119APR_DECLARE(apr_status_t) apr_socket_shutdown(apr_socket_t *thesocket, 120 apr_shutdown_how_e how) 121{ 122 if (shutdown(thesocket->socketdes, how) == 0) { 123 return APR_SUCCESS; 124 } 125 else { 126 return APR_OS2_STATUS(sock_errno()); 127 } 128} 129 130APR_DECLARE(apr_status_t) apr_socket_close(apr_socket_t *thesocket) 131{ 132 apr_pool_cleanup_kill(thesocket->pool, thesocket, socket_cleanup); 133 return socket_cleanup(thesocket); 134} 135 136APR_DECLARE(apr_status_t) apr_socket_bind(apr_socket_t *sock, 137 apr_sockaddr_t *sa) 138{ 139 if (bind(sock->socketdes, 140 (struct sockaddr *)&sa->sa, 141 sa->salen) == -1) 142 return APR_OS2_STATUS(sock_errno()); 143 else { 144 sock->local_addr = sa; 145 /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */ 146 if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing w/ 0 */ 147 sock->local_port_unknown = 1; /* kernel got us an ephemeral port */ 148 } 149 return APR_SUCCESS; 150 } 151} 152 153APR_DECLARE(apr_status_t) apr_socket_listen(apr_socket_t *sock, 154 apr_int32_t backlog) 155{ 156 if (listen(sock->socketdes, backlog) == -1) 157 return APR_OS2_STATUS(sock_errno()); 158 else 159 return APR_SUCCESS; 160} 161 162APR_DECLARE(apr_status_t) apr_socket_accept(apr_socket_t **new, 163 apr_socket_t *sock, 164 apr_pool_t *connection_context) 165{ 166 alloc_socket(new, connection_context); 167 set_socket_vars(*new, sock->local_addr->sa.sin.sin_family, SOCK_STREAM, sock->protocol); 168 169 (*new)->timeout = -1; 170 (*new)->nonblock = FALSE; 171 172 (*new)->socketdes = accept(sock->socketdes, 173 (struct sockaddr *)&(*new)->remote_addr->sa, 174 &(*new)->remote_addr->salen); 175 176 if ((*new)->socketdes < 0) { 177 return APR_OS2_STATUS(sock_errno()); 178 } 179 180 *(*new)->local_addr = *sock->local_addr; 181 (*new)->local_addr->pool = connection_context; 182 (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port); 183 184 /* fix up any pointers which are no longer valid */ 185 if (sock->local_addr->sa.sin.sin_family == AF_INET) { 186 (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin.sin_addr; 187 } 188 189 apr_pool_cleanup_register((*new)->pool, (void *)(*new), 190 socket_cleanup, apr_pool_cleanup_null); 191 return APR_SUCCESS; 192} 193 194APR_DECLARE(apr_status_t) apr_socket_connect(apr_socket_t *sock, 195 apr_sockaddr_t *sa) 196{ 197 if ((connect(sock->socketdes, (struct sockaddr *)&sa->sa.sin, 198 sa->salen) < 0) && 199 (sock_errno() != SOCEINPROGRESS)) { 200 return APR_OS2_STATUS(sock_errno()); 201 } 202 else { 203 int namelen = sizeof(sock->local_addr->sa.sin); 204 getsockname(sock->socketdes, (struct sockaddr *)&sock->local_addr->sa.sin, 205 &namelen); 206 sock->remote_addr = sa; 207 return APR_SUCCESS; 208 } 209} 210 211APR_DECLARE(apr_status_t) apr_socket_type_get(apr_socket_t *sock, int *type) 212{ 213 *type = sock->type; 214 return APR_SUCCESS; 215} 216 217APR_DECLARE(apr_status_t) apr_socket_data_get(void **data, const char *key, 218 apr_socket_t *sock) 219{ 220 sock_userdata_t *cur = sock->userdata; 221 222 *data = NULL; 223 224 while (cur) { 225 if (!strcmp(cur->key, key)) { 226 *data = cur->data; 227 break; 228 } 229 cur = cur->next; 230 } 231 232 return APR_SUCCESS; 233} 234 235 236 237APR_DECLARE(apr_status_t) apr_socket_data_set(apr_socket_t *sock, void *data, const char *key, 238 apr_status_t (*cleanup) (void *)) 239{ 240 sock_userdata_t *new = apr_palloc(sock->pool, sizeof(sock_userdata_t)); 241 242 new->key = apr_pstrdup(sock->pool, key); 243 new->data = data; 244 new->next = sock->userdata; 245 sock->userdata = new; 246 247 if (cleanup) { 248 apr_pool_cleanup_register(sock->pool, data, cleanup, cleanup); 249 } 250 251 return APR_SUCCESS; 252} 253 254APR_DECLARE(apr_status_t) apr_os_sock_get(apr_os_sock_t *thesock, apr_socket_t *sock) 255{ 256 *thesock = sock->socketdes; 257 return APR_SUCCESS; 258} 259 260APR_DECLARE(apr_status_t) apr_os_sock_make(apr_socket_t **apr_sock, 261 apr_os_sock_info_t *os_sock_info, 262 apr_pool_t *cont) 263{ 264 alloc_socket(apr_sock, cont); 265 set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, os_sock_info->protocol); 266 (*apr_sock)->timeout = -1; 267 (*apr_sock)->socketdes = *os_sock_info->os_sock; 268 if (os_sock_info->local) { 269 memcpy(&(*apr_sock)->local_addr->sa.sin, 270 os_sock_info->local, 271 (*apr_sock)->local_addr->salen); 272 /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */ 273 (*apr_sock)->local_addr->port = ntohs((*apr_sock)->local_addr->sa.sin.sin_port); 274 } 275 else { 276 (*apr_sock)->local_port_unknown = (*apr_sock)->local_interface_unknown = 1; 277 } 278 if (os_sock_info->remote) { 279 memcpy(&(*apr_sock)->remote_addr->sa.sin, 280 os_sock_info->remote, 281 (*apr_sock)->remote_addr->salen); 282 /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */ 283 (*apr_sock)->remote_addr->port = ntohs((*apr_sock)->remote_addr->sa.sin.sin_port); 284 } 285 else { 286 (*apr_sock)->remote_addr_unknown = 1; 287 } 288 289 apr_pool_cleanup_register((*apr_sock)->pool, (void *)(*apr_sock), 290 socket_cleanup, apr_pool_cleanup_null); 291 292 return APR_SUCCESS; 293} 294 295APR_DECLARE(apr_status_t) apr_os_sock_put(apr_socket_t **sock, apr_os_sock_t *thesock, apr_pool_t *cont) 296{ 297 if (cont == NULL) { 298 return APR_ENOPOOL; 299 } 300 if ((*sock) == NULL) { 301 alloc_socket(sock, cont); 302 set_socket_vars(*sock, AF_INET, SOCK_STREAM, 0); 303 (*sock)->timeout = -1; 304 } 305 306 (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1; 307 (*sock)->remote_addr_unknown = 1; 308 (*sock)->socketdes = *thesock; 309 return APR_SUCCESS; 310} 311 312APR_POOL_IMPLEMENT_ACCESSOR(socket); 313 314APR_IMPLEMENT_INHERIT_SET(socket, inherit, pool, socket_cleanup) 315 316APR_IMPLEMENT_INHERIT_UNSET(socket, inherit, pool, socket_cleanup) 317 318