免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3203 | 回复: 3
打印 上一主题 下一主题

发一个netstat源代码。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-05-28 13:18 |只看该作者 |倒序浏览
atalk.c

  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. #ifndef lint
  34. /*
  35. static char sccsid[] = "@(#)atalk.c        1.1 (Whistle) 6/6/96";
  36. */
  37. static const char rcsid[] =
  38.   "$FreeBSD: src/usr.bin/netstat/atalk.c,v 1.13.2.2 2001/09/17 14:53:17 ru Exp $";
  39. #endif /* not lint */

  40. #include <sys/param.h>;
  41. #include <sys/queue.h>;
  42. #include <sys/socket.h>;
  43. #include <sys/socketvar.h>;
  44. #include <sys/protosw.h>;

  45. #include <net/route.h>;

  46. #include <netatalk/at.h>;
  47. #include <netatalk/ddp_var.h>;

  48. #include <errno.h>;
  49. #include <nlist.h>;
  50. #include <netdb.h>;
  51. #include <stdio.h>;
  52. #include <string.h>;
  53. #include "netstat.h"

  54. struct        ddpcb ddpcb;
  55. struct        socket sockb;

  56. static        int first = 1;

  57. /*
  58. * Print a summary of connections related to a Network Systems
  59. * protocol.  For XXX, also give state of connection.
  60. * Listening processes (aflag) are suppressed unless the
  61. * -a (all) flag is specified.
  62. */

  63. static char *
  64. at_pr_net(struct sockaddr_at *sat, int numeric)
  65. {
  66. static        char mybuf[50];

  67.         if (!numeric) {
  68.                 switch(sat->;sat_addr.s_net) {
  69.                 case 0xffff:
  70.                         return "????";
  71.                 case ATADDR_ANYNET:
  72.                         return("*");
  73.                 }
  74.         }
  75.         sprintf(mybuf,"%hu",ntohs(sat->;sat_addr.s_net));
  76.         return mybuf;
  77. }

  78. static char *
  79. at_pr_host(struct sockaddr_at *sat, int numeric)
  80. {
  81. static        char mybuf[50];

  82.         if (!numeric) {
  83.                 switch(sat->;sat_addr.s_node) {
  84.                 case ATADDR_BCAST:
  85.                         return "bcast";
  86.                 case ATADDR_ANYNODE:
  87.                         return("*");
  88.                 }
  89.         }
  90.         sprintf(mybuf,"%d",(unsigned int)sat->;sat_addr.s_node);
  91.         return mybuf;
  92. }

  93. static char *
  94. at_pr_port(struct sockaddr_at *sat)
  95. {
  96. static        char mybuf[50];
  97.         struct servent *serv;

  98.         switch(sat->;sat_port) {
  99.         case ATADDR_ANYPORT:
  100.                 return("*");
  101.         case 0xff:
  102.                 return "????";
  103.         default:
  104.                 if (numeric_port) {
  105.                         (void)snprintf(mybuf, sizeof(mybuf), "%d",
  106.                             (unsigned int)sat->;sat_port);
  107.                 } else {
  108.                         serv = getservbyport(sat->;sat_port, "ddp");
  109.                         if (serv == NULL)
  110.                                 (void)snprintf(mybuf, sizeof(mybuf), "%d",
  111.                                     (unsigned int) sat->;sat_port);
  112.                         else
  113.                                 (void) snprintf(mybuf, sizeof(mybuf), "%s",
  114.                                     serv->;s_name);
  115.                 }
  116.         }
  117.         return mybuf;
  118. }

  119. static char *
  120. at_pr_range(struct sockaddr_at *sat)
  121. {
  122. static        char mybuf[50];

  123.         if(sat->;sat_range.r_netrange.nr_firstnet
  124.            != sat->;sat_range.r_netrange.nr_lastnet) {
  125.                 sprintf(mybuf,"%d-%d",
  126.                         ntohs(sat->;sat_range.r_netrange.nr_firstnet),
  127.                         ntohs(sat->;sat_range.r_netrange.nr_lastnet));
  128.         } else {
  129.                 sprintf(mybuf,"%d",
  130.                         ntohs(sat->;sat_range.r_netrange.nr_firstnet));
  131.         }
  132.         return mybuf;
  133. }


  134. /* what == 0 for addr only == 3 */
  135. /*         1 for net */
  136. /*         2 for host */
  137. /*         4 for port */
  138. /*         8 for numeric only */
  139. char *
  140. atalk_print(struct sockaddr *sa, int what)
  141. {
  142.         struct sockaddr_at *sat = (struct sockaddr_at *)sa;
  143.         static        char mybuf[50];
  144.         int numeric = (what & 0x08);

  145.         mybuf[0] = 0;
  146.         switch (what & 0x13) {
  147.         case 0:
  148.                 mybuf[0] = 0;
  149.                 break;
  150.         case 1:
  151.                 sprintf(mybuf,"%s",at_pr_net(sat, numeric));
  152.                 break;
  153.         case 2:
  154.                 sprintf(mybuf,"%s",at_pr_host(sat, numeric));
  155.                 break;
  156.         case 3:
  157.                 sprintf(mybuf,"%s.%s",
  158.                                 at_pr_net(sat, numeric),
  159.                                 at_pr_host(sat, numeric));
  160.                 break;
  161.         case 0x10:
  162.                 sprintf(mybuf,"%s", at_pr_range(sat));
  163.         }
  164.         if (what & 4) {
  165.                 sprintf(mybuf+strlen(mybuf),".%s",at_pr_port(sat));
  166.         }
  167.         return mybuf;
  168. }

  169. char *
  170. atalk_print2(struct sockaddr *sa, struct sockaddr *mask, int what)
  171. {
  172.   int n;
  173.   static char buf[100];
  174.   struct sockaddr_at *sat1, *sat2;
  175.   struct sockaddr_at thesockaddr;
  176.   struct sockaddr *sa2;

  177.   sat1 = (struct sockaddr_at *)sa;
  178.   sat2 = (struct sockaddr_at *)mask;
  179.   sa2 = (struct sockaddr *)&

  180.   thesockaddr.sat_addr.s_net = sat1->;sat_addr.s_net & sat2->;sat_addr.s_net;
  181.   n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 |(what & 8)));
  182.   if(sat2->;sat_addr.s_net != 0xFFFF) {
  183.     thesockaddr.sat_addr.s_net = sat1->;sat_addr.s_net | ~sat2->;sat_addr.s_net;
  184.     n += snprintf(buf + n, sizeof(buf) - n,
  185.                 "-%s", atalk_print(sa2, 1 |(what & 8)));
  186.   }
  187.   if(what & 2)
  188.   n += snprintf(buf + n, sizeof(buf) - n, ".%s", atalk_print(sa, what&(~1)));
  189.   return(buf);
  190. }

  191. void
  192. atalkprotopr(u_long off __unused, char *name, int af __unused)
  193. {
  194.         struct ddpcb *this, *next;

  195.         if (off == 0)
  196.                 return;
  197.         kread(off, (char *)&this, sizeof (struct ddpcb *));
  198.         for ( ; this != NULL; this = next) {
  199.                 kread((u_long)this, (char *)&ddpcb, sizeof (ddpcb));
  200.                 next = ddpcb.ddp_next;
  201. #if 0
  202.                 if (!aflag && atalk_nullhost(ddpcb.ddp_lsat) ) {
  203.                         continue;
  204.                 }
  205. #endif
  206.                 kread((u_long)ddpcb.ddp_socket, (char *)&sockb, sizeof (sockb));
  207.                 if (first) {
  208.                         printf("Active ATALK connections");
  209.                         if (aflag)
  210.                                 printf(" (including servers)");
  211.                         putchar('\n');
  212.                         if (Aflag)
  213.                                 printf("%-8.8s ", "PCB");
  214.                         printf(Aflag ?
  215.                                 "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
  216.                                 "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
  217.                                 "Proto", "Recv-Q", "Send-Q",
  218.                                 "Local Address", "Foreign Address", "(state)");
  219.                         first = 0;
  220.                 }
  221.                 if (Aflag)
  222.                         printf("%8lx ", (u_long) this);
  223.                 printf("%-5.5s %6lu %6lu ", name, sockb.so_rcv.sb_cc,
  224.                         sockb.so_snd.sb_cc);
  225.                 printf(Aflag?" %-18.18s":" %-22.22s", atalk_print(
  226.                                         (struct sockaddr *)&ddpcb.ddp_lsat,7));
  227.                 printf(Aflag?" %-18.18s":" %-22.22s", atalk_print(
  228.                                         (struct sockaddr *)&ddpcb.ddp_fsat,7));
  229.                 putchar('\n');
  230.         }
  231. }

  232. #define ANY(x,y,z) if (x || sflag <= 1) \
  233.         printf("\t%lu %s%s%s\n",x,y,plural(x),z)

  234. /*
  235. * Dump DDP statistics structure.
  236. */
  237. void
  238. ddp_stats(u_long off __unused, char *name, int af __unused)
  239. {
  240.         struct ddpstat ddpstat;

  241.         if (off == 0)
  242.                 return;
  243.         kread(off, (char *)&ddpstat, sizeof (ddpstat));
  244.         printf("%s:\n", name);
  245.         ANY(ddpstat.ddps_short, "packet", " with short headers ");
  246.         ANY(ddpstat.ddps_long, "packet", " with long headers ");
  247.         ANY(ddpstat.ddps_nosum, "packet", " with no checksum ");
  248.         ANY(ddpstat.ddps_tooshort, "packet", " too short ");
  249.         ANY(ddpstat.ddps_badsum, "packet", " with bad checksum ");
  250.         ANY(ddpstat.ddps_toosmall, "packet", " with not enough data ");
  251.         ANY(ddpstat.ddps_forward, "packet", " forwarded ");
  252.         ANY(ddpstat.ddps_encap, "packet", " encapsulated ");
  253.         ANY(ddpstat.ddps_cantforward, "packet", " rcvd for unreachable dest ");
  254.         ANY(ddpstat.ddps_nosockspace, "packet", " dropped due to no socket space ");
  255. }
