1 /**
2 * collectd - src/tcpconns.c
3 * Copyright (C) 2007,2008 Florian octo Forster
4 * Copyright (C) 2008 Michael Stapelberg
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Author:
20 * Florian octo Forster <octo at verplant.org>
21 * Michael Stapelberg <michael+git at stapelberg.de>
22 **/
24 /**
25 * Code within `HAVE_LIBKVM_NLIST' blocks is provided under the following
26 * license:
27 *
28 * $collectd: parts of tcpconns.c, 2008/08/08 03:48:30 Michael Stapelberg $
29 * $OpenBSD: inet.c,v 1.100 2007/06/19 05:28:30 ray Exp $
30 * $NetBSD: inet.c,v 1.14 1995/10/03 21:42:37 thorpej Exp $
31 *
32 * Copyright (c) 1983, 1988, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
60 #include "collectd.h"
61 #include "common.h"
62 #include "plugin.h"
64 #if defined(__OpenBSD__) || defined(__NetBSD__)
65 #undef HAVE_SYSCTLBYNAME /* force HAVE_LIBKVM_NLIST path */
66 #endif
68 #if !KERNEL_LINUX && !HAVE_SYSCTLBYNAME && !HAVE_LIBKVM_NLIST && !KERNEL_AIX
69 # error "No applicable input method."
70 #endif
72 #if KERNEL_LINUX
73 /* #endif KERNEL_LINUX */
75 #elif HAVE_SYSCTLBYNAME
76 # include <sys/socketvar.h>
77 # include <sys/sysctl.h>
79 /* Some includes needed for compiling on FreeBSD */
80 #include <sys/time.h>
81 #if HAVE_SYS_TYPES_H
82 # include <sys/types.h>
83 #endif
84 #if HAVE_SYS_SOCKET_H
85 # include <sys/socket.h>
86 #endif
87 #if HAVE_NET_IF_H
88 # include <net/if.h>
89 #endif
91 # include <net/route.h>
92 # include <netinet/in.h>
93 # include <netinet/in_systm.h>
94 # include <netinet/ip.h>
95 # include <netinet/ip6.h>
96 # include <netinet/in_pcb.h>
97 # include <netinet/ip_var.h>
98 # include <netinet/tcp.h>
99 # include <netinet/tcpip.h>
100 # include <netinet/tcp_seq.h>
101 # include <netinet/tcp_var.h>
102 /* #endif HAVE_SYSCTLBYNAME */
104 /* This is for OpenBSD and NetBSD. */
105 #elif HAVE_LIBKVM_NLIST
106 # include <sys/queue.h>
107 # include <sys/socket.h>
108 # include <net/route.h>
109 # include <netinet/in.h>
110 # include <netinet/in_systm.h>
111 # include <netinet/ip.h>
112 # include <netinet/ip_var.h>
113 # include <netinet/in_pcb.h>
114 # include <netinet/tcp.h>
115 # include <netinet/tcp_timer.h>
116 # include <netinet/tcp_var.h>
117 # include <netdb.h>
118 # include <arpa/inet.h>
119 # include <nlist.h>
120 # include <kvm.h>
121 /* #endif HAVE_LIBKVM_NLIST */
123 #elif KERNEL_AIX
124 # include <arpa/inet.h>
125 # include <sys/socketvar.h>
126 #endif /* KERNEL_AIX */
128 #if KERNEL_LINUX
129 static const char *tcp_state[] =
130 {
131 "", /* 0 */
132 "ESTABLISHED",
133 "SYN_SENT",
134 "SYN_RECV",
135 "FIN_WAIT1",
136 "FIN_WAIT2",
137 "TIME_WAIT",
138 "CLOSED",
139 "CLOSE_WAIT",
140 "LAST_ACK",
141 "LISTEN", /* 10 */
142 "CLOSING"
143 };
145 # define TCP_STATE_LISTEN 10
146 # define TCP_STATE_MIN 1
147 # define TCP_STATE_MAX 11
148 /* #endif KERNEL_LINUX */
150 #elif HAVE_SYSCTLBYNAME
151 static const char *tcp_state[] =
152 {
153 "CLOSED",
154 "LISTEN",
155 "SYN_SENT",
156 "SYN_RECV",
157 "ESTABLISHED",
158 "CLOSE_WAIT",
159 "FIN_WAIT1",
160 "CLOSING",
161 "LAST_ACK",
162 "FIN_WAIT2",
163 "TIME_WAIT"
164 };
166 # define TCP_STATE_LISTEN 1
167 # define TCP_STATE_MIN 0
168 # define TCP_STATE_MAX 10
169 /* #endif HAVE_SYSCTLBYNAME */
171 #elif HAVE_LIBKVM_NLIST
172 static const char *tcp_state[] =
173 {
174 "CLOSED",
175 "LISTEN",
176 "SYN_SENT",
177 "SYN_RECV",
178 "ESTABLISHED",
179 "CLOSE_WAIT",
180 "FIN_WAIT1",
181 "CLOSING",
182 "LAST_ACK",
183 "FIN_WAIT2",
184 "TIME_WAIT"
185 };
187 static kvm_t *kvmd;
188 static u_long inpcbtable_off = 0;
189 struct inpcbtable *inpcbtable_ptr = NULL;
191 # define TCP_STATE_LISTEN 1
192 # define TCP_STATE_MIN 1
193 # define TCP_STATE_MAX 10
194 /* #endif HAVE_LIBKVM_NLIST */
196 #elif KERNEL_AIX
197 static const char *tcp_state[] =
198 {
199 "CLOSED",
200 "LISTEN",
201 "SYN_SENT",
202 "SYN_RCVD",
203 "ESTABLISHED",
204 "CLOSE_WAIT",
205 "FIN_WAIT_1",
206 "CLOSING",
207 "LAST_ACK",
208 "FIN_WAIT_2",
209 "TIME_WAIT"
210 };
212 # define TCP_STATE_LISTEN 1
213 # define TCP_STATE_MIN 0
214 # define TCP_STATE_MAX 10
216 struct netinfo_conn {
217 uint32_t unknow1[2];
218 uint16_t dstport;
219 uint16_t unknow2;
220 struct in6_addr dstaddr;
221 uint16_t srcport;
222 uint16_t unknow3;
223 struct in6_addr srcaddr;
224 uint32_t unknow4[36];
225 uint16_t tcp_state;
226 uint16_t unknow5[7];
227 };
229 struct netinfo_header {
230 unsigned int proto;
231 unsigned int size;
232 };
234 # define NETINFO_TCP 3
235 extern int netinfo (int proto, void *data, int *size, int n);
236 #endif /* KERNEL_AIX */
238 #define PORT_COLLECT_LOCAL 0x01
239 #define PORT_COLLECT_REMOTE 0x02
240 #define PORT_IS_LISTENING 0x04
242 typedef struct port_entry_s
243 {
244 uint16_t port;
245 uint16_t flags;
246 uint32_t count_local[TCP_STATE_MAX + 1];
247 uint32_t count_remote[TCP_STATE_MAX + 1];
248 struct port_entry_s *next;
249 } port_entry_t;
251 static const char *config_keys[] =
252 {
253 "ListeningPorts",
254 "LocalPort",
255 "RemotePort"
256 };
257 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
259 static int port_collect_listening = 0;
260 static port_entry_t *port_list_head = NULL;
262 static void conn_submit_port_entry (port_entry_t *pe)
263 {
264 value_t values[1];
265 value_list_t vl = VALUE_LIST_INIT;
266 int i;
268 vl.values = values;
269 vl.values_len = 1;
270 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
271 sstrncpy (vl.plugin, "tcpconns", sizeof (vl.plugin));
272 sstrncpy (vl.type, "tcp_connections", sizeof (vl.type));
274 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
275 || (pe->flags & PORT_COLLECT_LOCAL))
276 {
277 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
278 "%"PRIu16"-local", pe->port);
280 for (i = 1; i <= TCP_STATE_MAX; i++)
281 {
282 vl.values[0].gauge = pe->count_local[i];
284 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
286 plugin_dispatch_values (&vl);
287 }
288 }
290 if (pe->flags & PORT_COLLECT_REMOTE)
291 {
292 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
293 "%"PRIu16"-remote", pe->port);
295 for (i = 1; i <= TCP_STATE_MAX; i++)
296 {
297 vl.values[0].gauge = pe->count_remote[i];
299 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
301 plugin_dispatch_values (&vl);
302 }
303 }
304 } /* void conn_submit */
306 static void conn_submit_all (void)
307 {
308 port_entry_t *pe;
310 for (pe = port_list_head; pe != NULL; pe = pe->next)
311 conn_submit_port_entry (pe);
312 } /* void conn_submit_all */
314 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
315 {
316 port_entry_t *ret;
318 ret = port_list_head;
319 while (ret != NULL)
320 {
321 if (ret->port == port)
322 break;
323 ret = ret->next;
324 }
326 if ((ret == NULL) && (create != 0))
327 {
328 ret = (port_entry_t *) malloc (sizeof (port_entry_t));
329 if (ret == NULL)
330 return (NULL);
331 memset (ret, '\0', sizeof (port_entry_t));
333 ret->port = port;
334 ret->next = port_list_head;
335 port_list_head = ret;
336 }
338 return (ret);
339 } /* port_entry_t *conn_get_port_entry */
341 /* Removes ports that were added automatically due to the `ListeningPorts'
342 * setting but which are no longer listening. */
343 static void conn_reset_port_entry (void)
344 {
345 port_entry_t *prev = NULL;
346 port_entry_t *pe = port_list_head;
348 while (pe != NULL)
349 {
350 /* If this entry was created while reading the files (ant not when handling
351 * the configuration) remove it now. */
352 if ((pe->flags & (PORT_COLLECT_LOCAL
353 | PORT_COLLECT_REMOTE
354 | PORT_IS_LISTENING)) == 0)
355 {
356 port_entry_t *next = pe->next;
358 DEBUG ("tcpconns plugin: Removing temporary entry "
359 "for listening port %"PRIu16, pe->port);
361 if (prev == NULL)
362 port_list_head = next;
363 else
364 prev->next = next;
366 sfree (pe);
367 pe = next;
369 continue;
370 }
372 memset (pe->count_local, '\0', sizeof (pe->count_local));
373 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
374 pe->flags &= ~PORT_IS_LISTENING;
376 pe = pe->next;
377 }
378 } /* void conn_reset_port_entry */
380 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
381 {
382 port_entry_t *pe = NULL;
384 if ((state > TCP_STATE_MAX)
385 #if TCP_STATE_MIN > 0
386 || (state < TCP_STATE_MIN)
387 #endif
388 )
389 {
390 NOTICE ("tcpconns plugin: Ignoring connection with "
391 "unknown state 0x%02"PRIx8".", state);
392 return (-1);
393 }
395 /* Listening sockets */
396 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
397 {
398 pe = conn_get_port_entry (port_local, 1 /* create */);
399 if (pe != NULL)
400 pe->flags |= PORT_IS_LISTENING;
401 }
403 DEBUG ("tcpconns plugin: Connection %"PRIu16" <-> %"PRIu16" (%s)",
404 port_local, port_remote, tcp_state[state]);
406 pe = conn_get_port_entry (port_local, 0 /* no create */);
407 if (pe != NULL)
408 pe->count_local[state]++;
410 pe = conn_get_port_entry (port_remote, 0 /* no create */);
411 if (pe != NULL)
412 pe->count_remote[state]++;
414 return (0);
415 } /* int conn_handle_ports */
417 #if KERNEL_LINUX
418 static int conn_handle_line (char *buffer)
419 {
420 char *fields[32];
421 int fields_len;
423 char *endptr;
425 char *port_local_str;
426 char *port_remote_str;
427 uint16_t port_local;
428 uint16_t port_remote;
430 uint8_t state;
432 int buffer_len = strlen (buffer);
434 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
435 buffer[--buffer_len] = '\0';
436 if (buffer_len <= 0)
437 return (-1);
439 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
440 if (fields_len < 12)
441 {
442 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
443 return (-1);
444 }
446 port_local_str = strchr (fields[1], ':');
447 port_remote_str = strchr (fields[2], ':');
449 if ((port_local_str == NULL) || (port_remote_str == NULL))
450 return (-1);
451 port_local_str++;
452 port_remote_str++;
453 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
454 return (-1);
456 endptr = NULL;
457 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
458 if ((endptr == NULL) || (*endptr != '\0'))
459 return (-1);
461 endptr = NULL;
462 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
463 if ((endptr == NULL) || (*endptr != '\0'))
464 return (-1);
466 endptr = NULL;
467 state = (uint8_t) strtol (fields[3], &endptr, 16);
468 if ((endptr == NULL) || (*endptr != '\0'))
469 return (-1);
471 return (conn_handle_ports (port_local, port_remote, state));
472 } /* int conn_handle_line */
474 static int conn_read_file (const char *file)
475 {
476 FILE *fh;
477 char buffer[1024];
479 fh = fopen (file, "r");
480 if (fh == NULL)
481 return (-1);
483 while (fgets (buffer, sizeof (buffer), fh) != NULL)
484 {
485 conn_handle_line (buffer);
486 } /* while (fgets) */
488 fclose (fh);
490 return (0);
491 } /* int conn_read_file */
492 /* #endif KERNEL_LINUX */
494 #elif HAVE_SYSCTLBYNAME
495 /* #endif HAVE_SYSCTLBYNAME */
497 #elif HAVE_LIBKVM_NLIST
498 #endif /* HAVE_LIBKVM_NLIST */
500 static int conn_config (const char *key, const char *value)
501 {
502 if (strcasecmp (key, "ListeningPorts") == 0)
503 {
504 if (IS_TRUE (value))
505 port_collect_listening = 1;
506 else
507 port_collect_listening = 0;
508 }
509 else if ((strcasecmp (key, "LocalPort") == 0)
510 || (strcasecmp (key, "RemotePort") == 0))
511 {
512 port_entry_t *pe;
513 int port = atoi (value);
515 if ((port < 1) || (port > 65535))
516 {
517 ERROR ("tcpconns plugin: Invalid port: %i", port);
518 return (1);
519 }
521 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
522 if (pe == NULL)
523 {
524 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
525 return (1);
526 }
528 if (strcasecmp (key, "LocalPort") == 0)
529 pe->flags |= PORT_COLLECT_LOCAL;
530 else
531 pe->flags |= PORT_COLLECT_REMOTE;
532 }
533 else
534 {
535 return (-1);
536 }
538 return (0);
539 } /* int conn_config */
541 #if KERNEL_LINUX
542 static int conn_init (void)
543 {
544 if (port_list_head == NULL)
545 port_collect_listening = 1;
547 return (0);
548 } /* int conn_init */
550 static int conn_read (void)
551 {
552 int errors_num = 0;
554 conn_reset_port_entry ();
556 if (conn_read_file ("/proc/net/tcp") != 0)
557 errors_num++;
558 if (conn_read_file ("/proc/net/tcp6") != 0)
559 errors_num++;
561 if (errors_num < 2)
562 {
563 conn_submit_all ();
564 }
565 else
566 {
567 ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
568 "coult be read.");
569 return (-1);
570 }
572 return (0);
573 } /* int conn_read */
574 /* #endif KERNEL_LINUX */
576 #elif HAVE_SYSCTLBYNAME
577 static int conn_read (void)
578 {
579 int status;
580 char *buffer;
581 size_t buffer_len;;
583 struct xinpgen *in_orig;
584 struct xinpgen *in_ptr;
586 conn_reset_port_entry ();
588 buffer_len = 0;
589 status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
590 if (status < 0)
591 {
592 ERROR ("tcpconns plugin: sysctlbyname failed.");
593 return (-1);
594 }
596 buffer = (char *) malloc (buffer_len);
597 if (buffer == NULL)
598 {
599 ERROR ("tcpconns plugin: malloc failed.");
600 return (-1);
601 }
603 status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
604 if (status < 0)
605 {
606 ERROR ("tcpconns plugin: sysctlbyname failed.");
607 sfree (buffer);
608 return (-1);
609 }
611 if (buffer_len <= sizeof (struct xinpgen))
612 {
613 ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
614 sfree (buffer);
615 return (-1);
616 }
618 in_orig = (struct xinpgen *) buffer;
619 for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
620 in_ptr->xig_len > sizeof (struct xinpgen);
621 in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
622 {
623 struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
624 struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
625 struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
627 /* Ignore non-TCP sockets */
628 if (so->xso_protocol != IPPROTO_TCP)
629 continue;
631 /* Ignore PCBs which were freed during copyout. */
632 if (inp->inp_gencnt > in_orig->xig_gen)
633 continue;
635 if (((inp->inp_vflag & INP_IPV4) == 0)
636 && ((inp->inp_vflag & INP_IPV6) == 0))
637 continue;
639 conn_handle_ports (ntohs (inp->inp_lport), ntohs (inp->inp_fport),
640 tp->t_state);
641 } /* for (in_ptr) */
643 in_orig = NULL;
644 in_ptr = NULL;
645 sfree (buffer);
647 conn_submit_all ();
649 return (0);
650 } /* int conn_read */
651 /* #endif HAVE_SYSCTLBYNAME */
653 #elif HAVE_LIBKVM_NLIST
654 static int kread (u_long addr, void *buf, int size)
655 {
656 int status;
658 status = kvm_read (kvmd, addr, buf, size);
659 if (status != size)
660 {
661 ERROR ("tcpconns plugin: kvm_read failed (got %i, expected %i): %s\n",
662 status, size, kvm_geterr (kvmd));
663 return (-1);
664 }
665 return (0);
666 } /* int kread */
668 static int conn_init (void)
669 {
670 char buf[_POSIX2_LINE_MAX];
671 struct nlist nl[] =
672 {
673 #define N_TCBTABLE 0
674 { "_tcbtable" },
675 { "" }
676 };
677 int status;
679 kvmd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, buf);
680 if (kvmd == NULL)
681 {
682 ERROR ("tcpconns plugin: kvm_openfiles failed: %s", buf);
683 return (-1);
684 }
686 status = kvm_nlist (kvmd, nl);
687 if (status < 0)
688 {
689 ERROR ("tcpconns plugin: kvm_nlist failed with status %i.", status);
690 return (-1);
691 }
693 if (nl[N_TCBTABLE].n_type == 0)
694 {
695 ERROR ("tcpconns plugin: Error looking up kernel's namelist: "
696 "N_TCBTABLE is invalid.");
697 return (-1);
698 }
700 inpcbtable_off = (u_long) nl[N_TCBTABLE].n_value;
701 inpcbtable_ptr = (struct inpcbtable *) nl[N_TCBTABLE].n_value;
703 return (0);
704 } /* int conn_init */
706 static int conn_read (void)
707 {
708 struct inpcbtable table;
709 struct inpcb *head;
710 struct inpcb *next;
711 struct inpcb inpcb;
712 struct tcpcb tcpcb;
713 int status;
715 conn_reset_port_entry ();
717 /* Read the pcbtable from the kernel */
718 status = kread (inpcbtable_off, &table, sizeof (table));
719 if (status != 0)
720 return (-1);
722 /* Get the `head' pcb */
723 head = (struct inpcb *) &(inpcbtable_ptr->inpt_queue);
724 /* Get the first pcb */
725 next = (struct inpcb *)CIRCLEQ_FIRST (&table.inpt_queue);
727 while (next != head)
728 {
729 /* Read the pcb pointed to by `next' into `inpcb' */
730 kread ((u_long) next, &inpcb, sizeof (inpcb));
732 /* Advance `next' */
733 next = (struct inpcb *)CIRCLEQ_NEXT (&inpcb, inp_queue);
735 /* Ignore sockets, that are not connected. */
736 #ifdef __NetBSD__
737 if (inpcb.inp_af == AF_INET6)
738 continue; /* XXX see netbsd/src/usr.bin/netstat/inet6.c */
739 #else
740 if (!(inpcb.inp_flags & INP_IPV6)
741 && (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY))
742 continue;
743 if ((inpcb.inp_flags & INP_IPV6)
744 && IN6_IS_ADDR_UNSPECIFIED (&inpcb.inp_laddr6))
745 continue;
746 #endif
748 kread ((u_long) inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
749 conn_handle_ports (ntohs(inpcb.inp_lport), ntohs(inpcb.inp_fport), tcpcb.t_state);
750 } /* while (next != head) */
752 conn_submit_all ();
754 return (0);
755 }
756 /* #endif HAVE_LIBKVM_NLIST */
758 #elif KERNEL_AIX
760 static int conn_read (void)
761 {
762 int size;
763 int i;
764 int nconn;
765 void *data;
766 struct netinfo_header *header;
767 struct netinfo_conn *conn;
769 conn_reset_port_entry ();
771 size = netinfo(NETINFO_TCP, 0, 0, 0);
772 if (size < 0)
773 {
774 ERROR ("tcpconns plugin: netinfo failed return: %i", size);
775 return (-1);
776 }
778 if (size == 0)
779 return (0);
781 if ((size - sizeof (struct netinfo_header)) % sizeof (struct netinfo_conn))
782 {
783 ERROR ("tcpconns plugin: invalid buffer size");
784 return (-1);
785 }
787 data = malloc(size);
788 if (data == NULL)
789 {
790 ERROR ("tcpconns plugin: malloc failed");
791 return (-1);
792 }
794 if (netinfo(NETINFO_TCP, data, &size, 0) < 0)
795 {
796 ERROR ("tcpconns plugin: netinfo failed");
797 free(data);
798 return (-1);
799 }
801 header = (struct netinfo_header *)data;
802 nconn = header->size;
803 conn = (struct netinfo_conn *)(data + sizeof(struct netinfo_header));
805 for (i=0; i < nconn; conn++, i++)
806 {
807 conn_handle_ports (conn->srcport, conn->dstport, conn->tcp_state);
808 }
810 free(data);
812 conn_submit_all ();
814 return (0);
815 }
816 #endif /* KERNEL_AIX */
818 void module_register (void)
819 {
820 plugin_register_config ("tcpconns", conn_config,
821 config_keys, config_keys_num);
822 #if KERNEL_LINUX
823 plugin_register_init ("tcpconns", conn_init);
824 #elif HAVE_SYSCTLBYNAME
825 /* no initialization */
826 #elif HAVE_LIBKVM_NLIST
827 plugin_register_init ("tcpconns", conn_init);
828 #elif KERNEL_AIX
829 /* no initialization */
830 #endif
831 plugin_register_read ("tcpconns", conn_read);
832 } /* void module_register */
834 /*
835 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker :
836 */