summaryrefslogtreecommitdiffstats
path: root/tools/regression/netipx/ipxdgramloopback/ipxdgramloopback.c
blob: 73b6a2a99a9b7deb725194908e8492347e8c9820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*-
 * Copyright (c) 2006 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/*
 * Simple netipx regression test that attempts to build an IPX datagram
 * socket pair and send a packet from one to the other.
 */

#include <sys/types.h>
#include <sys/socket.h>

#include <netipx/ipx.h>

#include <err.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#define	IPX_ENDPOINT	"0xbebe.1.0x8a13"
#define	PACKETLEN	128

int
main(int argc, char *argv[])
{
	struct sockaddr_ipx sipx_recv, sipx_send;
	u_char packet[PACKETLEN];
	int i, sock_recv, sock_send;
	ssize_t len;

	/*
	 * Socket to receive with.
	 */
	sock_recv = socket(PF_IPX, SOCK_DGRAM, 0);
	if (sock_recv < 0)
		err(-1, "sock_recv = socket(PF_IPX, SOCK_DGRAM, 0)");

	bzero(&sipx_recv, sizeof(sipx_recv));
	sipx_recv.sipx_len = sizeof(sipx_recv);
	sipx_recv.sipx_family = AF_IPX;
	sipx_recv.sipx_addr = ipx_addr(IPX_ENDPOINT);

	if (bind(sock_recv, (struct sockaddr *)&sipx_recv, sizeof(sipx_recv))
	    < 0)
		err(-1, "bind(sock_recv)");

	/*
	 * Set non-blocking to try to avoid blocking indefinitely if the
	 * packet doesn't end up in the right place.
	 */
	if (fcntl(sock_recv, F_SETFL, O_NONBLOCK) < 0)
		err(-1, "fcntl(O_NONBLOCK, sock_recv)");

	/*
	 * Socket to send with.
	 */
	sock_send = socket(PF_IPX, SOCK_DGRAM, 0);
	if (sock_send < 0)
		err(-1, "sock_send = socket(PF_IPX, SOCK_DGRAM, 0)");

	bzero(&sipx_send, sizeof(sipx_send));
	sipx_send.sipx_len = sizeof(sipx_send);
	sipx_send.sipx_family = AF_IPX;
	sipx_send.sipx_addr = ipx_addr(IPX_ENDPOINT);

	for (i = 0; i < PACKETLEN; i++)
		packet[i] = (i & 0xff);

	len = sendto(sock_send, packet, sizeof(packet), 0,
	    (struct sockaddr *)&sipx_send, sizeof(sipx_send));
	if (len < 0)
		err(-1, "sendto()");
	if (len != sizeof(packet))
		errx(-1, "sendto(): short send (%zu length, %zd sent)",
		    sizeof(packet), len);

	sleep(1);	/* Arbitrary non-zero amount. */

	bzero(packet, sizeof(packet));
	len = recv(sock_recv, packet, sizeof(packet), 0);
	if (len < 0)
		err(-1, "recv()");
	if (len != sizeof(packet))
		errx(-1, "recv(): short receive (%zu length, %zd received)",
		    sizeof(packet), len);

	for (i = 0; i < PACKETLEN; i++) {
		if (packet[i] != (i & 0xff))
			errx(-1, "recv(): byte %d wrong (%d instead of %d)",
			    i, packet[i], i & 0xff);
	}

	return (0);
}
OpenPOWER on IntegriCloud