/*
Copyright (c) 2000                      RIPE NCC


All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of the author not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

-------------------------------------------------------------------------------
Module Header
Filename          : testas.c
Purpose           : Read RVEC file and report as paths found for IP routes
Author            : Rene Wilhelm
Date              : 20020812
Description       : 
Language Version  : gcc version 2.95.2
OSs Tested        : Solaris 2.6
Command Line      : testas path
Input Files       : /ncc/ttpro/raw_data/
Output Files      :
Comments          : 
$Id: testas.c,v 1.3 2004/03/10 18:04:33 wilhelm Exp $
-------------------------------------------------------------------------------
*/

#include <netdb.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>

#include "common.h"
#include "read_line.h"
#include "digest.h"
#include "as.h"

/*
-------------------------------------------------------------------------------
Purpose           : The important function: process data from an RVEC file,
Params            : filename
Returns           : Number of lines processed & verbose output on errors
Comments          :
*/
void process_file (char* filename, GHashTable* asnums, int ip6flag) {

	gzFile fdz;
	FILE *fd;

	unsigned int src, dst, tt, len, npoints, gz;

	int t;

	char buf[4096];
	char bufcopy[4096];
	char ipvec[(MAX_IP_SIZE+1)*MAX_HOPS]; /* only extreme scenario fills this */
        char vector_ip6[(MAX_IP6_SIZE+1)*MAX_HOPS];

	char aspath[MAX_IP_SIZE*MAX_HOPS*2]; /* only extreme scenario fills this */

	int nlines=0;
	int dst_reached=0;
	int dst_notreached=0;

	char *p, *link;
	char *src_ip6, *dst_ip6;


	/* Allocate memory to store fullrec, empty it */

	gz = 0;


	fd = fdz = NULL;

	/* Check if filename starts with RVEC */
	link = strrchr(filename,'/');
	if (link == NULL)  return;
	if (strncmp(link,"/RVEC",5) != 0) return;
	
	
	/* Check if filename ends with gz */
	p = strrchr(filename, '.');
	if (p && strcmp(p+1,"gz") == 0) gz = 1;
	    
	if(gz)
	{
		/* use gzopen */
		if((fdz = gzopen(filename, "rb")) == NULL)
		{
			fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
			return;
		};
	}
	else
	{
		/* use regular open */
		if((fd = fopen(filename,"rb")) == NULL)
		{
			fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
			return;
		};
	};

	
	/* read file */
	while((gz && !gzeof(fdz)) || (!gz && !feof(fd))) {

usleep(0.2);
		if(gz) {
			if(gzgets(fdz, buf, sizeof(buf)) == Z_NULL) continue;
		}
		else {
			if(fgets(buf, sizeof(buf), fd) == NULL) continue;
		}
	
		/* copy line buffer, since read_line will modify it */
		memccpy(bufcopy, buf, '\0', sizeof(buf));
		

		fprintf(stderr, "reading line %d ...\n", nlines);

                /* Parse the line */
		if (!ip6flag) {
                	t = read_line(buf,&src,&dst,&tt,&npoints,ipvec,aspath,asnums);
                } else {
                        t = read_line_ip6(buf,&src_ip6,&dst_ip6,&tt,&npoints,vector_ip6,aspath);
		}

		nlines++;
                /* Check if function returned 0 */
                if (t > 0) {
			fprintf(stderr, "Error decoding line: error code %d:\n%s\n",t,bufcopy);
                        continue;
                }  
                else if (t == 0) {
                        dst_reached++;
                }
                else {
                        dst_notreached++;
		}

		fprintf(stderr, bufcopy);
		fprintf(stderr, "path %s\n\n", aspath);
		fprintf(stderr, "sleep 0.5 sec ...\n");
		usleep(500);
	}

	fprintf(stderr, "%d measurements, %d correct, %d reached target\n",
		nlines, dst_reached+dst_notreached, dst_reached);

	/* Close file, report errors */
	if (gz) gzclose(fdz);
	else fclose (fd);

	return;
}

/*
-------------------------------------------------------------------------------
Purpose           : Main function - Initialize and process files
Params            : filename containing the RVEC files
Returns           : 0
Comments          :
*/

int main(int argc, char *argv[]) {
	char *path;
	GHashTable *asnums;


	/* Check arguments */
	if (argc < 2) 
	{ 
		fprintf (stderr,"Usage: %s <filename>\n",argv[0]); 
		exit (1); 
	};

	/* Initialization: open connection, read routes and ranges */

        RRopen(RR_SERVER, RR_SERVICE);
        RRconnect();


        asnums  = g_hash_table_new (g_int_hash,g_int_equal);


	path = argv[1];

	if (strstr(path, "RVEC6") == NULL) {
	        process_file (path, asnums, 0);
	}
	else {
	        process_file (path, asnums, 1);
	}

	exit (0);
};



