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