tcpconns plugin: Add compatibility for OpenBSD.
[collectd.git] / src / tcpconns.c
1 /**
2  * collectd - src/tcpconns.c
3  * Copyright (C) 2007  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Author:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 /**
23  * Code within `__OpenBSD__' blocks is provided under the following license:
24  *
25  * $collectd: parts of tcpconns.c, 2008/08/08 03:48:30 Michael Stapelberg $
26  * $OpenBSD: inet.c,v 1.100 2007/06/19 05:28:30 ray Exp $
27  * $NetBSD: inet.c,v 1.14 1995/10/03 21:42:37 thorpej Exp $
28  *
29  * Copyright (c) 1983, 1988, 1993
30  *      The Regents of the University of California.  All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in the
39  *    documentation and/or other materials provided with the distribution.
40  * 3. Neither the name of the University nor the names of its contributors
41  *    may be used to endorse or promote products derived from this software
42  *    without specific prior written permission.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54  * SUCH DAMAGE.
55  */
56
57 #include "collectd.h"
58 #include "common.h"
59 #include "plugin.h"
60
61 #if !KERNEL_LINUX && !HAVE_SYSCTLBYNAME && !__OpenBSD__
62 # error "No applicable input method."
63 #endif
64
65 #if KERNEL_LINUX
66 /* #endif KERNEL_LINUX */
67
68 #elif HAVE_SYSCTLBYNAME
69 # include <sys/socketvar.h>
70 # include <sys/sysctl.h>
71
72 /* Some includes needed for compiling on FreeBSD */
73 #include <sys/time.h>
74 #if HAVE_SYS_TYPES_H
75 # include <sys/types.h>
76 #endif
77 #if HAVE_SYS_SOCKET_H
78 # include <sys/socket.h>
79 #endif
80 #if HAVE_NET_IF_H
81 # include <net/if.h>
82 #endif
83
84 # include <net/route.h>
85 # include <netinet/in.h>
86 # include <netinet/in_systm.h>
87 # include <netinet/ip.h>
88 # include <netinet/ip6.h>
89 # include <netinet/in_pcb.h>
90 # include <netinet/ip_var.h>
91 # include <netinet/tcp.h>
92 # include <netinet/tcpip.h>
93 # include <netinet/tcp_seq.h>
94 # include <netinet/tcp_var.h>
95 /* #endif HAVE_SYSCTLBYNAME */
96
97 #elif __OpenBSD__
98 # include <sys/queue.h>
99 # 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/in_pcb.h>
105 # include <netinet/tcp.h>
106 # include <netinet/tcp_timer.h>
107 # include <netinet/tcp_var.h>
108 # include <netdb.h>
109 # include <arpa/inet.h>
110 # include <nlist.h>
111 # include <kvm.h>
112 #endif /* __OpenBSD__ */
113
114 #if KERNEL_LINUX
115 static const char *tcp_state[] =
116 {
117   "", /* 0 */
118   "ESTABLISHED",
119   "SYN_SENT",
120   "SYN_RECV",
121   "FIN_WAIT1",
122   "FIN_WAIT2",
123   "TIME_WAIT",
124   "CLOSED",
125   "CLOSE_WAIT",
126   "LAST_ACK",
127   "LISTEN", /* 10 */
128   "CLOSING"
129 };
130
131 # define TCP_STATE_LISTEN 10
132 # define TCP_STATE_MIN 1
133 # define TCP_STATE_MAX 11
134 /* #endif KERNEL_LINUX */
135
136 #elif HAVE_SYSCTLBYNAME
137 static const char *tcp_state[] =
138 {
139   "CLOSED",
140   "LISTEN",
141   "SYN_SENT",
142   "SYN_RECV",
143   "ESTABLISHED",
144   "CLOSE_WAIT",
145   "FIN_WAIT1",
146   "CLOSING",
147   "LAST_ACK",
148   "FIN_WAIT2",
149   "TIME_WAIT"
150 };
151
152 # define TCP_STATE_LISTEN 1
153 # define TCP_STATE_MIN 0
154 # define TCP_STATE_MAX 10
155 /* #endif HAVE_SYSCTLBYNAME */
156
157 #elif __OpenBSD__
158 static const char *tcp_state[] =
159 {
160   "CLOSED",
161   "LISTEN",
162   "SYN_SENT",
163   "SYN_RCVD",
164   "ESTABLISHED",
165   "CLOSE_WAIT",
166   "FIN_WAIT_1",
167   "CLOSING",
168   "LAST_ACK",
169   "FIN_WAIT_2",
170   "TIME_WAIT"
171 };
172
173 static kvm_t *kvmd;
174
175 static struct nlist nl[] = {
176 #define N_MBSTAT        0
177         { "_mbstat" },
178 #define N_IPSTAT        1
179         { "_ipstat" },
180 #define N_TCBTABLE      2
181         { "_tcbtable" },
182 #define N_TCPSTAT       3
183         { "_tcpstat" },
184 #define N_UDBTABLE      4
185         { "_udbtable" },
186 #define N_UDPSTAT       5
187         { "_udpstat" },
188 #define N_IFNET         6
189         { "_ifnet" },
190 #define N_ICMPSTAT      7
191         { "_icmpstat" },
192 #define N_RTSTAT        8
193         { "_rtstat" },
194 #define N_UNIXSW        9
195         { "_unixsw" },
196 #define N_RTREE         10
197         { "_rt_tables"},
198 #define N_FILE          11
199         { "_file" },
200 #define N_IGMPSTAT      12
201         { "_igmpstat" },
202 #define N_MRTPROTO      13
203         { "_ip_mrtproto" },
204 #define N_MRTSTAT       14
205         { "_mrtstat" },
206 #define N_MFCHASHTBL    15
207         { "_mfchashtbl" },
208 #define N_MFCHASH       16
209         { "_mfchash" },
210         { "_viftable" },
211 #define N_AHSTAT        18
212         { "_ahstat"},
213 #define N_ESPSTAT       19
214         { "_espstat"},
215 #define N_IP4STAT       20
216         { "_ipipstat"},
217 #define N_DDPSTAT       21
218         { "_ddpstat"},
219 #define N_DDPCB         22
220         { "_ddpcb"},
221 #define N_ETHERIPSTAT   23
222         { "_etheripstat"},
223 #define N_IP6STAT       24
224         { "_ip6stat" },
225 #define N_ICMP6STAT     25
226         { "_icmp6stat" },
227 #define N_PIM6STAT      26
228         { "_pim6stat" },
229 #define N_MRT6PROTO     27
230         { "_ip6_mrtproto" },
231 #define N_MRT6STAT      28
232         { "_mrt6stat" },
233 #define N_MF6CTABLE     29
234         { "_mf6ctable" },
235 #define N_MIF6TABLE     30
236         { "_mif6table" },
237 #define N_MBPOOL        31
238         { "_mbpool" },
239 #define N_MCLPOOL       32
240         { "_mclpool" },
241 #define N_IPCOMPSTAT    33
242         { "_ipcompstat" },
243 #define N_RIP6STAT      34
244         { "_rip6stat" },
245 #define N_CARPSTAT      35
246         { "_carpstats" },
247 #define N_RAWIPTABLE    36
248         { "_rawcbtable" },
249 #define N_RAWIP6TABLE   37
250         { "_rawin6pcbtable" },
251 #define N_PFSYNCSTAT    38
252         { "_pfsyncstats" },
253 #define N_PIMSTAT       39
254         { "_pimstat" },
255 #define N_AF2RTAFIDX    40
256         { "_af2rtafidx" },
257 #define N_RTBLIDMAX     41
258         { "_rtbl_id_max" },
259 #define N_RTMASK        42
260         { "_mask_rnhead" },
261
262         { "" }
263 };
264
265 # define TCP_STATE_LISTEN 1
266 # define TCP_STATE_MIN 1
267 # define TCP_STATE_MAX 10
268 #endif /* __OpenBSD__ */
269
270 #define PORT_COLLECT_LOCAL  0x01
271 #define PORT_COLLECT_REMOTE 0x02
272 #define PORT_IS_LISTENING   0x04
273
274 typedef struct port_entry_s
275 {
276   uint16_t port;
277   uint16_t flags;
278   uint32_t count_local[TCP_STATE_MAX + 1];
279   uint32_t count_remote[TCP_STATE_MAX + 1];
280   struct port_entry_s *next;
281 } port_entry_t;
282
283 static const char *config_keys[] =
284 {
285   "ListeningPorts",
286   "LocalPort",
287   "RemotePort"
288 };
289 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
290
291 static int port_collect_listening = 0;
292 static port_entry_t *port_list_head = NULL;
293
294 static void conn_submit_port_entry (port_entry_t *pe)
295 {
296   value_t values[1];
297   value_list_t vl = VALUE_LIST_INIT;
298   int i;
299
300   vl.values = values;
301   vl.values_len = 1;
302   vl.time = time (NULL);
303   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
304   sstrncpy (vl.plugin, "tcpconns", sizeof (vl.plugin));
305   sstrncpy (vl.type, "tcp_connections", sizeof (vl.type));
306
307   if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
308       || (pe->flags & PORT_COLLECT_LOCAL))
309   {
310     ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
311         "%hu-local", pe->port);
312
313     for (i = 1; i <= TCP_STATE_MAX; i++)
314     {
315       vl.values[0].gauge = pe->count_local[i];
316
317       sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
318
319       plugin_dispatch_values (&vl);
320     }
321   }
322
323   if (pe->flags & PORT_COLLECT_REMOTE)
324   {
325     ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
326         "%hu-remote", pe->port);
327
328     for (i = 1; i <= TCP_STATE_MAX; i++)
329     {
330       vl.values[0].gauge = pe->count_remote[i];
331
332       sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
333
334       plugin_dispatch_values (&vl);
335     }
336   }
337 } /* void conn_submit */
338
339 static void conn_submit_all (void)
340 {
341   port_entry_t *pe;
342
343   for (pe = port_list_head; pe != NULL; pe = pe->next)
344     conn_submit_port_entry (pe);
345 } /* void conn_submit_all */
346
347 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
348 {
349   port_entry_t *ret;
350
351   ret = port_list_head;
352   while (ret != NULL)
353   {
354     if (ret->port == port)
355       break;
356     ret = ret->next;
357   }
358
359   if ((ret == NULL) && (create != 0))
360   {
361     ret = (port_entry_t *) malloc (sizeof (port_entry_t));
362     if (ret == NULL)
363       return (NULL);
364     memset (ret, '\0', sizeof (port_entry_t));
365
366     ret->port = port;
367     ret->next = port_list_head;
368     port_list_head = ret;
369   }
370
371   return (ret);
372 } /* port_entry_t *conn_get_port_entry */
373
374 /* Removes ports that were added automatically due to the `ListeningPorts'
375  * setting but which are no longer listening. */
376 static void conn_reset_port_entry (void)
377 {
378   port_entry_t *prev = NULL;
379   port_entry_t *pe = port_list_head;
380
381   while (pe != NULL)
382   {
383     /* If this entry was created while reading the files (ant not when handling
384      * the configuration) remove it now. */
385     if ((pe->flags & (PORT_COLLECT_LOCAL
386             | PORT_COLLECT_REMOTE
387             | PORT_IS_LISTENING)) == 0)
388     {
389       port_entry_t *next = pe->next;
390
391       DEBUG ("tcpconns plugin: Removing temporary entry "
392           "for listening port %hu", pe->port);
393
394       if (prev == NULL)
395         port_list_head = next;
396       else
397         prev->next = next;
398
399       sfree (pe);
400       pe = next;
401
402       continue;
403     }
404
405     memset (pe->count_local, '\0', sizeof (pe->count_local));
406     memset (pe->count_remote, '\0', sizeof (pe->count_remote));
407     pe->flags &= ~PORT_IS_LISTENING;
408
409     pe = pe->next;
410   }
411 } /* void conn_reset_port_entry */
412
413 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
414 {
415   port_entry_t *pe = NULL;
416
417   if ((state > TCP_STATE_MAX)
418 #if TCP_STATE_MIN > 0
419       || (state < TCP_STATE_MIN)
420 #endif
421      )
422   {
423     NOTICE ("tcpconns plugin: Ignoring connection with unknown state 0x%02x.",
424         state);
425     return (-1);
426   }
427
428   /* Listening sockets */
429   if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
430   {
431     pe = conn_get_port_entry (port_local, 1 /* create */);
432     if (pe != NULL)
433       pe->flags |= PORT_IS_LISTENING;
434   }
435
436   DEBUG ("tcpconns plugin: Connection %hu <-> %hu (%s)",
437       port_local, port_remote, tcp_state[state]);
438
439   pe = conn_get_port_entry (port_local, 0 /* no create */);
440   if (pe != NULL)
441     pe->count_local[state]++;
442
443   pe = conn_get_port_entry (port_remote, 0 /* no create */);
444   if (pe != NULL)
445     pe->count_remote[state]++;
446
447   return (0);
448 } /* int conn_handle_ports */
449
450 #if KERNEL_LINUX
451 static int conn_handle_line (char *buffer)
452 {
453   char *fields[32];
454   int   fields_len;
455
456   char *endptr;
457
458   char *port_local_str;
459   char *port_remote_str;
460   uint16_t port_local;
461   uint16_t port_remote;
462
463   uint8_t state;
464
465   int buffer_len = strlen (buffer);
466
467   while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
468     buffer[--buffer_len] = '\0';
469   if (buffer_len <= 0)
470     return (-1);
471
472   fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
473   if (fields_len < 12)
474   {
475     DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
476     return (-1);
477   }
478
479   port_local_str  = strchr (fields[1], ':');
480   port_remote_str = strchr (fields[2], ':');
481
482   if ((port_local_str == NULL) || (port_remote_str == NULL))
483     return (-1);
484   port_local_str++;
485   port_remote_str++;
486   if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
487     return (-1);
488
489   endptr = NULL;
490   port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
491   if ((endptr == NULL) || (*endptr != '\0'))
492     return (-1);
493
494   endptr = NULL;
495   port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
496   if ((endptr == NULL) || (*endptr != '\0'))
497     return (-1);
498
499   endptr = NULL;
500   state = (uint8_t) strtol (fields[3], &endptr, 16);
501   if ((endptr == NULL) || (*endptr != '\0'))
502     return (-1);
503
504   return (conn_handle_ports (port_local, port_remote, state));
505 } /* int conn_handle_line */
506
507 static int conn_read_file (const char *file)
508 {
509   FILE *fh;
510   char buffer[1024];
511
512   fh = fopen (file, "r");
513   if (fh == NULL)
514     return (-1);
515
516   while (fgets (buffer, sizeof (buffer), fh) != NULL)
517   {
518     conn_handle_line (buffer);
519   } /* while (fgets) */
520
521   fclose (fh);
522
523   return (0);
524 } /* int conn_read_file */
525 /* #endif KERNEL_LINUX */
526
527 #elif HAVE_SYSCTLBYNAME
528 #endif /* HAVE_SYSCTLBYNAME */
529
530 static int conn_config (const char *key, const char *value)
531 {
532   if (strcasecmp (key, "ListeningPorts") == 0)
533   {
534     if ((strcasecmp (value, "Yes") == 0)
535         || (strcasecmp (value, "True") == 0)
536         || (strcasecmp (value, "On") == 0))
537       port_collect_listening = 1;
538     else
539       port_collect_listening = 0;
540   }
541   else if ((strcasecmp (key, "LocalPort") == 0)
542       || (strcasecmp (key, "RemotePort") == 0))
543   {
544       port_entry_t *pe;
545       int port = atoi (value);
546
547       if ((port < 1) || (port > 65535))
548       {
549         ERROR ("tcpconns plugin: Invalid port: %i", port);
550         return (1);
551       }
552
553       pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
554       if (pe == NULL)
555       {
556         ERROR ("tcpconns plugin: conn_get_port_entry failed.");
557         return (1);
558       }
559
560       if (strcasecmp (key, "LocalPort") == 0)
561         pe->flags |= PORT_COLLECT_LOCAL;
562       else
563         pe->flags |= PORT_COLLECT_REMOTE;
564   }
565   else
566   {
567     return (-1);
568   }
569
570   return (0);
571 } /* int conn_config */
572
573 #if KERNEL_LINUX
574 static int conn_init (void)
575 {
576   if (port_list_head == NULL)
577     port_collect_listening = 1;
578
579   return (0);
580 } /* int conn_init */
581
582 static int conn_read (void)
583 {
584   int errors_num = 0;
585
586   conn_reset_port_entry ();
587
588   if (conn_read_file ("/proc/net/tcp") != 0)
589     errors_num++;
590   if (conn_read_file ("/proc/net/tcp6") != 0)
591     errors_num++;
592
593   if (errors_num < 2)
594   {
595     conn_submit_all ();
596   }
597   else
598   {
599     ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
600         "coult be read.");
601     return (-1);
602   }
603
604   return (0);
605 } /* int conn_read */
606 /* #endif KERNEL_LINUX */
607
608 #elif HAVE_SYSCTLBYNAME
609 static int conn_read (void)
610 {
611   int status;
612   char *buffer;
613   size_t buffer_len;;
614
615   struct xinpgen *in_orig;
616   struct xinpgen *in_ptr;
617
618   conn_reset_port_entry ();
619
620   buffer_len = 0;
621   status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
622   if (status < 0)
623   {
624     ERROR ("tcpconns plugin: sysctlbyname failed.");
625     return (-1);
626   }
627
628   buffer = (char *) malloc (buffer_len);
629   if (buffer == NULL)
630   {
631     ERROR ("tcpconns plugin: malloc failed.");
632     return (-1);
633   }
634
635   status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
636   if (status < 0)
637   {
638     ERROR ("tcpconns plugin: sysctlbyname failed.");
639     sfree (buffer);
640     return (-1);
641   }
642
643   if (buffer_len <= sizeof (struct xinpgen))
644   {
645     ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
646     sfree (buffer);
647     return (-1);
648   }
649
650   in_orig = (struct xinpgen *) buffer;
651   for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
652       in_ptr->xig_len > sizeof (struct xinpgen);
653       in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
654   {
655     struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
656     struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
657     struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
658
659     /* Ignore non-TCP sockets */
660     if (so->xso_protocol != IPPROTO_TCP)
661       continue;
662
663     /* Ignore PCBs which were freed during copyout. */
664     if (inp->inp_gencnt > in_orig->xig_gen)
665       continue;
666
667     if (((inp->inp_vflag & INP_IPV4) == 0)
668         && ((inp->inp_vflag & INP_IPV6) == 0))
669       continue;
670
671     conn_handle_ports (inp->inp_lport, inp->inp_fport, tp->t_state);
672   } /* for (in_ptr) */
673
674   in_orig = NULL;
675   in_ptr = NULL;
676   sfree (buffer);
677
678   conn_submit_all ();
679
680   return (0);
681 } /* int conn_read */
682 /* #endif HAVE_SYSCTLBYNAME */
683
684 #elif __OpenBSD__
685 static int kread(u_long addr, void *buf, int size)
686 {
687   if (kvm_read(kvmd, addr, buf, size) != size)
688   {
689     ERROR ("tcpconns plugin: %s\n", kvm_geterr(kvmd));
690     return (-1);
691   }
692   return (0);
693 }
694
695 static int conn_init (void)
696 {
697   char buf[_POSIX2_LINE_MAX];
698   if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL)
699   {
700     ERROR("tcpconns plugin: %s", buf);
701     return (-1);
702   }
703   if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0)
704   {
705     ERROR("tcpconns plugin: No namelist.");
706     return (-1);
707   }
708   return (0);
709 }
710
711 static int conn_read (void)
712 {
713   u_long off = nl[2].n_value;
714   struct inpcbtable table;
715   struct inpcb *head, *next, *prev;
716   struct inpcb inpcb;
717   struct tcpcb tcpcb;
718
719   conn_reset_port_entry ();
720
721   kread(off, &table, sizeof(table));
722   prev = head = (struct inpcb *)&CIRCLEQ_FIRST(&((struct inpcbtable *)off)->inpt_queue);
723   next = CIRCLEQ_FIRST(&table.inpt_queue);
724
725   while (next != head) {
726     kread((u_long)next, &inpcb, sizeof(inpcb));
727     prev = next;
728     next = CIRCLEQ_NEXT(&inpcb, inp_queue);
729     if (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
730       continue;
731     kread((u_long)inpcb.inp_ppcb, &tcpcb, sizeof(tcpcb));
732     conn_handle_ports (ntohs(inpcb.inp_lport), ntohs(inpcb.inp_fport), tcpcb.t_state);
733   }
734
735   conn_submit_all ();
736
737   return (0);
738 }
739 #endif /* __OpenBSD__ */
740
741 void module_register (void)
742 {
743         plugin_register_config ("tcpconns", conn_config,
744                         config_keys, config_keys_num);
745 #if KERNEL_LINUX
746         plugin_register_init ("tcpconns", conn_init);
747 #elif HAVE_SYSCTLBYNAME
748         /* no initialization */
749 #elif __OpenBSD__
750         plugin_register_init ("tcpconns", conn_init);
751 #endif
752         plugin_register_read ("tcpconns", conn_read);
753 } /* void module_register */
754
755 /*
756  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
757  */