xref: /freebsd/usr.bin/netstat/route.c (revision a1a4f1a0d87b594d3f17a97dc0127eec1417e6f6)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "From: @(#)route.c	8.6 (Berkeley) 4/28/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41 
42 #include <sys/param.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/time.h>
46 
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
52 
53 #include <netinet/in.h>
54 #include <netipx/ipx.h>
55 #include <netatalk/at.h>
56 
57 #ifdef NS
58 #include <netns/ns.h>
59 #endif
60 
61 #include <sys/sysctl.h>
62 
63 #include <netdb.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <err.h>
69 #include <time.h>
70 #include "netstat.h"
71 
72 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
73 
74 /*
75  * Definitions for showing gateway flags.
76  */
77 struct bits {
78 	u_long	b_mask;
79 	char	b_val;
80 } bits[] = {
81 	{ RTF_UP,	'U' },
82 	{ RTF_GATEWAY,	'G' },
83 	{ RTF_HOST,	'H' },
84 	{ RTF_REJECT,	'R' },
85 	{ RTF_DYNAMIC,	'D' },
86 	{ RTF_MODIFIED,	'M' },
87 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
88 	{ RTF_CLONING,	'C' },
89 	{ RTF_XRESOLVE,	'X' },
90 	{ RTF_LLINFO,	'L' },
91 	{ RTF_STATIC,	'S' },
92 	{ RTF_PROTO1,	'1' },
93 	{ RTF_PROTO2,	'2' },
94 	{ RTF_WASCLONED,'W' },
95 	{ RTF_PRCLONING,'c' },
96 	{ RTF_PROTO3,	'3' },
97 	{ RTF_BLACKHOLE,'B' },
98 	{ RTF_BROADCAST,'b' },
99 	{ 0 }
100 };
101 
102 typedef union {
103 	long	dummy;		/* Helps align structure. */
104 	struct	sockaddr u_sa;
105 	u_short	u_data[128];
106 } sa_u;
107 
108 static sa_u pt_u;
109 
110 int	do_rtent = 0;
111 struct	rtentry rtentry;
112 struct	radix_node rnode;
113 struct	radix_mask rmask;
114 struct	radix_node_head *rt_tables[AF_MAX+1];
115 
116 int	NewTree = 0;
117 
118 static struct sockaddr *kgetsa __P((struct sockaddr *));
119 static void p_tree __P((struct radix_node *));
120 static void p_rtnode __P((void));
121 static void ntreestuff __P((void));
122 static void np_rtentry __P((struct rt_msghdr *));
123 static void p_sockaddr __P((struct sockaddr *, struct sockaddr *, int, int));
124 static void p_flags __P((int, char *));
125 static void p_rtentry __P((struct rtentry *));
126 static u_long forgemask __P((u_long));
127 static void domask __P((char *, u_long, u_long));
128 
129 /*
130  * Print routing tables.
131  */
132 void
133 routepr(rtree)
134 	u_long rtree;
135 {
136 	struct radix_node_head *rnh, head;
137 	int i;
138 
139 	printf("Routing tables\n");
140 
141 	if (Aflag == 0 && NewTree)
142 		ntreestuff();
143 	else {
144 		if (rtree == 0) {
145 			printf("rt_tables: symbol not in namelist\n");
146 			return;
147 		}
148 
149 		kget(rtree, rt_tables);
150 		for (i = 0; i <= AF_MAX; i++) {
151 			if ((rnh = rt_tables[i]) == 0)
152 				continue;
153 			kget(rnh, head);
154 			if (i == AF_UNSPEC) {
155 				if (Aflag && af == 0) {
156 					printf("Netmasks:\n");
157 					p_tree(head.rnh_treetop);
158 				}
159 			} else if (af == AF_UNSPEC || af == i) {
160 				pr_family(i);
161 				do_rtent = 1;
162 				pr_rthdr();
163 				p_tree(head.rnh_treetop);
164 			}
165 		}
166 	}
167 }
168 
169 /*
170  * Print address family header before a section of the routing table.
171  */
172 void
173 pr_family(af)
174 	int af;
175 {
176 	char *afname;
177 
178 	switch (af) {
179 	case AF_INET:
180 		afname = "Internet";
181 		break;
182 	case AF_IPX:
183 		afname = "IPX";
184 		break;
185 #ifdef NS
186 	case AF_NS:
187 		afname = "XNS";
188 		break;
189 #endif
190 	case AF_ISO:
191 		afname = "ISO";
192 		break;
193 	case AF_APPLETALK:
194 		afname = "AppleTalk";
195 		break;
196 	case AF_CCITT:
197 		afname = "X.25";
198 		break;
199 	default:
200 		afname = NULL;
201 		break;
202 	}
203 	if (afname)
204 		printf("\n%s:\n", afname);
205 	else
206 		printf("\nProtocol Family %d:\n", af);
207 }
208 
209 /* column widths; each followed by one space */
210 #define	WID_DST		18	/* width of destination column */
211 #define	WID_GW		18	/* width of gateway column */
212 
213 /*
214  * Print header for routing table columns.
215  */
216 void
217 pr_rthdr()
218 {
219 	if (Aflag)
220 		printf("%-8.8s ","Address");
221 	printf("%-*.*s %-*.*s %-6.6s  %6.6s%8.8s  %8.8s %6s\n",
222 		WID_DST, WID_DST, "Destination",
223 		WID_GW, WID_GW, "Gateway",
224 		"Flags", "Refs", "Use", "Netif", "Expire");
225 }
226 
227 static struct sockaddr *
228 kgetsa(dst)
229 	register struct sockaddr *dst;
230 {
231 
232 	kget(dst, pt_u.u_sa);
233 	if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
234 		kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
235 	return (&pt_u.u_sa);
236 }
237 
238 static void
239 p_tree(rn)
240 	struct radix_node *rn;
241 {
242 
243 again:
244 	kget(rn, rnode);
245 	if (rnode.rn_b < 0) {
246 		if (Aflag)
247 			printf("%-8.8lx ", (u_long)rn);
248 		if (rnode.rn_flags & RNF_ROOT) {
249 			if (Aflag)
250 				printf("(root node)%s",
251 				    rnode.rn_dupedkey ? " =>\n" : "\n");
252 		} else if (do_rtent) {
253 			kget(rn, rtentry);
254 			p_rtentry(&rtentry);
255 			if (Aflag)
256 				p_rtnode();
257 		} else {
258 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
259 				   NULL, 0, 44);
260 			putchar('\n');
261 		}
262 		if ((rn = rnode.rn_dupedkey))
263 			goto again;
264 	} else {
265 		if (Aflag && do_rtent) {
266 			printf("%-8.8lx ", (u_long)rn);
267 			p_rtnode();
268 		}
269 		rn = rnode.rn_r;
270 		p_tree(rnode.rn_l);
271 		p_tree(rn);
272 	}
273 }
274 
275 char	nbuf[20];
276 
277 static void
278 p_rtnode()
279 {
280 	struct radix_mask *rm = rnode.rn_mklist;
281 
282 	if (rnode.rn_b < 0) {
283 		if (rnode.rn_mask) {
284 			printf("\t  mask ");
285 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
286 				   NULL, 0, -1);
287 		} else if (rm == 0)
288 			return;
289 	} else {
290 		sprintf(nbuf, "(%d)", rnode.rn_b);
291 		printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_l, (u_long)rnode.rn_r);
292 	}
293 	while (rm) {
294 		kget(rm, rmask);
295 		sprintf(nbuf, " %d refs, ", rmask.rm_refs);
296 		printf(" mk = %8.8lx {(%d),%s",
297 			(u_long)rm, -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
298 		if (rmask.rm_flags & RNF_NORMAL) {
299 			struct radix_node rnode_aux;
300 			printf(" <normal>, ");
301 			kget(rmask.rm_leaf, rnode_aux);
302 			p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
303 				    NULL, 0, -1);
304 		} else
305 		    p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
306 				NULL, 0, -1);
307 		putchar('}');
308 		if ((rm = rmask.rm_mklist))
309 			printf(" ->");
310 	}
311 	putchar('\n');
312 }
313 
314 static void
315 ntreestuff()
316 {
317 	size_t needed;
318 	int mib[6];
319 	char *buf, *next, *lim;
320 	register struct rt_msghdr *rtm;
321 
322 	mib[0] = CTL_NET;
323 	mib[1] = PF_ROUTE;
324 	mib[2] = 0;
325 	mib[3] = 0;
326 	mib[4] = NET_RT_DUMP;
327 	mib[5] = 0;
328 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
329 		err(1, "sysctl: net.route.0.0.dump estimate");
330 	}
331 
332 	if ((buf = malloc(needed)) == 0) {
333 		err(2, "malloc(%lu)", (unsigned long)needed);
334 	}
335 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
336 		err(1, "sysctl: net.route.0.0.dump");
337 	}
338 	lim  = buf + needed;
339 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
340 		rtm = (struct rt_msghdr *)next;
341 		np_rtentry(rtm);
342 	}
343 }
344 
345 static void
346 np_rtentry(rtm)
347 	register struct rt_msghdr *rtm;
348 {
349 	register struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
350 #ifdef notdef
351 	static int masks_done, banner_printed;
352 #endif
353 	static int old_af;
354 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
355 
356 #ifdef notdef
357 	/* for the moment, netmasks are skipped over */
358 	if (!banner_printed) {
359 		printf("Netmasks:\n");
360 		banner_printed = 1;
361 	}
362 	if (masks_done == 0) {
363 		if (rtm->rtm_addrs != RTA_DST ) {
364 			masks_done = 1;
365 			af = sa->sa_family;
366 		}
367 	} else
368 #endif
369 		af = sa->sa_family;
370 	if (af != old_af) {
371 		pr_family(af);
372 		old_af = af;
373 	}
374 	if (rtm->rtm_addrs == RTA_DST)
375 		p_sockaddr(sa, NULL, 0, 36);
376 	else {
377 		p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
378 		if (sa->sa_len == 0)
379 			sa->sa_len = sizeof(long);
380 		sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
381 		p_sockaddr(sa, NULL, 0, 18);
382 	}
383 	p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
384 	putchar('\n');
385 }
386 
387 static void
388 p_sockaddr(sa, mask, flags, width)
389 	struct sockaddr *sa, *mask;
390 	int flags, width;
391 {
392 	char workbuf[128], *cplim;
393 	register char *cp = workbuf;
394 
395 	switch(sa->sa_family) {
396 	case AF_INET:
397 	    {
398 		register struct sockaddr_in *sin = (struct sockaddr_in *)sa;
399 
400 		if (sin->sin_addr.s_addr == INADDR_ANY)
401 			cp = "default";
402 		else if (flags & RTF_HOST)
403 			cp = routename(sin->sin_addr.s_addr);
404 		else if (mask)
405 			cp = netname(sin->sin_addr.s_addr,
406 				     ntohl(((struct sockaddr_in *)mask)
407 					   ->sin_addr.s_addr));
408 		else
409 			cp = netname(sin->sin_addr.s_addr, 0L);
410 		break;
411 	    }
412 
413 	case AF_IPX:
414 	    {
415 		struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
416 		if (ipx_nullnet(satoipx_addr(work)))
417 			cp = "default";
418 		else
419 			cp = ipx_print(sa);
420 		break;
421 	    }
422 	case AF_APPLETALK:
423 	    {
424 		if (!(flags & RTF_HOST) && mask)
425 			cp = atalk_print2(sa,mask,9);
426 		else
427 			cp = atalk_print(sa,11);
428 		break;
429 	    }
430 #ifdef NS
431 	case AF_NS:
432 		cp = ns_print(sa);
433 		break;
434 #endif
435 
436 	case AF_LINK:
437 	    {
438 		register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
439 
440 		if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
441 		    sdl->sdl_slen == 0)
442 			(void) sprintf(workbuf, "link#%d", sdl->sdl_index);
443 		else
444 			switch (sdl->sdl_type) {
445 
446 			case IFT_ETHER:
447 			    {
448 				register int i;
449 				register u_char *lla = (u_char *)sdl->sdl_data +
450 				    sdl->sdl_nlen;
451 
452 				cplim = "";
453 				for (i = 0; i < sdl->sdl_alen; i++, lla++) {
454 					cp += sprintf(cp, "%s%x", cplim, *lla);
455 					cplim = ":";
456 				}
457 				cp = workbuf;
458 				break;
459 			    }
460 
461 			default:
462 				cp = link_ntoa(sdl);
463 				break;
464 			}
465 		break;
466 	    }
467 
468 	default:
469 	    {
470 		register u_char *s = (u_char *)sa->sa_data, *slim;
471 
472 		slim =  sa->sa_len + (u_char *) sa;
473 		cplim = cp + sizeof(workbuf) - 6;
474 		cp += sprintf(cp, "(%d)", sa->sa_family);
475 		while (s < slim && cp < cplim) {
476 			cp += sprintf(cp, " %02x", *s++);
477 			if (s < slim)
478 			    cp += sprintf(cp, "%02x", *s++);
479 		}
480 		cp = workbuf;
481 	    }
482 	}
483 	if (width < 0 )
484 		printf("%s ", cp);
485 	else {
486 		if (nflag)
487 			printf("%-*s ", width, cp);
488 		else
489 			printf("%-*.*s ", width, width, cp);
490 	}
491 }
492 
493 static void
494 p_flags(f, format)
495 	register int f;
496 	char *format;
497 {
498 	char name[33], *flags;
499 	register struct bits *p = bits;
500 
501 	for (flags = name; p->b_mask; p++)
502 		if (p->b_mask & f)
503 			*flags++ = p->b_val;
504 	*flags = '\0';
505 	printf(format, name);
506 }
507 
508 static void
509 p_rtentry(rt)
510 	register struct rtentry *rt;
511 {
512 	static struct ifnet ifnet, *lastif;
513 	static char name[16];
514 	static char prettyname[9];
515 	struct sockaddr *sa;
516 	sa_u addr, mask;
517 
518 	/*
519 	 * Don't print protocol-cloned routes unless -a.
520 	 */
521 	if (rt->rt_parent && !aflag)
522 		return;
523 
524 	bzero(&addr, sizeof(addr));
525 	if ((sa = kgetsa(rt_key(rt))))
526 		bcopy(sa, &addr, sa->sa_len);
527 	bzero(&mask, sizeof(mask));
528 	if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
529 		bcopy(sa, &mask, sa->sa_len);
530 	p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, WID_DST);
531 	p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, WID_GW);
532 	p_flags(rt->rt_flags, "%-6.6s ");
533 	printf("%6d %8ld ", rt->rt_refcnt, rt->rt_use);
534 	if (rt->rt_ifp) {
535 		if (rt->rt_ifp != lastif) {
536 			kget(rt->rt_ifp, ifnet);
537 			kread((u_long)ifnet.if_name, name, 16);
538 			lastif = rt->rt_ifp;
539 			snprintf(prettyname, sizeof prettyname,
540 				 "%.6s%d", name, ifnet.if_unit);
541 		}
542 		printf("%8.8s", prettyname);
543 		if (rt->rt_rmx.rmx_expire) {
544 			time_t expire_time;
545 
546 			if ((expire_time =
547 			    rt->rt_rmx.rmx_expire - time((time_t *)0)) > 0)
548 				printf(" %6d%s", (int)expire_time,
549 				    rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
550 		} else if (rt->rt_nodes[0].rn_dupedkey) {
551 			printf(" =>");
552 		}
553 
554 	}
555 	putchar('\n');
556 }
557 
558 char *
559 routename(in)
560 	u_long in;
561 {
562 	register char *cp;
563 	static char line[MAXHOSTNAMELEN + 1];
564 	struct hostent *hp;
565 
566 	cp = 0;
567 	if (!nflag) {
568 		hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
569 			AF_INET);
570 		if (hp) {
571 			cp = hp->h_name;
572 			trimdomain(cp);
573 		}
574 	}
575 	if (cp) {
576 		strncpy(line, cp, sizeof(line) - 1);
577 		line[sizeof(line) - 1] = '\0';
578 	} else {
579 #define C(x)	((x) & 0xff)
580 		in = ntohl(in);
581 		sprintf(line, "%lu.%lu.%lu.%lu",
582 		    C(in >> 24), C(in >> 16), C(in >> 8), C(in));
583 	}
584 	return (line);
585 }
586 
587 static u_long
588 forgemask(a)
589 	u_long a;
590 {
591 	u_long m;
592 
593 	if (IN_CLASSA(a))
594 		m = IN_CLASSA_NET;
595 	else if (IN_CLASSB(a))
596 		m = IN_CLASSB_NET;
597 	else
598 		m = IN_CLASSC_NET;
599 	return (m);
600 }
601 
602 static void
603 domask(dst, addr, mask)
604 	char *dst;
605 	u_long addr, mask;
606 {
607 	register int b, i;
608 
609 	if (!mask || (forgemask(addr) == mask)) {
610 		*dst = '\0';
611 		return;
612 	}
613 	i = 0;
614 	for (b = 0; b < 32; b++)
615 		if (mask & (1 << b)) {
616 			register int bb;
617 
618 			i = b;
619 			for (bb = b+1; bb < 32; bb++)
620 				if (!(mask & (1 << bb))) {
621 					i = -1;	/* noncontig */
622 					break;
623 				}
624 			break;
625 		}
626 	if (i == -1)
627 		sprintf(dst, "&0x%lx", mask);
628 	else
629 		sprintf(dst, "/%d", 32-i);
630 }
631 
632 /*
633  * Return the name of the network whose address is given.
634  * The address is assumed to be that of a net or subnet, not a host.
635  */
636 char *
637 netname(in, mask)
638 	u_long in, mask;
639 {
640 	char *cp = 0;
641 	static char line[MAXHOSTNAMELEN + 1];
642 	struct netent *np = 0;
643 	u_long net, omask, dmask;
644 	register u_long i;
645 
646 	i = ntohl(in);
647 	omask = mask;
648 	if (!nflag && i) {
649 		dmask = forgemask(i);
650 		net = i & dmask;
651 		if (!(np = getnetbyaddr(i, AF_INET)) && net != i)
652 			np = getnetbyaddr(net, AF_INET);
653 		if (np) {
654 			cp = np->n_name;
655 			trimdomain(cp);
656 		}
657 	}
658 	if (cp)
659 		strncpy(line, cp, sizeof(line) - 1);
660 	else if ((i & 0xffffff) == 0)
661 		sprintf(line, "%lu", C(i >> 24));
662 	else if ((i & 0xffff) == 0)
663 		sprintf(line, "%lu.%lu", C(i >> 24) , C(i >> 16));
664 	else if ((i & 0xff) == 0)
665 		sprintf(line, "%lu.%lu.%lu", C(i >> 24), C(i >> 16), C(i >> 8));
666 	else
667 		sprintf(line, "%lu.%lu.%lu.%lu", C(i >> 24),
668 			C(i >> 16), C(i >> 8), C(i));
669 	domask(line+strlen(line), i, omask);
670 	return (line);
671 }
672 
673 /*
674  * Print routing statistics
675  */
676 void
677 rt_stats(off)
678 	u_long off;
679 {
680 	struct rtstat rtstat;
681 
682 	if (off == 0) {
683 		printf("rtstat: symbol not in namelist\n");
684 		return;
685 	}
686 	kread(off, (char *)&rtstat, sizeof (rtstat));
687 	printf("routing:\n");
688 	printf("\t%u bad routing redirect%s\n",
689 		rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
690 	printf("\t%u dynamically created route%s\n",
691 		rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
692 	printf("\t%u new gateway%s due to redirects\n",
693 		rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
694 	printf("\t%u destination%s found unreachable\n",
695 		rtstat.rts_unreach, plural(rtstat.rts_unreach));
696 	printf("\t%u use%s of a wildcard route\n",
697 		rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
698 }
699 
700 char *
701 ipx_print(sa)
702 	register struct sockaddr *sa;
703 {
704 	u_short port;
705 	struct servent *sp = 0;
706 	char *net = "", *host = "";
707 	register char *p;
708 	register u_char *q;
709 	struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
710 	static char mybuf[50];
711 	char cport[10], chost[15], cnet[15];
712 
713 	port = ntohs(work.x_port);
714 
715 	if (ipx_nullnet(work) && ipx_nullhost(work)) {
716 
717 		if (port) {
718 			if (sp)
719 				sprintf(mybuf, "*.%s", sp->s_name);
720 			else
721 				sprintf(mybuf, "*.%x", port);
722 		} else
723 			sprintf(mybuf, "*.*");
724 
725 		return (mybuf);
726 	}
727 
728 	if (ipx_wildnet(work))
729 		net = "any";
730 	else if (ipx_nullnet(work))
731 		net = "*";
732 	else {
733 		q = work.x_net.c_net;
734 		sprintf(cnet, "%02x%02x%02x%02x",
735 			q[0], q[1], q[2], q[3]);
736 		for (p = cnet; *p == '0' && p < cnet + 8; p++)
737 			continue;
738 		net = p;
739 	}
740 
741 	if (ipx_wildhost(work))
742 		host = "any";
743 	else if (ipx_nullhost(work))
744 		host = "*";
745 	else {
746 		q = work.x_host.c_host;
747 		sprintf(chost, "%02x%02x%02x%02x%02x%02x",
748 			q[0], q[1], q[2], q[3], q[4], q[5]);
749 		for (p = chost; *p == '0' && p < chost + 12; p++)
750 			continue;
751 		host = p;
752 	}
753 
754 	if (port) {
755 		if (strcmp(host, "*") == 0)
756 			host = "";
757 		if (sp)
758 			snprintf(cport, sizeof(cport),
759 				"%s%s", *host ? "." : "", sp->s_name);
760 		else
761 			snprintf(cport, sizeof(cport),
762 				"%s%x", *host ? "." : "", port);
763 	} else
764 		*cport = 0;
765 
766 	snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
767 	return(mybuf);
768 }
769 
770 char *
771 ipx_phost(sa)
772 	struct sockaddr *sa;
773 {
774 	register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
775 	struct sockaddr_ipx work;
776 	static union ipx_net ipx_zeronet;
777 	char *p;
778 	struct ipx_addr in;
779 
780 	work = *sipx;
781 	in = work.sipx_addr;
782 
783 	work.sipx_addr.x_port = 0;
784 	work.sipx_addr.x_net = ipx_zeronet;
785 	p = ipx_print((struct sockaddr *)&work);
786 	if (strncmp("*.", p, 2) == 0) p += 2;
787 
788 	return(p);
789 }
790 
791 #ifdef NS
792 short ns_nullh[] = {0,0,0};
793 short ns_bh[] = {-1,-1,-1};
794 
795 char *
796 ns_print(sa)
797 	register struct sockaddr *sa;
798 {
799 	register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
800 	struct ns_addr work;
801 	union { union ns_net net_e; u_long long_e; } net;
802 	u_short port;
803 	static char mybuf[50], cport[10], chost[25];
804 	char *host = "";
805 	register char *p; register u_char *q;
806 
807 	work = sns->sns_addr;
808 	port = ntohs(work.x_port);
809 	work.x_port = 0;
810 	net.net_e  = work.x_net;
811 	if (ns_nullhost(work) && net.long_e == 0) {
812 		if (port ) {
813 			sprintf(mybuf, "*.%xH", port);
814 			upHex(mybuf);
815 		} else
816 			sprintf(mybuf, "*.*");
817 		return (mybuf);
818 	}
819 
820 	if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
821 		host = "any";
822 	} else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
823 		host = "*";
824 	} else {
825 		q = work.x_host.c_host;
826 		sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
827 			q[0], q[1], q[2], q[3], q[4], q[5]);
828 		for (p = chost; *p == '0' && p < chost + 12; p++)
829 			continue;
830 		host = p;
831 	}
832 	if (port)
833 		sprintf(cport, ".%xH", htons(port));
834 	else
835 		*cport = 0;
836 
837 	sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
838 	upHex(mybuf);
839 	return(mybuf);
840 }
841 
842 char *
843 ns_phost(sa)
844 	struct sockaddr *sa;
845 {
846 	register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
847 	struct sockaddr_ns work;
848 	static union ns_net ns_zeronet;
849 	char *p;
850 
851 	work = *sns;
852 	work.sns_addr.x_port = 0;
853 	work.sns_addr.x_net = ns_zeronet;
854 
855 	p = ns_print((struct sockaddr *)&work);
856 	if (strncmp("0H.", p, 3) == 0)
857 		p += 3;
858 	return(p);
859 }
860 #endif
861 
862 void
863 upHex(p0)
864 	char *p0;
865 {
866 	register char *p = p0;
867 
868 	for (; *p; p++)
869 		switch (*p) {
870 
871 		case 'a':
872 		case 'b':
873 		case 'c':
874 		case 'd':
875 		case 'e':
876 		case 'f':
877 			*p += ('A' - 'a');
878 			break;
879 		}
880 }
881