Fix some minor issues found by cppcheck.
[oweals/tinc.git] / src / top.c
1 /*
2     top.c -- Show real-time statistics from a running tincd
3     Copyright (C) 2011-2013 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 #ifdef HAVE_CURSES
23
24 #undef KEY_EVENT  /* There are conflicting declarations for KEY_EVENT in Windows wincon.h and curses.h. */
25 #include <curses.h>
26
27 #include "control_common.h"
28 #include "list.h"
29 #include "names.h"
30 #include "tincctl.h"
31 #include "top.h"
32 #include "xalloc.h"
33
34 typedef struct nodestats_t {
35         char *name;
36         int i;
37         uint64_t in_packets;
38         uint64_t in_bytes;
39         uint64_t out_packets;
40         uint64_t out_bytes;
41         float in_packets_rate;
42         float in_bytes_rate;
43         float out_packets_rate;
44         float out_bytes_rate;
45         bool known;
46 } nodestats_t;
47
48 static const char *const sortname[] = {
49         "name",
50         "in pkts",
51         "in bytes",
52         "out pkts",
53         "out bytes",
54         "tot pkts",
55         "tot bytes",
56 };
57
58 static int sortmode = 0;
59 static bool cumulative = false;
60
61 static list_t node_list;
62 static struct timeval cur, prev, diff;
63 static int delay = 1000;
64 static bool changed = true;
65 static const char *bunit = "bytes";
66 static float bscale = 1;
67 static const char *punit = "pkts";
68 static float pscale = 1;
69
70 static bool update(int fd) {
71         if(!sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC))
72                 return false;
73
74         gettimeofday(&cur, NULL);
75
76         timersub(&cur, &prev, &diff);
77         prev = cur;
78         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
79
80         char line[4096];
81         char name[4096];
82         int code;
83         int req;
84         uint64_t in_packets;
85         uint64_t in_bytes;
86         uint64_t out_packets;
87         uint64_t out_bytes;
88
89         for list_each(nodestats_t, ns, &node_list)
90                 ns->known = false;
91
92         while(recvline(fd, line, sizeof line)) {
93                 int n = sscanf(line, "%d %d %4095s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
94
95                 if(n == 2)
96                         return true;
97
98                 if(n != 7)
99                         return false;
100
101                 nodestats_t *found = NULL;
102
103                 for list_each(nodestats_t, ns, &node_list) {
104                         int result = strcmp(name, ns->name);
105                         if(result > 0) {
106                                 continue;
107                         } if(result == 0) {
108                                 found = ns;
109                                 break;
110                         } else {
111                                 found = xzalloc(sizeof *found);
112                                 found->name = xstrdup(name);
113                                 list_insert_before(&node_list, node, found);
114                                 changed = true;
115                                 break;
116                         }
117                 }
118
119                 if(!found) {
120                         found = xzalloc(sizeof *found);
121                         found->name = xstrdup(name);
122                         list_insert_tail(&node_list, found);
123                         changed = true;
124                 }
125
126                 found->known = true;
127                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
128                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
129                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
130                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
131                 found->in_packets = in_packets;
132                 found->in_bytes = in_bytes;
133                 found->out_packets = out_packets;
134                 found->out_bytes = out_bytes;
135         }
136
137         return false;
138 }
139
140 static int cmpfloat(float a, float b) {
141         if(a < b)
142                 return -1;
143         else if(a > b)
144                 return 1;
145         else
146                 return 0;
147 }
148
149 static int cmpu64(uint64_t a, uint64_t b) {
150         if(a < b)
151                 return -1;
152         else if(a > b)
153                 return 1;
154         else
155                 return 0;
156 }
157
158 static int sortfunc(const void *a, const void *b) {
159         const nodestats_t *na = *(const nodestats_t **)a;
160         const nodestats_t *nb = *(const nodestats_t **)b;
161         int result;
162
163         switch(sortmode) {
164                 case 1:
165                         if(cumulative)
166                                 result = -cmpu64(na->in_packets, nb->in_packets);
167                         else
168                                 result = -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
169                 case 2:
170                         if(cumulative)
171                                 result = -cmpu64(na->in_bytes, nb->in_bytes);
172                         else
173                                 result = -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
174                 case 3:
175                         if(cumulative)
176                                 result = -cmpu64(na->out_packets, nb->out_packets);
177                         else
178                                 result = -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
179                 case 4:
180                         if(cumulative)
181                                 result = -cmpu64(na->out_bytes, nb->out_bytes);
182                         else
183                                 result = -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
184                 case 5:
185                         if(cumulative)
186                                 result = -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
187                         else
188                                 result = -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
189                 case 6:
190                         if(cumulative)
191                                 result = -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
192                         else
193                                 result = -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
194                 default:
195                         result = strcmp(na->name, nb->name);
196         }
197
198         if(result)
199                 return result;
200         else
201                 return na->i - nb->i;
202 }
203
204 static void redraw(void) {
205         erase();
206
207         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-10s  %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
208         attrset(A_REVERSE);
209         mvprintw(2, 0, "Node                IN %s   IN %s   OUT %s  OUT %s", punit, bunit, punit, bunit);
210         chgat(-1, A_REVERSE, 0, NULL);
211
212         static nodestats_t **sorted = 0;
213         static int n = 0;
214         if(changed) {
215                 n = 0;
216                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
217                 for list_each(nodestats_t, ns, &node_list)
218                         sorted[n++] = ns;
219                 changed = false;
220         }
221
222         for(int i = 0; i < n; i++)
223                 sorted[i]->i = i;
224
225         if(sorted)
226                 qsort(sorted, n, sizeof *sorted, sortfunc);
227
228         for(int i = 0, row = 3; i < n; i++, row++) {
229                 nodestats_t *node = sorted[i];
230                 if(node->known)
231                         if(node->in_packets_rate || node->out_packets_rate)
232                                 attrset(A_BOLD);
233                         else
234                                 attrset(A_NORMAL);
235                 else
236                         attrset(A_DIM);
237
238                 if(cumulative)
239                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
240                                         node->name, node->in_packets * pscale, node->in_bytes * bscale, node->out_packets * pscale, node->out_bytes * bscale);
241                 else
242                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
243                                         node->name, node->in_packets_rate * pscale, node->in_bytes_rate * bscale, node->out_packets_rate * pscale, node->out_bytes_rate * bscale);
244         }
245
246         attrset(A_NORMAL);
247         move(1, 0);
248
249         refresh();
250 }
251
252 void top(int fd) {
253         initscr();
254         timeout(delay);
255         bool running = true;
256
257         while(running) {
258                 if(!update(fd))
259                         break;
260
261                 redraw();
262
263                 switch(getch()) {
264                         case 's': {
265                                 timeout(-1);
266                                 float input = delay * 1e-3;
267                                 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
268                                 scanw("%f", &input);
269                                 if(input < 0.1)
270                                         input = 0.1;
271                                 delay = input * 1e3;
272                                 timeout(delay);
273                                 break;
274                         }
275                         case 'c':
276                                 cumulative = !cumulative;
277                                 break;
278                         case 'n':
279                                 sortmode = 0;
280                                 break;
281                         case 'i':
282                                 sortmode = 2;
283                                 break;
284                         case 'I':
285                                 sortmode = 1;
286                                 break;
287                         case 'o':
288                                 sortmode = 4;
289                                 break;
290                         case 'O':
291                                 sortmode = 3;
292                                 break;
293                         case 't':
294                                 sortmode = 6;
295                                 break;
296                         case 'T':
297                                 sortmode = 5;
298                                 break;
299                         case 'b':
300                                 bunit = "bytes";
301                                 bscale = 1;
302                                 punit = "pkts";
303                                 pscale = 1;
304                                 break;
305                         case 'k':
306                                 bunit = "kbyte";
307                                 bscale = 1e-3;
308                                 punit = "pkts";
309                                 pscale = 1;
310                                 break;
311                         case 'M':
312                                 bunit = "Mbyte";
313                                 bscale = 1e-6;
314                                 punit = "kpkt";
315                                 pscale = 1e-3;
316                                 break;
317                         case 'G':
318                                 bunit = "Gbyte";
319                                 bscale = 1e-9;
320                                 punit = "Mpkt";
321                                 pscale = 1e-6;
322                                 break;
323                         case 'q':
324                         case KEY_BREAK:
325                                 running = false;
326                                 break;
327                         default:
328                                 break;
329                 }
330         }
331
332         endwin();
333 }
334
335 #endif