11 #include "addr_hash.h"
12 #include "serv_hash.h"
15 #include "sorted_list.h"
18 #include "ui_common.h"
20 /* 2, 10 and 40 seconds */
21 int history_divs[HISTORY_DIVISIONS] = {1, 5, 20};
23 #define UNIT_DIVISIONS 4
24 char* unit_bits[UNIT_DIVISIONS] = { "b", "Kb", "Mb", "Gb"};
25 char* unit_bytes[UNIT_DIVISIONS] = { "B", "KB", "MB", "GB"};
27 extern hash_type* history;
28 extern int history_pos;
29 extern int history_len;
32 * Compare two screen lines based on bandwidth. Start comparing from the
35 int screen_line_bandwidth_compare(host_pair_line* aa, host_pair_line* bb, int start_div) {
37 switch(options.linedisplay) {
38 case OPTION_LINEDISPLAY_ONE_LINE_SENT:
39 for(i = start_div; i < HISTORY_DIVISIONS; i++) {
40 if(aa->sent[i] != bb->sent[i]) {
41 return(aa->sent[i] < bb->sent[i]);
45 case OPTION_LINEDISPLAY_ONE_LINE_RECV:
46 for(i = start_div; i < HISTORY_DIVISIONS; i++) {
47 if(aa->recv[i] != bb->recv[i]) {
48 return(aa->recv[i] < bb->recv[i]);
52 case OPTION_LINEDISPLAY_TWO_LINE:
53 case OPTION_LINEDISPLAY_ONE_LINE_BOTH:
54 /* fallback to the combined sent+recv that also act as fallback for sent/recv */
57 for(i = start_div; i < HISTORY_DIVISIONS; i++) {
58 if(aa->recv[i] + aa->sent[i] != bb->recv[i] + bb->sent[i]) {
59 return(aa->recv[i] + aa->sent[i] < bb->recv[i] + bb->sent[i]);
66 * Compare two screen lines based on hostname / IP. Fall over to compare by
69 int screen_line_host_compare(void* a, void* b, host_pair_line* aa, host_pair_line* bb) {
70 char hosta[HOSTNAME_LENGTH], hostb[HOSTNAME_LENGTH];
73 /* This isn't overly efficient because we resolve again before
75 if (options.dnsresolution) {
76 resolve(aa->ap.af, a, hosta, HOSTNAME_LENGTH);
77 resolve(bb->ap.af, b, hostb, HOSTNAME_LENGTH);
80 inet_ntop(aa->ap.af, a, hosta, sizeof(hosta));
81 inet_ntop(bb->ap.af, b, hostb, sizeof(hostb));
84 r = strcmp(hosta, hostb);
87 return screen_line_bandwidth_compare(aa, bb, 2);
97 * Compare two screen lines based on the sorting options selected.
99 int screen_line_compare(void* a, void* b) {
100 host_pair_line* aa = (host_pair_line*)a;
101 host_pair_line* bb = (host_pair_line*)b;
102 if(options.sort == OPTION_SORT_DIV1) {
103 return screen_line_bandwidth_compare(aa, bb, 0);
105 else if(options.sort == OPTION_SORT_DIV2) {
106 return screen_line_bandwidth_compare(aa, bb, 1);
108 else if(options.sort == OPTION_SORT_DIV3) {
109 return screen_line_bandwidth_compare(aa, bb, 2);
111 else if(options.sort == OPTION_SORT_SRC) {
112 return screen_line_host_compare(&(aa->ap.src6), &(bb->ap.src6), aa, bb);
114 else if(options.sort == OPTION_SORT_DEST) {
115 return screen_line_host_compare(&(aa->ap.dst6), &(bb->ap.dst6), aa, bb);
122 * Format a data size in human-readable format
124 void readable_size(float n, char* buf, int bsize, int ksize, int bytes) {
129 /* Convert to bits? */
135 if(n < size * 1000 || i >= UNIT_DIVISIONS - 1) {
136 snprintf(buf, bsize, " %4.0f%s", n / size, bytes ? unit_bytes[i] : unit_bits[i]);
142 snprintf(buf, bsize, " %4.2f%s", n / size, bytes ? unit_bytes[i] : unit_bits[i]);
145 else if(n < size * 100) {
146 snprintf(buf, bsize, " %4.1f%s", n / size, bytes ? unit_bytes[i] : unit_bits[i]);
152 int history_length(const int d) {
153 if (history_len < history_divs[d])
154 return history_len * RESOLUTION;
156 return history_divs[d] * RESOLUTION;
159 void screen_list_init() {
160 screen_list.compare = &screen_line_compare;
161 sorted_list_initialise(&screen_list);
164 void screen_list_clear() {
165 sorted_list_node* nn = NULL;
166 peaksent = peakrecv = peaktotal = 0;
167 while((nn = sorted_list_next_item(&screen_list, nn)) != NULL) {
170 sorted_list_destroy(&screen_list);
174 * Calculate peaks and totals
176 void calculate_totals() {
179 for(i = 0; i < HISTORY_LENGTH; i++) {
181 int ii = (HISTORY_LENGTH + history_pos - i) % HISTORY_LENGTH;
183 for(j = 0; j < HISTORY_DIVISIONS; j++) {
184 if(i < history_divs[j]) {
185 totals.recv[j] += history_totals.recv[ii];
186 totals.sent[j] += history_totals.sent[ii];
190 if(history_totals.recv[i] > peakrecv) {
191 peakrecv = history_totals.recv[i];
193 if(history_totals.sent[i] > peaksent) {
194 peaksent = history_totals.sent[i];
196 if(history_totals.recv[i] + history_totals.sent[i] > peaktotal) {
197 peaktotal = history_totals.recv[i] + history_totals.sent[i];
200 for(i = 0; i < HISTORY_DIVISIONS; i++) {
201 int t = history_length(i);
207 void make_screen_list() {
208 hash_node_type* n = NULL;
209 while(hash_next_item(screen_hash, &n) == HASH_STATUS_OK) {
210 host_pair_line* line = (host_pair_line*)n->rec;
212 for(i = 0; i < HISTORY_DIVISIONS; i++) {
213 line->recv[i] /= history_length(i);
214 line->sent[i] /= history_length(i);
217 /* Don't make a new, sorted screen list if order is frozen
219 if(!options.freezeorder) {
220 sorted_list_insert(&screen_list, line);
227 * Zeros all data in the screen hash, but does not remove items.
229 void screen_hash_clear() {
230 hash_node_type* n = NULL;
231 while(hash_next_item(screen_hash, &n) == HASH_STATUS_OK) {
232 host_pair_line* hpl = (host_pair_line*)n->rec;
233 hpl->total_recv = hpl->total_sent = 0;
234 memset(hpl->recv, 0, sizeof(hpl->recv));
235 memset(hpl->sent, 0, sizeof(hpl->sent));
239 void analyse_data() {
240 hash_node_type* n = NULL;
242 if(options.paused == 1) {
247 memset(&totals, 0, sizeof totals);
249 if(options.freezeorder) {
254 hash_delete_all(screen_hash);
257 while(hash_next_item(history, &n) == HASH_STATUS_OK) {
258 history_type* d = (history_type*)n->rec;
259 host_pair_line* screen_line;
261 host_pair_line **h_p_l_pp;
263 } u_screen_line = { &screen_line };
270 ap = *(addr_pair*)n->key;
272 /* Aggregate hosts, if required */
273 if(options.aggregate_src) {
274 memset(&ap.src6, '\0', sizeof(ap.src6));
276 if(options.aggregate_dest) {
277 memset(&ap.dst6, '\0', sizeof(ap.dst6));
280 /* Aggregate ports, if required */
281 if(options.showports == OPTION_PORTS_DEST || options.showports == OPTION_PORTS_OFF) {
284 if(options.showports == OPTION_PORTS_SRC || options.showports == OPTION_PORTS_OFF) {
287 if(options.showports == OPTION_PORTS_OFF) {
292 if(hash_find(screen_hash, &ap, u_screen_line.void_pp) == HASH_STATUS_KEY_NOT_FOUND) {
293 screen_line = xcalloc(1, sizeof *screen_line);
294 hash_insert(screen_hash, &ap, screen_line);
295 screen_line->ap = ap;
298 screen_line->total_sent += d->total_sent;
299 screen_line->total_recv += d->total_recv;
301 for(i = 0; i < HISTORY_LENGTH; i++) {
303 int ii = (HISTORY_LENGTH + history_pos - i) % HISTORY_LENGTH;
305 for(j = 0; j < HISTORY_DIVISIONS; j++) {
306 if(i < history_divs[j]) {
307 screen_line->recv[j] += d->recv[ii];
308 screen_line->sent[j] += d->sent[ii];
322 void sprint_host(char * line, int af, struct in6_addr* addr, unsigned int port, unsigned int protocol, int L, int unspecified_as_star) {
323 char hostname[HOSTNAME_LENGTH];
324 char service[HOSTNAME_LENGTH];
329 } u_s_name = { &s_name };
334 if(IN6_IS_ADDR_UNSPECIFIED(addr) && unspecified_as_star) {
335 sprintf(hostname, " * ");
338 if (options.dnsresolution)
339 resolve(af, addr, hostname, L);
341 inet_ntop(af, addr, hostname, sizeof(hostname));
343 left = strlen(hostname);
347 skey.protocol = protocol;
348 if(options.portresolution && hash_find(service_hash, &skey, u_s_name.void_pp) == HASH_STATUS_OK) {
349 snprintf(service, HOSTNAME_LENGTH, ":%s", s_name);
352 snprintf(service, HOSTNAME_LENGTH, ":%d", port);
359 /* If we're showing IPv6 addresses with a port number, put them in square
361 if(port == 0 || af == AF_INET || L < 2) {
362 sprintf(line, "%-*s", L, hostname);
365 sprintf(line, "[%-.*s]", L-2, hostname);
368 if(left > (L - strlen(service))) {
369 left = L - strlen(service);
374 sprintf(line + left, "%-*s", L-left, service);