inet.c revision 1.14 1 1.14 thorpej /* $NetBSD: inet.c,v 1.14 1995/10/03 21:42:37 thorpej Exp $ */
2 1.14 thorpej
3 1.1 cgd /*
4 1.9 mycroft * Copyright (c) 1983, 1988, 1993
5 1.9 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.14 thorpej #if 0
38 1.14 thorpej static char sccsid[] = "from: @(#)inet.c 8.4 (Berkeley) 4/20/94";
39 1.14 thorpej #else
40 1.14 thorpej static char *rcsid = "$NetBSD: inet.c,v 1.14 1995/10/03 21:42:37 thorpej Exp $";
41 1.14 thorpej #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/param.h>
45 1.10 cgd #include <sys/queue.h>
46 1.1 cgd #include <sys/socket.h>
47 1.1 cgd #include <sys/socketvar.h>
48 1.1 cgd #include <sys/mbuf.h>
49 1.1 cgd #include <sys/protosw.h>
50 1.1 cgd
51 1.1 cgd #include <net/route.h>
52 1.1 cgd #include <netinet/in.h>
53 1.1 cgd #include <netinet/in_systm.h>
54 1.1 cgd #include <netinet/ip.h>
55 1.1 cgd #include <netinet/in_pcb.h>
56 1.1 cgd #include <netinet/ip_icmp.h>
57 1.1 cgd #include <netinet/icmp_var.h>
58 1.6 brezak #include <netinet/igmp_var.h>
59 1.1 cgd #include <netinet/ip_var.h>
60 1.1 cgd #include <netinet/tcp.h>
61 1.1 cgd #include <netinet/tcpip.h>
62 1.1 cgd #include <netinet/tcp_seq.h>
63 1.1 cgd #define TCPSTATES
64 1.1 cgd #include <netinet/tcp_fsm.h>
65 1.1 cgd #include <netinet/tcp_timer.h>
66 1.1 cgd #include <netinet/tcp_var.h>
67 1.1 cgd #include <netinet/tcp_debug.h>
68 1.1 cgd #include <netinet/udp.h>
69 1.1 cgd #include <netinet/udp_var.h>
70 1.1 cgd
71 1.9 mycroft #include <arpa/inet.h>
72 1.1 cgd #include <netdb.h>
73 1.1 cgd #include <stdio.h>
74 1.1 cgd #include <string.h>
75 1.9 mycroft #include <unistd.h>
76 1.9 mycroft #include "netstat.h"
77 1.1 cgd
78 1.1 cgd struct inpcb inpcb;
79 1.1 cgd struct tcpcb tcpcb;
80 1.1 cgd struct socket sockb;
81 1.1 cgd
82 1.9 mycroft char *inetname __P((struct in_addr *));
83 1.9 mycroft void inetprint __P((struct in_addr *, int, char *));
84 1.1 cgd
85 1.1 cgd /*
86 1.1 cgd * Print a summary of connections related to an Internet
87 1.1 cgd * protocol. For TCP, also give state of connection.
88 1.1 cgd * Listening processes (aflag) are suppressed unless the
89 1.1 cgd * -a (all) flag is specified.
90 1.1 cgd */
91 1.9 mycroft void
92 1.1 cgd protopr(off, name)
93 1.8 cgd u_long off;
94 1.1 cgd char *name;
95 1.1 cgd {
96 1.12 mycroft struct inpcbtable table;
97 1.13 cgd register struct inpcb *head, *next, *prev;
98 1.13 cgd struct inpcb inpcb;
99 1.1 cgd int istcp;
100 1.1 cgd static int first = 1;
101 1.1 cgd
102 1.1 cgd if (off == 0)
103 1.1 cgd return;
104 1.1 cgd istcp = strcmp(name, "tcp") == 0;
105 1.12 mycroft kread(off, (char *)&table, sizeof table);
106 1.13 cgd prev = head =
107 1.13 cgd (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue.cqh_first;
108 1.13 cgd next = table.inpt_queue.cqh_first;
109 1.13 cgd
110 1.13 cgd while (next != head) {
111 1.12 mycroft kread((u_long)next, (char *)&inpcb, sizeof inpcb);
112 1.13 cgd if (inpcb.inp_queue.cqe_prev != prev) {
113 1.1 cgd printf("???\n");
114 1.1 cgd break;
115 1.1 cgd }
116 1.13 cgd prev = next;
117 1.13 cgd next = inpcb.inp_queue.cqe_next;
118 1.13 cgd
119 1.1 cgd if (!aflag &&
120 1.12 mycroft inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
121 1.1 cgd continue;
122 1.9 mycroft kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb));
123 1.1 cgd if (istcp) {
124 1.9 mycroft kread((u_long)inpcb.inp_ppcb,
125 1.9 mycroft (char *)&tcpcb, sizeof (tcpcb));
126 1.1 cgd }
127 1.1 cgd if (first) {
128 1.1 cgd printf("Active Internet connections");
129 1.1 cgd if (aflag)
130 1.1 cgd printf(" (including servers)");
131 1.1 cgd putchar('\n');
132 1.1 cgd if (Aflag)
133 1.1 cgd printf("%-8.8s ", "PCB");
134 1.1 cgd printf(Aflag ?
135 1.1 cgd "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" :
136 1.1 cgd "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n",
137 1.1 cgd "Proto", "Recv-Q", "Send-Q",
138 1.1 cgd "Local Address", "Foreign Address", "(state)");
139 1.1 cgd first = 0;
140 1.1 cgd }
141 1.1 cgd if (Aflag)
142 1.1 cgd if (istcp)
143 1.1 cgd printf("%8x ", inpcb.inp_ppcb);
144 1.1 cgd else
145 1.13 cgd printf("%8x ", prev);
146 1.1 cgd printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
147 1.1 cgd sockb.so_snd.sb_cc);
148 1.9 mycroft inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport, name);
149 1.9 mycroft inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport, name);
150 1.1 cgd if (istcp) {
151 1.1 cgd if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
152 1.1 cgd printf(" %d", tcpcb.t_state);
153 1.1 cgd else
154 1.1 cgd printf(" %s", tcpstates[tcpcb.t_state]);
155 1.1 cgd }
156 1.1 cgd putchar('\n');
157 1.1 cgd }
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd /*
161 1.1 cgd * Dump TCP statistics structure.
162 1.1 cgd */
163 1.9 mycroft void
164 1.1 cgd tcp_stats(off, name)
165 1.8 cgd u_long off;
166 1.1 cgd char *name;
167 1.1 cgd {
168 1.1 cgd struct tcpstat tcpstat;
169 1.1 cgd
170 1.1 cgd if (off == 0)
171 1.1 cgd return;
172 1.1 cgd printf ("%s:\n", name);
173 1.9 mycroft kread(off, (char *)&tcpstat, sizeof (tcpstat));
174 1.9 mycroft
175 1.9 mycroft #define p(f, m) if (tcpstat.f || sflag <= 1) \
176 1.9 mycroft printf(m, tcpstat.f, plural(tcpstat.f))
177 1.9 mycroft #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
178 1.9 mycroft printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
179 1.9 mycroft #define p3(f, m) if (tcpstat.f || sflag <= 1) \
180 1.9 mycroft printf(m, tcpstat.f, plurales(tcpstat.f))
181 1.1 cgd
182 1.1 cgd p(tcps_sndtotal, "\t%d packet%s sent\n");
183 1.1 cgd p2(tcps_sndpack,tcps_sndbyte,
184 1.1 cgd "\t\t%d data packet%s (%d byte%s)\n");
185 1.1 cgd p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
186 1.1 cgd "\t\t%d data packet%s (%d byte%s) retransmitted\n");
187 1.1 cgd p2(tcps_sndacks, tcps_delack,
188 1.1 cgd "\t\t%d ack-only packet%s (%d delayed)\n");
189 1.1 cgd p(tcps_sndurg, "\t\t%d URG only packet%s\n");
190 1.1 cgd p(tcps_sndprobe, "\t\t%d window probe packet%s\n");
191 1.1 cgd p(tcps_sndwinup, "\t\t%d window update packet%s\n");
192 1.1 cgd p(tcps_sndctrl, "\t\t%d control packet%s\n");
193 1.1 cgd p(tcps_rcvtotal, "\t%d packet%s received\n");
194 1.1 cgd p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%d ack%s (for %d byte%s)\n");
195 1.1 cgd p(tcps_rcvdupack, "\t\t%d duplicate ack%s\n");
196 1.1 cgd p(tcps_rcvacktoomuch, "\t\t%d ack%s for unsent data\n");
197 1.1 cgd p2(tcps_rcvpack, tcps_rcvbyte,
198 1.1 cgd "\t\t%d packet%s (%d byte%s) received in-sequence\n");
199 1.1 cgd p2(tcps_rcvduppack, tcps_rcvdupbyte,
200 1.1 cgd "\t\t%d completely duplicate packet%s (%d byte%s)\n");
201 1.9 mycroft p(tcps_pawsdrop, "\t\t%d old duplicate packet%s\n");
202 1.1 cgd p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
203 1.1 cgd "\t\t%d packet%s with some dup. data (%d byte%s duped)\n");
204 1.1 cgd p2(tcps_rcvoopack, tcps_rcvoobyte,
205 1.1 cgd "\t\t%d out-of-order packet%s (%d byte%s)\n");
206 1.1 cgd p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
207 1.1 cgd "\t\t%d packet%s (%d byte%s) of data after window\n");
208 1.1 cgd p(tcps_rcvwinprobe, "\t\t%d window probe%s\n");
209 1.1 cgd p(tcps_rcvwinupd, "\t\t%d window update packet%s\n");
210 1.1 cgd p(tcps_rcvafterclose, "\t\t%d packet%s received after close\n");
211 1.1 cgd p(tcps_rcvbadsum, "\t\t%d discarded for bad checksum%s\n");
212 1.1 cgd p(tcps_rcvbadoff, "\t\t%d discarded for bad header offset field%s\n");
213 1.1 cgd p(tcps_rcvshort, "\t\t%d discarded because packet too short\n");
214 1.1 cgd p(tcps_connattempt, "\t%d connection request%s\n");
215 1.1 cgd p(tcps_accepts, "\t%d connection accept%s\n");
216 1.1 cgd p(tcps_connects, "\t%d connection%s established (including accepts)\n");
217 1.1 cgd p2(tcps_closed, tcps_drops,
218 1.1 cgd "\t%d connection%s closed (including %d drop%s)\n");
219 1.1 cgd p(tcps_conndrops, "\t%d embryonic connection%s dropped\n");
220 1.1 cgd p2(tcps_rttupdated, tcps_segstimed,
221 1.1 cgd "\t%d segment%s updated rtt (of %d attempt%s)\n");
222 1.1 cgd p(tcps_rexmttimeo, "\t%d retransmit timeout%s\n");
223 1.1 cgd p(tcps_timeoutdrop, "\t\t%d connection%s dropped by rexmit timeout\n");
224 1.1 cgd p(tcps_persisttimeo, "\t%d persist timeout%s\n");
225 1.1 cgd p(tcps_keeptimeo, "\t%d keepalive timeout%s\n");
226 1.1 cgd p(tcps_keepprobe, "\t\t%d keepalive probe%s sent\n");
227 1.1 cgd p(tcps_keepdrops, "\t\t%d connection%s dropped by keepalive\n");
228 1.9 mycroft p(tcps_predack, "\t%d correct ACK header prediction%s\n");
229 1.9 mycroft p(tcps_preddat, "\t%d correct data packet header prediction%s\n");
230 1.9 mycroft p3(tcps_pcbcachemiss, "\t%d PCB cache miss%s\n");
231 1.1 cgd #undef p
232 1.1 cgd #undef p2
233 1.9 mycroft #undef p3
234 1.1 cgd }
235 1.1 cgd
236 1.1 cgd /*
237 1.1 cgd * Dump UDP statistics structure.
238 1.1 cgd */
239 1.9 mycroft void
240 1.1 cgd udp_stats(off, name)
241 1.8 cgd u_long off;
242 1.1 cgd char *name;
243 1.1 cgd {
244 1.1 cgd struct udpstat udpstat;
245 1.9 mycroft u_long delivered;
246 1.1 cgd
247 1.1 cgd if (off == 0)
248 1.1 cgd return;
249 1.9 mycroft kread(off, (char *)&udpstat, sizeof (udpstat));
250 1.9 mycroft printf("%s:\n", name);
251 1.9 mycroft #define p(f, m) if (udpstat.f || sflag <= 1) \
252 1.9 mycroft printf(m, udpstat.f, plural(udpstat.f))
253 1.9 mycroft p(udps_ipackets, "\t%u datagram%s received\n");
254 1.9 mycroft p(udps_hdrops, "\t%u with incomplete header\n");
255 1.9 mycroft p(udps_badlen, "\t%u with bad data length field\n");
256 1.9 mycroft p(udps_badsum, "\t%u with bad checksum\n");
257 1.9 mycroft p(udps_noport, "\t%u dropped due to no socket\n");
258 1.9 mycroft p(udps_noportbcast, "\t%u broadcast/multicast datagram%s dropped due to no socket\n");
259 1.9 mycroft p(udps_fullsock, "\t%u dropped due to full socket buffers\n");
260 1.9 mycroft delivered = udpstat.udps_ipackets -
261 1.9 mycroft udpstat.udps_hdrops -
262 1.9 mycroft udpstat.udps_badlen -
263 1.9 mycroft udpstat.udps_badsum -
264 1.9 mycroft udpstat.udps_noport -
265 1.9 mycroft udpstat.udps_noportbcast -
266 1.9 mycroft udpstat.udps_fullsock;
267 1.9 mycroft if (delivered || sflag <= 1)
268 1.9 mycroft printf("\t%u delivered\n", delivered);
269 1.9 mycroft p(udps_opackets, "\t%u datagram%s output\n");
270 1.9 mycroft #undef p
271 1.1 cgd }
272 1.1 cgd
273 1.1 cgd /*
274 1.1 cgd * Dump IP statistics structure.
275 1.1 cgd */
276 1.9 mycroft void
277 1.1 cgd ip_stats(off, name)
278 1.8 cgd u_long off;
279 1.1 cgd char *name;
280 1.1 cgd {
281 1.1 cgd struct ipstat ipstat;
282 1.1 cgd
283 1.1 cgd if (off == 0)
284 1.1 cgd return;
285 1.9 mycroft kread(off, (char *)&ipstat, sizeof (ipstat));
286 1.9 mycroft printf("%s:\n", name);
287 1.9 mycroft
288 1.9 mycroft #define p(f, m) if (ipstat.f || sflag <= 1) \
289 1.9 mycroft printf(m, ipstat.f, plural(ipstat.f))
290 1.9 mycroft
291 1.9 mycroft p(ips_total, "\t%u total packet%s received\n");
292 1.9 mycroft p(ips_badsum, "\t%u bad header checksum%s\n");
293 1.9 mycroft p(ips_toosmall, "\t%u with size smaller than minimum\n");
294 1.9 mycroft p(ips_tooshort, "\t%u with data size < data length\n");
295 1.9 mycroft p(ips_badhlen, "\t%u with header length < data size\n");
296 1.9 mycroft p(ips_badlen, "\t%u with data length < header length\n");
297 1.9 mycroft p(ips_badoptions, "\t%u with bad options\n");
298 1.9 mycroft p(ips_badvers, "\t%u with incorrect version number\n");
299 1.9 mycroft p(ips_fragments, "\t%u fragment%s received\n");
300 1.9 mycroft p(ips_fragdropped, "\t%u fragment%s dropped (dup or out of space)\n");
301 1.11 cgd p(ips_badfrags, "\t%u malformed fragment%s dropped\n");
302 1.9 mycroft p(ips_fragtimeout, "\t%u fragment%s dropped after timeout\n");
303 1.9 mycroft p(ips_reassembled, "\t%u packet%s reassembled ok\n");
304 1.9 mycroft p(ips_delivered, "\t%u packet%s for this host\n");
305 1.9 mycroft p(ips_noproto, "\t%u packet%s for unknown/unsupported protocol\n");
306 1.9 mycroft p(ips_forward, "\t%u packet%s forwarded\n");
307 1.9 mycroft p(ips_cantforward, "\t%u packet%s not forwardable\n");
308 1.9 mycroft p(ips_redirectsent, "\t%u redirect%s sent\n");
309 1.9 mycroft p(ips_localout, "\t%u packet%s sent from this host\n");
310 1.9 mycroft p(ips_rawout, "\t%u packet%s sent with fabricated ip header\n");
311 1.9 mycroft p(ips_odropped, "\t%u output packet%s dropped due to no bufs, etc.\n");
312 1.9 mycroft p(ips_noroute, "\t%u output packet%s discarded due to no route\n");
313 1.9 mycroft p(ips_fragmented, "\t%u output datagram%s fragmented\n");
314 1.9 mycroft p(ips_ofragments, "\t%u fragment%s created\n");
315 1.9 mycroft p(ips_cantfrag, "\t%u datagram%s that can't be fragmented\n");
316 1.9 mycroft #undef p
317 1.1 cgd }
318 1.1 cgd
319 1.1 cgd static char *icmpnames[] = {
320 1.1 cgd "echo reply",
321 1.1 cgd "#1",
322 1.1 cgd "#2",
323 1.1 cgd "destination unreachable",
324 1.1 cgd "source quench",
325 1.1 cgd "routing redirect",
326 1.1 cgd "#6",
327 1.1 cgd "#7",
328 1.1 cgd "echo",
329 1.1 cgd "#9",
330 1.1 cgd "#10",
331 1.1 cgd "time exceeded",
332 1.1 cgd "parameter problem",
333 1.1 cgd "time stamp",
334 1.1 cgd "time stamp reply",
335 1.1 cgd "information request",
336 1.1 cgd "information request reply",
337 1.1 cgd "address mask request",
338 1.1 cgd "address mask reply",
339 1.1 cgd };
340 1.1 cgd
341 1.1 cgd /*
342 1.1 cgd * Dump ICMP statistics.
343 1.1 cgd */
344 1.9 mycroft void
345 1.1 cgd icmp_stats(off, name)
346 1.8 cgd u_long off;
347 1.1 cgd char *name;
348 1.1 cgd {
349 1.1 cgd struct icmpstat icmpstat;
350 1.1 cgd register int i, first;
351 1.1 cgd
352 1.1 cgd if (off == 0)
353 1.1 cgd return;
354 1.9 mycroft kread(off, (char *)&icmpstat, sizeof (icmpstat));
355 1.9 mycroft printf("%s:\n", name);
356 1.9 mycroft
357 1.9 mycroft #define p(f, m) if (icmpstat.f || sflag <= 1) \
358 1.9 mycroft printf(m, icmpstat.f, plural(icmpstat.f))
359 1.9 mycroft
360 1.9 mycroft p(icps_error, "\t%u call%s to icmp_error\n");
361 1.9 mycroft p(icps_oldicmp,
362 1.9 mycroft "\t%u error%s not generated 'cuz old message was icmp\n");
363 1.1 cgd for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
364 1.1 cgd if (icmpstat.icps_outhist[i] != 0) {
365 1.1 cgd if (first) {
366 1.1 cgd printf("\tOutput histogram:\n");
367 1.1 cgd first = 0;
368 1.1 cgd }
369 1.1 cgd printf("\t\t%s: %u\n", icmpnames[i],
370 1.1 cgd icmpstat.icps_outhist[i]);
371 1.1 cgd }
372 1.9 mycroft p(icps_badcode, "\t%u message%s with bad code fields\n");
373 1.9 mycroft p(icps_tooshort, "\t%u message%s < minimum length\n");
374 1.9 mycroft p(icps_checksum, "\t%u bad checksum%s\n");
375 1.9 mycroft p(icps_badlen, "\t%u message%s with bad length\n");
376 1.1 cgd for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
377 1.1 cgd if (icmpstat.icps_inhist[i] != 0) {
378 1.1 cgd if (first) {
379 1.1 cgd printf("\tInput histogram:\n");
380 1.1 cgd first = 0;
381 1.1 cgd }
382 1.1 cgd printf("\t\t%s: %u\n", icmpnames[i],
383 1.1 cgd icmpstat.icps_inhist[i]);
384 1.1 cgd }
385 1.9 mycroft p(icps_reflect, "\t%u message response%s generated\n");
386 1.9 mycroft #undef p
387 1.6 brezak }
388 1.6 brezak
389 1.6 brezak /*
390 1.9 mycroft * Dump IGMP statistics structure.
391 1.6 brezak */
392 1.6 brezak void
393 1.6 brezak igmp_stats(off, name)
394 1.8 cgd u_long off;
395 1.6 brezak char *name;
396 1.6 brezak {
397 1.6 brezak struct igmpstat igmpstat;
398 1.6 brezak
399 1.6 brezak if (off == 0)
400 1.6 brezak return;
401 1.9 mycroft kread(off, (char *)&igmpstat, sizeof (igmpstat));
402 1.9 mycroft printf("%s:\n", name);
403 1.9 mycroft
404 1.9 mycroft #define p(f, m) if (igmpstat.f || sflag <= 1) \
405 1.9 mycroft printf(m, igmpstat.f, plural(igmpstat.f))
406 1.9 mycroft #define py(f, m) if (igmpstat.f || sflag <= 1) \
407 1.9 mycroft printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
408 1.9 mycroft p(igps_rcv_total, "\t%u message%s received\n");
409 1.9 mycroft p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
410 1.9 mycroft p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
411 1.9 mycroft py(igps_rcv_queries, "\t%u membership quer%s received\n");
412 1.9 mycroft py(igps_rcv_badqueries, "\t%u membership quer%s received with invalid field(s)\n");
413 1.9 mycroft p(igps_rcv_reports, "\t%u membership report%s received\n");
414 1.9 mycroft p(igps_rcv_badreports, "\t%u membership report%s received with invalid field(s)\n");
415 1.9 mycroft p(igps_rcv_ourreports, "\t%u membership report%s received for groups to which we belong\n");
416 1.9 mycroft p(igps_snd_reports, "\t%u membership report%s sent\n");
417 1.9 mycroft #undef p
418 1.9 mycroft #undef py
419 1.1 cgd }
420 1.1 cgd
421 1.1 cgd /*
422 1.1 cgd * Pretty print an Internet address (net address + port).
423 1.1 cgd * If the nflag was specified, use numbers instead of names.
424 1.1 cgd */
425 1.9 mycroft void
426 1.1 cgd inetprint(in, port, proto)
427 1.1 cgd register struct in_addr *in;
428 1.9 mycroft int port;
429 1.1 cgd char *proto;
430 1.1 cgd {
431 1.1 cgd struct servent *sp = 0;
432 1.9 mycroft char line[80], *cp;
433 1.1 cgd int width;
434 1.1 cgd
435 1.9 mycroft sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(in));
436 1.1 cgd cp = index(line, '\0');
437 1.1 cgd if (!nflag && port)
438 1.1 cgd sp = getservbyport((int)port, proto);
439 1.1 cgd if (sp || port == 0)
440 1.1 cgd sprintf(cp, "%.8s", sp ? sp->s_name : "*");
441 1.1 cgd else
442 1.1 cgd sprintf(cp, "%d", ntohs((u_short)port));
443 1.1 cgd width = Aflag ? 18 : 22;
444 1.1 cgd printf(" %-*.*s", width, width, line);
445 1.1 cgd }
446 1.1 cgd
447 1.1 cgd /*
448 1.1 cgd * Construct an Internet address representation.
449 1.9 mycroft * If the nflag has been supplied, give
450 1.1 cgd * numeric value, otherwise try for symbolic name.
451 1.1 cgd */
452 1.1 cgd char *
453 1.9 mycroft inetname(inp)
454 1.9 mycroft struct in_addr *inp;
455 1.1 cgd {
456 1.1 cgd register char *cp;
457 1.1 cgd static char line[50];
458 1.1 cgd struct hostent *hp;
459 1.1 cgd struct netent *np;
460 1.1 cgd static char domain[MAXHOSTNAMELEN + 1];
461 1.1 cgd static int first = 1;
462 1.1 cgd
463 1.1 cgd if (first && !nflag) {
464 1.1 cgd first = 0;
465 1.1 cgd if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
466 1.1 cgd (cp = index(domain, '.')))
467 1.1 cgd (void) strcpy(domain, cp + 1);
468 1.1 cgd else
469 1.1 cgd domain[0] = 0;
470 1.1 cgd }
471 1.1 cgd cp = 0;
472 1.9 mycroft if (!nflag && inp->s_addr != INADDR_ANY) {
473 1.9 mycroft int net = inet_netof(*inp);
474 1.9 mycroft int lna = inet_lnaof(*inp);
475 1.1 cgd
476 1.1 cgd if (lna == INADDR_ANY) {
477 1.1 cgd np = getnetbyaddr(net, AF_INET);
478 1.1 cgd if (np)
479 1.1 cgd cp = np->n_name;
480 1.1 cgd }
481 1.1 cgd if (cp == 0) {
482 1.9 mycroft hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
483 1.1 cgd if (hp) {
484 1.1 cgd if ((cp = index(hp->h_name, '.')) &&
485 1.1 cgd !strcmp(cp + 1, domain))
486 1.1 cgd *cp = 0;
487 1.1 cgd cp = hp->h_name;
488 1.1 cgd }
489 1.1 cgd }
490 1.1 cgd }
491 1.9 mycroft if (inp->s_addr == INADDR_ANY)
492 1.1 cgd strcpy(line, "*");
493 1.1 cgd else if (cp)
494 1.1 cgd strcpy(line, cp);
495 1.1 cgd else {
496 1.9 mycroft inp->s_addr = ntohl(inp->s_addr);
497 1.1 cgd #define C(x) ((x) & 0xff)
498 1.9 mycroft sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
499 1.9 mycroft C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
500 1.1 cgd }
501 1.1 cgd return (line);
502 1.1 cgd }
503