+ found->known = true;
+ found->in_packets_rate = (in_packets - found->in_packets) / interval;
+ found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
+ found->out_packets_rate = (out_packets - found->out_packets) / interval;
+ found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
+ found->in_packets = in_packets;
+ found->in_bytes = in_bytes;
+ found->out_packets = out_packets;
+ found->out_bytes = out_bytes;
+ }
+
+ return false;
+}
+
+static int cmpfloat(float a, float b) {
+ if(a < b)
+ return -1;
+ else if(a > b)
+ return 1;
+ else
+ return 0;
+}
+
+static int cmpu64(uint64_t a, uint64_t b) {
+ if(a < b)
+ return -1;
+ else if(a > b)
+ return 1;
+ else
+ return 0;
+}
+
+static int sortfunc(const void *a, const void *b) {
+ const nodestats_t *na = *(const nodestats_t **)a;
+ const nodestats_t *nb = *(const nodestats_t **)b;
+ int result;
+
+ switch(sortmode) {
+ case 1:
+ if(cumulative)
+ result = -cmpu64(na->in_packets, nb->in_packets);
+ else
+ result = -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
+ break;
+ case 2:
+ if(cumulative)
+ result = -cmpu64(na->in_bytes, nb->in_bytes);
+ else
+ result = -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
+ break;
+ case 3:
+ if(cumulative)
+ result = -cmpu64(na->out_packets, nb->out_packets);
+ else
+ result = -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
+ break;
+ case 4:
+ if(cumulative)
+ result = -cmpu64(na->out_bytes, nb->out_bytes);
+ else
+ result = -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
+ break;
+ case 5:
+ if(cumulative)
+ result = -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
+ else
+ result = -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
+ break;
+ case 6:
+ if(cumulative)
+ result = -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
+ else
+ result = -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
+ break;
+ default:
+ result = strcmp(na->name, nb->name);
+ break;
+ }
+
+ if(result)
+ return result;
+ else
+ return na->i - nb->i;
+}
+
+static void redraw(void) {
+ erase();
+
+ mvprintw(0, 0, "Tinc %-16s Nodes: %4d Sort: %-10s %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
+ attrset(A_REVERSE);
+ mvprintw(2, 0, "Node IN %s IN %s OUT %s OUT %s", punit, bunit, punit, bunit);
+ chgat(-1, A_REVERSE, 0, NULL);
+
+ static nodestats_t **sorted = 0;
+ static int n = 0;
+ if(changed) {
+ n = 0;
+ sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
+ for list_each(nodestats_t, ns, &node_list)
+ sorted[n++] = ns;
+ changed = false;
+ }
+
+ for(int i = 0; i < n; i++)
+ sorted[i]->i = i;
+
+ if(sorted)
+ qsort(sorted, n, sizeof *sorted, sortfunc);