2 * collectd - src/tcpconns.c
3 * Copyright (C) 2007,2008 Florian octo Forster
4 * Copyright (C) 2008 Michael Stapelberg
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.
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.
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
20 * Florian octo Forster <octo at collectd.org>
21 * Michael Stapelberg <michael+git at stapelberg.de>
25 * Code within `HAVE_LIBKVM_NLIST' blocks is provided under the following
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 $
32 * Copyright (c) 1983, 1988, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
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.
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
65 #if defined(__OpenBSD__) || defined(__NetBSD__)
66 #undef HAVE_SYSCTLBYNAME /* force HAVE_LIBKVM_NLIST path */
69 #if !KERNEL_LINUX && !HAVE_SYSCTLBYNAME && !HAVE_LIBKVM_NLIST && !KERNEL_AIX
70 # error "No applicable input method."
74 # include <asm/types.h>
75 # include <linux/netlink.h>
76 #if HAVE_LINUX_INET_DIAG_H
77 # include <linux/inet_diag.h>
79 # include <arpa/inet.h>
80 /* #endif KERNEL_LINUX */
82 #elif HAVE_SYSCTLBYNAME
83 # include <sys/socketvar.h>
84 # include <sys/sysctl.h>
86 /* Some includes needed for compiling on FreeBSD */
89 # include <sys/types.h>
95 # include <net/route.h>
96 # include <netinet/in.h>
97 # include <netinet/in_systm.h>
98 # include <netinet/ip.h>
99 # include <netinet/ip6.h>
100 # include <netinet/in_pcb.h>
101 # include <netinet/ip_var.h>
102 # include <netinet/tcp.h>
103 # include <netinet/tcpip.h>
104 # include <netinet/tcp_seq.h>
105 # include <netinet/tcp_var.h>
106 /* #endif HAVE_SYSCTLBYNAME */
108 /* This is for OpenBSD and NetBSD. */
109 #elif HAVE_LIBKVM_NLIST
110 # include <sys/queue.h>
111 # include <net/route.h>
112 # include <netinet/in.h>
113 # include <netinet/in_systm.h>
114 # include <netinet/ip.h>
115 # include <netinet/ip_var.h>
116 # include <netinet/in_pcb.h>
117 # include <netinet/tcp.h>
118 # include <netinet/tcp_timer.h>
119 # include <netinet/tcp_var.h>
121 # include <arpa/inet.h>
122 # if !defined(HAVE_BSD_NLIST_H) || !HAVE_BSD_NLIST_H
124 # else /* HAVE_BSD_NLIST_H */
125 # include <bsd/nlist.h>
128 /* #endif HAVE_LIBKVM_NLIST */
131 # include <arpa/inet.h>
132 # include <sys/socketvar.h>
133 #endif /* KERNEL_AIX */
136 #if HAVE_STRUCT_LINUX_INET_DIAG_REQ
139 struct inet_diag_req r;
143 static const char *tcp_state[] =
159 # define TCP_STATE_LISTEN 10
160 # define TCP_STATE_MIN 1
161 # define TCP_STATE_MAX 11
162 /* #endif KERNEL_LINUX */
164 #elif HAVE_SYSCTLBYNAME
165 static const char *tcp_state[] =
180 # define TCP_STATE_LISTEN 1
181 # define TCP_STATE_MIN 0
182 # define TCP_STATE_MAX 10
183 /* #endif HAVE_SYSCTLBYNAME */
185 #elif HAVE_LIBKVM_NLIST
186 static const char *tcp_state[] =
202 static u_long inpcbtable_off = 0;
203 struct inpcbtable *inpcbtable_ptr = NULL;
205 # define TCP_STATE_LISTEN 1
206 # define TCP_STATE_MIN 1
207 # define TCP_STATE_MAX 10
208 /* #endif HAVE_LIBKVM_NLIST */
211 static const char *tcp_state[] =
226 # define TCP_STATE_LISTEN 1
227 # define TCP_STATE_MIN 0
228 # define TCP_STATE_MAX 10
230 struct netinfo_conn {
234 struct in6_addr dstaddr;
237 struct in6_addr srcaddr;
238 uint32_t unknow4[36];
243 struct netinfo_header {
248 # define NETINFO_TCP 3
249 extern int netinfo (int proto, void *data, int *size, int n);
250 #endif /* KERNEL_AIX */
252 #define PORT_COLLECT_LOCAL 0x01
253 #define PORT_COLLECT_REMOTE 0x02
254 #define PORT_IS_LISTENING 0x04
256 typedef struct port_entry_s
260 uint32_t count_local[TCP_STATE_MAX + 1];
261 uint32_t count_remote[TCP_STATE_MAX + 1];
262 struct port_entry_s *next;
265 static const char *config_keys[] =
272 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
274 static int port_collect_listening = 0;
275 static int port_collect_total = 0;
276 static port_entry_t *port_list_head = NULL;
277 static uint32_t count_total[TCP_STATE_MAX + 1];
280 #if HAVE_STRUCT_LINUX_INET_DIAG_REQ
281 /* This depends on linux inet_diag_req because if this structure is missing,
282 * sequence_number is useless and we get a compilation warning.
284 static uint32_t sequence_number = 0;
292 } linux_source = SRC_DUNNO;
295 static void conn_prepare_vl (value_list_t *vl, value_t *values)
299 sstrncpy (vl->plugin, "tcpconns", sizeof (vl->plugin));
300 sstrncpy (vl->type, "tcp_connections", sizeof (vl->type));
303 static void conn_submit_port_entry (port_entry_t *pe)
306 value_list_t vl = VALUE_LIST_INIT;
308 conn_prepare_vl (&vl, values);
310 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
311 || (pe->flags & PORT_COLLECT_LOCAL))
313 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
314 "%"PRIu16"-local", pe->port);
316 for (int i = 1; i <= TCP_STATE_MAX; i++)
318 vl.values[0].gauge = pe->count_local[i];
320 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
322 plugin_dispatch_values (&vl);
326 if (pe->flags & PORT_COLLECT_REMOTE)
328 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
329 "%"PRIu16"-remote", pe->port);
331 for (int i = 1; i <= TCP_STATE_MAX; i++)
333 vl.values[0].gauge = pe->count_remote[i];
335 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
337 plugin_dispatch_values (&vl);
340 } /* void conn_submit */
342 static void conn_submit_port_total (void)
345 value_list_t vl = VALUE_LIST_INIT;
347 conn_prepare_vl (&vl, values);
349 sstrncpy (vl.plugin_instance, "all", sizeof (vl.plugin_instance));
351 for (int i = 1; i <= TCP_STATE_MAX; i++)
353 vl.values[0].gauge = count_total[i];
355 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
357 plugin_dispatch_values (&vl);
361 static void conn_submit_all (void)
363 if (port_collect_total)
364 conn_submit_port_total ();
366 for (port_entry_t *pe = port_list_head; pe != NULL; pe = pe->next)
367 conn_submit_port_entry (pe);
368 } /* void conn_submit_all */
370 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
374 ret = port_list_head;
377 if (ret->port == port)
382 if ((ret == NULL) && (create != 0))
384 ret = calloc (1, sizeof (*ret));
389 ret->next = port_list_head;
390 port_list_head = ret;
394 } /* port_entry_t *conn_get_port_entry */
396 /* Removes ports that were added automatically due to the `ListeningPorts'
397 * setting but which are no longer listening. */
398 static void conn_reset_port_entry (void)
400 port_entry_t *prev = NULL;
401 port_entry_t *pe = port_list_head;
403 memset (&count_total, '\0', sizeof(count_total));
407 /* If this entry was created while reading the files (ant not when handling
408 * the configuration) remove it now. */
409 if ((pe->flags & (PORT_COLLECT_LOCAL
410 | PORT_COLLECT_REMOTE
411 | PORT_IS_LISTENING)) == 0)
413 port_entry_t *next = pe->next;
415 DEBUG ("tcpconns plugin: Removing temporary entry "
416 "for listening port %"PRIu16, pe->port);
419 port_list_head = next;
429 memset (pe->count_local, '\0', sizeof (pe->count_local));
430 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
431 pe->flags &= ~PORT_IS_LISTENING;
436 } /* void conn_reset_port_entry */
438 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
440 port_entry_t *pe = NULL;
442 if ((state > TCP_STATE_MAX)
443 #if TCP_STATE_MIN > 0
444 || (state < TCP_STATE_MIN)
448 NOTICE ("tcpconns plugin: Ignoring connection with "
449 "unknown state 0x%02"PRIx8".", state);
453 count_total[state]++;
455 /* Listening sockets */
456 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
458 pe = conn_get_port_entry (port_local, 1 /* create */);
460 pe->flags |= PORT_IS_LISTENING;
463 DEBUG ("tcpconns plugin: Connection %"PRIu16" <-> %"PRIu16" (%s)",
464 port_local, port_remote, tcp_state[state]);
466 pe = conn_get_port_entry (port_local, 0 /* no create */);
468 pe->count_local[state]++;
470 pe = conn_get_port_entry (port_remote, 0 /* no create */);
472 pe->count_remote[state]++;
475 } /* int conn_handle_ports */
478 /* Returns zero on success, less than zero on socket error and greater than
479 * zero on other errors. */
480 static int conn_read_netlink (void)
482 #if HAVE_STRUCT_LINUX_INET_DIAG_REQ
484 struct inet_diag_msg *r;
487 /* If this fails, it's likely a permission problem. We'll fall back to
488 * reading this information from files below. */
489 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG);
492 ERROR ("tcpconns plugin: conn_read_netlink: socket(AF_NETLINK, SOCK_RAW, "
493 "NETLINK_INET_DIAG) failed: %s",
494 sstrerror (errno, buf, sizeof (buf)));
498 struct sockaddr_nl nladdr = {
499 .nl_family = AF_NETLINK
503 .nlh.nlmsg_len = sizeof(req),
504 .nlh.nlmsg_type = TCPDIAG_GETSOCK,
505 /* NLM_F_ROOT: return the complete table instead of a single entry.
506 * NLM_F_MATCH: return all entries matching criteria (not implemented)
507 * NLM_F_REQUEST: must be set on all request messages */
508 .nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST,
510 /* The sequence_number is used to track our messages. Since netlink is not
511 * reliable, we don't want to end up with a corrupt or incomplete old
512 * message in case the system is/was out of memory. */
513 .nlh.nlmsg_seq = ++sequence_number,
514 .r.idiag_family = AF_INET,
515 .r.idiag_states = 0xfff,
521 .iov_len = sizeof(req)
524 struct msghdr msg = {
525 .msg_name = (void*)&nladdr,
526 .msg_namelen = sizeof(nladdr),
531 if (sendmsg (fd, &msg, 0) < 0)
533 ERROR ("tcpconns plugin: conn_read_netlink: sendmsg(2) failed: %s",
534 sstrerror (errno, buf, sizeof (buf)));
540 iov.iov_len = sizeof(buf);
547 memset(&msg, 0, sizeof(msg));
548 msg.msg_name = (void*)&nladdr;
549 msg.msg_namelen = sizeof(nladdr);
553 status = recvmsg(fd, (void *) &msg, /* flags = */ 0);
556 if ((errno == EINTR) || (errno == EAGAIN))
559 ERROR ("tcpconns plugin: conn_read_netlink: recvmsg(2) failed: %s",
560 sstrerror (errno, buf, sizeof (buf)));
564 else if (status == 0)
567 DEBUG ("tcpconns plugin: conn_read_netlink: Unexpected zero-sized "
568 "reply from netlink socket.");
572 h = (struct nlmsghdr*)buf;
573 while (NLMSG_OK(h, status))
575 if (h->nlmsg_seq != sequence_number)
577 h = NLMSG_NEXT(h, status);
581 if (h->nlmsg_type == NLMSG_DONE)
586 else if (h->nlmsg_type == NLMSG_ERROR)
588 struct nlmsgerr *msg_error;
590 msg_error = NLMSG_DATA(h);
591 WARNING ("tcpconns plugin: conn_read_netlink: Received error %i.",
600 /* This code does not (need to) distinguish between IPv4 and IPv6. */
601 conn_handle_ports (ntohs(r->id.idiag_sport),
602 ntohs(r->id.idiag_dport),
605 h = NLMSG_NEXT(h, status);
606 } /* while (NLMSG_OK) */
609 /* Not reached because the while() loop above handles the exit condition. */
613 #endif /* HAVE_STRUCT_LINUX_INET_DIAG_REQ */
614 } /* int conn_read_netlink */
616 static int conn_handle_line (char *buffer)
623 char *port_local_str;
624 char *port_remote_str;
626 uint16_t port_remote;
630 int buffer_len = strlen (buffer);
632 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
633 buffer[--buffer_len] = '\0';
637 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
640 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
644 port_local_str = strchr (fields[1], ':');
645 port_remote_str = strchr (fields[2], ':');
647 if ((port_local_str == NULL) || (port_remote_str == NULL))
651 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
655 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
656 if ((endptr == NULL) || (*endptr != '\0'))
660 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
661 if ((endptr == NULL) || (*endptr != '\0'))
665 state = (uint8_t) strtol (fields[3], &endptr, 16);
666 if ((endptr == NULL) || (*endptr != '\0'))
669 return (conn_handle_ports (port_local, port_remote, state));
670 } /* int conn_handle_line */
672 static int conn_read_file (const char *file)
677 fh = fopen (file, "r");
681 while (fgets (buffer, sizeof (buffer), fh) != NULL)
683 conn_handle_line (buffer);
684 } /* while (fgets) */
689 } /* int conn_read_file */
690 /* #endif KERNEL_LINUX */
692 #elif HAVE_SYSCTLBYNAME
693 /* #endif HAVE_SYSCTLBYNAME */
695 #elif HAVE_LIBKVM_NLIST
696 #endif /* HAVE_LIBKVM_NLIST */
698 static int conn_config (const char *key, const char *value)
700 if (strcasecmp (key, "ListeningPorts") == 0)
703 port_collect_listening = 1;
705 port_collect_listening = 0;
707 else if ((strcasecmp (key, "LocalPort") == 0)
708 || (strcasecmp (key, "RemotePort") == 0))
711 int port = atoi (value);
713 if ((port < 1) || (port > 65535))
715 ERROR ("tcpconns plugin: Invalid port: %i", port);
719 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
722 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
726 if (strcasecmp (key, "LocalPort") == 0)
727 pe->flags |= PORT_COLLECT_LOCAL;
729 pe->flags |= PORT_COLLECT_REMOTE;
731 else if (strcasecmp (key, "AllPortsSummary") == 0)
734 port_collect_total = 1;
736 port_collect_total = 0;
744 } /* int conn_config */
747 static int conn_init (void)
749 if (port_collect_total == 0 && port_list_head == NULL)
750 port_collect_listening = 1;
753 } /* int conn_init */
755 static int conn_read (void)
759 conn_reset_port_entry ();
761 if (linux_source == SRC_NETLINK)
763 status = conn_read_netlink ();
765 else if (linux_source == SRC_PROC)
769 if (conn_read_file ("/proc/net/tcp") != 0)
771 if (conn_read_file ("/proc/net/tcp6") != 0)
779 else /* if (linux_source == SRC_DUNNO) */
781 /* Try to use netlink for getting this data, it is _much_ faster on systems
782 * with a large amount of connections. */
783 status = conn_read_netlink ();
786 INFO ("tcpconns plugin: Reading from netlink succeeded. "
787 "Will use the netlink method from now on.");
788 linux_source = SRC_NETLINK;
792 INFO ("tcpconns plugin: Reading from netlink failed. "
793 "Will read from /proc from now on.");
794 linux_source = SRC_PROC;
796 /* return success here to avoid the "plugin failed" message. */
807 } /* int conn_read */
808 /* #endif KERNEL_LINUX */
810 #elif HAVE_SYSCTLBYNAME
811 static int conn_read (void)
817 struct xinpgen *in_orig;
818 struct xinpgen *in_ptr;
820 conn_reset_port_entry ();
823 status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
826 ERROR ("tcpconns plugin: sysctlbyname failed.");
830 buffer = malloc (buffer_len);
833 ERROR ("tcpconns plugin: malloc failed.");
837 status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
840 ERROR ("tcpconns plugin: sysctlbyname failed.");
845 if (buffer_len <= sizeof (struct xinpgen))
847 ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
852 in_orig = (struct xinpgen *) buffer;
853 for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
854 in_ptr->xig_len > sizeof (struct xinpgen);
855 in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
857 struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
858 struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
859 struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
861 /* Ignore non-TCP sockets */
862 if (so->xso_protocol != IPPROTO_TCP)
865 /* Ignore PCBs which were freed during copyout. */
866 if (inp->inp_gencnt > in_orig->xig_gen)
869 if (((inp->inp_vflag & INP_IPV4) == 0)
870 && ((inp->inp_vflag & INP_IPV6) == 0))
873 conn_handle_ports (ntohs (inp->inp_lport), ntohs (inp->inp_fport),
884 } /* int conn_read */
885 /* #endif HAVE_SYSCTLBYNAME */
887 #elif HAVE_LIBKVM_NLIST
888 static int kread (u_long addr, void *buf, int size)
892 status = kvm_read (kvmd, addr, buf, size);
895 ERROR ("tcpconns plugin: kvm_read failed (got %i, expected %i): %s\n",
896 status, size, kvm_geterr (kvmd));
902 static int conn_init (void)
904 char buf[_POSIX2_LINE_MAX];
913 kvmd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, buf);
916 ERROR ("tcpconns plugin: kvm_openfiles failed: %s", buf);
920 status = kvm_nlist (kvmd, nl);
923 ERROR ("tcpconns plugin: kvm_nlist failed with status %i.", status);
927 if (nl[N_TCBTABLE].n_type == 0)
929 ERROR ("tcpconns plugin: Error looking up kernel's namelist: "
930 "N_TCBTABLE is invalid.");
934 inpcbtable_off = (u_long) nl[N_TCBTABLE].n_value;
935 inpcbtable_ptr = (struct inpcbtable *) nl[N_TCBTABLE].n_value;
938 } /* int conn_init */
940 static int conn_read (void)
942 struct inpcbtable table;
943 #if !defined(__OpenBSD__) && (defined(__NetBSD_Version__) && __NetBSD_Version__ <= 699002700)
951 conn_reset_port_entry ();
953 /* Read the pcbtable from the kernel */
954 status = kread (inpcbtable_off, &table, sizeof (table));
958 #if defined(__OpenBSD__) || (defined(__NetBSD_Version__) && __NetBSD_Version__ > 699002700)
959 /* inpt_queue is a TAILQ on OpenBSD */
960 /* Get the first pcb */
961 next = (struct inpcb *)TAILQ_FIRST (&table.inpt_queue);
964 /* Get the `head' pcb */
965 head = (struct inpcb *) &(inpcbtable_ptr->inpt_queue);
966 /* Get the first pcb */
967 next = (struct inpcb *)CIRCLEQ_FIRST (&table.inpt_queue);
972 /* Read the pcb pointed to by `next' into `inpcb' */
973 status = kread ((u_long) next, &inpcb, sizeof (inpcb));
978 #if defined(__OpenBSD__) || (defined(__NetBSD_Version__) && __NetBSD_Version__ > 699002700)
979 /* inpt_queue is a TAILQ on OpenBSD */
980 next = (struct inpcb *)TAILQ_NEXT (&inpcb, inp_queue);
982 next = (struct inpcb *)CIRCLEQ_NEXT (&inpcb, inp_queue);
985 /* Ignore sockets, that are not connected. */
987 if (inpcb.inp_af == AF_INET6)
988 continue; /* XXX see netbsd/src/usr.bin/netstat/inet6.c */
990 if (!(inpcb.inp_flags & INP_IPV6)
991 && (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY))
993 if ((inpcb.inp_flags & INP_IPV6)
994 && IN6_IS_ADDR_UNSPECIFIED (&inpcb.inp_laddr6))
998 status = kread ((u_long) inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
1001 conn_handle_ports (ntohs(inpcb.inp_lport), ntohs(inpcb.inp_fport), tcpcb.t_state);
1002 } /* while (next != head) */
1008 /* #endif HAVE_LIBKVM_NLIST */
1012 static int conn_read (void)
1017 struct netinfo_header *header;
1018 struct netinfo_conn *conn;
1020 conn_reset_port_entry ();
1022 size = netinfo(NETINFO_TCP, 0, 0, 0);
1025 ERROR ("tcpconns plugin: netinfo failed return: %i", size);
1032 if ((size - sizeof (struct netinfo_header)) % sizeof (struct netinfo_conn))
1034 ERROR ("tcpconns plugin: invalid buffer size");
1038 data = malloc(size);
1041 ERROR ("tcpconns plugin: malloc failed");
1045 if (netinfo(NETINFO_TCP, data, &size, 0) < 0)
1047 ERROR ("tcpconns plugin: netinfo failed");
1052 header = (struct netinfo_header *)data;
1053 nconn = header->size;
1054 conn = (struct netinfo_conn *)(data + sizeof(struct netinfo_header));
1056 for (int i = 0; i < nconn; conn++, i++)
1058 conn_handle_ports (conn->srcport, conn->dstport, conn->tcp_state);
1067 #endif /* KERNEL_AIX */
1069 void module_register (void)
1071 plugin_register_config ("tcpconns", conn_config,
1072 config_keys, config_keys_num);
1074 plugin_register_init ("tcpconns", conn_init);
1075 #elif HAVE_SYSCTLBYNAME
1076 /* no initialization */
1077 #elif HAVE_LIBKVM_NLIST
1078 plugin_register_init ("tcpconns", conn_init);
1080 /* no initialization */
1082 plugin_register_read ("tcpconns", conn_read);
1083 } /* void module_register */
1086 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker :