]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/inet.c
2 * Copyright (c) 1983, 1988, 1993, 1995
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
36 static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
38 static const char rcsid
[] =
39 "$Id: inet.c,v 1.6 2003/07/08 22:49:49 lindak Exp $";
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/protosw.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
54 #include <netinet/ip6.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet/ip_icmp.h>
58 #include <netinet/icmp_var.h>
59 #include <netinet/igmp_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/tcp.h>
62 #include <netinet/tcpip.h>
63 #include <netinet/tcp_seq.h>
65 #include <netinet/tcp_fsm.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/tcp_debug.h>
69 #include <netinet/udp.h>
70 #include <netinet/udp_var.h>
72 #include <arpa/inet.h>
83 char *inetname (struct in_addr
*);
84 void inetprint (struct in_addr
*, int, char *, int);
86 extern void inet6print (struct in6_addr
*, int, char *, int);
87 static int udp_done
, tcp_done
;
91 typedef struct __table_private table_t
;
93 extern table_t
*_nc_table_new(uint32_t n
);
94 extern void _nc_table_free(table_t
*tin
);
96 extern void _nc_table_insert(table_t
*t
, const char *key
, void *datum
);
97 extern void *_nc_table_find(table_t
*t
, const char *key
);
98 extern void _nc_table_delete(table_t
*t
, const char *key
);
100 static table_t
*_serv_cache
= NULL
;
103 * Read and cache all known services
111 if (_serv_cache
!= NULL
) return;
113 _serv_cache
= _nc_table_new(8192);
116 while (NULL
!= (s
= getservent()))
118 if (s
->s_name
== NULL
) continue;
120 asprintf(&key
, "%hu/%s", (unsigned short)ntohs(s
->s_port
), s
->s_proto
);
121 name
= strdup(s
->s_name
);
122 name
= _nc_table_find(_serv_cache
, key
);
123 if (name
== NULL
) _nc_table_insert(_serv_cache
, key
, name
);
133 _nc_table_free(_serv_cache
);
138 _serv_cache_getservbyport(int port
, char *proto
)
140 static struct servent s
;
146 memset(&s
, 0, sizeof(struct servent
));
147 asprintf(&key
, "%u/%s", port
, (proto
== NULL
) ? "udp" : proto
);
149 s
.s_name
= _nc_table_find(_serv_cache
, key
);
151 if (s
.s_name
== NULL
) return NULL
;
162 * Print a summary of connections related to an Internet
163 * protocol. For TCP, also give state of connection.
164 * Listening processes (aflag) are suppressed unless the
165 * -a (all) flag is specified.
168 protopr(u_long proto
, /* for sysctl version we pass proto # */
172 static int first
= 1;
175 struct tcpcb
*tp
= NULL
;
177 struct xinpgen
*xig
, *oxig
;
191 mibvar
= "net.inet.tcp.pcblist";
200 mibvar
= "net.inet.udp.pcblist";
203 mibvar
= "net.inet.divert.pcblist";
206 mibvar
= "net.inet.raw.pcblist";
210 if (sysctlbyname(mibvar
, 0, &len
, 0, 0) < 0) {
212 warn("sysctl: %s", mibvar
);
215 if ((buf
= malloc(len
)) == 0) {
216 warn("malloc %lu bytes", (u_long
)len
);
219 if (sysctlbyname(mibvar
, buf
, &len
, 0, 0) < 0) {
220 warn("sysctl: %s", mibvar
);
226 * Bail-out to avoid logic error in the loop below when
227 * there is in fact no more control block to process
229 if (len
<= sizeof(struct xinpgen
)) {
234 oxig
= xig
= (struct xinpgen
*)buf
;
235 for (xig
= (struct xinpgen
*)((char *)xig
+ xig
->xig_len
);
236 xig
->xig_len
> sizeof(struct xinpgen
);
237 xig
= (struct xinpgen
*)((char *)xig
+ xig
->xig_len
)) {
239 tp
= &((struct xtcpcb
*)xig
)->xt_tp
;
240 inp
= &((struct xtcpcb
*)xig
)->xt_inp
;
241 so
= &((struct xtcpcb
*)xig
)->xt_socket
;
243 inp
= &((struct xinpcb
*)xig
)->xi_inp
;
244 so
= &((struct xinpcb
*)xig
)->xi_socket
;
247 /* Ignore sockets for protocols other than the desired one. */
248 if (so
->xso_protocol
!= (int)proto
)
251 /* Ignore PCBs which were freed during copyout. */
252 if (inp
->inp_gencnt
> oxig
->xig_gen
)
255 if ((af
== AF_INET
&& (inp
->inp_vflag
& INP_IPV4
) == 0)
257 || (af
== AF_INET6
&& (inp
->inp_vflag
& INP_IPV6
) == 0)
259 || (af
== AF_UNSPEC
&& ((inp
->inp_vflag
& INP_IPV4
) == 0
269 * Local address is not an indication of listening socket or
270 * server sockey but just rather the socket has been bound.
271 * That why many UDP sockets were not displayed in the original code.
273 if (!aflag
&& istcp
&& tp
->t_state
<= TCPS_LISTEN
)
279 inet_lnaof(inp
->inp_laddr
) == INADDR_ANY
)
281 || (af
== AF_INET6
&&
282 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
284 || (af
== AF_UNSPEC
&&
285 (((inp
->inp_vflag
& INP_IPV4
) != 0 &&
286 inet_lnaof(inp
->inp_laddr
) == INADDR_ANY
)
288 || ((inp
->inp_vflag
& INP_IPV6
) != 0 &&
289 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
296 if (Lflag
&& !so
->so_qlimit
)
301 printf("Active Internet connections");
303 printf(" (including servers)");
306 "Current listen queue sizes (qlen/incqlen/maxqlen)");
309 printf("%-8.8s ", "Socket");
311 printf("%-14.14s %-22.22s\n",
312 "Listen", "Local Address");
314 printf((Aflag
&& !Wflag
) ?
315 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" :
316 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n",
317 "Proto", "Recv-Q", "Send-Q",
318 "Local Address", "Foreign Address",
324 printf("%8lx ", (u_long
)inp
->inp_ppcb
);
326 printf("%8lx ", (u_long
)so
->so_pcb
);
331 snprintf(buf
, 15, "%d/%d/%d", so
->so_qlen
,
332 so
->so_incqlen
, so
->so_qlimit
);
333 printf("%-14.14s ", buf
);
339 if ((inp
->inp_vflag
& INP_IPV6
) != 0)
340 vchar
= ((inp
->inp_vflag
& INP_IPV4
) != 0)
344 vchar
= ((inp
->inp_vflag
& INP_IPV4
) != 0)
347 printf("%-3.3s%-2.2s %6ld %6ld ", name
, vchar
,
352 if (inp
->inp_vflag
& INP_IPV4
) {
353 inetprint(&inp
->inp_laddr
, (int)inp
->inp_lport
,
356 inetprint(&inp
->inp_faddr
,
357 (int)inp
->inp_fport
, name
, 1);
360 else if (inp
->inp_vflag
& INP_IPV6
) {
361 inet6print(&inp
->in6p_laddr
,
362 (int)inp
->inp_lport
, name
, 1);
364 inet6print(&inp
->in6p_faddr
,
365 (int)inp
->inp_fport
, name
, 1);
366 } /* else nothing printed now */
368 } else if (inp
->inp_flags
& INP_ANONPORT
) {
369 if (inp
->inp_vflag
& INP_IPV4
) {
370 inetprint(&inp
->inp_laddr
, (int)inp
->inp_lport
,
373 inetprint(&inp
->inp_faddr
,
374 (int)inp
->inp_fport
, name
, 0);
377 else if (inp
->inp_vflag
& INP_IPV6
) {
378 inet6print(&inp
->in6p_laddr
,
379 (int)inp
->inp_lport
, name
, 1);
381 inet6print(&inp
->in6p_faddr
,
382 (int)inp
->inp_fport
, name
, 0);
383 } /* else nothing printed now */
386 if (inp
->inp_vflag
& INP_IPV4
) {
387 inetprint(&inp
->inp_laddr
, (int)inp
->inp_lport
,
390 inetprint(&inp
->inp_faddr
,
391 (int)inp
->inp_fport
, name
,
396 else if (inp
->inp_vflag
& INP_IPV6
) {
397 inet6print(&inp
->in6p_laddr
,
398 (int)inp
->inp_lport
, name
, 0);
400 inet6print(&inp
->in6p_faddr
,
401 (int)inp
->inp_fport
, name
,
404 } /* else nothing printed now */
407 if (istcp
&& !Lflag
) {
408 if (tp
->t_state
< 0 || tp
->t_state
>= TCP_NSTATES
)
409 printf("%d", tp
->t_state
);
411 printf("%s", tcpstates
[tp
->t_state
]);
412 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
413 /* Show T/TCP `hidden state' */
414 if (tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
))
416 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
421 if (xig
!= oxig
&& xig
->xig_gen
!= oxig
->xig_gen
) {
422 if (oxig
->xig_count
> xig
->xig_count
) {
423 printf("Some %s sockets may have been deleted.\n",
425 } else if (oxig
->xig_count
< xig
->xig_count
) {
426 printf("Some %s sockets may have been created.\n",
429 printf("Some %s sockets may have been created or deleted",
437 * Dump TCP statistics structure.
440 tcp_stats(u_long off
, char *name
, int af
)
442 struct tcpstat tcpstat
;
443 size_t len
= sizeof tcpstat
;
445 if (sysctlbyname("net.inet.tcp.stats", &tcpstat
, &len
, 0, 0) < 0) {
446 warn("sysctl: net.inet.tcp.stats");
457 printf ("%s:\n", name
);
459 #define p(f, m) if (tcpstat.f || sflag <= 1) \
460 printf(m, tcpstat.f, plural(tcpstat.f))
461 #define p1a(f, m) if (tcpstat.f || sflag <= 1) \
463 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
464 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
465 #define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
466 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
467 #define p3(f, m) if (tcpstat.f || sflag <= 1) \
468 printf(m, tcpstat.f, plurales(tcpstat.f))
470 p(tcps_sndtotal
, "\t%lu packet%s sent\n");
471 p2(tcps_sndpack
,tcps_sndbyte
,
472 "\t\t%lu data packet%s (%lu byte%s)\n");
473 p2(tcps_sndrexmitpack
, tcps_sndrexmitbyte
,
474 "\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
475 p(tcps_mturesent
, "\t\t%lu resend%s initiated by MTU discovery\n");
476 p2a(tcps_sndacks
, tcps_delack
,
477 "\t\t%lu ack-only packet%s (%lu delayed)\n");
478 p(tcps_sndurg
, "\t\t%lu URG only packet%s\n");
479 p(tcps_sndprobe
, "\t\t%lu window probe packet%s\n");
480 p(tcps_sndwinup
, "\t\t%lu window update packet%s\n");
481 p(tcps_sndctrl
, "\t\t%lu control packet%s\n");
482 p(tcps_rcvtotal
, "\t%lu packet%s received\n");
483 p2(tcps_rcvackpack
, tcps_rcvackbyte
, "\t\t%lu ack%s (for %lu byte%s)\n");
484 p(tcps_rcvdupack
, "\t\t%lu duplicate ack%s\n");
485 p(tcps_rcvacktoomuch
, "\t\t%lu ack%s for unsent data\n");
486 p2(tcps_rcvpack
, tcps_rcvbyte
,
487 "\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
488 p2(tcps_rcvduppack
, tcps_rcvdupbyte
,
489 "\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
490 p(tcps_pawsdrop
, "\t\t%lu old duplicate packet%s\n");
491 p2(tcps_rcvpartduppack
, tcps_rcvpartdupbyte
,
492 "\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
493 p2(tcps_rcvoopack
, tcps_rcvoobyte
,
494 "\t\t%lu out-of-order packet%s (%lu byte%s)\n");
495 p2(tcps_rcvpackafterwin
, tcps_rcvbyteafterwin
,
496 "\t\t%lu packet%s (%lu byte%s) of data after window\n");
497 p(tcps_rcvwinprobe
, "\t\t%lu window probe%s\n");
498 p(tcps_rcvwinupd
, "\t\t%lu window update packet%s\n");
499 p(tcps_rcvafterclose
, "\t\t%lu packet%s received after close\n");
500 p(tcps_rcvbadsum
, "\t\t%lu discarded for bad checksum%s\n");
501 p(tcps_rcvbadoff
, "\t\t%lu discarded for bad header offset field%s\n");
502 p1a(tcps_rcvshort
, "\t\t%lu discarded because packet too short\n");
503 p(tcps_connattempt
, "\t%lu connection request%s\n");
504 p(tcps_accepts
, "\t%lu connection accept%s\n");
505 p(tcps_badsyn
, "\t%lu bad connection attempt%s\n");
506 p(tcps_listendrop
, "\t%lu listen queue overflow%s\n");
507 p(tcps_connects
, "\t%lu connection%s established (including accepts)\n");
508 p2(tcps_closed
, tcps_drops
,
509 "\t%lu connection%s closed (including %lu drop%s)\n");
510 p(tcps_cachedrtt
, "\t\t%lu connection%s updated cached RTT on close\n");
512 "\t\t%lu connection%s updated cached RTT variance on close\n");
513 p(tcps_cachedssthresh
,
514 "\t\t%lu connection%s updated cached ssthresh on close\n");
515 p(tcps_conndrops
, "\t%lu embryonic connection%s dropped\n");
516 p2(tcps_rttupdated
, tcps_segstimed
,
517 "\t%lu segment%s updated rtt (of %lu attempt%s)\n");
518 p(tcps_rexmttimeo
, "\t%lu retransmit timeout%s\n");
519 p(tcps_timeoutdrop
, "\t\t%lu connection%s dropped by rexmit timeout\n");
520 p(tcps_persisttimeo
, "\t%lu persist timeout%s\n");
521 p(tcps_persistdrop
, "\t\t%lu connection%s dropped by persist timeout\n");
522 p(tcps_keeptimeo
, "\t%lu keepalive timeout%s\n");
523 p(tcps_keepprobe
, "\t\t%lu keepalive probe%s sent\n");
524 p(tcps_keepdrops
, "\t\t%lu connection%s dropped by keepalive\n");
525 p(tcps_predack
, "\t%lu correct ACK header prediction%s\n");
526 p(tcps_preddat
, "\t%lu correct data packet header prediction%s\n");
535 * Dump UDP statistics structure.
538 udp_stats(u_long off
, char *name
, int af
)
540 struct udpstat udpstat
;
541 size_t len
= sizeof udpstat
;
544 if (sysctlbyname("net.inet.udp.stats", &udpstat
, &len
, 0, 0) < 0) {
545 warn("sysctl: net.inet.udp.stats");
556 printf("%s:\n", name
);
557 #define p(f, m) if (udpstat.f || sflag <= 1) \
558 printf(m, udpstat.f, plural(udpstat.f))
559 #define p1a(f, m) if (udpstat.f || sflag <= 1) \
561 p(udps_ipackets
, "\t%lu datagram%s received\n");
562 p1a(udps_hdrops
, "\t%lu with incomplete header\n");
563 p1a(udps_badlen
, "\t%lu with bad data length field\n");
564 p1a(udps_badsum
, "\t%lu with bad checksum\n");
566 p1a(udps_nosum
, "\t%lu with no checksum\n");
568 p1a(udps_noport
, "\t%lu dropped due to no socket\n");
570 "\t%lu broadcast/multicast datagram%s dropped due to no socket\n");
571 p1a(udps_fullsock
, "\t%lu dropped due to full socket buffers\n");
572 p1a(udpps_pcbhashmiss
, "\t%lu not for hashed pcb\n");
573 delivered
= udpstat
.udps_ipackets
-
574 udpstat
.udps_hdrops
-
575 udpstat
.udps_badlen
-
576 udpstat
.udps_badsum
-
577 udpstat
.udps_noport
-
578 udpstat
.udps_noportbcast
-
579 udpstat
.udps_fullsock
;
580 if (delivered
|| sflag
<= 1)
581 printf("\t%lu delivered\n", delivered
);
582 p(udps_opackets
, "\t%lu datagram%s output\n");
588 * Dump IP statistics structure.
591 ip_stats(u_long off
, char *name
, int af
)
593 struct ipstat ipstat
;
594 size_t len
= sizeof ipstat
;
596 if (sysctlbyname("net.inet.ip.stats", &ipstat
, &len
, 0, 0) < 0) {
597 warn("sysctl: net.inet.ip.stats");
601 printf("%s:\n", name
);
603 #define p(f, m) if (ipstat.f || sflag <= 1) \
604 printf(m, ipstat.f, plural(ipstat.f))
605 #define p1a(f, m) if (ipstat.f || sflag <= 1) \
608 p(ips_total
, "\t%lu total packet%s received\n");
609 p(ips_badsum
, "\t%lu bad header checksum%s\n");
610 p1a(ips_toosmall
, "\t%lu with size smaller than minimum\n");
611 p1a(ips_tooshort
, "\t%lu with data size < data length\n");
612 p1a(ips_toolong
, "\t%lu with ip length > max ip packet size\n");
613 p1a(ips_badhlen
, "\t%lu with header length < data size\n");
614 p1a(ips_badlen
, "\t%lu with data length < header length\n");
615 p1a(ips_badoptions
, "\t%lu with bad options\n");
616 p1a(ips_badvers
, "\t%lu with incorrect version number\n");
617 p(ips_fragments
, "\t%lu fragment%s received\n");
618 p(ips_fragdropped
, "\t%lu fragment%s dropped (dup or out of space)\n");
619 p(ips_fragtimeout
, "\t%lu fragment%s dropped after timeout\n");
620 p(ips_reassembled
, "\t%lu packet%s reassembled ok\n");
621 p(ips_delivered
, "\t%lu packet%s for this host\n");
622 p(ips_noproto
, "\t%lu packet%s for unknown/unsupported protocol\n");
623 p(ips_forward
, "\t%lu packet%s forwarded");
624 p(ips_fastforward
, " (%lu packet%s fast forwarded)");
625 if (ipstat
.ips_forward
|| sflag
<= 1)
627 p(ips_cantforward
, "\t%lu packet%s not forwardable\n");
629 "\t%lu packet%s received for unknown multicast group\n");
630 p(ips_redirectsent
, "\t%lu redirect%s sent\n");
631 p(ips_localout
, "\t%lu packet%s sent from this host\n");
632 p(ips_rawout
, "\t%lu packet%s sent with fabricated ip header\n");
634 "\t%lu output packet%s dropped due to no bufs, etc.\n");
635 p(ips_noroute
, "\t%lu output packet%s discarded due to no route\n");
636 p(ips_fragmented
, "\t%lu output datagram%s fragmented\n");
637 p(ips_ofragments
, "\t%lu fragment%s created\n");
638 p(ips_cantfrag
, "\t%lu datagram%s that can't be fragmented\n");
639 p(ips_nogif
, "\t%lu tunneling packet%s that can't find gif\n");
640 p(ips_badaddr
, "\t%lu datagram%s with bad address in header\n");
645 static char *icmpnames
[] = {
649 "destination unreachable",
655 "router advertisement",
656 "router solicitation",
661 "information request",
662 "information request reply",
663 "address mask request",
664 "address mask reply",
668 * Dump ICMP statistics.
671 icmp_stats(u_long off
, char *name
, int af
)
673 struct icmpstat icmpstat
;
675 int mib
[4]; /* CTL_NET + PF_INET + IPPROTO_ICMP + req */
680 mib
[2] = IPPROTO_ICMP
;
681 mib
[3] = ICMPCTL_STATS
;
683 len
= sizeof icmpstat
;
684 memset(&icmpstat
, 0, len
);
685 if (sysctl(mib
, 4, &icmpstat
, &len
, (void *)0, 0) < 0)
686 return; /* XXX should complain, but not traditional */
688 printf("%s:\n", name
);
690 #define p(f, m) if (icmpstat.f || sflag <= 1) \
691 printf(m, icmpstat.f, plural(icmpstat.f))
692 #define p1a(f, m) if (icmpstat.f || sflag <= 1) \
693 printf(m, icmpstat.f)
695 p(icps_error
, "\t%lu call%s to icmp_error\n");
697 "\t%lu error%s not generated 'cuz old message was icmp\n");
698 for (first
= 1, i
= 0; i
< ICMP_MAXTYPE
+ 1; i
++)
699 if (icmpstat
.icps_outhist
[i
] != 0) {
701 printf("\tOutput histogram:\n");
704 printf("\t\t%s: %lu\n", icmpnames
[i
],
705 icmpstat
.icps_outhist
[i
]);
707 p(icps_badcode
, "\t%lu message%s with bad code fields\n");
708 p(icps_tooshort
, "\t%lu message%s < minimum length\n");
709 p(icps_checksum
, "\t%lu bad checksum%s\n");
710 p(icps_badlen
, "\t%lu message%s with bad length\n");
711 p1a(icps_bmcastecho
, "\t%lu multicast echo requests ignored\n");
712 p1a(icps_bmcasttstamp
, "\t%lu multicast timestamp requests ignored\n");
713 for (first
= 1, i
= 0; i
< ICMP_MAXTYPE
+ 1; i
++)
714 if (icmpstat
.icps_inhist
[i
] != 0) {
716 printf("\tInput histogram:\n");
719 printf("\t\t%s: %lu\n", icmpnames
[i
],
720 icmpstat
.icps_inhist
[i
]);
722 p(icps_reflect
, "\t%lu message response%s generated\n");
725 mib
[3] = ICMPCTL_MASKREPL
;
727 if (sysctl(mib
, 4, &i
, &len
, (void *)0, 0) < 0)
729 printf("\tICMP address mask responses are %sabled\n",
734 * Dump IGMP statistics structure.
737 igmp_stats(u_long off
, char *name
, int af
)
739 struct igmpstat igmpstat
;
740 size_t len
= sizeof igmpstat
;
742 if (sysctlbyname("net.inet.igmp.stats", &igmpstat
, &len
, 0, 0) < 0) {
743 warn("sysctl: net.inet.igmp.stats");
747 printf("%s:\n", name
);
749 #define p(f, m) if (igmpstat.f || sflag <= 1) \
750 printf(m, igmpstat.f, plural(igmpstat.f))
751 #define py(f, m) if (igmpstat.f || sflag <= 1) \
752 printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
753 p(igps_rcv_total
, "\t%u message%s received\n");
754 p(igps_rcv_tooshort
, "\t%u message%s received with too few bytes\n");
755 p(igps_rcv_badsum
, "\t%u message%s received with bad checksum\n");
756 py(igps_rcv_queries
, "\t%u membership quer%s received\n");
757 py(igps_rcv_badqueries
, "\t%u membership quer%s received with invalid field(s)\n");
758 p(igps_rcv_reports
, "\t%u membership report%s received\n");
759 p(igps_rcv_badreports
, "\t%u membership report%s received with invalid field(s)\n");
760 p(igps_rcv_ourreports
, "\t%u membership report%s received for groups to which we belong\n");
761 p(igps_snd_reports
, "\t%u membership report%s sent\n");
767 * Pretty print an Internet address (net address + port).
770 inetprint(struct in_addr
*in
, int port
, char *proto
, int numeric_port
)
772 struct servent
*sp
= 0;
777 sprintf(line
, "%s.", inetname(in
));
779 sprintf(line
, "%.*s.", (Aflag
&& !numeric_port
) ? 12 : 16, inetname(in
));
780 cp
= index(line
, '\0');
781 if (!numeric_port
&& port
)
782 #ifdef _SERVICE_CACHE_
783 sp
= _serv_cache_getservbyport(port
, proto
);
785 sp
= getservbyport((int)port
, proto
);
788 sprintf(cp
, "%.15s ", sp
? sp
->s_name
: "*");
790 sprintf(cp
, "%d ", ntohs((u_short
)port
));
791 width
= (Aflag
&& !Wflag
) ? 18 : 22;
793 printf("%-*s ", width
, line
);
795 printf("%-*.*s ", width
, width
, line
);
799 * Construct an Internet address representation.
800 * If the nflag has been supplied, give
801 * numeric value, otherwise try for symbolic name.
804 inetname(struct in_addr
*inp
)
807 static char line
[MAXHOSTNAMELEN
];
812 if (!nflag
&& inp
->s_addr
!= INADDR_ANY
) {
813 int net
= inet_netof(*inp
);
814 int lna
= inet_lnaof(*inp
);
816 if (lna
== INADDR_ANY
) {
817 np
= getnetbyaddr(net
, AF_INET
);
822 hp
= gethostbyaddr((char *)inp
, sizeof (*inp
), AF_INET
);
825 //### trimdomain(cp, strlen(cp));
829 if (inp
->s_addr
== INADDR_ANY
)
832 strncpy(line
, cp
, sizeof(line
) - 1);
833 line
[sizeof(line
) - 1] = '\0';
835 inp
->s_addr
= ntohl(inp
->s_addr
);
836 #define C(x) ((u_int)((x) & 0xff))
837 sprintf(line
, "%u.%u.%u.%u", C(inp
->s_addr
>> 24),
838 C(inp
->s_addr
>> 16), C(inp
->s_addr
>> 8), C(inp
->s_addr
));