xref: /trueos/tools/regression/netipx/ipxdgramloopback/ipxdgramloopback.c (revision b972b67ed72b5687a023c92602aaef64163b2f59)
1 /*-
2  * Copyright (c) 2006 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * Simple netipx regression test that attempts to build an IPX datagram
31  * socket pair and send a packet from one to the other.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 
37 #include <netipx/ipx.h>
38 
39 #include <err.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #define	IPX_ENDPOINT	"0xbebe.1.0x8a13"
45 #define	PACKETLEN	128
46 
47 int
main(int argc,char * argv[])48 main(int argc, char *argv[])
49 {
50 	struct sockaddr_ipx sipx_recv, sipx_send;
51 	u_char packet[PACKETLEN];
52 	int i, sock_recv, sock_send;
53 	ssize_t len;
54 
55 	/*
56 	 * Socket to receive with.
57 	 */
58 	sock_recv = socket(PF_IPX, SOCK_DGRAM, 0);
59 	if (sock_recv < 0)
60 		err(-1, "sock_recv = socket(PF_IPX, SOCK_DGRAM, 0)");
61 
62 	bzero(&sipx_recv, sizeof(sipx_recv));
63 	sipx_recv.sipx_len = sizeof(sipx_recv);
64 	sipx_recv.sipx_family = AF_IPX;
65 	sipx_recv.sipx_addr = ipx_addr(IPX_ENDPOINT);
66 
67 	if (bind(sock_recv, (struct sockaddr *)&sipx_recv, sizeof(sipx_recv))
68 	    < 0)
69 		err(-1, "bind(sock_recv)");
70 
71 	/*
72 	 * Set non-blocking to try to avoid blocking indefinitely if the
73 	 * packet doesn't end up in the right place.
74 	 */
75 	if (fcntl(sock_recv, F_SETFL, O_NONBLOCK) < 0)
76 		err(-1, "fcntl(O_NONBLOCK, sock_recv)");
77 
78 	/*
79 	 * Socket to send with.
80 	 */
81 	sock_send = socket(PF_IPX, SOCK_DGRAM, 0);
82 	if (sock_send < 0)
83 		err(-1, "sock_send = socket(PF_IPX, SOCK_DGRAM, 0)");
84 
85 	bzero(&sipx_send, sizeof(sipx_send));
86 	sipx_send.sipx_len = sizeof(sipx_send);
87 	sipx_send.sipx_family = AF_IPX;
88 	sipx_send.sipx_addr = ipx_addr(IPX_ENDPOINT);
89 
90 	for (i = 0; i < PACKETLEN; i++)
91 		packet[i] = (i & 0xff);
92 
93 	len = sendto(sock_send, packet, sizeof(packet), 0,
94 	    (struct sockaddr *)&sipx_send, sizeof(sipx_send));
95 	if (len < 0)
96 		err(-1, "sendto()");
97 	if (len != sizeof(packet))
98 		errx(-1, "sendto(): short send (%zu length, %zd sent)",
99 		    sizeof(packet), len);
100 
101 	sleep(1);	/* Arbitrary non-zero amount. */
102 
103 	bzero(packet, sizeof(packet));
104 	len = recv(sock_recv, packet, sizeof(packet), 0);
105 	if (len < 0)
106 		err(-1, "recv()");
107 	if (len != sizeof(packet))
108 		errx(-1, "recv(): short receive (%zu length, %zd received)",
109 		    sizeof(packet), len);
110 
111 	for (i = 0; i < PACKETLEN; i++) {
112 		if (packet[i] != (i & 0xff))
113 			errx(-1, "recv(): byte %d wrong (%d instead of %d)",
114 			    i, packet[i], i & 0xff);
115 	}
116 
117 	return (0);
118 }
119