//////////////////////////////////////////////////////////////////////
// PPPTraf 1.0. (C) DiSKiLLeR, 9/2/2000. (diskiller@cnbinc.com)     //
//                                                                  //
// This program is distributed under the terms of the GNU License.  //
// Please refer to the file README and COPYING.                     //
//////////////////////////////////////////////////////////////////////

/*
 * I must say. This is the file i am most displeased with, and can
 * do with the most work. Ah well, next version. - diskiller
 */

#include <stdio.h>
#include <string.h>
#include <ncurses.h>

#include "data.h"
#include "bpf.h"

#define VERSION	"1.0"

void ntoascii(unsigned long n, char *str);

#define COLOR_BACKGRND	9
#define	COLOR_TITLEBAR	10
#define COLOR_STATUSBAR	11
#define COLOR_TEXT	12

static WINDOW *main;
static WINDOW *list;

int interface_init()
{
	char line[81];
	main = initscr();
	start_color();
	cbreak();
	noecho();
	halfdelay(10);

	list = newpad(100, 78);		// 100 from data.h

	keypad(list, TRUE);
	keypad(main, TRUE);

	init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
	init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
	init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
	init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
	init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
	init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
        init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
	init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);

	init_pair(COLOR_BACKGRND,  COLOR_WHITE, COLOR_BLUE);
	init_pair(COLOR_TITLEBAR,  COLOR_WHITE, COLOR_CYAN);
	init_pair(COLOR_STATUSBAR, COLOR_WHITE, COLOR_CYAN);
	init_pair(COLOR_TEXT,      COLOR_WHITE, COLOR_BLUE);

	wbkgd(main, ' ' | COLOR_PAIR(COLOR_BACKGRND));
	wbkgd(list, ' ' | COLOR_PAIR(COLOR_BACKGRND));

	// Title Bar ////
	wattron(main, COLOR_PAIR(COLOR_STATUSBAR) | A_BOLD);
	sprintf(line, "                                  PPPTraf v%s                                 ", VERSION);
	mvwaddstr(main, 0, 0, line);

	// Status Bar ////
	wattron(main, COLOR_PAIR(COLOR_STATUSBAR) | A_BOLD);
	mvwaddstr(main, 24, 0, "   Shift+Q = Quit      Shift+R = Reset      (C) diskiller@cnbinc.com, 25/1/2000  ");

	// Heading ////
	wattron(main, COLOR_PAIR(COLOR_TEXT));
	mvwaddstr(main, 02, 0, "  IP                Host                     Bytes in             Bytes out ");

	// Lines ////
	wattron(main, COLOR_PAIR(COLOR_TEXT));
	mvwaddstr(main, 03, 1, "------------------------------------------------------------------------------");
	mvwaddstr(main, 20, 1, "------------------------------------------------------------------------------");

	// Bottom stuff ////
	wattron(main, COLOR_PAIR(COLOR_TEXT));
	mvwaddstr(main, 21, 0, "                    Total");
	mvwaddstr(main, 22, 0, "                    Dropped");


	refresh();
	return;
}


int interface_loop(struct ahost *hostlist)
{
	int p=0;	// when scrolling, this is the list position.
	int n=0;	// index into loop
	int l=0;	// length of list. (not 100, but the number of items on there).
	int c;		// keyboard character
	unsigned long total_in = 0;
	unsigned long total_out = 0;
	unsigned long n_dropped;
	char bytesin[20];
	char bytesout[20];
	char dropped[20];

	for(;;)
	{
	l=0;
	total_in=0;
	total_out=0;

	for(n=0 ; n<=99 ; n++)
	{
		wattron(main, COLOR_PAIR(COLOR_TEXT) | A_BOLD);

		total_in += hostlist[n].in;
		total_out += hostlist[n].out;

		ntoascii(hostlist[n].in, bytesin);
		ntoascii(hostlist[n].out, bytesout);


		// end of list. Special handling.
		if(hostlist[n].ip[0] == '\0')
		{
			mvwaddstr(list, n, 1, "Other");
			mvwaddstr(list, n, 19, "-");
			mvwaddstr(list, n, 37, bytesin);
			mvwaddstr(list, n, 59, bytesout);

			break;
		}
		else
		{
			mvwaddstr(list, n, 1, hostlist[n].ip);
			mvwaddstr(list, n, 19, hostlist[n].host);
			mvwaddstr(list, n, 37, bytesin);
			mvwaddstr(list, n, 59, bytesout);

			l++;
		}
	}


	// Totals
	ntoascii(total_in, bytesin);
	ntoascii(total_out, bytesout);
	mvwaddstr(main, 21, 38, bytesin);
	mvwaddstr(main, 21, 60, bytesout);

	n_dropped = bpf_dropped();
	ntoascii(n_dropped, dropped);
	mvwaddstr(main, 22, 38, dropped);


	//refresh();
	prefresh(list, p, 0, 4, 1, 19, 78);

	// Leave FBSD cursor somewhere nice
	// (like off the bottom of the fucking screen! urgh, ugly block)
	move(26,0);

	//c = wgetch(list);
	c = getch();

	if(c == KEY_UP)
		if(p>=0)
			p--;

	if(c == KEY_DOWN)
		if(p<= (l-16))
			p++;

	if(c == 'R')
		data_reset();

	if(c == 'Q')
	{
		endwin();
		return;
	}
	}
}



void ntoascii(unsigned long n, char *str)
{
	int bytes;
	int kbytes;
	int mbytes;
	int gbytes;

	//printf("%d = ", n);

	if(n >= 1000000000)
	{
		gbytes = n / 1000000000;
		n = n % 1000000000;

		mbytes = n / 1000000;
		n = n % 1000000;

		kbytes = n / 1000;
		bytes = n % 1000;

		sprintf(str, "%3d,%.3d,%.3d,%.3d\n", gbytes, mbytes, kbytes, bytes);
		return;
	}

	if(n >= 1000000)
	{
		mbytes = n / 1000000;
		n = n % 1000000;

		kbytes = n / 1000;
		bytes = n % 1000;

		sprintf(str, "%7d,%.3d,%.3d\n", mbytes, kbytes, bytes);
		return;
	}

	if(n >= 1000)
	{
		kbytes = n / 1000;
		bytes = n % 1000;

		sprintf(str, "%11d,%.3d\n", kbytes, bytes);
		return;
	}

	if(n < 1000)
	{
		sprintf(str, "%15d\n", n);
		return;
	}
}


syntax highlighted by Code2HTML, v. 0.9.1