]> arthur.barton.de Git - netatalk.git/blob - bin/getzones/getzones.c
massive commenting/autoconf changes
[netatalk.git] / bin / getzones / getzones.c
1 /*
2  * $Id: getzones.c,v 1.4 2001-06-29 14:14:46 rufustfirefly Exp $
3  */
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif /* HAVE_CONFIG_H */
8
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <sys/param.h>
12 #include <sys/uio.h>
13 #include <sys/time.h>
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif /* HAVE_UNISTD_H */
17 #ifdef HAVE_NETDB_H
18 #include <netdb.h>
19 #endif /* HAVE_NETDB_H */
20 #include <stdio.h>
21 #include <string.h>
22 #include <netatalk/endian.h>
23 #include <netatalk/at.h>
24 #include <atalk/atp.h>
25 #include <atalk/util.h>
26 #include <atalk/zip.h>
27
28 void print_zones(short n, char *buf);
29
30 void usage( s )
31     char *s;
32 {
33     fprintf( stderr, "usage:\t%s [-m | -l] [address]\n", s );
34     exit( 1 );
35 }
36
37 int main( argc, argv )
38     int         argc;
39     char        *argv[];
40 {
41     struct atp_handle   *ah;
42     struct atp_block    atpb;
43     struct sockaddr_at  saddr;
44     struct servent      *se;
45     char                reqdata[4], buf[ ATP_MAXDATA ];
46     struct iovec        iov;
47     short               temp, index = 0;
48     int                 c, myzoneflg = 0, localzonesflg = 0, errflg = 0;
49     extern int          optind;
50
51     reqdata[ 0 ] = ZIPOP_GETZONELIST;
52
53     while (( c = getopt( argc, argv, "ml" )) != EOF ) {
54         switch (c) {
55         case 'm':
56             if ( localzonesflg ) {
57                 ++errflg;
58             }
59             ++myzoneflg;
60             reqdata[ 0 ] = ZIPOP_GETMYZONE;
61             break;
62         case 'l':
63             if ( myzoneflg ) {
64                 ++errflg;
65             }
66             ++localzonesflg;
67             reqdata[ 0 ] = ZIPOP_GETLOCALZONES;
68             break;
69         default:
70             ++errflg;
71         }
72     }
73
74     if ( errflg || argc - optind > 1 ) {
75         usage( argv[ 0 ] );
76     }
77
78     memset( &saddr, 0, sizeof( struct sockaddr_at ));
79 #ifdef BSD4_4
80     saddr.sat_len = sizeof( struct sockaddr_at );
81 #endif /* BSD4_4 */
82     saddr.sat_family = AF_APPLETALK;
83     if (( se = getservbyname( "zip", "ddp" )) == NULL )
84         saddr.sat_port = 6;
85     else 
86         saddr.sat_port = ntohs( se->s_port );
87
88     if ( argc == optind ) {
89         saddr.sat_addr.s_net = ATADDR_ANYNET;
90         saddr.sat_addr.s_node = ATADDR_ANYNODE;
91     } else {
92         if ( !atalk_aton( argv[ optind ], &saddr.sat_addr )) {
93             fprintf( stderr, "Bad address.\n" );
94             exit( 1 );
95         }
96     }
97
98     if (( ah = atp_open( ATADDR_ANYPORT, &saddr.sat_addr )) == NULL ) {
99         perror( "atp_open" );
100         exit( 1 );
101     }
102
103     index = ( myzoneflg ? 0 : 1 );
104     reqdata[1] = 0;
105
106     do {
107         atpb.atp_saddr = &saddr;
108         temp = htons( index );
109         memcpy( reqdata + 2, &temp, 2 );
110         atpb.atp_sreqdata = reqdata;
111         atpb.atp_sreqdlen = 4;
112         atpb.atp_sreqto = 2;
113         atpb.atp_sreqtries = 5;
114
115         /* send getzone request zones (or get my zone)
116         */
117         if ( atp_sreq( ah, &atpb, 1, 0 ) < 0 ) {
118             perror( "atp_sreq" );
119             exit( 1 );
120         }
121
122         iov.iov_base = buf;
123         iov.iov_len = ATP_MAXDATA;
124         atpb.atp_rresiov = &iov;
125         atpb.atp_rresiovcnt = 1;
126
127         if ( atp_rresp( ah, &atpb ) < 0 ) {
128             perror( "atp_rresp" );
129             exit( 1 );
130         }
131
132         memcpy( &temp, (char *) iov.iov_base + 2, 2 );
133         temp = ntohs( temp );
134         print_zones( temp, (char *) iov.iov_base+4 );
135         index += temp;
136     } while ( !myzoneflg && !((char *)iov.iov_base)[ 0 ] );
137
138     if ( atp_close( ah ) != 0 ) {
139         perror( "atp_close" );
140         exit( 1 );
141     }
142
143     exit( 0 );
144 }
145
146
147 void print_zones( n, buf )
148     short       n;      /* number of zones in this packet */
149     char        *buf;   /* zone length/name pairs */
150 {
151     for ( ; n--; buf += (*buf) + 1 ) {
152         printf( "%.*s\n", *buf, buf+1 );
153     }
154 }