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