~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to ksysguard/ksysguardd/Linux/netstat.c

  • Committer: Bazaar Package Importer
  • Author(s): Ana Beatriz Guerrero Lopez
  • Date: 2009-04-05 05:22:13 UTC
  • mfrom: (0.4.2 experimental) (0.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 235.
  • Revision ID: james.westby@ubuntu.com-20090405052213-39thr4l6p2ss07uj
Tags: 4:4.2.2-1
* New upstream release:
  - khtml fixes. (Closes: #290285, #359680)
  - Default konsole sessions can be deleted. (Closes: #286342)
  - Tag widget uses standard application palette. (Closes: #444800)
  - ... and surely many more but we have lost track...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    KSysGuard, the KDE System Guard
3
 
   
4
 
        Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
5
 
    
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.
9
 
 
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.
14
 
 
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.
18
 
 
19
 
*/
20
 
 
21
 
#include <config.h>
22
 
 
23
 
#include <arpa/inet.h>
24
 
#include <netdb.h>
25
 
#include <stdio.h>
26
 
#include <stdlib.h>
27
 
#include <string.h>
28
 
#include <time.h>
29
 
 
30
 
#include "ksysguardd.h"
31
 
#include "Command.h"
32
 
#include "ccont.h"
33
 
#include "netstat.h"
34
 
 
35
 
static CONTAINER TcpSocketList = 0;
36
 
static CONTAINER UdpSocketList = 0;
37
 
static CONTAINER UnixSocketList = 0;
38
 
static CONTAINER RawSocketList = 0;
39
 
 
40
 
static int num_tcp = 0;
41
 
static int num_udp = 0;
42
 
static int num_unix = 0;
43
 
static int num_raw = 0;
44
 
 
45
 
typedef struct {
46
 
        char local_addr[128];
47
 
        char local_port[128];
48
 
        char remote_addr[128];
49
 
        char remote_port[128];
50
 
        char state[128];
51
 
        int uid;
52
 
} SocketInfo;
53
 
 
54
 
typedef struct {
55
 
        int refcount;
56
 
        char type[128];
57
 
        char state[128];
58
 
        int inode;
59
 
        char path[256];
60
 
} UnixInfo;
61
 
 
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);
67
 
 
68
 
static time_t TcpUdpRaw_timeStamp = 0;
69
 
static time_t Unix_timeStamp = 0;
70
 
static time_t NetStat_timeStamp = 0;
71
 
 
72
 
static const char *raw_type[] =
73
 
{
74
 
        "",
75
 
        "stream",
76
 
        "dgram",
77
 
        "raw",
78
 
        "rdm",
79
 
        "seqpacket",
80
 
        "packet"
81
 
};
82
 
 
83
 
static const char *raw_state[] =
84
 
{
85
 
        "free",
86
 
        "unconnected",
87
 
        "connecting",
88
 
        "connected",
89
 
        "disconnecting"
90
 
};
91
 
 
92
 
static const char *conn_state[] =
93
 
{
94
 
        "",
95
 
        "established",
96
 
        "syn_sent",
97
 
        "syn_recv",
98
 
        "fin_wait1",
99
 
        "fin_wait2",
100
 
        "time_wait",
101
 
        "close",
102
 
        "close_wait",
103
 
        "last_ack",
104
 
        "listen",
105
 
        "closing"
106
 
};
107
 
 
108
 
char *get_serv_name(int port, const char *proto)
109
 
{
110
 
        static char buffer[1024];
111
 
        struct servent *service;
112
 
 
113
 
        if (port == 0) {
114
 
                return (char *)"*";
115
 
        }
116
 
 
117
 
        memset(buffer, 0, sizeof(buffer));
118
 
        
119
 
        if ((service = getservbyport(ntohs(port), proto)) == NULL) {
120
 
                snprintf(buffer, sizeof(buffer), "%d", port);
121
 
        } else {
122
 
                strlcpy(buffer, service->s_name, sizeof(buffer));
123
 
        }
124
 
 
125
 
        return (char *)buffer;
126
 
}
127
 
 
128
 
char *get_host_name(int addr)
129
 
{
130
 
        static char buffer[1024];
131
 
        struct hostent *host;
132
 
        struct in_addr a_addr;
133
 
 
134
 
        if (addr == 0) {
135
 
                return (char *)"*";
136
 
        }
137
 
 
138
 
        memset(buffer, 0, sizeof(buffer));
139
 
 
140
 
        if ((host = gethostbyaddr((char *)&addr, 4, AF_INET)) == NULL) {
141
 
                a_addr.s_addr = addr;
142
 
                return inet_ntoa(a_addr);
143
 
        } else {
144
 
                strlcpy(buffer, host->h_name, sizeof(buffer));
145
 
                return (char *)buffer;
146
 
        }
147
 
}
148
 
 
149
 
char *get_proto_name(int number)
150
 
{
151
 
        static char buffer[1024];
152
 
        struct protoent *protocol;
153
 
 
154
 
        if (number == 0) {
155
 
                return (char *)"*";
156
 
        }
157
 
 
158
 
        memset(buffer, 0, sizeof(buffer));
159
 
 
160
 
        if ((protocol = getprotobynumber(number)) == NULL) {
161
 
                snprintf(buffer, sizeof(buffer), "%d", number);
162
 
        } else {
163
 
                strlcpy(buffer, protocol->p_name, sizeof(buffer));
164
 
        }
165
 
 
166
 
        return (char *)buffer;
167
 
}
168
 
 
169
 
int get_num_sockets(FILE *netstat)
170
 
{
171
 
        char line[1024];
172
 
        int line_count = 0;
173
 
        
174
 
        while (fgets(line, 1024, netstat) != NULL)
175
 
                line_count++;
176
 
 
177
 
        return line_count - 1;
178
 
}
179
 
 
180
 
void printSocketInfo(SocketInfo* socket_info)
181
 
{
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,
187
 
                socket_info->state,
188
 
                socket_info->uid);
189
 
}
190
 
 
191
 
/*
192
 
================================ public part =================================
193
 
*/
194
 
 
195
 
void
196
 
initNetStat(struct SensorModul* sm)
197
 
{
198
 
        FILE *netstat;
199
 
        
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);
203
 
                fclose(netstat);
204
 
        }
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);
208
 
                fclose(netstat);
209
 
        }
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);
213
 
                fclose(netstat);
214
 
        }
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);
218
 
                fclose(netstat);
219
 
        }
220
 
 
221
 
        TcpSocketList = new_ctnr();
222
 
        UdpSocketList = new_ctnr();
223
 
        RawSocketList = new_ctnr();
224
 
        UnixSocketList = new_ctnr();
225
 
}
226
 
 
227
 
void
228
 
exitNetStat(void)
229
 
{
230
 
        destr_ctnr(TcpSocketList, free);
231
 
        destr_ctnr(UdpSocketList, free);
232
 
        destr_ctnr(RawSocketList, free);
233
 
        destr_ctnr(UnixSocketList, free);
234
 
}
235
 
 
236
 
int
237
 
updateNetStat(void)
238
 
{
239
 
        FILE *netstat;
240
 
 
241
 
        if ((netstat = fopen("/proc/net/tcp", "r")) != NULL) {
242
 
                num_tcp = get_num_sockets(netstat);
243
 
                fclose(netstat);
244
 
        }
245
 
 
246
 
        if ((netstat = fopen("/proc/net/udp", "r")) != NULL) {
247
 
                num_udp = get_num_sockets(netstat);
248
 
                fclose(netstat);
249
 
        }
250
 
 
251
 
        if ((netstat = fopen("/proc/net/unix", "r")) != NULL) {
252
 
                num_unix = get_num_sockets(netstat);
253
 
                fclose(netstat);
254
 
        }
255
 
        if ((netstat = fopen("/proc/net/raw", "r")) != NULL) {
256
 
                num_raw = get_num_sockets(netstat);
257
 
                fclose(netstat);
258
 
        }
259
 
 
260
 
        NetStat_timeStamp = time(0);
261
 
        return 0;
262
 
}
263
 
 
264
 
int
265
 
updateNetStatTcpUdpRaw(const char *cmd)
266
 
{
267
 
        FILE *netstat;
268
 
        char buffer[1024];
269
 
        uint local_addr, local_port;
270
 
        uint remote_addr, remote_port;
271
 
        int uid, i;
272
 
        uint state;
273
 
        SocketInfo *socket_info;
274
 
 
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));
279
 
        }
280
 
 
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));
285
 
        }
286
 
 
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));
291
 
        }
292
 
 
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);
297
 
                return -1;
298
 
        }
299
 
 
300
 
        fgets(buffer, sizeof(buffer), netstat);
301
 
 
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,
307
 
                        &state,
308
 
                        &uid);
309
 
 
310
 
                        if ((socket_info = (SocketInfo *)malloc(sizeof(SocketInfo))) == NULL) {
311
 
                                continue;
312
 
                        }
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));
315
 
 
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;
321
 
 
322
 
                                push_ctnr(TcpSocketList, socket_info);
323
 
                        }
324
 
 
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;
330
 
 
331
 
                                push_ctnr(UdpSocketList, socket_info);
332
 
                        }
333
 
 
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;
339
 
 
340
 
                                push_ctnr(RawSocketList, socket_info);
341
 
                        }
342
 
                }
343
 
        }
344
 
        fclose(netstat);
345
 
        TcpUdpRaw_timeStamp = time(0);
