simple_send.c revision 132304
12786Ssos/*-
22786Ssos * Copyright (c) 2004 Robert N. M. Watson
32786Ssos * All rights reserved.
42786Ssos *
52786Ssos * Redistribution and use in source and binary forms, with or without
62786Ssos * modification, are permitted provided that the following conditions
72786Ssos * are met:
82786Ssos * 1. Redistributions of source code must retain the above copyright
92786Ssos *    notice, this list of conditions and the following disclaimer.
102786Ssos * 2. Redistributions in binary form must reproduce the above copyright
112786Ssos *    notice, this list of conditions and the following disclaimer in the
122786Ssos *    documentation and/or other materials provided with the distribution.
132786Ssos *
142786Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152786Ssos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162786Ssos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172786Ssos * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182786Ssos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192786Ssos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202786Ssos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212786Ssos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222786Ssos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232786Ssos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242786Ssos * SUCH DAMAGE.
252786Ssos *
262786Ssos * $FreeBSD: head/tools/regression/netatalk/simple_send/simple_send.c 132304 2004-07-17 17:16:16Z rwatson $
272786Ssos */
282786Ssos
292786Ssos#include <sys/types.h>
302786Ssos#include <sys/socket.h>
312786Ssos
322786Ssos#include <arpa/inet.h>
332786Ssos
342786Ssos#include <netatalk/at.h>
352786Ssos
362786Ssos#include <err.h>
372786Ssos#include <errno.h>
382786Ssos#include <stdio.h>
392786Ssos#include <stdlib.h>
402786Ssos#include <string.h>
412786Ssos#include <unistd.h>
422786Ssos
432786Ssos/*
442786Ssos * This is a simple test tool to bind netatalk SOCK_DGRAM sockets and perform
452786Ssos * simple send operations that exercise each combination of bound and
462786Ssos * connected endpoints, with the intent of exercising the various kernel send
472786Ssos * case.
482786Ssos *
492786Ssos * In order to run this test, configure NETATALK into the kernel.  Use
502786Ssos * ifconfig to set an appletalk address on an interface.  Run this tool with
512786Ssos * two arguments: a local address and port number, and a remote address and
522786Ssos * port number.
532786Ssos *
542786Ssos * It is recommended that you try running it with some interesting address
552786Ssos * and port thresholds, including ATADDR_ANYNET, ATADDR_ANYNODE,
562786Ssos * ATADDR_ANYPORT, and ATADDR_ANYBCAST.  Try both remote unicast addresses
572786Ssos * and the local address, which will help to test local delivery (although
582786Ssos * not socket receive).
592786Ssos */
602786Ssos
612786Ssos/*
622786Ssos * Create a netatalk socket with specified source and destination, if
632786Ssos * desired.  If a source is specified, bind it.  If a destination is
642786Ssos * specified, connect it.
652786Ssos */
662786Ssosstatic int
672786Ssossocket_between(struct sockaddr_at *from, struct sockaddr_at *to)
682786Ssos{
692786Ssos	int s;
702786Ssos
712786Ssos	s = socket(PF_APPLETALK, SOCK_DGRAM, ATPROTO_DDP);
722786Ssos	if (s == -1)
732786Ssos		errx(1, "socket: %s\n", strerror(errno));
742786Ssos
752786Ssos	if (from != NULL) {
762786Ssos		if (bind(s, (struct sockaddr *)from, sizeof(*from)) != 0)
772786Ssos			errx(1, "bind: %u.%u returned %s\n",
782786Ssos			    ntohs(from->sat_addr.s_net), from->sat_addr.s_node,
792786Ssos			    strerror(errno));
802786Ssos	}
815994Ssos
822786Ssos	if (to != NULL) {
832786Ssos		if (connect(s, (struct sockaddr *)to, sizeof(*to)) != 0)
842786Ssos			errx(1, "connect: %u.%u returned %s\n",
852786Ssos			    ntohs(to->sat_addr.s_net), to->sat_addr.s_node,
862786Ssos			    strerror(errno));
872786Ssos	}
885994Ssos	return (s);
892786Ssos}
902786Ssos
912786Ssosint
922786Ssosmain(int argc, char *argv[])
932786Ssos{
942786Ssos	struct sockaddr_at sat_from, sat_to;
952786Ssos	char *addr_from, *addr_to;
962786Ssos	u_int net, node, port;
972786Ssos	char msg[] = "TEST";
982786Ssos	ssize_t len;
992786Ssos	int s;
1002786Ssos
1012786Ssos	if (argc != 3)
1022786Ssos		errx(1, "simple_send from_addr to_addr");
1032786Ssos
1042786Ssos	addr_from = argv[1];
1052786Ssos	sat_from.sat_family = AF_APPLETALK;
1062786Ssos	sat_from.sat_len = sizeof(sat_from);
1072786Ssos	if (sscanf(addr_from, "%u.%u:%u", &net, &node, &port) != 3 ||
1085994Ssos	    net > 0xfff || node > 0xfe)
1092786Ssos		errx(1, "%s: illegal address", addr_from);
1105994Ssos	sat_from.sat_addr.s_net = htons(net);
1115994Ssos	sat_from.sat_addr.s_node = node;
1125994Ssos	sat_from.sat_port = port;
113
114	addr_to = argv[2];
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;
123
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,
126	    sat_from.sat_port,
127	    ntohs(sat_to.sat_addr.s_net), sat_to.sat_addr.s_net,
128	    sat_to.sat_port);
129
130	/*
131	 * First, create a socket and use explicit sendto() to specify
132	 * destination.
133	 */
134	s = socket_between(NULL, NULL);
135	len = sendto(s, msg, sizeof(msg), 0, (struct sockaddr *)&sat_to,
136	    sizeof(sat_to));
137	close(s);
138
139	/*
140	 * Next, specify the destination for a connect() but not the source.
141	 */
142	s = socket_between(NULL, &sat_to);
143	len = send(s, msg, sizeof(msg), 0);
144	close(s);
145
146	/*
147	 * Now, bind the source, but not connect the destination.
148	 */
149	s = socket_between(&sat_from, NULL);
150	len = sendto(s, msg, sizeof(msg), 0, (struct sockaddr *)&sat_to,
151	    sizeof(sat_to));
152	close(s);
153
154	/*
155	 * Finally, bind and connect.
156	 */
157	s = socket_between(&sat_from, &sat_to);
158	len = send(s, msg, sizeof(msg), 0);
159	close(s);
160
161	exit(0);
162}
163