1156952Sume/* 2156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3156952Sume * Copyright (c) 1996-1999 by Internet Software Consortium. 4156952Sume * 5156952Sume * Permission to use, copy, modify, and distribute this software for any 6156952Sume * purpose with or without fee is hereby granted, provided that the above 7156952Sume * copyright notice and this permission notice appear in all copies. 8156952Sume * 9156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11156952Sume * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16156952Sume */ 17156952Sume 18156952Sume#if defined(LIBC_SCCS) && !defined(lint) 19269867Sumestatic const char rcsid[] = "$Id: nsap_addr.c,v 1.5 2005/07/28 06:51:48 marka Exp $"; 20156952Sume#endif /* LIBC_SCCS and not lint */ 21156956Sume#include <sys/cdefs.h> 22156956Sume__FBSDID("$FreeBSD$"); 23156952Sume 24156952Sume#include "port_before.h" 25156952Sume 26156952Sume#include <sys/param.h> 27156952Sume#include <sys/socket.h> 28156952Sume 29156952Sume#include <netinet/in.h> 30156952Sume#include <arpa/inet.h> 31156952Sume#include <arpa/nameser.h> 32156952Sume 33156952Sume#include <ctype.h> 34156952Sume#include <resolv.h> 35156952Sume#include <resolv_mt.h> 36156952Sume 37156952Sume#include "port_after.h" 38156952Sume 39156952Sumestatic char 40156952Sumextob(int c) { 41156952Sume return (c - (((c >= '0') && (c <= '9')) ? '0' : '7')); 42156952Sume} 43156952Sume 44156952Sumeu_int 45156952Sumeinet_nsap_addr(const char *ascii, u_char *binary, int maxlen) { 46156952Sume u_char c, nib; 47156952Sume u_int len = 0; 48156952Sume 49156952Sume if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X')) 50156952Sume return (0); 51156952Sume ascii += 2; 52156952Sume 53156952Sume while ((c = *ascii++) != '\0' && len < (u_int)maxlen) { 54156952Sume if (c == '.' || c == '+' || c == '/') 55156952Sume continue; 56156952Sume if (!isascii(c)) 57156952Sume return (0); 58156952Sume if (islower(c)) 59156952Sume c = toupper(c); 60156952Sume if (isxdigit(c)) { 61156952Sume nib = xtob(c); 62156952Sume c = *ascii++; 63156952Sume if (c != '\0') { 64156952Sume c = toupper(c); 65156952Sume if (isxdigit(c)) { 66156952Sume *binary++ = (nib << 4) | xtob(c); 67156952Sume len++; 68156952Sume } else 69156952Sume return (0); 70156952Sume } 71156952Sume else 72156952Sume return (0); 73156952Sume } 74156952Sume else 75156952Sume return (0); 76156952Sume } 77156952Sume return (len); 78156952Sume} 79156952Sume 80156952Sumechar * 81156952Sumeinet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) { 82156952Sume int nib; 83156952Sume int i; 84156952Sume char *tmpbuf = inet_nsap_ntoa_tmpbuf; 85156952Sume char *start; 86156952Sume 87156952Sume if (ascii) 88156952Sume start = ascii; 89156952Sume else { 90156952Sume ascii = tmpbuf; 91156952Sume start = tmpbuf; 92156952Sume } 93156952Sume 94156952Sume *ascii++ = '0'; 95156952Sume *ascii++ = 'x'; 96156952Sume 97156952Sume if (binlen > 255) 98156952Sume binlen = 255; 99156952Sume 100156952Sume for (i = 0; i < binlen; i++) { 101156952Sume nib = *binary >> 4; 102156952Sume *ascii++ = nib + (nib < 10 ? '0' : '7'); 103156952Sume nib = *binary++ & 0x0f; 104156952Sume *ascii++ = nib + (nib < 10 ? '0' : '7'); 105156952Sume if (((i % 2) == 0 && (i + 1) < binlen)) 106156952Sume *ascii++ = '.'; 107156952Sume } 108156952Sume *ascii = '\0'; 109156952Sume return (start); 110156952Sume} 111156956Sume 112156956Sume/* 113156956Sume * Weak aliases for applications that use certain private entry points, 114156956Sume * and fail to include <arpa/inet.h>. 115156956Sume */ 116156956Sume#undef inet_nsap_addr 117156956Sume__weak_reference(__inet_nsap_addr, inet_nsap_addr); 118156956Sume#undef inet_nsap_ntoa 119156956Sume__weak_reference(__inet_nsap_ntoa, inet_nsap_ntoa); 120170244Sume 121170244Sume/*! \file */ 122