]> arthur.barton.de Git - netatalk.git/blob - libatalk/nbp/nbp_util.c
Trunk-BP: includes cleanup.
[netatalk.git] / libatalk / nbp / nbp_util.c
1 /*
2  * $Id: nbp_util.c,v 1.3.2.1 2002-02-08 00:04:49 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 <signal.h>
14
15 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19
20 #include <netatalk/endian.h>
21 #include <netatalk/at.h>
22
23 #include <atalk/nbp.h>
24 #include <atalk/ddp.h>
25 #include <atalk/util.h>
26
27 #ifdef HAVE_NETDB_H
28 #include <netdb.h>
29 #endif /* HAVE_NETDB_H */
30
31 #include  "nbp_conf.h"
32
33 char            nbp_send[ 1024 ];
34 char            nbp_recv[ 1024 ];
35 u_char          nbp_port = 0;
36 unsigned char   nbp_id = 0;
37
38 int nbp_parse( data, nn, len )
39     char                *data;
40     struct nbpnve       *nn;
41     int                 len;
42 {
43     struct nbptuple     nt;
44
45     memcpy( &nt, data, SZ_NBPTUPLE);
46     data += SZ_NBPTUPLE;
47     len -= SZ_NBPTUPLE;
48     if ( len < 0 ) {
49         return( -1 );
50     }
51
52 #ifdef BSD4_4
53     nn->nn_sat.sat_len = sizeof( struct sockaddr_at );
54 #endif /* BSD4_4 */
55     nn->nn_sat.sat_family = AF_APPLETALK;
56     nn->nn_sat.sat_addr.s_net = nt.nt_net;
57     nn->nn_sat.sat_addr.s_node = nt.nt_node;
58     nn->nn_sat.sat_port = nt.nt_port;
59
60     nn->nn_objlen = *data++;
61     len -= nn->nn_objlen + 1;
62     if ( len < 0 ) {
63         return( -1 );
64     }
65     if ( nn->nn_objlen > NBPSTRLEN ) {
66         return( -1 );
67     }
68     memcpy( nn->nn_obj, data, nn->nn_objlen );
69     data += nn->nn_objlen;
70
71     nn->nn_typelen = *data++;
72     len -= nn->nn_typelen + 1;
73     if ( len < 0 ) {
74         return( -1 );
75     }
76     if ( nn->nn_typelen > NBPSTRLEN ) {
77         return( 1 );
78     }
79     memcpy( nn->nn_type, data, nn->nn_typelen );
80
81     data += nn->nn_typelen;
82     nn->nn_zonelen = *data++;
83     len -= nn->nn_zonelen + 1;
84     if ( len < 0 ) {
85         return( -1 );
86     }
87     if ( nn->nn_zonelen > NBPSTRLEN ) {
88         return( 1 );
89     }
90     memcpy( nn->nn_zone, data, nn->nn_zonelen );
91
92     return( len );
93 }
94
95 #define NBPM_OBJ        (1<<1)
96 #define NBPM_TYPE       (1<<2)
97 #define NBPM_ZONE       (1<<3)
98
99 int nbp_match( n1, n2, flags )
100     struct nbpnve       *n1, *n2;
101     int                 flags;
102 {
103     int                 match = 0;
104
105     if ( flags & NBPMATCH_NOZONE ) {
106         match |= NBPM_ZONE;
107     }
108
109     if ( !( flags & NBPMATCH_NOGLOB )) {
110         if ( n1->nn_objlen == 1 && n1->nn_obj[0] == '=' ) {
111             match |= NBPM_OBJ;
112         }
113         if ( n1->nn_typelen == 1 && n1->nn_type[0] == '=' ) {
114             match |= NBPM_TYPE;
115         }
116     }
117
118     if ( !( match & NBPM_OBJ )) {
119         if ( n1->nn_objlen != n2->nn_objlen ||
120                 strndiacasecmp( n1->nn_obj, n2->nn_obj, n1->nn_objlen )) {
121             return( 0 );
122         }
123     }
124     if ( !( match & NBPM_TYPE )) {
125         if ( n1->nn_typelen != n2->nn_typelen ||
126                 strndiacasecmp( n1->nn_type, n2->nn_type, n1->nn_typelen )) {
127             return( 0 );
128         }
129     }
130     if ( !( match & NBPM_ZONE )) {
131         if ( n1->nn_zonelen != n2->nn_zonelen ||
132                 strndiacasecmp( n1->nn_zone, n2->nn_zone, n1->nn_zonelen )) {
133             return( 0 );
134         }
135     }
136
137     return( 1 );
138 }
139
140 int nbp_name( name, objp, typep, zonep )
141     const char  *name;
142     char        **objp, **typep, **zonep;
143 {
144     static char buf[ 32 + 1 + 32 + 1 + 32 + 1 ];
145     char        *p;
146
147     if ( name ) {
148         if ( strlen( name ) + 1 > sizeof( buf )) {
149             return( -1 );
150         }
151         strcpy( buf, name );
152
153         if (( p = strrchr( buf, '@' )) != NULL ) {
154             *p++ = '\0';
155             *zonep = p;
156         }
157         if (( p = strrchr( buf, ':' )) != NULL ) {
158             *p++ = '\0';
159             *typep = p;
160         }
161         if ( *buf != '\0' ) {
162             *objp = buf;
163         }
164     }
165
166     return( 0 );
167 }