2 * Copyright (c) 1996-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Hartmut Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: libunimsg/netnatm/misc/straddr.c,v 1.4 2004/07/08 08:22:02 brandt Exp $
32 #include <sys/types.h>
34 #include <sys/ctype.h>
35 #include <sys/libkern.h>
40 #include <netnatm/addr.h>
43 * Convert an NSAP address from the ASCII format to the binary.
44 * ASCII format means each byte formatted as a 2-byte hex number
45 * with dots freely interspersed between the bytes.
46 * If the conversion is succesful, the function returns 0, -1
47 * on conversion errors.
50 uni_str2nsap(u_char *out, const char *in)
55 for(i = 0; i < 20; i++) {
56 while((c = *in++) == '.')
58 if(!isascii(c) || !isxdigit(c))
60 out[i] = isdigit(c) ? (c - '0')
61 : islower(c) ? (c - 'a' + 10)
65 if(!isascii(c) || !isxdigit(c))
67 out[i] |= isdigit(c) ? (c - '0')
68 : islower(c) ? (c - 'a' + 10)
75 * Parse an emebedded E.164 NSAP address.
76 * If check is 0, the contents of the last 11 bytes are ignored
77 * If check is 1, the contents of all of these but the selector byte
78 * are checked to be zero. If check is 2 all 11 bytes must be 0.
81 uni_nsap2e164(char *e164, const u_char *nsap, int check)
89 if((nsap[8] & 0xf) != 0xf)
91 for(i = 1; i <= 7; i++) {
92 d = (nsap[i] >> 4) & 0xf;
93 if(d == 0x00 && p == e164)
100 if(d == 0x00 && p == e164)
106 d = (nsap[i] >> 4) & 0xf;
107 if(d != 0x00 || p == e164) {
118 while(i < ((check == 1) ? 19 : 20)) {
128 * Convert a binary representation to ASCII. The standard formats are
129 * recognized and dotted. Non-standard formats get no dots altogether.
132 uni_prefix2str(char *out, const u_char *in, u_int len, int dotit)
134 static char hex[16] = "0123456789abcdef";
135 static int fmt[3][6] = {
136 { 1, 2, 10, 6, 1, 0 },
137 { 1, 2, 10, 6, 1, 0 },
138 { 1, 8, 4, 6, 1, 0 },
152 for(f = 0; fmt[i][f]; f++) {
157 for(b = 0; b < fmt[i][f]; b++) {
160 *out++ = hex[(*in >> 4) & 0xf];
161 *out++ = hex[*in & 0xf];
173 case 0x45: /* E.164 */
180 for(i = 0; i < len; i++) {
181 *out++ = hex[(*in >> 4) & 0xf];
182 *out++ = hex[*in & 0xf];
189 uni_nsap2str(char *out, const u_char *in, int dotit)
191 uni_prefix2str(out, in, 20, dotit);
195 * Make an embedded E.164 NSAP address from a NSAP address.
196 * The E.164 address is a string of digits, at least one digit and
197 * not more than 15 digits long. The NSAP address will start with
198 * byte 0x45 and then a 8 byte field, which contains the right
199 * justified E.164 address in BCD coding, filled with a 0xf to the
200 * right. The rest of the address is zero.
201 * The function returns 0 if everything is ok, -1 in case of a wrong
205 uni_e1642nsap(u_char *nsap, const char *e164)
211 if((len = strlen(e164)) > 15 || len == 0)
213 for(i = 0; i < len; i++)
214 if(!isdigit(e164[i]))
218 fill = (15 - len) / 2;
222 *nsap++ = *e164++ - '0';
227 *nsap = (*e164++ - '0') << 4;
228 *nsap++ |= *e164 - '0';
230 *nsap++ = ((*e164++ - '0') << 4) | 0xf;
231 for(fill = 0; fill < 11; fill++)