Home | History | Annotate | Line # | Download | only in net
compat_ns_ntoa.c revision 1.1
      1  1.1  matt /*	$NetBSD: compat_ns_ntoa.c,v 1.1 2006/08/26 16:07:01 matt Exp $	*/
      2  1.1  matt 
      3  1.1  matt /*
      4  1.1  matt  * Copyright (c) 1986, 1993
      5  1.1  matt  *	The Regents of the University of California.  All rights reserved.
      6  1.1  matt  *
      7  1.1  matt  * Redistribution and use in source and binary forms, with or without
      8  1.1  matt  * modification, are permitted provided that the following conditions
      9  1.1  matt  * are met:
     10  1.1  matt  * 1. Redistributions of source code must retain the above copyright
     11  1.1  matt  *    notice, this list of conditions and the following disclaimer.
     12  1.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  matt  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  matt  *    documentation and/or other materials provided with the distribution.
     15  1.1  matt  * 3. Neither the name of the University nor the names of its contributors
     16  1.1  matt  *    may be used to endorse or promote products derived from this software
     17  1.1  matt  *    without specific prior written permission.
     18  1.1  matt  *
     19  1.1  matt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  matt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  matt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  matt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1  matt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  matt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  matt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  matt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  matt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  matt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  matt  * SUCH DAMAGE.
     30  1.1  matt  */
     31  1.1  matt 
     32  1.1  matt #include <sys/cdefs.h>
     33  1.1  matt #if defined(LIBC_SCCS) && !defined(lint)
     34  1.1  matt #if 0
     35  1.1  matt static char sccsid[] = "@(#)ns_ntoa.c	8.1 (Berkeley) 6/4/93";
     36  1.1  matt #else
     37  1.1  matt __RCSID("$NetBSD: compat_ns_ntoa.c,v 1.1 2006/08/26 16:07:01 matt Exp $");
     38  1.1  matt #endif
     39  1.1  matt #endif /* LIBC_SCCS and not lint */
     40  1.1  matt 
     41  1.1  matt #include <sys/param.h>
     42  1.1  matt #include <compat/include/ns.h>
     43  1.1  matt 
     44  1.1  matt #include <assert.h>
     45  1.1  matt #include <stdio.h>
     46  1.1  matt 
     47  1.1  matt static char *spectHex __P((char *));
     48  1.1  matt 
     49  1.1  matt char *
     50  1.1  matt ns_ntoa(addr)
     51  1.1  matt 	struct ns_addr addr;
     52  1.1  matt {
     53  1.1  matt 	static char obuf[40];
     54  1.1  matt 	union { union ns_net net_e; u_int32_t long_e; } net;
     55  1.1  matt 	u_int16_t port = htons(addr.x_port);
     56  1.1  matt 	char *cp;
     57  1.1  matt 	char *cp2;
     58  1.1  matt 	u_int8_t *up = addr.x_host.c_host;
     59  1.1  matt 	u_int8_t *uplim = up + 6;
     60  1.1  matt 
     61  1.1  matt 	net.net_e = addr.x_net;
     62  1.1  matt 	sprintf(obuf, "%x", ntohl(net.long_e));
     63  1.1  matt 	cp = spectHex(obuf);
     64  1.1  matt 	cp2 = cp + 1;
     65  1.1  matt 	while (up < uplim && *up==0)
     66  1.1  matt 		up++;
     67  1.1  matt 	if (up == uplim) {
     68  1.1  matt 		if (port) {
     69  1.1  matt 			sprintf(cp, ".0");
     70  1.1  matt 			cp += 2;
     71  1.1  matt 		}
     72  1.1  matt 	} else {
     73  1.1  matt 		sprintf(cp, ".%x", *up++);
     74  1.1  matt 		while (up < uplim) {
     75  1.1  matt 			while (*cp) cp++;
     76  1.1  matt 			sprintf(cp, "%02x", *up++);
     77  1.1  matt 		}
     78  1.1  matt 		cp = spectHex(cp2);
     79  1.1  matt 	}
     80  1.1  matt 	if (port) {
     81  1.1  matt 		sprintf(cp, ".%x", port);
     82  1.1  matt 		spectHex(cp + 1);
     83  1.1  matt 	}
     84  1.1  matt 	return (obuf);
     85  1.1  matt }
     86  1.1  matt 
     87  1.1  matt static char *
     88  1.1  matt spectHex(p0)
     89  1.1  matt 	char *p0;
     90  1.1  matt {
     91  1.1  matt 	int ok = 0;
     92  1.1  matt 	int nonzero = 0;
     93  1.1  matt 	char *p = p0;
     94  1.1  matt 
     95  1.1  matt 	_DIAGASSERT(p0 != NULL);
     96  1.1  matt 
     97  1.1  matt 	for (; *p; p++)
     98  1.1  matt 		switch (*p) {
     99  1.1  matt 		case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
    100  1.1  matt 			*p += ('A' - 'a');
    101  1.1  matt 			/* FALLTHROUGH */
    102  1.1  matt 		case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
    103  1.1  matt 			ok = 1;
    104  1.1  matt 			/* FALLTHROUGH */
    105  1.1  matt 		case '1': case '2': case '3': case '4': case '5':
    106  1.1  matt 		case '6': case '7': case '8': case '9':
    107  1.1  matt 			nonzero = 1;
    108  1.1  matt 		}
    109  1.1  matt 	if (nonzero && !ok) { *p++ = 'H'; *p = 0; }
    110  1.1  matt 	return (p);
    111  1.1  matt }
    112