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
64 #if defined(__OpenBSD__) || defined(__NetBSD__)
65 #undef HAVE_SYSCTLBYNAME /* force HAVE_LIBKVM_NLIST path */
68 #if !KERNEL_LINUX && !HAVE_SYSCTLBYNAME && !HAVE_LIBKVM_NLIST && !KERNEL_AIX
69 # error "No applicable input method."
73 # include <asm/types.h>
74 /* sys/socket.h is necessary to compile when using netlink on older systems. */
75 # include <sys/socket.h>
76 # include <linux/netlink.h>
77 #if HAVE_LINUX_INET_DIAG_H
78 # include <linux/inet_diag.h>
80 # include <sys/socket.h>
81 # include <arpa/inet.h>
82 /* #endif KERNEL_LINUX */
84 #elif HAVE_SYSCTLBYNAME
85 # include <sys/socketvar.h>
86 # include <sys/sysctl.h>
88 /* Some includes needed for compiling on FreeBSD */
91 # include <sys/types.h>
94 # include <sys/socket.h>
100 # include <net/route.h>
101 # include <netinet/in.h>
102 # include <netinet/in_systm.h>
103 # include <netinet/ip.h>
104 # include <netinet/ip6.h>
105 # include <netinet/in_pcb.h>
106 # include <netinet/ip_var.h>
107 # include <netinet/tcp.h>
108 # include <netinet/tcpip.h>
109 # include <netinet/tcp_seq.h>
110 # include <netinet/tcp_var.h>
111 /* #endif HAVE_SYSCTLBYNAME */
113 /* This is for OpenBSD and NetBSD. */
114 #elif HAVE_LIBKVM_NLIST
115 # include <sys/queue.h>
116 # include <sys/socket.h>
117 # include <net/route.h>
118 # include <netinet/in.h>
119 # include <netinet/in_systm.h>
120 # include <netinet/ip.h>
121 # include <netinet/ip_var.h>
122 # include <netinet/in_pcb.h>
123 # include <netinet/tcp.h>
124 # include <netinet/tcp_timer.h>
125 # include <netinet/tcp_var.h>
127 # include <arpa/inet.h>
128 # if !defined(HAVE_BSD_NLIST_H) || !HAVE_BSD_NLIST_H
130 # else /* HAVE_BSD_NLIST_H */
131 # include <bsd/nlist.h>
134 /* #endif HAVE_LIBKVM_NLIST */
137 # include <arpa/inet.h>
138 # include <sys/socketvar.h>
139 #endif /* KERNEL_AIX */
142 #if HAVE_STRUCT_LINUX_INET_DIAG_REQ
145 struct inet_diag_req r;
149 static const char *tcp_state[] =
165 # define TCP_STATE_LISTEN 10
166 # define TCP_STATE_MIN 1
167 # define TCP_STATE_MAX 11
168 /* #endif KERNEL_LINUX */
170 #elif HAVE_SYSCTLBYNAME
171 static const char *tcp_state[] =
186 # define TCP_STATE_LISTEN 1
187 # define TCP_STATE_MIN 0
188 # define TCP_STATE_MAX 10
189 /* #endif HAVE_SYSCTLBYNAME */
191 #elif HAVE_LIBKVM_NLIST
192 static const char *tcp_state[] =
208 static u_long inpcbtable_off = 0;
209 struct inpcbtable *inpcbtable_ptr = NULL;
211 # define TCP_STATE_LISTEN 1
212 # define TCP_STATE_MIN 1
213 # define TCP_STATE_MAX 10
214 /* #endif HAVE_LIBKVM_NLIST */
217 static const char *tcp_state[] =
232 # define TCP_STATE_LISTEN 1
233 # define TCP_STATE_MIN 0
234 # define TCP_STATE_MAX 10
236 struct netinfo_conn {
240 struct in6_addr dstaddr;
243 struct in6_addr srcaddr;
244 uint32_t unknow4[36];
249 struct netinfo_header {
254 # define NETINFO_TCP 3
255 extern int netinfo (int proto, void *data, int *size, int n);
256 #endif /* KERNEL_AIX */
258 #define PORT_COLLECT_LOCAL 0x01
259 #define PORT_COLLECT_REMOTE 0x02
260 #define PORT_IS_LISTENING 0x04
262 typedef struct port_entry_s
266 uint32_t count_local[TCP_STATE_MAX + 1];
267 uint32_t count_remote[TCP_STATE_MAX + 1];
268 struct port_entry_s *next;
271 static const char *config_keys[] =
278 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
280 static int port_collect_listening = 0;
281 static int port_collect_total = 0;
282 static port_entry_t *port_list_head = NULL;
283 static uint32_t count_total[TCP_STATE_MAX + 1];
286 #if HAVE_STRUCT_LINUX_INET_DIAG_REQ
287 /* This depends on linux inet_diag_req because if this structure is missing,
288 * sequence_number is useless and we get a compilation warning.
290 static uint32_t sequence_number = 0;
298 } linux_source = SRC_DUNNO;
301 static void conn_prepare_vl (value_list_t *vl, value_t *values)
305 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
306 sstrncpy (vl->plugin, "tcpconns", sizeof (vl->plugin));
307 sstrncpy (vl->type, "tcp_connections", sizeof (vl->type));
310 static void conn_submit_port_entry (port_entry_t *pe)
313 value_list_t vl = VALUE_LIST_INIT;
316 conn_prepare_vl (&vl, values);
318 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
319 || (pe->flags & PORT_COLLECT_LOCAL))
321 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
322 "%"PRIu16"-local", pe->port);
324 for (i = 1; i <= TCP_STATE_MAX; i++)
326 vl.values[0].gauge = pe->count_local[i];
328 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
330 plugin_dispatch_values (&vl);
334 if (pe->flags & PORT_COLLECT_REMOTE)
336 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
337 "%"PRIu16"-remote", pe->port);
339 for (i = 1; i <= TCP_STATE_MAX; i++)
341 vl.values[0].gauge = pe->count_remote[i];
343 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
345 plugin_dispatch_values (&vl);
348 } /* void conn_submit */
350 static void conn_submit_port_total (void)
353 value_list_t vl = VALUE_LIST_INIT;
356 conn_prepare_vl (&vl, values);
358 sstrncpy (vl.plugin_instance, "all", sizeof (vl.plugin_instance));
360 for (i = 1; i <= TCP_STATE_MAX; i++)
362 vl.values[0].gauge = count_total[i];
364 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
366 plugin_dispatch_values (&vl);
370 static void conn_submit_all (void)
374 if (port_collect_total)
375 conn_submit_port_total ();
377 for (pe = port_list_head; pe != NULL; pe = pe->next)
378 conn_submit_port_entry (pe);
379 } /* void conn_submit_all */
381 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
385 ret = port_list_head;
388 if (ret->port == port)
393 if ((ret == NULL) && (create != 0))
395 ret = (port_entry_t *) malloc (sizeof (port_entry_t));
398 memset (ret, '\0', sizeof (port_entry_t));
401 ret->next = port_list_head;
402 port_list_head = ret;
406 } /* port_entry_t *conn_get_port_entry */
408 /* Removes ports that were added automatically due to the `ListeningPorts'
409 * setting but which are no longer listening. */
410 static void conn_reset_port_entry (void)
412 port_entry_t *prev = NULL;
413 port_entry_t *pe = port_list_head;
415 memset (&count_total, '\0', sizeof(count_total));
419 /* If this entry was created while reading the files (ant not when handling
420 * the configuration) remove it now. */
421 if ((pe->flags & (PORT_COLLECT_LOCAL
422 | PORT_COLLECT_REMOTE
423 | PORT_IS_LISTENING)) == 0)
425 port_entry_t *next = pe->next;
427 DEBUG ("tcpconns plugin: Removing temporary entry "
428 "for listening port %"PRIu16, pe->port);
431 port_list_head = next;
441 memset (pe->count_local, '\0', sizeof (pe->count_local));
442 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
443 pe->flags &= ~PORT_IS_LISTENING;
447 } /* void conn_reset_port_entry */
449 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
451 port_entry_t *pe = NULL;
453 if ((state > TCP_STATE_MAX)
454 #if TCP_STATE_MIN > 0
455 || (state < TCP_STATE_MIN)
459 NOTICE ("tcpconns plugin: Ignoring connection with "
460 "unknown state 0x%02"PRIx8".", state);
464 count_total[state]++;
466 /* Listening sockets */
467 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
469 pe = conn_get_port_entry (port_local, 1 /* create */);
471 pe->flags |= PORT_IS_LISTENING;
474 DEBUG ("tcpconns plugin: Connection %"PRIu16" <-> %"PRIu16" (%s)",
475 port_local, port_remote, tcp_state[state]);
477 pe = conn_get_port_entry (port_local, 0 /* no create */);
479 pe->count_local[state]++;
481 pe = conn_get_port_entry (port_remote, 0 /* no create */);
483 pe->count_remote[state]++;
486 } /* int conn_handle_ports */
489 /* Returns zero on success, less than zero on socket error and greater than
490 * zero on other errors. */
491 static int conn_read_netlink (void)
493 #if HAVE_STRUCT_LINUX_INET_DIAG_REQ
495 struct sockaddr_nl nladdr;
499 struct inet_diag_msg *r;
502 /* If this fails, it's likely a permission problem. We'll fall back to
503 * reading this information from files below. */
504 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG);
507 ERROR ("tcpconns plugin: conn_read_netlink: socket(AF_NETLINK, SOCK_RAW, "
508 "NETLINK_INET_DIAG) failed: %s",
509 sstrerror (errno, buf, sizeof (buf)));
513 memset(&nladdr, 0, sizeof(nladdr));
514 nladdr.nl_family = AF_NETLINK;
516 memset(&req, 0, sizeof(req));
517 req.nlh.nlmsg_len = sizeof(req);
518 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
519 /* NLM_F_ROOT: return the complete table instead of a single entry.
520 * NLM_F_MATCH: return all entries matching criteria (not implemented)
521 * NLM_F_REQUEST: must be set on all request messages */
522 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
523 req.nlh.nlmsg_pid = 0;
524 /* The sequence_number is used to track our messages. Since netlink is not
525 * reliable, we don't want to end up with a corrupt or incomplete old
526 * message in case the system is/was out of memory. */
527 req.nlh.nlmsg_seq = ++sequence_number;
528 req.r.idiag_family = AF_INET;
529 req.r.idiag_states = 0xfff;
532 memset(&iov, 0, sizeof(iov));
534 iov.iov_len = sizeof(req);
536 memset(&msg, 0, sizeof(msg));
537 msg.msg_name = (void*)&nladdr;
538 msg.msg_namelen = sizeof(nladdr);
542 if (sendmsg (fd, &msg, 0) < 0)
544 ERROR ("tcpconns plugin: conn_read_netlink: sendmsg(2) failed: %s",
545 sstrerror (errno, buf, sizeof (buf)));
551 iov.iov_len = sizeof(buf);
558 memset(&msg, 0, sizeof(msg));
559 msg.msg_name = (void*)&nladdr;
560 msg.msg_namelen = sizeof(nladdr);
564 status = recvmsg(fd, (void *) &msg, /* flags = */ 0);
567 if ((errno == EINTR) || (errno == EAGAIN))
570 ERROR ("tcpconns plugin: conn_read_netlink: recvmsg(2) failed: %s",
571 sstrerror (errno, buf, sizeof (buf)));
575 else if (status == 0)
578 DEBUG ("tcpconns plugin: conn_read_netlink: Unexpected zero-sized "
579 "reply from netlink socket.");
583 h = (struct nlmsghdr*)buf;
584 while (NLMSG_OK(h, status))
586 if (h->nlmsg_seq != sequence_number)
588 h = NLMSG_NEXT(h, status);
592 if (h->nlmsg_type == NLMSG_DONE)
597 else if (h->nlmsg_type == NLMSG_ERROR)
599 struct nlmsgerr *msg_error;
601 msg_error = NLMSG_DATA(h);
602 WARNING ("tcpconns plugin: conn_read_netlink: Received error %i.",
611 /* This code does not (need to) distinguish between IPv4 and IPv6. */
612 conn_handle_ports (ntohs(r->id.idiag_sport),
613 ntohs(r->id.idiag_dport),
616 h = NLMSG_NEXT(h, status);
617 } /* while (NLMSG_OK) */
620 /* Not reached because the while() loop above handles the exit condition. */
624 #endif /* HAVE_STRUCT_LINUX_INET_DIAG_REQ */
625 } /* int conn_read_netlink */
627 static int conn_handle_line (char *buffer)
634 char *port_local_str;
635 char *port_remote_str;
637 uint16_t port_remote;
641 int buffer_len = strlen (buffer);
643 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
644 buffer[--buffer_len] = '\0';
648 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
651 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
655 port_local_str = strchr (fields[1], ':');
656 port_remote_str = strchr (fields[2], ':');
658 if ((port_local_str == NULL) || (port_remote_str == NULL))
662 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
666 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
667 if ((endptr == NULL) || (*endptr != '\0'))
671 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
672 if ((endptr == NULL) || (*endptr != '\0'))
676 state = (uint8_t) strtol (fields[3], &endptr, 16);
677 if ((endptr == NULL) || (*endptr != '\0'))
680 return (conn_handle_ports (port_local, port_remote, state));
681 } /* int conn_handle_line */
683 static int conn_read_file (const char *file)
688 fh = fopen (file, "r");
692 while (fgets (buffer, sizeof (buffer), fh) != NULL)
694 conn_handle_line (buffer);
695 } /* while (fgets) */
700 } /* int conn_read_file */
701 /* #endif KERNEL_LINUX */
703 #elif HAVE_SYSCTLBYNAME
704 /* #endif HAVE_SYSCTLBYNAME */
706 #elif HAVE_LIBKVM_NLIST
707 #endif /* HAVE_LIBKVM_NLIST */
709 static int conn_config (const char *key, const char *value)
711 if (strcasecmp (key, "ListeningPorts") == 0)
714 port_collect_listening = 1;
716 port_collect_listening = 0;
718 else if ((strcasecmp (key, "LocalPort") == 0)
719 || (strcasecmp (key, "RemotePort") == 0))
722 int port = atoi (value);
724 if ((port < 1) || (port > 65535))
726 ERROR ("tcpconns plugin: Invalid port: %i", port);
730 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
733 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
737 if (strcasecmp (key, "LocalPort") == 0)
738 pe->flags |= PORT_COLLECT_LOCAL;
740 pe->flags |= PORT_COLLECT_REMOTE;
742 else if (strcasecmp (key, "AllPortsSummary") == 0)
745 port_collect_total = 1;
747 port_collect_total = 0;
755 } /* int conn_config */
758 static int conn_init (void)
760 if (port_collect_total == 0 && port_list_head == NULL)
761 port_collect_listening = 1;
764 } /* int conn_init */
766 static int conn_read (void)
770 conn_reset_port_entry ();
772 if (linux_source == SRC_NETLINK)
774 status = conn_read_netlink ();
776 else if (linux_source == SRC_PROC)
780 if (conn_read_file ("/proc/net/tcp") != 0)
782 if (conn_read_file ("/proc/net/tcp6") != 0)
790 else /* if (linux_source == SRC_DUNNO) */
792 /* Try to use netlink for getting this data, it is _much_ faster on systems
793 * with a large amount of connections. */
794 status = conn_read_netlink ();
797 INFO ("tcpconns plugin: Reading from netlink succeeded. "
798 "Will use the netlink method from now on.");
799 linux_source = SRC_NETLINK;
803 INFO ("tcpconns plugin: Reading from netlink failed. "
804 "Will read from /proc from now on.");
805 linux_source = SRC_PROC;
807 /* return success here to avoid the "plugin failed" message. */
818 } /* int conn_read */
819 /* #endif KERNEL_LINUX */
821 #elif HAVE_SYSCTLBYNAME
822 static int conn_read (void)
828 struct xinpgen *in_orig;
829 struct xinpgen *in_ptr;
831 conn_reset_port_entry ();
834 status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
837 ERROR ("tcpconns plugin: sysctlbyname failed.");
841 buffer = (char *) malloc (buffer_len);
844 ERROR ("tcpconns plugin: malloc failed.");
848 status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
851 ERROR ("tcpconns plugin: sysctlbyname failed.");
856 if (buffer_len <= sizeof (struct xinpgen))
858 ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
863 in_orig = (struct xinpgen *) buffer;
864 for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
865 in_ptr->xig_len > sizeof (struct xinpgen);
866 in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
868 struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
869 struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
870 struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
872 /* Ignore non-TCP sockets */
873 if (so->xso_protocol != IPPROTO_TCP)
876 /* Ignore PCBs which were freed during copyout. */
877 if (inp->inp_gencnt > in_orig->xig_gen)
880 if (((inp->inp_vflag & INP_IPV4) == 0)
881 && ((inp->inp_vflag & INP_IPV6) == 0))
884 conn_handle_ports (ntohs (inp->inp_lport), ntohs (inp->inp_fport),
895 } /* int conn_read */
896 /* #endif HAVE_SYSCTLBYNAME */
898 #elif HAVE_LIBKVM_NLIST
899 static int kread (u_long addr, void *buf, int size)
903 status = kvm_read (kvmd, addr, buf, size);
906 ERROR ("tcpconns plugin: kvm_read failed (got %i, expected %i): %s\n",
907 status, size, kvm_geterr (kvmd));
913 static int conn_init (void)
915 char buf[_POSIX2_LINE_MAX];
924 kvmd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, buf);
927 ERROR ("tcpconns plugin: kvm_openfiles failed: %s", buf);
931 status = kvm_nlist (kvmd, nl);
934 ERROR ("tcpconns plugin: kvm_nlist failed with status %i.", status);
938 if (nl[N_TCBTABLE].n_type == 0)
940 ERROR ("tcpconns plugin: Error looking up kernel's namelist: "
941 "N_TCBTABLE is invalid.");
945 inpcbtable_off = (u_long) nl[N_TCBTABLE].n_value;
946 inpcbtable_ptr = (struct inpcbtable *) nl[N_TCBTABLE].n_value;
949 } /* int conn_init */
951 static int conn_read (void)
953 struct inpcbtable table;
954 #if !defined(__OpenBSD__) && (defined(__NetBSD_Version__) && __NetBSD_Version__ <= 699002700)
962 conn_reset_port_entry ();
964 /* Read the pcbtable from the kernel */
965 status = kread (inpcbtable_off, &table, sizeof (table));
969 #if defined(__OpenBSD__) || (defined(__NetBSD_Version__) && __NetBSD_Version__ > 699002700)
970 /* inpt_queue is a TAILQ on OpenBSD */
971 /* Get the first pcb */
972 next = (struct inpcb *)TAILQ_FIRST (&table.inpt_queue);
975 /* Get the `head' pcb */
976 head = (struct inpcb *) &(inpcbtable_ptr->inpt_queue);
977 /* Get the first pcb */
978 next = (struct inpcb *)CIRCLEQ_FIRST (&table.inpt_queue);
983 /* Read the pcb pointed to by `next' into `inpcb' */
984 kread ((u_long) next, &inpcb, sizeof (inpcb));
987 #if defined(__OpenBSD__) || (defined(__NetBSD_Version__) && __NetBSD_Version__ > 699002700)
988 /* inpt_queue is a TAILQ on OpenBSD */
989 next = (struct inpcb *)TAILQ_NEXT (&inpcb, inp_queue);
991 next = (struct inpcb *)CIRCLEQ_NEXT (&inpcb, inp_queue);
994 /* Ignore sockets, that are not connected. */
996 if (inpcb.inp_af == AF_INET6)
997 continue; /* XXX see netbsd/src/usr.bin/netstat/inet6.c */
999 if (!(inpcb.inp_flags & INP_IPV6)
1000 && (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY))
1002 if ((inpcb.inp_flags & INP_IPV6)
1003 && IN6_IS_ADDR_UNSPECIFIED (&inpcb.inp_laddr6))
1007 kread ((u_long) inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
1008 conn_handle_ports (ntohs(inpcb.inp_lport), ntohs(inpcb.inp_fport), tcpcb.t_state);
1009 } /* while (next != head) */
1015 /* #endif HAVE_LIBKVM_NLIST */
1019 static int conn_read (void)
1025 struct netinfo_header *header;
1026 struct netinfo_conn *conn;
1028 conn_reset_port_entry ();
1030 size = netinfo(NETINFO_TCP, 0, 0, 0);
1033 ERROR ("tcpconns plugin: netinfo failed return: %i", size);
1040 if ((size - sizeof (struct netinfo_header)) % sizeof (struct netinfo_conn))
1042 ERROR ("tcpconns plugin: invalid buffer size");
1046 data = malloc(size);
1049 ERROR ("tcpconns plugin: malloc failed");
1053 if (netinfo(NETINFO_TCP, data, &size, 0) < 0)
1055 ERROR ("tcpconns plugin: netinfo failed");
1060 header = (struct netinfo_header *)data;
1061 nconn = header->size;
1062 conn = (struct netinfo_conn *)(data + sizeof(struct netinfo_header));
1064 for (i=0; i < nconn; conn++, i++)
1066 conn_handle_ports (conn->srcport, conn->dstport, conn->tcp_state);
1075 #endif /* KERNEL_AIX */
1077 void module_register (void)
1079 plugin_register_config ("tcpconns", conn_config,
1080 config_keys, config_keys_num);
1082 plugin_register_init ("tcpconns", conn_init);
1083 #elif HAVE_SYSCTLBYNAME
1084 /* no initialization */
1085 #elif HAVE_LIBKVM_NLIST
1086 plugin_register_init ("tcpconns", conn_init);
1088 /* no initialization */
1090 plugin_register_read ("tcpconns", conn_read);
1091 } /* void module_register */
1094 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker :