2 * $Id: nbp_lkup.c,v 1.5 2009-10-13 22:55:37 didg Exp $
4 * Copyright (c) 1990,1997 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
16 #include <sys/types.h>
17 #include <sys/param.h>
18 #include <sys/socket.h>
21 #include <netatalk/endian.h>
22 #include <netatalk/at.h>
23 #include <netatalk/ddp.h>
24 #include <atalk/compat.h>
25 #include <atalk/nbp.h>
26 #include <atalk/netddp.h>
27 #include <atalk/ddp.h>
31 #endif /* HAVE_NETDB_H */
35 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
37 #define SOCKLEN_T unsigned int
38 #endif /* ! SOCKLEN_T */
40 int nbp_lookup( const char *obj, const char *type, const char *zone, struct nbpnve *nn,
42 const struct at_addr *ataddr)
44 struct sockaddr_at addr, from;
45 struct timeval tv, tv_begin, tv_end;
51 char *data = nbp_send;
53 int s, cnt, tries, sc, cc, i, c;
55 memset(&addr, 0, sizeof(addr));
56 memset(&from, 0, sizeof(from));
58 memcpy(&addr.sat_addr, ataddr, sizeof(struct at_addr));
59 if ((s = netddp_open(&addr, &from)) < 0)
62 *data++ = DDPTYPE_NBP;
64 nh.nh_op = from.sat_addr.s_node ? NBPOP_BRRQ : NBPOP_LKUP;
65 #else /* MACOSX_SERVER */
66 nh.nh_op = NBPOP_BRRQ;
67 #endif /* MACOSX_SERVER */
71 memcpy( data, &nh, SZ_NBPHDR );
74 memset(&nt, 0, sizeof(nt));
75 nt.nt_net = addr.sat_addr.s_net;
76 nt.nt_node = addr.sat_addr.s_node;
77 nt.nt_port = addr.sat_port;
79 memcpy( data, &nt, SZ_NBPTUPLE);
83 if (( cc = strlen( obj )) > NBPSTRLEN ) goto lookup_err;
85 memcpy( data, obj, cc );
89 *data++ = '='; /* match anything */
93 if (( cc = strlen( type )) > NBPSTRLEN ) goto lookup_err;
95 memcpy( data, type, cc );
99 *data++ = '='; /* match anything */
103 if (( cc = strlen( zone )) > NBPSTRLEN ) goto lookup_err;
105 memcpy( data, zone, cc );
109 *data++ = '*'; /* default zone */
112 if ( nbp_port == 0 ) {
113 if (( se = getservbyname( "nbp", "ddp" )) == NULL ) {
116 nbp_port = ntohs( se->s_port );
121 if (from.sat_addr.s_node) {
122 memcpy(&addr.sat_addr, &from.sat_addr, sizeof(addr.sat_addr));
124 addr.sat_addr.s_net = ATADDR_ANYNET;
125 addr.sat_addr.s_node = ATADDR_BCAST;
127 #endif /* MACOSX_SERVER */
128 addr.sat_port = nbp_port;
132 sc = data - nbp_send;
133 while ( tries > 0 ) {
134 if ( netddp_sendto( s, nbp_send, sc, 0, (struct sockaddr *)&addr,
135 sizeof( struct sockaddr_at )) < 0 ) {
145 if ( gettimeofday( &tv_begin, NULL ) < 0 ) {
148 if (( c = select( s + 1, &fds, NULL, NULL, &tv )) < 0 ) {
151 if ( c == 0 || FD_ISSET( s, &fds ) == 0 ) {
154 if ( gettimeofday( &tv_end, NULL ) < 0 ) {
157 if ( tv_begin.tv_usec > tv_end.tv_sec ) {
158 tv_end.tv_usec += 1000000;
161 if (( tv.tv_usec -= ( tv_end.tv_usec - tv_begin.tv_usec )) < 0 ) {
162 tv.tv_usec += 1000000;
165 if (( tv.tv_sec -= ( tv_end.tv_sec - tv_begin.tv_sec )) < 0 ) {
169 namelen = sizeof( struct sockaddr_at );
170 if (( cc = netddp_recvfrom( s, nbp_recv, sizeof( nbp_recv ), 0,
171 (struct sockaddr *)&from, &namelen )) < 0 ) {
176 if ( *data++ != DDPTYPE_NBP ) {
181 memcpy( &nh, data, SZ_NBPHDR );
183 if ( nh.nh_op != NBPOP_LKUPREPLY ) {
188 while (( i = nbp_parse( data, &nve, cc )) >= 0 ) {
192 * Check to see if nve is already in nn. If not,
193 * put it in, and increment cnt.
195 for ( i = 0; i < cnt; i++ ) {
196 if ( nbp_match( &nve, &nn[ i ],
197 NBPMATCH_NOZONE|NBPMATCH_NOGLOB )) {
204 if ( cnt == nncnt ) {
209 if ( cnt == nncnt ) {