Apache HTTPD
testsockets.c
Go to the documentation of this file.
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 "apr_network_io.h"
18#include "apr_errno.h"
19#include "apr_general.h"
20#include "apr_lib.h"
21#include "testutil.h"
22
23#define STRLEN 21
24
25static void tcp_socket(abts_case *tc, void *data)
26{
27 apr_status_t rv;
29 int type;
30
34
38
40}
41
42static void udp_socket(abts_case *tc, void *data)
43{
44 apr_status_t rv;
46 int type;
47
51
55
57}
58
59#if APR_HAVE_IPV6
60static void tcp6_socket(abts_case *tc, void *data)
61{
62 apr_status_t rv;
64
67 ABTS_NOT_IMPL(tc, "IPv6 not enabled");
68 return;
69 }
73}
74
75static void udp6_socket(abts_case *tc, void *data)
76{
77 apr_status_t rv;
79
82 ABTS_NOT_IMPL(tc, "IPv6 not enabled");
83 return;
84 }
88}
89#endif
90
91static void sendto_receivefrom_helper(abts_case *tc, const char *addr,
92 int family)
93{
94 apr_status_t rv;
97 char sendbuf[STRLEN] = "APR_INET, SOCK_DGRAM";
98 char recvbuf[80];
99 char *ip_addr;
101 apr_sockaddr_t *from;
103 apr_size_t len = 30;
104
106#if APR_HAVE_IPV6
108 ABTS_NOT_IMPL(tc, "IPv6 not enabled");
109 return;
110 }
111#endif
113 if (rv != APR_SUCCESS)
114 return;
117 if (rv != APR_SUCCESS)
118 return;
119
120 rv = apr_sockaddr_info_get(&to, addr, family, 7772, 0, p);
122 rv = apr_sockaddr_info_get(&from, addr, family, 7771, 0, p);
124
126 APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket", rv);
128 APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket2", rv);
129
130 rv = apr_socket_bind(sock, to);
131 APR_ASSERT_SUCCESS(tc, "Could not bind socket", rv);
132 if (rv != APR_SUCCESS)
133 return;
134 rv = apr_mcast_hops(sock, 10);
135 APR_ASSERT_SUCCESS(tc, "Could not set multicast hops", rv);
136 if (rv != APR_SUCCESS)
137 return;
138
139 rv = apr_socket_bind(sock2, from);
140 APR_ASSERT_SUCCESS(tc, "Could not bind second socket", rv);
141 if (rv != APR_SUCCESS)
142 return;
143
144 len = STRLEN;
145 rv = apr_socket_sendto(sock2, to, 0, sendbuf, &len);
148
149 /* fill the "from" sockaddr with a random address from another
150 * family to ensure that recvfrom sets it up properly. */
151#if APR_HAVE_IPV6
152 if (family == APR_INET)
153 rv = apr_sockaddr_info_get(&from, "3ffE:816e:abcd:1234::1",
154 APR_INET6, 4242, 0, p);
155 else
156#endif
157 rv = apr_sockaddr_info_get(&from, "127.1.2.3", APR_INET, 4242, 0, p);
159
160 len = 80;
161 rv = apr_socket_recvfrom(from, sock, 0, recvbuf, &len);
164 ABTS_STR_EQUAL(tc, "APR_INET, SOCK_DGRAM", recvbuf);
165
167 fromport = from->port;
169 ABTS_INT_EQUAL(tc, 7771, fromport);
170
173}
174
175static void sendto_receivefrom(abts_case *tc, void *data)
176{
177 int failed;
178 sendto_receivefrom_helper(tc, "127.0.0.1", APR_INET);
179 failed = tc->failed; tc->failed = 0;
180 ABTS_TRUE(tc, !failed);
181}
182
183#if APR_HAVE_IPV6
184static void sendto_receivefrom6(abts_case *tc, void *data)
185{
186 int failed;
188 failed = tc->failed; tc->failed = 0;
189 ABTS_TRUE(tc, !failed);
190}
191#endif
192
193static void socket_userdata(abts_case *tc, void *data)
194{
196 apr_status_t rv;
197 void *user;
198 const char *key = "GENERICKEY";
199
204
205 rv = apr_socket_data_set(sock1, "SOCK1", key, NULL);
207 rv = apr_socket_data_set(sock2, "SOCK2", key, NULL);
209
210 rv = apr_socket_data_get(&user, key, sock1);
212 ABTS_STR_EQUAL(tc, "SOCK1", user);
213 rv = apr_socket_data_get(&user, key, sock2);
215 ABTS_STR_EQUAL(tc, "SOCK2", user);
216}
217
219{
220 suite = ADD_SUITE(suite)
221
224
226
227#if APR_HAVE_IPV6
230
232#endif
233
235
236 return suite;
237}
238
const char apr_size_t len
Definition ap_regex.h:187
void abts_run_test(abts_suite *ts, test_func f, void *value)
Definition abts.c:175
#define ABTS_SIZE_EQUAL(a, b, c)
Definition abts.h:121
#define ABTS_TRUE(a, b)
Definition abts.h:127
#define ADD_SUITE(suite)
Definition abts.h:67
#define ABTS_PTR_NOTNULL(a, b)
Definition abts.h:125
#define ABTS_NOT_IMPL(a, b)
Definition abts.h:129
#define ABTS_STR_EQUAL(a, b, c)
Definition abts.h:123
#define ABTS_INT_EQUAL(a, b, c)
Definition abts.h:109
#define APR_ASSERT_SUCCESS(tc, ctxt, rv)
Definition testutil.h:58
APR Error Codes.
APR Miscellaneous library routines.
APR general purpose library routines.
APR Network library.
#define APR_STATUS_IS_EAFNOSUPPORT(s)
Definition apr_errno.h:1326
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char * key
void * data
int type
apr_sockaddr_t * addr
int family
apr_socket_t * sock
apr_uint16_t apr_port_t
#define APR_INET
int to
#define APR_SO_REUSEADDR
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
int failed
Definition abts.h:59
#define STRLEN
Definition testsockets.c:23
abts_suite * testsockets(abts_suite *suite)
static void sendto_receivefrom(abts_case *tc, void *data)
static void socket_userdata(abts_case *tc, void *data)
static void sendto_receivefrom_helper(abts_case *tc, const char *addr, int family)
Definition testsockets.c:91
static void tcp_socket(abts_case *tc, void *data)
Definition testsockets.c:25
static void udp_socket(abts_case *tc, void *data)
Definition testsockets.c:42
apr_status_t apr_socket_sendto(apr_socket_t *sock, apr_sockaddr_t *where, apr_int32_t flags, const char *buf, apr_size_t *len)
Definition sendrecv.c:112
apr_status_t apr_socket_recvfrom(apr_sockaddr_t *from, apr_socket_t *sock, apr_int32_t flags, char *buf, apr_size_t *len)
Definition sendrecv.c:146
apr_status_t apr_socket_bind(apr_socket_t *sock, apr_sockaddr_t *sa)
Definition sockets.c:216
apr_status_t apr_socket_close(apr_socket_t *thesocket)
Definition sockets.c:211
apr_status_t apr_socket_type_get(apr_socket_t *sock, int *type)
Definition sockets.c:466
apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type, int protocol, apr_pool_t *cont)
Definition sockets.c:116
apr_status_t apr_socket_data_get(void **data, const char *key, apr_socket_t *sock)
Definition sockets.c:472
apr_status_t apr_socket_data_set(apr_socket_t *sock, void *data, const char *key, apr_status_t(*cleanup)(void *))
Definition sockets.c:489
apr_status_t apr_socket_opt_set(apr_socket_t *sock, apr_int32_t opt, apr_int32_t on)
Definition sockopt.c:113