#### ASpath-tree v.4.2 - Released on Thu APR 17 2003, h.16:58:12
#### File: lib/bgp-tree.pl Last modified on Fri APR 11 2003, h.23:56:04

# Routine to create the current tree table (input: type of tree -whole (0), bb (1), otherIANA (2)-,
# output: array containing HTML formatted table cells)
sub create_output_table {
	my ($tree_type) = @_;

	my @sim = ("<IMG SRC=\"$ICONS/e.gif\">",
                   "<IMG SRC=\"$ICONS/v.gif\">",
                   "<IMG SRC=\"$ICONS/h.gif\">",
                   "<IMG SRC=\"$ICONS/l.gif\">",
                   "<IMG SRC=\"$ICONS/t.gif\">",
                   "");
	my @asn_1 = ();
	my @asn_2 = ();
	my @bar = ();
	my @cell = ();
	my @f_table = ();
	my ($key, $filename, $flag_tmp, $flag, $num_as, $i, $j, $ncol, $n, $asn_toprint);
	my @keys = ();
	my $history_reldir = substr($HISTORYHTMLDIR, length($HTMLDIR_LOCAL) + 1, length($HISTORYHTMLDIR) - length($HTMLDIR_LOCAL));

# Set and sort ASpaths array for $tree_type
	if ($tree_type != 0) {
		foreach $key (keys(%TREE_ROWS)) {
			$TREE_ROWS{$key} =~ /(\S+)\s+(\S+)\s+(\S+)/;
			$flag_tmp = $1;
			$num_as = $2;
			if ($flag_tmp =~ /$tree_type/) {
				push(@keys, $key);
				if ($ncol < $num_as) {$ncol = $num_as}
			}
		}
	}
	else {
		@keys = (keys(%TREE_ROWS));
		$ncol = length($keys[0])/5;
	}
	@keys = sort(@keys);
	$ncol--;
	$n = $ncol*2+1;

	unless (@keys) {return(())};
	for ($j = $#keys; $j >= 0; $j--) {
		@asn_1 = &convert_asn($keys[$j]);
		if ($j > 0) {@asn_2 = &convert_asn($keys[$j-1])}
		else {@asn_2 = ()}
		$n = $ncol*2+1;
		for ($i = $ncol; $i >= 0; $i--) {
			if (!defined($asn_1[$i])) {$n = $n-2}
			elsif (join("", @asn_1[(0)..($i)]) ne join("", @asn_2[(0)..($i)])) {
			        $asn_toprint = substr($ASNAME{$asn_1[$i]}, 0, 11);
			        $asn_toprint =~ s/-/\&nbsp\;/g;
				if ($i eq $#asn_1) {
					$colspan = $ncol*2 - $n + 1;
					if ($colspan > 0) {$cell[$n+1] = "<TD COLSPAN=$colspan></TD>"}
					$TREE_ROWS{$keys[$j]} =~ /(\S+)\s+(\S+)\s+(\S+)/;
					$flag_tmp = $1;
					$filename = $3;
					$flag = '';
					if ($tree_type == 0) {$asn_toprint = "<TD><A HREF=\"$LINKDIR\/$filename\">$asn_toprint</A>"}
					elsif ($tree_type == 1) {$asn_toprint ="<TD><A HREF=\"$LINKDIRBB\/$filename\">$asn_toprint</A>"}
					elsif ($tree_type == 3) {$asn_toprint = "<TD><A HREF=\"$LINKDIRIANA\/$filename\">$asn_toprint</A>"}
					if (($tree_type == 0) && ($flag_tmp =~ /[025]/)) {$flag .= "O"}
					if ($flag_tmp =~ /[u]/) {$flag .= "U"}
					if ($flag) {$asn_toprint .= "<SPAN CLASS=r10><NOBR>&nbsp;!$flag!</SPAN></TD>"}
					else {$asn_toprint .= "</TD>"}
					$cell[$n--] = $asn_toprint;
				}
				else {
					$cell[$n--] = "<TD>$asn_toprint</TD>";
				}
				if (join("", @asn_1[(0)..($i-1)]) eq join("", @asn_2[(0)..($i-1)])) {
					if (!defined($asn_2[$i])) {
						if ($bar[$i]) {
							$cell[$n--] = "<TD>".$sim[0]."</TD>";
							$bar[$i] = "";
						}
						else {$cell[$n--] = "<TD>".$sim[2]."</TD>"}
					}
					else {
						if ($bar[$i]) {$cell[$n--] = "<TD>".$sim[4]."</TD>"}
						else {
							$cell[$n--] = "<TD>".$sim[3]."</TD>";
							$bar[$i] = 1;
						}
					}
				}
				elsif ($bar[$i]) {
					$cell[$n--] = "<TD>".$sim[0]."</TD>";
					$bar[$i] = "";
				}
				else {$cell[$n--] = "<TD>".$sim[2]."</TD>"}
			}
			elsif (!defined($asn_2[$i+1])) {
				$cell[$n--] = "<TD ALIGN=right>".$sim[3]."</TD>";
				if ($bar[$i]) {$cell[$n--] = "<TD>".$sim[1]."</TD>"}
				else {$cell[$n--] = "<TD>".$sim[5]."</TD>"}
			}
			else {
				$cell[$n--] = "<TD>".$sim[5]."</TD>";
				if ($bar[$i]) {$cell[$n--] = "<TD>".$sim[1]."</TD>"}
				else {$cell[$n--] = "<TD>".$sim[5]."</TD>"}
			}
		} #end for columns
		$f_table[$j] = join("", @cell);
		@cell = ();
	} #end for rows
	return(@f_table);
}

