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