aboutsummaryrefslogtreecommitdiff
path: root/networking/rxtxrpt.c
blob: 609cedbf081bf543dbf282d38a9dba3b31e78355 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
rxtxrpt.c

Report RX and TX statistics. Also report IPv6 address and the IPv6 routing
table if it has changed.
*/

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "libbb.h"

#define DEV_FILE	"/proc/net/dev"
#define IF_INET6_FILE	"/proc/net/if_inet6"
#define IPV6_ROUTE_FILE	"/proc/net/ipv6_route"
#define SUFFIX		".new"

int do_atlas= 0;

static int rpt_rxtx(void);
static int setup_ipv6_rpt(char *cache_name, int *need_report);
static int rpt_ipv6(char *cache_name);
static void report(const char *fmt, ...);
static void report_err(const char *fmt, ...);

int rxtxrpt_main(int argc, char *argv[])
{
	int r, need_report;
	unsigned opt;
	char *opt_atlas, *cache_name;

	opt_atlas= NULL;
	opt_complementary= NULL;
	opt= getopt32(argv, "A:", &opt_atlas);

	do_atlas= (opt_atlas != NULL);

	if (argc > optind+1)
		bb_show_usage();

	cache_name= NULL;
	if (argc == optind+1)
		cache_name= argv[optind];

	if (do_atlas)
	{
		printf("%s %lu ", opt_atlas, time(NULL));
	}

	r= rpt_rxtx();
	if (r != 0)
		return r;

	if (cache_name)
	{
		r= setup_ipv6_rpt(cache_name, &need_report);
		if (r != 0)
			return r;
		if (need_report)
		{
			r= rpt_ipv6(cache_name);
			if (r != 0)
				return r;
		}
	}

	if (do_atlas)
		printf("\n");

	return 0;
}

static int rpt_rxtx(void)
{
	int i;
	char *cp;
	FILE *file;
	char buf[256];

	file= fopen(DEV_FILE, "r");
	if (!file)
	{
		report_err("unable to open '%s'", DEV_FILE);
		return 1;
	}

	/* Skip two lines */
	if (fgets(buf, sizeof(buf), file) == NULL ||
		fgets(buf, sizeof(buf), file) == NULL)
	{
		report_err("unable to read from '%s'", DEV_FILE);
		fclose(file);
		return 1;
	}

	/* Copy two line */
	for (i= 0; i<2; i++)
	{
		if (fgets(buf, sizeof(buf), file) == NULL)
		{
			report_err("unable to read from '%s'", DEV_FILE);
			fclose(file);
			return 1;
		}

		if (do_atlas)
		{
			/* Get rid of newline */
			cp= strchr(buf, '\n');
			if (cp) *cp= '\0';

			if (i != 0)
				printf(" NEWLINE ");
		}
		fputs(buf, stdout);
	}
	fclose(file);

	return 0;
}