复制代码


route.c

  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. #ifndef lint
  34. #if 0
  35. static char sccsid[] = "From: @(#)route.c        8.6 (Berkeley) 4/28/95";
  36. #endif
  37. static const char rcsid[] =
  38.   "$FreeBSD: src/usr.bin/netstat/route.c,v 1.41.2.11 2001/10/18 10:33:25 ru Exp $";
  39. #endif /* not lint */

  40. #include <sys/param.h>;
  41. #include <sys/protosw.h>;
  42. #include <sys/socket.h>;
  43. #include <sys/time.h>;

  44. #include <net/if.h>;
  45. #include <net/if_var.h>;
  46. #include <net/if_dl.h>;
  47. #include <net/if_types.h>;
  48. #include <net/route.h>;

  49. #include <netinet/in.h>;
  50. #include <netipx/ipx.h>;
  51. #include <netatalk/at.h>;
  52. #include <netgraph/ng_socket.h>;

  53. #ifdef NS
  54. #include <netns/ns.h>;
  55. #endif

  56. #include <sys/sysctl.h>;

  57. #include <arpa/inet.h>;
  58. #include <libutil.h>;
  59. #include <netdb.h>;
  60. #include <stdio.h>;
  61. #include <stdlib.h>;
  62. #include <string.h>;
  63. #include <unistd.h>;
  64. #include <err.h>;
  65. #include <time.h>;
  66. #include "netstat.h"

  67. #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))


  68. /* alignment constraint for routing socket */
  69. #define ROUNDUP(a) \
  70.        ((a) >; 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
  71. #define ADVANCE(x, n) (x += ROUNDUP((n)->;sa_len))

  72. /*
  73. * Definitions for showing gateway flags.
  74. */
  75. struct 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_CLONING,        'C' },
  87.         { RTF_XRESOLVE,        'X' },
  88.         { RTF_LLINFO,        'L' },
  89.         { RTF_STATIC,        'S' },
  90.         { RTF_PROTO1,        '1' },
  91.         { RTF_PROTO2,        '2' },
  92.         { RTF_WASCLONED,'W' },
  93.         { RTF_PRCLONING,'c' },
  94.         { RTF_PROTO3,        '3' },
  95.         { RTF_BLACKHOLE,'B' },
  96.         { RTF_BROADCAST,'b' },
  97.         { 0 }
  98. };

  99. typedef union {
  100.         long        dummy;                /* Helps align structure. */
  101.         struct        sockaddr u_sa;
  102.         u_short        u_data[128];
  103. } sa_u;

  104. static sa_u pt_u;

  105. int        do_rtent = 0;
  106. struct        rtentry rtentry;
  107. struct        radix_node rnode;
  108. struct        radix_mask rmask;
  109. struct        radix_node_head *rt_tables[AF_MAX+1];

  110. int        NewTree = 0;

  111. static struct sockaddr *kgetsa (struct sockaddr *);
  112. static void p_tree (struct radix_node *);
  113. static void p_rtnode (void);
  114. static void ntreestuff (void);
  115. static void np_rtentry (struct rt_msghdr *);
  116. static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int);
  117. static void p_flags (int, char *);
  118. static void p_rtentry (struct rtentry *);
  119. static u_long forgemask (u_long);
  120. static void domask (char *, u_long, u_long);

  121. /*
  122. * Print routing tables.
  123. */
  124. void
  125. routepr(u_long rtree)
  126. {
  127.         struct radix_node_head *rnh, head;
  128.         int i;

  129.         printf("Routing tables\n");

  130.         if (Aflag == 0 && NewTree)
  131.                 ntreestuff();
  132.         else {
  133.                 if (rtree == 0) {
  134.                         printf("rt_tables: symbol not in namelist\n");
  135.                         return;
  136.                 }

  137.                 kget(rtree, rt_tables);
  138.                 for (i = 0; i <= AF_MAX; i++) {
  139.                         if ((rnh = rt_tables[i]) == 0)
  140.                                 continue;
  141.                         kget(rnh, head);
  142.                         if (i == AF_UNSPEC) {
  143.                                 if (Aflag && af == 0) {
  144.                                         printf("Netmasks:\n");
  145.                                         p_tree(head.rnh_treetop);
  146.                                 }
  147.                         } else if (af == AF_UNSPEC || af == i) {
  148.                                 pr_family(i);
  149.                                 do_rtent = 1;
  150.                                 pr_rthdr(i);
  151.                                 p_tree(head.rnh_treetop);
  152.                         }
  153.                 }
  154.         }
  155. }

  156. /*
  157. * Print address family header before a section of the routing table.
  158. */
  159. void
  160. pr_family(int af)
  161. {
  162.         char *afname;

  163.         switch (af) {
  164.         case AF_INET:
  165.                 afname = "Internet";
  166.                 break;
  167. #ifdef INET6
  168.         case AF_INET6:
  169.                 afname = "Internet6";
  170.                 break;
  171. #endif /*INET6*/
  172.         case AF_IPX:
  173.                 afname = "IPX";
  174.                 break;
  175. #ifdef NS
  176.         case AF_NS:
  177.                 afname = "XNS";
  178.                 break;
  179. #endif
  180.         case AF_ISO:
  181.                 afname = "ISO";
  182.                 break;
  183.         case AF_APPLETALK:
  184.                 afname = "AppleTalk";
  185.                 break;
  186.         case AF_CCITT:
  187.                 afname = "X.25";
  188.                 break;
  189.         case AF_NETGRAPH:
  190.                 afname = "Netgraph";
  191.                 break;
  192.         default:
  193.                 afname = NULL;
  194.                 break;
  195.         }
  196.         if (afname)
  197.                 printf("\n%s:\n", afname);
  198.         else
  199.                 printf("\nProtocol Family %d:\n", af);
  200. }

  201. /* column widths; each followed by one space */
  202. #ifndef INET6
  203. #define        WID_DST(af)         18        /* width of destination column */
  204. #define        WID_GW(af)        18        /* width of gateway column */
  205. #define        WID_IF(af)        6        /* width of netif column */
  206. #else
  207. #define        WID_DST(af) \
  208.         ((af) == AF_INET6 ? (Wflag ? 39 : (numeric_addr ? 33: 18)) : 18)
  209. #define        WID_GW(af) \
  210.         ((af) == AF_INET6 ? (Wflag ? 31 : (numeric_addr ? 29 : 18)) : 18)
  211. #define        WID_IF(af)        ((af) == AF_INET6 ? 8 : 6)
  212. #endif /*INET6*/

  213. /*
  214. * Print header for routing table columns.
  215. */
  216. void
  217. pr_rthdr(int af)
  218. {

  219.         if (Aflag)
  220.                 printf("%-8.8s ","Address");
  221.         if (af == AF_INET || Wflag)
  222.                 if (Wflag)
  223.                         printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %*.*s %6s\n",
  224.                                 WID_DST(af), WID_DST(af), "Destination",
  225.                                 WID_GW(af), WID_GW(af), "Gateway",
  226.                                 "Flags", "Refs", "Use", "Mtu",
  227.                                 WID_IF(af), WID_IF(af), "Netif", "Expire");
  228.                 else
  229.                         printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %*.*s %6s\n",
  230.                                 WID_DST(af), WID_DST(af), "Destination",
  231.                                 WID_GW(af), WID_GW(af), "Gateway",
  232.                                 "Flags", "Refs", "Use",
  233.                                 WID_IF(af), WID_IF(af), "Netif", "Expire");
  234.         else
  235.                 printf("%-*.*s %-*.*s %-6.6s  %8.8s %6s\n",
  236.                         WID_DST(af), WID_DST(af), "Destination",
  237.                         WID_GW(af), WID_GW(af), "Gateway",
  238.                         "Flags", "Netif", "Expire");
  239. }

  240. static struct sockaddr *
  241. kgetsa(struct sockaddr *dst)
  242. {

  243.         kget(dst, pt_u.u_sa);
  244.         if (pt_u.u_sa.sa_len >; sizeof (pt_u.u_sa))
  245.                 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
  246.         return (&pt_u.u_sa);
  247. }

  248. static void
  249. p_tree(struct radix_node *rn)
  250. {

  251. again:
  252.         kget(rn, rnode);
  253.         if (rnode.rn_bit < 0) {
  254.                 if (Aflag)
  255.                         printf("%-8.8lx ", (u_long)rn);
  256.                 if (rnode.rn_flags & RNF_ROOT) {
  257.                         if (Aflag)
  258.                                 printf("(root node)%s",
  259.                                     rnode.rn_dupedkey ? " =>;\n" : "\n");
  260.                 } else if (do_rtent) {
  261.                         kget(rn, rtentry);
  262.                         p_rtentry(&rtentry);
  263.                         if (Aflag)
  264.                                 p_rtnode();
  265.                 } else {
  266.                         p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
  267.                                    NULL, 0, 44);
  268.                         putchar('\n');
  269.                 }
  270.                 if ((rn = rnode.rn_dupedkey))
  271.                         goto again;
  272.         } else {
  273.                 if (Aflag && do_rtent) {
  274.                         printf("%-8.8lx ", (u_long)rn);
  275.                         p_rtnode();
  276.                 }
  277.                 rn = rnode.rn_right;
  278.                 p_tree(rnode.rn_left);
  279.                 p_tree(rn);
  280.         }
  281. }

  282. char        nbuf[20];

  283. static void
  284. p_rtnode(void)
  285. {
  286.         struct radix_mask *rm = rnode.rn_mklist;

  287.         if (rnode.rn_bit < 0) {
  288.                 if (rnode.rn_mask) {
  289.                         printf("\t  mask ");
  290.                         p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
  291.                                    NULL, 0, -1);
  292.                 } else if (rm == 0)
  293.                         return;
  294.         } else {
  295.                 sprintf(nbuf, "(%d)", rnode.rn_bit);
  296.                 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right);
  297.         }
  298.         while (rm) {
  299.                 kget(rm, rmask);
  300.                 sprintf(nbuf, " %d refs, ", rmask.rm_refs);
  301.                 printf(" mk = %8.8lx {(%d),%s",
  302.                         (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " ");
  303.                 if (rmask.rm_flags & RNF_NORMAL) {
  304.                         struct radix_node rnode_aux;
  305.                         printf(" <normal>;, ");
  306.                         kget(rmask.rm_leaf, rnode_aux);
  307.                         p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
  308.                                     NULL, 0, -1);
  309.                 } else
  310.                     p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
  311.                                 NULL, 0, -1);
  312.                 putchar('}');
  313.                 if ((rm = rmask.rm_mklist))
  314.                         printf(" ->;");
  315.         }
  316.         putchar('\n');
  317. }

  318. static void
  319. ntreestuff(void)
  320. {
  321.         size_t needed;
  322.         int mib[6];
  323.         char *buf, *next, *lim;
  324.         register struct rt_msghdr *rtm;

  325.         mib[0] = CTL_NET;
  326.         mib[1] = PF_ROUTE;
  327.         mib[2] = 0;
  328.         mib[3] = 0;
  329.         mib[4] = NET_RT_DUMP;
  330.         mib[5] = 0;
  331.         if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
  332.                 err(1, "sysctl: net.route.0.0.dump estimate");
  333.         }

  334.         if ((buf = malloc(needed)) == 0) {
  335.                 err(2, "malloc(%lu)", (unsigned long)needed);
  336.         }
  337.         if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
  338.                 err(1, "sysctl: net.route.0.0.dump");
  339.         }
  340.         lim  = buf + needed;
  341.         for (next = buf; next < lim; next += rtm->;rtm_msglen) {
  342.                 rtm = (struct rt_msghdr *)next;
  343.                 np_rtentry(rtm);
  344.         }
  345. }

  346. static void
  347. np_rtentry(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. #ifdef notdef
  356.         /* for the moment, netmasks are skipped over */
  357.         if (!banner_printed) {
  358.                 printf("Netmasks:\n");
  359.                 banner_printed = 1;
  360.         }
  361.         if (masks_done == 0) {
  362.                 if (rtm->;rtm_addrs != RTA_DST ) {
  363.                         masks_done = 1;
  364.                         af = sa->;sa_family;
  365.                 }
  366.         } else
  367. #endif
  368.                 af = sa->;sa_family;
  369.         if (af != old_af) {
  370.                 pr_family(af);
  371.                 old_af = af;
  372.         }
  373.         if (rtm->;rtm_addrs == RTA_DST)
  374.                 p_sockaddr(sa, NULL, 0, 36);
  375.         else {
  376.                 p_sockaddr(sa, NULL, rtm->;rtm_flags, 16);
  377.                 sa = (struct sockaddr *)(ROUNDUP(sa->;sa_len) + (char *)sa);
  378.                 p_sockaddr(sa, NULL, 0, 18);
  379.         }
  380.         p_flags(rtm->;rtm_flags & interesting, "%-6.6s ");
  381.         putchar('\n');
  382. }

  383. static void
  384. p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
  385. {
  386.         char workbuf[128], *cplim;
  387.         register char *cp = workbuf;

  388.         switch(sa->;sa_family) {
  389.         case AF_INET:
  390.             {
  391.                 register struct sockaddr_in *sin = (struct sockaddr_in *)sa;

  392.                 if ((sin->;sin_addr.s_addr == INADDR_ANY) &&
  393.                         mask &&
  394.                         ntohl(((struct sockaddr_in *)mask)->;sin_addr.s_addr)
  395.                                 ==0L)
  396.                                 cp = "default" ;
  397.                 else if (flags & RTF_HOST)
  398.                         cp = routename(sin->;sin_addr.s_addr);
  399.                 else if (mask)
  400.                         cp = netname(sin->;sin_addr.s_addr,
  401.                                      ntohl(((struct sockaddr_in *)mask)
  402.                                            ->;sin_addr.s_addr));
  403.                 else
  404.                         cp = netname(sin->;sin_addr.s_addr, 0L);
  405.                 break;
  406.             }

  407. #ifdef INET6
  408.         case AF_INET6:
  409.             {
  410.                 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
  411.                 struct in6_addr *in6 = &sa6->;sin6_addr;

  412.                 /*
  413.                  * XXX: This is a special workaround for KAME kernels.
  414.                  * sin6_scope_id field of SA should be set in the future.
  415.                  */
  416.                 if (IN6_IS_ADDR_LINKLOCAL(in6) ||
  417.                     IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
  418.                     /* XXX: override is ok? */
  419.                     sa6->;sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->;s6_addr[2]);
  420.                     *(u_short *)&in6->;s6_addr[2] = 0;
  421.                 }

  422.                 if (flags & RTF_HOST)
  423.                     cp = routename6(sa6);
  424.                 else if (mask)
  425.                     cp = netname6(sa6,
  426.                                   &((struct sockaddr_in6 *)mask)->;sin6_addr);
  427.                 else {
  428.                     cp = netname6(sa6, NULL);
  429.                 }
  430.                 break;
  431.             }
  432. #endif /*INET6*/

  433.         case AF_IPX:
  434.             {
  435.                 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->;sipx_addr;
  436.                 if (ipx_nullnet(satoipx_addr(work)))
  437.                         cp = "default";
  438.                 else
  439.                         cp = ipx_print(sa);
  440.                 break;
  441.             }
  442.         case AF_APPLETALK:
  443.             {
  444.                 if (!(flags & RTF_HOST) && mask)
  445.                         cp = atalk_print2(sa,mask,9);
  446.                 else
  447.                         cp = atalk_print(sa,11);
  448.                 break;
  449.             }
  450.         case AF_NETGRAPH:
  451.             {
  452.                 printf("%s", ((struct sockaddr_ng *)sa)->;sg_data);
  453.                 break;
  454.             }
  455. #ifdef NS
  456.         case AF_NS:
  457.                 cp = ns_print(sa);
  458.                 break;
  459. #endif

  460.         case AF_LINK:
  461.             {
  462.                 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;

  463.                 if (sdl->;sdl_nlen == 0 && sdl->;sdl_alen == 0 &&
  464.                     sdl->;sdl_slen == 0)
  465.                         (void) sprintf(workbuf, "link#%d", sdl->;sdl_index);
  466.                 else
  467.                         switch (sdl->;sdl_type) {

  468.                         case IFT_ETHER:
  469.                             {
  470.                                 register int i;
  471.                                 register u_char *lla = (u_char *)sdl->;sdl_data +
  472.                                     sdl->;sdl_nlen;

  473.                                 cplim = "";
  474.                                 for (i = 0; i < sdl->;sdl_alen; i++, lla++) {
  475.                                         cp += sprintf(cp, "%s%x", cplim, *lla);
  476.                                         cplim = ":";
  477.                                 }
  478.                                 cp = workbuf;
  479.                                 break;
  480.                             }

  481.                         default:
  482.                                 cp = link_ntoa(sdl);
  483.                                 break;
  484.                         }
  485.                 break;
  486.             }

  487.         default:
  488.             {
  489.                 register u_char *s = (u_char *)sa->;sa_data, *slim;

  490.                 slim =  sa->;sa_len + (u_char *) sa;
  491.                 cplim = cp + sizeof(workbuf) - 6;
  492.                 cp += sprintf(cp, "(%d)", sa->;sa_family);
  493.                 while (s < slim && cp < cplim) {
  494.                         cp += sprintf(cp, " %02x", *s++);
  495.                         if (s < slim)
  496.                             cp += sprintf(cp, "%02x", *s++);
  497.                 }
  498.                 cp = workbuf;
  499.             }
  500.         }
  501.         if (width < 0 )
  502.                 printf("%s ", cp);
  503.         else {
  504.                 if (numeric_addr)
  505.                         printf("%-*s ", width, cp);
  506.                 else
  507.                         printf("%-*.*s ", width, width, cp);
  508.         }
  509. }

  510. static void
  511. p_flags(int f, char *format)
  512. {
  513.         char name[33], *flags;
  514.         register struct bits *p = bits;

  515.         for (flags = name; p->;b_mask; p++)
  516.                 if (p->;b_mask & f)
  517.                         *flags++ = p->;b_val;
  518.         *flags = '\0';
  519.         printf(format, name);
  520. }

  521. static void
  522. p_rtentry(struct rtentry *rt)
  523. {
  524.         static struct ifnet ifnet, *lastif;
  525.         struct rtentry parent;
  526.         static char name[16];
  527.         static char prettyname[9];
  528.         struct sockaddr *sa;
  529.         sa_u addr, mask;

  530.         /*
  531.          * Don't print protocol-cloned routes unless -a.
  532.          */
  533.         if (rt->;rt_flags & RTF_WASCLONED && !aflag) {
  534.                 kget(rt->;rt_parent, parent);
  535.                 if (parent.rt_flags & RTF_PRCLONING)
  536.                         return;
  537.         }

  538.         bzero(&addr, sizeof(addr));
  539.         if ((sa = kgetsa(rt_key(rt))))
  540.                 bcopy(sa, &addr, sa->;sa_len);
  541.         bzero(&mask, sizeof(mask));
  542.         if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
  543.                 bcopy(sa, &mask, sa->;sa_len);
  544.         p_sockaddr(&addr.u_sa, &mask.u_sa, rt->;rt_flags,
  545.             WID_DST(addr.u_sa.sa_family));
  546.         p_sockaddr(kgetsa(rt->;rt_gateway), NULL, RTF_HOST,
  547.             WID_GW(addr.u_sa.sa_family));
  548.         p_flags(rt->;rt_flags, "%-6.6s ");
  549.         if (addr.u_sa.sa_family == AF_INET || Wflag) {
  550.                 printf("%6ld %8ld ", rt->;rt_refcnt, rt->;rt_use);
  551.                 if (Wflag) {
  552.                         if (rt->;rt_rmx.rmx_mtu != 0)
  553.                                 printf("%6lu ", rt->;rt_rmx.rmx_mtu);
  554.                         else
  555.                                 printf("%6s ", "");
  556.                 }
  557.         }
  558.         if (rt->;rt_ifp) {
  559.                 if (rt->;rt_ifp != lastif) {
  560.                         kget(rt->;rt_ifp, ifnet);
  561.                         kread((u_long)ifnet.if_name, name, 16);
  562.                         lastif = rt->;rt_ifp;
  563.                         snprintf(prettyname, sizeof prettyname,
  564.                                  "%s%d", name, ifnet.if_unit);
  565.                 }
  566.                 printf("%*.*s", WID_IF(addr.u_sa.sa_family),
  567.                     WID_IF(addr.u_sa.sa_family), prettyname);
  568.                 if (rt->;rt_rmx.rmx_expire) {
  569.                         time_t expire_time;

  570.                         if ((expire_time =
  571.                             rt->;rt_rmx.rmx_expire - time((time_t *)0)) >; 0)
  572.                                 printf(" %6d", (int)expire_time);
  573.                 }
  574.                 if (rt->;rt_nodes[0].rn_dupedkey)
  575.                         printf(" =>;");
  576.         }
  577.         putchar('\n');
  578. }

  579. char *
  580. routename(u_long in)
  581. {
  582.         register char *cp;
  583.         static char line[MAXHOSTNAMELEN];
  584.         struct hostent *hp;

  585.         cp = 0;
  586.         if (!numeric_addr) {
  587.                 hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
  588.                         AF_INET);
  589.                 if (hp) {
  590.                         cp = hp->;h_name;
  591.                         trimdomain(cp, strlen(cp));
  592.                 }
  593.         }
  594.         if (cp) {
  595.                 strncpy(line, cp, sizeof(line) - 1);
  596.                 line[sizeof(line) - 1] = '\0';
  597.         } else {
  598. #define C(x)        ((x) & 0xff)
  599.                 in = ntohl(in);
  600.                 sprintf(line, "%lu.%lu.%lu.%lu",
  601.                     C(in >;>; 24), C(in >;>; 16), C(in >;>; 8), C(in));
  602.         }
  603.         return (line);
  604. }

  605. static u_long
  606. forgemask(u_long a)
  607. {
  608.         u_long m;

  609.         if (IN_CLASSA(a))
  610.                 m = IN_CLASSA_NET;
  611.         else if (IN_CLASSB(a))
  612.                 m = IN_CLASSB_NET;
  613.         else
  614.                 m = IN_CLASSC_NET;
  615.         return (m);
  616. }

  617. static void
  618. domask(char *dst, u_long addr, u_long mask)
  619. {
  620.         register int b, i;

  621.         if (!mask || (forgemask(addr) == mask)) {
  622.                 *dst = '\0';
  623.                 return;
  624.         }
  625.         i = 0;
  626.         for (b = 0; b < 32; b++)
  627.                 if (mask & (1 << b)) {
  628.                         register int bb;

  629.                         i = b;
  630.                         for (bb = b+1; bb < 32; bb++)
  631.                                 if (!(mask & (1 << bb))) {
  632.                                         i = -1;        /* noncontig */
  633.                                         break;
  634.                                 }
  635.                         break;
  636.                 }
  637.         if (i == -1)
  638.                 sprintf(dst, "&0x%lx", mask);
  639.         else
  640.                 sprintf(dst, "/%d", 32-i);
  641. }

  642. /*
  643. * Return the name of the network whose address is given.
  644. * The address is assumed to be that of a net or subnet, not a host.
  645. */
  646. char *
  647. netname(u_long in, u_long mask)
  648. {
  649.         char *cp = 0;
  650.         static char line[MAXHOSTNAMELEN];
  651.         struct netent *np = 0;
  652.         u_long dmask;
  653.         register u_long i;

  654. #define        NSHIFT(m) (                                                        \
  655.         (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT :                        \
  656.         (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT :                        \
  657.         (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT :                        \
  658.         0)

  659.         i = ntohl(in);
  660.         dmask = forgemask(i);
  661.         if (!numeric_addr && i) {
  662.                 np = getnetbyaddr(i >;>; NSHIFT(mask), AF_INET);
  663.                 if (np == NULL && mask == 0)
  664.                         np = getnetbyaddr(i >;>; NSHIFT(dmask), AF_INET);
  665.                 if (np != NULL) {
  666.                         cp = np->;n_name;
  667.                         trimdomain(cp, strlen(cp));
  668.                 }
  669.         }
  670. #undef NSHIFT
  671.         if (cp != NULL) {
  672.                 strncpy(line, cp, sizeof(line) - 1);
  673.                 line[sizeof(line) - 1] = '\0';
  674.         } else {
  675.                 switch (dmask) {
  676.                 case IN_CLASSA_NET:
  677.                         if ((i & IN_CLASSA_HOST) == 0) {
  678.                                 sprintf(line, "%lu", C(i >;>; 24));
  679.                                 break;
  680.                         }
  681.                         /* FALLTHROUGH */
  682.                 case IN_CLASSB_NET:
  683.                         if ((i & IN_CLASSB_HOST) == 0) {
  684.                                 sprintf(line, "%lu.%lu",
  685.                                         C(i >;>; 24), C(i >;>; 16));
  686.                                 break;
  687.                         }
  688.                         /* FALLTHROUGH */
  689.                 case IN_CLASSC_NET:
  690.                         if ((i & IN_CLASSC_HOST) == 0) {
  691.                                 sprintf(line, "%lu.%lu.%lu",
  692.                                         C(i >;>; 24), C(i >;>; 16), C(i >;>; 8));
  693.                                 break;
  694.                         }
  695.                         /* FALLTHROUGH */
  696.                 default:
  697.                         sprintf(line, "%lu.%lu.%lu.%lu",
  698.                                 C(i >;>; 24), C(i >;>; 16), C(i >;>; 8), C(i));
  699.                         break;
  700.                 }
  701.         }
  702.         domask(line + strlen(line), i, mask);
  703.         return (line);
  704. }

  705. #ifdef INET6
  706. char *
  707. netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
  708. {
  709.         static char line[MAXHOSTNAMELEN];
  710.         u_char *p = (u_char *)mask;
  711.         u_char *lim;
  712.         int masklen, illegal = 0, flag = NI_WITHSCOPEID;

  713.         if (mask) {
  714.                 for (masklen = 0, lim = p + 16; p < lim; p++) {
  715.                         switch (*p) {
  716.                          case 0xff:
  717.                                  masklen += 8;
  718.                                  break;
  719.                          case 0xfe:
  720.                                  masklen += 7;
  721.                                  break;
  722.                          case 0xfc:
  723.                                  masklen += 6;
  724.                                  break;
  725.                          case 0xf8:
  726.                                  masklen += 5;
  727.                                  break;
  728.                          case 0xf0:
  729.                                  masklen += 4;
  730.                                  break;
  731.                          case 0xe0:
  732.                                  masklen += 3;
  733.                                  break;
  734.                          case 0xc0:
  735.                                  masklen += 2;
  736.                                  break;
  737.                          case 0x80:
  738.                                  masklen += 1;
  739.                                  break;
  740.                          case 0x00:
  741.                                  break;
  742.                          default:
  743.                                  illegal ++;
  744.                                  break;
  745.                         }
  746.                 }
  747.                 if (illegal)
  748.                         fprintf(stderr, "illegal prefixlen\n");
  749.         }
  750.         else
  751.                 masklen = 128;

  752.         if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->;sin6_addr))
  753.                 return("default");

  754.         if (numeric_addr)
  755.                 flag |= NI_NUMERICHOST;
  756.         getnameinfo((struct sockaddr *)sa6, sa6->;sin6_len, line, sizeof(line),
  757.                     NULL, 0, flag);

  758.         if (numeric_addr)
  759.                 sprintf(&line[strlen(line)], "/%d", masklen);

  760.         return line;
  761. }

  762. char *
  763. routename6(struct sockaddr_in6 *sa6)
  764. {
  765.         static char line[MAXHOSTNAMELEN];
  766.         int flag = NI_WITHSCOPEID;
  767.         /* use local variable for safety */
  768.         struct sockaddr_in6 sa6_local = {AF_INET6, sizeof(sa6_local),};

  769.         sa6_local.sin6_addr = sa6->;sin6_addr;
  770.         sa6_local.sin6_scope_id = sa6->;sin6_scope_id;

  771.         if (numeric_addr)
  772.                 flag |= NI_NUMERICHOST;

  773.         getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
  774.                     line, sizeof(line), NULL, 0, flag);

  775.         return line;
  776. }
  777. #endif /*INET6*/

  778. /*
  779. * Print routing statistics
  780. */
  781. void
  782. rt_stats(u_long rtsaddr, u_long rttaddr)
  783. {
  784.         struct rtstat rtstat;
  785.         int rttrash;

  786.         if (rtsaddr == 0) {
  787.                 printf("rtstat: symbol not in namelist\n");
  788.                 return;
  789.         }
  790.         if (rttaddr == 0) {
  791.                 printf("rttrash: symbol not in namelist\n");
  792.                 return;
  793.         }
  794.         kread(rtsaddr, (char *)&rtstat, sizeof (rtstat));
  795.         kread(rttaddr, (char *)&rttrash, sizeof (rttrash));
  796.         printf("routing:\n");

  797. #define        p(f, m) if (rtstat.f || sflag <= 1) \
  798.         printf(m, rtstat.f, plural(rtstat.f))

  799.         p(rts_badredirect, "\t%u bad routing redirect%s\n");
  800.         p(rts_dynamic, "\t%u dynamically created route%s\n");
  801.         p(rts_newgateway, "\t%u new gateway%s due to redirects\n");
  802.         p(rts_unreach, "\t%u destination%s found unreachable\n");
  803.         p(rts_wildcard, "\t%u use%s of a wildcard route\n");
  804. #undef p

  805.         if (rttrash || sflag <= 1)
  806.                 printf("\t%u route%s not in table but not freed\n",
  807.                     rttrash, plural(rttrash));
  808. }

  809. char *
  810. ipx_print(struct sockaddr *sa)
  811. {
  812.         u_short port;
  813.         struct servent *sp = 0;
  814.         char *net = "", *host = "";
  815.         register char *p;
  816.         register u_char *q;
  817.         struct ipx_addr work = ((struct sockaddr_ipx *)sa)->;sipx_addr;
  818.         static char mybuf[50];
  819.         char cport[10], chost[15], cnet[15];

  820.         port = ntohs(work.x_port);

  821.         if (ipx_nullnet(work) && ipx_nullhost(work)) {

  822.                 if (port) {
  823.                         if (sp)
  824.                                 sprintf(mybuf, "*.%s", sp->;s_name);
  825.                         else
  826.                                 sprintf(mybuf, "*.%x", port);
  827.                 } else
  828.                         sprintf(mybuf, "*.*");

  829.                 return (mybuf);
  830.         }

  831.         if (ipx_wildnet(work))
  832.                 net = "any";
  833.         else if (ipx_nullnet(work))
  834.                 net = "*";
  835.         else {
  836.                 q = work.x_net.c_net;
  837.                 sprintf(cnet, "%02x%02x%02x%02x",
  838.                         q[0], q[1], q[2], q[3]);
  839.                 for (p = cnet; *p == '0' && p < cnet + 8; p++)
  840.                         continue;
  841.                 net = p;
  842.         }

  843.         if (ipx_wildhost(work))
  844.                 host = "any";
  845.         else if (ipx_nullhost(work))
  846.                 host = "*";
  847.         else {
  848.                 q = work.x_host.c_host;
  849.                 sprintf(chost, "%02x%02x%02x%02x%02x%02x",
  850.                         q[0], q[1], q[2], q[3], q[4], q[5]);
  851.                 for (p = chost; *p == '0' && p < chost + 12; p++)
  852.                         continue;
  853.                 host = p;
  854.         }

  855.         if (port) {
  856.                 if (strcmp(host, "*") == 0)
  857.                         host = "";
  858.                 if (sp)       
  859.                         snprintf(cport, sizeof(cport),
  860.                                 "%s%s", *host ? "." : "", sp->;s_name);
  861.                 else       
  862.                         snprintf(cport, sizeof(cport),
  863.                                 "%s%x", *host ? "." : "", port);
  864.         } else
  865.                 *cport = 0;

  866.         snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
  867.         return(mybuf);
  868. }

  869. char *
  870. ipx_phost(struct sockaddr *sa)
  871. {
  872.         register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
  873.         struct sockaddr_ipx work;
  874.         static union ipx_net ipx_zeronet;
  875.         char *p;
  876.         struct ipx_addr in;

  877.         work = *sipx;
  878.         in = work.sipx_addr;

  879.         work.sipx_addr.x_port = 0;
  880.         work.sipx_addr.x_net = ipx_zeronet;
  881.         p = ipx_print((struct sockaddr *)&work);
  882.         if (strncmp("*.", p, 2) == 0) p += 2;

  883.         return(p);
  884. }

  885. #ifdef NS
  886. short ns_nullh[] = {0,0,0};
  887. short ns_bh[] = {-1,-1,-1};

  888. char *
  889. ns_print(struct sockaddr *sa)
  890. {
  891.         register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
  892.         struct ns_addr work;
  893.         union { union ns_net net_e; u_long long_e; } net;
  894.         u_short port;
  895.         static char mybuf[50], cport[10], chost[25];
  896.         char *host = "";
  897.         register char *p; register u_char *q;

  898.         work = sns->;sns_addr;
  899.         port = ntohs(work.x_port);
  900.         work.x_port = 0;
  901.         net.net_e  = work.x_net;
  902.         if (ns_nullhost(work) && net.long_e == 0) {
  903.                 if (port ) {
  904.                         sprintf(mybuf, "*.%xH", port);
  905.                         upHex(mybuf);
  906.                 } else
  907.                         sprintf(mybuf, "*.*");
  908.                 return (mybuf);
  909.         }

  910.         if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
  911.                 host = "any";
  912.         } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
  913.                 host = "*";
  914.         } else {
  915.                 q = work.x_host.c_host;
  916.                 sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
  917.                         q[0], q[1], q[2], q[3], q[4], q[5]);
  918.                 for (p = chost; *p == '0' && p < chost + 12; p++)
  919.                         continue;
  920.                 host = p;
  921.         }
  922.         if (port)
  923.                 sprintf(cport, ".%xH", htons(port));
  924.         else
  925.                 *cport = 0;

  926.         sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
  927.         upHex(mybuf);
  928.         return(mybuf);
  929. }

  930. char *
  931. ns_phost(struct sockaddr *sa)
  932. {
  933.         register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
  934.         struct sockaddr_ns work;
  935.         static union ns_net ns_zeronet;
  936.         char *p;

  937.         work = *sns;
  938.         work.sns_addr.x_port = 0;
  939.         work.sns_addr.x_net = ns_zeronet;

  940.         p = ns_print((struct sockaddr *)&work);
  941.         if (strncmp("0H.", p, 3) == 0)
  942.                 p += 3;
  943.         return(p);
  944. }
  945. #endif

  946. void
  947. upHex(char *p0)
  948. {
  949.         register char *p = p0;

  950.         for (; *p; p++)
  951.                 switch (*p) {

  952.                 case 'a':
  953.                 case 'b':
  954.                 case 'c':
  955.                 case 'd':
  956.                 case 'e':
  957.                 case 'f':
  958.                         *p += ('A' - 'a');
  959.                         break;
  960.                 }
  961. }
