1 /*- 2 * SPDX-License-Identifier: ISC 3 * 4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (c) 1996-1999 by Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include "port_before.h" 21 22 #include <sys/param.h> 23 #include <sys/socket.h> 24 25 #include <netinet/in.h> 26 #include <arpa/inet.h> 27 #include <arpa/nameser.h> 28 29 #include <ctype.h> 30 #include <resolv.h> 31 #include <resolv_mt.h> 32 33 #include "port_after.h" 34 35 static char 36 xtob(int c) { 37 return (c - (((c >= '0') && (c <= '9')) ? '0' : '7')); 38 } 39 40 u_int 41 inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) { 42 u_char c, nib; 43 u_int len = 0; 44 45 if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X')) 46 return (0); 47 ascii += 2; 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 = toupper(c); 56 if (isxdigit(c)) { 57 nib = xtob(c); 58 c = *ascii++; 59 if (c != '\0') { 60 c = 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(int binlen, const u_char *binary, char *ascii) { 78 int nib; 79 int i; 80 char *tmpbuf = inet_nsap_ntoa_tmpbuf; 81 char *start; 82 83 if (ascii) 84 start = ascii; 85 else { 86 ascii = tmpbuf; 87 start = tmpbuf; 88 } 89 90 *ascii++ = '0'; 91 *ascii++ = 'x'; 92 93 if (binlen > 255) 94 binlen = 255; 95 96 for (i = 0; i < binlen; i++) { 97 nib = *binary >> 4; 98 *ascii++ = nib + (nib < 10 ? '0' : '7'); 99 nib = *binary++ & 0x0f; 100 *ascii++ = nib + (nib < 10 ? '0' : '7'); 101 if (((i % 2) == 0 && (i + 1) < binlen)) 102 *ascii++ = '.'; 103 } 104 *ascii = '\0'; 105 return (start); 106 } 107 108 /* 109 * Weak aliases for applications that use certain private entry points, 110 * and fail to include <arpa/inet.h>. 111 */ 112 #undef inet_nsap_addr 113 __weak_reference(__inet_nsap_addr, inet_nsap_addr); 114 #undef inet_nsap_ntoa 115 __weak_reference(__inet_nsap_ntoa, inet_nsap_ntoa); 116 117 /*! \file */ 118