/
iftop.c
180 lines (162 loc) · 4.37 KB
/
iftop.c
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
/*_
* Copyright 2011,2014 Scyphus Solutions Co. Ltd. All rights reserved.
*
* Authors:
* Hirochika Asai <asai@scyphus.co.jp>
*/
#define TRAFFIC_MGT 1
#include "config.h"
#include "ifmon.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#if WITH_CURSES
#include <curses.h>
#endif
static void
_usage(const char *prog)
{
fprintf(stderr, "Usage: %s <ifname1> [<ifname2> ...]\n", prog);
exit(EXIT_FAILURE);
}
int
main(int argc, const char *const argv[], const char *const envp[])
{
ifmon_t *ifmons;
char **ifnames;
int nifs;
int ret;
int flags;
int ch;
double delta;
double dtxb;
double drxb;
double dtxp;
double drxp;
int i;
if ( 2 > argc ) {
_usage(argv[0]);
}
/* Get interfaces */
nifs = argc - 1;
ifnames = malloc(sizeof(char *) * nifs);
if ( NULL == ifnames ) {
perror("malloc()");
return EXIT_FAILURE;
}
ifmons = malloc(sizeof(ifmon_t) * nifs);
if ( NULL == ifmons ) {
perror("malloc()");
return EXIT_FAILURE;
}
for ( i = 0; i < nifs; i++ ) {
ifnames[i] = strdup(argv[i+1]);
if ( NULL == ifnames[i] ) {
perror("strdup()");
return EXIT_FAILURE;
}
if ( NULL == ifmon_init(&ifmons[i], ifnames[i]) ) {
/* Error */
fprintf(stderr, "Error: ifname=%s\n", ifnames[i]);
return EXIT_FAILURE;
}
}
#if WITH_CURSES
/* curses */
if ( NULL == initscr() ) {
fprintf(stderr, "initscr(): Cannot initialize screen\n");
return EXIT_FAILURE;
}
cbreak();
noecho();
scrollok(stdscr, FALSE);
move(0, 0);
#endif
/* Set stdin as non-blocking */
flags = fcntl(STDIN_FILENO, F_GETFL, 0);
if ( -1 == flags ) {
flags = 0;
}
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
for ( ;; ) {
#if WITH_CURSES
ch = getch();
if ( 'q' == ch ) {
break;
}
#endif
for ( i = 0; i < nifs; i++ ) {
ret = ifmon_update(&ifmons[i]);
if ( 0 != ret ) {
/* Error */
break;
}
}
#if WITH_CURSES
clear();
#endif
#if WITH_CURSES
time_t timer;
time(&timer);
/*
struct tm *tm;
tm = localtime(&timer);
*/
/*printw("Time: %.6f\n", );*/
printw("Interface statistics\n%s\n", ctime(&timer));
printw(" pkts/s kbps\n");
printw(" TX RX TX RX\n");
#endif
for ( i = 0; i < nifs; i++ ) {
delta = ifmons[i].curstat.time - ifmons[i].prevstat.time;
dtxp = ifmons[i].curstat.total_pkts.tx
- ifmons[i].prevstat.total_pkts.tx;
drxp = ifmons[i].curstat.total_pkts.rx
- ifmons[i].prevstat.total_pkts.rx;
dtxb = ifmons[i].curstat.total_bytes.tx
- ifmons[i].prevstat.total_bytes.tx;
drxb = ifmons[i].curstat.total_bytes.rx
- ifmons[i].prevstat.total_bytes.rx;
#if WITH_CURSES
printw("%-8.8s: %11.02lf %11.02lf %11.02lf %11.02lf\n",
ifnames[i], dtxp/delta, drxp/delta,
8*dtxb/delta/1000, 8*drxb/delta/1000);
#else
printf("%-8.8s: %11.02lf %11.02lf %11.02lf %11.02lf\n",
ifnames[i], dtxp/delta, drxp/delta,
8*dtxb/delta/1000, 8*drxb/delta/1000);
#endif
}
#if 0
printw("Time: %.6f (%.6f)\n",
ifmon.curstat.time, ifmon.curstat.time - ifmon.prevstat.time);
printw("\tTX: %15llu pkts\tRX: %15llu pkts\n",
ifmon.curstat.total_pkts.tx - ifmon.prevstat.total_pkts.tx,
ifmon.curstat.total_pkts.rx - ifmon.prevstat.total_pkts.rx);
printw("\tTX: %15llu bytes\tRX: %15llu bytes\n",
ifmon.curstat.total_bytes.tx - ifmon.prevstat.total_bytes.tx,
ifmon.curstat.total_bytes.rx - ifmon.prevstat.total_bytes.rx);
#endif
#if WITH_CURSES
refresh();
#endif
sleep(1);
}
#if WITH_CURSES
/* End curses */
endwin();
#endif
return 0;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/