复制代码


netstat.h

  1. /*
  2. * Copyright (c) 1992, 1993
  3. *        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. *        @(#)netstat.h        8.2 (Berkeley) 1/4/94
  34. * $FreeBSD: src/usr.bin/netstat/netstat.h,v 1.16.2.7 2001/09/17 15:17:46 ru Exp $
  35. */

  36. #include <sys/cdefs.h>;

  37. extern int        Aflag;        /* show addresses of protocol control block */
  38. extern int        aflag;        /* show all sockets (including servers) */
  39. extern int        bflag;        /* show i/f total bytes in/out */
  40. extern int        dflag;        /* show i/f dropped packets */
  41. extern int        gflag;        /* show group (multicast) routing or stats */
  42. extern int        iflag;        /* show interfaces */
  43. extern int        Lflag;        /* show size of listen queues */
  44. extern int        mflag;        /* show memory stats */
  45. extern int        numeric_addr;        /* show addresses numerically */
  46. extern int        numeric_port;        /* show ports numerically */
  47. extern int        rflag;        /* show routing tables (or routing stats) */
  48. extern int        sflag;        /* show protocol statistics */
  49. extern int        tflag;        /* show i/f watchdog timers */
  50. extern int        Wflag;        /* wide display */
  51. extern int        zflag;        /* zero stats */

  52. extern int        interval; /* repeat interval for i/f stats */

  53. extern char        *interface; /* desired i/f for stats, or NULL for all i/fs */
  54. extern int        unit;        /* unit number for above */

  55. extern int        af;        /* address family */

  56. int        kread (u_long addr, char *buf, int size);
  57. char        *plural (int);
  58. char        *plurales (int);

  59. void        protopr (u_long, char *, int);
  60. void        tcp_stats (u_long, char *, int);
  61. void        udp_stats (u_long, char *, int);
  62. void        ip_stats (u_long, char *, int);
  63. void        icmp_stats (u_long, char *, int);
  64. void        igmp_stats (u_long, char *, int);
  65. #ifdef IPSEC
  66. void        ipsec_stats (u_long, char *, int);
  67. #endif

  68. #ifdef INET6
  69. void        ip6_stats (u_long, char *, int);
  70. void        ip6_ifstats (char *);
  71. void        icmp6_stats (u_long, char *, int);
  72. void        icmp6_ifstats (char *);
  73. void        pim6_stats (u_long, char *, int);
  74. void        rip6_stats (u_long, char *, int);
  75. void        mroute6pr (u_long, u_long);
  76. void        mrt6_stats (u_long);

  77. struct sockaddr_in6;
  78. struct in6_addr;
  79. char *routename6 (struct sockaddr_in6 *);
  80. char *netname6 (struct sockaddr_in6 *, struct in6_addr *);
  81. #endif /*INET6*/

  82. #ifdef IPSEC
  83. void        pfkey_stats (u_long, char *, int);
  84. #endif

  85. void        bdg_stats (u_long, char *, int);

  86. void        mbpr (u_long, u_long, u_long, u_long);

  87. void        hostpr (u_long, u_long);
  88. void        impstats (u_long, u_long);

  89. void        intpr (int, u_long, void (*)(char *));

  90. void        pr_rthdr (int);
  91. void        pr_family (int);
  92. void        rt_stats (u_long, u_long);
  93. char        *ipx_pnet (struct sockaddr *);
  94. char        *ipx_phost (struct sockaddr *);
  95. char        *ns_phost (struct sockaddr *);
  96. void        upHex (char *);

  97. char        *routename (u_long);
  98. char        *netname (u_long, u_long);
  99. char        *atalk_print (struct sockaddr *, int);
  100. char        *atalk_print2 (struct sockaddr *, struct sockaddr *, int);
  101. char        *ipx_print (struct sockaddr *);
  102. char        *ns_print (struct sockaddr *);
  103. void        routepr (u_long);

  104. void        ipxprotopr (u_long, char *, int);
  105. void        spx_stats (u_long, char *, int);
  106. void        ipx_stats (u_long, char *, int);
  107. void        ipxerr_stats (u_long, char *, int);

  108. void        nsprotopr (u_long, char *, int);
  109. void        spp_stats (u_long, char *, int);
  110. void        idp_stats (u_long, char *, int);
  111. void        nserr_stats (u_long, char *, int);

  112. void        atalkprotopr (u_long, char *, int);
  113. void        ddp_stats (u_long, char *, int);

  114. void        netgraphprotopr (u_long, char *, int);

  115. void        unixpr (void);

  116. void        esis_stats (u_long, char *, int);
  117. void        clnp_stats (u_long, char *, int);
  118. void        cltp_stats (u_long, char *, int);
  119. void        iso_protopr (u_long, char *, int);
  120. void        iso_protopr1 (u_long, int);
  121. void        tp_protopr (u_long, char *, int);
  122. void        tp_inproto (u_long);
  123. void        tp_stats (caddr_t, caddr_t);

  124. void        mroutepr (u_long, u_long);
  125. void        mrt_stats (u_long);
