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