2 * Copyright (c) 1990,1997 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
9 #include <sys/socket.h>
10 #include <sys/signal.h>
12 #include <netatalk/endian.h>
13 #include <netatalk/at.h>
14 #include <netatalk/ddp.h>
15 #include <atalk/compat.h>
16 #include <atalk/nbp.h>
17 #include <atalk/netddp.h>
18 #include <atalk/ddp.h>
25 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
27 #define SOCKLEN_T unsigned int
30 int nbp_lookup( obj, type, zone, nn, nncnt, ataddr )
31 const char *obj, *type, *zone;
34 const struct at_addr *ataddr;
36 struct sockaddr_at addr, from;
37 struct timeval tv, tv_begin, tv_end;
43 char *data = nbp_send;
45 int s, cnt, tries, sc, cc, i, c;
47 memset(&addr, 0, sizeof(addr));
48 memset(&from, 0, sizeof(from));
50 memcpy(&addr.sat_addr, ataddr, sizeof(struct at_addr));
51 if ((s = netddp_open(&addr, &from)) < 0)
54 *data++ = DDPTYPE_NBP;
56 nh.nh_op = from.sat_addr.s_node ? NBPOP_BRRQ : NBPOP_LKUP;
58 nh.nh_op = NBPOP_BRRQ;
63 memcpy( data, &nh, SZ_NBPHDR );
66 memset(&nt, 0, sizeof(nt));
67 nt.nt_net = addr.sat_addr.s_net;
68 nt.nt_node = addr.sat_addr.s_node;
69 nt.nt_port = addr.sat_port;
71 memcpy( data, &nt, SZ_NBPTUPLE);
75 if (( cc = strlen( obj )) > NBPSTRLEN ) goto lookup_err;
77 memcpy( data, obj, cc );
81 *data++ = '='; /* match anything */
85 if (( cc = strlen( type )) > NBPSTRLEN ) goto lookup_err;
87 memcpy( data, type, cc );
91 *data++ = '='; /* match anything */
95 if (( cc = strlen( zone )) > NBPSTRLEN ) goto lookup_err;
97 memcpy( data, zone, cc );
101 *data++ = '*'; /* default zone */
104 if ( nbp_port == 0 ) {
105 if (( se = getservbyname( "nbp", "ddp" )) == NULL ) {
108 nbp_port = ntohs( se->s_port );
113 if (from.sat_addr.s_node) {
114 memcpy(&addr.sat_addr, &from.sat_addr, sizeof(addr.sat_addr));
116 addr.sat_addr.s_net = ATADDR_ANYNET;
117 addr.sat_addr.s_node = ATADDR_BCAST;
120 addr.sat_port = nbp_port;
124 sc = data - nbp_send;
125 while ( tries > 0 ) {
126 if ( netddp_sendto( s, nbp_send, sc, 0, (struct sockaddr *)&addr,
127 sizeof( struct sockaddr_at )) < 0 ) {
137 if ( gettimeofday( &tv_begin, NULL ) < 0 ) {
140 if (( c = select( s + 1, &fds, NULL, NULL, &tv )) < 0 ) {
143 if ( c == 0 || FD_ISSET( s, &fds ) == 0 ) {
146 if ( gettimeofday( &tv_end, NULL ) < 0 ) {
149 if ( tv_begin.tv_usec > tv_end.tv_sec ) {
150 tv_end.tv_usec += 1000000;
153 if (( tv.tv_usec -= ( tv_end.tv_usec - tv_begin.tv_usec )) < 0 ) {
154 tv.tv_usec += 1000000;
157 if (( tv.tv_sec -= ( tv_end.tv_sec - tv_begin.tv_sec )) < 0 ) {
161 namelen = sizeof( struct sockaddr_at );
162 if (( cc = netddp_recvfrom( s, nbp_recv, sizeof( nbp_recv ), 0,
163 (struct sockaddr *)&from, &namelen )) < 0 ) {
168 if ( *data++ != DDPTYPE_NBP ) {
173 memcpy( &nh, data, SZ_NBPHDR );
175 if ( nh.nh_op != NBPOP_LKUPREPLY ) {
180 while (( i = nbp_parse( data, &nve, cc )) >= 0 ) {
184 * Check to see if nve is already in nn. If not,
185 * put it in, and increment cnt.
187 for ( i = 0; i < cnt; i++ ) {
188 if ( nbp_match( &nve, &nn[ i ],
189 NBPMATCH_NOZONE|NBPMATCH_NOGLOB )) {
196 if ( cnt == nncnt ) {
201 if ( cnt == nncnt ) {