4772c7c2cfa7da322f2c05df99483c3d74b93462
[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_RECV",
164   "ESTABLISHED",
165   "CLOSE_WAIT",
166   "FIN_WAIT1",
167   "CLOSING",
168   "LAST_ACK",
169   "FIN_WAIT2",
170   "TIME_WAIT"
171 };
172
173 static kvm_t *kvmd;
174 static u_long      inpcbtable_off = 0;
175 struct inpcbtable *inpcbtable_ptr = NULL;
176
177 #if 0
178 static struct nlist nl[] = { /* {{{ */
179 #define N_MBSTAT        0
180         { "_mbstat" },
181 #define N_IPSTAT        1
182         { "_ipstat" },
183 #define N_TCBTABLE      2
184         { "_tcbtable" },
185 #define N_TCPSTAT       3
186         { "_tcpstat" },
187 #define N_UDBTABLE      4
188         { "_udbtable" },
189 #define N_UDPSTAT       5
190         { "_udpstat" },
191 #define N_IFNET         6
192         { "_ifnet" },
193 #define N_ICMPSTAT      7
194         { "_icmpstat" },
195 #define N_RTSTAT        8
196         { "_rtstat" },
197 #define N_UNIXSW        9
198         { "_unixsw" },
199 #define N_RTREE         10
200         { "_rt_tables"},
201 #define N_FILE          11
202         { "_file" },
203 #define N_IGMPSTAT      12
204         { "_igmpstat" },
205 #define N_MRTPROTO      13
206         { "_ip_mrtproto" },
207 #define N_MRTSTAT       14
208         { "_mrtstat" },
209 #define N_MFCHASHTBL    15
210         { "_mfchashtbl" },
211 #define N_MFCHASH       16
212         { "_mfchash" },
213         { "_viftable" },
214 #define N_AHSTAT        18
215         { "_ahstat"},
216 #define N_ESPSTAT       19
217         { "_espstat"},
218 #define N_IP4STAT       20
219         { "_ipipstat"},
220 #define N_DDPSTAT       21
221         { "_ddpstat"},
222 #define N_DDPCB         22
223         { "_ddpcb"},
224 #define N_ETHERIPSTAT   23
225         { "_etheripstat"},
226 #define N_IP6STAT       24
227         { "_ip6stat" },
228 #define N_ICMP6STAT     25
229         { "_icmp6stat" },
230 #define N_PIM6STAT      26
231         { "_pim6stat" },
232 #define N_MRT6PROTO     27
233         { "_ip6_mrtproto" },
234 #define N_MRT6STAT      28
235         { "_mrt6stat" },
236 #define N_MF6CTABLE     29
237         { "_mf6ctable" },
238 #define N_MIF6TABLE     30
239         { "_mif6table" },
240 #define N_MBPOOL        31
241         { "_mbpool" },
242 #define N_MCLPOOL       32
243         { "_mclpool" },
244 #define N_IPCOMPSTAT    33
245         { "_ipcompstat" },
246 #define N_RIP6STAT      34
247         { "_rip6stat" },
248 #define N_CARPSTAT      35
249         { "_carpstats" },
250 #define N_RAWIPTABLE    36
251         { "_rawcbtable" },
252 #define N_RAWIP6TABLE   37
253         { "_rawin6pcbtable" },
254 #define N_PFSYNCSTAT    38
255         { "_pfsyncstats" },
256 #define N_PIMSTAT       39
257         { "_pimstat" },
258 #define N_AF2RTAFIDX    40
259         { "_af2rtafidx" },
260 #define N_RTBLIDMAX     41
261         { "_rtbl_id_max" },
262 #define N_RTMASK        42
263         { "_mask_rnhead" },
264
265         { "" }
266 }; /* }}} struct nlist nl[] */
267 #endif /* 0 */
268
269 # define TCP_STATE_LISTEN 1
270 # define TCP_STATE_MIN 1
271 # define TCP_STATE_MAX 10
272 #endif /* __OpenBSD__ */
273
274 #define PORT_COLLECT_LOCAL  0x01
275 #define PORT_COLLECT_REMOTE 0x02
276 #define PORT_IS_LISTENING   0x04
277
278 typedef struct port_entry_s
279 {
280   uint16_t port;
281   uint16_t flags;
282   uint32_t count_local[TCP_STATE_MAX + 1];
283   uint32_t count_remote[TCP_STATE_MAX + 1];
284   struct port_entry_s *next;
285 } port_entry_t;
286
287 static const char *config_keys[] =
288 {
289   "ListeningPorts",
290   "LocalPort",
291   "RemotePort"
292 };
293 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
294
295 static int port_collect_listening = 0;
296 static port_entry_t *port_list_head = NULL;
297
298 static void conn_submit_port_entry (port_entry_t *pe)
299 {
300   value_t values[1];
301   value_list_t vl = VALUE_LIST_INIT;
302   int i;
303
304   vl.values = values;
305   vl.values_len = 1;
306   vl.time = time (NULL);
307   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
308   sstrncpy (vl.plugin, "tcpconns", sizeof (vl.plugin));
309   sstrncpy (vl.type, "tcp_connections", sizeof (vl.type));
310
311   if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
312       || (pe->flags & PORT_COLLECT_LOCAL))
313   {
314     ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
315         "%hu-local", pe->port);
316
317     for (i = 1; i <= TCP_STATE_MAX; i++)
318     {
319       vl.values[0].gauge = pe->count_local[i];
320
321       sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
322
323       plugin_dispatch_values (&vl);
324     }
325   }
326
327   if (pe->flags & PORT_COLLECT_REMOTE)
328   {
329     ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
330         "%hu-remote", pe->port);
331
332     for (i = 1; i <= TCP_STATE_MAX; i++)
333     {
334       vl.values[0].gauge = pe->count_remote[i];
335
336       sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
337
338       plugin_dispatch_values (&vl);
339     }
340   }
341 } /* void conn_submit */
342
343 static void conn_submit_all (void)
344 {
345   port_entry_t *pe;
346
347   for (pe = port_list_head; pe != NULL; pe = pe->next)
348     conn_submit_port_entry (pe);
349 } /* void conn_submit_all */
350
351 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
352 {
353   port_entry_t *ret;
354
355   ret = port_list_head;
356   while (ret != NULL)
357   {
358     if (ret->port == port)
359       break;
360     ret = ret->next;
361   }
362
363   if ((ret == NULL) && (create != 0))
364   {
365     ret = (port_entry_t *) malloc (sizeof (port_entry_t));
366     if (ret == NULL)
367       return (NULL);
368     memset (ret, '\0', sizeof (port_entry_t));
369
370     ret->port = port;
371     ret->next = port_list_head;
372     port_list_head = ret;
373   }
374
375   return (ret);
376 } /* port_entry_t *conn_get_port_entry */
377
378 /* Removes ports that were added automatically due to the `ListeningPorts'
379  * setting but which are no longer listening. */
380 static void conn_reset_port_entry (void)
381 {
382   port_entry_t *prev = NULL;
383   port_entry_t *pe = port_list_head;
384
385   while (pe != NULL)
386   {
387     /* If this entry was created while reading the files (ant not when handling
388      * the configuration) remove it now. */
389     if ((pe->flags & (PORT_COLLECT_LOCAL
390             | PORT_COLLECT_REMOTE
391             | PORT_IS_LISTENING)) == 0)
392     {
393       port_entry_t *next = pe->next;
394
395       DEBUG ("tcpconns plugin: Removing temporary entry "
396           "for listening port %hu", pe->port);
397
398       if (prev == NULL)
399         port_list_head = next;
400       else
401         prev->next = next;
402
403       sfree (pe);
404       pe = next;
405
406       continue;
407     }
408
409     memset (pe->count_local, '\0', sizeof (pe->count_local));
410     memset (pe->count_remote, '\0', sizeof (pe->count_remote));
411     pe->flags &= ~PORT_IS_LISTENING;
412
413     pe = pe->next;
414   }
415 } /* void conn_reset_port_entry */
416
417 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
418 {
419   port_entry_t *pe = NULL;
420
421   if ((state > TCP_STATE_MAX)
422 #if TCP_STATE_MIN > 0
423       || (state < TCP_STATE_MIN)
424 #endif
425      )
426   {
427     NOTICE ("tcpconns plugin: Ignoring connection with unknown state 0x%02x.",
428         state);
429     return (-1);
430   }
431
432   /* Listening sockets */
433   if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
434   {
435     pe = conn_get_port_entry (port_local, 1 /* create */);
436     if (pe != NULL)
437       pe->flags |= PORT_IS_LISTENING;
438   }
439
440   DEBUG ("tcpconns plugin: Connection %hu <-> %hu (%s)",
441       port_local, port_remote, tcp_state[state]);
442
443   pe = conn_get_port_entry (port_local, 0 /* no create */);
444   if (pe != NULL)
445     pe->count_local[state]++;
446
447   pe = conn_get_port_entry (port_remote, 0 /* no create */);
448   if (pe != NULL)
449     pe->count_remote[state]++;
450
451   return (0);
452 } /* int conn_handle_ports */
453
454 #if KERNEL_LINUX
455 static int conn_handle_line (char *buffer)
456 {
457   char *fields[32];
458   int   fields_len;
459
460   char *endptr;
461
462   char *port_local_str;
463   char *port_remote_str;
464   uint16_t port_local;
465   uint16_t port_remote;
466
467   uint8_t state;
468
469   int buffer_len = strlen (buffer);
470
471   while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
472     buffer[--buffer_len] = '\0';
473   if (buffer_len <= 0)
474     return (-1);
475
476   fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
477   if (fields_len < 12)
478   {
479     DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
480     return (-1);
481   }
482
483   port_local_str  = strchr (fields[1], ':');
484   port_remote_str = strchr (fields[2], ':');
485
486   if ((port_local_str == NULL) || (port_remote_str == NULL))
487     return (-1);
488   port_local_str++;
489   port_remote_str++;
490   if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
491     return (-1);
492
493   endptr = NULL;
494   port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
495   if ((endptr == NULL) || (*endptr != '\0'))
496     return (-1);
497
498   endptr = NULL;
499   port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
500   if ((endptr == NULL) || (*endptr != '\0'))
501     return (-1);
502
503   endptr = NULL;
504   state = (uint8_t) strtol (fields[3], &endptr, 16);
505   if ((endptr == NULL) || (*endptr != '\0'))
506     return (-1);
507
508   return (conn_handle_ports (port_local, port_remote, state));
509 } /* int conn_handle_line */
510
511 static int conn_read_file (const char *file)
512 {
513   FILE *fh;
514   char buffer[1024];
515
516   fh = fopen (file, "r");
517   if (fh == NULL)
518     return (-1);
519
520   while (fgets (buffer, sizeof (buffer), fh) != NULL)
521   {
522     conn_handle_line (buffer);
523   } /* while (fgets) */
524
525   fclose (fh);
526
527   return (0);
528 } /* int conn_read_file */
529 /* #endif KERNEL_LINUX */
530
531 #elif HAVE_SYSCTLBYNAME
532 /* #endif HAVE_SYSCTLBYNAME */
533
534 #elif __OpenBSD__
535 #endif /* __OpenBSD__ */
536
537 static int conn_config (const char *key, const char *value)
538 {
539   if (strcasecmp (key, "ListeningPorts") == 0)
540   {
541     if ((strcasecmp (value, "Yes") == 0)
542         || (strcasecmp (value, "True") == 0)
543         || (strcasecmp (value, "On") == 0))
544       port_collect_listening = 1;
545     else
546       port_collect_listening = 0;
547   }
548   else if ((strcasecmp (key, "LocalPort") == 0)
549       || (strcasecmp (key, "RemotePort") == 0))
550   {
551       port_entry_t *pe;
552       int port = atoi (value);
553
554       if ((port < 1) || (port > 65535))
555       {
556         ERROR ("tcpconns plugin: Invalid port: %i", port);
557         return (1);
558       }
559
560       pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
561       if (pe == NULL)
562       {
563         ERROR ("tcpconns plugin: conn_get_port_entry failed.");
564         return (1);
565       }
566
567       if (strcasecmp (key, "LocalPort") == 0)
568         pe->flags |= PORT_COLLECT_LOCAL;
569       else
570         pe->flags |= PORT_COLLECT_REMOTE;
571   }
572   else
573   {
574     return (-1);
575   }
576
577   return (0);
578 } /* int conn_config */
579
580 #if KERNEL_LINUX
581 static int conn_init (void)
582 {
583   if (port_list_head == NULL)
584     port_collect_listening = 1;
585
586   return (0);
587 } /* int conn_init */
588
589 static int conn_read (void)
590 {
591   int errors_num = 0;
592
593   conn_reset_port_entry ();
594
595   if (conn_read_file ("/proc/net/tcp") != 0)
596     errors_num++;
597   if (conn_read_file ("/proc/net/tcp6") != 0)
598     errors_num++;
599
600   if (errors_num < 2)
601   {
602     conn_submit_all ();
603   }
604   else
605   {
606     ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
607         "coult be read.");
608     return (-1);
609   }
610
611   return (0);
612 } /* int conn_read */
613 /* #endif KERNEL_LINUX */
614
615 #elif HAVE_SYSCTLBYNAME
616 static int conn_read (void)
617 {
618   int status;
619   char *buffer;
620   size_t buffer_len;;
621
622   struct xinpgen *in_orig;
623   struct xinpgen *in_ptr;
624
625   conn_reset_port_entry ();
626
627   buffer_len = 0;
628   status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
629   if (status < 0)
630   {
631     ERROR ("tcpconns plugin: sysctlbyname failed.");
632     return (-1);
633   }
634
635   buffer = (char *) malloc (buffer_len);
636   if (buffer == NULL)
637   {
638     ERROR ("tcpconns plugin: malloc failed.");
639     return (-1);
640   }
641
642   status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
643   if (status < 0)
644   {
645     ERROR ("tcpconns plugin: sysctlbyname failed.");
646     sfree (buffer);
647     return (-1);
648   }
649
650   if (buffer_len <= sizeof (struct xinpgen))
651   {
652     ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
653     sfree (buffer);
654     return (-1);
655   }
656
657   in_orig = (struct xinpgen *) buffer;
658   for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
659       in_ptr->xig_len > sizeof (struct xinpgen);
660       in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
661   {
662     struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
663     struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
664     struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
665
666     /* Ignore non-TCP sockets */
667     if (so->xso_protocol != IPPROTO_TCP)
668       continue;
669
670     /* Ignore PCBs which were freed during copyout. */
671     if (inp->inp_gencnt > in_orig->xig_gen)
672       continue;
673
674     if (((inp->inp_vflag & INP_IPV4) == 0)
675         && ((inp->inp_vflag & INP_IPV6) == 0))
676       continue;
677
678     conn_handle_ports (inp->inp_lport, inp->inp_fport, tp->t_state);
679   } /* for (in_ptr) */
680
681   in_orig = NULL;
682   in_ptr = NULL;
683   sfree (buffer);
684
685   conn_submit_all ();
686
687   return (0);
688 } /* int conn_read */
689 /* #endif HAVE_SYSCTLBYNAME */
690
691 #elif __OpenBSD__
692 static int kread (u_long addr, void *buf, int size)
693 {
694   int status;
695
696   status = kvm_read (kvmd, addr, buf, size);
697   if (status != size)
698   {
699     ERROR ("tcpconns plugin: kvm_read failed (got %i, expected %i): %s\n",
700         status, size, kvm_geterr (kvmd));
701     return (-1);
702   }
703   return (0);
704 } /* int kread */
705
706 static int conn_init (void)
707 {
708   char buf[_POSIX2_LINE_MAX];
709   struct nlist nl[] =
710   {
711 #define N_TCBTABLE 0
712     { "_tcbtable" },
713     { "" }
714   };
715   int status;
716
717   kvmd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, buf);
718   if (kvmd == NULL)
719   {
720     ERROR ("tcpconns plugin: kvm_openfiles failed: %s", buf);
721     return (-1);
722   }
723
724   status = kvm_nlist (kvmd, nl);
725   if (status < 0)
726   {
727     ERROR ("tcpconns plugin: kvm_nlist failed with status %i.", status);
728     return (-1);
729   }
730
731   if (nl[N_TCBTABLE].n_type == 0)
732   {
733     ERROR ("tcpconns plugin: Error looking up kernel's namelist: "
734         "N_TCBTABLE is invalid.");
735     return (-1);
736   }
737
738   inpcbtable_off = (u_long) nl[N_TCBTABLE].n_value;
739   inpcbtable_ptr = (struct inpcbtable *) nl[N_TCBTABLE].n_value;
740
741   return (0);
742 } /* int conn_init */
743
744 static int conn_read (void)
745 {
746   u_long off = nl[2].n_value;
747   struct inpcbtable table;
748   struct inpcb *head;
749   struct inpcb *next;
750   struct inpcb inpcb;
751   struct tcpcb tcpcb;
752   int status;
753
754   conn_reset_port_entry ();
755
756   /* Read the pcbtable from the kernel */
757   status = kread (inpcbtable_off, &table, sizeof (table));
758   if (status != 0)
759     return (-1);
760
761   /* Get the `head' pcb */
762   head = (struct inpcb *) &(inpcbtable_ptr->inpt_queue);
763   /* Get the first pcb */
764   next = CIRCLEQ_FIRST (&table.inpt_queue);
765
766   while (next != head)
767   {
768     /* Read the pcb pointed to by `next' into `inpcb' */
769     kread ((u_long) next, &inpcb, sizeof (inpcb));
770
771     /* Advance `next' */
772     next = CIRCLEQ_NEXT (&inpcb, inp_queue);
773
774     /* Ignore sockets, that are not connected. */
775     if (!(inpcb.inp_flags & INP_IPV6)
776         && (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY))
777       continue;
778     if ((inpcb.inp_flags & INP_IPV6)
779         && IN6_IS_ADDR_UNSPECIFIED (&inpcb.inp_laddr6))
780       continue;
781
782     kread ((u_long) inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
783     conn_handle_ports (ntohs(inpcb.inp_lport), ntohs(inpcb.inp_fport), tcpcb.t_state);
784   } /* while (next != head) */
785
786   conn_submit_all ();
787
788   return (0);
789 }
790 #endif /* __OpenBSD__ */
791
792 void module_register (void)
793 {
794         plugin_register_config ("tcpconns", conn_config,
795                         config_keys, config_keys_num);
796 #if KERNEL_LINUX
797         plugin_register_init ("tcpconns", conn_init);
798 #elif HAVE_SYSCTLBYNAME
799         /* no initialization */
800 #elif __OpenBSD__
801         plugin_register_init ("tcpconns", conn_init);
802 #endif
803         plugin_register_read ("tcpconns", conn_read);
804 } /* void module_register */
805
806 /*
807  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker :
808  */