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
* testssl: Simple APR SSL sockets test.
21
#include "apr_general.h"
22
#include "apr_pools.h"
23
#include "apr_errno.h"
24
#include "apr_getopt.h"
26
#define APR_WANT_STRFUNC
30
#include "apr_network_io.h"
38
#include <stdlib.h> /* for atexit(), malloc() */
47
{ "svn.apache.org", 443, "GET / HTTP/1.0\n\n", 1 },
51
static apr_ssl_socket_t *createSocket(apr_ssl_factory_t *asf,
52
apr_pollset_t *pollset,
53
apr_pool_t *pool, int blocking)
55
apr_ssl_socket_t *sock;
57
printf("::Creating SSL socket\n");
58
rv = apr_ssl_socket_create(&sock, AF_INET, SOCK_STREAM, 0, asf, NULL);
59
if (rv != APR_SUCCESS) {
60
printf("\tFailed to create socket\n");
63
rv = apr_pollset_add_ssl_socket(pollset, sock);
64
if (rv != APR_SUCCESS) {
65
printf("\tFailed to add to pollset\n");
72
static apr_status_t connectSocket(apr_ssl_socket_t *sock,
73
const char *host, int port,
77
apr_sockaddr_t *remoteSA;
79
printf("::Connecting socket\n");
80
rv = apr_sockaddr_info_get(&remoteSA, host, APR_UNSPEC, port, 0, pool);
81
if (rv != APR_SUCCESS) {
82
printf("\tFailed to get address for '%s', port %d\n", host, port);
85
rv = apr_ssl_socket_connect(sock, remoteSA);
86
if (rv != APR_SUCCESS) {
87
printf("\tFailed to connect to '%s' port %d\n", host, port);
94
static apr_status_t socketRead(apr_ssl_socket_t *sock,
95
apr_pollset_t *pollset,
96
char *buf, apr_size_t *len)
99
const apr_pollfd_t *descs = NULL;
102
printf("::Reading from socket\n");
103
rv = apr_ssl_socket_set_poll_events(sock, APR_POLLIN);
104
if (rv != APR_SUCCESS) {
105
printf("\tUnable to change socket poll events!\n");
109
rv = apr_pollset_poll(pollset, 30 * APR_USEC_PER_SEC, &lrv, &descs);
110
if (APR_STATUS_IS_TIMEUP(rv)) {
111
printf("\tTime up!\n");
116
printf("\tIncorrect return count, %d\n", lrv);
119
if (descs[0].client_data != sock) {
120
printf("\tWrong socket returned?!\n");
123
if ((descs[0].rtnevents & APR_POLLIN) == 0) {
124
printf("\tSocket wasn't ready? huh? req [%08x] vs rtn [%08x]\n",
125
descs[0].reqevents, descs[0].rtnevents);
128
rv = apr_ssl_socket_recv(sock, buf, len);
129
if (rv == APR_SUCCESS)
130
printf("\tOK, read %d bytes\n", *len);
132
printf("\tFailed\n");
136
static apr_status_t socketWrite(apr_ssl_socket_t *sock,
137
apr_pollset_t *pollset,
138
const char *buf, apr_size_t *len)
141
const apr_pollfd_t *descs = NULL;
144
printf("::Writing to socket\n");
145
rv = apr_ssl_socket_set_poll_events(sock, APR_POLLOUT);
146
if (rv != APR_SUCCESS) {
147
printf("\tUnable to change socket poll events!\n");
151
rv = apr_pollset_poll(pollset, 30 * APR_USEC_PER_SEC, &lrv, &descs);
152
if (APR_STATUS_IS_TIMEUP(rv)) {
153
printf("\tTime up!\n");
157
printf("\tIncorrect return count, %d\n", lrv);
160
if (descs[0].client_data != sock) {
161
printf("\tWrong socket returned?!\n");
164
if ((descs[0].rtnevents & APR_POLLOUT) == 0) {
165
printf("\tSocket wasn't ready? huh?\n");
168
rv = apr_ssl_socket_send(sock, buf, len);
169
if (rv == APR_SUCCESS)
170
printf("\tOK, wrote %d bytes\n", *len);
172
printf("\tFailed\n");
176
apr_status_t socketClose(apr_ssl_socket_t *sock, apr_pollset_t *pollset)
179
printf("::Closing socket\n");
180
rv = apr_pollset_remove_ssl_socket(sock);
181
if (rv != APR_SUCCESS)
182
printf("\tUnable to remove socket from pollset?\n");
183
rv = apr_ssl_socket_close(sock);
184
if (rv != APR_SUCCESS)
185
printf("\tFailed to close SSL socket\n");
192
int main(int argc, const char * const * argv)
195
apr_ssl_factory_t *asf = NULL;
197
apr_pollset_t *pollset;
199
(void) apr_initialize();
200
apr_pool_create(&pool, NULL);
201
atexit(apr_terminate);
203
printf("SSL Library: %s\n", apr_ssl_library_name());
205
if (apr_pollset_create(&pollset, 1, pool, 0) != APR_SUCCESS) {
206
printf("Failed to create pollset!\n");
210
if (apr_ssl_factory_create(&asf, NULL, NULL, NULL,
211
APR_SSL_FACTORY_CLIENT, pool) != APR_SUCCESS) {
212
fprintf(stderr, "Unable to create client factory\n");
215
for(i = 0; tests[i].host; i++) {
216
apr_ssl_socket_t *sslSock = createSocket(asf, pollset, pool, 0);
220
rv = connectSocket(sslSock, tests[i].host, tests[i].port, pool);
221
if (rv == APR_SUCCESS) {
222
apr_size_t len = strlen(tests[i].request);
223
rv = socketWrite(sslSock, pollset, tests[i].request, &len);
224
if (rv == APR_SUCCESS) {
227
rv = socketRead(sslSock, pollset, buffer, &len);
230
socketClose(sslSock, pollset);
234
apr_pollset_destroy(pollset);
235
apr_pool_destroy(pool);