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 <stdlib.h>
18 #include "testsock.h"
19 #include "apr_network_io.h"
20 #include "apr_pools.h"
21 
main(int argc,char * argv[])22 int main(int argc, char *argv[])
23 {
24     apr_pool_t *p;
25     apr_socket_t *sock;
26     apr_status_t rv;
27     apr_sockaddr_t *remote_sa;
28 
29     apr_initialize();
30     atexit(apr_terminate);
31     apr_pool_create(&p, NULL);
32 
33     if (argc < 2) {
34         exit(-1);
35     }
36 
37     rv = apr_sockaddr_info_get(&remote_sa, "127.0.0.1", APR_UNSPEC, 8021, 0, p);
38     if (rv != APR_SUCCESS) {
39         exit(-1);
40     }
41 
42     if (apr_socket_create(&sock, remote_sa->family, SOCK_STREAM, 0,
43                 p) != APR_SUCCESS) {
44         exit(-1);
45     }
46 
47     rv = apr_socket_timeout_set(sock, apr_time_from_sec(3));
48     if (rv) {
49         exit(-1);
50     }
51 
52     apr_socket_connect(sock, remote_sa);
53 
54     if (!strcmp("read", argv[1])) {
55         char datarecv[STRLEN];
56         apr_size_t length = STRLEN;
57         apr_status_t rv;
58 
59         memset(datarecv, 0, STRLEN);
60         rv = apr_socket_recv(sock, datarecv, &length);
61         apr_socket_close(sock);
62         if (APR_STATUS_IS_TIMEUP(rv)) {
63             exit(SOCKET_TIMEOUT);
64         }
65 
66         if (strcmp(datarecv, DATASTR)) {
67             exit(-1);
68         }
69 
70         exit(length);
71     }
72     else if (!strcmp("write", argv[1])) {
73         apr_size_t length = strlen(DATASTR);
74         apr_socket_send(sock, DATASTR, &length);
75 
76         apr_socket_close(sock);
77         exit(length);
78     }
79     exit(-1);
80 }
81