Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/tcpinfo.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014 Davide Madrisan <[email protected]>
5 *
6 * A library for checking TCP network and socket informations
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE /* activate extra prototypes for glibc */
23 #endif
24
25 #include <sys/types.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <arpa/inet.h>
31 #if HAVE_NETINET_IN_H
32 # include <netinet/in.h>
33 #endif
34
35 #include "common.h"
36 #include "messages.h"
37 #include "system.h"
38
39 #define PROC_TCPINFO "/proc/net/tcp"
40 #define PROC_TCP6INFO "/proc/net/tcp6"
41
42 #define TCP_UNSET 0
43 #define TCP_VERBOSE (1 << 1)
44 #define TCP_v4 (1 << 2)
45 #define TCP_v6 (1 << 3)
46
47 typedef enum tcp_status
48 {
49 TCP_ESTABLISHED = 1,
50 TCP_SYN_SENT,
51 TCP_SYN_RECV,
52 TCP_FIN_WAIT1,
53 TCP_FIN_WAIT2,
54 TCP_TIME_WAIT,
55 TCP_CLOSE,
56 TCP_CLOSE_WAIT,
57 TCP_LAST_ACK,
58 TCP_LISTEN,
59 TCP_CLOSING /* now a valid state */
60 } tcp_status;
61
62 static const char *tcp_state[] =
63 {
64 "",
65 "ESTABLISHED",
66 "SYN_SENT",
67 "SYN_RECV",
68 "FIN_WAIT1",
69 "FIN_WAIT2",
70 "TIME_WAIT",
71 "CLOSE",
72 "CLOSE_WAIT",
73 "LAST_ACK",
74 "LISTEN",
75 "CLOSING"
76 };
77
78 typedef struct proc_tcptable_data
79 {
80 unsigned long tcp_established;
81 unsigned long tcp_syn_sent;
82 unsigned long tcp_syn_recv;
83 unsigned long tcp_fin_wait1;
84 unsigned long tcp_fin_wait2;
85 unsigned long tcp_time_wait;
86 unsigned long tcp_close;
87 unsigned long tcp_close_wait;
88 unsigned long tcp_last_ack;
89 unsigned long tcp_listen;
90 unsigned long tcp_closing;
91 } proc_tcptable_data_t;
92
93 typedef struct proc_tcptable
94 {
95 int refcount;
96 struct proc_tcptable_data *data;
97 } proc_tcptable_t;
98
99
100 /* Parses /proc/net/tcp and /proc/net/tcp6 */
101
102 static void
103 procparser_tcp (const char *procfile, struct proc_tcptable_data *data,
104 bool verbose)
105 {
106 FILE *fp;
107 char *line = NULL;
108 size_t len = 0;
109 ssize_t nread;
110
111 char local_addr_buf[128], rem_addr_buf[128];
112 unsigned int slot, num, local_port, rem_port;
113 unsigned int state, timer_run, uid, timeout;
114 unsigned long lnr = 0;
115 unsigned long rxq, txq, time_len, retr, inode;
116 #if HAVE_AFINET6
117 char lbuffer[INET6_ADDRSTRLEN], rbuffer[INET6_ADDRSTRLEN];
118 struct in6_addr in6;
119 #else
120 char lbuffer[INET_ADDRSTRLEN], rbuffer[INET_ADDRSTRLEN];
121 #endif
122 struct sockaddr_in in;
123
124 if ((fp = fopen (procfile, "r")) == NULL)
125 plugin_error (STATE_UNKNOWN, errno, "error opening %s", procfile);
126
127 /* sl local_addr:local_port rem_addr:rem_port st ... */
128 while ((nread = getline (&line, &len, fp)) != -1)
129 {
130 if (++lnr == 1) /* Skip the heading line */
131 {
132 if (verbose)
133 printf ("[%s]\nproto %-11s %20s %22s\n", procfile,
134 "status", "local-addr:port", "remote-addr:port");
135 continue;
136 }
137
138 num =
139 sscanf (line,
140 "%u: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X "
141 "%lX:%lX %X:%lX %lX %u %u %lu %*s\n",
142 &slot, local_addr_buf, &local_port, rem_addr_buf, &rem_port,
143 &state, &txq, &rxq, &timer_run, &time_len, &retr, &uid,
144 &timeout, &inode);
145 if (num < 11)
146 fprintf (stderr, "warning, got bogus tcp line.\n%s", line);
147
148 switch (state)
149 {
150 case TCP_ESTABLISHED:
151 data -> tcp_established++;
152 break;
153 case TCP_SYN_SENT:
154 data -> tcp_syn_sent++;
155 break;
156 case TCP_SYN_RECV:
157 data -> tcp_syn_recv++;
158 break;
159 case TCP_FIN_WAIT1:
160 data -> tcp_fin_wait1++;
161 break;
162 case TCP_FIN_WAIT2:
163 data -> tcp_fin_wait2++;
164 break;
165 case TCP_TIME_WAIT:
166 data -> tcp_time_wait++;
167 break;
168 case TCP_CLOSE:
169 data -> tcp_close++;
170 break;
171 case TCP_CLOSE_WAIT:
172 data -> tcp_close_wait++;
173 break;
174 case TCP_LAST_ACK:
175 data -> tcp_last_ack++;
176 break;
177 case TCP_LISTEN:
178 data -> tcp_listen++;
179 break;
180 case TCP_CLOSING:
181 data -> tcp_closing++;
182 break;
183 }
184
185 if (verbose == false)
186 continue;
187
188 /* Demangle what the kernel gives us */
189 if (strlen (local_addr_buf) > 8)
190 {
191 #if HAVE_AFINET6
192 sscanf (local_addr_buf, "%08X%08X%08X%08X",
193 &in6.s6_addr32[0], &in6.s6_addr32[1],
194 &in6.s6_addr32[2], &in6.s6_addr32[3]);
195 /*in6.sin6_family = AF_INET6;*/
196 inet_ntop (AF_INET6, &in6, lbuffer, sizeof (lbuffer));
197 sscanf (rem_addr_buf, "%08X%08X%08X%08X",
198 &in6.s6_addr32[0], &in6.s6_addr32[1],
199 &in6.s6_addr32[2], &in6.s6_addr32[3]);
200 inet_ntop (AF_INET6, &in6, rbuffer, sizeof (rbuffer));
201
202 printf(" tcp6 %-11s %15s:%-6u %15s:%-6u\n", tcp_state[state],
203 lbuffer, local_port, rbuffer, rem_port);
204 #endif
205 }
206 else
207 {
208 sscanf (local_addr_buf, "%X", &in.sin_addr.s_addr);
209 /*in.sa_family = AF_INET;*/
210 inet_ntop (AF_INET, &in.sin_addr, lbuffer, sizeof (lbuffer));
211 sscanf (rem_addr_buf, "%X", &in.sin_addr.s_addr);
212 inet_ntop (AF_INET, &in.sin_addr, rbuffer, sizeof (rbuffer));
213
214 printf(" tcp %-11s %15s:%-6u %15s:%-6u\n", tcp_state[state],
215 lbuffer, local_port, rbuffer, rem_port);
216 }
217 }
218
219 free (line);
220 }
221
222 /* Allocates space for a new tcptable object.
223 * Returns 0 if all went ok. Errors are returned as negative values. */
224
225 int
226 proc_tcptable_new (struct proc_tcptable **tcptable)
227 {
228 struct proc_tcptable *t;
229
230 t = calloc (1, sizeof (struct proc_tcptable));
231 if (!t)
232 return -ENOMEM;
233
234 t->refcount = 1;
235 t->data = calloc (1, sizeof (struct proc_tcptable_data));
236 if (!t->data)
237 {
238 free (t);
239 return -ENOMEM;
240 }
241
242 *tcptable = t;
243 return 0;
244 }
245
246 /* Fill the proc_tcptable structure pointed will the values found in the
247 * proc filesystem. */
248
249 void
250 proc_tcptable_read (struct proc_tcptable *tcptable, int flags)
251 {
252 if (tcptable == NULL)
253 return;
254
255 bool verbose = (flags & TCP_VERBOSE) ? true : false;
256 struct proc_tcptable_data *data = tcptable->data;
257
258 if (flags & TCP_v4)
259 procparser_tcp (PROC_TCPINFO, data, verbose);
260
261 if (flags & TCP_v6)
262 procparser_tcp (PROC_TCP6INFO, data, verbose);
263 }
264
265 struct proc_tcptable *
266 proc_tcptable_unref (struct proc_tcptable *tcptable)
267 {
268 if (tcptable == NULL)
269 return NULL;
270
271 tcptable->refcount--;
272 if (tcptable->refcount > 0)
273 return tcptable;
274
275 free (tcptable->data);
276 free (tcptable);
277 return NULL;
278 }
279
280 #define proc_tcp_get(arg) \
281 unsigned long proc_tcp_get_tcp_ ## arg (struct proc_tcptable *p) \
282 { return (p == NULL) ? 0 : p->data->tcp_ ## arg; }
283
284 proc_tcp_get (established)
285 proc_tcp_get (syn_sent)
286 proc_tcp_get (syn_recv)
287 proc_tcp_get (fin_wait1)
288 proc_tcp_get (fin_wait2)
289 proc_tcp_get (time_wait)
290 proc_tcp_get (close)
291 proc_tcp_get (close_wait)
292 proc_tcp_get (last_ack)
293 proc_tcp_get (listen)
294 proc_tcp_get (closing)