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_pcb.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/ip.h>
100 #include <netinet/ip6.h>
101 #include <netinet/ip_var.h>
102 #include <netinet/tcp.h>
103 #include <netinet/tcp_seq.h>
104 #include <netinet/tcp_var.h>
105 #include <netinet/tcpip.h>
106 /* #endif HAVE_SYSCTLBYNAME */
108 /* This is for OpenBSD and NetBSD. */
109 #elif HAVE_LIBKVM_NLIST
110 #include <arpa/inet.h>
111 #include <net/route.h>
113 #include <netinet/in.h>
114 #include <netinet/in_pcb.h>
115 #include <netinet/in_systm.h>
116 #include <netinet/ip.h>
117 #include <netinet/ip_var.h>
118 #include <netinet/tcp.h>
119 #include <netinet/tcp_timer.h>
120 #include <netinet/tcp_var.h>
121 #include <sys/queue.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[] = {"", /* 0 */
156 #define TCP_STATE_LISTEN 10
157 #define TCP_STATE_MIN 1
158 #define TCP_STATE_MAX 11
159 /* #endif KERNEL_LINUX */
161 #elif HAVE_SYSCTLBYNAME
162 static const char *tcp_state[] = {"CLOSED", "LISTEN", "SYN_SENT",
163 "SYN_RECV", "ESTABLISHED", "CLOSE_WAIT",
164 "FIN_WAIT1", "CLOSING", "LAST_ACK",
165 "FIN_WAIT2", "TIME_WAIT"};
167 #define TCP_STATE_LISTEN 1
168 #define TCP_STATE_MIN 0
169 #define TCP_STATE_MAX 10
170 /* #endif HAVE_SYSCTLBYNAME */
172 #elif HAVE_LIBKVM_NLIST
173 static const char *tcp_state[] = {"CLOSED", "LISTEN", "SYN_SENT",
174 "SYN_RECV", "ESTABLISHED", "CLOSE_WAIT",
175 "FIN_WAIT1", "CLOSING", "LAST_ACK",
176 "FIN_WAIT2", "TIME_WAIT"};
179 static u_long inpcbtable_off = 0;
180 struct inpcbtable *inpcbtable_ptr = NULL;
182 #define TCP_STATE_LISTEN 1
183 #define TCP_STATE_MIN 1
184 #define TCP_STATE_MAX 10
185 /* #endif HAVE_LIBKVM_NLIST */
188 static const char *tcp_state[] = {"CLOSED", "LISTEN", "SYN_SENT",
189 "SYN_RECV", "ESTABLISHED", "CLOSE_WAIT",
190 "FIN_WAIT1", "CLOSING", "LAST_ACK",
191 "FIN_WAIT2", "TIME_WAIT"};
193 #define TCP_STATE_LISTEN 1
194 #define TCP_STATE_MIN 0
195 #define TCP_STATE_MAX 10
197 struct netinfo_conn {
201 struct in6_addr dstaddr;
204 struct in6_addr srcaddr;
205 uint32_t unknow4[36];
210 struct netinfo_header {
215 #define NETINFO_TCP 3
216 extern int netinfo(int proto, void *data, int *size, int n);
217 #endif /* KERNEL_AIX */
219 #define PORT_COLLECT_LOCAL 0x01
220 #define PORT_COLLECT_REMOTE 0x02
221 #define PORT_IS_LISTENING 0x04
223 typedef struct port_entry_s {
226 uint32_t count_local[TCP_STATE_MAX + 1];
227 uint32_t count_remote[TCP_STATE_MAX + 1];
228 struct port_entry_s *next;
231 static const char *config_keys[] = {"ListeningPorts", "LocalPort", "RemotePort",
233 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
235 static int port_collect_listening = 0;
236 static int port_collect_total = 0;
237 static port_entry_t *port_list_head = NULL;
238 static uint32_t count_total[TCP_STATE_MAX + 1];
241 #if HAVE_STRUCT_LINUX_INET_DIAG_REQ
242 /* This depends on linux inet_diag_req because if this structure is missing,
243 * sequence_number is useless and we get a compilation warning.
245 static uint32_t sequence_number = 0;
248 static enum { SRC_DUNNO, SRC_NETLINK, SRC_PROC } linux_source = SRC_DUNNO;
251 static void conn_prepare_vl(value_list_t *vl, value_t *values) {
254 sstrncpy(vl->host, hostname_g, sizeof(vl->host));
255 sstrncpy(vl->plugin, "tcpconns", sizeof(vl->plugin));
256 sstrncpy(vl->type, "tcp_connections", sizeof(vl->type));
259 static void conn_submit_port_entry(port_entry_t *pe) {
261 value_list_t vl = VALUE_LIST_INIT;
263 conn_prepare_vl(&vl, values);
265 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING)) ||
266 (pe->flags & PORT_COLLECT_LOCAL)) {
267 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
268 "%" PRIu16 "-local", pe->port);
270 for (int i = 1; i <= TCP_STATE_MAX; i++) {
271 vl.values[0].gauge = pe->count_local[i];
273 sstrncpy(vl.type_instance, tcp_state[i], sizeof(vl.type_instance));
275 plugin_dispatch_values(&vl);
279 if (pe->flags & PORT_COLLECT_REMOTE) {
280 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
281 "%" PRIu16 "-remote", pe->port);
283 for (int i = 1; i <= TCP_STATE_MAX; i++) {
284 vl.values[0].gauge = pe->count_remote[i];
286 sstrncpy(vl.type_instance, tcp_state[i], sizeof(vl.type_instance));
288 plugin_dispatch_values(&vl);
291 } /* void conn_submit */
293 static void conn_submit_port_total(void) {
295 value_list_t vl = VALUE_LIST_INIT;
297 conn_prepare_vl(&vl, values);
299 sstrncpy(vl.plugin_instance, "all", sizeof(vl.plugin_instance));
301 for (int i = 1; i <= TCP_STATE_MAX; i++) {
302 vl.values[0].gauge = count_total[i];
304 sstrncpy(vl.type_instance, tcp_state[i], sizeof(vl.type_instance));
306 plugin_dispatch_values(&vl);
310 static void conn_submit_all(void) {
311 if (port_collect_total)
312 conn_submit_port_total();
314 for (port_entry_t *pe = port_list_head; pe != NULL; pe = pe->next)
315 conn_submit_port_entry(pe);
316 } /* void conn_submit_all */
318 static port_entry_t *conn_get_port_entry(uint16_t port, int create) {
321 ret = port_list_head;
322 while (ret != NULL) {
323 if (ret->port == port)
328 if ((ret == NULL) && (create != 0)) {
329 ret = calloc(1, sizeof(*ret));
334 ret->next = port_list_head;
335 port_list_head = 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 port_entry_t *prev = NULL;
345 port_entry_t *pe = port_list_head;
347 memset(&count_total, '\0', sizeof(count_total));
350 /* If this entry was created while reading the files (ant not when handling
351 * the configuration) remove it now. */
353 (PORT_COLLECT_LOCAL | PORT_COLLECT_REMOTE | PORT_IS_LISTENING)) == 0) {
354 port_entry_t *next = pe->next;
356 DEBUG("tcpconns plugin: Removing temporary entry "
357 "for listening port %" PRIu16,
361 port_list_head = next;
371 memset(pe->count_local, '\0', sizeof(pe->count_local));
372 memset(pe->count_remote, '\0', sizeof(pe->count_remote));
373 pe->flags &= ~PORT_IS_LISTENING;
378 } /* void conn_reset_port_entry */
380 static int conn_handle_ports(uint16_t port_local, uint16_t port_remote,
382 port_entry_t *pe = NULL;
384 if ((state > TCP_STATE_MAX)
385 #if TCP_STATE_MIN > 0
386 || (state < TCP_STATE_MIN)
389 NOTICE("tcpconns plugin: Ignoring connection with "
390 "unknown state 0x%02" PRIx8 ".",
395 count_total[state]++;
397 /* Listening sockets */
398 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0)) {
399 pe = conn_get_port_entry(port_local, 1 /* create */);
401 pe->flags |= PORT_IS_LISTENING;
404 DEBUG("tcpconns plugin: Connection %" PRIu16 " <-> %" PRIu16 " (%s)",
405 port_local, port_remote, tcp_state[state]);
407 pe = conn_get_port_entry(port_local, 0 /* no create */);
409 pe->count_local[state]++;
411 pe = conn_get_port_entry(port_remote, 0 /* no create */);
413 pe->count_remote[state]++;
416 } /* int conn_handle_ports */
419 /* Returns zero on success, less than zero on socket error and greater than
420 * zero on other errors. */
421 static int conn_read_netlink(void) {
422 #if HAVE_STRUCT_LINUX_INET_DIAG_REQ
424 struct inet_diag_msg *r;
427 /* If this fails, it's likely a permission problem. We'll fall back to
428 * reading this information from files below. */
429 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG);
431 ERROR("tcpconns plugin: conn_read_netlink: socket(AF_NETLINK, SOCK_RAW, "
432 "NETLINK_INET_DIAG) failed: %s",
433 sstrerror(errno, buf, sizeof(buf)));
437 struct sockaddr_nl nladdr = {.nl_family = AF_NETLINK};
440 .nlh.nlmsg_len = sizeof(req),
441 .nlh.nlmsg_type = TCPDIAG_GETSOCK,
442 /* NLM_F_ROOT: return the complete table instead of a single entry.
443 * NLM_F_MATCH: return all entries matching criteria (not implemented)
444 * NLM_F_REQUEST: must be set on all request messages */
445 .nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST,
447 /* The sequence_number is used to track our messages. Since netlink is not
448 * reliable, we don't want to end up with a corrupt or incomplete old
449 * message in case the system is/was out of memory. */
450 .nlh.nlmsg_seq = ++sequence_number,
451 .r.idiag_family = AF_INET,
452 .r.idiag_states = 0xfff,
455 struct iovec iov = {.iov_base = &req, .iov_len = sizeof(req)};
457 struct msghdr msg = {.msg_name = (void *)&nladdr,
458 .msg_namelen = sizeof(nladdr),
462 if (sendmsg(fd, &msg, 0) < 0) {
463 ERROR("tcpconns plugin: conn_read_netlink: sendmsg(2) failed: %s",
464 sstrerror(errno, buf, sizeof(buf)));
470 iov.iov_len = sizeof(buf);
476 memset(&msg, 0, sizeof(msg));
477 msg.msg_name = (void *)&nladdr;
478 msg.msg_namelen = sizeof(nladdr);
482 status = recvmsg(fd, (void *)&msg, /* flags = */ 0);
484 if ((errno == EINTR) || (errno == EAGAIN))
487 ERROR("tcpconns plugin: conn_read_netlink: recvmsg(2) failed: %s",
488 sstrerror(errno, buf, sizeof(buf)));
491 } else if (status == 0) {
493 DEBUG("tcpconns plugin: conn_read_netlink: Unexpected zero-sized "
494 "reply from netlink socket.");
498 h = (struct nlmsghdr *)buf;
499 while (NLMSG_OK(h, status)) {
500 if (h->nlmsg_seq != sequence_number) {
501 h = NLMSG_NEXT(h, status);
505 if (h->nlmsg_type == NLMSG_DONE) {
508 } else if (h->nlmsg_type == NLMSG_ERROR) {
509 struct nlmsgerr *msg_error;
511 msg_error = NLMSG_DATA(h);
512 WARNING("tcpconns plugin: conn_read_netlink: Received error %i.",
521 /* This code does not (need to) distinguish between IPv4 and IPv6. */
522 conn_handle_ports(ntohs(r->id.idiag_sport), ntohs(r->id.idiag_dport),
525 h = NLMSG_NEXT(h, status);
526 } /* while (NLMSG_OK) */
529 /* Not reached because the while() loop above handles the exit condition. */
533 #endif /* HAVE_STRUCT_LINUX_INET_DIAG_REQ */
534 } /* int conn_read_netlink */
536 static int conn_handle_line(char *buffer) {
542 char *port_local_str;
543 char *port_remote_str;
545 uint16_t port_remote;
549 int buffer_len = strlen(buffer);
551 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
552 buffer[--buffer_len] = '\0';
556 fields_len = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
557 if (fields_len < 12) {
558 DEBUG("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
562 port_local_str = strchr(fields[1], ':');
563 port_remote_str = strchr(fields[2], ':');
565 if ((port_local_str == NULL) || (port_remote_str == NULL))
569 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
573 port_local = (uint16_t)strtol(port_local_str, &endptr, 16);
574 if ((endptr == NULL) || (*endptr != '\0'))
578 port_remote = (uint16_t)strtol(port_remote_str, &endptr, 16);
579 if ((endptr == NULL) || (*endptr != '\0'))
583 state = (uint8_t)strtol(fields[3], &endptr, 16);
584 if ((endptr == NULL) || (*endptr != '\0'))
587 return (conn_handle_ports(port_local, port_remote, state));
588 } /* int conn_handle_line */
590 static int conn_read_file(const char *file) {
594 fh = fopen(file, "r");
598 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
599 conn_handle_line(buffer);
600 } /* while (fgets) */
605 } /* int conn_read_file */
606 /* #endif KERNEL_LINUX */
608 #elif HAVE_SYSCTLBYNAME
609 /* #endif HAVE_SYSCTLBYNAME */
611 #elif HAVE_LIBKVM_NLIST
612 #endif /* HAVE_LIBKVM_NLIST */
614 static int conn_config(const char *key, const char *value) {
615 if (strcasecmp(key, "ListeningPorts") == 0) {
617 port_collect_listening = 1;
619 port_collect_listening = 0;
620 } else if ((strcasecmp(key, "LocalPort") == 0) ||
621 (strcasecmp(key, "RemotePort") == 0)) {
623 int port = atoi(value);
625 if ((port < 1) || (port > 65535)) {
626 ERROR("tcpconns plugin: Invalid port: %i", port);
630 pe = conn_get_port_entry((uint16_t)port, 1 /* create */);
632 ERROR("tcpconns plugin: conn_get_port_entry failed.");
636 if (strcasecmp(key, "LocalPort") == 0)
637 pe->flags |= PORT_COLLECT_LOCAL;
639 pe->flags |= PORT_COLLECT_REMOTE;
640 } else if (strcasecmp(key, "AllPortsSummary") == 0) {
642 port_collect_total = 1;
644 port_collect_total = 0;
650 } /* int conn_config */
653 static int conn_init(void) {
654 if (port_collect_total == 0 && port_list_head == NULL)
655 port_collect_listening = 1;
658 } /* int conn_init */
660 static int conn_read(void) {
663 conn_reset_port_entry();
665 if (linux_source == SRC_NETLINK) {
666 status = conn_read_netlink();
667 } else if (linux_source == SRC_PROC) {
670 if (conn_read_file("/proc/net/tcp") != 0)
672 if (conn_read_file("/proc/net/tcp6") != 0)
679 } else /* if (linux_source == SRC_DUNNO) */
681 /* Try to use netlink for getting this data, it is _much_ faster on systems
682 * with a large amount of connections. */
683 status = conn_read_netlink();
685 INFO("tcpconns plugin: Reading from netlink succeeded. "
686 "Will use the netlink method from now on.");
687 linux_source = SRC_NETLINK;
689 INFO("tcpconns plugin: Reading from netlink failed. "
690 "Will read from /proc from now on.");
691 linux_source = SRC_PROC;
693 /* return success here to avoid the "plugin failed" message. */
704 } /* int conn_read */
705 /* #endif KERNEL_LINUX */
707 #elif HAVE_SYSCTLBYNAME
708 static int conn_read(void) {
714 struct xinpgen *in_orig;
715 struct xinpgen *in_ptr;
717 conn_reset_port_entry();
720 status = sysctlbyname("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
722 ERROR("tcpconns plugin: sysctlbyname failed.");
726 buffer = malloc(buffer_len);
727 if (buffer == NULL) {
728 ERROR("tcpconns plugin: malloc failed.");
732 status = sysctlbyname("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
734 ERROR("tcpconns plugin: sysctlbyname failed.");
739 if (buffer_len <= sizeof(struct xinpgen)) {
740 ERROR("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
745 in_orig = (struct xinpgen *)buffer;
746 for (in_ptr = (struct xinpgen *)(((char *)in_orig) + in_orig->xig_len);
747 in_ptr->xig_len > sizeof(struct xinpgen);
748 in_ptr = (struct xinpgen *)(((char *)in_ptr) + in_ptr->xig_len)) {
749 struct tcpcb *tp = &((struct xtcpcb *)in_ptr)->xt_tp;
750 struct inpcb *inp = &((struct xtcpcb *)in_ptr)->xt_inp;
751 struct xsocket *so = &((struct xtcpcb *)in_ptr)->xt_socket;
753 /* Ignore non-TCP sockets */
754 if (so->xso_protocol != IPPROTO_TCP)
757 /* Ignore PCBs which were freed during copyout. */
758 if (inp->inp_gencnt > in_orig->xig_gen)
761 if (((inp->inp_vflag & INP_IPV4) == 0) &&
762 ((inp->inp_vflag & INP_IPV6) == 0))
765 conn_handle_ports(ntohs(inp->inp_lport), ntohs(inp->inp_fport),
776 } /* int conn_read */
777 /* #endif HAVE_SYSCTLBYNAME */
779 #elif HAVE_LIBKVM_NLIST
780 static int kread(u_long addr, void *buf, int size) {
783 status = kvm_read(kvmd, addr, buf, size);
784 if (status != size) {
785 ERROR("tcpconns plugin: kvm_read failed (got %i, expected %i): %s\n",
786 status, size, kvm_geterr(kvmd));
792 static int conn_init(void) {
793 char buf[_POSIX2_LINE_MAX];
794 struct nlist nl[] = {
796 {"_tcbtable"}, {""}};
799 kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf);
801 ERROR("tcpconns plugin: kvm_openfiles failed: %s", buf);
805 status = kvm_nlist(kvmd, nl);
807 ERROR("tcpconns plugin: kvm_nlist failed with status %i.", status);
811 if (nl[N_TCBTABLE].n_type == 0) {
812 ERROR("tcpconns plugin: Error looking up kernel's namelist: "
813 "N_TCBTABLE is invalid.");
817 inpcbtable_off = (u_long)nl[N_TCBTABLE].n_value;
818 inpcbtable_ptr = (struct inpcbtable *)nl[N_TCBTABLE].n_value;
821 } /* int conn_init */
823 static int conn_read(void) {
824 struct inpcbtable table;
825 #if !defined(__OpenBSD__) && \
826 (defined(__NetBSD_Version__) && __NetBSD_Version__ <= 699002700)
834 conn_reset_port_entry();
836 /* Read the pcbtable from the kernel */
837 status = kread(inpcbtable_off, &table, sizeof(table));
841 #if defined(__OpenBSD__) || \
842 (defined(__NetBSD_Version__) && __NetBSD_Version__ > 699002700)
843 /* inpt_queue is a TAILQ on OpenBSD */
844 /* Get the first pcb */
845 next = (struct inpcb *)TAILQ_FIRST(&table.inpt_queue);
848 /* Get the `head' pcb */
849 head = (struct inpcb *)&(inpcbtable_ptr->inpt_queue);
850 /* Get the first pcb */
851 next = (struct inpcb *)CIRCLEQ_FIRST(&table.inpt_queue);
856 /* Read the pcb pointed to by `next' into `inpcb' */
857 status = kread((u_long)next, &inpcb, sizeof(inpcb));
862 #if defined(__OpenBSD__) || \
863 (defined(__NetBSD_Version__) && __NetBSD_Version__ > 699002700)
864 /* inpt_queue is a TAILQ on OpenBSD */
865 next = (struct inpcb *)TAILQ_NEXT(&inpcb, inp_queue);
867 next = (struct inpcb *)CIRCLEQ_NEXT(&inpcb, inp_queue);
870 /* Ignore sockets, that are not connected. */
872 if (inpcb.inp_af == AF_INET6)
873 continue; /* XXX see netbsd/src/usr.bin/netstat/inet6.c */
875 if (!(inpcb.inp_flags & INP_IPV6) &&
876 (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY))
878 if ((inpcb.inp_flags & INP_IPV6) &&
879 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6))
883 status = kread((u_long)inpcb.inp_ppcb, &tcpcb, sizeof(tcpcb));
886 conn_handle_ports(ntohs(inpcb.inp_lport), ntohs(inpcb.inp_fport),
888 } /* while (next != head) */
894 /* #endif HAVE_LIBKVM_NLIST */
898 static int conn_read(void) {
902 struct netinfo_header *header;
903 struct netinfo_conn *conn;
905 conn_reset_port_entry();
907 size = netinfo(NETINFO_TCP, 0, 0, 0);
909 ERROR("tcpconns plugin: netinfo failed return: %i", size);
916 if ((size - sizeof(struct netinfo_header)) % sizeof(struct netinfo_conn)) {
917 ERROR("tcpconns plugin: invalid buffer size");
923 ERROR("tcpconns plugin: malloc failed");
927 if (netinfo(NETINFO_TCP, data, &size, 0) < 0) {
928 ERROR("tcpconns plugin: netinfo failed");
933 header = (struct netinfo_header *)data;
934 nconn = header->size;
935 conn = (struct netinfo_conn *)(data + sizeof(struct netinfo_header));
937 for (int i = 0; i < nconn; conn++, i++) {
938 conn_handle_ports(conn->srcport, conn->dstport, conn->tcp_state);
947 #endif /* KERNEL_AIX */
949 void module_register(void) {
950 plugin_register_config("tcpconns", conn_config, config_keys, config_keys_num);
952 plugin_register_init("tcpconns", conn_init);
953 #elif HAVE_SYSCTLBYNAME
954 /* no initialization */
955 #elif HAVE_LIBKVM_NLIST
956 plugin_register_init("tcpconns", conn_init);
958 /* no initialization */
960 plugin_register_read("tcpconns", conn_read);
961 } /* void module_register */
964 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker :