2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the name of The University
10 * of Michigan not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior
12 * permission. This software is supplied as is without expressed or
13 * implied warranties of any kind.
15 * Research Systems Unix Group
16 * The University of Michigan
18 * 535 W. William Street
21 * netatalk@itd.umich.edu
25 * AppleTalk Echo Protocol Client
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
43 #include <netatalk/endian.h>
44 #include <netatalk/at.h>
45 #include <atalk/compat.h>
46 #include <atalk/aep.h>
47 #include <atalk/nbp.h>
48 #include <atalk/netddp.h>
49 #include <atalk/ddp.h>
51 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature */
53 #define SOCKLEN_T unsigned int
56 struct sockaddr_at target;
57 int s, nsent = 0, nrecv = 0;
58 time_t totalms = 0, minms = -1, maxms = -1;
59 static unsigned int pings = 0;
61 #if !defined( ibm032 ) && !defined( _IBMR2 )
67 printf( "\n----%d.%d AEP Statistics----\n",
68 ntohs( target.sat_addr.s_net ), target.sat_addr.s_node );
69 printf( "%d packets sent, %d packets received, %d%% packet loss\n",
70 nsent, nrecv, (( nsent - nrecv ) * 100 ) / nsent );
72 printf( "round-trip (ms) min/avg/max = %d/%d/%d\n",
73 minms, totalms / nrecv, maxms );
79 #if !defined( ibm032 ) && !defined( _IBMR2 )
86 static unsigned int seq = 0;
91 bcopy( &seq, p, sizeof( unsigned int ));
92 p += sizeof( unsigned int );
95 if ( gettimeofday( &tv, (struct timezone *)0 ) < 0 ) {
96 perror( "gettimeofday" );
99 bcopy( &tv, p, sizeof( struct timeval ));
100 p += sizeof( struct timeval );
102 if ( netddp_sendto( s, buf, p - buf, 0, (struct sockaddr *) &target,
103 sizeof( struct sockaddr_at )) < 0 ) {
108 if (pings && nsent > pings) done();
118 struct sockaddr_at sat, saddr;
119 struct timeval tv, atv;
121 char *obj = NULL, *type = "Workstation", *zone = "*";
126 char buf[ 1024 ], *p;
132 memset(&saddr, 0, sizeof(saddr));
133 while (( cc = getopt( ac, av, "c:A:" )) != EOF ) {
136 if (!atalk_aton(optarg, &saddr.sat_addr)) {
137 fprintf(stderr, "Bad address.\n");
141 pings = atoi( optarg );
149 if ( ac - optind != 1 ) {
155 * Save the port, since nbp_lookup calls getservbyname() to get the
158 if (( se = getservbyname( "echo", "ddp" )) == NULL )
161 port = ntohs( se->s_port );
163 memset( &target, 0, sizeof( struct sockaddr_at ));
165 target.sat_len = sizeof( struct sockaddr_at );
167 target.sat_family = AF_APPLETALK;
168 if ( !atalk_aton( av[ optind ], &target.sat_addr )) {
169 if ( nbp_name( av[ optind ], &obj, &type, &zone ) || !obj ) {
170 fprintf( stderr, "Bad name: %s\n", av[ optind ] );
173 if ( nbp_lookup( obj, type, zone, &nn, 1, &saddr.sat_addr) <= 0 ) {
174 fprintf( stderr, "Can't find: %s\n", av[ optind ] );
177 memcpy( &target, &nn.nn_sat, sizeof( struct sockaddr_at ));
179 target.sat_port = port;
181 if ((s = netddp_open(saddr.sat_addr.s_net || saddr.sat_addr.s_node ?
182 &saddr : NULL, NULL)) < 0) {
187 sv.sa_handler = aep_send;
188 sigemptyset( &sv.sa_mask );
189 sigaddset( &sv.sa_mask, SIGINT );
190 sv.sa_flags = SA_RESTART;
191 if ( sigaction( SIGALRM, &sv, (struct sigaction *)0 ) < 0 ) {
192 perror( "sigaction" );
196 sv.sa_handler = done;
197 sigemptyset( &sv.sa_mask );
198 sigaddset( &sv.sa_mask, SIGALRM );
199 sv.sa_flags = SA_RESTART;
200 if ( sigaction( SIGINT, &sv, (struct sigaction *)0 ) < 0 ) {
201 perror( "sigaction" );
205 it.it_interval.tv_sec = 1L;
206 it.it_interval.tv_usec = 0L;
207 it.it_value.tv_sec = 1L;
208 it.it_value.tv_usec = 0L;
210 if ( setitimer( ITIMER_REAL, &it, (struct itimerval *)0 ) < 0 ) {
211 perror( "setitimer" );
216 satlen = sizeof( struct sockaddr_at );
217 if (( cc = netddp_recvfrom( s, buf, sizeof( buf ), 0,
218 (struct sockaddr *) &sat,
220 if ( errno == EINTR ) {
224 perror( "recvfrom" );
229 if ( *p++ != DDPTYPE_AEP || *p++ != AEPOP_REPLY ) {
230 fprintf( stderr, "%s: bad packet!\n", av[ 0 ] );
233 if ( gettimeofday( &tv, (struct timezone *)0 ) < 0 ) {
234 perror( "gettimeofday" );
237 bcopy( p, &seq, sizeof( unsigned int ));
238 p += sizeof( unsigned int );
239 bcopy( p, &atv, sizeof( struct timeval ));
241 ms = ( tv.tv_sec - atv.tv_sec ) * 1000 +
242 ( tv.tv_usec - atv.tv_usec ) / 1000;
247 if ( ms < minms || minms == -1 ) {
250 printf( "%d bytes from %u.%u: aep_seq=%d. time=%d. ms\n",
251 cc, ntohs( sat.sat_addr.s_net ), sat.sat_addr.s_node,
253 if (pings && seq + 1 >= pings) done();
259 fprintf( stderr, "usage:\t%s [-A source address ] [-c count] ( addr | nbpname )\n", av0 );