17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: nsap_addr.c,v 1.5 2005/07/28 06:51:48 marka Exp $";
207c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include "port_before.h"
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include <sys/types.h>
257c478bd9Sstevel@tonic-gate #include <sys/param.h>
267c478bd9Sstevel@tonic-gate #include <sys/socket.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <netinet/in.h>
297c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
307c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <resolv.h>
34*9525b14bSRao Shoaib #include <resolv_mt.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "port_after.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static char
xtob(int c)397c478bd9Sstevel@tonic-gate xtob(int c) {
407c478bd9Sstevel@tonic-gate 	return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
417c478bd9Sstevel@tonic-gate }
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate u_int
inet_nsap_addr(const char * ascii,u_char * binary,int maxlen)447c478bd9Sstevel@tonic-gate inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
457c478bd9Sstevel@tonic-gate 	u_char c, nib;
467c478bd9Sstevel@tonic-gate 	u_int len = 0;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
497c478bd9Sstevel@tonic-gate 		return (0);
507c478bd9Sstevel@tonic-gate 	ascii += 2;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
537c478bd9Sstevel@tonic-gate 		if (c == '.' || c == '+' || c == '/')
547c478bd9Sstevel@tonic-gate 			continue;
557c478bd9Sstevel@tonic-gate 		if (!isascii(c))
567c478bd9Sstevel@tonic-gate 			return (0);
577c478bd9Sstevel@tonic-gate 		if (islower(c))
587c478bd9Sstevel@tonic-gate 			c = toupper(c);
597c478bd9Sstevel@tonic-gate 		if (isxdigit(c)) {
607c478bd9Sstevel@tonic-gate 			nib = xtob(c);
617c478bd9Sstevel@tonic-gate 			c = *ascii++;
627c478bd9Sstevel@tonic-gate 			if (c != '\0') {
637c478bd9Sstevel@tonic-gate 				c = toupper(c);
647c478bd9Sstevel@tonic-gate 				if (isxdigit(c)) {
657c478bd9Sstevel@tonic-gate 					*binary++ = (nib << 4) | xtob(c);
667c478bd9Sstevel@tonic-gate 					len++;
677c478bd9Sstevel@tonic-gate 				} else
687c478bd9Sstevel@tonic-gate 					return (0);
697c478bd9Sstevel@tonic-gate 			}
707c478bd9Sstevel@tonic-gate 			else
717c478bd9Sstevel@tonic-gate 				return (0);
727c478bd9Sstevel@tonic-gate 		}
737c478bd9Sstevel@tonic-gate 		else
747c478bd9Sstevel@tonic-gate 			return (0);
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 	return (len);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate char *
inet_nsap_ntoa(int binlen,const u_char * binary,char * ascii)807c478bd9Sstevel@tonic-gate inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
817c478bd9Sstevel@tonic-gate 	int nib;
827c478bd9Sstevel@tonic-gate 	int i;
83*9525b14bSRao Shoaib 	char *tmpbuf = inet_nsap_ntoa_tmpbuf;
847c478bd9Sstevel@tonic-gate 	char *start;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (ascii)
877c478bd9Sstevel@tonic-gate 		start = ascii;
887c478bd9Sstevel@tonic-gate 	else {
897c478bd9Sstevel@tonic-gate 		ascii = tmpbuf;
907c478bd9Sstevel@tonic-gate 		start = tmpbuf;
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	*ascii++ = '0';
947c478bd9Sstevel@tonic-gate 	*ascii++ = 'x';
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (binlen > 255)
977c478bd9Sstevel@tonic-gate 		binlen = 255;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	for (i = 0; i < binlen; i++) {
1007c478bd9Sstevel@tonic-gate 		nib = *binary >> 4;
1017c478bd9Sstevel@tonic-gate 		*ascii++ = nib + (nib < 10 ? '0' : '7');
1027c478bd9Sstevel@tonic-gate 		nib = *binary++ & 0x0f;
1037c478bd9Sstevel@tonic-gate 		*ascii++ = nib + (nib < 10 ? '0' : '7');
1047c478bd9Sstevel@tonic-gate 		if (((i % 2) == 0 && (i + 1) < binlen))
1057c478bd9Sstevel@tonic-gate 			*ascii++ = '.';
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	*ascii = '\0';
1087c478bd9Sstevel@tonic-gate 	return (start);
1097c478bd9Sstevel@tonic-gate }
110*9525b14bSRao Shoaib 
111*9525b14bSRao Shoaib /*! \file */
112