summaryrefslogtreecommitdiff
path: root/sysdeps/linux/netload.c
blob: b5b2fbdd45c0b23c8e731d7d355a859be149c9f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* $Id$ */

/* Copyright (C) 1998-99 Martin Baulig
   This file is part of LibGTop 1.0.

   Contributed by Martin Baulig <martin@home-of-linux.org>, October 1998.

   LibGTop is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.

   LibGTop is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   for more details.

   You should have received a copy of the GNU General Public License
   along with LibGTop; see the file COPYING. If not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/netload.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
/* GNU LibC */
#include <net/if.h>
#include <netinet/ip_icmp.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <net/if.h>
#else /* Libc 5 */
#include <linux/if.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#endif

#define _GLIBTOP_IP_FW_ACCTIN	0x1000	/* Account incoming packets only. */
#define _GLIBTOP_IP_FW_ACCTOUT	0x2000	/* Account outgoing packets only. */

static const unsigned long _glibtop_sysdeps_netload = 
(1 << GLIBTOP_NETLOAD_ERRORS_IN) +
(1 << GLIBTOP_NETLOAD_ERRORS_OUT) +
(1 << GLIBTOP_NETLOAD_COLLISIONS);

static const unsigned long _glibtop_sysdeps_netload_data =
(1 << GLIBTOP_NETLOAD_ADDRESS) +
(1 << GLIBTOP_NETLOAD_SUBNET) +
(1 << GLIBTOP_NETLOAD_MTU);

static const unsigned long _glibtop_sysdeps_netload_bytes =
(1 << GLIBTOP_NETLOAD_BYTES_IN) +
(1 << GLIBTOP_NETLOAD_BYTES_OUT) +
(1 << GLIBTOP_NETLOAD_BYTES_TOTAL);

static const unsigned long _glibtop_sysdeps_netload_packets =
(1 << GLIBTOP_NETLOAD_PACKETS_IN) +
(1 << GLIBTOP_NETLOAD_PACKETS_OUT) +
(1 << GLIBTOP_NETLOAD_PACKETS_TOTAL);

static const unsigned long _glibtop_sysdeps_netload_total =
(1 << GLIBTOP_NETLOAD_PACKETS_TOTAL) +
(1 << GLIBTOP_NETLOAD_BYTES_TOTAL);

static const unsigned long _glibtop_sysdeps_netload_in =
(1 << GLIBTOP_NETLOAD_PACKETS_TOTAL) +
(1 << GLIBTOP_NETLOAD_BYTES_TOTAL) +
(1 << GLIBTOP_NETLOAD_PACKETS_IN) +
(1 << GLIBTOP_NETLOAD_BYTES_IN);

static const unsigned long _glibtop_sysdeps_netload_out =
(1 << GLIBTOP_NETLOAD_PACKETS_TOTAL) +
(1 << GLIBTOP_NETLOAD_BYTES_TOTAL) +
(1 << GLIBTOP_NETLOAD_PACKETS_OUT) +
(1 << GLIBTOP_NETLOAD_BYTES_OUT);

/* Init function. */

void
glibtop_init_netload_s (glibtop *server)
{
    server->sysdeps.netload = _glibtop_sysdeps_netload |
	_glibtop_sysdeps_netload_data |
	_glibtop_sysdeps_netload_bytes |
	_glibtop_sysdeps_netload_packets;
}

/* Provides network statistics. */