346
 
 
347
 
        return 0;
348
 
}
349
 
 
350
 
int
351
 
updateNetStatUnix(void)
352
 
{
353
 
        FILE *file;
354
 
        char buffer[1024];
355
 
        char path[256];
356
 
        int ref_count, type, state, inode, i;
357
 
        UnixInfo *unix_info;
358
 
 
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");
363
 
                return -1;
364
 
        }
365
 
 
366
 
        for (i = level_ctnr(UnixSocketList); i >= 0; --i)
367
 
                free(pop_ctnr(UnixSocketList));
368
 
 
369
 
        fgets(buffer, sizeof(buffer), file);
370
 
 
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);
375
 
 
376
 
                        if ((unix_info = (UnixInfo *)malloc(sizeof(UnixInfo))) == NULL) {
377
 
                                continue;
378
 
                        }
379
 
 
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));
385
 
 
386
 
                        push_ctnr(UnixSocketList, unix_info);
387
 
                }
388
 
        }
389
 
        fclose(file);
390
 
        Unix_timeStamp = time(0);
391
 
 
392
 
        return 0;
393
 
}
394
 
 
395
 
void
396
 
printNetStat(const char* cmd)
397
 
{
398
 
        if ((time(0) - NetStat_timeStamp) >= UPDATEINTERVAL)
399
 
                updateNetStat();
400
 
 
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);
409
 
}
410
 
 
411
 
void
412
 
printNetStatInfo(const char* cmd)
413
 
{
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");
422
 
}
423
 
 
424
 
void
425
 
printNetStatTcpUdpRaw(const char *cmd)
426
 
{
427
 
        SocketInfo* socket_info;
428
 
 
429
 
        if (strstr(cmd, "tcp")) {
430
 
                if ((time(0) - TcpUdpRaw_timeStamp) >= UPDATEINTERVAL)
431
 
                        updateNetStatTcpUdpRaw("tcp");
432
 
 
433
 
                for (socket_info = first_ctnr(TcpSocketList); socket_info; socket_info = next_ctnr(TcpSocketList))
434
 
                        printSocketInfo(socket_info);
435
 
 
436
 
                if (level_ctnr(TcpSocketList) == 0)
437
 
                        fprintf(CurrentClient, "\n");
438
 
        }
439
 
 
440
 
        if (strstr(cmd, "udp")) {
441
 
                if ((time(0) - TcpUdpRaw_timeStamp) >= UPDATEINTERVAL)
442
 
                        updateNetStatTcpUdpRaw("udp");
443
 
 
444
 
                for (socket_info = first_ctnr(UdpSocketList); socket_info; socket_info = next_ctnr(UdpSocketList))
445
 
                        printSocketInfo(socket_info);
446
 
 
447
 
                if (level_ctnr(UdpSocketList) == 0)
448
 
                        fprintf(CurrentClient, "\n");
449
 
        }
450
 
 
451
 
        if (strstr(cmd, "raw")) {
452
 
                if ((time(0) - TcpUdpRaw_timeStamp) >= UPDATEINTERVAL)
453
 
                        updateNetStatTcpUdpRaw("raw");
454
 
 
455
 
                for (socket_info = first_ctnr(RawSocketList); socket_info; socket_info = next_ctnr(RawSocketList))
456
 
                        printSocketInfo(socket_info);
457
 
 
458
 
                if (level_ctnr(RawSocketList) == 0)
459
 
                        fprintf(CurrentClient, "\n");
460
 
        }
461
 
}
462
 
 
463
 
void
464
 
printNetStatTcpUdpRawInfo(const char *cmd)
465
 
{
466
 
        (void) cmd;
467
 
        fprintf(CurrentClient, "Local Address\tPort\tForeign Address\tPort\tState\tUID\ns\ts\ts\ts\ts\td\n");
468
 
}
469
 
 
470
 
void printNetStatUnix(const char *cmd)
471
 
{
472
 
        UnixInfo* unix_info;
473
 
 
474
 
        (void) cmd;
475
 
        if ((time(0) - Unix_timeStamp) >= UPDATEINTERVAL)
476
 
                updateNetStatUnix();
477
 
        
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",
480
 
                        unix_info->refcount,
481
 
                        unix_info->type,
482
 
                        unix_info->state,
483
 
                        unix_info->inode,
484
 
                        unix_info->path);
485
 
        }
486
 
 
487
 
        if (level_ctnr(UnixSocketList) == 0)
488
 
                fprintf(CurrentClient, "\n");
489
 
}
490
 
 
491
 
void printNetStatUnixInfo(const char *cmd)
492
 
{
493
 
        (void) cmd;
494
 
        fprintf(CurrentClient, "RefCount\tType\tState\tInode\tPath\nd\ts\ts\td\ts\n");
495
 
}