compat_ns_ntoa.c revision 1.2 1 1.2 matt /* $NetBSD: compat_ns_ntoa.c,v 1.2 2012/03/20 17:05:59 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.2 matt __RCSID("$NetBSD: compat_ns_ntoa.c,v 1.2 2012/03/20 17:05:59 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.2 matt static char *spectHex(char *);
48 1.1 matt
49 1.1 matt char *
50 1.2 matt ns_ntoa(struct ns_addr addr)
51 1.1 matt {
52 1.1 matt static char obuf[40];
53 1.2 matt union { union ns_net net_e; uint32_t long_e; } net;
54 1.2 matt uint16_t port = htons(addr.x_port);
55 1.1 matt char *cp;
56 1.1 matt char *cp2;
57 1.2 matt uint8_t *up = addr.x_host.c_host;
58 1.2 matt uint8_t *uplim = up + 6;
59 1.1 matt
60 1.1 matt net.net_e = addr.x_net;
61 1.1 matt sprintf(obuf, "%x", ntohl(net.long_e));
62 1.1 matt cp = spectHex(obuf);
63 1.1 matt cp2 = cp + 1;
64 1.1 matt while (up < uplim && *up==0)
65 1.1 matt up++;
66 1.1 matt if (up == uplim) {
67 1.1 matt if (port) {
68 1.1 matt sprintf(cp, ".0");
69 1.1 matt cp += 2;
70 1.1 matt }
71 1.1 matt } else {
72 1.1 matt sprintf(cp, ".%x", *up++);
73 1.1 matt while (up < uplim) {
74 1.1 matt while (*cp) cp++;
75 1.1 matt sprintf(cp, "%02x", *up++);
76 1.1 matt }
77 1.1 matt cp = spectHex(cp2);
78 1.1 matt }
79 1.1 matt if (port) {
80 1.1 matt sprintf(cp, ".%x", port);
81 1.1 matt spectHex(cp + 1);
82 1.1 matt }
83 1.1 matt return (obuf);
84 1.1 matt }
85 1.1 matt
86 1.1 matt static char *
87 1.2 matt spectHex(char *p0)
88 1.1 matt {
89 1.1 matt int ok = 0;
90 1.1 matt int nonzero = 0;
91 1.1 matt char *p = p0;
92 1.1 matt
93 1.1 matt _DIAGASSERT(p0 != NULL);
94 1.1 matt
95 1.1 matt for (; *p; p++)
96 1.1 matt switch (*p) {
97 1.1 matt case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
98 1.1 matt *p += ('A' - 'a');
99 1.1 matt /* FALLTHROUGH */
100 1.1 matt case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
101 1.1 matt ok = 1;
102 1.1 matt /* FALLTHROUGH */
103 1.1 matt case '1': case '2': case '3': case '4': case '5':
104 1.1 matt case '6': case '7': case '8': case '9':
105 1.1 matt nonzero = 1;
106 1.1 matt }
107 1.1 matt if (nonzero && !ok) { *p++ = 'H'; *p = 0; }
108 1.1 matt return (p);
109 1.1 matt }
110