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