nsap_addr.c revision 1.1.1.3 1 1.1.1.3 ghen /* $NetBSD: nsap_addr.c,v 1.1.1.3 2007/03/30 20:16:19 ghen 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.1.2 christos static const char rcsid[] = "Id: nsap_addr.c,v 1.3.18.2 2005/07/28 07:38:08 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.1.2 christos #include <resolv_mt.h>
37 1.1 christos
38 1.1 christos #include "port_after.h"
39 1.1 christos
40 1.1 christos static char
41 1.1 christos xtob(int c) {
42 1.1 christos return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
43 1.1 christos }
44 1.1 christos
45 1.1 christos u_int
46 1.1 christos inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
47 1.1 christos u_char c, nib;
48 1.1 christos u_int len = 0;
49 1.1 christos
50 1.1 christos if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
51 1.1 christos return (0);
52 1.1 christos ascii += 2;
53 1.1 christos
54 1.1 christos while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
55 1.1 christos if (c == '.' || c == '+' || c == '/')
56 1.1 christos continue;
57 1.1 christos if (!isascii(c))
58 1.1 christos return (0);
59 1.1 christos if (islower(c))
60 1.1 christos c = toupper(c);
61 1.1 christos if (isxdigit(c)) {
62 1.1 christos nib = xtob(c);
63 1.1 christos c = *ascii++;
64 1.1 christos if (c != '\0') {
65 1.1 christos c = toupper(c);
66 1.1 christos if (isxdigit(c)) {
67 1.1 christos *binary++ = (nib << 4) | xtob(c);
68 1.1 christos len++;
69 1.1 christos } else
70 1.1 christos return (0);
71 1.1 christos }
72 1.1 christos else
73 1.1 christos return (0);
74 1.1 christos }
75 1.1 christos else
76 1.1 christos return (0);
77 1.1 christos }
78 1.1 christos return (len);
79 1.1 christos }
80 1.1 christos
81 1.1 christos char *
82 1.1 christos inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
83 1.1 christos int nib;
84 1.1 christos int i;
85 1.1.1.2 christos char *tmpbuf = inet_nsap_ntoa_tmpbuf;
86 1.1 christos char *start;
87 1.1 christos
88 1.1 christos if (ascii)
89 1.1 christos start = ascii;
90 1.1 christos else {
91 1.1 christos ascii = tmpbuf;
92 1.1 christos start = tmpbuf;
93 1.1 christos }
94 1.1 christos
95 1.1 christos *ascii++ = '0';
96 1.1 christos *ascii++ = 'x';
97 1.1 christos
98 1.1 christos if (binlen > 255)
99 1.1 christos binlen = 255;
100 1.1 christos
101 1.1 christos for (i = 0; i < binlen; i++) {
102 1.1 christos nib = *binary >> 4;
103 1.1 christos *ascii++ = nib + (nib < 10 ? '0' : '7');
104 1.1 christos nib = *binary++ & 0x0f;
105 1.1 christos *ascii++ = nib + (nib < 10 ? '0' : '7');
106 1.1 christos if (((i % 2) == 0 && (i + 1) < binlen))
107 1.1 christos *ascii++ = '.';
108 1.1 christos }
109 1.1 christos *ascii = '\0';
110 1.1 christos return (start);
111 1.1 christos }
112 1.1.1.2 christos
113 1.1.1.2 christos /*! \file */
114