125987Ssklower /* 225987Ssklower * Copyright (c) 1986 Regents of the University of California. 3*35361Sbostic * All rights reserved. 425987Ssklower * 5*35361Sbostic * Redistribution and use in source and binary forms are permitted 6*35361Sbostic * provided that the above copyright notice and this paragraph are 7*35361Sbostic * duplicated in all such forms and that any documentation, 8*35361Sbostic * advertising materials, and other materials related to such 9*35361Sbostic * distribution and use acknowledge that the software was developed 10*35361Sbostic * by the University of California, Berkeley. The name of the 11*35361Sbostic * University may not be used to endorse or promote products derived 12*35361Sbostic * from this software without specific prior written permission. 13*35361Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*35361Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*35361Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1625987Ssklower */ 1725987Ssklower 1826638Sdonn #if defined(LIBC_SCCS) && !defined(lint) 19*35361Sbostic static char sccsid[] = "@(#)ns_ntoa.c 6.4 (Berkeley) 08/12/88"; 20*35361Sbostic #endif /* LIBC_SCCS and not lint */ 2125987Ssklower 2225987Ssklower #include <sys/types.h> 2325987Ssklower #include <netns/ns.h> 2425987Ssklower 2525987Ssklower char * 2625987Ssklower ns_ntoa(addr) 2725987Ssklower struct ns_addr addr; 2825987Ssklower { 2925987Ssklower static char obuf[40]; 3025987Ssklower char *spectHex(); 3126073Ssklower union { union ns_net net_e; u_long long_e; } net; 3225987Ssklower u_short port = htons(addr.x_port); 3325987Ssklower register char *cp; 3425987Ssklower char *cp2; 3525987Ssklower register u_char *up = addr.x_host.c_host; 3625987Ssklower u_char *uplim = up + 6; 3725987Ssklower 3826073Ssklower net.net_e = addr.x_net; 3926073Ssklower sprintf(obuf, "%lx", ntohl(net.long_e)); 4025987Ssklower cp = spectHex(obuf); 4125987Ssklower cp2 = cp + 1; 4225987Ssklower while (*up==0 && up < uplim) up++; 4326073Ssklower if (up == uplim) { 4426073Ssklower if (port) { 4526073Ssklower sprintf(cp, ".0"); 4626073Ssklower cp += 2; 4726073Ssklower } 4826073Ssklower } else { 4926073Ssklower sprintf(cp, ".%x", *up++); 5026073Ssklower while (up < uplim) { 5126073Ssklower while (*cp) cp++; 5226073Ssklower sprintf(cp, "%02x", *up++); 5326073Ssklower } 5426073Ssklower cp = spectHex(cp2); 5525987Ssklower } 5625987Ssklower if (port) { 5725987Ssklower sprintf(cp, ".%x", port); 5825987Ssklower spectHex(cp + 1); 5925987Ssklower } 6025987Ssklower return (obuf); 6125987Ssklower } 6225987Ssklower 6325987Ssklower static char * 6425987Ssklower spectHex(p0) 6525987Ssklower char *p0; 6625987Ssklower { 6725987Ssklower int ok = 0; 6825987Ssklower int nonzero = 0; 6925987Ssklower register char *p = p0; 7025987Ssklower for (; *p; p++) switch (*p) { 7125987Ssklower 7225987Ssklower case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 7325987Ssklower *p += ('A' - 'a'); 7425987Ssklower /* fall into . . . */ 7525987Ssklower case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 7625987Ssklower ok = 1; 7725987Ssklower case '1': case '2': case '3': case '4': case '5': 7825987Ssklower case '6': case '7': case '8': case '9': 7925987Ssklower nonzero = 1; 8025987Ssklower } 8125987Ssklower if (nonzero && !ok) { *p++ = 'H'; *p = 0; } 8225987Ssklower return (p); 8325987Ssklower } 84