2 * Copyright (c) 1990,1997 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
11 #include <sys/types.h>
12 #include <sys/param.h>
13 #include <sys/socket.h>
14 #include <sys/signal.h>
16 #include <netatalk/endian.h>
17 #include <netatalk/at.h>
18 #include <netatalk/ddp.h>
19 #include <atalk/compat.h>
20 #include <atalk/nbp.h>
21 #include <atalk/netddp.h>
22 #include <atalk/ddp.h>
29 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
31 #define SOCKLEN_T unsigned int
34 int nbp_lookup( obj, type, zone, nn, nncnt, ataddr )
35 const char *obj, *type, *zone;
38 const struct at_addr *ataddr;
40 struct sockaddr_at addr, from;
41 struct timeval tv, tv_begin, tv_end;
47 char *data = nbp_send;
49 int s, cnt, tries, sc, cc, i, c;
51 memset(&addr, 0, sizeof(addr));
52 memset(&from, 0, sizeof(from));
54 memcpy(&addr.sat_addr, ataddr, sizeof(struct at_addr));
55 if ((s = netddp_open(&addr, &from)) < 0)
58 *data++ = DDPTYPE_NBP;
60 nh.nh_op = from.sat_addr.s_node ? NBPOP_BRRQ : NBPOP_LKUP;
62 nh.nh_op = NBPOP_BRRQ;
67 memcpy( data, &nh, SZ_NBPHDR );
70 memset(&nt, 0, sizeof(nt));
71 nt.nt_net = addr.sat_addr.s_net;
72 nt.nt_node = addr.sat_addr.s_node;
73 nt.nt_port = addr.sat_port;
75 memcpy( data, &nt, SZ_NBPTUPLE);
79 if (( cc = strlen( obj )) > NBPSTRLEN ) goto lookup_err;
81 memcpy( data, obj, cc );
85 *data++ = '='; /* match anything */
89 if (( cc = strlen( type )) > NBPSTRLEN ) goto lookup_err;
91 memcpy( data, type, cc );
95 *data++ = '='; /* match anything */
99 if (( cc = strlen( zone )) > NBPSTRLEN ) goto lookup_err;
101 memcpy( data, zone, cc );
105 *data++ = '*'; /* default zone */
108 if ( nbp_port == 0 ) {
109 if (( se = getservbyname( "nbp", "ddp" )) == NULL ) {
112 nbp_port = ntohs( se->s_port );
117 if (from.sat_addr.s_node) {
118 memcpy(&addr.sat_addr, &from.sat_addr, sizeof(addr.sat_addr));
120 addr.sat_addr.s_net = ATADDR_ANYNET;
121 addr.sat_addr.s_node = ATADDR_BCAST;
124 addr.sat_port = nbp_port;
128 sc = data - nbp_send;
129 while ( tries > 0 ) {
130 if ( netddp_sendto( s, nbp_send, sc, 0, (struct sockaddr *)&addr,
131 sizeof( struct sockaddr_at )) < 0 ) {
141 if ( gettimeofday( &tv_begin, NULL ) < 0 ) {
144 if (( c = select( s + 1, &fds, NULL, NULL, &tv )) < 0 ) {
147 if ( c == 0 || FD_ISSET( s, &fds ) == 0 ) {
150 if ( gettimeofday( &tv_end, NULL ) < 0 ) {
153 if ( tv_begin.tv_usec > tv_end.tv_sec ) {
154 tv_end.tv_usec += 1000000;
157 if (( tv.tv_usec -= ( tv_end.tv_usec - tv_begin.tv_usec )) < 0 ) {
158 tv.tv_usec += 1000000;
161 if (( tv.tv_sec -= ( tv_end.tv_sec - tv_begin.tv_sec )) < 0 ) {
165 namelen = sizeof( struct sockaddr_at );
166 if (( cc = netddp_recvfrom( s, nbp_recv, sizeof( nbp_recv ), 0,
167 (struct sockaddr *)&from, &namelen )) < 0 ) {
172 if ( *data++ != DDPTYPE_NBP ) {
177 memcpy( &nh, data, SZ_NBPHDR );
179 if ( nh.nh_op != NBPOP_LKUPREPLY ) {
184 while (( i = nbp_parse( data, &nve, cc )) >= 0 ) {
188 * Check to see if nve is already in nn. If not,
189 * put it in, and increment cnt.
191 for ( i = 0; i < cnt; i++ ) {
192 if ( nbp_match( &nve, &nn[ i ],
193 NBPMATCH_NOZONE|NBPMATCH_NOGLOB )) {
200 if ( cnt == nncnt ) {
205 if ( cnt == nncnt ) {