summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/nsap_addr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/nsap_addr.c')
-rw-r--r--src/lib/libc/net/nsap_addr.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/lib/libc/net/nsap_addr.c b/src/lib/libc/net/nsap_addr.c
new file mode 100644
index 0000000000..58c0d5e493
--- /dev/null
+++ b/src/lib/libc/net/nsap_addr.c
@@ -0,0 +1,102 @@
1/* $OpenBSD: nsap_addr.c,v 1.5 2005/03/25 13:24:12 otto Exp $ */
2
3/*
4 * Copyright (c) 1996 by Internet Software Consortium.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 * SOFTWARE.
18 */
19
20#if defined(LIBC_SCCS) && !defined(lint)
21#if 0
22static char rcsid[] = "$From: nsap_addr.c,v 8.3 1996/08/05 08:31:35 vixie Exp $";
23#else
24static char rcsid[] = "$OpenBSD: nsap_addr.c,v 1.5 2005/03/25 13:24:12 otto Exp $";
25#endif
26#endif /* LIBC_SCCS and not lint */
27
28#include <sys/types.h>
29#include <sys/param.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/nameser.h>
33#include <ctype.h>
34#include <resolv.h>
35
36static char
37xtob(int c)
38{
39 return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
40}
41
42u_int
43inet_nsap_addr(const char *ascii, u_char *binary, int maxlen)
44{
45 u_char c, nib;
46 u_int len = 0;
47
48 while ((c = *ascii++) != '\0' && len < maxlen) {
49 if (c == '.' || c == '+' || c == '/')
50 continue;
51 if (!isascii(c))
52 return (0);
53 if (islower(c))
54 c = toupper(c);
55 if (isxdigit(c)) {
56 nib = xtob(c);
57 if ((c = *ascii++)) {
58 c = toupper(c);
59 if (isxdigit(c)) {
60 *binary++ = (nib << 4) | xtob(c);
61 len++;
62 } else
63 return (0);
64 }
65 else
66 return (0);
67 }
68 else
69 return (0);
70 }
71 return (len);
72}
73
74char *
75inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii)
76{
77 int nib;
78 int i;
79 static char tmpbuf[255*3];
80 char *start;
81
82 if (ascii)
83 start = ascii;
84 else {
85 ascii = tmpbuf;
86 start = tmpbuf;
87 }
88
89 if (binlen > 255)
90 binlen = 255;
91
92 for (i = 0; i < binlen; i++) {
93 nib = *binary >> 4;
94 *ascii++ = nib + (nib < 10 ? '0' : '7');
95 nib = *binary++ & 0x0f;
96 *ascii++ = nib + (nib < 10 ? '0' : '7');
97 if (((i % 2) == 0 && (i + 1) < binlen))
98 *ascii++ = '.';
99 }
100 *ascii = '\0';
101 return (start);
102}