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>
44 #include <netatalk/endian.h>
45 #include <netatalk/at.h>
46 #include <atalk/compat.h>
47 #include <atalk/aep.h>
48 #include <atalk/nbp.h>
49 #include <atalk/netddp.h>
50 #include <atalk/ddp.h>
51 #include <atalk/util.h>
53 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature */
55 #define SOCKLEN_T unsigned int
58 struct sockaddr_at target;
59 int s, nsent = 0, nrecv = 0;
60 time_t totalms = 0, minms = -1, maxms = -1;
61 static unsigned int pings = 0;
68 printf( "\n----%d.%d AEP Statistics----\n",
69 ntohs( target.sat_addr.s_net ), target.sat_addr.s_node );
70 printf( "%d packets sent, %d packets received, %d%% packet loss\n",
71 nsent, nrecv, (( nsent - nrecv ) * 100 ) / nsent );
73 printf( "round-trip (ms) min/avg/max = %ld/%ld/%ld\n",
74 minms, totalms / nrecv, maxms );
84 static unsigned int seq = 0;
89 bcopy( &seq, p, sizeof( unsigned int ));
90 p += sizeof( unsigned int );
93 if ( gettimeofday( &tv, (struct timezone *)0 ) < 0 ) {
94 perror( "gettimeofday" );
97 bcopy( &tv, p, sizeof( struct timeval ));
98 p += sizeof( struct timeval );
100 if ( netddp_sendto( s, buf, p - buf, 0, (struct sockaddr *) &target,
101 sizeof( struct sockaddr_at )) < 0 ) {
106 if (pings && nsent > pings) done();
116 struct sockaddr_at sat, saddr;
117 struct timeval tv, atv;
119 char *obj = NULL, *type = "Workstation", *zone = "*";
124 char buf[ 1024 ], *p;
130 memset(&saddr, 0, sizeof(saddr));
131 while (( cc = getopt( ac, av, "c:A:" )) != EOF ) {
134 if (!atalk_aton(optarg, &saddr.sat_addr)) {
135 fprintf(stderr, "Bad address.\n");
139 pings = atoi( optarg );
147 if ( ac - optind != 1 ) {
153 * Save the port, since nbp_lookup calls getservbyname() to get the
156 if (( se = getservbyname( "echo", "ddp" )) == NULL )
159 port = ntohs( se->s_port );
161 memset( &target, 0, sizeof( struct sockaddr_at ));
163 target.sat_len = sizeof( struct sockaddr_at );
165 target.sat_family = AF_APPLETALK;
166 if ( !atalk_aton( av[ optind ], &target.sat_addr )) {
167 if ( nbp_name( av[ optind ], &obj, &type, &zone ) || !obj ) {
168 fprintf( stderr, "Bad name: %s\n", av[ optind ] );
171 if ( nbp_lookup( obj, type, zone, &nn, 1, &saddr.sat_addr) <= 0 ) {
172 fprintf( stderr, "Can't find: %s\n", av[ optind ] );
175 memcpy( &target, &nn.nn_sat, sizeof( struct sockaddr_at ));
177 target.sat_port = port;
179 if ((s = netddp_open(saddr.sat_addr.s_net || saddr.sat_addr.s_node ?
180 &saddr : NULL, NULL)) < 0) {
185 sv.sa_handler = aep_send;
186 sigemptyset( &sv.sa_mask );
187 sigaddset( &sv.sa_mask, SIGINT );
188 sv.sa_flags = SA_RESTART;
189 if ( sigaction( SIGALRM, &sv, (struct sigaction *)0 ) < 0 ) {
190 perror( "sigaction" );
194 sv.sa_handler = done;
195 sigemptyset( &sv.sa_mask );
196 sigaddset( &sv.sa_mask, SIGALRM );
197 sv.sa_flags = SA_RESTART;
198 if ( sigaction( SIGINT, &sv, (struct sigaction *)0 ) < 0 ) {
199 perror( "sigaction" );
203 it.it_interval.tv_sec = 1L;
204 it.it_interval.tv_usec = 0L;
205 it.it_value.tv_sec = 1L;
206 it.it_value.tv_usec = 0L;
208 if ( setitimer( ITIMER_REAL, &it, (struct itimerval *)0 ) < 0 ) {
209 perror( "setitimer" );
214 satlen = sizeof( struct sockaddr_at );
215 if (( cc = netddp_recvfrom( s, buf, sizeof( buf ), 0,
216 (struct sockaddr *) &sat,
218 if ( errno == EINTR ) {
222 perror( "recvfrom" );
227 if ( *p++ != DDPTYPE_AEP || *p++ != AEPOP_REPLY ) {
228 fprintf( stderr, "%s: bad packet!\n", av[ 0 ] );
231 if ( gettimeofday( &tv, (struct timezone *)0 ) < 0 ) {
232 perror( "gettimeofday" );
235 bcopy( p, &seq, sizeof( unsigned int ));
236 p += sizeof( unsigned int );
237 bcopy( p, &atv, sizeof( struct timeval ));
239 ms = ( tv.tv_sec - atv.tv_sec ) * 1000 +
240 ( tv.tv_usec - atv.tv_usec ) / 1000;
245 if ( ms < minms || minms == -1 ) {
248 printf( "%d bytes from %u.%u: aep_seq=%d. time=%ld. ms\n",
249 cc, ntohs( sat.sat_addr.s_net ), sat.sat_addr.s_node,
251 if (pings && seq + 1 >= pings) done();
255 void usage( char * av0 )
257 fprintf( stderr, "usage:\t%s [-A source address ] [-c count] ( addr | nbpname )\n", av0 );