diff options
Diffstat (limited to 'src/lib/libc/net/nsap_addr.c')
-rw-r--r-- | src/lib/libc/net/nsap_addr.c | 109 |
1 files changed, 109 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..22a5f8d66e --- /dev/null +++ b/src/lib/libc/net/nsap_addr.c | |||
@@ -0,0 +1,109 @@ | |||
1 | /* $OpenBSD: nsap_addr.c,v 1.4 1997/07/09 01:08:45 millert 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 | ||
22 | static char rcsid[] = "$From: nsap_addr.c,v 8.3 1996/08/05 08:31:35 vixie Exp $"; | ||
23 | #else | ||
24 | static char rcsid[] = "$OpenBSD: nsap_addr.c,v 1.4 1997/07/09 01:08:45 millert 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 | |||
36 | static char | ||
37 | xtob(c) | ||
38 | register int c; | ||
39 | { | ||
40 | return (c - (((c >= '0') && (c <= '9')) ? '0' : '7')); | ||
41 | } | ||
42 | |||
43 | u_int | ||
44 | inet_nsap_addr(ascii, binary, maxlen) | ||
45 | const char *ascii; | ||
46 | u_char *binary; | ||
47 | int maxlen; | ||
48 | { | ||
49 | register u_char c, nib; | ||
50 | u_int len = 0; | ||
51 | |||
52 | while ((c = *ascii++) != '\0' && len < maxlen) { | ||
53 | if (c == '.' || c == '+' || c == '/') | ||
54 | continue; | ||
55 | if (!isascii(c)) | ||
56 | return (0); | ||
57 | if (islower(c)) | ||
58 | c = toupper(c); | ||
59 | if (isxdigit(c)) { | ||
60 | nib = xtob(c); | ||
61 | if ((c = *ascii++)) { | ||
62 | c = toupper(c); | ||
63 | if (isxdigit(c)) { | ||
64 | *binary++ = (nib << 4) | xtob(c); | ||
65 | len++; | ||
66 | } else | ||
67 | return (0); | ||
68 | } | ||
69 | else | ||
70 | return (0); | ||
71 | } | ||
72 | else | ||
73 | return (0); | ||
74 | } | ||
75 | return (len); | ||
76 | } | ||
77 | |||
78 | char * | ||
79 | inet_nsap_ntoa(binlen, binary, ascii) | ||
80 | int binlen; | ||
81 | register const u_char *binary; | ||
82 | register char *ascii; | ||
83 | { | ||
84 | register int nib; | ||
85 | int i; | ||
86 | static char tmpbuf[255*3]; | ||
87 | char *start; | ||
88 | |||
89 | if (ascii) | ||
90 | start = ascii; | ||
91 | else { | ||
92 | ascii = tmpbuf; | ||
93 | start = tmpbuf; | ||
94 | } | ||
95 | |||
96 | if (binlen > 255) | ||
97 | binlen = 255; | ||
98 | |||
99 | for (i = 0; i < binlen; i++) { | ||
100 | nib = *binary >> 4; | ||
101 | *ascii++ = nib + (nib < 10 ? '0' : '7'); | ||
102 | nib = *binary++ & 0x0f; | ||
103 | *ascii++ = nib + (nib < 10 ? '0' : '7'); | ||
104 | if (((i % 2) == 0 && (i + 1) < binlen)) | ||
105 | *ascii++ = '.'; | ||
106 | } | ||
107 | *ascii = '\0'; | ||
108 | return (start); | ||
109 | } | ||