//////////////////////////////////////////////////////////////////////
// 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.                     //
//////////////////////////////////////////////////////////////////////

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

#include "data.h"

static struct ahost host[100];
static pthread_mutex_t mut;

void data_init()
{
	int n;
	FILE *fp;
	char ip[20];
	char hostname[20];

	fp = fopen("/usr/local/etc/ppptraf.conf", "r");

	if(fp==NULL)
	{
		fprintf(stderr, "Fatal Error: Cannot open ppptraf.conf\n");
		exit(1);
	}

	for(n=0 ; n<=99 ; n++)
	{
		host[n].ip[0] = '\0';
		host[n].host[0] = '\0';
		host[n].in = 0;
		host[n].out = 0;
	}

	n=0;
	while( fscanf(fp, "%s %s", ip, hostname) != EOF )
	{
		//printf("%s - %s\n", ip, hostname);
		strcpy(host[n].ip, ip);
		strcpy(host[n].host, hostname);

		n++;
	}

	fclose(fp);
}

int data_store(char *src, char *dest, int size)
{
	int n;
	char *t;
	pthread_mutex_lock(&mut);

	for(n=0 ; n<=99 ; n++)
	{
		/** 'Other' **/
		if(host[n].ip[0] == '\0')
		{
			host[n].in += size;
			host[n].out += size;
			break;
		}

		/** sending data **/
		if(strcmp(host[n].ip, src) == 0)
		{
			host[n].out += size;
			break;
		}

		/** recieving data **/
		if(strcmp(host[n].ip, dest) == 0)
		{
			host[n].in += size;
			break;
		}
	}

	pthread_mutex_unlock(&mut);
}

struct ahost *data_get()
{
	return host;
}

void data_reset()
{
	int n;

	pthread_mutex_lock(&mut);

	for(n=0 ; n<=99 ; n++)
	{
		host[n].in = 0;
		host[n].out = 0;
	}

	pthread_mutex_unlock(&mut);
}


syntax highlighted by Code2HTML, v. 0.9.1