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