10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate * Copyright (c) 1996-1999 by Internet Software Consortium.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate *
9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate */
170Sstevel@tonic-gate
180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: nsap_addr.c,v 1.5 2005/07/28 06:51:48 marka Exp $";
200Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
210Sstevel@tonic-gate
220Sstevel@tonic-gate #include "port_before.h"
230Sstevel@tonic-gate
240Sstevel@tonic-gate #include <sys/types.h>
250Sstevel@tonic-gate #include <sys/param.h>
260Sstevel@tonic-gate #include <sys/socket.h>
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <netinet/in.h>
290Sstevel@tonic-gate #include <arpa/inet.h>
300Sstevel@tonic-gate #include <arpa/nameser.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <ctype.h>
330Sstevel@tonic-gate #include <resolv.h>
34*11038SRao.Shoaib@Sun.COM #include <resolv_mt.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include "port_after.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate static char
xtob(int c)390Sstevel@tonic-gate xtob(int c) {
400Sstevel@tonic-gate return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
410Sstevel@tonic-gate }
420Sstevel@tonic-gate
430Sstevel@tonic-gate u_int
inet_nsap_addr(const char * ascii,u_char * binary,int maxlen)440Sstevel@tonic-gate inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
450Sstevel@tonic-gate u_char c, nib;
460Sstevel@tonic-gate u_int len = 0;
470Sstevel@tonic-gate
480Sstevel@tonic-gate if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
490Sstevel@tonic-gate return (0);
500Sstevel@tonic-gate ascii += 2;
510Sstevel@tonic-gate
520Sstevel@tonic-gate while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
530Sstevel@tonic-gate if (c == '.' || c == '+' || c == '/')
540Sstevel@tonic-gate continue;
550Sstevel@tonic-gate if (!isascii(c))
560Sstevel@tonic-gate return (0);
570Sstevel@tonic-gate if (islower(c))
580Sstevel@tonic-gate c = toupper(c);
590Sstevel@tonic-gate if (isxdigit(c)) {
600Sstevel@tonic-gate nib = xtob(c);
610Sstevel@tonic-gate c = *ascii++;
620Sstevel@tonic-gate if (c != '\0') {
630Sstevel@tonic-gate c = toupper(c);
640Sstevel@tonic-gate if (isxdigit(c)) {
650Sstevel@tonic-gate *binary++ = (nib << 4) | xtob(c);
660Sstevel@tonic-gate len++;
670Sstevel@tonic-gate } else
680Sstevel@tonic-gate return (0);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate else
710Sstevel@tonic-gate return (0);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate else
740Sstevel@tonic-gate return (0);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate return (len);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate
790Sstevel@tonic-gate char *
inet_nsap_ntoa(int binlen,const u_char * binary,char * ascii)800Sstevel@tonic-gate inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
810Sstevel@tonic-gate int nib;
820Sstevel@tonic-gate int i;
83*11038SRao.Shoaib@Sun.COM char *tmpbuf = inet_nsap_ntoa_tmpbuf;
840Sstevel@tonic-gate char *start;
850Sstevel@tonic-gate
860Sstevel@tonic-gate if (ascii)
870Sstevel@tonic-gate start = ascii;
880Sstevel@tonic-gate else {
890Sstevel@tonic-gate ascii = tmpbuf;
900Sstevel@tonic-gate start = tmpbuf;
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate *ascii++ = '0';
940Sstevel@tonic-gate *ascii++ = 'x';
950Sstevel@tonic-gate
960Sstevel@tonic-gate if (binlen > 255)
970Sstevel@tonic-gate binlen = 255;
980Sstevel@tonic-gate
990Sstevel@tonic-gate for (i = 0; i < binlen; i++) {
1000Sstevel@tonic-gate nib = *binary >> 4;
1010Sstevel@tonic-gate *ascii++ = nib + (nib < 10 ? '0' : '7');
1020Sstevel@tonic-gate nib = *binary++ & 0x0f;
1030Sstevel@tonic-gate *ascii++ = nib + (nib < 10 ? '0' : '7');
1040Sstevel@tonic-gate if (((i % 2) == 0 && (i + 1) < binlen))
1050Sstevel@tonic-gate *ascii++ = '.';
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate *ascii = '\0';
1080Sstevel@tonic-gate return (start);
1090Sstevel@tonic-gate }
110*11038SRao.Shoaib@Sun.COM
111*11038SRao.Shoaib@Sun.COM /*! \file */
112