# Routine to generate HTML files for current tree tables
sub create_html_tree {
	my ($tree_type, $time_change) = @_;

	my ($output_file, $title, $img_src, $lastchange);
	my $i = 0;
	my @tree = ();

	my $history_reldir = substr($HISTORYHTMLDIR, length($HTMLDIR_LOCAL) + 1, length($HISTORYHTMLDIR) - length($HTMLDIR_LOCAL));
	my $printable_date = &convert_date($time_change);

	if ($tree_type == 0) { # complete
		$output_file = $OUTPUTCOMPLETE;
		$title = "The whole IPv6 BGP table";
	}
	elsif ($tree_type == 1) { # backbone
		$output_file = $OUTPUTBB;
		$title = "IPv6 BGP table: the backbone";
	}
	elsif ($tree_type == 3) { # otherIANA
		$output_file = $OUTPUTIANA;
		$title = "IPv6 BGP table: &quot;2001&quot; routes";
	}

	open(HTMLFILE,">".$output_file);

	print HTMLFILE &header($title, 1);

	print HTMLFILE "<TABLE>\n<TR>";
	if ($SITEPICTURE) {
		print HTMLFILE "<TD VALIGN=top ROWSPAN=3><IMG SRC=\"$ICONS/$SITEPICTURE\" BORDER=0></TD>\n";
	}
	print HTMLFILE "<TD VALIGN=bottom ALIGN=center CLASS=a32b>$title</TD></TR>\n";
	print HTMLFILE "<TR><TD ALIGN=center VALIGN=top CLASS=n10b>Updated on $LONGDATE</TD></TR>\n";
	print HTMLFILE "<TR><TD>&nbsp;</TD></TR>\n";

	print HTMLFILE "</TABLE>\n";
	print HTMLFILE "<P>\n";
	print HTMLFILE "<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>\n";
	if ($ROOTPICTURE) {$img_src = "<IMG SRC=\"$ICONS/$ROOTPICTURE\" BORDER=0>"}
	else {$img_src = $SITENAME}
	if (-e $DETAILFILE) {$img_src =  "<A HREF=\"details/local.html\">".$img_src."</A>"}
	$img_src .= "&nbsp;";
	@tree = &create_output_table($tree_type);

	print HTMLFILE "<TR ALIGN=center VALIGN=bottom NOWRAP><TD CLASS=a16b>$img_src</TD>$tree[0]</TR>\n";
	for ($i = 1; $i < @tree; $i++) {
		print HTMLFILE "<TR ALIGN=center VALIGN=bottom NOWRAP><TD></TD>$tree[$i]</TR>\n";
	}
	print HTMLFILE "</TABLE>\n<HR>\n";

# The whole IPv6 BGP table
	if ($tree_type == 0) {
		if ($time_change) {
			if ($CHANGESFLAG && -e $OUTPUTCOMPLETECHANGES) {$lastchange = "<A HREF=\"changes/bgp-page-complete-changes.html\" CLASS=a12b>Last change</A>"}
			else {$lastchange = "Last change"}
			print HTMLFILE "		<SPAN CLASS=n12>$lastchange on the tree detected on $printable_date</SPAN><BR><HR>\n";
		}
		print HTMLFILE "
		<SPAN CLASS=n10>The IPv6 BGP routing tree is obtained elaborating
		the AS path information of the IPv6 BGP routing table.
		A link to detailed routing information is available at the end of each branch
		of the tree.<BR>
		Warnings (<SPAN CLASS=r10>!</SPAN>) are raised for odd and unstable routes:
		<UL>
		<LI><SPAN CLASS=r10>U</SPAN> raises a warning that a route entry is Unstable;\n
		<LI><SPAN CLASS=r10>O</SPAN> raises a warning that there are Odd route entries
		(invalid or unaggregated prefixes).</UL>\n
		";
	}
# The 6bone backbone IPv6 BGP table
	elsif ($tree_type == 1) {
		if ($time_change) {
			if ($CHANGESFLAG && -e $OUTPUTBBCHANGES) {$lastchange = "<A HREF=\"changes/bgp-page-backbone-changes.html\" CLASS=a12b>Last change</A>"}
			else {$lastchange = "Last change"}
			print HTMLFILE "		<SPAN CLASS=n12>$lastchange on the tree detected on $printable_date</SPAN><HR>\n";
		}
		if (@NRPTLAS) {
			$i = 0;
			print HTMLFILE "<SPAN CLASS=n12>We are not receiving any routing information for ";
			print HTMLFILE "the following pTLAs:</SPAN><BR>\n<SPAN CLASS=n10>\n";
			for (sort(@NRPTLAS)) {
				print HTMLFILE "<A HREF=\"./$history_reldir/$_.html\">$_</A>";
				if ($i < (@NRPTLAS-1)) {print HTMLFILE "<TT><B>, </B></TT>"}
				$i++;
			}
			print HTMLFILE "</SPAN>\n<HR>\n\n";
		}
		print HTMLFILE "
		<SPAN CLASS=n10>
		The IPv6 BGP routing tree for the 6bone backbone is obtained elaborating
		the AS path information of the IPv6 BGP routing table.<BR>
		A link to detailed routing information is available at the end of each branch of the
		tree.<BR>
		Warnings (<SPAN CLASS=r10>!U!</SPAN>) are raised for unstable routes.\n
		";
	}
# The other IANA IPv6 BGP table
	elsif ($tree_type == 3) {
		if ($time_change) {
			if ($CHANGESFLAG && -e $OUTPUTIANACHANGES) {$lastchange = "<A HREF=\"changes/bgp-page-otherIANAchanges.html\" CLASS=a12b>Last change</A>"}
			else {$lastchange = "Last change"}
			print HTMLFILE "		<SPAN CLASS=n12>$lastchange on the tree detected on $printable_date</SPAN><BR><HR>\n";
		}
		print HTMLFILE "
		<SPAN CLASS=n10>The IPv6 BGP routing tree for the &quot;2001&quot; routes (route entries containing prefixes assigned by the RIRs
                within the 2001::/16 address space and prefix length in range [29,35]) is obtained elaborating the AS path
		information of the IPv6 BGP routing table.<BR>
		A link to detailed routing information is available at the end of each branch
		of the tree.<BR>
		Warnings (<SPAN CLASS=r10>!U!</SPAN>) are raised for unstable routes.\n
		";
	}

	print HTMLFILE "
	<HR>\n".&footer."
	</SPAN>\n</BODY>\n</HTML>\n";

	close(HTMLFILE);

	$output_file =~ m!([^/]*)$!;
	&log_it("    $1 created", 1);
	return(0);
}

