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