~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/network_io/os2/sockets.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * 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
 
 
33
static 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
 
 
50
static 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
 
 
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)->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
    (*new)->remote_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
 
66
                                                        sizeof(apr_sockaddr_t));
 
67
    (*new)->remote_addr->pool = p;
 
68
 
 
69
    /* Create a pollset with room for one descriptor. */
 
70
    /* ### check return codes */
 
71
    (void) apr_pollset_create(&(*new)->pollset, 1, p, 0);
 
72
}
 
73
 
 
74
APR_DECLARE(apr_status_t) apr_socket_protocol_get(apr_socket_t *sock, int *protocol)
 
75
{
 
76
    *protocol = sock->protocol;
 
77
    return APR_SUCCESS;
 
78
}
 
79
 
 
80
APR_DECLARE(apr_status_t) apr_socket_create(apr_socket_t **new, int family, int type,
 
81
                                            int protocol, apr_pool_t *cont)
 
82
{
 
83
    int downgrade = (family == AF_UNSPEC);
 
84
    apr_pollfd_t pfd;
 
85
 
 
86
    if (family == AF_UNSPEC) {
 
87
#if APR_HAVE_IPV6
 
88
        family = AF_INET6;
 
89
#else
 
90
        family = AF_INET;
 
91
#endif
 
92
    }
 
93
 
 
94
    alloc_socket(new, cont);
 
95
 
 
96
    (*new)->socketdes = socket(family, type, protocol);
 
97
#if APR_HAVE_IPV6
 
98
    if ((*new)->socketdes < 0 && downgrade) {
 
99
        family = AF_INET;
 
100
        (*new)->socketdes = socket(family, type, protocol);
 
101
    }
 
102
#endif
 
103
 
 
104
    if ((*new)->socketdes < 0) {
 
105
        return APR_OS2_STATUS(sock_errno());
 
106
    }
 
107
    set_socket_vars(*new, family, type, protocol);
 
108
 
 
109
    (*new)->timeout = -1;
 
110
    (*new)->nonblock = FALSE;
 
111
    apr_pool_cleanup_register((*new)->pool, (void *)(*new), 
 
112
                        socket_cleanup, apr_pool_cleanup_null);
 
113
 
 
114
    return APR_SUCCESS;
 
115
 
116
 
 
117
APR_DECLARE(apr_status_t) apr_socket_shutdown(apr_socket_t *thesocket, 
 
118
                                              apr_shutdown_how_e how)
 
119
{
 
120
    if (shutdown(thesocket->socketdes, how) == 0) {
 
121
        return APR_SUCCESS;
 
122
    }
 
123
    else {
 
124
        return APR_OS2_STATUS(sock_errno());
 
125
    }
 
126
}
 
127
 
 
128
APR_DECLARE(apr_status_t) apr_socket_close(apr_socket_t *thesocket)
 
129
{
 
130
    apr_pool_cleanup_kill(thesocket->pool, thesocket, socket_cleanup);
 
131
    return socket_cleanup(thesocket);
 
132
}
 
133
 
 
134
APR_DECLARE(apr_status_t) apr_socket_bind(apr_socket_t *sock,
 
135
                                          apr_sockaddr_t *sa)
 
136
{
 
137
    if (bind(sock->socketdes, 
 
138
             (struct sockaddr *)&sa->sa,
 
139
             sa->salen) == -1)
 
140
        return APR_OS2_STATUS(sock_errno());
 
141
    else {
 
142
        sock->local_addr = sa;
 
143
        return APR_SUCCESS;
 
144
    }
 
145
}
 
146
 
 
147
APR_DECLARE(apr_status_t) apr_socket_listen(apr_socket_t *sock, 
 
148
                                            apr_int32_t backlog)
 
149
{
 
150
    if (listen(sock->socketdes, backlog) == -1)
 
151
        return APR_OS2_STATUS(sock_errno());
 
152
    else
 
153
        return APR_SUCCESS;
 
154
}
 
155
 
 
156
APR_DECLARE(apr_status_t) apr_socket_accept(apr_socket_t **new, 
 
157
                                            apr_socket_t *sock,
 
158
                                            apr_pool_t *connection_context)
 
159
{
 
160
    alloc_socket(new, connection_context);
 
161
    set_socket_vars(*new, sock->local_addr->sa.sin.sin_family, SOCK_STREAM, sock->protocol);
 
162
 
 
163
    (*new)->timeout = -1;
 
164
    (*new)->nonblock = FALSE;
 
165
 
 
166
    (*new)->socketdes = accept(sock->socketdes, 
 
167
                               (struct sockaddr *)&(*new)->remote_addr->sa,
 
168
                               &(*new)->remote_addr->salen);
 
169
 
 
170
    if ((*new)->socketdes < 0) {
 
171
        return APR_OS2_STATUS(sock_errno());
 
172
    }
 
173
 
 
174
    *(*new)->local_addr = *sock->local_addr;
 
175
    (*new)->local_addr->pool = connection_context;
 
176
    (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port);
 
177
 
 
178
    /* fix up any pointers which are no longer valid */
 
179
    if (sock->local_addr->sa.sin.sin_family == AF_INET) {
 
180
        (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin.sin_addr;
 
181
    }
 
182
 
 
183
    apr_pool_cleanup_register((*new)->pool, (void *)(*new), 
 
184
                        socket_cleanup, apr_pool_cleanup_null);
 
185
    return APR_SUCCESS;
 
186
}
 
187
 
 
188
APR_DECLARE(apr_status_t) apr_socket_connect(apr_socket_t *sock,
 
189
                                             apr_sockaddr_t *sa)
 
190
{
 
191
    if ((connect(sock->socketdes, (struct sockaddr *)&sa->sa.sin, 
 
192
                 sa->salen) < 0) &&
 
193
        (sock_errno() != SOCEINPROGRESS)) {
 
194
        return APR_OS2_STATUS(sock_errno());
 
195
    }
 
196
    else {
 
197
        int namelen = sizeof(sock->local_addr->sa.sin);
 
198
        getsockname(sock->socketdes, (struct sockaddr *)&sock->local_addr->sa.sin, 
 
199
                    &namelen);
 
200
        sock->remote_addr = sa;
 
201
        return APR_SUCCESS;
 
202
    }
 
203
}
 
204
 
 
205
APR_DECLARE(apr_status_t) apr_socket_type_get(apr_socket_t *sock, int *type)
 
206
{
 
207
    *type = sock->type;
 
208
    return APR_SUCCESS;
 
209
}
 
210
 
 
211
APR_DECLARE(apr_status_t) apr_socket_data_get(void **data, const char *key,
 
212
                                     apr_socket_t *sock)
 
213
{
 
214
    sock_userdata_t *cur = sock->userdata;
 
215
 
 
216
    *data = NULL;
 
217
 
 
218
    while (cur) {
 
219
        if (!strcmp(cur->key, key)) {
 
220
            *data = cur->data;
 
221
            break;
 
222
        }
 
223
        cur = cur->next;
 
224
    }
 
225
 
 
226
    return APR_SUCCESS;
 
227
}
 
228
 
 
229
 
 
230
 
 
231
APR_DECLARE(apr_status_t) apr_socket_data_set(apr_socket_t *sock, void *data, const char *key,
 
232
                                     apr_status_t (*cleanup) (void *))
 
233
{
 
234
    sock_userdata_t *new = apr_palloc(sock->pool, sizeof(sock_userdata_t));
 
235
 
 
236
    new->key = apr_pstrdup(sock->pool, key);
 
237
    new->data = data;
 
238
    new->next = sock->userdata;
 
239
    sock->userdata = new;
 
240
 
 
241
    if (cleanup) {
 
242
        apr_pool_cleanup_register(sock->pool, data, cleanup, cleanup);
 
243
    }
 
244
 
 
245
    return APR_SUCCESS;
 
246
}
 
247
 
 
248
APR_DECLARE(apr_status_t) apr_os_sock_get(apr_os_sock_t *thesock, apr_socket_t *sock)
 
249
{
 
250
    *thesock = sock->socketdes;
 
251
    return APR_SUCCESS;
 
252
}
 
253
 
 
254
APR_DECLARE(apr_status_t) apr_os_sock_make(apr_socket_t **apr_sock, 
 
255
                                           apr_os_sock_info_t *os_sock_info, 
 
256
                                           apr_pool_t *cont)
 
257
{
 
258
    alloc_socket(apr_sock, cont);
 
259
    set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, os_sock_info->protocol);
 
260
    (*apr_sock)->timeout = -1;
 
261
    (*apr_sock)->socketdes = *os_sock_info->os_sock;
 
262
    if (os_sock_info->local) {
 
263
        memcpy(&(*apr_sock)->local_addr->sa.sin, 
 
264
               os_sock_info->local, 
 
265
               (*apr_sock)->local_addr->salen);
 
266
        /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
 
267
        (*apr_sock)->local_addr->port = ntohs((*apr_sock)->local_addr->sa.sin.sin_port);
 
268
    }
 
269
    else {
 
270
        (*apr_sock)->local_port_unknown = (*apr_sock)->local_interface_unknown = 1;
 
271
    }
 
272
    if (os_sock_info->remote) {
 
273
        memcpy(&(*apr_sock)->remote_addr->sa.sin, 
 
274
               os_sock_info->remote,
 
275
               (*apr_sock)->remote_addr->salen);
 
276
        /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
 
277
        (*apr_sock)->remote_addr->port = ntohs((*apr_sock)->remote_addr->sa.sin.sin_port);
 
278
    }
 
279
    else {
 
280
        (*apr_sock)->remote_addr_unknown = 1;
 
281
    }
 
282
        
 
283
    apr_pool_cleanup_register((*apr_sock)->pool, (void *)(*apr_sock), 
 
284
                        socket_cleanup, apr_pool_cleanup_null);
 
285
 
 
286
    return APR_SUCCESS;
 
287
}
 
288
 
 
289
APR_DECLARE(apr_status_t) apr_os_sock_put(apr_socket_t **sock, apr_os_sock_t *thesock, apr_pool_t *cont)
 
290
{
 
291
    if (cont == NULL) {
 
292
        return APR_ENOPOOL;
 
293
    }
 
294
    if ((*sock) == NULL) {
 
295
        alloc_socket(sock, cont);
 
296
        set_socket_vars(*sock, AF_INET, SOCK_STREAM, 0);
 
297
        (*sock)->timeout = -1;
 
298
    }
 
299
 
 
300
    (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
 
301
    (*sock)->remote_addr_unknown = 1;
 
302
    (*sock)->socketdes = *thesock;
 
303
    return APR_SUCCESS;
 
304
}
 
305
 
 
306
APR_POOL_IMPLEMENT_ACCESSOR(socket);
 
307
 
 
308
APR_IMPLEMENT_INHERIT_SET(socket, inherit, pool, socket_cleanup)
 
309
 
 
310
APR_IMPLEMENT_INHERIT_UNSET(socket, inherit, pool, socket_cleanup)
 
311