upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / network_io / unix / sockets.c
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 #if defined(BEOS) && !defined(BEOS_BONE)
25 #define close closesocket
26 #endif
27
28 static char generic_inaddr_any[16] = {0}; /* big enough for IPv4 or IPv6 */
29
30 static apr_status_t socket_cleanup(void *sock)
31 {
32     apr_socket_t *thesocket = sock;
33
34     if (close(thesocket->socketdes) == 0) {
35         thesocket->socketdes = -1;
36         return APR_SUCCESS;
37     }
38     else {
39         return errno;
40     }
41 }
42
43 static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol)
44 {
45     sock->type = type;
46     sock->protocol = protocol;
47     apr_sockaddr_vars_set(sock->local_addr, family, 0);
48     apr_sockaddr_vars_set(sock->remote_addr, family, 0);
49     sock->netmask = 0;
50 #if defined(BEOS) && !defined(BEOS_BONE)
51     /* BeOS pre-BONE has TCP_NODELAY on by default and it can't be
52      * switched off!
53      */
54     sock->netmask |= APR_TCP_NODELAY;
55 #endif
56 }
57
58 static 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)->cntxt = p;
62     (*new)->local_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->cntxt,
63                                                        sizeof(apr_sockaddr_t));
64     (*new)->local_addr->pool = p;
65     (*new)->remote_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->cntxt,
66                                                         sizeof(apr_sockaddr_t));
67     (*new)->remote_addr->pool = p;
68 }
69
70 apr_status_t apr_socket_protocol_get(apr_socket_t *sock, int *protocol)
71 {
72     *protocol = sock->protocol;
73     return APR_SUCCESS;
74 }
75
76 apr_status_t apr_socket_create_ex(apr_socket_t **new, int ofamily, int type,
77                                   int protocol, apr_pool_t *cont)
78 {
79     int family = ofamily;
80
81     if (family == APR_UNSPEC) {
82 #if APR_HAVE_IPV6
83         family = APR_INET6;
84 #else
85         family = APR_INET;
86 #endif
87     }
88
89     alloc_socket(new, cont);
90
91     (*new)->socketdes = socket(family, type, protocol);
92
93 #if APR_HAVE_IPV6
94     if ((*new)->socketdes < 0 && ofamily == APR_UNSPEC) {
95         family = APR_INET;
96         (*new)->socketdes = socket(family, type, protocol);
97     }
98 #endif
99
100     if ((*new)->socketdes < 0) {
101         return errno;
102     }
103     set_socket_vars(*new, family, type, protocol);
104
105     (*new)->timeout = -1;
106     (*new)->inherit = 0;
107     apr_pool_cleanup_register((*new)->cntxt, (void *)(*new), socket_cleanup,
108                               socket_cleanup);
109     return APR_SUCCESS;
110
111
112 apr_status_t apr_socket_create(apr_socket_t **new, int family, int type,
113                                apr_pool_t *cont)
114 {
115     return apr_socket_create_ex(new, family, type, 0, cont);
116 }
117
118 apr_status_t apr_socket_shutdown(apr_socket_t *thesocket, 
119                                  apr_shutdown_how_e how)
120 {
121     return (shutdown(thesocket->socketdes, how) == -1) ? errno : APR_SUCCESS;
122 }
123
124 apr_status_t apr_socket_close(apr_socket_t *thesocket)
125 {
126     return apr_pool_cleanup_run(thesocket->cntxt, thesocket, socket_cleanup);
127 }
128
129 apr_status_t apr_socket_bind(apr_socket_t *sock, apr_sockaddr_t *sa)
130 {
131     if (bind(sock->socketdes, 
132              (struct sockaddr *)&sa->sa, sa->salen) == -1) {
133         return errno;
134     }
135     else {
136         sock->local_addr = sa;
137         /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
138         if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing w/ 0 */
139             sock->local_port_unknown = 1; /* kernel got us an ephemeral port */
140         }
141         return APR_SUCCESS;
142     }
143 }
144
145 apr_status_t apr_socket_listen(apr_socket_t *sock, apr_int32_t backlog)
146 {
147     if (listen(sock->socketdes, backlog) == -1)
148         return errno;
149     else
150         return APR_SUCCESS;
151 }
152
153 apr_status_t apr_socket_accept(apr_socket_t **new, apr_socket_t *sock,
154                                apr_pool_t *connection_context)
155 {
156     alloc_socket(new, connection_context);
157     set_socket_vars(*new, sock->local_addr->sa.sin.sin_family, SOCK_STREAM, sock->protocol);
158
159 #ifndef HAVE_POLL
160     (*new)->connected = 1;
161 #endif
162     (*new)->timeout = -1;
163     
164     (*new)->socketdes = accept(sock->socketdes, 
165                                (struct sockaddr *)&(*new)->remote_addr->sa,
166                                &(*new)->remote_addr->salen);
167
168     if ((*new)->socketdes < 0) {
169         return errno;
170     }
171 #ifdef TPF
172     if ((*new)->socketdes == 0) { 
173         /* 0 is an invalid socket for TPF */
174         return APR_EINTR;
175     }
176 #endif
177
178     *(*new)->local_addr = *sock->local_addr;
179
180     /* The above assignment just overwrote the pool entry. Setting the local_addr 
181        pool for the accepted socket back to what it should be.  Otherwise all 
182        allocations for this socket will come from a server pool that is not
183        freed until the process goes down.*/
184     (*new)->local_addr->pool = connection_context;
185
186     /* fix up any pointers which are no longer valid */
187     if (sock->local_addr->sa.sin.sin_family == AF_INET) {
188         (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin.sin_addr;
189     }
190 #if APR_HAVE_IPV6
191     else if (sock->local_addr->sa.sin.sin_family == AF_INET6) {
192         (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin6.sin6_addr;
193     }
194 #endif
195     (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port);
196     if (sock->local_port_unknown) {
197         /* not likely for a listening socket, but theoretically possible :) */
198         (*new)->local_port_unknown = 1;
199     }
200
201 #if APR_TCP_NODELAY_INHERITED
202     if (apr_is_option_set(sock->netmask, APR_TCP_NODELAY) == 1) {
203         apr_set_option(&(*new)->netmask, APR_TCP_NODELAY, 1);
204     }
205 #endif /* TCP_NODELAY_INHERITED */
206 #if APR_O_NONBLOCK_INHERITED
207     if (apr_is_option_set(sock->netmask, APR_SO_NONBLOCK) == 1) {
208         apr_set_option(&(*new)->netmask, APR_SO_NONBLOCK, 1);
209     }
210 #endif /* APR_O_NONBLOCK_INHERITED */
211
212     if (sock->local_interface_unknown ||
213         !memcmp(sock->local_addr->ipaddr_ptr,
214                 generic_inaddr_any,
215                 sock->local_addr->ipaddr_len)) {
216         /* If the interface address inside the listening socket's local_addr wasn't 
217          * up-to-date, we don't know local interface of the connected socket either.
218          *
219          * If the listening socket was not bound to a specific interface, we
220          * don't know the local_addr of the connected socket.
221          */
222         (*new)->local_interface_unknown = 1;
223     }
224
225     (*new)->inherit = 0;
226     apr_pool_cleanup_register((*new)->cntxt, (void *)(*new), socket_cleanup,
227                               socket_cleanup);
228     return APR_SUCCESS;
229 }
230
231 apr_status_t apr_socket_connect(apr_socket_t *sock, apr_sockaddr_t *sa)
232 {
233     int rc;        
234
235     do {
236         rc = connect(sock->socketdes,
237                      (const struct sockaddr *)&sa->sa.sin,
238                      sa->salen);
239     } while (rc == -1 && errno == EINTR);
240
241     /* we can see EINPROGRESS the first time connect is called on a non-blocking
242      * socket; if called again, we can see EALREADY
243      */
244     if (rc == -1 && (errno == EINPROGRESS || errno == EALREADY) &&
245         apr_is_option_set(sock->netmask, APR_SO_TIMEOUT)) {
246         rc = apr_wait_for_io_or_timeout(NULL, sock, 0);
247         if (rc != APR_SUCCESS) {
248             return rc;
249         }
250
251 #ifdef SO_ERROR
252         {
253             int error;
254             apr_socklen_t len = sizeof(error);
255             if ((rc = getsockopt(sock->socketdes, SOL_SOCKET, SO_ERROR, 
256                                  (char *)&error, &len)) < 0) {
257                 return errno;
258             }
259             if (error) {
260                 return error;
261             }
262         }
263 #endif /* SO_ERROR */
264     }
265
266     if (rc == -1 && errno != EISCONN) {
267         return errno;
268     }
269
270     sock->remote_addr = sa;
271     if (sock->local_addr->port == 0) {
272         /* connect() got us an ephemeral port */
273         sock->local_port_unknown = 1;
274     }
275     if (!memcmp(sock->local_addr->ipaddr_ptr,
276                 generic_inaddr_any,
277                 sock->local_addr->ipaddr_len)) {
278         /* not bound to specific local interface; connect() had to assign
279          * one for the socket
280          */
281         sock->local_interface_unknown = 1;
282     }
283 #ifndef HAVE_POLL
284     sock->connected=1;
285 #endif
286     return APR_SUCCESS;
287 }
288
289 apr_status_t apr_socket_data_get(void **data, const char *key, apr_socket_t *sock)
290 {
291     sock_userdata_t *cur = sock->userdata;
292
293     *data = NULL;
294
295     while (cur) {
296         if (!strcmp(cur->key, key)) {
297             *data = cur->data;
298             break;
299         }
300         cur = cur->next;
301     }
302
303     return APR_SUCCESS;
304 }
305
306 apr_status_t apr_socket_data_set(apr_socket_t *sock, void *data, const char *key,
307                                  apr_status_t (*cleanup) (void *))
308 {
309     sock_userdata_t *new = apr_palloc(sock->cntxt, sizeof(sock_userdata_t));
310
311     new->key = apr_pstrdup(sock->cntxt, key);
312     new->data = data;
313     new->next = sock->userdata;
314     sock->userdata = new;
315
316     if (cleanup) {
317         apr_pool_cleanup_register(sock->cntxt, data, cleanup, cleanup);
318     }
319
320     return APR_SUCCESS;
321 }
322
323 apr_status_t apr_os_sock_get(apr_os_sock_t *thesock, apr_socket_t *sock)
324 {
325     *thesock = sock->socketdes;
326     return APR_SUCCESS;
327 }
328
329 apr_status_t apr_os_sock_make(apr_socket_t **apr_sock, 
330                               apr_os_sock_info_t *os_sock_info, 
331                               apr_pool_t *cont)
332 {
333     alloc_socket(apr_sock, cont);
334 #ifdef APR_ENABLE_FOR_1_0 /* no protocol field yet */
335     set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, os_sock_info->protocol);
336 #else
337     set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, 0);
338 #endif
339     (*apr_sock)->timeout = -1;
340     (*apr_sock)->socketdes = *os_sock_info->os_sock;
341     if (os_sock_info->local) {
342         memcpy(&(*apr_sock)->local_addr->sa.sin, 
343                os_sock_info->local, 
344                (*apr_sock)->local_addr->salen);
345         /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
346         (*apr_sock)->local_addr->port = ntohs((*apr_sock)->local_addr->sa.sin.sin_port);
347     }
348     else {
349         (*apr_sock)->local_port_unknown = (*apr_sock)->local_interface_unknown = 1;
350     }
351     if (os_sock_info->remote) {
352 #ifndef HAVE_POLL
353         (*apr_sock)->connected = 1;
354 #endif
355         memcpy(&(*apr_sock)->remote_addr->sa.sin, 
356                os_sock_info->remote,
357                (*apr_sock)->remote_addr->salen);
358         /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
359         (*apr_sock)->remote_addr->port = ntohs((*apr_sock)->remote_addr->sa.sin.sin_port);
360     }
361     else {
362         (*apr_sock)->remote_addr_unknown = 1;
363     }
364         
365     (*apr_sock)->inherit = 0;
366     apr_pool_cleanup_register((*apr_sock)->cntxt, (void *)(*apr_sock), 
367                               socket_cleanup, socket_cleanup);
368     return APR_SUCCESS;
369 }
370
371 apr_status_t apr_os_sock_put(apr_socket_t **sock, apr_os_sock_t *thesock, 
372                            apr_pool_t *cont)
373 {
374     /* XXX Bogus assumption that *sock points at anything legit */
375     if ((*sock) == NULL) {
376         alloc_socket(sock, cont);
377         /* XXX IPv6 figure out the family here! */
378         /* XXX figure out the actual socket type here */
379         /* *or* just decide that apr_os_sock_put() has to be told the family and type */
380         set_socket_vars(*sock, APR_INET, SOCK_STREAM, 0);
381         (*sock)->timeout = -1;
382     }
383     (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
384     (*sock)->remote_addr_unknown = 1;
385     (*sock)->socketdes = *thesock;
386     return APR_SUCCESS;
387 }
388
389 APR_IMPLEMENT_INHERIT_SET(socket, inherit, cntxt, socket_cleanup)
390
391 APR_IMPLEMENT_INHERIT_UNSET(socket, inherit, cntxt, socket_cleanup)
392
393 /* deprecated */
394 apr_status_t apr_shutdown(apr_socket_t *thesocket, apr_shutdown_how_e how)
395 {
396     return apr_socket_shutdown(thesocket, how);
397 }
398
399 /* deprecated */
400 apr_status_t apr_bind(apr_socket_t *sock, apr_sockaddr_t *sa)
401 {
402     return apr_socket_bind(sock, sa);
403 }
404
405 /* deprecated */
406 apr_status_t apr_listen(apr_socket_t *sock, apr_int32_t backlog)
407 {
408     return apr_socket_listen(sock, backlog);
409 }
410
411 /* deprecated */
412 apr_status_t apr_accept(apr_socket_t **new, apr_socket_t *sock,
413                         apr_pool_t *connection_context)
414 {
415     return apr_socket_accept(new, sock, connection_context);
416 }
417
418 /* deprecated */
419 apr_status_t apr_connect(apr_socket_t *sock, apr_sockaddr_t *sa)
420 {
421     return apr_socket_connect(sock, sa);
422 }