2 * $Id: getzones.c,v 1.9 2009-10-14 01:38:28 didg 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 static void print_zones(short n, char *buf);
32 static void usage( char *s)
34 fprintf( stderr, "usage:\t%s [-m | -l] [address]\n", s );
38 int main( int argc, char *argv[])
40 struct atp_handle *ah;
41 struct atp_block atpb;
42 struct sockaddr_at saddr;
44 char reqdata[4], buf[ ATP_MAXDATA ];
46 short temp, index = 0;
47 int c, myzoneflg = 0, localzonesflg = 0, errflg = 0;
50 reqdata[ 0 ] = ZIPOP_GETZONELIST;
52 while (( c = getopt( argc, argv, "ml" )) != EOF ) {
55 if ( localzonesflg ) {
59 reqdata[ 0 ] = ZIPOP_GETMYZONE;
66 reqdata[ 0 ] = ZIPOP_GETLOCALZONES;
73 if ( errflg || argc - optind > 1 ) {
77 memset( &saddr, 0, sizeof( struct sockaddr_at ));
79 saddr.sat_len = sizeof( struct sockaddr_at );
81 saddr.sat_family = AF_APPLETALK;
82 if (( se = getservbyname( "zip", "ddp" )) == NULL )
85 saddr.sat_port = ntohs( se->s_port );
87 if ( argc == optind ) {
88 saddr.sat_addr.s_net = ATADDR_ANYNET;
89 saddr.sat_addr.s_node = ATADDR_ANYNODE;
91 if ( !atalk_aton( argv[ optind ], &saddr.sat_addr )) {
92 fprintf( stderr, "Bad address.\n" );
97 if (( ah = atp_open( ATADDR_ANYPORT, &saddr.sat_addr )) == NULL ) {
102 index = ( myzoneflg ? 0 : 1 );
106 atpb.atp_saddr = &saddr;
107 temp = htons( index );
108 memcpy( reqdata + 2, &temp, 2 );
109 atpb.atp_sreqdata = reqdata;
110 atpb.atp_sreqdlen = 4;
112 atpb.atp_sreqtries = 5;
114 /* send getzone request zones (or get my zone)
116 if ( atp_sreq( ah, &atpb, 1, 0 ) < 0 ) {
117 perror( "atp_sreq" );
122 iov.iov_len = ATP_MAXDATA;
123 atpb.atp_rresiov = &iov;
124 atpb.atp_rresiovcnt = 1;
126 if ( atp_rresp( ah, &atpb ) < 0 ) {
127 perror( "atp_rresp" );
131 memcpy( &temp, (char *) iov.iov_base + 2, 2 );
132 temp = ntohs( temp );
133 print_zones( temp, (char *) iov.iov_base+4 );
135 } while ( !myzoneflg && !((char *)iov.iov_base)[ 0 ] );
137 if ( atp_close( ah ) != 0 ) {
138 perror( "atp_close" );
147 * n: number of zones in this packet
148 * buf: zone length/name pairs
150 static void print_zones( short n, char *buf )
155 for ( ; n--; buf += (*buf) + 1 ) {
157 if ((size_t)(-1) == (zone_len = convert_string_allocate( CH_MAC,
158 CH_UNIX, buf+1, *buf, &zone)) ) {
160 if (( zone = strdup(buf+1)) == NULL ) {
166 printf( "%.*s\n", (int)zone_len, zone );