1*5688dfcfSmatt /* $NetBSD: compat_ns_ntoa.c,v 1.2 2012/03/20 17:05:59 matt Exp $ */
295ff9c00Smatt
395ff9c00Smatt /*
495ff9c00Smatt * Copyright (c) 1986, 1993
595ff9c00Smatt * The Regents of the University of California. All rights reserved.
695ff9c00Smatt *
795ff9c00Smatt * Redistribution and use in source and binary forms, with or without
895ff9c00Smatt * modification, are permitted provided that the following conditions
995ff9c00Smatt * are met:
1095ff9c00Smatt * 1. Redistributions of source code must retain the above copyright
1195ff9c00Smatt * notice, this list of conditions and the following disclaimer.
1295ff9c00Smatt * 2. Redistributions in binary form must reproduce the above copyright
1395ff9c00Smatt * notice, this list of conditions and the following disclaimer in the
1495ff9c00Smatt * documentation and/or other materials provided with the distribution.
1595ff9c00Smatt * 3. Neither the name of the University nor the names of its contributors
1695ff9c00Smatt * may be used to endorse or promote products derived from this software
1795ff9c00Smatt * without specific prior written permission.
1895ff9c00Smatt *
1995ff9c00Smatt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2095ff9c00Smatt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2195ff9c00Smatt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2295ff9c00Smatt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2395ff9c00Smatt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2495ff9c00Smatt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2595ff9c00Smatt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2695ff9c00Smatt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2795ff9c00Smatt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2895ff9c00Smatt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2995ff9c00Smatt * SUCH DAMAGE.
3095ff9c00Smatt */
3195ff9c00Smatt
3295ff9c00Smatt #include <sys/cdefs.h>
3395ff9c00Smatt #if defined(LIBC_SCCS) && !defined(lint)
3495ff9c00Smatt #if 0
3595ff9c00Smatt static char sccsid[] = "@(#)ns_ntoa.c 8.1 (Berkeley) 6/4/93";
3695ff9c00Smatt #else
37*5688dfcfSmatt __RCSID("$NetBSD: compat_ns_ntoa.c,v 1.2 2012/03/20 17:05:59 matt Exp $");
3895ff9c00Smatt #endif
3995ff9c00Smatt #endif /* LIBC_SCCS and not lint */
4095ff9c00Smatt
4195ff9c00Smatt #include <sys/param.h>
4295ff9c00Smatt #include <compat/include/ns.h>
4395ff9c00Smatt
4495ff9c00Smatt #include <assert.h>
4595ff9c00Smatt #include <stdio.h>
4695ff9c00Smatt
47*5688dfcfSmatt static char *spectHex(char *);
4895ff9c00Smatt
4995ff9c00Smatt char *
ns_ntoa(struct ns_addr addr)50*5688dfcfSmatt ns_ntoa(struct ns_addr addr)
5195ff9c00Smatt {
5295ff9c00Smatt static char obuf[40];
53*5688dfcfSmatt union { union ns_net net_e; uint32_t long_e; } net;
54*5688dfcfSmatt uint16_t port = htons(addr.x_port);
5595ff9c00Smatt char *cp;
5695ff9c00Smatt char *cp2;
57*5688dfcfSmatt uint8_t *up = addr.x_host.c_host;
58*5688dfcfSmatt uint8_t *uplim = up + 6;
5995ff9c00Smatt
6095ff9c00Smatt net.net_e = addr.x_net;
6195ff9c00Smatt sprintf(obuf, "%x", ntohl(net.long_e));
6295ff9c00Smatt cp = spectHex(obuf);
6395ff9c00Smatt cp2 = cp + 1;
6495ff9c00Smatt while (up < uplim && *up==0)
6595ff9c00Smatt up++;
6695ff9c00Smatt if (up == uplim) {
6795ff9c00Smatt if (port) {
6895ff9c00Smatt sprintf(cp, ".0");
6995ff9c00Smatt cp += 2;
7095ff9c00Smatt }
7195ff9c00Smatt } else {
7295ff9c00Smatt sprintf(cp, ".%x", *up++);
7395ff9c00Smatt while (up < uplim) {
7495ff9c00Smatt while (*cp) cp++;
7595ff9c00Smatt sprintf(cp, "%02x", *up++);
7695ff9c00Smatt }
7795ff9c00Smatt cp = spectHex(cp2);
7895ff9c00Smatt }
7995ff9c00Smatt if (port) {
8095ff9c00Smatt sprintf(cp, ".%x", port);
8195ff9c00Smatt spectHex(cp + 1);
8295ff9c00Smatt }
8395ff9c00Smatt return (obuf);
8495ff9c00Smatt }
8595ff9c00Smatt
8695ff9c00Smatt static char *
spectHex(char * p0)87*5688dfcfSmatt spectHex(char *p0)
8895ff9c00Smatt {
8995ff9c00Smatt int ok = 0;
9095ff9c00Smatt int nonzero = 0;
9195ff9c00Smatt char *p = p0;
9295ff9c00Smatt
9395ff9c00Smatt _DIAGASSERT(p0 != NULL);
9495ff9c00Smatt
9595ff9c00Smatt for (; *p; p++)
9695ff9c00Smatt switch (*p) {
9795ff9c00Smatt case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
9895ff9c00Smatt *p += ('A' - 'a');
9995ff9c00Smatt /* FALLTHROUGH */
10095ff9c00Smatt case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
10195ff9c00Smatt ok = 1;
10295ff9c00Smatt /* FALLTHROUGH */
10395ff9c00Smatt case '1': case '2': case '3': case '4': case '5':
10495ff9c00Smatt case '6': case '7': case '8': case '9':
10595ff9c00Smatt nonzero = 1;
10695ff9c00Smatt }
10795ff9c00Smatt if (nonzero && !ok) { *p++ = 'H'; *p = 0; }
10895ff9c00Smatt return (p);
10995ff9c00Smatt }
110