void
glibtop_get_netload_s (glibtop *server, glibtop_netload *buf,
		       const char *interface)
{
    char buffer [BUFSIZ], *p;
    int have_bytes, fields, skfd;
    FILE *f;

    memset (buf, 0, sizeof (glibtop_netload));

    skfd = socket (AF_INET, SOCK_DGRAM, 0);
    if (skfd) {
	struct ifreq ifr;
	struct sockaddr_in addr;
	char *address;
	unsigned flags;

	strcpy (ifr.ifr_name, interface);
	if (!ioctl (skfd, SIOCGIFFLAGS, &ifr)) {
	    buf->flags |= (1 << GLIBTOP_NETLOAD_IF_FLAGS);
	    flags = ifr.ifr_flags;
	} else
	    flags = 0;

	if (flags & IFF_UP)
	    buf->if_flags |= (1 << GLIBTOP_IF_FLAGS_UP);

	if (flags & IFF_BROADCAST)
	    buf->if_flags |= (1 << GLIBTOP_IF_FLAGS_BROADCAST);

	if (flags & IFF_DEBUG)
	    buf->if_flags |= (1 << GLIBTOP_IF_FLAGS_DEBUG);

	if (flags & IFF_LOOPBACK)
	    buf->if_flags |= (1 << GLIBTOP_IF_FLAGS_LOOPBACK);

	if (flags & IFF_POINTOPOINT)
	    buf->if_flags |= (1 << GLIBTOP_IF_FLAGS_POINTOPOINT);

	if (flags & IFF_RUNNING)
	    buf->if_flags |= (1 << GLIBTOP_IF_FLAGS_RUNNING);

	if (flags & IFF_NOARP)
	    buf->if_flags |= (1 << GLIBTOP_IF_FLAGS_NOARP);

	if (flags & IFF_PROMISC)
	    buf->if_flags |= (1 << GLIBTOP_IF_FLAGS_PROMISC);

	if (flags & IFF_ALLMULTI)
	    buf->if_flags |= (1 << GLIBTOP_IF_FLAGS_ALLMULTI);

	if (flags & IFF_MULTICAST)
	    buf->if_flags |= (1 << GLIBTOP_IF_FLAGS_MULTICAST);

	strcpy (ifr.ifr_name, interface);
	if (!ioctl (skfd, SIOCGIFADDR, &ifr)) {
	    struct sockaddr_in addr =
		*(struct sockaddr_in *) &ifr.ifr_addr;
	    buf->address = addr.sin_addr.s_addr;
	    buf->flags |= (1 << GLIBTOP_NETLOAD_ADDRESS);
	}

	strcpy (ifr.ifr_name, interface);
	if (!ioctl (skfd, SIOCGIFNETMASK, &ifr)) {
	    struct sockaddr_in addr =
		*(struct sockaddr_in *) &ifr.ifr_addr;
	    buf->subnet = addr.sin_addr.s_addr;
	    buf->flags |= (1 << GLIBTOP_NETLOAD_SUBNET);
	}

	strcpy (ifr.ifr_name, interface);
	if (!ioctl (skfd, SIOCGIFMTU, &ifr)) {
	    buf->mtu = ifr.ifr_mtu;
	    buf->flags |= (1 << GLIBTOP_NETLOAD_MTU);
	}

	close (skfd);
    }

    /* Linux 2.1.114 - don't know where exactly this was added, but
	 * recent kernels have byte count in /proc/net/dev so we don't
	 * need IP accounting.
	 */

    if (server->os_version_code < 131442) {

	/* If IP accounting is enabled in the kernel and it is
	 * enabled for the requested interface, we use it to
	 * get the data. In this case, we don't use /proc/net/dev
	 * to get errors and collisions.
	 */

	f = fopen ("/proc/net/ip_acct", "r");
	if (f) {
	    int success = 0;

	    /* Skip over the header line. */
	    fgets (buffer, BUFSIZ-1, f);

	    while (fgets (buffer, BUFSIZ-1, f)) {
		unsigned long flags, packets, bytes;
		char *p, *dev;

		/* Skip over the network thing. */		
		dev = skip_token (buffer) + 1;
		p = skip_token (dev);
		*p++ = 0;

		if (strcmp (dev, interface))
		    continue;

		success = 1;

		p = skip_token (p);

		flags   = strtoul (p, &p, 16);

		p = skip_multiple_token (p, 2);

		packets = strtoul (p, &p, 0);
		bytes   = strtoul (p, &p, 0);

		if (flags & _GLIBTOP_IP_FW_ACCTIN) {
		    /* Incoming packets only. */

		    buf->packets_total += packets;
		    buf->packets_in += packets;

		    buf->bytes_total += bytes;
		    buf->bytes_in += bytes;

		    buf->flags |= _glibtop_sysdeps_netload_in;

		} else if (flags & _GLIBTOP_IP_FW_ACCTOUT) {
		    /* Outgoing packets only. */

		    buf->packets_total += packets;
		    buf->packets_out += packets;

		    buf->bytes_total += bytes;
		    buf->bytes_out += bytes;

		    buf->flags |= _glibtop_sysdeps_netload_out;

		} else {
		    /* Only have total values. */

		    buf->packets_total += packets;
		    buf->bytes_total += bytes;

		    buf->flags |= _glibtop_sysdeps_netload_total;
		}
	    }

	    fclose (f);

	    if (success) return;
	}
    }

    /* Ok, either IP accounting is not enabled in the kernel or
	 * it was not enabled for the requested interface. */

    f = fopen ("/proc/net/dev", "r");
    if (!f) return;

	/* Skip over the header line. */
    fgets (buffer, BUFSIZ-1, f);
    fgets (buffer, BUFSIZ-1, f);

	/* Starting with 2.1.xx (don't know exactly which version)
	 * /proc/net/dev contains both byte and package counters. */

    p = strchr (buffer, '|');
    if (!p) {
	fclose (f);
	return;
    }

    /* Do we already have byte counters ? */
    have_bytes = strncmp (++p, "bytes", 5) == 0;

    /* Count remaining 'Receive' fields so we know where
	 * the first 'Transmit' field starts. */
	
    fields = 0;
    while (*p != '|') {
	if (!isspace (*p++)) continue;
	while (isspace (*p++)) ;
	fields++;
    }

    /* Should never happen. */
    if (fields < 2) return;
    fields--;

    while (fgets (buffer, BUFSIZ-1, f)) {
	char *p, *dev;

	dev = buffer;
	while (isspace (*dev)) dev++;

	p = strchr (dev, ':');
	if (!p) continue;
	*p++ = 0;

	/* If it's not a digit, then it's most likely an error
	 * message like 'No statistics available'. */
	while (isspace (*p)) p++;
	if (!isdigit (*p)) continue;

	if (strcmp (dev, interface))
	    continue;

	/* Only read byte counts if we really have them. */

	if (have_bytes) {
	    buf->bytes_in = strtoul (p, &p, 0);
	    fields--;
	}

	buf->packets_in = strtoul (p, &p, 0);
	buf->errors_in  = strtoul (p, &p, 0);

	p = skip_multiple_token (p, fields);

	if (have_bytes)
	    buf->bytes_out = strtoul (p, &p, 0);

	buf->packets_out = strtoul (p, &p, 0);
	buf->errors_out  = strtoul (p, &p, 0);

	p = skip_multiple_token (p, 2);

	buf->collisions  = strtoul (p, &p, 0);

	/* Compute total valules. */

	buf->bytes_total = buf->bytes_in + buf->bytes_out;
	buf->packets_total = buf->packets_in + buf->packets_out;

	/* And now the flags. */

	buf->flags |= _glibtop_sysdeps_netload;
	buf->flags |= _glibtop_sysdeps_netload_packets;

	if (have_bytes)
	    buf->flags |= _glibtop_sysdeps_netload_bytes;
    }

    fclose (f);
}