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