2 * Copyright (c) 2004 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
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <arpa/inet.h>
34 #include <netatalk/at.h>
44 * This is a simple test tool to bind netatalk SOCK_DGRAM sockets and perform
45 * simple send operations that exercise each combination of bound and
46 * connected endpoints, with the intent of exercising the various kernel send
49 * In order to run this test, configure NETATALK into the kernel. Use
50 * ifconfig to set an appletalk address on an interface. Run this tool with
51 * two arguments: a local address and port number, and a remote address and
54 * It is recommended that you try running it with some interesting address
55 * and port thresholds, including ATADDR_ANYNET, ATADDR_ANYNODE,
56 * ATADDR_ANYPORT, and ATADDR_ANYBCAST. Try both remote unicast addresses
57 * and the local address, which will help to test local delivery (although
58 * not socket receive).
62 * Create a netatalk socket with specified source and destination, if
63 * desired. If a source is specified, bind it. If a destination is
64 * specified, connect it.
67 socket_between(struct sockaddr_at *from, struct sockaddr_at *to)
71 s = socket(PF_APPLETALK, SOCK_DGRAM, ATPROTO_DDP);
73 errx(1, "socket: %s\n", strerror(errno));
76 if (bind(s, (struct sockaddr *)from, sizeof(*from)) != 0)
77 errx(1, "bind: %u.%u returned %s\n",
78 ntohs(from->sat_addr.s_net), from->sat_addr.s_node,
83 if (connect(s, (struct sockaddr *)to, sizeof(*to)) != 0)
84 errx(1, "connect: %u.%u returned %s\n",
85 ntohs(to->sat_addr.s_net), to->sat_addr.s_node,
92 main(int argc, char *argv[])
94 struct sockaddr_at sat_from, sat_to;
95 char *addr_from, *addr_to;
96 u_int net, node, port;
102 errx(1, "simple_send from_addr to_addr");
105 sat_from.sat_family = AF_APPLETALK;
106 sat_from.sat_len = sizeof(sat_from);
107 if (sscanf(addr_from, "%u.%u:%u", &net, &node, &port) != 3 ||
108 net > 0xfff || node > 0xfe)
109 errx(1, "%s: illegal address", addr_from);
110 sat_from.sat_addr.s_net = htons(net);
111 sat_from.sat_addr.s_node = node;
112 sat_from.sat_port = port;
115 sat_to.sat_family = AF_APPLETALK;
116 sat_to.sat_len = sizeof(sat_to);
117 if (sscanf(addr_to, "%u.%u:%u", &net, &node, &port) != 3 ||
118 net > 0xffff || node > 0xfe)
119 errx(1, "%s: illegal address", addr_to);
120 sat_to.sat_addr.s_net = htons(net);
121 sat_to.sat_addr.s_node = node;
122 sat_from.sat_port = port;
124 printf("Address source is %u.%u:%u, address destination is %u.%u:%u\n",
125 ntohs(sat_from.sat_addr.s_net), sat_from.sat_addr.s_node,
127 ntohs(sat_to.sat_addr.s_net), sat_to.sat_addr.s_node,
131 * First, create a socket and use explicit sendto() to specify
134 s = socket_between(NULL, NULL);
135 len = sendto(s, msg, sizeof(msg), 0, (struct sockaddr *)&sat_to,
140 * Next, specify the destination for a connect() but not the source.
142 s = socket_between(NULL, &sat_to);
143 len = send(s, msg, sizeof(msg), 0);
147 * Now, bind the source, but not connect the destination.
149 s = socket_between(&sat_from, NULL);
150 len = sendto(s, msg, sizeof(msg), 0, (struct sockaddr *)&sat_to,
155 * Finally, bind and connect.
157 s = socket_between(&sat_from, &sat_to);
158 len = send(s, msg, sizeof(msg), 0);