2 * $Id: getzones.c,v 1.5 2001-07-31 19:49:02 srittau Exp $
7 #endif /* HAVE_CONFIG_H */
10 #include <sys/socket.h>
11 #include <sys/param.h>
16 #endif /* HAVE_UNISTD_H */
19 #endif /* HAVE_NETDB_H */
23 #include <netatalk/endian.h>
24 #include <netatalk/at.h>
25 #include <atalk/atp.h>
26 #include <atalk/util.h>
27 #include <atalk/zip.h>
29 void print_zones(short n, char *buf);
34 fprintf( stderr, "usage:\t%s [-m | -l] [address]\n", s );
38 int main( argc, argv )
42 struct atp_handle *ah;
43 struct atp_block atpb;
44 struct sockaddr_at saddr;
46 char reqdata[4], buf[ ATP_MAXDATA ];
48 short temp, index = 0;
49 int c, myzoneflg = 0, localzonesflg = 0, errflg = 0;
52 reqdata[ 0 ] = ZIPOP_GETZONELIST;
54 while (( c = getopt( argc, argv, "ml" )) != EOF ) {
57 if ( localzonesflg ) {
61 reqdata[ 0 ] = ZIPOP_GETMYZONE;
68 reqdata[ 0 ] = ZIPOP_GETLOCALZONES;
75 if ( errflg || argc - optind > 1 ) {
79 memset( &saddr, 0, sizeof( struct sockaddr_at ));
81 saddr.sat_len = sizeof( struct sockaddr_at );
83 saddr.sat_family = AF_APPLETALK;
84 if (( se = getservbyname( "zip", "ddp" )) == NULL )
87 saddr.sat_port = ntohs( se->s_port );
89 if ( argc == optind ) {
90 saddr.sat_addr.s_net = ATADDR_ANYNET;
91 saddr.sat_addr.s_node = ATADDR_ANYNODE;
93 if ( !atalk_aton( argv[ optind ], &saddr.sat_addr )) {
94 fprintf( stderr, "Bad address.\n" );
99 if (( ah = atp_open( ATADDR_ANYPORT, &saddr.sat_addr )) == NULL ) {
100 perror( "atp_open" );
104 index = ( myzoneflg ? 0 : 1 );
108 atpb.atp_saddr = &saddr;
109 temp = htons( index );
110 memcpy( reqdata + 2, &temp, 2 );
111 atpb.atp_sreqdata = reqdata;
112 atpb.atp_sreqdlen = 4;
114 atpb.atp_sreqtries = 5;
116 /* send getzone request zones (or get my zone)
118 if ( atp_sreq( ah, &atpb, 1, 0 ) < 0 ) {
119 perror( "atp_sreq" );
124 iov.iov_len = ATP_MAXDATA;
125 atpb.atp_rresiov = &iov;
126 atpb.atp_rresiovcnt = 1;
128 if ( atp_rresp( ah, &atpb ) < 0 ) {
129 perror( "atp_rresp" );
133 memcpy( &temp, (char *) iov.iov_base + 2, 2 );
134 temp = ntohs( temp );
135 print_zones( temp, (char *) iov.iov_base+4 );
137 } while ( !myzoneflg && !((char *)iov.iov_base)[ 0 ] );
139 if ( atp_close( ah ) != 0 ) {
140 perror( "atp_close" );
148 void print_zones( n, buf )
149 short n; /* number of zones in this packet */
150 char *buf; /* zone length/name pairs */
152 for ( ; n--; buf += (*buf) + 1 ) {
153 printf( "%.*s\n", *buf, buf+1 );