Debian Codegraph @

apps/snmpnetstat/inetx.c

raw
1 /*
2 * $OpenBSD: inet6.c,v 1.31 2004/11/17 01:47:20 itojun Exp $
3 */
4 /*
5 * BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp
6 */
7 /*
8 * Copyright (c) 1983, 1988, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <net-snmp/net-snmp-config.h>
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 #ifdef HAVE_WINSOCK_H
42 #include "winstub.h"
43 #endif
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
46 #endif
47 #ifdef HAVE_NETDB_H
48 #include <netdb.h>
49 #endif
50 #ifdef HAVE_SYS_TYPES_H
51 #include <sys/types.h>
52 #endif
53
54 #ifdef HAVE_NETINET_IN_H
55 #include <netinet/in.h>
56 #endif
57 #ifdef HAVE_ARPA_INET_H
58 #include <arpa/inet.h>
59 #endif
60
61 #include <net-snmp/net-snmp-includes.h>
62
63 #include "main.h"
64 #include "netstat.h"
65
66 struct stat_table {
67 unsigned int entry; /* entry number in table */
68 /*
69 * format string to printf(description, value)
70 * warning: the %d must be before the %s
71 */
72 char description[80];
73 };
74
75 void inetxprint(int, struct sockaddr_in6, int, const char *,
76 int);
77
78 /*
79 * Print a summary of TCP connections
80 * Listening processes are suppressed unless the
81 * -a (all) flag is specified.
82 */
83 const char *tcpxstates[] = {
84 "",
85 "CLOSED",
86 "LISTEN",
87 "SYNSENT",
88 "SYNRECEIVED",
89 "ESTABLISHED",
90 "FINWAIT1",
91 "FINWAIT2",
92 "CLOSEWAIT",
93 "LASTACK",
94 "CLOSING",
95 "TIMEWAIT"
96 };
97
98 #define TCP_NSTATES 11
99
100 typedef struct {
101 int stat;
102 int hcstat;
103 const char *str;
104 } systemstats_t;
105
106 systemstats_t systemstats[] = {
107 {3, 4, "datagrams input"},
108 {5, 6, "octets received"},
109 {7, 0, "hdr errors input"},
110 {8, 0, "no routes input"},
111 {9, 0, "address errors input"},
112 {10, 0, "unknown protocol input"},
113 {12, 13, "input datagrams forwarded"},
114 {11, 0, "truncated datagrams input"},
115 {14, 0, "input reassembly required"},
116 {15, 0, "input reassemled OK"},
117 {16, 0, "input reassembly failed"},
118 {17, 0, "input datagrams discarded"},
119 {18, 19, "input datagrams received"},
120 {20, 21, "output datagram requests"},
121 {22, 0, "output no route"},
122 {23, 24, "datagrams forwarded"},
123 {25, 0, "output datagrams discarded"},
124 {26, 0, "output datagrams fragmentation required"},
125 {27, 0, "output datagrams fragmented"},
126 {28, 0, "output fragmentation failed"},
127 {29, 0, "fragments created"},
128 {30, 31, "datagrams transmitted"},
129 {32, 33, "octets transmitted"},
130 {0}
131 };
132
133 systemstats_t icmpstats[] = {
134 {2, 0, "input messages"},
135 {3, 0, "input errors"},
136 {4, 0, "output messages"},
137 {5, 0, "output errors"},
138 {0}
139 };
140
141 typedef struct {
142 int code;
143 const char *name;
144 } codelist_t;
145
146 codelist_t icmpcodes[] = {
147 {0, "Echo reply"},
148 {3, "Destination unreachable"},
149 {4, "Source quench"},
150 {5, "Redirect"},
151 {6, "Alternate host address"},
152 {8, "Echo request"},
153 {9, "Router advertisement"},
154 {10, "Router selection"},
155 {11, "Time exceeded"},
156 {12, "Parameter problem"},
157 {13, "Timestamp request"},
158 {14, "Timestamp reply"},
159 {15, "Information request"},
160 {16, "Information reply"},
161 {17, "Address mask request"},
162 {18, "Address mask reply"},
163 {0}
164 };
165
166 codelist_t icmp6codes[] = {
167 {1, "Destination Unreachable"},
168 {2, "Packet Too Big"},
169 {3, "Time Exceeded"},
170 {4, "Parameter Problem"},
171 {100, "Private experimentation 100"},
172 {101, "Private experimentation 101"},
173 {127, "Reserved for expansion of ICMPv6 error messages"},
174 {128, "Echo Request"},
175 {129, "Echo Reply"},
176 {130, "Multicast Listener Query"},
177 {131, "Multicast Listener Report"},
178 {132, "Multicast Listener Done"},
179 {133, "Router Solicitation"},
180 {134, "Router Advertisement"},
181 {135, "Neighbor Solicitation"},
182 {136, "Neighbor Advertisement"},
183 {137, "Redirect Message"},
184 {138, "Router Renumbering"},
185 {139, "ICMP Node Information Query"},
186 {140, "ICMP Node Information Response"},
187 {141, "Inverse Neighbor Discovery Solicitation Message"},
188 {142, "Inverse Neighbor Discovery Advertisement Message"},
189 {143, "Version 2 Multicast Listener Report"},
190 {144, "Home Agent Address Discovery Request Message"},
191 {145, "Home Agent Address Discovery Reply Message"},
192 {146, "Mobile Prefix Solicitation"},
193 {147, "Mobile Prefix Advertisement"},
194 {148, "Certification Path Solicitation Message"},
195 {149, "Certification Path Advertisement Message"},
196 {151, "Multicast Router Advertisement"},
197 {152, "Multicast Router Solicitation"},
198 {153, "Multicast Router Termination"},
199 {154, "FMIPv6 Messages"},
200 {155, "RPL Control Message"},
201 {0}
202 };
203
204
205 void
206 tcpxprotopr(const char *name)
207 {
208 netsnmp_variable_list *var, *vp, *pvar;
209 oid tcpConnectionState_oid[] =
210 { 1, 3, 6, 1, 2, 1, 6, 19, 1, 7 };
211 size_t tcpConnectionState_len =
212 OID_LENGTH(tcpConnectionState_oid);
213 int state, i;
214 struct sockaddr_in6 localAddr, remoteAddr;
215 int localPort, remotePort, pid = 0;
216 int localType, remoteType, inx;
217 int first = 1;
218 static int done = 0;
219
220 if (done++)
221 return;
222
223 /*
224 * Walking the v6 tcpConnectionState column will provide all
225 * the necessary information.
226 */
227 var = NULL;
228 snmp_varlist_add_variable(&var, tcpConnectionState_oid,
229 tcpConnectionState_len, ASN_NULL, NULL, 0);
230 if (netsnmp_query_walk(var, ss) != SNMP_ERR_NOERROR) {
231 snmp_free_varbind(var);
232 return;
233 }
234 if ((var->type & 0xF0) == 0x80) { /* Exception */
235 snmp_free_varbind(var);
236 return;
237 }
238
239 for (vp = var; vp; vp = vp->next_variable) {
240 char lname[5];
241 state = *vp->val.integer;
242 inx = tcpConnectionState_len;
243 pvar = NULL;
244
245 vp->name[inx - 1] = 8;
246 snmp_varlist_add_variable(&pvar, vp->name, vp->name_length,
247 ASN_NULL, NULL, 0);
248 if (netsnmp_query_get(pvar, ss) != SNMP_ERR_NOERROR) {
249 snmp_free_var(pvar);
250 return;
251 }
252 if ((pvar->type & 0xF0) != 0x80) /* Exception */
253 pid = *pvar->val.integer;
254
255 /*
256 * Extract the local/remote information from the index values
257 */
258 localType = vp->name[inx++];
259 for (i = 0; i < vp->name[inx]; i++)
260 localAddr.sin6_addr.s6_addr[i] = vp->name[inx + i + 1];
261 inx += vp->name[inx] + 1;
262 localPort = vp->name[inx++];
263 remoteType = vp->name[inx++];
264 for (i = 0; i < vp->name[inx]; i++)
265 remoteAddr.sin6_addr.s6_addr[i] = vp->name[inx + i + 1];
266 inx += vp->name[inx] + 1;
267 remotePort = vp->name[inx++];
268
269 snmp_free_varbind(pvar);
270
271 if (af == AF_INET && localType == 2)
272 continue;
273 if (af == AF_INET6 && localType != 2)
274 continue;
275
276 if (first) {
277 printf("Active Internet (%s) Connections", "tcp");
278 putchar('\n');
279 printf("%-5.5s %-27.27s %-27.27s %11.11s %5.5s\n",
280 "Proto", "Local Address", "Remote Address", "State",
281 "PID");
282 first = 0;
283 }
284
285 strlcpy(lname, "tcp", sizeof lname);
286 if (localType == 2)
287 lname[3] = '6';
288 else
289 lname[3] = '4';
290 lname[4] = 0;
291 printf("%-5.5s", lname);
292 inetxprint(localType, localAddr, localPort, "tcp", 1);
293 inetxprint(remoteType, remoteAddr, remotePort, "tcp", 0);
294 if (state < 1 || state > TCP_NSTATES)
295 printf(" %11d %5d\n", state, pid);
296 else
297 printf(" %11s %5d\n", tcpxstates[state], pid);
298 }
299 snmp_free_varbind(var);
300
301 if (aflag)
302 listenxprotopr(name);
303 }
304
305 /*
306 * Print a summary of listening "connections"
307 */
308 void
309 listenxprotopr(const char *name)
310 {
311 netsnmp_variable_list *var, *vp;
312 oid tcpListenerProcess_oid[] =
313 { 1, 3, 6, 1, 2, 1, 6, 20, 1, 4 };
314 size_t tcpListenerProcess_len =
315 OID_LENGTH(tcpListenerProcess_oid);
316 struct sockaddr_in6 localAddr;
317 int localType, localPort, pid;
318 int i, inx;
319
320 /*
321 * Walking a single column of the udpTable will provide
322 * all the necessary information from the index values.
323 */
324 var = NULL;
325 snmp_varlist_add_variable(&var, tcpListenerProcess_oid,
326 tcpListenerProcess_len, ASN_NULL, NULL, 0);
327 if (netsnmp_query_walk(var, ss) != SNMP_ERR_NOERROR)
328 return;
329 if ((var->type & 0xF0) == 0x80) /* Exception */
330 return;
331
332 printf("Listening Internet (%s) Connections\n", "tcp");
333 printf("%-5.5s %-27.27s %5s\n", "Proto", "Local Address", "PID");
334 for (vp = var; vp; vp = vp->next_variable) {
335 char lname[5];
336 inx = tcpListenerProcess_len;
337 /*
338 * Extract the local port from the index values, but take
339 * the IP address from the varbind value, (which is why
340 * we walked udpLocalAddress rather than udpLocalPort)
341 */
342 localType = vp->name[inx++];
343 if (af == AF_INET && localType == 2)
344 continue;
345 if (af == AF_INET6 && localType != 2)
346 continue;
347
348 for (i = 0; i < vp->name[inx]; i++)
349 localAddr.sin6_addr.s6_addr[i] = vp->name[inx + i + 1];
350 inx += vp->name[inx] + 1;
351 localPort = vp->name[inx++];
352 pid = *vp->val.integer;
353 strlcpy(lname, "tcp", sizeof lname);
354 if (localType == 2)
355 lname[3] = '6';
356 else
357 lname[3] = '4';
358 lname[4] = 0;
359 printf("%-5.5s", lname);
360 inetxprint(localType, localAddr, localPort, "tcp", 1);
361 printf(" %5d\n", pid);
362 }
363 snmp_free_varbind(var);
364 }
365
366 /*
367 * Print a summary of UDPv6 "connections"
368 * XXX - what about "listening" services ??
369 */
370 void
371 udpxprotopr(const char *name)
372 {
373 netsnmp_variable_list *var, *vp;
374 oid udpEndpointProcess_oid[] =
375 { 1, 3, 6, 1, 2, 1, 7, 7, 1, 8 };
376 size_t udpEndpointProcess_len =
377 OID_LENGTH(udpEndpointProcess_oid);
378 struct sockaddr_in6 localAddr, remoteAddr;
379 int localType, remoteType, localPort, remotePort, pid;
380 int i, inx;
381 static int done = 0;
382
383 if (done++)
384 return;
385
386 /*
387 * Walking a single column of the udpTable will provide
388 * all the necessary information from the index values.
389 */
390 var = NULL;
391 snmp_varlist_add_variable(&var, udpEndpointProcess_oid,
392 udpEndpointProcess_len, ASN_NULL, NULL, 0);
393 if (netsnmp_query_walk(var, ss) != SNMP_ERR_NOERROR) {
394 snmp_free_varbind(var);
395 return;
396 }
397 if ((var->type & 0xF0) == 0x80) { /* Exception */
398 snmp_free_varbind(var);
399 return;
400 }
401
402 printf("Active Internet (%s) Connections\n", "udp");
403 printf("%-5.5s %-27.27s %-27.27s %5s\n", "Proto", "Local Address",
404 "Remote Address", "PID");
405 for (vp = var; vp; vp = vp->next_variable) {
406 char lname[5];
407 inx = udpEndpointProcess_len;
408 /*
409 * Extract the local port from the index values, but take
410 * the IP address from the varbind value, (which is why
411 * we walked udpLocalAddress rather than udpLocalPort)
412 */
413 localType = vp->name[inx++];
414 if (af == AF_INET && localType == 2)
415 continue;
416 if (af == AF_INET6 && localType != 2)
417 continue;
418 for (i = 0; i < vp->name[inx]; i++)
419 localAddr.sin6_addr.s6_addr[i] = vp->name[inx + i + 1];
420 inx += vp->name[inx] + 1;
421 localPort = vp->name[inx++];
422 remoteType = vp->name[inx++];
423 for (i = 0; i < vp->name[inx]; i++)
424 remoteAddr.sin6_addr.s6_addr[i] = vp->name[inx + i + 1];
425 inx += vp->name[inx] + 1;
426 remotePort = vp->name[inx++];
427 pid = *vp->val.integer;
428 strlcpy(lname, "udp", sizeof lname);
429 if (localType == 2)
430 lname[3] = '6';
431 else
432 lname[3] = '4';
433 lname[4] = 0;
434 printf("%-5.5s", lname);
435 inetxprint(localType, localAddr, localPort, "udp", 1);
436 inetxprint(remoteType, remoteAddr, remotePort, "udp", 1);
437 printf(" %5d\n", pid);
438 }
439 snmp_free_varbind(var);
440 }
441
442 static void
443 statsprint(const char *name, const systemstats_t * st, int proto,
444 const oid * tbl, size_t tbllen)
445 {
446 oid var[32];
447 size_t len;
448 netsnmp_variable_list *vb;
449
450 memcpy(var, tbl, tbllen * sizeof(oid));
451 var[tbllen + 1] = proto;
452 len = tbllen + 2;
453
454 printf("%s:\n", name);
455 while (st->stat) {
456 vb = NULL;
457 if (st->hcstat) {
458 var[tbllen] = st->hcstat;
459 snmp_varlist_add_variable(&vb, var, len, ASN_NULL, NULL, 0);
460 if (netsnmp_query_get(vb, ss) != SNMP_ERR_NOERROR) {
461 snmp_free_var(vb);
462 vb = NULL;
463 }
464 }
465 if (!vb) {
466 var[tbllen] = st->stat;
467 snmp_varlist_add_variable(&vb, var, len, ASN_NULL, NULL, 0);
468 if (netsnmp_query_get(vb, ss) != SNMP_ERR_NOERROR) {
469 snmp_free_var(vb);
470 vb = NULL;
471 }
472 }
473 if (vb) {
474 if (vb->type == ASN_COUNTER) {
475 if (*vb->val.integer > 0 || sflag == 1)
476 printf("%14lu %s\n", *vb->val.integer, st->str);
477 } else if (vb->type == ASN_COUNTER64) {
478 char a64buf[I64CHARSZ + 1];
479 printU64(a64buf, vb->val.counter64);
480 if (strcmp(a64buf, "0") != 0 || sflag == 1)
481 printf("%14s %s\n", a64buf, st->str);
482 } else
483 printf("%14s %s\n", "-", st->str);
484 snmp_free_varbind(vb);
485 } else {
486 printf("%14s %s\n", "-", st->str);
487 }
488 st++;
489 }
490 }
491
492 static void
493 prhisto(const char *name, const oid * var, size_t len, int ver,
494 codelist_t * cs)
495 {
496 netsnmp_variable_list *vb = NULL, *vp;
497 codelist_t *cp;
498 int code;
499 char nocode[32];
500
501 snmp_varlist_add_variable(&vb, var, len, ASN_NULL, NULL, 0);
502 if (netsnmp_query_walk(vb, ss) != SNMP_ERR_NOERROR) {
503 snmp_free_var(vb);
504 return;
505 }
506 printf(" %s histogram:\n", name);
507 printf(" %10s %10s %s\n", "input", "output", "type");
508 for (code = 0; code < 256; code++) {
509 unsigned long inp = 0, out = 0;
510 int found = 0;
511 vp = vb;
512 while (vp && found != 2) {
513 if (vp->name[11] == code && vp->name[10] == ver) {
514 if (vp->name[9] == 3)
515 inp = *vp->val.integer;
516 else
517 out = *vp->val.integer;
518 found++;
519 }
520 vp = vp->next_variable;
521 }
522 if (found) {
523 cp = cs;
524 while (cp->name && cp->code != code)
525 cp++;
526 if (inp || out || sflag == 1) {
527 if (!cp->code)
528 snprintf(nocode, sizeof nocode, "type %d", code);
529 printf(" %10lu %10lu %s\n", inp, out,
530 cp->name ? cp->name : nocode);
531 }
532 }
533 }
534 snmp_free_varbind(vb);
535 }
536
537 void
538 ipx_stats(const char *name)
539 {
540 oid ipsysstat_oid[] = { 1, 3, 6, 1, 2, 1, 4, 31, 1, 1 };
541 size_t ipsysstat_len =
542 sizeof(ipsysstat_oid) / sizeof(ipsysstat_oid[0]);
543 static int first = 1;
544
545 if (!first)
546 return;
547 first = 0;
548
549 if (!name || strcmp(name, "ip") == 0)
550 statsprint("ip", systemstats, 1, ipsysstat_oid, ipsysstat_len);
551 if (!name || strcmp(name, "ip6") == 0)
552 statsprint("ip6", systemstats, 2, ipsysstat_oid, ipsysstat_len);
553 }
554
555 void
556 icmpx_stats(const char *name)
557 {
558 oid icmpstat_oid[] = { 1, 3, 6, 1, 2, 1, 5, 29, 1 };
559 size_t icmpstat_len =
560 sizeof(icmpstat_oid) / sizeof(icmpstat_oid[0]);
561 oid icmpmsg_oid[] = { 1, 3, 6, 1, 2, 1, 5, 30, 1 };
562 size_t icmpmsg_len =
563 sizeof(icmpmsg_oid) / sizeof(icmpmsg_oid[0]);
564 static int first = 1;
565
566 if (!first)
567 return;
568 first = 0;
569
570 if (!name || strcmp(name, "icmp") == 0) {
571 statsprint("icmp", icmpstats, 1, icmpstat_oid, icmpstat_len);
572 prhisto("icmp", icmpmsg_oid, icmpmsg_len, 1, icmpcodes);
573 }
574 if (!name || strcmp(name, "icmp6") == 0) {
575 statsprint("icmp6", icmpstats, 2, icmpstat_oid, icmpstat_len);
576 prhisto("icmp6", icmpmsg_oid, icmpmsg_len, 2, icmp6codes);
577 }
578 }
579
580
581 static void
582 unknownprint(void)
583 {
584 char line[80], *cp;
585 int width = 27;
586
587 if (vflag)
588 snprintf(line, sizeof line, "%s.", "*");
589 else
590 snprintf(line, sizeof line, "%.*s.", width - 9, "*");
591 cp = strchr(line, '\0');
592 snprintf(cp, line + sizeof line - cp, vflag ? "%s" : "%.8s", "*");
593 if (vflag && width < strlen(line))
594 width = strlen(line);
595 printf(" %-*.*s", width, width, line);
596 }
597
598 /*
599 * Pretty print an Internet address (net address + port).
600 * If the nflag was specified, use numbers instead of names.
601 */
602
603 void
604 inetxprint(int proto, struct sockaddr_in6 in6, int port, const char *name,
605 int local)
606 {
607
608 if (proto == 2)
609 inet6print(&in6.sin6_addr, port, name, local);
610 else if (proto == 1)
611 inetprint((struct in_addr *) &in6.sin6_addr.s6_addr, port, name,
612 local);
613 else if (proto == 0)
614 unknownprint();
615 else
616 abort();
617 }