Any test returning non-zero should stop make
[apr-util.git] / ssl / apr_ssl_socket.c
blob4f58b664c4c8d018dbca44b61c546920aa9a8302
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.h"
18 #include "apr_errno.h"
19 #include "apr_pools.h"
20 #include "apr_strings.h"
21 #define APR_WANT_MEMFUNC
22 #define APR_WANT_STRFUNC
23 #include "apr_want.h"
24 #include "apr_general.h"
26 #include "apu_config.h"
27 #include "apu.h"
28 #include "apr_ssl.h"
30 #ifdef APU_HAVE_SSL
32 #include "apr_ssl_private.h"
33 #include "apr_network_io.h"
34 #include "apr_portable.h"
36 #include <stdio.h>
39 APU_DECLARE(apr_status_t) apr_ssl_socket_create(apr_ssl_socket_t **sock,
40 int family, int type,
41 int protocol,
42 apr_ssl_factory_t *asf,
43 apr_pool_t *p)
45 apr_ssl_socket_t *sslSock;
46 apr_pool_t *thepool;
48 if (!asf)
49 return -1;
51 thepool = p ? p : asf->pool;
52 if (!thepool)
53 return APR_ENOPOOL;
55 sslSock = apr_pcalloc(thepool, sizeof(*sslSock));
56 if (!sslSock)
57 return ENOMEM;
59 if (apr_socket_create(&sslSock->plain, family, type, protocol, thepool)
60 != APR_SUCCESS) {
61 return -1;
63 sslSock->pool = thepool;
64 sslSock->factory = asf;
65 if (apu_ssl_socket_create(sslSock, asf) != APR_SUCCESS) {
66 apr_socket_close(sslSock->plain);
67 return -1;
70 *sock = sslSock;
71 return APR_SUCCESS;
74 APU_DECLARE(apr_status_t) apr_ssl_socket_close(apr_ssl_socket_t *sock)
76 apr_status_t rv;
77 if (!sock || !sock->sslData)
78 return APR_EINVAL;
80 if ((rv = apu_ssl_socket_close(sock)) != APR_SUCCESS)
81 return rv;
82 return apr_socket_close(sock->plain);
85 APU_DECLARE(apr_status_t) apr_ssl_socket_connect(apr_ssl_socket_t *sock,
86 apr_sockaddr_t *sa)
88 apr_status_t rv;
89 int sslErr;
91 if (!sock || !sock->sslData || !sock->plain)
92 return APR_EINVAL;
94 if ((rv = apr_socket_connect(sock->plain, sa)) != APR_SUCCESS)
95 return rv;
96 return apu_ssl_connect(sock);
99 APU_DECLARE(apr_status_t) apr_ssl_socket_send(apr_ssl_socket_t *sock,
100 const char *buf,
101 apr_size_t *len)
103 return apu_ssl_send(sock, buf, len);
106 APU_DECLARE(apr_status_t) apr_ssl_socket_recv(apr_ssl_socket_t * sock,
107 char *buf, apr_size_t *len)
109 return apu_ssl_recv(sock, buf, len);
112 APU_DECLARE(apr_status_t) apr_ssl_socket_bind(apr_ssl_socket_t *sock,
113 apr_sockaddr_t *sa)
115 return apr_socket_bind(sock->plain, sa);
118 APU_DECLARE(apr_status_t) apr_ssl_socket_listen(apr_ssl_socket_t *sock,
119 apr_int32_t backlog)
121 return apr_socket_listen(sock->plain, backlog);
124 APU_DECLARE(apr_status_t) apr_ssl_socket_accept(apr_ssl_socket_t **news,
125 apr_ssl_socket_t *sock,
126 apr_pool_t *conn)
128 apr_status_t rv;
129 apr_socket_t *newSock;
130 apr_ssl_socket_t *newSSLSock;
131 apr_pool_t *thepool;
133 if (!sock || !sock->sslData)
134 return APR_EINVAL;
136 thepool = (conn ? conn : sock->pool);
137 if (!thepool)
138 return APR_ENOPOOL;
140 rv = apr_socket_accept(&newSock, sock->plain, thepool);
141 if (rv != APR_SUCCESS)
142 return rv;
144 newSSLSock = apr_pcalloc(thepool, sizeof(*newSSLSock));
145 if (!newSSLSock) {
146 apr_socket_close(newSock);
147 return ENOMEM;
149 newSSLSock->plain = newSock;
150 if (apu_ssl_accept(newSSLSock, sock, thepool) != APR_SUCCESS) {
151 apr_socket_close(newSock);
152 return APR_EGENERAL;
154 *news = newSSLSock;
155 return APR_SUCCESS;
158 APU_DECLARE(apr_status_t) apr_ssl_socket_raw_error(apr_ssl_socket_t *sock)
160 if (!sock)
161 return APR_EINVAL;
162 return apu_ssl_raw_error(sock);
165 APU_DECLARE(apr_status_t) apr_pollset_add_ssl_socket(apr_pollset_t *pollset,
166 apr_ssl_socket_t *sock)
168 apr_status_t rv;
169 if (sock->pollset)
170 /* socket is already in a pollset - return an error... */
171 return EALREADY;
173 if (!sock->poll) {
174 sock->poll = apr_pcalloc(sock->pool, sizeof(*sock->poll));
176 sock->poll->desc_type = APR_POLL_SOCKET;
177 sock->poll->desc.s = sock->plain;
178 sock->poll->client_data = sock;
180 sock->poll->reqevents = APR_POLLIN | APR_POLLOUT;
181 rv = apr_pollset_add(pollset, sock->poll);
182 if (rv != APR_SUCCESS)
183 sock->poll = NULL;
184 sock->pollset = pollset;
185 return rv;
190 APU_DECLARE(apr_status_t) apr_pollset_remove_ssl_socket(apr_ssl_socket_t *sock)
192 apr_status_t rv;
193 if (!sock->pollset)
194 return EINVAL;
195 rv = apr_pollset_remove(sock->pollset, sock->poll);
196 sock->pollset = NULL;
197 return rv;
200 APU_DECLARE(apr_status_t) apr_ssl_socket_set_poll_events(apr_ssl_socket_t *sock,
201 apr_int16_t events)
203 apr_status_t rv;
204 if (!sock->poll)
205 return EINVAL;
206 if ((rv = apr_pollset_remove(sock->pollset, sock->poll))
207 == APR_SUCCESS) {
208 sock->poll->reqevents = events;
209 rv = apr_pollset_add(sock->pollset, sock->poll);
211 return rv;
215 #else /* ! APU_HAVE_SSL */
217 APU_DECLARE(apr_status_t) apr_ssl_socket_create(apr_ssl_socket_t **sock,
218 int family, int type,
219 int protocol,
220 apr_ssl_factory_t *asf,
221 apr_pool_t *p)
223 return APR_ENOTIMPL;
226 APU_DECLARE(apr_status_t) apr_ssl_socket_close(apr_ssl_socket_t *sock)
228 return APR_ENOTIMPL;
231 APU_DECLARE(apr_status_t) apr_ssl_socket_connect(apr_ssl_socket_t *sock,
232 apr_sockaddr_t *sa)
234 return APR_ENOTIMPL;
237 APU_DECLARE(apr_status_t) apr_ssl_socket_send(apr_ssl_socket_t *sock,
238 const char *buf,
239 apr_size_t *len)
241 return APR_ENOTIMPL;
244 APU_DECLARE(apr_status_t) apr_ssl_socket_recv(apr_ssl_socket_t * sock,
245 char *buf, apr_size_t *len)
247 return APR_ENOTIMPL;
250 APU_DECLARE(apr_status_t) apr_ssl_socket_bind(apr_ssl_socket_t *sock,
251 apr_sockaddr_t *sa)
253 return APR_ENOTIMPL;
256 APU_DECLARE(apr_status_t) apr_ssl_socket_listen(apr_ssl_socket_t *sock,
257 apr_int32_t backlog)
259 return APR_ENOTIMPL;
262 APU_DECLARE(apr_status_t) apr_ssl_socket_accept(apr_ssl_socket_t **news,
263 apr_ssl_socket_t *sock,
264 apr_pool_t *conn)
266 return APR_ENOTIMPL;
269 APU_DECLARE(apr_status_t) apr_ssl_socket_raw_error(apr_ssl_socket_t *sock)
271 return APR_ENOTIMPL;
274 APU_DECLARE(apr_status_t) apr_pollset_add_ssl_socket(apr_pollset_t *pollset,
275 apr_ssl_socket_t *sock)
277 return APR_ENOTIMPL;
280 APU_DECLARE(apr_status_t) apr_pollset_remove_ssl_socket(apr_ssl_socket_t *sock)
282 return APR_ENOTIMPL;
285 APU_DECLARE(apr_status_t) apr_ssl_socket_set_poll_events(apr_ssl_socket_t *sock,
286 apr_int16_t events)
288 return APR_ENOTIMPL;
291 #endif /* APU_HAVE_SSL */