Small fixes for Windows.
[tinc] / src / top.c
1 /*
2     top.c -- Show real-time statistics from a running tincd
3     Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
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 along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include <curses.h>
23
24 #include "control_common.h"
25 #include "list.h"
26 #include "tincctl.h"
27 #include "top.h"
28 #include "xalloc.h"
29
30 typedef struct nodestats_t {
31         char *name;
32         uint64_t in_packets;
33         uint64_t in_bytes;
34         uint64_t out_packets;
35         uint64_t out_bytes;
36         float in_packets_rate;
37         float in_bytes_rate;
38         float out_packets_rate;
39         float out_bytes_rate;
40         bool known;
41 } nodestats_t;
42
43 static const char *const sortname[] = {
44         "name",
45         "in pkts",
46         "in bytes",
47         "out pkts",
48         "out bytes",
49         "tot pkts",
50         "tot bytes",
51 };
52
53 static int sortmode = 0;
54 static bool cumulative = false;
55
56 static list_t node_list;
57 static struct timeval now, prev, diff;
58 static int delay = 1000;
59 static bool running = true;
60 static bool changed = true;
61 static const char *unit = "bytes";
62 static float scale = 1;
63
64 #ifndef timersub
65 #define timersub(a, b, c) do {(c)->tv_sec = (a)->tv_sec - (b)->tv_sec; (c)->tv_usec = (a)->tv_usec = (b)->tv_usec;} while(0)
66 #endif
67
68 static void update(int fd) {
69         sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
70         gettimeofday(&now, NULL);
71
72         timersub(&now, &prev, &diff);
73         prev = now;
74         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
75
76         char line[4096];
77         char name[4096];
78         int code;
79         int req;
80         uint64_t in_packets;
81         uint64_t in_bytes;
82         uint64_t out_packets;
83         uint64_t out_bytes;
84
85         for(list_node_t *i = node_list.head; i; i = i->next) {
86                 nodestats_t *node = i->data;
87                 node->known = false;
88         }
89
90         while(recvline(fd, line, sizeof line)) {
91                 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
92
93                 if(n == 2)
94                         break;
95
96                 if(n != 7) {
97                         endwin();
98                         fprintf(stderr, "Error receiving traffic information\n");
99                         exit(1);
100                 }
101
102                 nodestats_t *found = NULL;
103
104                 for(list_node_t *i = node_list.head; i; i = i->next) {
105                         nodestats_t *node = i->data;
106                         int result = strcmp(name, node->name);
107                         if(result > 0) {
108                                 continue;
109                         } if(result == 0) {
110                                 found = node;
111                                 break;
112                         } else {
113                                 found = xmalloc_and_zero(sizeof *found);
114                                 found->name = xstrdup(name);
115                                 list_insert_before(&node_list, i, found);
116                                 changed = true;
117                                 break;
118                         }
119                 }
120
121                 if(!found) {
122                         found = xmalloc_and_zero(sizeof *found);
123                         found->name = xstrdup(name);
124                         list_insert_tail(&node_list, found);
125                         changed = true;
126                 }
127
128                 found->known = true;
129                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
130                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
131                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
132                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
133                 found->in_packets = in_packets;
134                 found->in_bytes = in_bytes;
135                 found->out_packets = out_packets;
136                 found->out_bytes = out_bytes;
137         }
138 }
139
140 static void redraw(void) {
141         erase();
142
143         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-8s  %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
144         attrset(A_REVERSE);
145         mvprintw(2, 0, "Node                IN pkts   IN %s   OUT pkts  OUT %s", unit, unit);
146         chgat(-1, A_REVERSE, 0, NULL);
147
148         static nodestats_t **sorted = 0;
149         static int n = 0;
150         if(changed) {
151                 n = 0;
152                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
153                 for(list_node_t *i = node_list.head; i; i = i->next)
154                         sorted[n++] = i->data;
155                 changed = false;
156         }
157         
158         int cmpfloat(float a, float b) {
159                 if(a < b)
160                         return -1;
161                 else if(a > b)
162                         return 1;
163                 else
164                         return 0;
165         }
166
167         int cmpu64(uint64_t a, uint64_t b) {
168                 if(a < b)
169                         return -1;
170                 else if(a > b)
171                         return 1;
172                 else
173                         return 0;
174         }
175
176         int sortfunc(const void *a, const void *b) {
177                 const nodestats_t *na = *(const nodestats_t **)a;
178                 const nodestats_t *nb = *(const nodestats_t **)b;
179                 switch(sortmode) {
180                         case 1:
181                                 if(cumulative)
182                                         return -cmpu64(na->in_packets, nb->in_packets);
183                                 else
184                                         return -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
185                         case 2:
186                                 if(cumulative)
187                                         return -cmpu64(na->in_bytes, nb->in_bytes);
188                                 else
189                                         return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
190                         case 3:
191                                 if(cumulative)
192                                         return -cmpu64(na->out_packets, nb->out_packets);
193                                 else
194                                         return -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
195                         case 4:
196                                 if(cumulative)
197                                         return -cmpu64(na->out_bytes, nb->out_bytes);
198                                 else
199                                         return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
200                         case 5:
201                                 if(cumulative)
202                                         return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
203                                 else
204                                         return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
205                         case 6:
206                                 if(cumulative)
207                                         return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
208                                 else
209                                         return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
210                         default:
211                                 return strcmp(na->name, nb->name);
212                 }
213         }
214
215         qsort(sorted, n, sizeof *sorted, sortfunc);
216
217         for(int i = 0, row = 3; i < n; i++, row++) {
218                 nodestats_t *node = sorted[i];
219                 if(node->known)
220                         if(node->in_packets_rate || node->out_packets_rate)
221                                 attrset(A_BOLD);
222                         else
223                                 attrset(A_NORMAL);
224                 else
225                         attrset(A_DIM);
226
227                 if(cumulative)
228                         mvprintw(row, 0, "%-16s %10"PRIu64" %10.0f %10"PRIu64" %10.0f",
229                                         node->name, node->in_packets, node->in_bytes * scale, node->out_packets, node->out_bytes * scale);
230                 else
231                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
232                                         node->name, node->in_packets_rate, node->in_bytes_rate * scale, node->out_packets_rate, node->out_bytes_rate * scale);
233         }
234
235         attrset(A_NORMAL);
236         move(1, 0);
237
238         refresh();
239 }
240
241 void top(int fd) {
242         initscr();
243         timeout(delay);
244
245         while(running) {
246                 update(fd);
247                 redraw();
248
249                 switch(getch()) {
250                         case 's': {
251                                 timeout(-1);
252                                 float input = delay * 1e-3;
253                                 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
254                                 scanw("%f", &input);
255                                 if(input < 0.1)
256                                         input = 0.1;
257                                 delay = input * 1e3;
258                                 timeout(delay);
259                                 break;
260                         }
261                         case 'c':
262                                   cumulative = !cumulative;
263                                   break;
264                         case 'n':
265                                   sortmode = 0;
266                                   break;
267                         case 'i':
268                                   sortmode = 2;
269                                   break;
270                         case 'I':
271                                   sortmode = 1;
272                                   break;
273                         case 'o':
274                                   sortmode = 4;
275                                   break;
276                         case 'O':
277                                   sortmode = 3;
278                                   break;
279                         case 't':
280                                   sortmode = 6;
281                                   break;
282                         case 'T':
283                                   sortmode = 5;
284                                   break;
285                         case 'b':
286                                   unit = "bytes";
287                                   scale = 1;
288                                   break;
289                         case 'k':
290                                   unit = "kbyte";
291                                   scale = 1e-3;
292                                   break;
293                         case 'M':
294                                   unit = "Mbyte";
295                                   scale = 1e-6;
296                                   break;
297                         case 'G':
298                                   unit = "Gbyte";
299                                   scale = 1e-9;
300                                   break;
301                         case 'q':
302                         case 27:
303                         case KEY_BREAK:
304                                 running = false;
305                                 break;
306                         default:
307                                 break;
308                 }
309         }
310
311         endwin();
312 }