/*
	**
	** textfe.c 
	**
	** Text display frontend to network traffic visualiser
	**
	** Copyright 1998 Damien Miller <dmiller@ilogic.com.au>
	**
	** This software is licensed under the terms of the GNU General 
	** Public License (GPL). Please see the file COPYING for details.
	** 
	** $Id: textfe.c,v 1.8 1998/12/01 00:28:19 dmiller Exp $
	**
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "textfe.h"
#include "util.h"
#include "summary-sort.h"

/* Prototypes */
static void print_host(host_t *host, GHashTable *peers, unsigned int max_peers, int peer_sort_mode, int peer_reverse);
static void print_peers(GHashTable *peers, u_int64_t *peer_keys, unsigned int num_peers, unsigned int max_peers, int peer_sort_mode, int peer_reverse);
static void format_hostname(host_t *host, char *s, size_t s_len);
static const char *format_time(time_t t);

/* Displays summary of network traffic */
void text_display_summary(summary_t *p, unsigned int max_hosts, int host_sort_mode, int host_reverse, unsigned int max_peers, int peer_sort_mode, int peer_reverse)
{
	host_list_t		*sorted_hosts;
	host_list_t		*last_sh;
	
	setlinebuf(stdout);
	
	sorted_hosts = sort_hosts(p->hosts, max_hosts, host_sort_mode, host_reverse);

	printf("*** Collection started at %s\n", format_time(p->summary_start));
	printf("*** Collection concluded at %s\n", format_time(p->summary_finish));
	printf("\n");

	while(sorted_hosts != NULL)
	{
		last_sh = sorted_hosts;
		print_host(sorted_hosts->host, p->peers, max_peers, peer_sort_mode, peer_reverse);
		sorted_hosts = sorted_hosts->next;
		free(last_sh);
	}

	printf("Total active hosts:        %d\n", p->active_hosts);
	printf("Total bytes:               %qd\n", p->total_bytes);
	printf("Total packets:             %qd\n", p->total_packets);
	printf("Total connection requests: %qd\n", p->total_connections);
	printf("\n");
}

static void print_host(host_t *host, GHashTable *peers, unsigned int max_peers, int peer_sort_mode, int peer_reverse)
{
	struct in_addr i;

	i.s_addr = htonl(host->ip_addr);
	
	if (host->hostname != NULL)
		printf("%s (%s)\n", host->hostname, inet_ntoa(i));
	else	
		printf("Unknown (%s)\n", inet_ntoa(i));

	if (host->n_peers != 0)
		print_peers(peers, host->peer_keys, host->n_peers, max_peers, 
						peer_sort_mode, peer_reverse);
	
	printf(" [ %Lu bytes sent, %Lu bytes received ]\n", host->bytes_sent, host->bytes_received);
	printf(" [ %Lu packets sent, %Lu packets received ]\n", host->packets_sent, host->packets_received);
	printf(" [ %Lu connection requests sent, %Lu connection requests received ]\n", host->connections_sent, host->connections_received);
	printf(" [ First traffic recorded at %s ]\n", format_time(host->first_seen));
	printf(" [ Last traffic recorded at %s ]\n", format_time(host->last_seen));

	printf("\n");
}

static void print_peers(GHashTable *peers, u_int64_t *peer_keys, unsigned int num_peers, unsigned int max_peers, int peer_sort_mode, int peer_reverse)
{
	peer_list_t	*peer_list;
	peer_list_t	*last_pl;
	char			src_hostname[1024];
	char			dst_hostname[1024];
	
	peer_list = sort_peers(peer_keys, num_peers, peers, max_peers, 
								  peer_sort_mode, peer_reverse);

	while(peer_list != NULL)
	{
		format_hostname(peer_list->peer->host1, src_hostname, sizeof(src_hostname));
		format_hostname(peer_list->peer->host2, dst_hostname, sizeof(dst_hostname));
		
		printf("\t%s -> %s (%Lu bytes, %Lu packets)\n", 
				 src_hostname, dst_hostname,
				 peer_list->peer->bytes_sent_1_to_2, 
				 peer_list->peer->packets_sent_1_to_2);
		
		last_pl = peer_list;
		peer_list = peer_list->next;
		free(last_pl->peer);
		free(last_pl);
	}
}

static void format_hostname(host_t *host, char *s, size_t s_len)
{
	static struct	in_addr i;

	i.s_addr = htonl(host->ip_addr);
	
	if (host->hostname != NULL)
		snprintf(s, s_len, "%s", host->hostname);
	else	
		snprintf(s, s_len, "Unknown (%s)", inet_ntoa(i));
}

static const char *format_time(time_t t)
{
	static char	ts[128];
	struct tm	*tm;
	
	tm = localtime(&t);
	
	strftime(ts, sizeof(ts), "%a, %d %b %Y %H:%M:%S %Z", tm);
	
	return(ts);
}
