]> arthur.barton.de Git - netatalk.git/blob - libatalk/nbp/nbp_unrgstr.c
5aa4a4b76cf6ef759cfe5a734710fe32bcbd5ef6
[netatalk.git] / libatalk / nbp / nbp_unrgstr.c
1 /*
2  * $Id: nbp_unrgstr.c,v 1.4 2002-01-17 06:12:02 srittau Exp $
3  *
4  * Copyright (c) 1990,1997 Regents of The University of Michigan.
5  * All Rights Reserved. See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <string.h>
13 #include <errno.h>
14 #include <signal.h>
15
16 #include <sys/types.h>
17 #include <sys/param.h>
18 #include <sys/socket.h>
19 #include <sys/time.h>
20
21 #include <netatalk/endian.h>
22 #include <netatalk/at.h>
23 #include <atalk/nbp.h>
24 #include <atalk/netddp.h>
25 #include <atalk/ddp.h>
26
27 #ifdef HAVE_NETDB_H
28 #include <netdb.h>
29 #endif /* HAVE_NETDB_H */
30 #include  "nbp_conf.h"
31
32 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
33 #ifndef SOCKLEN_T
34 #define SOCKLEN_T unsigned int
35 #endif /* ! SOCKLEN_T */
36
37 int nbp_unrgstr( obj, type, zone, addr )
38     const char          *obj, *type, *zone;
39     const struct at_addr *addr;
40 {
41     struct sockaddr_at  to;
42     struct nbphdr       nh;
43     struct timeval      timeout;
44     fd_set              readfd;
45     struct servent      *se;
46     char                *data;
47     int                 s, cc;
48     SOCKLEN_T           namelen;
49
50
51     memset(&to, 0, sizeof(to));
52     if ((s = netddp_open(&to, NULL)) < 0)
53         return -1;
54
55     data = nbp_send;
56     *data++ = DDPTYPE_NBP;
57     nh.nh_op = NBPOP_UNRGSTR;
58     nh.nh_cnt = 1;
59     nh.nh_id = ++nbp_id;
60     memcpy( data, &nh, SZ_NBPHDR );
61     data += SZ_NBPHDR;
62
63     memset(data, 0, SZ_NBPTUPLE);
64     data += SZ_NBPTUPLE;
65
66     if ( obj ) {
67         if (( cc = strlen( obj )) > NBPSTRLEN ) return( -1 );
68         *data++ = cc;
69         memcpy( data, obj, cc );
70         data += cc;
71     } else {
72         *data++ = 0;
73     }
74
75     if ( type ) {
76         if (( cc = strlen( type )) > NBPSTRLEN ) return( -1 );
77         *data++ = cc;
78         memcpy( data, type, cc );
79         data += cc;
80     } else {
81         *data++ = 0;
82     }
83
84     if ( zone ) {
85         if (( cc = strlen( zone )) > NBPSTRLEN ) return( -1 );
86         *data++ = cc;
87         memcpy( data, zone, cc );
88         data += cc;
89     } else {
90         *data++ = 0;
91     }
92
93     memset( &to, 0, sizeof( struct sockaddr_at ));
94     to.sat_family = AF_APPLETALK;
95     if (addr) 
96       memcpy(&to.sat_addr, addr, sizeof(struct at_addr));
97 #ifdef BSD4_4
98     to.sat_len = sizeof( struct sockaddr_at );
99 #endif /* BSD4_4 */
100
101     if ( nbp_port == 0 ) {
102         if (( se = getservbyname( "nbp", "ddp" )) == NULL ) {
103             nbp_port = 2;
104         } else {
105             nbp_port = ntohs( se->s_port );
106         }
107     }
108     to.sat_port = nbp_port;
109
110     if ( netddp_sendto( s, nbp_send, data - nbp_send, 0,
111                         (struct sockaddr *)&to,
112                         sizeof( struct sockaddr_at )) < 0 ) {
113         goto unregister_err;
114     }
115
116     FD_ZERO( &readfd );
117     FD_SET( s, &readfd );
118     timeout.tv_sec = 2;
119     timeout.tv_usec = 0;
120     if (( cc = select( s + 1, &readfd, 0, 0, &timeout )) < 0 ) {
121         goto unregister_err;
122     }
123     if ( cc == 0 ) {
124         errno = ETIMEDOUT;
125         goto unregister_err;
126     }
127
128     namelen = sizeof( struct sockaddr_at );
129     if (( cc = netddp_recvfrom( s, nbp_recv, sizeof( nbp_recv ), 0,
130                         (struct sockaddr *)&to, &namelen )) < 0 ) {
131         goto unregister_err;
132     }
133     netddp_close( s );
134
135     data = nbp_recv;
136     if ( *data++ != DDPTYPE_NBP ) {
137         return( -1 );
138     }
139     memcpy( &nh, data, SZ_NBPHDR );
140     if ( nh.nh_op != NBPOP_OK ) {
141         return( -1 );
142     }
143     return( 0 );
144
145 unregister_err:
146     netddp_close(s);
147     return -1;
148 }