summaryrefslogtreecommitdiffstats
path: root/sys/contrib/ngatm/netnatm/misc/straddr.c
blob: 78207aa009f61ecde76d049bbd346f8461b3ea13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright (c) 1996-2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 * 	All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Author: Hartmut Brandt <harti@freebsd.org>
 *
 * $Begemot: libunimsg/atm/misc/straddr.c,v 1.3 2003/09/19 12:05:45 hbb Exp $
 */

#include <sys/types.h>
#ifdef _KERNEL
#include <sys/ctype.h>
#include <sys/libkern.h>
#else
#include <ctype.h>
#include <string.h>
#endif
#include <netnatm/addr.h>

/*
 * Convert an NSAP address from the ASCII format to the binary.
 * ASCII format means each byte formatted as a 2-byte hex number
 * with dots freely interspersed between the bytes.
 * If the conversion is succesful, the function returns 0, -1
 * on conversion errors.
 */
int
uni_str2nsap(u_char *out, const char *in)
{
	int i;
	int c;

	for(i = 0; i < 20; i++) {
		while((c = *in++) == '.')
			;
		if(!isascii(c) || !isxdigit(c))
			return -1;
		out[i] = isdigit(c) ? (c - '0')
			: islower(c) ? (c - 'a' + 10)
			: (c - 'A' + 10);
		out[i] <<= 4;
		c = *in++;
		if(!isascii(c) || !isxdigit(c))
			return -1;
		out[i] |= isdigit(c) ? (c - '0')
			: islower(c) ? (c - 'a' + 10)
			: (c - 'A' + 10);
	}
	return *in != '\0';
}

/*
 * Parse an emebedded E.164 NSAP address.
 * If check is 0, the contents of the last 11 bytes are ignored
 * If check is 1, the contents of all of these but the selector byte
 * are checked to be zero. If check is 2 all 11 bytes must be 0.
 */
int
uni_nsap2e164(char *e164, const u_char *nsap, int check)
{
	char *p = e164;
	u_int d;
	int i;

	if(nsap[0] != 0x45)
		return -1;
	if((nsap[8] & 0xf) != 0xf)
		return -1;
	for(i = 1; i <= 7; i++) {
		d = (nsap[i] >> 4) & 0xf;
		if(d == 0x00 && p == e164)
			continue;
		if(d >= 0xa)
			return -1;
		*p++ = d + '0';

		d = nsap[i] & 0xf;
		if(d == 0x00 && p == e164)
			continue;
		if(d >= 0xa)
			return -1;
		*p++ = d + '0';
	}
	d = (nsap[i] >> 4) & 0xf;
	if(d != 0x00 || p == e164) {
		if(d >= 0xa)
			return -1;
		*p++ = d + '0';
	}
	if(p == e164)
		return -1;
	*p++ = 0;

	if(check == 0)
		return 0;
	while(i < ((check == 1) ? 19 : 20)) {
		if(nsap[i] != 0x00)
			return -1;
		i++;
	}

	return 0;
}

/*
 * Convert a binary representation to ASCII. The standard formats are
 * recognized and dotted. Non-standard formats get no dots altogether.
 */
void
uni_prefix2str(char *out, const u_char *in, u_int len, int dotit)
{
	static char hex[16] = "0123456789abcdef";
	static int fmt[3][6] = {
		{ 1, 2, 10, 6, 1, 0 },
		{ 1, 2, 10, 6, 1, 0 },
		{ 1, 8,  4, 6, 1, 0 },
	};
	int f, b;
	u_int i;

	if (len > 20)
		len = 20;

	if(dotit) {
		switch(*in) {

		  case 0x39:	/* DCC */
			i = 0;
  fmt:
			for(f = 0; fmt[i][f]; f++) {
				if (len == 0)
					goto done;
				if(f != 0)
					*out++ = '.';
				for(b = 0; b < fmt[i][f]; b++) {
					if (len-- == 0)
						goto done;
					*out++ = hex[(*in >> 4) & 0xf];
					*out++ = hex[*in & 0xf];
					in++;
				}
			}
  done:
			*out = '\0';
			return;

		  case 0x47:	/* ICD */
			i = 1;
			goto fmt;

		  case 0x45:	/* E.164 */
			i = 2;
			goto fmt;
		}
	}

	/* undotted */
	for(i = 0; i < len; i++) {
		*out++ = hex[(*in >> 4) & 0xf];
		*out++ = hex[*in & 0xf];
		in++;
	}
	*out = '\0';
}

void
uni_nsap2str(char *out, const u_char *in, int dotit)
{
	uni_prefix2str(out, in, 20, dotit);
}

/*
 * Make an embedded E.164 NSAP address from a NSAP address.
 * The E.164 address is a string of digits, at least one digit and
 * not more than 15 digits long. The NSAP address will start with
 * byte 0x45 and then a 8 byte field, which contains the right
 * justified E.164 address in BCD coding, filled with a 0xf to the
 * right. The rest of the address is zero.
 * The function returns 0 if everything is ok, -1 in case of a wrong
 * E.164 address.
 */
int
uni_e1642nsap(u_char *nsap, const char *e164)
{
	size_t len;
	int fill;
	u_int i;

	if((len = strlen(e164)) > 15 || len == 0)
		return -1;
	for(i = 0; i < len; i++)
		if(!isdigit(e164[i]))
			return -1;

	*nsap++ = 0x45;
	fill = (15 - len) / 2;
	while(fill--)
		*nsap++ = 0x00;
	if((len & 1) == 0) {
		*nsap++ = *e164++ - '0';
		len--;
	}
	while(len > 1) {
		len -= 2;
		*nsap = (*e164++ - '0') << 4;
		*nsap++ |= *e164 - '0';
	}
	*nsap++ = ((*e164++ - '0') << 4) | 0xf;
	for(fill = 0; fill < 11; fill++)
		*nsap++ = 0;

	return 0;
}
OpenPOWER on IntegriCloud