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
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/socket.h>
39 #include <netatalk/endian.h>
40 #include <netatalk/at.h>
41 #include <atalk/compat.h>
42 #include <atalk/aep.h>
43 #include <atalk/nbp.h>
44 #include <atalk/netddp.h>
45 #include <atalk/ddp.h>
47 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature */
49 #define SOCKLEN_T unsigned int
52 struct sockaddr_at target;
53 int s, nsent = 0, nrecv = 0;
54 time_t totalms = 0, minms = -1, maxms = -1;
55 static unsigned int pings = 0;
57 #if !defined( ibm032 ) && !defined( _IBMR2 )
63 printf( "\n----%d.%d AEP Statistics----\n",
64 ntohs( target.sat_addr.s_net ), target.sat_addr.s_node );
65 printf( "%d packets sent, %d packets received, %d%% packet loss\n",
66 nsent, nrecv, (( nsent - nrecv ) * 100 ) / nsent );
68 printf( "round-trip (ms) min/avg/max = %d/%d/%d\n",
69 minms, totalms / nrecv, maxms );
75 #if !defined( ibm032 ) && !defined( _IBMR2 )
82 static unsigned int seq = 0;
87 bcopy( &seq, p, sizeof( unsigned int ));
88 p += sizeof( unsigned int );
91 if ( gettimeofday( &tv, (struct timezone *)0 ) < 0 ) {
92 perror( "gettimeofday" );
95 bcopy( &tv, p, sizeof( struct timeval ));
96 p += sizeof( struct timeval );
98 if ( netddp_sendto( s, buf, p - buf, 0, (struct sockaddr *) &target,
99 sizeof( struct sockaddr_at )) < 0 ) {
104 if (pings && nsent > pings) done();
114 struct sockaddr_at sat, saddr;
115 struct timeval tv, atv;
117 char *obj = NULL, *type = "Workstation", *zone = "*";
122 char buf[ 1024 ], *p;
128 memset(&saddr, 0, sizeof(saddr));
129 while (( cc = getopt( ac, av, "c:A:" )) != EOF ) {
132 if (!atalk_aton(optarg, &saddr.sat_addr)) {
133 fprintf(stderr, "Bad address.\n");
137 pings = atoi( optarg );
145 if ( ac - optind != 1 ) {
151 * Save the port, since nbp_lookup calls getservbyname() to get the
154 if (( se = getservbyname( "echo", "ddp" )) == NULL )
157 port = ntohs( se->s_port );
159 memset( &target, 0, sizeof( struct sockaddr_at ));
161 target.sat_len = sizeof( struct sockaddr_at );
163 target.sat_family = AF_APPLETALK;
164 if ( !atalk_aton( av[ optind ], &target.sat_addr )) {
165 if ( nbp_name( av[ optind ], &obj, &type, &zone ) || !obj ) {
166 fprintf( stderr, "Bad name: %s\n", av[ optind ] );
169 if ( nbp_lookup( obj, type, zone, &nn, 1, &saddr.sat_addr) <= 0 ) {
170 fprintf( stderr, "Can't find: %s\n", av[ optind ] );
173 memcpy( &target, &nn.nn_sat, sizeof( struct sockaddr_at ));
175 target.sat_port = port;
177 if ((s = netddp_open(saddr.sat_addr.s_net || saddr.sat_addr.s_node ?
178 &saddr : NULL, NULL)) < 0) {
183 sv.sa_handler = aep_send;
184 sigemptyset( &sv.sa_mask );
185 sigaddset( &sv.sa_mask, SIGINT );
186 sv.sa_flags = SA_RESTART;
187 if ( sigaction( SIGALRM, &sv, (struct sigaction *)0 ) < 0 ) {
188 perror( "sigaction" );
192 sv.sa_handler = done;
193 sigemptyset( &sv.sa_mask );
194 sigaddset( &sv.sa_mask, SIGALRM );
195 sv.sa_flags = SA_RESTART;
196 if ( sigaction( SIGINT, &sv, (struct sigaction *)0 ) < 0 ) {
197 perror( "sigaction" );
201 it.it_interval.tv_sec = 1L;
202 it.it_interval.tv_usec = 0L;
203 it.it_value.tv_sec = 1L;
204 it.it_value.tv_usec = 0L;
206 if ( setitimer( ITIMER_REAL, &it, (struct itimerval *)0 ) < 0 ) {
207 perror( "setitimer" );
212 satlen = sizeof( struct sockaddr_at );
213 if (( cc = netddp_recvfrom( s, buf, sizeof( buf ), 0,
214 (struct sockaddr *) &sat,
216 if ( errno == EINTR ) {
220 perror( "recvfrom" );
225 if ( *p++ != DDPTYPE_AEP || *p++ != AEPOP_REPLY ) {
226 fprintf( stderr, "%s: bad packet!\n", av[ 0 ] );
229 if ( gettimeofday( &tv, (struct timezone *)0 ) < 0 ) {
230 perror( "gettimeofday" );
233 bcopy( p, &seq, sizeof( unsigned int ));
234 p += sizeof( unsigned int );
235 bcopy( p, &atv, sizeof( struct timeval ));
237 ms = ( tv.tv_sec - atv.tv_sec ) * 1000 +
238 ( tv.tv_usec - atv.tv_usec ) / 1000;
243 if ( ms < minms || minms == -1 ) {
246 printf( "%d bytes from %u.%u: aep_seq=%d. time=%d. ms\n",
247 cc, ntohs( sat.sat_addr.s_net ), sat.sat_addr.s_node,
249 if (pings && seq + 1 >= pings) done();
255 fprintf( stderr, "usage:\t%s [-A source address ] [-c count] ( addr | nbpname )\n", av0 );