# Routine to create a html detail file
sub create_html_detailfile {
	my ($filename, $aspath, $vector_index, $hints_index, $prepend) = @_;
	my @vector = @$vector_index;
	my %hints = %$hints_index;
	my @item = ();
	my ($ncol, $line, $key, $prefix, $origin_as, $owner, $xtla_site, $timestring, $timecolor, $updates);
	my ($detailslink, $status, $fal_whois, $prepended_as, $as_number);
	my $history_reldir = substr($HISTORYHTMLDIR, length($HTMLDIR_LOCAL) + 1, length($HISTORYHTMLDIR) - length($HTMLDIR_LOCAL));

	open(HTMLDETAILS,">".$filename);

	print HTMLDETAILS &header("Routing entry detail: $LONGDATE", 0);

	print HTMLDETAILS "<SPAN CLASS=a16b>AS Path:</SPAN><BR>\n";
	print HTMLDETAILS "<SPAN CLASS=n12>$aspath</SPAN><P>\n";

	print HTMLDETAILS "<SPAN CLASS=a16b>Advertised prefixes:</SPAN><BR>\n";
	print HTMLDETAILS "<P><TABLE BORDER=0 CELLPADDING=1 CELLSPACING=0>\n";
	print HTMLDETAILS "<TR>\n";
	print HTMLDETAILS "<TD ALIGN=center CLASS=n10b>&nbsp;Status&nbsp;</SPAN></TD>\n";
	print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
	print HTMLDETAILS "<TD ALIGN=center CLASS=n10b>&nbsp;Destination prefix&nbsp;</SPAN></TD>\n";
	print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
	print HTMLDETAILS "<TD ALIGN=center CLASS=n10b>&nbsp;Origin&nbsp;</TD>\n";
	if (defined($prepend)) {
		print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
		print HTMLDETAILS "<TD ALIGN=center CLASS=n10b>&nbsp;Prepended AS&nbsp;<BR>(times)</TD>\n";
		$ncol = 15;
	} else {$ncol = 13}
	print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
	print HTMLDETAILS "<TD ALIGN=center CLASS=n10b>&nbsp;Assigned to&nbsp;</TD>\n";
	print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
	print HTMLDETAILS "<TD ALIGN=center CLASS=n10b>&nbsp;xTLA owner&nbsp;</TD>\n";
	print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
	print HTMLDETAILS "<TD ALIGN=center CLASS=n10b>Time&nbsp;(updates)<BR>&nbsp;since last change&nbsp;</TD>\n";
	print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
	print HTMLDETAILS "<TD ALIGN=center CLASS=n10b>&nbsp;Details</TD>\n";
	print HTMLDETAILS "</TR>\n";

	print HTMLDETAILS "<TR>\n";
	print HTMLDETAILS "<TD COLSPAN=$ncol BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
	print HTMLDETAILS "</TR>\n";
	foreach $line (@vector) {
		@item = split(/\s+/, $line);
		$prefix = $item[0];
		if ($item[1] eq '?' || $item[1] eq '?') {$owner = '?'}
		else {
			if (&includedprefix($prefix,"3ffe::/16")) {$flag_whois = 0}
			else {$flag_whois = 1}
			$owner = &format_owner($item[1],$flag_whois);
		}
		$timestring = &convert_sincelastchange_time($item[2]);
		$timestring =~ s/\s/\&nbsp\;/g;
		if (int($item[2]/60) <= $WARNINGTIME) {$timecolor = "\"#FF0000\""}
		else {$timecolor = "\"#000000\""}
		$updates = $item[3];
		if ($item[4] ne 'n/a') {$detailslink = "<A HREF=\"../../$history_reldir/$item[4].html\">view</A>"}
		else {$detailslink = $item[4]}
		if ($item[5] eq '?' || $item[5] eq '?') {$xtla_site = '?'}
		else {$xtla_site = &format_owner($item[5],$flag_whois)}
		$origin_as = &format_originAS($item[6]);
		if  (substr($ROUTE_FLAGS{$prefix}, 4, 1) eq '?') {$origin_as .= '&nbsp;[incomplete]'}
		elsif  (substr($ROUTE_FLAGS{$prefix}, 4, 1) eq 'i') {$origin_as .= '&nbsp;[IGP]'}
		elsif  (substr($ROUTE_FLAGS{$prefix}, 4, 1) eq 'e') {$origin_as .= '&nbsp;[EGP]'}

		print HTMLDETAILS "<TR>\n";
		print HTMLDETAILS "<TD CLASS=t12 ALIGN=center>".substr($ROUTE_FLAGS{$prefix}, 0, 2)."&nbsp;</TD>\n";
		print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
		print HTMLDETAILS "<TD CLASS=t12>$prefix&nbsp;</TD>\n";
		print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
		print HTMLDETAILS "<TD CLASS=t12>&nbsp;$origin_as&nbsp;</TD>\n";
		if ($ncol == 15) {
			print HTMLDETAILS "</TD>\n<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
			print HTMLDETAILS "<TD CLASS=t10>";
			@item = grep(/\S+/,(split(/\s+/, $$prepend{$prefix})));
			foreach my $element (@item) {
				$element =~ /(\d+)\#(\d+)/;
				$prepended_as =  &format_originAS($1);
				print HTMLDETAILS "$prepended_as\&nbsp;($2)";
				unless ($element eq $item[$#item]) {print HTMLDETAILS ",\&nbsp;"}
			}
		}
		print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
		print HTMLDETAILS "<TD CLASS=t12>&nbsp;$owner&nbsp;</TD>\n";
		print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
		print HTMLDETAILS "<TD CLASS=t12>&nbsp;$xtla_site&nbsp;</TD>\n";
		print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
		print HTMLDETAILS "<TD CLASS=t12>&nbsp;<FONT COLOR=$timecolor>$timestring&nbsp;($updates)</FONT>&nbsp;</TD>\n";
		print HTMLDETAILS "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../../$ICONS/p.gif\"></TD>\n";
		print HTMLDETAILS "<TD CLASS=t12>&nbsp;$detailslink</TD>\n";
		print HTMLDETAILS "</TR>\n";
	}
	print HTMLDETAILS "</TABLE>\n<P>\n";

	print HTMLDETAILS "<SPAN CLASS=n10>Status Codes:<BR>\n";
	print HTMLDETAILS "$STATUSCODES</SPAN><P>\n";

	if (%hints) {
		print HTMLDETAILS "<SPAN CLASS=a16b>Hints:</SPAN><BR>\n";
		print HTMLDETAILS "<SPAN CLASS=n10>\n";
		foreach $key (keys(%hints)) {
			$as_number =  &format_originAS($key);
			print HTMLDETAILS "$as_number: $hints{$key}<BR>\n";
		}
		print HTMLDETAILS "</SPAN>\n";
	}

	print HTMLDETAILS "
	<P><HR>\n
	<SPAN CLASS=n10>".&footer."
	</SPAN>\n</BODY>\n</HTML>\n";

	close(HTMLDETAILS);
	return(1);
}

(1);
#### ASpath-tree v.4.2 - Released on Thu APR 17 2003, h.16:58:12
#### File: lib/bgp-tree.pl Last modified on Fri APR 11 2003, h.23:56:04
