simple_send.c revision 156815
178344Sobrien/*- 278344Sobrien * Copyright (c) 2004 Robert N. M. Watson 378344Sobrien * All rights reserved. 478344Sobrien * 578344Sobrien * Redistribution and use in source and binary forms, with or without 678344Sobrien * modification, are permitted provided that the following conditions 778344Sobrien * are met: 878344Sobrien * 1. Redistributions of source code must retain the above copyright 978344Sobrien * notice, this list of conditions and the following disclaimer. 1078344Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1178344Sobrien * notice, this list of conditions and the following disclaimer in the 1278344Sobrien * documentation and/or other materials provided with the distribution. 1378344Sobrien * 1478344Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1578344Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1678344Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1778344Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1878344Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1978344Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2078344Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2178344Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2278344Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2378344Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/tools/regression/netatalk/simple_send/simple_send.c 156815 2006-03-17 20:14:25Z rwatson $ 27 */ 28 29#include <sys/types.h> 30#include <sys/socket.h> 31 32#include <arpa/inet.h> 33 34#include <netatalk/at.h> 35 36#include <err.h> 37#include <errno.h> 38#include <stdio.h> 39#include <stdlib.h> 40#include <string.h> 41#include <unistd.h> 42 43/* 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 47 * case. 48 * 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 52 * port number. 53 * 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). 59 */ 60 61/* 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. 65 */ 66static int 67socket_between(struct sockaddr_at *from, struct sockaddr_at *to) 68{ 69 int s; 70 71 s = socket(PF_APPLETALK, SOCK_DGRAM, ATPROTO_DDP); 72 if (s == -1) 73 errx(1, "socket: %s\n", strerror(errno)); 74 75 if (from != NULL) { 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, 79 strerror(errno)); 80 } 81 82 if (to != NULL) { 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, 86 strerror(errno)); 87 } 88 return (s); 89} 90 91int 92main(int argc, char *argv[]) 93{ 94 struct sockaddr_at sat_from, sat_to; 95 char *addr_from, *addr_to; 96 u_int net, node, port; 97 char msg[] = "TEST"; 98 ssize_t len; 99 int s; 100 101 if (argc != 3) 102 errx(1, "simple_send from_addr to_addr"); 103 104 addr_from = argv[1]; 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; 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_node, 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