//
// $Id: ipfw-core.c,v 1.9 2001/11/22 13:29:04 mavetju Exp $
//

//
// The core functions of ipfw-graph
//

#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ipfw-graph.h"

DATA_TYPE data_bytes;		// data for the bytes received
DATA_TYPE *data=NULL;		// data for the ipfw rules
int	data_size;		// number of elements of data

int	xsize;			// xsize of the output
int	ysize_bytes,ysize_drawing;	// ysize of the bytes/piles output
long	maxyvalues[MAXSTORED];	// maxyvalues per column in the output
long	maxyvalue=-1;		// biggest of the maxyvalues

// tables with pointers to the drawing functions
struct funtable drawtable[]={
    {	linear_drawing,
	linear_bytes,
	default_stats,
	bg_drawing,
	bg_bytes,
	"Linear drawing",
	"All",
	0,
	NULL,
	NULL
    },

    {	denyonly_drawing,
	denyonly_bytes,
	default_stats,
	bg_drawing,
	bg_bytes,
	"Deny-only drawing",
	"Deny",
	0,
	NULL,
	NULL
    },

    {	allowonly_drawing,
	allowonly_bytes,
	default_stats,
	bg_drawing,
	bg_bytes,
	"Allow-only drawing",
	"Allow",
	0,
	NULL,
	NULL
    },

    {	NULL,				NULL,				
	NULL
    }
};


//
// periodic called function to redraw the screen
//
gint update(void) {
    debug("updating data (current page is %d)",current_page);
    getdata();
    (*drawtable[current_page].bgdrawing_fun)();
    (*drawtable[current_page].drawing_fun)();
    (*drawtable[current_page].bgbytes_fun)();
    (*drawtable[current_page].bytes_fun)();
    (*drawtable[current_page].stats_fun)();

    gtk_widget_queue_draw(drawtable[current_page].area);
    gtk_widget_queue_draw(bytes_area);

    return 1;
}

//
// Read the config from ipfw
//
void get_config(void) {
    FILE *	fin;
    char	s[1000];
    char *	p;
    int		x;
    char *	ipfwal=BINDIR"/ipfw-al defs";

    if ((fin=popen(ipfwal,"r"))==NULL) {
	perror("ipfw-al defs");
	exit(0);
    }

    while (!feof(fin)) {
	if (fgets(s,sizeof(s),fin)==NULL)
	    break;
	if (s[0]==0) continue;
	if (s[0]=='#') break;

	s[strlen(s)-1]=0;

	data=(DATA_TYPE *)realloc(data,(data_size+1)*sizeof(DATA_TYPE));

	data[data_size].new=0;
	data[data_size].old=0;
	data[data_size].gc=NULL;
	for (x=0;x<MAXSTORED;x++)
	    data[data_size].table[x]=0;

	if ((p=strchr(s,' '))==NULL) {
	    perror(s);
	    exit(0);
	}
	*p=0;
	p++;
	data[data_size].number=strdup(s);
	data[data_size].rule=strdup(p);

	if (strncmp(data[data_size].rule,"allow",5)==0)
	    data[data_size].allow=TRUE;
	if (strncmp(data[data_size].rule,"deny",4)==0)
	    data[data_size].deny=TRUE;

	data_size++;
    }
    pclose(fin);
}

int main(int argc, char *argv[]) {
    int i;
    int incr;

    init_gtk(&argc,&argv);

    for (i=0;i<MAXSTORED;i++) maxyvalues[i]=1;

    data=NULL;
    data_bytes.new=0;
    data_bytes.old=0;
    data_bytes.rule=NULL;
    data_bytes.number=NULL;

    data_size=0;
    get_config();

    incr=0x7fff/data_size;
    data_bytes.gc=gc_white;
    for (i=0;i<data_size;i++) {
	if (strncmp(data[i].rule,"deny",5)==0)
	    data[i].gc=GetPen(NewColour(0,0,0x4000+i*incr));
	else if (strncmp(data[i].rule,"allow",5)==0)
	    data[i].gc=GetPen(NewColour(0,0x4000+i*incr,0));
	else
	    data[i].gc=GetPen(NewColour(0x4000+i*incr,0,0));
    }

    gtk_timeout_add(1000,(GtkFunction)update,NULL);

    gtk_main();

    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1