nsap_addr.c revision 1.1.1.1 1 1.1 christos /* $NetBSD: nsap_addr.c,v 1.1.1.1 2004/05/20 22:29:02 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.1 christos #if defined(LIBC_SCCS) && !defined(lint)
21 1.1 christos static const char rcsid[] = "Id: nsap_addr.c,v 1.2.206.1 2004/03/09 08:33:33 marka Exp";
22 1.1 christos #endif /* LIBC_SCCS and not lint */
23 1.1 christos
24 1.1 christos #include "port_before.h"
25 1.1 christos
26 1.1 christos #include <sys/types.h>
27 1.1 christos #include <sys/param.h>
28 1.1 christos #include <sys/socket.h>
29 1.1 christos
30 1.1 christos #include <netinet/in.h>
31 1.1 christos #include <arpa/inet.h>
32 1.1 christos #include <arpa/nameser.h>
33 1.1 christos
34 1.1 christos #include <ctype.h>
35 1.1 christos #include <resolv.h>
36 1.1 christos
37 1.1 christos #include "port_after.h"
38 1.1 christos
39 1.1 christos static char
40 1.1 christos xtob(int c) {
41 1.1 christos return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
42 1.1 christos }
43 1.1 christos
44 1.1 christos u_int
45 1.1 christos inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
46 1.1 christos u_char c, nib;
47 1.1 christos u_int len = 0;
48 1.1 christos
49 1.1 christos if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
50 1.1 christos return (0);
51 1.1 christos ascii += 2;
52 1.1 christos
53 1.1 christos while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
54 1.1 christos if (c == '.' || c == '+' || c == '/')
55 1.1 christos continue;
56 1.1 christos if (!isascii(c))
57 1.1 christos return (0);
58 1.1 christos if (islower(c))
59 1.1 christos c = toupper(c);
60 1.1 christos if (isxdigit(c)) {
61 1.1 christos nib = xtob(c);
62 1.1 christos c = *ascii++;
63 1.1 christos if (c != '\0') {
64 1.1 christos c = toupper(c);
65 1.1 christos if (isxdigit(c)) {
66 1.1 christos *binary++ = (nib << 4) | xtob(c);
67 1.1 christos len++;
68 1.1 christos } else
69 1.1 christos return (0);
70 1.1 christos }
71 1.1 christos else
72 1.1 christos return (0);
73 1.1 christos }
74 1.1 christos else
75 1.1 christos return (0);
76 1.1 christos }
77 1.1 christos return (len);
78 1.1 christos }
79 1.1 christos
80 1.1 christos char *
81 1.1 christos inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
82 1.1 christos int nib;
83 1.1 christos int i;
84 1.1 christos static char tmpbuf[2+255*3];
85 1.1 christos char *start;
86 1.1 christos
87 1.1 christos if (ascii)
88 1.1 christos start = ascii;
89 1.1 christos else {
90 1.1 christos ascii = tmpbuf;
91 1.1 christos start = tmpbuf;
92 1.1 christos }
93 1.1 christos
94 1.1 christos *ascii++ = '0';
95 1.1 christos *ascii++ = 'x';
96 1.1 christos
97 1.1 christos if (binlen > 255)
98 1.1 christos binlen = 255;
99 1.1 christos
100 1.1 christos for (i = 0; i < binlen; i++) {
101 1.1 christos nib = *binary >> 4;
102 1.1 christos *ascii++ = nib + (nib < 10 ? '0' : '7');
103 1.1 christos nib = *binary++ & 0x0f;
104 1.1 christos *ascii++ = nib + (nib < 10 ? '0' : '7');
105 1.1 christos if (((i % 2) == 0 && (i + 1) < binlen))
106 1.1 christos *ascii++ = '.';
107 1.1 christos }
108 1.1 christos *ascii = '\0';
109 1.1 christos return (start);
110 1.1 christos }
111