]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/netbsd-tests/net/net/t_tcp.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / netbsd-tests / net / net / t_tcp.c
1 /*      $NetBSD: t_tcp.c,v 1.3 2013/10/17 12:53:28 christos Exp $       */
2
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by the NetBSD
18  *        Foundation, Inc. and its contributors.
19  * 4. Neither the name of The NetBSD Foundation nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include <sys/cdefs.h>
37 #ifdef __RCSID
38 __RCSID("$Id: t_tcp.c,v 1.3 2013/10/17 12:53:28 christos Exp $");
39 #endif
40
41 /* Example code. Should block; does with accept not paccept. */
42 /* Original by: Justin Cormack <justin@specialbusrvrervice.com> */
43
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <stdio.h>
49 #include <stdbool.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <errno.h>
53 #include <err.h>
54 #include <stdlib.h>
55 #include <signal.h>
56
57 #ifdef TEST
58 #define FAIL(msg, ...)  err(EXIT_FAILURE, msg, ## __VA_ARGS__)
59 #else 
60 #include <atf-c.h> 
61 #define FAIL(msg, ...)  ATF_CHECK_MSG(0, msg, ## __VA_ARGS__); goto fail
62 #endif
63
64 static void
65 ding(int al)
66 {
67 }
68
69 static void 
70 paccept_block(bool pacceptblock, bool fcntlblock)
71 {
72         int srvr = -1, clnt = -1, as = -1;
73         int ok, fl, n;
74         char buf[10];
75         struct sockaddr_in sin, ba;
76         struct sigaction sa;
77
78         srvr = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
79         if (srvr == -1)
80                 FAIL("socket");
81
82         memset(&sin, 0, sizeof(sin));
83         sin.sin_family = AF_INET;
84 #ifdef BSD4_4
85         sin.sin_len = sizeof(sin);
86 #endif
87         sin.sin_port = htons(0);
88         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
89         ok = bind(srvr, (const struct sockaddr *)&sin, (socklen_t)sizeof(sin));
90         if (ok == -1)
91                 FAIL("bind");
92
93         socklen_t addrlen = sizeof(struct sockaddr_in);
94         ok = getsockname(srvr, (struct sockaddr *)&ba, &addrlen);
95         if (ok == -1)
96                 FAIL("getsockname");
97
98         ok = listen(srvr, SOMAXCONN);
99         if (ok == -1)
100                 FAIL("listen");
101
102         clnt = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
103         if (clnt == -1)
104                 FAIL("socket");
105
106         /* may not connect first time */
107         ok = connect(clnt, (struct sockaddr *) &ba, addrlen);
108         as = paccept(srvr, NULL, NULL, NULL, pacceptblock ? 0 : SOCK_NONBLOCK);
109         ok = connect(clnt, (struct sockaddr *) &ba, addrlen);
110         if (ok == -1 && errno != EISCONN)
111                 FAIL("both connects failed");
112
113 #if 0
114         fl = fcntl(srvr, F_GETFL, 0);
115         if (fl == -1)
116                 FAIL("fnctl getfl");
117
118         ok = fcntl(srvr, F_SETFL, fl & ~O_NONBLOCK);
119         if (ok == -1)
120                 FAIL("fnctl setfl");
121 #endif
122
123         if (as == -1) {         /* not true under NetBSD */
124                 as = paccept(srvr, NULL, NULL, NULL, pacceptblock ? 0 : SOCK_NONBLOCK);
125                 if (as == -1)
126                         FAIL("paccept");
127         }
128         if (fcntlblock) {
129                 fl = fcntl(as, F_GETFL, 0);
130                 if (fl == -1)
131                         FAIL("fnctl");
132                 if (fl != (O_RDWR|O_NONBLOCK))
133                         FAIL("fl 0x%x != 0x%x\n", fl, O_RDWR|O_NONBLOCK);
134                 ok = fcntl(as, F_SETFL, fl & ~O_NONBLOCK);
135                 if (ok == -1)
136                         FAIL("fnctl setfl");
137
138                 fl = fcntl(as, F_GETFL, 0);
139                 if (fl & O_NONBLOCK)
140                         FAIL("fl non blocking after reset");
141         }
142         sa.sa_handler = ding;
143         sa.sa_flags = 0;
144         sigemptyset(&sa.sa_mask);
145         sigaction(SIGALRM, &sa, NULL);
146         alarm(1);
147         n = read(as, buf, 10);
148
149         if (pacceptblock || fcntlblock) {
150                 if (n == -1 && errno != EINTR)
151                         FAIL("read");
152         } else {
153                 if (n != -1 || errno != EWOULDBLOCK)
154                         FAIL("read");
155         }
156         return;
157 fail:
158         close(srvr);
159         close(clnt);
160         close(as);
161 }
162
163 #ifndef TEST
164
165 ATF_TC(paccept_reset_nonblock);
166 ATF_TC_HEAD(paccept_reset_nonblock, tc)
167 {
168
169         atf_tc_set_md_var(tc, "descr", "Check that paccept(2) resets "
170             "the non-blocking flag on non-blocking sockets");
171 }
172
173 ATF_TC_BODY(paccept_reset_nonblock, tc)
174 {
175         paccept_block(true, false);
176 }
177
178 ATF_TC(fcntl_reset_nonblock);
179 ATF_TC_HEAD(fcntl_reset_nonblock, tc)
180 {
181
182         atf_tc_set_md_var(tc, "descr", "Check that fcntl(2) resets "
183             "the non-blocking flag on non-blocking sockets");
184 }
185
186 ATF_TC_BODY(fcntl_reset_nonblock, tc)
187 {
188         paccept_block(false, true);
189 }
190
191 ATF_TC(paccept_nonblock);
192 ATF_TC_HEAD(paccept_nonblock, tc)
193 {
194
195         atf_tc_set_md_var(tc, "descr", "Check that fcntl(2) resets "
196             "the non-blocking flag on non-blocking sockets");
197 }
198
199 ATF_TC_BODY(paccept_nonblock, tc)
200 {
201         paccept_block(false, false);
202 }
203
204 ATF_TP_ADD_TCS(tp)
205 {
206
207         ATF_TP_ADD_TC(tp, paccept_reset_nonblock);
208         ATF_TP_ADD_TC(tp, fcntl_reset_nonblock);
209         ATF_TP_ADD_TC(tp, paccept_nonblock);
210         return atf_no_error();
211 }
212 #else
213 int
214 main(int argc, char *argv[])
215 {
216         paccept_block(false);
217         paccept_block(true);
218         return 0;
219 }
220 #endif