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.
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
24 #include "apr_general.h"
26 #include "apu_config.h"
32 #include "apr_ssl_private.h"
33 #include "apr_network_io.h"
34 #include "apr_portable.h"
39 APU_DECLARE(apr_status_t
) apr_ssl_socket_create(apr_ssl_socket_t
**sock
,
42 apr_ssl_factory_t
*asf
,
45 apr_ssl_socket_t
*sslSock
;
51 thepool
= p
? p
: asf
->pool
;
55 sslSock
= apr_pcalloc(thepool
, sizeof(*sslSock
));
59 if (apr_socket_create(&sslSock
->plain
, family
, type
, protocol
, thepool
)
63 sslSock
->pool
= thepool
;
64 sslSock
->factory
= asf
;
65 if (apu_ssl_socket_create(sslSock
, asf
) != APR_SUCCESS
) {
66 apr_socket_close(sslSock
->plain
);
74 APU_DECLARE(apr_status_t
) apr_ssl_socket_close(apr_ssl_socket_t
*sock
)
77 if (!sock
|| !sock
->sslData
)
80 if ((rv
= apu_ssl_socket_close(sock
)) != APR_SUCCESS
)
82 return apr_socket_close(sock
->plain
);
85 APU_DECLARE(apr_status_t
) apr_ssl_socket_connect(apr_ssl_socket_t
*sock
,
91 if (!sock
|| !sock
->sslData
|| !sock
->plain
)
94 if ((rv
= apr_socket_connect(sock
->plain
, sa
)) != APR_SUCCESS
)
96 return apu_ssl_connect(sock
);
99 APU_DECLARE(apr_status_t
) apr_ssl_socket_send(apr_ssl_socket_t
*sock
,
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
,
115 return apr_socket_bind(sock
->plain
, sa
);
118 APU_DECLARE(apr_status_t
) apr_ssl_socket_listen(apr_ssl_socket_t
*sock
,
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
,
129 apr_socket_t
*newSock
;
130 apr_ssl_socket_t
*newSSLSock
;
133 if (!sock
|| !sock
->sslData
)
136 thepool
= (conn
? conn
: sock
->pool
);
140 rv
= apr_socket_accept(&newSock
, sock
->plain
, thepool
);
141 if (rv
!= APR_SUCCESS
)
144 newSSLSock
= apr_pcalloc(thepool
, sizeof(*newSSLSock
));
146 apr_socket_close(newSock
);
149 newSSLSock
->plain
= newSock
;
150 if (apu_ssl_accept(newSSLSock
, sock
, thepool
) != APR_SUCCESS
) {
151 apr_socket_close(newSock
);
158 APU_DECLARE(apr_status_t
) apr_ssl_socket_raw_error(apr_ssl_socket_t
*sock
)
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
)
170 /* socket is already in a pollset - return an error... */
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
)
184 sock
->pollset
= pollset
;
190 APU_DECLARE(apr_status_t
) apr_pollset_remove_ssl_socket(apr_ssl_socket_t
*sock
)
195 rv
= apr_pollset_remove(sock
->pollset
, sock
->poll
);
196 sock
->pollset
= NULL
;
200 APU_DECLARE(apr_status_t
) apr_ssl_socket_set_poll_events(apr_ssl_socket_t
*sock
,
206 if ((rv
= apr_pollset_remove(sock
->pollset
, sock
->poll
))
208 sock
->poll
->reqevents
= events
;
209 rv
= apr_pollset_add(sock
->pollset
, sock
->poll
);
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
,
220 apr_ssl_factory_t
*asf
,
226 APU_DECLARE(apr_status_t
) apr_ssl_socket_close(apr_ssl_socket_t
*sock
)
231 APU_DECLARE(apr_status_t
) apr_ssl_socket_connect(apr_ssl_socket_t
*sock
,
237 APU_DECLARE(apr_status_t
) apr_ssl_socket_send(apr_ssl_socket_t
*sock
,
244 APU_DECLARE(apr_status_t
) apr_ssl_socket_recv(apr_ssl_socket_t
* sock
,
245 char *buf
, apr_size_t
*len
)
250 APU_DECLARE(apr_status_t
) apr_ssl_socket_bind(apr_ssl_socket_t
*sock
,
256 APU_DECLARE(apr_status_t
) apr_ssl_socket_listen(apr_ssl_socket_t
*sock
,
262 APU_DECLARE(apr_status_t
) apr_ssl_socket_accept(apr_ssl_socket_t
**news
,
263 apr_ssl_socket_t
*sock
,
269 APU_DECLARE(apr_status_t
) apr_ssl_socket_raw_error(apr_ssl_socket_t
*sock
)
274 APU_DECLARE(apr_status_t
) apr_pollset_add_ssl_socket(apr_pollset_t
*pollset
,
275 apr_ssl_socket_t
*sock
)
280 APU_DECLARE(apr_status_t
) apr_pollset_remove_ssl_socket(apr_ssl_socket_t
*sock
)
285 APU_DECLARE(apr_status_t
) apr_ssl_socket_set_poll_events(apr_ssl_socket_t
*sock
,
291 #endif /* APU_HAVE_SSL */