-void screen_list_init() {
- screen_list.compare = &screen_line_compare;
- sorted_list_initialise(&screen_list);
-}
-
-void screen_list_clear() {
- sorted_list_node* nn = NULL;
- peaksent = peakrecv = peaktotal = 0;
- while((nn = sorted_list_next_item(&screen_list, nn)) != NULL) {
- free(nn->data);
- }
- sorted_list_destroy(&screen_list);
-}
-
-void calculate_totals() {
- int i;
-
- /**
- * Calculate peaks and totals
- */
- for(i = 0; i < HISTORY_LENGTH; i++) {
- int j;
- int ii = (HISTORY_LENGTH + history_pos - i) % HISTORY_LENGTH;
-
- for(j = 0; j < HISTORY_DIVISIONS; j++) {
- if(i < history_divs[j]) {
- totals.recv[j] += history_totals.recv[ii];
- totals.sent[j] += history_totals.sent[ii];
- }
- }
-
- if(history_totals.recv[i] > peakrecv) {
- peakrecv = history_totals.recv[i];
- }
- if(history_totals.sent[i] > peaksent) {
- peaksent = history_totals.sent[i];
- }
- if(history_totals.recv[i] + history_totals.sent[i] > peaktotal) {
- peaktotal = history_totals.recv[i] + history_totals.sent[i];
- }
- }
- for(i = 0; i < HISTORY_DIVISIONS; i++) {
- int t = history_length(i);
- totals.recv[i] /= t;
- totals.sent[i] /= t;
- }
-}
-
-void make_screen_list() {
- hash_node_type* n = NULL;
- while(hash_next_item(screen_hash, &n) == HASH_STATUS_OK) {
- host_pair_line* line = (host_pair_line*)n->rec;
- int i;
- for(i = 0; i < HISTORY_DIVISIONS; i++) {
- line->recv[i] /= history_length(i);
- line->sent[i] /= history_length(i);
- }
-
- /* Don't make a new, sorted screen list if order is frozen
- */
- if(!options.freezeorder) {
- sorted_list_insert(&screen_list, line);
- }
-
- }
-}
-
-/*
- * Zeros all data in the screen hash, but does not remove items.
- */
-void screen_hash_clear() {
- hash_node_type* n = NULL;
- while(hash_next_item(screen_hash, &n) == HASH_STATUS_OK) {
- host_pair_line* hpl = (host_pair_line*)n->rec;
- hpl->total_recv = hpl->total_sent = 0;
- memset(hpl->recv, 0, sizeof(hpl->recv));
- memset(hpl->sent, 0, sizeof(hpl->sent));
- }
-}
-
-void analyse_data() {
- hash_node_type* n = NULL;
-
- if(options.paused == 1) {
- return;
- }
-
- // Zero totals
- memset(&totals, 0, sizeof totals);
-
- if(options.freezeorder) {
- screen_hash_clear();
- }
- else {
- screen_list_clear();
- hash_delete_all(screen_hash);
- }
-
- while(hash_next_item(history, &n) == HASH_STATUS_OK) {
- history_type* d = (history_type*)n->rec;
- host_pair_line* screen_line;
- union {
- host_pair_line **h_p_l_pp;
- void **void_pp;
- } u_screen_line = { &screen_line };
- addr_pair ap;
- int i;
- int tsent, trecv;
- tsent = trecv = 0;
-
-
- ap = *(addr_pair*)n->key;
-
- /* Aggregate hosts, if required */
- if(options.aggregate_src) {
- memset(&ap.src6, '\0', sizeof(ap.src6));
- }
- if(options.aggregate_dest) {
- memset(&ap.dst6, '\0', sizeof(ap.dst6));
- }
-
- /* Aggregate ports, if required */
- if(options.showports == OPTION_PORTS_DEST || options.showports == OPTION_PORTS_OFF) {
- ap.src_port = 0;
- }
- if(options.showports == OPTION_PORTS_SRC || options.showports == OPTION_PORTS_OFF) {
- ap.dst_port = 0;
- }
- if(options.showports == OPTION_PORTS_OFF) {
- ap.protocol = 0;
- }
-
-
- if(hash_find(screen_hash, &ap, u_screen_line.void_pp) == HASH_STATUS_KEY_NOT_FOUND) {
- screen_line = xcalloc(1, sizeof *screen_line);
- hash_insert(screen_hash, &ap, screen_line);
- screen_line->ap = ap;
- }
-
- screen_line->total_sent += d->total_sent;
- screen_line->total_recv += d->total_recv;
-
- for(i = 0; i < HISTORY_LENGTH; i++) {
- int j;
- int ii = (HISTORY_LENGTH + history_pos - i) % HISTORY_LENGTH;
-
- for(j = 0; j < HISTORY_DIVISIONS; j++) {
- if(i < history_divs[j]) {
- screen_line->recv[j] += d->recv[ii];
- screen_line->sent[j] += d->sent[ii];
- }
- }
- }
-
- }
-
- make_screen_list();
-
-
- calculate_totals();
-
-}
-
-void sprint_host(char * line, int af, struct in6_addr* addr, unsigned int port, unsigned int protocol, int L) {
- char hostname[HOSTNAME_LENGTH];
- char service[HOSTNAME_LENGTH];
- char* s_name;
- union {
- char **ch_pp;
- void **void_pp;
- } u_s_name = { &s_name };
-
- ip_service skey;
- int left;
-
- if(IN6_IS_ADDR_UNSPECIFIED(addr)) {
- sprintf(hostname, " * ");
- }
- else {
- if (options.dnsresolution)
- resolve(af, addr, hostname, L);
- else
- inet_ntop(af, addr, hostname, sizeof(hostname));
- }
- left = strlen(hostname);
-
- if(port != 0) {
- skey.port = port;
- skey.protocol = protocol;
- if(options.portresolution && hash_find(service_hash, &skey, u_s_name.void_pp) == HASH_STATUS_OK) {
- snprintf(service, HOSTNAME_LENGTH, ":%s", s_name);
- }
- else {
- snprintf(service, HOSTNAME_LENGTH, ":%d", port);
- }
- }
- else {
- service[0] = '\0';
- }
-
-
- sprintf(line, "%-*s", L, hostname);
- if(left > (L - strlen(service))) {
- left = L - strlen(service);
- if(left < 0) {
- left = 0;
- }
- }
- sprintf(line + left, "%-*s", L-left, service);
-}
-
-