static int setup_ipv6_rpt(char *cache_name, int *need_report)
{
	int i, r;
	char *cp, *cp1;
	char filename[80];
	char buf1[1024];
	char buf2[1024];
	FILE *in_file, *out_file, *cache_file;

	*need_report= 0;

	if (strlen(cache_name) + strlen(SUFFIX) + 1 > sizeof(filename))
	{
		report("cache name '%s' too long", cache_name);
		return 1;
	}

	strlcpy(filename, cache_name, sizeof(filename));
	strlcat(filename, SUFFIX, sizeof(filename));

	out_file= fopen(filename, "w");
	if (out_file == NULL)
	{
		report_err("unable to create '%s'", filename);
		return 1;
	}

	/* Copy IF_INET6_FILE */
	in_file= fopen(IF_INET6_FILE, "r");
	if (in_file == NULL)
	{
		report_err("unable to open '%s'", IF_INET6_FILE);
		fclose(out_file);
		return 1;
	}

	while (r= fread(buf1, 1, sizeof(buf1), in_file), r > 0)
	{
		if (fwrite(buf1, 1, r, out_file) != r)
		{
			report_err("error writing to '%s'", filename);
			fclose(in_file);
			fclose(out_file);
			return 1;
		}
	}
	if (ferror(in_file))
	{
		report_err("error reading from '%s'", IF_INET6_FILE);
		fclose(in_file);
		fclose(out_file);
		return 1;
	}
	fclose(in_file);

	/* Copy IPV6_ROUTE_FILE */
	in_file= fopen(IPV6_ROUTE_FILE, "r");
	if (in_file == NULL)
	{
		report_err("unable to open '%s'", IPV6_ROUTE_FILE);
		fclose(out_file);
		return 1;
	}

	while (fgets(buf1, sizeof(buf1), in_file) != NULL)
	{
		/* Cut out Ref and Use fields */
		cp= buf1;
		for (i= 0; i<6; i++)
		{
			if (cp && cp[0] != '\0')
				cp= strchr(cp+1, ' ');
		}
		if (!cp && cp[0] == '\0')
		{
			report("bad data in '%s'", IPV6_ROUTE_FILE);
			fclose(in_file);
			fclose(out_file);
			return 1;
		}
		cp++;
		/* Find the end of the two fields */
		cp1= cp;
		for (i= 0; i<2; i++)
		{
			if (cp1 && cp1[0] != '\0')
				cp1= strchr(cp1+1, ' ');
		}
		if (!cp1 && cp1[0] == '\0')
		{
			report("bad data in '%s'", IPV6_ROUTE_FILE);
			fclose(in_file);
			fclose(out_file);
			return 1;
		}
		cp1++;
		/* And delete the two fields */
		memmove(cp, cp1, strlen(cp1)+1);

		if (fputs(buf1, out_file) == -1)
		{
			report_err("error writing to '%s'", filename);
			fclose(in_file);
			fclose(out_file);
			return 1;
		}
	}
	if (ferror(in_file))
	{
		report_err("error reading from '%s'", IPV6_ROUTE_FILE);
		fclose(in_file);
		fclose(out_file);
		return 1;
	}
	fclose(in_file);

	/* Now check if the new file is different from the cache one */
	fclose(out_file);
	cache_file= fopen(cache_name, "r");
	if (cache_file == NULL)
	{
		/* Assume that any kind of error here calls for reporting */
		*need_report= 1;
	}

	if (cache_file)
	{
		in_file= fopen(filename, "r");
		if (in_file == NULL)
		{
			report_err("unable to open '%s'", filename);
			fclose(cache_file);
			return 1;
		}

		/* Compare them */
		while (r= fread(buf1, 1, sizeof(buf1), cache_file), r > 0)
		{
			if (fread(buf2, 1, sizeof(buf2), in_file) != r)
			{
				/* Ignore errors, just report */
				*need_report= 1;
				break;
			}

			if (memcmp(buf1, buf2, r) != 0)
			{
				/* Something changed, report */
				*need_report= 1;
				break;
			}
		}

		/* Maybe something got added */
		if (!*need_report)
		{
			if (fread(buf2, 1, sizeof(buf2), in_file) != 0)
			{
				*need_report= 1;
			}
		}
		fclose(cache_file);
		fclose(in_file);
	}

	if (*need_report)
	{
		if (rename(filename, cache_name) == -1)
		{
			report_err("renaming '%s' to '%s' failed",
				filename, cache_name);
			return 1;
		}
	}
	else
	{
		if (unlink(filename) == -1)
		{
			report_err("unlinking '%s' failed",
				filename);
		}
	}

	return 0;
}

static int rpt_ipv6(char *cache_name)
{
	FILE *file;
	char *cp;
	char buf[256];

	file= fopen(cache_name, "r");
	if (!file)
	{
		report_err("unable to open '%s'", cache_name);
		return 1;
	}

	/* Copy all lines */
	while (fgets(buf, sizeof(buf), file) != NULL)
	{
		if (do_atlas)
		{
			/* Get rid of newline */
			cp= strchr(buf, '\n');
			if (cp) *cp= '\0';

			printf(" NEWLINE ");
		}
		fputs(buf, stdout);
	}
	fclose(file);

	return 0;
}

static void report(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);

	fprintf(stderr, "rxtxrpt: ");
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, "\n");

	va_end(ap);
}

static void report_err(const char *fmt, ...)
{
	int t_errno;
	va_list ap;

	t_errno= errno;

	va_start(ap, fmt);

	fprintf(stderr, "rxtxrpt: ");
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, ": %s\n", strerror(t_errno));

	va_end(ap);
}