Home | History | Annotate | Line # | Download | only in inet
nsap_addr.c revision 1.5.8.1
      1  1.5.8.1       jym /*	$NetBSD: nsap_addr.c,v 1.5.8.1 2009/05/13 19:18:24 jym Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5      1.1  christos  * Copyright (c) 1996-1999 by Internet Software Consortium.
      6      1.1  christos  *
      7      1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8      1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9      1.1  christos  * copyright notice and this permission notice appear in all copies.
     10      1.1  christos  *
     11      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12      1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13      1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14      1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15      1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16      1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17      1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18      1.1  christos  */
     19      1.1  christos 
     20      1.2  christos #include <sys/cdefs.h>
     21      1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     22      1.2  christos #if 0
     23      1.5  christos static const char rcsid[] = "Id: nsap_addr.c,v 1.5 2005/07/28 06:51:48 marka Exp";
     24      1.2  christos #else
     25  1.5.8.1       jym __RCSID("$NetBSD: nsap_addr.c,v 1.5.8.1 2009/05/13 19:18:24 jym Exp $");
     26      1.2  christos #endif
     27      1.1  christos #endif /* LIBC_SCCS and not lint */
     28      1.1  christos 
     29      1.1  christos #include "port_before.h"
     30      1.1  christos 
     31      1.2  christos #include "namespace.h"
     32      1.1  christos #include <sys/types.h>
     33      1.1  christos #include <sys/param.h>
     34      1.1  christos #include <sys/socket.h>
     35      1.1  christos 
     36      1.1  christos #include <netinet/in.h>
     37      1.1  christos #include <arpa/inet.h>
     38      1.1  christos #include <arpa/nameser.h>
     39      1.1  christos 
     40      1.2  christos #include <assert.h>
     41      1.1  christos #include <ctype.h>
     42      1.1  christos #include <resolv.h>
     43      1.3  christos #include <resolv_mt.h>
     44      1.1  christos 
     45      1.1  christos #include "port_after.h"
     46      1.1  christos 
     47      1.2  christos #ifdef __weak_alias
     48      1.2  christos __weak_alias(inet_nsap_addr,_inet_nsap_addr)
     49      1.2  christos __weak_alias(inet_nsap_ntoa,_inet_nsap_ntoa)
     50      1.2  christos #endif
     51      1.2  christos 
     52      1.1  christos static char
     53      1.1  christos xtob(int c) {
     54      1.1  christos 	return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
     55      1.1  christos }
     56      1.1  christos 
     57      1.1  christos u_int
     58      1.1  christos inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
     59      1.1  christos 	u_char c, nib;
     60      1.1  christos 	u_int len = 0;
     61      1.1  christos 
     62      1.2  christos 	_DIAGASSERT(ascii != NULL);
     63      1.2  christos 	_DIAGASSERT(binary != NULL);
     64      1.2  christos 
     65      1.1  christos 	if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
     66      1.1  christos 		return (0);
     67      1.1  christos 	ascii += 2;
     68      1.1  christos 
     69      1.1  christos 	while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
     70      1.1  christos 		if (c == '.' || c == '+' || c == '/')
     71      1.1  christos 			continue;
     72      1.1  christos 		if (!isascii(c))
     73      1.1  christos 			return (0);
     74      1.1  christos 		if (islower(c))
     75      1.1  christos 			c = toupper(c);
     76      1.1  christos 		if (isxdigit(c)) {
     77      1.1  christos 			nib = xtob(c);
     78      1.1  christos 			c = *ascii++;
     79      1.1  christos 			if (c != '\0') {
     80      1.1  christos 				c = toupper(c);
     81      1.1  christos 				if (isxdigit(c)) {
     82      1.1  christos 					*binary++ = (nib << 4) | xtob(c);
     83      1.1  christos 					len++;
     84      1.1  christos 				} else
     85      1.1  christos 					return (0);
     86      1.1  christos 			}
     87      1.1  christos 			else
     88      1.1  christos 				return (0);
     89      1.1  christos 		}
     90      1.1  christos 		else
     91      1.1  christos 			return (0);
     92      1.1  christos 	}
     93      1.1  christos 	return (len);
     94      1.1  christos }
     95      1.1  christos 
     96      1.1  christos char *
     97      1.1  christos inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
     98      1.1  christos 	int nib;
     99      1.1  christos 	int i;
    100      1.3  christos 	char *tmpbuf = inet_nsap_ntoa_tmpbuf;
    101      1.1  christos 	char *start;
    102      1.1  christos 
    103      1.2  christos 	_DIAGASSERT(binary != NULL);
    104      1.2  christos 
    105      1.1  christos 	if (ascii)
    106      1.1  christos 		start = ascii;
    107      1.1  christos 	else {
    108      1.1  christos 		ascii = tmpbuf;
    109      1.1  christos 		start = tmpbuf;
    110      1.1  christos 	}
    111      1.1  christos 
    112      1.1  christos 	*ascii++ = '0';
    113      1.1  christos 	*ascii++ = 'x';
    114      1.1  christos 
    115      1.1  christos 	if (binlen > 255)
    116      1.1  christos 		binlen = 255;
    117      1.1  christos 
    118      1.1  christos 	for (i = 0; i < binlen; i++) {
    119      1.2  christos 		nib = (u_int32_t)*binary >> 4;
    120      1.1  christos 		*ascii++ = nib + (nib < 10 ? '0' : '7');
    121      1.1  christos 		nib = *binary++ & 0x0f;
    122      1.1  christos 		*ascii++ = nib + (nib < 10 ? '0' : '7');
    123      1.1  christos 		if (((i % 2) == 0 && (i + 1) < binlen))
    124      1.1  christos 			*ascii++ = '.';
    125      1.1  christos 	}
    126      1.1  christos 	*ascii = '\0';
    127      1.1  christos 	return (start);
    128      1.1  christos }
    129      1.3  christos 
    130      1.3  christos /*! \file */
    131