复制代码


netgraph.h

  1. /*
  2. * Copyright (c) 1996-1999 Whistle Communications, Inc.
  3. * All rights reserved.
  4. *
  5. * Subject to the following obligations and disclaimer of warranty, use and
  6. * redistribution of this software, in source or object code forms, with or
  7. * without modifications are expressly permitted by Whistle Communications;
  8. * provided, however, that:
  9. * 1. Any and all reproductions of the source or object code must include the
  10. *    copyright notice above and the following disclaimer of warranties; and
  11. * 2. No rights are granted, in any manner or form, to use Whistle
  12. *    Communications, Inc. trademarks, including the mark "WHISTLE
  13. *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  14. *    such appears in the above copyright notice or in the software.
  15. *
  16. * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  17. * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  18. * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
  19. * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
  22. * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
  23. * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
  24. * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
  25. * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
  26. * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  27. * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * $FreeBSD: src/usr.bin/netstat/netgraph.c,v 1.3.2.2 2001/08/10 09:07:09 ru Exp $
  35. */

  36. #ifndef lint
  37. static const char rcsid[] =
  38.         "$Id: atalk.c,v 1.11 1998/07/06 21:01:22 bde Exp $";
  39. #endif /* not lint */

  40. #include <sys/param.h>;
  41. #include <sys/queue.h>;
  42. #include <sys/socket.h>;
  43. #include <sys/socketvar.h>;
  44. #include <sys/protosw.h>;
  45. #include <sys/linker.h>;

  46. #include <net/route.h>;

  47. #include <netgraph.h>;
  48. #include <netgraph/ng_message.h>;
  49. #include <netgraph/ng_socket.h>;
  50. #include <netgraph/ng_socketvar.h>;

  51. #include <nlist.h>;
  52. #include <errno.h>;
  53. #include <stdio.h>;
  54. #include <string.h>;
  55. #include <unistd.h>;
  56. #include <err.h>;
  57. #include "netstat.h"

  58. static        int first = 1;
  59. static        int csock = -1;

  60. void
  61. netgraphprotopr(u_long off, char *name, int af __unused)
  62. {
  63.         struct ngpcb *this, *next;
  64.         struct ngpcb ngpcb;
  65.         struct ngsock info;
  66.         struct socket sockb;
  67.         int debug = 1;

  68.         /* If symbol not found, try looking in the KLD module */
  69.         if (off == 0) {
  70.                 const char *const modname = "ng_socket.ko";
  71. /* XXX We should get "mpath" from "sysctl kern.module_path" */
  72.                 const char *mpath[] = { "/", "/boot/", "/modules/", NULL };
  73.                 struct nlist sym[] = { { "_ngsocklist" }, { NULL } };
  74.                 const char **pre;
  75.                 struct kld_file_stat ks;
  76.                 int fileid;

  77.                 /* See if module is loaded */
  78.                 if ((fileid = kldfind(modname)) < 0) {
  79.                         if (debug)
  80.                                 warn("kldfind(%s)", modname);
  81.                         return;
  82.                 }

  83.                 /* Get module info */
  84.                 memset(&ks, 0, sizeof(ks));
  85.                 ks.version = sizeof(struct kld_file_stat);
  86.                 if (kldstat(fileid, &ks) < 0) {
  87.                         if (debug)
  88.                                 warn("kldstat(%d)", fileid);
  89.                         return;
  90.                 }

  91.                 /* Get symbol table from module file */
  92.                 for (pre = mpath; *pre; pre++) {
  93.                         char path[MAXPATHLEN];

  94.                         snprintf(path, sizeof(path), "%s%s", *pre, modname);
  95.                         if (nlist(path, sym) == 0)
  96.                                 break;
  97.                 }

  98.                 /* Did we find it? */
  99.                 if (sym[0].n_value == 0) {
  100.                         if (debug)
  101.                                 warnx("%s not found", modname);
  102.                         return;
  103.                 }

  104.                 /* Symbol found at load address plus symbol offset */
  105.                 off = (u_long) ks.address + sym[0].n_value;
  106.         }

  107.         /* Get pointer to first socket */
  108.         kread(off, (char *)&this, sizeof(this));

  109.         /* Get my own socket node */
  110.         if (csock == -1)
  111.                 NgMkSockNode(NULL, &csock, NULL);

  112.         for (; this != NULL; this = next) {
  113.                 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
  114.                 struct ng_mesg *resp = (struct ng_mesg *) rbuf;
  115.                 struct nodeinfo *ni = (struct nodeinfo *) resp->;data;
  116.                 char path[64];

  117.                 /* Read in ngpcb structure */
  118.                 kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
  119.                 next = LIST_NEXT(&ngpcb, socks);

  120.                 /* Read in socket structure */
  121.                 kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));

  122.                 /* Check type of socket */
  123.                 if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
  124.                         continue;
  125.                 if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
  126.                         continue;

  127.                 /* Do headline */
  128.                 if (first) {
  129.                         printf("Netgraph sockets\n");
  130.                         if (Aflag)
  131.                                 printf("%-8.8s ", "PCB");
  132.                         printf("%-5.5s %-6.6s %-6.6s %-14.14s %s\n",
  133.                             "Type", "Recv-Q", "Send-Q",
  134.                             "Node Address", "#Hooks");
  135.                         first = 0;
  136.                 }

  137.                 /* Show socket */
  138.                 if (Aflag)
  139.                         printf("%8lx ", (u_long) this);
  140.                 printf("%-5.5s %6lu %6lu ",
  141.                     name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc);

  142.                 /* Get ngsock structure */
  143.                 if (ngpcb.sockdata == 0)        /* unconnected data socket */
  144.                         goto finish;
  145.                 kread((u_long)ngpcb.sockdata, (char *)&info, sizeof(info));

  146.                 /* Get info on associated node */
  147.                 if (info.node == 0 || csock == -1)
  148.                         goto finish;
  149.                 snprintf(path, sizeof(path), "[%lx]:", (u_long) info.node);
  150.                 if (NgSendMsg(csock, path,
  151.                     NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
  152.                         goto finish;
  153.                 if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
  154.                         goto finish;

  155.                 /* Display associated node info */
  156.                 if (*ni->;name != '\0')
  157.                         snprintf(path, sizeof(path), "%s:", ni->;name);
  158.                 printf("%-14.14s %4d", path, ni->;hooks);
  159. finish:
  160.                 putchar('\n');
  161.         }
  162. }
