2
KSysGuard, the KDE System Guard
4
Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of version 2 of the GNU General Public
8
License as published by the Free Software Foundation.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
#include <arpa/inet.h>
30
#include "ksysguardd.h"
35
static CONTAINER TcpSocketList = 0;
36
static CONTAINER UdpSocketList = 0;
37
static CONTAINER UnixSocketList = 0;
38
static CONTAINER RawSocketList = 0;
40
static int num_tcp = 0;
41
static int num_udp = 0;
42
static int num_unix = 0;
43
static int num_raw = 0;
48
char remote_addr[128];
49
char remote_port[128];
62
char *get_serv_name(int port, const char *proto);
63
char *get_host_name(int addr);
64
char *get_proto_name(int number);
65
int get_num_sockets(FILE *netstat);
66
void printSocketInfo(SocketInfo* socket_info);
68
static time_t TcpUdpRaw_timeStamp = 0;
69
static time_t Unix_timeStamp = 0;
70
static time_t NetStat_timeStamp = 0;
72
static const char *raw_type[] =
83
static const char *raw_state[] =
92
static const char *conn_state[] =
108
char *get_serv_name(int port, const char *proto)
110
static char buffer[1024];
111
struct servent *service;
117
memset(buffer, 0, sizeof(buffer));
119
if ((service = getservbyport(ntohs(port), proto)) == NULL) {
120
snprintf(buffer, sizeof(buffer), "%d", port);
122
strlcpy(buffer, service->s_name, sizeof(buffer));
125
return (char *)buffer;
128
char *get_host_name(int addr)
130
static char buffer[1024];
131
struct hostent *host;
132
struct in_addr a_addr;
138
memset(buffer, 0, sizeof(buffer));
140
if ((host = gethostbyaddr((char *)&addr, 4, AF_INET)) == NULL) {
141
a_addr.s_addr = addr;
142
return inet_ntoa(a_addr);
144
strlcpy(buffer, host->h_name, sizeof(buffer));
145
return (char *)buffer;
149
char *get_proto_name(int number)
151
static char buffer[1024];
152
struct protoent *protocol;
158
memset(buffer, 0, sizeof(buffer));
160
if ((protocol = getprotobynumber(number)) == NULL) {
161
snprintf(buffer, sizeof(buffer), "%d", number);
163
strlcpy(buffer, protocol->p_name, sizeof(buffer));
166
return (char *)buffer;
169
int get_num_sockets(FILE *netstat)
174
while (fgets(line, 1024, netstat) != NULL)
177
return line_count - 1;
180
void printSocketInfo(SocketInfo* socket_info)
182
fprintf(CurrentClient, "%s\t%s\t%s\t%s\t%s\t%d\n",
183
socket_info->local_addr,
184
socket_info->local_port,
185
socket_info->remote_addr,
186
socket_info->remote_port,
192
================================ public part =================================
196
initNetStat(struct SensorModul* sm)
200
if ((netstat = fopen("/proc/net/tcp", "r")) != NULL) {
201
registerMonitor("network/sockets/tcp/count", "integer", printNetStat, printNetStatInfo, sm);
202
registerMonitor("network/sockets/tcp/list", "listview", printNetStatTcpUdpRaw, printNetStatTcpUdpRawInfo, sm);
205
if ((netstat = fopen("/proc/net/udp", "r")) != NULL) {
206
registerMonitor("network/sockets/udp/count", "integer", printNetStat, printNetStatInfo, sm);
207
registerMonitor("network/sockets/udp/list", "listview", printNetStatTcpUdpRaw, printNetStatTcpUdpRawInfo, sm);
210
if ((netstat = fopen("/proc/net/unix", "r")) != NULL) {
211
registerMonitor("network/sockets/unix/count", "integer", printNetStat, printNetStatInfo, sm);
212
registerMonitor("network/sockets/unix/list", "listview", printNetStatUnix, printNetStatUnixInfo, sm);
215
if ((netstat = fopen("/proc/net/raw", "r")) != NULL) {
216
registerMonitor("network/sockets/raw/count", "integer", printNetStat, printNetStatInfo, sm);
217
registerMonitor("network/sockets/raw/list", "listview", printNetStatTcpUdpRaw, printNetStatTcpUdpRawInfo, sm);
221
TcpSocketList = new_ctnr();
222
UdpSocketList = new_ctnr();
223
RawSocketList = new_ctnr();
224
UnixSocketList = new_ctnr();
230
destr_ctnr(TcpSocketList, free);
231
destr_ctnr(UdpSocketList, free);
232
destr_ctnr(RawSocketList, free);
233
destr_ctnr(UnixSocketList, free);
241
if ((netstat = fopen("/proc/net/tcp", "r")) != NULL) {
242
num_tcp = get_num_sockets(netstat);
246
if ((netstat = fopen("/proc/net/udp", "r")) != NULL) {
247
num_udp = get_num_sockets(netstat);
251
if ((netstat = fopen("/proc/net/unix", "r")) != NULL) {
252
num_unix = get_num_sockets(netstat);
255
if ((netstat = fopen("/proc/net/raw", "r")) != NULL) {
256
num_raw = get_num_sockets(netstat);
260
NetStat_timeStamp = time(0);
265
updateNetStatTcpUdpRaw(const char *cmd)
269
uint local_addr, local_port;
270
uint remote_addr, remote_port;
273
SocketInfo *socket_info;
275
if (strstr(cmd, "tcp")) {
276
snprintf(buffer, sizeof(buffer), "/proc/net/tcp");
277
for (i = level_ctnr(TcpSocketList); i >= 0; --i)
278
free(pop_ctnr(TcpSocketList));
281
if (strstr(cmd, "udp")) {
282
snprintf(buffer, sizeof(buffer), "/proc/net/udp");
283
for (i = level_ctnr(UdpSocketList); i >= 0; --i)
284
free(pop_ctnr(UdpSocketList));
287
if (strstr(cmd, "raw")) {
288
snprintf(buffer, sizeof(buffer), "/proc/net/raw");
289
for (i = level_ctnr(RawSocketList); i >= 0; --i)
290
free(pop_ctnr(RawSocketList));
293
if ((netstat = fopen(buffer, "r")) == NULL) {
294
print_error("Cannot open \'%s\'!\n"
295
"The kernel needs to be compiled with support\n"
296
"for /proc filesystem enabled!\n", buffer);
300
fgets(buffer, sizeof(buffer), netstat);
302
while (fgets(buffer, sizeof(buffer), netstat) != NULL) {
303
if (strcmp(buffer, "")) {
304
sscanf(buffer, "%*d: %x:%x %x:%x %x %*x:%*x %*x:%*x %d",
305
&local_addr, &local_port,
306
&remote_addr, &remote_port,
310
if ((socket_info = (SocketInfo *)malloc(sizeof(SocketInfo))) == NULL) {
313
strlcpy(socket_info->local_addr, get_host_name(local_addr), sizeof(socket_info->local_addr));
314
strlcpy(socket_info->remote_addr, get_host_name(remote_addr), sizeof(socket_info->remote_addr));
316
if (strstr(cmd, "tcp")) {
317
strlcpy(socket_info->local_port, get_serv_name(local_port, "tcp"), sizeof(socket_info->local_port));
318
strlcpy(socket_info->remote_port, get_serv_name(remote_port, "tcp"), sizeof(socket_info->remote_port));
319
strlcpy(socket_info->state, conn_state[state], sizeof(socket_info->state));
320
socket_info->uid = uid;
322
push_ctnr(TcpSocketList, socket_info);
325
if (strstr(cmd, "udp")) {
326
strlcpy(socket_info->local_port, get_serv_name(local_port, "udp"), sizeof(socket_info->local_port));
327
strlcpy(socket_info->remote_port, get_serv_name(remote_port, "udp"), sizeof(socket_info->remote_port));
328
strlcpy(socket_info->state, conn_state[state], sizeof(socket_info->state));
329
socket_info->uid = uid;
331
push_ctnr(UdpSocketList, socket_info);
334
if (strstr(cmd, "raw")) {
335
strlcpy(socket_info->local_port, get_proto_name(local_port), sizeof(socket_info->local_port));
336
strlcpy(socket_info->remote_port, get_proto_name(remote_port), sizeof(socket_info->remote_port));
337
snprintf(socket_info->state, sizeof(socket_info->state)-1, "%d", state);
338
socket_info->uid = uid;
340
push_ctnr(RawSocketList, socket_info);
345
TcpUdpRaw_timeStamp = time(0);
351
updateNetStatUnix(void)
356
int ref_count, type, state, inode, i;
359
if ((file = fopen("/proc/net/unix", "r")) == NULL) {
360
print_error("Cannot open \'/proc/net/unix\'!\n"
361
"The kernel needs to be compiled with support\n"
362
"for /proc filesystem enabled!\n");
366
for (i = level_ctnr(UnixSocketList); i >= 0; --i)
367
free(pop_ctnr(UnixSocketList));
369
fgets(buffer, sizeof(buffer), file);
371
while (fgets(buffer, sizeof(buffer), file) != NULL) {
372
if (strcmp(buffer, "")) {
373
sscanf(buffer, "%*x: %d %*d %*d %d %d %d %255s",
374
&ref_count, &type, &state, &inode, path);
376
if ((unix_info = (UnixInfo *)malloc(sizeof(UnixInfo))) == NULL) {
380
unix_info->refcount = ref_count;
381
strlcpy(unix_info->type, raw_type[type], sizeof(unix_info->type));
382
strlcpy(unix_info->state, raw_state[state], sizeof(unix_info->state));
383
unix_info->inode = inode;
384
strlcpy(unix_info->path, path, sizeof(unix_info->path));
386
push_ctnr(UnixSocketList, unix_info);
390
Unix_timeStamp = time(0);
396
printNetStat(const char* cmd)
398
if ((time(0) - NetStat_timeStamp) >= UPDATEINTERVAL)
401
if (strstr(cmd, "tcp") != NULL)
402
fprintf(CurrentClient, "%d\n", num_tcp);
403
if (strstr(cmd, "udp") != NULL)
404
fprintf(CurrentClient, "%d\n", num_udp);
405
if (strstr(cmd, "unix") != NULL)
406
fprintf(CurrentClient, "%d\n", num_unix);
407
if (strstr(cmd, "raw") != NULL)
408
fprintf(CurrentClient, "%d\n", num_raw);
412
printNetStatInfo(const char* cmd)
414
if (strstr(cmd, "tcp") != NULL)
415
fprintf(CurrentClient, "Number of TCP-Sockets\t0\t0\tSockets\n");
416
if (strstr(cmd, "udp") != NULL)
417
fprintf(CurrentClient, "Number of UDP-Sockets\t0\t0\tSockets\n");
418
if (strstr(cmd, "unix") != NULL)
419
fprintf(CurrentClient, "Number of UnixDomain-Sockets\t0\t0\tSockets\n");
420
if (strstr(cmd, "raw") != NULL)
421
fprintf(CurrentClient, "Number of Raw-Sockets\t0\t0\tSockets\n");
425
printNetStatTcpUdpRaw(const char *cmd)
427
SocketInfo* socket_info;
429
if (strstr(cmd, "tcp")) {
430
if ((time(0) - TcpUdpRaw_timeStamp) >= UPDATEINTERVAL)
431
updateNetStatTcpUdpRaw("tcp");
433
for (socket_info = first_ctnr(TcpSocketList); socket_info; socket_info = next_ctnr(TcpSocketList))
434
printSocketInfo(socket_info);
436
if (level_ctnr(TcpSocketList) == 0)
437
fprintf(CurrentClient, "\n");
440
if (strstr(cmd, "udp")) {
441
if ((time(0) - TcpUdpRaw_timeStamp) >= UPDATEINTERVAL)
442
updateNetStatTcpUdpRaw("udp");
444
for (socket_info = first_ctnr(UdpSocketList); socket_info; socket_info = next_ctnr(UdpSocketList))
445
printSocketInfo(socket_info);
447
if (level_ctnr(UdpSocketList) == 0)
448
fprintf(CurrentClient, "\n");
451
if (strstr(cmd, "raw")) {
452
if ((time(0) - TcpUdpRaw_timeStamp) >= UPDATEINTERVAL)
453
updateNetStatTcpUdpRaw("raw");
455
for (socket_info = first_ctnr(RawSocketList); socket_info; socket_info = next_ctnr(RawSocketList))
456
printSocketInfo(socket_info);
458
if (level_ctnr(RawSocketList) == 0)
459
fprintf(CurrentClient, "\n");
464
printNetStatTcpUdpRawInfo(const char *cmd)
467
fprintf(CurrentClient, "Local Address\tPort\tForeign Address\tPort\tState\tUID\ns\ts\ts\ts\ts\td\n");
470
void printNetStatUnix(const char *cmd)
475
if ((time(0) - Unix_timeStamp) >= UPDATEINTERVAL)
478
for (unix_info = first_ctnr(UnixSocketList); unix_info; unix_info = next_ctnr(UnixSocketList)) {
479
fprintf(CurrentClient, "%d\t%s\t%s\t%d\t%s\n",
487
if (level_ctnr(UnixSocketList) == 0)
488
fprintf(CurrentClient, "\n");
491
void printNetStatUnixInfo(const char *cmd)
494
fprintf(CurrentClient, "RefCount\tType\tState\tInode\tPath\nd\ts\ts\td\ts\n");