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