xref: /haiku/src/system/libnetwork/netresolv/inet/nsap_addr.c (revision aa6411e23e78610d8eecaa4de48b6c71c994fbf3)
1*aa6411e2SAugustin Cavalier /*	$NetBSD: nsap_addr.c,v 1.6 2009/04/12 17:07:17 christos Exp $	*/
2*aa6411e2SAugustin Cavalier 
3*aa6411e2SAugustin Cavalier /*
4*aa6411e2SAugustin Cavalier  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5*aa6411e2SAugustin Cavalier  * Copyright (c) 1996-1999 by Internet Software Consortium.
6*aa6411e2SAugustin Cavalier  *
7*aa6411e2SAugustin Cavalier  * Permission to use, copy, modify, and distribute this software for any
8*aa6411e2SAugustin Cavalier  * purpose with or without fee is hereby granted, provided that the above
9*aa6411e2SAugustin Cavalier  * copyright notice and this permission notice appear in all copies.
10*aa6411e2SAugustin Cavalier  *
11*aa6411e2SAugustin Cavalier  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12*aa6411e2SAugustin Cavalier  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13*aa6411e2SAugustin Cavalier  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14*aa6411e2SAugustin Cavalier  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15*aa6411e2SAugustin Cavalier  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16*aa6411e2SAugustin Cavalier  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17*aa6411e2SAugustin Cavalier  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*aa6411e2SAugustin Cavalier  */
19*aa6411e2SAugustin Cavalier 
20*aa6411e2SAugustin Cavalier #include <sys/cdefs.h>
21*aa6411e2SAugustin Cavalier #if defined(LIBC_SCCS) && !defined(lint)
22*aa6411e2SAugustin Cavalier #if 0
23*aa6411e2SAugustin Cavalier static const char rcsid[] = "Id: nsap_addr.c,v 1.5 2005/07/28 06:51:48 marka Exp";
24*aa6411e2SAugustin Cavalier #else
25*aa6411e2SAugustin Cavalier __RCSID("$NetBSD: nsap_addr.c,v 1.6 2009/04/12 17:07:17 christos Exp $");
26*aa6411e2SAugustin Cavalier #endif
27*aa6411e2SAugustin Cavalier #endif /* LIBC_SCCS and not lint */
28*aa6411e2SAugustin Cavalier 
29*aa6411e2SAugustin Cavalier #include "port_before.h"
30*aa6411e2SAugustin Cavalier 
31*aa6411e2SAugustin Cavalier #include <sys/types.h>
32*aa6411e2SAugustin Cavalier #include <sys/param.h>
33*aa6411e2SAugustin Cavalier #include <sys/socket.h>
34*aa6411e2SAugustin Cavalier 
35*aa6411e2SAugustin Cavalier #include <netinet/in.h>
36*aa6411e2SAugustin Cavalier #include <arpa/inet.h>
37*aa6411e2SAugustin Cavalier #include <arpa/nameser.h>
38*aa6411e2SAugustin Cavalier 
39*aa6411e2SAugustin Cavalier #include <assert.h>
40*aa6411e2SAugustin Cavalier #include <ctype.h>
41*aa6411e2SAugustin Cavalier #include <resolv.h>
42*aa6411e2SAugustin Cavalier #include <resolv_mt.h>
43*aa6411e2SAugustin Cavalier 
44*aa6411e2SAugustin Cavalier #include "port_after.h"
45*aa6411e2SAugustin Cavalier 
46*aa6411e2SAugustin Cavalier static char
xtob(int c)47*aa6411e2SAugustin Cavalier xtob(int c) {
48*aa6411e2SAugustin Cavalier 	return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
49*aa6411e2SAugustin Cavalier }
50*aa6411e2SAugustin Cavalier 
51*aa6411e2SAugustin Cavalier u_int
inet_nsap_addr(const char * ascii,u_char * binary,int maxlen)52*aa6411e2SAugustin Cavalier inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
53*aa6411e2SAugustin Cavalier 	u_char c, nib;
54*aa6411e2SAugustin Cavalier 	u_int len = 0;
55*aa6411e2SAugustin Cavalier 
56*aa6411e2SAugustin Cavalier 	assert(ascii != NULL);
57*aa6411e2SAugustin Cavalier 	assert(binary != NULL);
58*aa6411e2SAugustin Cavalier 
59*aa6411e2SAugustin Cavalier 	if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
60*aa6411e2SAugustin Cavalier 		return (0);
61*aa6411e2SAugustin Cavalier 	ascii += 2;
62*aa6411e2SAugustin Cavalier 
63*aa6411e2SAugustin Cavalier 	while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
64*aa6411e2SAugustin Cavalier 		if (c == '.' || c == '+' || c == '/')
65*aa6411e2SAugustin Cavalier 			continue;
66*aa6411e2SAugustin Cavalier 		if (!isascii(c))
67*aa6411e2SAugustin Cavalier 			return (0);
68*aa6411e2SAugustin Cavalier 		if (islower(c))
69*aa6411e2SAugustin Cavalier 			c = toupper(c);
70*aa6411e2SAugustin Cavalier 		if (isxdigit(c)) {
71*aa6411e2SAugustin Cavalier 			nib = xtob(c);
72*aa6411e2SAugustin Cavalier 			c = *ascii++;
73*aa6411e2SAugustin Cavalier 			if (c != '\0') {
74*aa6411e2SAugustin Cavalier 				c = toupper(c);
75*aa6411e2SAugustin Cavalier 				if (isxdigit(c)) {
76*aa6411e2SAugustin Cavalier 					*binary++ = (nib << 4) | xtob(c);
77*aa6411e2SAugustin Cavalier 					len++;
78*aa6411e2SAugustin Cavalier 				} else
79*aa6411e2SAugustin Cavalier 					return (0);
80*aa6411e2SAugustin Cavalier 			}
81*aa6411e2SAugustin Cavalier 			else
82*aa6411e2SAugustin Cavalier 				return (0);
83*aa6411e2SAugustin Cavalier 		}
84*aa6411e2SAugustin Cavalier 		else
85*aa6411e2SAugustin Cavalier 			return (0);
86*aa6411e2SAugustin Cavalier 	}
87*aa6411e2SAugustin Cavalier 	return (len);
88*aa6411e2SAugustin Cavalier }
89*aa6411e2SAugustin Cavalier 
90*aa6411e2SAugustin Cavalier char *
inet_nsap_ntoa(int binlen,const u_char * binary,char * ascii)91*aa6411e2SAugustin Cavalier inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
92*aa6411e2SAugustin Cavalier 	int nib;
93*aa6411e2SAugustin Cavalier 	int i;
94*aa6411e2SAugustin Cavalier 	char *tmpbuf = inet_nsap_ntoa_tmpbuf;
95*aa6411e2SAugustin Cavalier 	char *start;
96*aa6411e2SAugustin Cavalier 
97*aa6411e2SAugustin Cavalier 	assert(binary != NULL);
98*aa6411e2SAugustin Cavalier 
99*aa6411e2SAugustin Cavalier 	if (ascii)
100*aa6411e2SAugustin Cavalier 		start = ascii;
101*aa6411e2SAugustin Cavalier 	else {
102*aa6411e2SAugustin Cavalier 		ascii = tmpbuf;
103*aa6411e2SAugustin Cavalier 		start = tmpbuf;
104*aa6411e2SAugustin Cavalier 	}
105*aa6411e2SAugustin Cavalier 
106*aa6411e2SAugustin Cavalier 	*ascii++ = '0';
107*aa6411e2SAugustin Cavalier 	*ascii++ = 'x';
108*aa6411e2SAugustin Cavalier 
109*aa6411e2SAugustin Cavalier 	if (binlen > 255)
110*aa6411e2SAugustin Cavalier 		binlen = 255;
111*aa6411e2SAugustin Cavalier 
112*aa6411e2SAugustin Cavalier 	for (i = 0; i < binlen; i++) {
113*aa6411e2SAugustin Cavalier 		nib = (u_int32_t)*binary >> 4;
114*aa6411e2SAugustin Cavalier 		*ascii++ = nib + (nib < 10 ? '0' : '7');
115*aa6411e2SAugustin Cavalier 		nib = *binary++ & 0x0f;
116*aa6411e2SAugustin Cavalier 		*ascii++ = nib + (nib < 10 ? '0' : '7');
117*aa6411e2SAugustin Cavalier 		if (((i % 2) == 0 && (i + 1) < binlen))
118*aa6411e2SAugustin Cavalier 			*ascii++ = '.';
119*aa6411e2SAugustin Cavalier 	}
120*aa6411e2SAugustin Cavalier 	*ascii = '\0';
121*aa6411e2SAugustin Cavalier 	return (start);
122*aa6411e2SAugustin Cavalier }
123*aa6411e2SAugustin Cavalier 
124*aa6411e2SAugustin Cavalier #undef inet_nsap_addr
125*aa6411e2SAugustin Cavalier #pragma weak inet_nsap_addr = __inet_nsap_addr
126*aa6411e2SAugustin Cavalier #undef inet_nsap_ntoa
127*aa6411e2SAugustin Cavalier #pragma weak inet_nsap_ntoa = __inet_nsap_ntoa
128