2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)route.c 8.6 (Berkeley) 4/28/95
34 * $FreeBSD: src/usr.bin/netstat/route.c,v 1.41.2.14 2002/07/17 02:22:22 kbyanc Exp $
35 * $DragonFly: src/usr.bin/netstat/route.c,v 1.12 2007/09/30 12:45:05 sephe Exp $
38 #include <sys/param.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
43 #include <net/ethernet.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netipx/ipx.h>
52 #include <netatalk/at.h>
53 #include <netgraph/socket/ng_socket.h>
59 #include <sys/sysctl.h>
61 #include <arpa/inet.h>
73 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
76 /* alignment constraint for routing socket */
78 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
79 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
82 * Definitions for showing gateway flags.
93 { RTF_MODIFIED
, 'M' },
94 { RTF_DONE
, 'd' }, /* Completed -- for routing messages only */
96 { RTF_XRESOLVE
, 'X' },
101 { RTF_WASCLONED
,'W' },
102 { RTF_PRCLONING
,'c' },
104 { RTF_BLACKHOLE
,'B' },
105 { RTF_BROADCAST
,'b' },
110 long dummy
; /* Helps align structure. */
111 struct sockaddr u_sa
;
118 struct rtentry rtentry
;
119 struct radix_node rnode
;
120 struct radix_mask rmask
;
121 struct radix_node_head
*rt_tables
[AF_MAX
+1];
125 static struct sockaddr
*kgetsa (struct sockaddr
*);
126 static void size_cols (int ef
, struct radix_node
*rn
);
127 static void size_cols_tree (struct radix_node
*rn
);
128 static void size_cols_rtentry (struct rtentry
*rt
);
129 static void p_tree (struct radix_node
*);
130 static void p_rtnode (void);
131 static void ntreestuff (void);
132 static void np_rtentry (struct rt_msghdr
*);
133 static void p_sockaddr (struct sockaddr
*, struct sockaddr
*, int, int);
134 static const char *fmt_sockaddr (struct sockaddr
*sa
, struct sockaddr
*mask
,
136 static void p_flags (int, char *);
137 static const char *fmt_flags(int f
);
138 static void p_rtentry (struct rtentry
*);
139 static u_long
forgemask (u_long
);
140 static void domask (char *, u_long
, u_long
);
143 * Print routing tables.
146 routepr(u_long rtree
)
148 struct radix_node_head
*rnh
, head
;
151 printf("Routing tables\n");
153 if (Aflag
== 0 && NewTree
) {
157 printf("rt_tables: symbol not in namelist\n");
164 rtree
+= cpuflag
* (AF_MAX
+ 1) * sizeof(void *);
166 kget(rtree
, rt_tables
);
167 for (i
= 0; i
<= AF_MAX
; i
++) {
168 if ((rnh
= rt_tables
[i
]) == 0)
171 if (i
== AF_UNSPEC
) {
172 if (Aflag
&& af
== 0) {
173 printf("Netmasks:\n");
174 p_tree(head
.rnh_treetop
);
176 } else if (af
== AF_UNSPEC
|| af
== i
) {
177 size_cols(i
, head
.rnh_treetop
);
181 p_tree(head
.rnh_treetop
);
188 * Print address family header before a section of the routing table.
201 afname
= "Internet6";
216 afname
= "AppleTalk";
229 printf("\n%s:\n", afname
);
231 printf("\nProtocol Family %d:\n", af
);
234 /* column widths; each followed by one space */
236 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
237 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
238 #define WID_IF_DEFAULT(af) (Wflag ? 8 : 6) /* width of netif column */
240 #define WID_DST_DEFAULT(af) \
241 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
242 #define WID_GW_DEFAULT(af) \
243 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
244 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 :6))
249 static int wid_flags
;
254 static int wid_expire
;
257 size_cols(int ef
, struct radix_node
*rn
)
259 wid_dst
= WID_DST_DEFAULT(ef
);
260 wid_gw
= WID_GW_DEFAULT(ef
);
265 wid_if
= WID_IF_DEFAULT(ef
);
273 size_cols_tree(struct radix_node
*rn
)
277 if (rnode
.rn_bit
< 0) {
278 if ((rnode
.rn_flags
& RNF_ROOT
) == 0) {
280 size_cols_rtentry(&rtentry
);
282 if ((rn
= rnode
.rn_dupedkey
))
286 size_cols_tree(rnode
.rn_left
);
292 size_cols_rtentry(struct rtentry
*rt
)
294 static struct ifnet ifnet
, *lastif
;
295 struct rtentry parent
;
296 static char buffer
[100];
303 * Don't print protocol-cloned routes unless -a.
305 if (rt
->rt_flags
& RTF_WASCLONED
&& !aflag
) {
306 kget(rt
->rt_parent
, parent
);
307 if (parent
.rt_flags
& RTF_PRCLONING
)
311 bzero(&addr
, sizeof(addr
));
312 if ((sa
= kgetsa(rt_key(rt
))))
313 bcopy(sa
, &addr
, sa
->sa_len
);
314 bzero(&mask
, sizeof(mask
));
315 if (rt_mask(rt
) && (sa
= kgetsa(rt_mask(rt
))))
316 bcopy(sa
, &mask
, sa
->sa_len
);
317 bp
= fmt_sockaddr(&addr
.u_sa
, &mask
.u_sa
, rt
->rt_flags
);
319 wid_dst
= MAX(len
, wid_dst
);
321 bp
= fmt_sockaddr(kgetsa(rt
->rt_gateway
), NULL
, RTF_HOST
);
323 wid_gw
= MAX(len
, wid_gw
);
325 bp
= fmt_flags(rt
->rt_flags
);
327 wid_flags
= MAX(len
, wid_flags
);
329 if (addr
.u_sa
.sa_family
== AF_INET
|| Wflag
) {
330 len
= snprintf(buffer
, sizeof(buffer
), "%ld", rt
->rt_refcnt
);
331 wid_refs
= MAX(len
, wid_refs
);
332 len
= snprintf(buffer
, sizeof(buffer
), "%lu", rt
->rt_use
);
333 wid_use
= MAX(len
, wid_use
);
334 if (Wflag
&& rt
->rt_rmx
.rmx_mtu
!= 0) {
335 len
= snprintf(buffer
, sizeof(buffer
),
336 "%lu", rt
->rt_rmx
.rmx_mtu
);
337 wid_mtu
= MAX(len
, wid_mtu
);
341 if (rt
->rt_ifp
!= lastif
) {
342 kget(rt
->rt_ifp
, ifnet
);
344 len
= strlen(ifnet
.if_xname
);
345 wid_if
= MAX(len
, wid_if
);
347 if (rt
->rt_rmx
.rmx_expire
) {
351 rt
->rt_rmx
.rmx_expire
- time(NULL
)) > 0) {
352 snprintf(buffer
, sizeof(buffer
), "%d",
354 wid_expire
= MAX(len
, wid_expire
);
362 * Print header for routing table columns.
369 printf("%-8.8s ","Address");
370 if (af
== AF_INET
|| Wflag
) {
372 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s %*s\n",
373 wid_dst
, wid_dst
, "Destination",
374 wid_gw
, wid_gw
, "Gateway",
375 wid_flags
, wid_flags
, "Flags",
376 wid_refs
, wid_refs
, "Refs",
377 wid_use
, wid_use
, "Use",
378 wid_mtu
, wid_mtu
, "Mtu",
379 wid_if
, wid_if
, "Netif",
380 wid_expire
, "Expire");
382 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*s\n",
383 wid_dst
, wid_dst
, "Destination",
384 wid_gw
, wid_gw
, "Gateway",
385 wid_flags
, wid_flags
, "Flags",
386 wid_refs
, wid_refs
, "Refs",
387 wid_use
, wid_use
, "Use",
388 wid_if
, wid_if
, "Netif",
389 wid_expire
, "Expire");
392 printf("%-*.*s %-*.*s %-*.*s %*.*s %*s\n",
393 wid_dst
, wid_dst
, "Destination",
394 wid_gw
, wid_gw
, "Gateway",
395 wid_flags
, wid_flags
, "Flags",
396 wid_if
, wid_if
, "Netif",
397 wid_expire
, "Expire");
401 static struct sockaddr
*
402 kgetsa(struct sockaddr
*dst
)
405 kget(dst
, pt_u
.u_sa
);
406 if (pt_u
.u_sa
.sa_len
> sizeof (pt_u
.u_sa
))
407 kread((u_long
)dst
, (char *)pt_u
.u_data
, pt_u
.u_sa
.sa_len
);
412 p_tree(struct radix_node
*rn
)
417 if (rnode
.rn_bit
< 0) {
419 printf("%-8.8lx ", (u_long
)rn
);
420 if (rnode
.rn_flags
& RNF_ROOT
) {
422 printf("(root node)%s",
423 rnode
.rn_dupedkey
? " =>\n" : "\n");
424 } else if (do_rtent
) {
430 p_sockaddr(kgetsa((struct sockaddr
*)rnode
.rn_key
),
434 if ((rn
= rnode
.rn_dupedkey
))
437 if (Aflag
&& do_rtent
) {
438 printf("%-8.8lx ", (u_long
)rn
);
442 p_tree(rnode
.rn_left
);
452 struct radix_mask
*rm
= rnode
.rn_mklist
;
454 if (rnode
.rn_bit
< 0) {
457 p_sockaddr(kgetsa((struct sockaddr
*)rnode
.rn_mask
),
462 sprintf(nbuf
, "(%d)", rnode
.rn_bit
);
463 printf("%6.6s %8.8lx : %8.8lx", nbuf
, (u_long
)rnode
.rn_left
, (u_long
)rnode
.rn_right
);
467 sprintf(nbuf
, " %d refs, ", rmask
.rm_refs
);
468 printf(" mk = %8.8lx {(%d),%s",
469 (u_long
)rm
, -1 - rmask
.rm_bit
, rmask
.rm_refs
? nbuf
: " ");
470 if (rmask
.rm_flags
& RNF_NORMAL
) {
471 struct radix_node rnode_aux
;
472 printf(" <normal>, ");
473 kget(rmask
.rm_leaf
, rnode_aux
);
474 p_sockaddr(kgetsa((struct sockaddr
*)rnode_aux
.rn_mask
),
477 p_sockaddr(kgetsa((struct sockaddr
*)rmask
.rm_mask
),
480 if ((rm
= rmask
.rm_next
))
492 char *buf
, *next
, *lim
;
493 struct rt_msghdr
*rtm
;
499 mib
[4] = NET_RT_DUMP
;
507 if (sysctl(mib
, miblen
, NULL
, &needed
, NULL
, 0) < 0) {
508 err(1, "sysctl: net.route.0.0.dump estimate");
511 if ((buf
= malloc(needed
)) == 0) {
512 err(2, "malloc(%lu)", (unsigned long)needed
);
514 if (sysctl(mib
, miblen
, buf
, &needed
, NULL
, 0) < 0) {
515 err(1, "sysctl: net.route.0.0.dump");
518 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
519 rtm
= (struct rt_msghdr
*)next
;
525 np_rtentry(struct rt_msghdr
*rtm
)
527 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
529 static int masks_done
, banner_printed
;
532 int af
= 0, interesting
= RTF_UP
| RTF_GATEWAY
| RTF_HOST
;
535 /* for the moment, netmasks are skipped over */
536 if (!banner_printed
) {
537 printf("Netmasks:\n");
540 if (masks_done
== 0) {
541 if (rtm
->rtm_addrs
!= RTA_DST
) {
552 if (rtm
->rtm_addrs
== RTA_DST
)
553 p_sockaddr(sa
, NULL
, 0, 36);
555 p_sockaddr(sa
, NULL
, rtm
->rtm_flags
, 16);
556 sa
= (struct sockaddr
*)(ROUNDUP(sa
->sa_len
) + (char *)sa
);
557 p_sockaddr(sa
, NULL
, 0, 18);
559 p_flags(rtm
->rtm_flags
& interesting
, "%-6.6s ");
564 p_sockaddr(struct sockaddr
*sa
, struct sockaddr
*mask
, int flags
, int width
)
568 cp
= fmt_sockaddr(sa
, mask
, flags
);
574 printf("%-*s ", width
, cp
);
576 printf("%-*.*s ", width
, width
, cp
);
581 fmt_sockaddr(struct sockaddr
*sa
, struct sockaddr
*mask
, int flags
)
583 static char workbuf
[128];
587 switch(sa
->sa_family
) {
590 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
592 if ((sin
->sin_addr
.s_addr
== INADDR_ANY
) &&
594 ntohl(((struct sockaddr_in
*)mask
)->sin_addr
.s_addr
)
597 else if (flags
& RTF_HOST
)
598 cp
= routename(sin
->sin_addr
.s_addr
);
600 cp
= netname(sin
->sin_addr
.s_addr
,
601 ntohl(((struct sockaddr_in
*)mask
)
604 cp
= netname(sin
->sin_addr
.s_addr
, 0L);
611 struct sockaddr_in6
*sa6
= (struct sockaddr_in6
*)sa
;
612 struct in6_addr
*in6
= &sa6
->sin6_addr
;
615 * XXX: This is a special workaround for KAME kernels.
616 * sin6_scope_id field of SA should be set in the future.
618 if (IN6_IS_ADDR_LINKLOCAL(in6
) ||
619 IN6_IS_ADDR_MC_LINKLOCAL(in6
)) {
620 /* XXX: override is ok? */
621 sa6
->sin6_scope_id
= (u_int32_t
)ntohs(*(u_short
*)&in6
->s6_addr
[2]);
622 *(u_short
*)&in6
->s6_addr
[2] = 0;
625 if (flags
& RTF_HOST
)
626 cp
= routename6(sa6
);
629 &((struct sockaddr_in6
*)mask
)->sin6_addr
);
631 cp
= netname6(sa6
, NULL
);
639 struct ipx_addr work
= ((struct sockaddr_ipx
*)sa
)->sipx_addr
;
640 if (ipx_nullnet(satoipx_addr(work
)))
648 if (!(flags
& RTF_HOST
) && mask
)
649 cp
= atalk_print2(sa
,mask
,9);
651 cp
= atalk_print(sa
,11);
656 printf("%s", ((struct sockaddr_ng
*)sa
)->sg_data
);
667 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
669 if (sdl
->sdl_nlen
== 0 && sdl
->sdl_alen
== 0 &&
671 (void) sprintf(workbuf
, "link#%d", sdl
->sdl_index
);
673 switch (sdl
->sdl_type
) {
677 if (sdl
->sdl_alen
== ETHER_ADDR_LEN
) {
678 cp
= ether_ntoa((struct ether_addr
*)
679 (sdl
->sdl_data
+ sdl
->sdl_nlen
));
692 u_char
*s
= (u_char
*)sa
->sa_data
, *slim
;
694 slim
= sa
->sa_len
+ (u_char
*) sa
;
695 cplim
= cp
+ sizeof(workbuf
) - 6;
696 cp
+= sprintf(cp
, "(%d)", sa
->sa_family
);
697 while (s
< slim
&& cp
< cplim
) {
698 cp
+= sprintf(cp
, " %02x", *s
++);
700 cp
+= sprintf(cp
, "%02x", *s
++);
710 p_flags(int f
, char *format
)
712 printf(format
, fmt_flags(f
));
718 static char name
[33];
720 struct bits
*p
= bits
;
722 for (flags
= name
; p
->b_mask
; p
++)
730 p_rtentry(struct rtentry
*rt
)
732 static struct ifnet ifnet
, *lastif
;
733 struct rtentry parent
;
734 static char buffer
[128];
735 static char prettyname
[128];
740 * Don't print protocol-cloned routes unless -a.
742 if (rt
->rt_flags
& RTF_WASCLONED
&& !aflag
) {
743 kget(rt
->rt_parent
, parent
);
744 if (parent
.rt_flags
& RTF_PRCLONING
)
748 bzero(&addr
, sizeof(addr
));
749 if ((sa
= kgetsa(rt_key(rt
))))
750 bcopy(sa
, &addr
, sa
->sa_len
);
751 bzero(&mask
, sizeof(mask
));
752 if (rt_mask(rt
) && (sa
= kgetsa(rt_mask(rt
))))
753 bcopy(sa
, &mask
, sa
->sa_len
);
754 p_sockaddr(&addr
.u_sa
, &mask
.u_sa
, rt
->rt_flags
, wid_dst
);
755 p_sockaddr(kgetsa(rt
->rt_gateway
), NULL
, RTF_HOST
, wid_gw
);
756 snprintf(buffer
, sizeof(buffer
), "%%-%d.%ds ", wid_flags
, wid_flags
);
757 p_flags(rt
->rt_flags
, buffer
);
758 if (addr
.u_sa
.sa_family
== AF_INET
|| Wflag
) {
759 printf("%*ld %*lu ", wid_refs
, rt
->rt_refcnt
,
760 wid_use
, rt
->rt_use
);
762 if (rt
->rt_rmx
.rmx_mtu
!= 0)
763 printf("%*lu ", wid_mtu
, rt
->rt_rmx
.rmx_mtu
);
765 printf("%*s ", wid_mtu
, "");
769 if (rt
->rt_ifp
!= lastif
) {
770 kget(rt
->rt_ifp
, ifnet
);
772 strlcpy(prettyname
, ifnet
.if_xname
, sizeof(prettyname
));
774 printf("%*.*s", wid_if
, wid_if
, prettyname
);
775 if (rt
->rt_rmx
.rmx_expire
) {
779 rt
->rt_rmx
.rmx_expire
- time((time_t *)0)) > 0)
780 printf(" %*d", wid_expire
, (int)expire_time
);
782 if (rt
->rt_nodes
[0].rn_dupedkey
)
792 static char line
[MAXHOSTNAMELEN
];
797 hp
= gethostbyaddr((char *)&in
, sizeof (struct in_addr
),
801 trimdomain(cp
, strlen(cp
));
805 strncpy(line
, cp
, sizeof(line
) - 1);
806 line
[sizeof(line
) - 1] = '\0';
808 #define C(x) ((x) & 0xff)
810 sprintf(line
, "%lu.%lu.%lu.%lu",
811 C(in
>> 24), C(in
>> 16), C(in
>> 8), C(in
));
823 else if (IN_CLASSB(a
))
831 domask(char *dst
, u_long addr
, u_long mask
)
835 if (!mask
|| (forgemask(addr
) == mask
)) {
840 for (b
= 0; b
< 32; b
++)
841 if (mask
& (1 << b
)) {
845 for (bb
= b
+1; bb
< 32; bb
++)
846 if (!(mask
& (1 << bb
))) {
847 i
= -1; /* noncontig */
853 sprintf(dst
, "&0x%lx", mask
);
855 sprintf(dst
, "/%d", 32-i
);
859 * Return the name of the network whose address is given.
860 * The address is assumed to be that of a net or subnet, not a host.
863 netname(u_long in
, u_long mask
)
866 static char line
[MAXHOSTNAMELEN
];
867 struct netent
*np
= 0;
871 #define NSHIFT(m) ( \
872 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
873 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
874 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
878 dmask
= forgemask(i
);
879 if (!numeric_addr
&& i
) {
880 np
= getnetbyaddr(i
>> NSHIFT(mask
), AF_INET
);
881 if (np
== NULL
&& mask
== 0)
882 np
= getnetbyaddr(i
>> NSHIFT(dmask
), AF_INET
);
885 trimdomain(cp
, strlen(cp
));
890 strncpy(line
, cp
, sizeof(line
) - 1);
891 line
[sizeof(line
) - 1] = '\0';
893 if (mask
<= IN_CLASSA_NET
&&
894 (i
& IN_CLASSA_HOST
) == 0) {
895 sprintf(line
, "%lu", C(i
>> 24));
896 } else if (mask
<= IN_CLASSB_NET
&&
897 (i
& IN_CLASSB_HOST
) == 0) {
898 sprintf(line
, "%lu.%lu",
899 C(i
>> 24), C(i
>> 16));
900 } else if (mask
<= IN_CLASSC_NET
&&
901 (i
& IN_CLASSC_HOST
) == 0) {
902 sprintf(line
, "%lu.%lu.%lu",
903 C(i
>> 24), C(i
>> 16), C(i
>> 8));
905 sprintf(line
, "%lu.%lu.%lu.%lu",
906 C(i
>> 24), C(i
>> 16), C(i
>> 8), C(i
));
909 domask(line
+ strlen(line
), i
, mask
);
915 netname6(struct sockaddr_in6
*sa6
, struct in6_addr
*mask
)
917 static char line
[MAXHOSTNAMELEN
];
918 u_char
*p
= (u_char
*)mask
;
920 int masklen
, illegal
= 0, flag
= NI_WITHSCOPEID
;
923 for (masklen
= 0, lim
= p
+ 16; p
< lim
; p
++) {
957 fprintf(stderr
, "illegal prefixlen\n");
962 if (masklen
== 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
))
966 flag
|= NI_NUMERICHOST
;
967 getnameinfo((struct sockaddr
*)sa6
, sa6
->sin6_len
, line
, sizeof(line
),
971 sprintf(&line
[strlen(line
)], "/%d", masklen
);
977 routename6(struct sockaddr_in6
*sa6
)
979 static char line
[MAXHOSTNAMELEN
];
980 int flag
= NI_WITHSCOPEID
;
981 /* use local variable for safety */
982 struct sockaddr_in6 sa6_local
= {AF_INET6
, sizeof(sa6_local
),};
984 sa6_local
.sin6_addr
= sa6
->sin6_addr
;
985 sa6_local
.sin6_scope_id
= sa6
->sin6_scope_id
;
988 flag
|= NI_NUMERICHOST
;
990 getnameinfo((struct sockaddr
*)&sa6_local
, sa6_local
.sin6_len
,
991 line
, sizeof(line
), NULL
, 0, flag
);
998 * Print routing statistics
1003 struct rtstatistics rts
;
1006 error
= kinfo_get_net_rtstatistics(&rts
);
1008 printf("routing: could not retrieve statistics\n");
1011 printf("routing:\n");
1013 #define p(f, m) if (rts.f || sflag <= 1) \
1014 printf(m, rts.f, plural(rts.f))
1016 p(rts_badredirect
, "\t%lu bad routing redirect%s\n");
1017 p(rts_dynamic
, "\t%lu dynamically created route%s\n");
1018 p(rts_newgateway
, "\t%lu new gateway%s due to redirects\n");
1019 p(rts_unreach
, "\t%lu destination%s found unreachable\n");
1020 p(rts_wildcard
, "\t%lu use%s of a wildcard route\n");
1025 ipx_print(struct sockaddr
*sa
)
1028 struct servent
*sp
= 0;
1029 char *net
= "", *host
= "";
1032 struct ipx_addr work
= ((struct sockaddr_ipx
*)sa
)->sipx_addr
;
1033 static char mybuf
[50];
1034 char cport
[10], chost
[15], cnet
[15];
1036 port
= ntohs(work
.x_port
);
1038 if (ipx_nullnet(work
) && ipx_nullhost(work
)) {
1042 sprintf(mybuf
, "*.%s", sp
->s_name
);
1044 sprintf(mybuf
, "*.%x", port
);
1046 sprintf(mybuf
, "*.*");
1051 if (ipx_wildnet(work
))
1053 else if (ipx_nullnet(work
))
1056 q
= work
.x_net
.c_net
;
1057 sprintf(cnet
, "%02x%02x%02x%02x",
1058 q
[0], q
[1], q
[2], q
[3]);
1059 for (p
= cnet
; *p
== '0' && p
< cnet
+ 8; p
++)
1064 if (ipx_wildhost(work
))
1066 else if (ipx_nullhost(work
))
1069 q
= work
.x_host
.c_host
;
1070 sprintf(chost
, "%02x%02x%02x%02x%02x%02x",
1071 q
[0], q
[1], q
[2], q
[3], q
[4], q
[5]);
1072 for (p
= chost
; *p
== '0' && p
< chost
+ 12; p
++)
1078 if (strcmp(host
, "*") == 0)
1081 snprintf(cport
, sizeof(cport
),
1082 "%s%s", *host
? "." : "", sp
->s_name
);
1084 snprintf(cport
, sizeof(cport
),
1085 "%s%x", *host
? "." : "", port
);
1089 snprintf(mybuf
, sizeof(mybuf
), "%s.%s%s", net
, host
, cport
);
1094 ipx_phost(struct sockaddr
*sa
)
1096 struct sockaddr_ipx
*sipx
= (struct sockaddr_ipx
*)sa
;
1097 struct sockaddr_ipx work
;
1098 static union ipx_net ipx_zeronet
;
1103 in
= work
.sipx_addr
;
1105 work
.sipx_addr
.x_port
= 0;
1106 work
.sipx_addr
.x_net
= ipx_zeronet
;
1107 p
= ipx_print((struct sockaddr
*)&work
);
1108 if (strncmp("*.", p
, 2) == 0) p
+= 2;
1114 short ns_nullh
[] = {0,0,0};
1115 short ns_bh
[] = {-1,-1,-1};
1118 ns_print(struct sockaddr
*sa
)
1120 struct sockaddr_ns
*sns
= (struct sockaddr_ns
*)sa
;
1121 struct ns_addr work
;
1122 union { union ns_net net_e
; u_long long_e
; } net
;
1124 static char mybuf
[50], cport
[10], chost
[25];
1128 work
= sns
->sns_addr
;
1129 port
= ntohs(work
.x_port
);
1131 net
.net_e
= work
.x_net
;
1132 if (ns_nullhost(work
) && net
.long_e
== 0) {
1134 sprintf(mybuf
, "*.%xH", port
);
1137 sprintf(mybuf
, "*.*");
1141 if (bcmp(ns_bh
, work
.x_host
.c_host
, 6) == 0) {
1143 } else if (bcmp(ns_nullh
, work
.x_host
.c_host
, 6) == 0) {
1146 q
= work
.x_host
.c_host
;
1147 sprintf(chost
, "%02x%02x%02x%02x%02x%02xH",
1148 q
[0], q
[1], q
[2], q
[3], q
[4], q
[5]);
1149 for (p
= chost
; *p
== '0' && p
< chost
+ 12; p
++)
1154 sprintf(cport
, ".%xH", htons(port
));
1158 sprintf(mybuf
,"%xH.%s%s", ntohl(net
.long_e
), host
, cport
);
1164 ns_phost(struct sockaddr
*sa
)
1166 struct sockaddr_ns
*sns
= (struct sockaddr_ns
*)sa
;
1167 struct sockaddr_ns work
;
1168 static union ns_net ns_zeronet
;
1172 work
.sns_addr
.x_port
= 0;
1173 work
.sns_addr
.x_net
= ns_zeronet
;
1175 p
= ns_print((struct sockaddr
*)&work
);
1176 if (strncmp("0H.", p
, 3) == 0)