复制代码


mroute6.c
[code]
/*
* Copyright (C) 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

/*
* Copyright (c) 1989 Stephen Deering
* Copyright (c) 1992, 1993
*        The Regents of the University of California.  All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Stephen Deering of Stanford University.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*        This product includes software developed by the University of
*        California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*        @(#)mroute.c        8.2 (Berkeley) 4/28/95
*        $FreeBSD: src/usr.bin/netstat/mroute6.c,v 1.1.2.8 2001/09/17 14:53:17 ru Exp $
*/

#ifdef INET6
#include <sys/param.h>;
#include <sys/queue.h>;
#include <sys/socket.h>;
#include <sys/socketvar.h>;
#include <sys/protosw.h>;

#include <net/if.h>;
#include <net/if_var.h>;
#include <net/route.h>;

#include <netinet/in.h>;

#include <stdio.h>;

#define        KERNEL 1
#include <netinet6/ip6_mroute.h>;
#undef KERNEL

#include "netstat.h"

#define        WID_ORG        (Wflag ? 39 : (numeric_addr ? 29 : 1) /* width of origin column */
#define        WID_GRP        (Wflag ? 18 : (numeric_addr ? 16 : 1) /* width of group column */

void
mroute6pr(u_long mfcaddr, u_long mifaddr)
{
        struct mf6c *mf6ctable[MF6CTBLSIZ], *mfcp;
        struct mif6 mif6table[MAXMIFS];
        struct mf6c mfc;
        struct rtdetq rte, *rtep;
        register struct mif6 *mifp;
        register mifi_t mifi;
        register int i;
        register int banner_printed;
        register int saved_numeric_addr;
        mifi_t maxmif = 0;
        long int waitings;

        if (mfcaddr == 0 || mifaddr == 0) {
                printf("No IPv6 multicast routing compiled into this"
                       " system.\n";
                return;
        }

        saved_numeric_addr = numeric_addr;
        numeric_addr = 1;

        kread(mifaddr, (char *)&mif6table, sizeof(mif6table));
        banner_printed = 0;
        for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) {
                struct ifnet ifnet;
                char ifname[IFNAMSIZ];

                if (mifp->;m6_ifp == NULL)
                        continue;

                kread((u_long)mifp->;m6_ifp, (char *)&ifnet, sizeof(ifnet));
                maxmif = mifi;
                if (!banner_printed) {
                        printf("\nIPv6 Multicast Interface Table\n"
                               " Mif   Rate   PhyIF   "
                               "kts-In   Pkts-Out\n";
                        banner_printed = 1;
                }

                printf("  %2u   %4d",
                       mifi, mifp->;m6_rate_limit);
                printf("   %5s", (mifp->;m6_flags & MIFF_REGISTER) ?
                       "reg0" : if_indextoname(ifnet.if_index, ifname));

                printf(" %9llu  %9llu\n", (unsigned long long)mifp->;m6_pkt_in,
                    (unsigned long long)mifp->;m6_pkt_out);
        }
        if (!banner_printed)
                printf("\nIPv6 Multicast Interface Table is empty\n";

        kread(mfcaddr, (char *)&mf6ctable, sizeof(mf6ctable));
        banner_printed = 0;
        for (i = 0; i < MF6CTBLSIZ; ++i) {
                mfcp = mf6ctable;
                while(mfcp) {
                        kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
                        if (!banner_printed) {
                                printf ("\nIPv6 Multicast Forwarding Cache\n";
                                printf(" %-*.*s %-*.*s %s",
                                       WID_ORG, WID_ORG, "Origin",
                                       WID_GRP, WID_GRP, "Group",
                                       "  Packets Waits In-Mif  Out-Mifs\n";
                                banner_printed = 1;
                        }

                        printf(" %-*.*s", WID_ORG, WID_ORG,
                               routename6(&mfc.mf6c_origin));
                        printf(" %-*.*s", WID_GRP, WID_GRP,
                               routename6(&mfc.mf6c_mcastgrp));
                        printf(" %9llu", (unsigned long long)mfc.mf6c_pkt_cnt);

                        for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) {
                                waitings++;
                                kread((u_long)rtep, (char *)&rte, sizeof(rte));
                                rtep = rte.next;
                        }
                        printf("   %3ld", waitings);

                        if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT)
                                printf(" ---   ";
                        else
                                printf("  %3d   ", mfc.mf6c_parent);
                        for (mifi = 0; mifi <= maxmif; mifi++) {
                                if (IF_ISSET(mifi, &mfc.mf6c_ifset))
                                        printf(" %u", mifi);
                        }
                        printf("\n";

                        mfcp = mfc.mf6c_next;
                }
        }
        if (!banner_printed)
                printf("\nIPv6 Multicast Routing Table is empty\n";

        printf("\n";
        numeric_addr = saved_numeric_addr;
}

void
mrt6_stats(u_long mstaddr)
{
        struct mrt6stat mrtstat;

        if (mstaddr == 0) {
                printf("No IPv6 multicast routing compiled into this"
                       " system.\n";
                return;
        }

        kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
        printf("IPv6 multicast forwarding:\n");

#define        p(f, m) if (mrtstat.f || sflag <= 1) \
        printf(m, (unsigned long long)mrtstat.f, plural(mrtstat.f))
#define        p2(f, m) if (mrtstat.f || sflag <= 1) \
        pri

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2003-05-28 13:25 |只看该作者

发一个netstat源代码。

怎么后面的部分没有贴好。

辛苦你一下,跟改!

论坛徽章:
0
3 [报告]
发表于 2003-05-28 16:52 |只看该作者

发一个netstat源代码。

好像是因为太长了,不让我贴了

论坛徽章:
0
4 [报告]
发表于 2003-05-28 18:36 |只看该作者

发一个netstat源代码。

多分几个跟帖传代码的好
不然一个帖那么长
看起来也会很累的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP