<?php

function pfxlist_generate($format, $asn, $pfxname, $pfxlen)
{
	global $cfg;

	$file = $cfg['paths']['db'] . $asn . $cfg['aggregate']['suffix'];

	if (!file_exists($file) || !is_readable($file)) {
		status(STATUS_ERROR, "Cannot open IRRDB prefix list file {$file}");
		return FALSE;
	}

	if (!($pfxfile = fopen($file, "r"))) {
		status(STATUS_ERROR, "Cannot open IRRDB prefix list file {$file}");
		return FALSE;
	}

	switch ($format) {
	    case 'cisco':
		pfxlist_generate_cisco($pfxfile, $asn, $pfxname, $pfxlen);
		break;
	    case 'juniper':
		pfxlist_generate_juniper($pfxfile, $asn, $pfxname, $pfxlen);
		break;
	    default:
		break;
	}

	fclose($pfxfile);
}

function pfxlist_generate_juniper($pfxfile, $asn, $pfxname, $pfxlen)
{
	global $cfg;
	$pfxname .= $asn;

	printf("policy-options {\n");
	printf("    replace: policy-statement %s {\n", $pfxname);
	printf("        term prefixes {\n");
	printf("            from {\n");

	while (!feof($pfxfile)) {
		if (!($line = rtrim(fgets($pfxfile, 64))))
			continue;

		printf("                route-filter %s upto /%d;\n", 
			$line, $pfxlen);
	}

	printf("            }\n");
	printf("            then next policy;\n");
	printf("        }\n");
	printf("        then reject;\n");
	printf("    }\n");
	printf("}\n");

	return 0;
}

function pfxlist_generate_cisco($pfxfile, $asn, $pfxname, $pfxlen)
{
	global $cfg;
	$pfxname .= $asn;

	printf("conf t\n");
	printf("no ip prefix-list %s\n", $pfxname);

	while (!feof($pfxfile)) {
		if (!($line = rtrim(fgets($pfxfile, 64))))
			continue;

		$prefix = explode("/", $line);

	        printf("ip prefix-list %s permit %s/%d", $pfxname, $prefix[0],
			$prefix[1]);

		if ($prefix[1] < $pfxlen)
			printf(" le %s\n", $pfxlen);
		else    
			printf("\n");
	}

	printf("end\n");
	printf("write mem\n");

	return 0;
}


?>
