2 * $Id: getzones.c,v 1.7 2005-04-28 20:49:19 bfernhomberg 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/unicode.h>
28 #include <atalk/zip.h>
30 void print_zones(short n, char *buf);
35 fprintf( stderr, "usage:\t%s [-m | -l] [address]\n", s );
39 int main( argc, argv )
43 struct atp_handle *ah;
44 struct atp_block atpb;
45 struct sockaddr_at saddr;
47 char reqdata[4], buf[ ATP_MAXDATA ];
49 short temp, index = 0;
50 int c, myzoneflg = 0, localzonesflg = 0, errflg = 0;
53 reqdata[ 0 ] = ZIPOP_GETZONELIST;
55 while (( c = getopt( argc, argv, "ml" )) != EOF ) {
58 if ( localzonesflg ) {
62 reqdata[ 0 ] = ZIPOP_GETMYZONE;
69 reqdata[ 0 ] = ZIPOP_GETLOCALZONES;
76 if ( errflg || argc - optind > 1 ) {
80 memset( &saddr, 0, sizeof( struct sockaddr_at ));
82 saddr.sat_len = sizeof( struct sockaddr_at );
84 saddr.sat_family = AF_APPLETALK;
85 if (( se = getservbyname( "zip", "ddp" )) == NULL )
88 saddr.sat_port = ntohs( se->s_port );
90 if ( argc == optind ) {
91 saddr.sat_addr.s_net = ATADDR_ANYNET;
92 saddr.sat_addr.s_node = ATADDR_ANYNODE;
94 if ( !atalk_aton( argv[ optind ], &saddr.sat_addr )) {
95 fprintf( stderr, "Bad address.\n" );
100 if (( ah = atp_open( ATADDR_ANYPORT, &saddr.sat_addr )) == NULL ) {
101 perror( "atp_open" );
105 index = ( myzoneflg ? 0 : 1 );
109 atpb.atp_saddr = &saddr;
110 temp = htons( index );
111 memcpy( reqdata + 2, &temp, 2 );
112 atpb.atp_sreqdata = reqdata;
113 atpb.atp_sreqdlen = 4;
115 atpb.atp_sreqtries = 5;
117 /* send getzone request zones (or get my zone)
119 if ( atp_sreq( ah, &atpb, 1, 0 ) < 0 ) {
120 perror( "atp_sreq" );
125 iov.iov_len = ATP_MAXDATA;
126 atpb.atp_rresiov = &iov;
127 atpb.atp_rresiovcnt = 1;
129 if ( atp_rresp( ah, &atpb ) < 0 ) {
130 perror( "atp_rresp" );
134 memcpy( &temp, (char *) iov.iov_base + 2, 2 );
135 temp = ntohs( temp );
136 print_zones( temp, (char *) iov.iov_base+4 );
138 } while ( !myzoneflg && !((char *)iov.iov_base)[ 0 ] );
140 if ( atp_close( ah ) != 0 ) {
141 perror( "atp_close" );
150 * n: number of zones in this packet
151 * buf: zone length/name pairs
153 void print_zones( short n, char *buf )
158 for ( ; n--; buf += (*buf) + 1 ) {
160 if ((size_t)(-1) == (zone_len = convert_string_allocate( CH_MAC,
161 CH_UNIX, buf+1, *buf, &zone)) ) {
163 if (( zone = strdup(buf+1)) == NULL ) {
169 printf( "%.*s\n", (int)zone_len, zone );