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