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