2 * Copyright (c) 2006 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Simple netipx regression test that attempts to build an SPX stream socket
31 * pair, and send data twice over the stream, once in each direction.
32 * Purposefully pick a small packet length that should fit into the buffers
33 * at both ends, and therefore not result in a buffer deadlock.
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <netipx/ipx.h>
48 #define IPX_ENDPOINT "0xbebe.1.0x8a13"
52 #define SPX_SUPPORTS_SENDTO_WITH_CONNECT
56 packet_fill(u_char *packet)
60 for (i = 0; i < PACKETLEN; i++)
61 packet[i] = (i & 0xff);
65 packet_check(u_char *packet)
69 for (i = 0; i < PACKETLEN; i++) {
70 if (packet[i] != (i & 0xff))
76 #ifdef SPX_SUPPORTS_SENDTO_WITH_CONNECT
78 my_sendto(int sock, const char *who, pid_t pid, struct sockaddr *sa,
81 u_char packet[PACKETLEN];
86 len = sendto(sock, packet, sizeof(packet), 0, sa, sa_len);
89 (void)kill(pid, SIGTERM);
91 err(-1, "%s: sendto()", who);
93 if (len != sizeof(packet)) {
94 (void)kill(pid, SIGTERM);
95 errx(-1, "%s: sendto(): short send (%d length, %d sent)",
96 who, sizeof(packet), len);
102 my_send(int sock, const char *who, pid_t pid)
104 u_char packet[PACKETLEN];
109 len = send(sock, packet, sizeof(packet), 0);
112 (void)kill(pid, SIGTERM);
114 err(-1, "%s: send()", who);
116 if (len != sizeof(packet)) {
117 (void)kill(pid, SIGTERM);
118 errx(-1, "%s: send(): short send (%d length, %d sent)", who,
119 sizeof(packet), len);
124 my_recv(int sock, const char *who, pid_t pid)
126 u_char packet[PACKETLEN];
130 bzero(packet, sizeof(packet));
131 len = recv(sock, packet, sizeof(packet), 0);
134 (void)kill(pid, SIGTERM);
136 err(-1, "%s: recv()", who);
138 if (len != sizeof(packet)) {
139 (void)kill(pid, SIGTERM);
140 errx(-1, "%s: recv(): got %d expected %d", who, len,
143 if (packet_check(packet) < 0) {
144 (void)kill(pid, SIGTERM);
145 errx(-1, "%s: recv(): got bad data", who);
150 main(int argc, char *argv[])
152 int error, sock_listen, sock_recv, sock_send;
153 struct sockaddr_ipx sipx_listen, sipx_send;
154 pid_t childpid, parentpid;
157 * Socket to receive with.
159 sock_listen = socket(PF_IPX, SOCK_STREAM, 0);
161 err(-1, "sock_listen = socket(PF_IPX, SOCK_STREAM, 0)");
163 bzero(&sipx_listen, sizeof(sipx_listen));
164 sipx_listen.sipx_len = sizeof(sipx_listen);
165 sipx_listen.sipx_family = AF_IPX;
166 sipx_listen.sipx_addr = ipx_addr(IPX_ENDPOINT);
168 if (bind(sock_listen, (struct sockaddr *)&sipx_listen,
169 sizeof(sipx_listen)) < 0)
170 err(-1, "bind(sock_listen)");
172 if (listen(sock_listen, -1) < 0)
173 err(-1, "listen(sock_listen)");
175 parentpid = getpid();
183 * The child: accept connections and process data on them.
186 sock_recv = accept(sock_listen, NULL, NULL);
192 my_recv(sock_recv, "listener", parentpid);
193 my_send(sock_recv, "listener", parentpid);
199 * The parent: connect, send data, receive it back, and exit;
200 * build two connections, once using a full connect() API
201 * call, and the second using sendto().
205 * Socket to send with.
207 sock_send = socket(PF_IPX, SOCK_STREAM, 0);
210 (void)kill(childpid, SIGTERM);
212 err(-1, "sock_send = socket(PF_IPX, SOCK_STREAM, 0)");
215 bzero(&sipx_send, sizeof(sipx_send));
216 sipx_send.sipx_len = sizeof(sipx_send);
217 sipx_send.sipx_family = AF_IPX;
218 sipx_send.sipx_addr = ipx_addr(IPX_ENDPOINT);
220 if (connect(sock_send, (struct sockaddr *)&sipx_send,
221 sizeof(sipx_send)) < 0) {
223 (void)kill(childpid, SIGTERM);
225 err(-1, "sock_send = socket(PF_IPX, SOCK_STREAM, 0)");
228 my_send(sock_send, "connector", childpid);
229 my_recv(sock_send, "connector", childpid);
233 #ifdef SPX_SUPPORTS_SENDTO_WITH_CONNECT
234 sock_send = socket(PF_IPX, SOCK_STREAM, 0);
237 (void)kill(childpid, SIGTERM);
239 err(-1, "sock_send = socket(PF_IPX, SOCK_STREAM, 0)");
242 bzero(&sipx_send, sizeof(sipx_send));
243 sipx_send.sipx_len = sizeof(sipx_send);
244 sipx_send.sipx_family = AF_IPX;
245 sipx_send.sipx_addr = ipx_addr(IPX_ENDPOINT);
247 my_sendto(sock_send, "connector", childpid,
248 (struct sockaddr *)&sipx_send, sizeof(sipx_send));
249 my_recv(sock_send, "connector", childpid);
254 (void)kill(childpid, SIGTERM);