2
* $Id: nbp_lkup.c,v 1.4 2002/01/17 06:12:02 srittau 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( obj, type, zone, nn, nncnt, ataddr )
41
const char *obj, *type, *zone;
44
const struct at_addr *ataddr;
46
struct sockaddr_at addr, from;
47
struct timeval tv, tv_begin, tv_end;
53
char *data = nbp_send;
55
int s, cnt, tries, sc, cc, i, c;
57
memset(&addr, 0, sizeof(addr));
58
memset(&from, 0, sizeof(from));
60
memcpy(&addr.sat_addr, ataddr, sizeof(struct at_addr));
61
if ((s = netddp_open(&addr, &from)) < 0)
64
*data++ = DDPTYPE_NBP;
66
nh.nh_op = from.sat_addr.s_node ? NBPOP_BRRQ : NBPOP_LKUP;
67
#else /* MACOSX_SERVER */
68
nh.nh_op = NBPOP_BRRQ;
69
#endif /* MACOSX_SERVER */
73
memcpy( data, &nh, SZ_NBPHDR );
76
memset(&nt, 0, sizeof(nt));
77
nt.nt_net = addr.sat_addr.s_net;
78
nt.nt_node = addr.sat_addr.s_node;
79
nt.nt_port = addr.sat_port;
81
memcpy( data, &nt, SZ_NBPTUPLE);
85
if (( cc = strlen( obj )) > NBPSTRLEN ) goto lookup_err;
87
memcpy( data, obj, cc );
91
*data++ = '='; /* match anything */
95
if (( cc = strlen( type )) > NBPSTRLEN ) goto lookup_err;
97
memcpy( data, type, cc );
101
*data++ = '='; /* match anything */
105
if (( cc = strlen( zone )) > NBPSTRLEN ) goto lookup_err;
107
memcpy( data, zone, cc );
111
*data++ = '*'; /* default zone */
114
if ( nbp_port == 0 ) {
115
if (( se = getservbyname( "nbp", "ddp" )) == NULL ) {
118
nbp_port = ntohs( se->s_port );
123
if (from.sat_addr.s_node) {
124
memcpy(&addr.sat_addr, &from.sat_addr, sizeof(addr.sat_addr));
126
addr.sat_addr.s_net = ATADDR_ANYNET;
127
addr.sat_addr.s_node = ATADDR_BCAST;
129
#endif /* MACOSX_SERVER */
130
addr.sat_port = nbp_port;
134
sc = data - nbp_send;
135
while ( tries > 0 ) {
136
if ( netddp_sendto( s, nbp_send, sc, 0, (struct sockaddr *)&addr,
137
sizeof( struct sockaddr_at )) < 0 ) {
147
if ( gettimeofday( &tv_begin, NULL ) < 0 ) {
150
if (( c = select( s + 1, &fds, NULL, NULL, &tv )) < 0 ) {
153
if ( c == 0 || FD_ISSET( s, &fds ) == 0 ) {
156
if ( gettimeofday( &tv_end, NULL ) < 0 ) {
159
if ( tv_begin.tv_usec > tv_end.tv_sec ) {
160
tv_end.tv_usec += 1000000;
163
if (( tv.tv_usec -= ( tv_end.tv_usec - tv_begin.tv_usec )) < 0 ) {
164
tv.tv_usec += 1000000;
167
if (( tv.tv_sec -= ( tv_end.tv_sec - tv_begin.tv_sec )) < 0 ) {
171
namelen = sizeof( struct sockaddr_at );
172
if (( cc = netddp_recvfrom( s, nbp_recv, sizeof( nbp_recv ), 0,
173
(struct sockaddr *)&from, &namelen )) < 0 ) {
178
if ( *data++ != DDPTYPE_NBP ) {
183
memcpy( &nh, data, SZ_NBPHDR );
185
if ( nh.nh_op != NBPOP_LKUPREPLY ) {
190
while (( i = nbp_parse( data, &nve, cc )) >= 0 ) {
194
* Check to see if nve is already in nn. If not,
195
* put it in, and increment cnt.
197
for ( i = 0; i < cnt; i++ ) {
198
if ( nbp_match( &nve, &nn[ i ],
199
NBPMATCH_NOZONE|NBPMATCH_NOGLOB )) {
206
if ( cnt == nncnt ) {
211
if ( cnt == nncnt ) {