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