sparse fixup: warning: Using plain integer as NULL pointer
[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
61 static void update(int fd) {
62         sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
63         gettimeofday(&now, NULL);
64
65         timersub(&now, &prev, &diff);
66         prev = now;
67         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
68
69         char line[4096];
70         char name[4096];
71         int code;
72         int req;
73         uint64_t in_packets;
74         uint64_t in_bytes;
75         uint64_t out_packets;
76         uint64_t out_bytes;
77
78         for(list_node_t *i = node_list.head; i; i = i->next) {
79                 nodestats_t *node = i->data;
80                 node->known = false;
81         }
82
83         while(recvline(fd, line, sizeof line)) {
84                 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
85
86                 if(n == 2)
87                         break;
88
89                 if(n != 7) {
90                         endwin();
91                         fprintf(stderr, "Error receiving traffic information\n");
92                         exit(1);
93                 }
94
95                 nodestats_t *found = NULL;
96
97                 for(list_node_t *i = node_list.head; i; i = i->next) {
98                         nodestats_t *node = i->data;
99                         int result = strcmp(name, node->name);
100                         if(result > 0) {
101                                 continue;
102                         } if(result == 0) {
103                                 found = node;
104                                 break;
105                         } else {
106                                 found = xmalloc_and_zero(sizeof *found);
107                                 found->name = xstrdup(name);
108                                 list_insert_after(&node_list, i, found);
109                                 break;
110                         }
111                 }
112
113                 if(!found) {
114                         found = xmalloc_and_zero(sizeof *found);
115                         found->name = xstrdup(name);
116                         list_insert_tail(&node_list, found);
117                 }
118
119                 found->known = true;
120                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
121                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
122                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
123                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
124                 found->in_packets = in_packets;
125                 found->in_bytes = in_bytes;
126                 found->out_packets = out_packets;
127                 found->out_bytes = out_bytes;
128         }
129 }
130
131 static void redraw(void) {
132         erase();
133
134         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-8s  %s", netname, node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
135         attrset(A_REVERSE);
136         mvprintw(2, 0, "Node                IN pkts   IN bytes   OUT pkts  OUT bytes");
137         chgat(-1, A_REVERSE, 0, NULL);
138
139         nodestats_t *sorted[node_list.count];
140         int n = 0;
141         for(list_node_t *i = node_list.head; i; i = i->next)
142                 sorted[n++] = i->data;
143         
144         int cmpfloat(float a, float b) {
145                 if(a < b)
146                         return -1;
147                 else if(a > b)
148                         return 1;
149                 else
150                         return 0;
151         }
152
153         int cmpu64(uint64_t a, uint64_t b) {
154                 if(a < b)
155                         return -1;
156                 else if(a > b)
157                         return 1;
158                 else
159                         return 0;
160         }
161
162         int sortfunc(const void *a, const void *b) {
163                 const nodestats_t *na = *(const nodestats_t **)a;
164                 const nodestats_t *nb = *(const nodestats_t **)b;
165                 switch(sortmode) {
166                         case 1:
167                                 if(cumulative)
168                                         return -cmpu64(na->in_packets, nb->in_packets);
169                                 else
170                                         return -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
171                         case 2:
172                                 if(cumulative)
173                                         return -cmpu64(na->in_bytes, nb->in_bytes);
174                                 else
175                                         return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
176                         case 3:
177                                 if(cumulative)
178                                         return -cmpu64(na->out_packets, nb->out_packets);
179                                 else
180                                         return -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
181                         case 4:
182                                 if(cumulative)
183                                         return -cmpu64(na->out_bytes, nb->out_bytes);
184                                 else
185                                         return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
186                         case 5:
187                                 if(cumulative)
188                                         return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
189                                 else
190                                         return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
191                         case 6:
192                                 if(cumulative)
193                                         return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
194                                 else
195                                         return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
196                         default:
197                                 return strcmp(na->name, nb->name);
198                 }
199         }
200
201         qsort(sorted, n, sizeof *sorted, sortfunc);
202
203         int row = 3;
204         for(int i = 0; i < n; i++, row++) {
205                 nodestats_t *node = sorted[i];
206                 if(node->known)
207                         if(node->in_packets_rate || node->out_packets_rate)
208                                 attrset(A_BOLD);
209                         else
210                                 attrset(A_NORMAL);
211                 else
212                         attrset(A_DIM);
213
214                 if(cumulative)
215                         mvprintw(row, 0, "%-16s %'10"PRIu64" %'10"PRIu64" %'10"PRIu64" %'10"PRIu64,
216                                         node->name, node->in_packets, node->in_bytes, node->out_packets, node->out_bytes);
217                 else
218                         mvprintw(row, 0, "%-16s %'10.0f %'10.0f %'10.0f %'10.0f",
219                                         node->name, node->in_packets_rate, node->in_bytes_rate, node->out_packets_rate, node->out_bytes_rate);
220         }
221
222         attrset(A_NORMAL);
223         move(1, 0);
224
225         refresh();
226 }
227
228 void top(int fd) {
229         initscr();
230         timeout(delay);
231
232         while(running) {
233                 update(fd);
234                 redraw();
235
236                 switch(getch()) {
237                         case 's': {
238                                 timeout(-1);
239                                 float input = delay * 1e-3;
240                                 printw("Change delay from %.1fs to: ", input);
241                                 scanw("%f", &input);
242                                 if(input < 0.1)
243                                         input = 0.1;
244                                 delay = input * 1e3;
245                                 timeout(delay);
246                                 break;
247                         }
248                         case 'c':
249                                   cumulative = !cumulative;
250                                   break;
251                         case 'n':
252                                   sortmode = 0;
253                                   break;
254                         case 'i':
255                                   sortmode = 2;
256                                   break;
257                         case 'I':
258                                   sortmode = 1;
259                                   break;
260                         case 'o':
261                                   sortmode = 4;
262                                   break;
263                         case 'O':
264                                   sortmode = 3;
265                                   break;
266                         case 't':
267                                   sortmode = 6;
268                                   break;
269                         case 'T':
270                                   sortmode = 5;
271                                   break;
272                         case 'q':
273                         case 27:
274                         case KEY_BREAK:
275                                 running = false;
276                                 break;
277                         default:
278                                 break;
279                 }
280         }
281
282         endwin();
283 }