summaryrefslogtreecommitdiffstats
path: root/make_ip_del_recovered.pl
blob: 8cc3a3da40398e2dc4d78a7dc3cdf515c38e9ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl
# https://www.iana.org/assignments/ipv4-recovered-address-space/ipv4-recovered-address-space-2.csv

use warnings;
use strict;
use autodie;

use Text::CSV;
use Net::Patricia;
use Net::CIDR;
use Net::IP;

my $csv = Text::CSV->new;
my $pt = parse_ip_del('ip_del_list');

open(my $in, '<', 'ipv4-recovered-address-space-2.csv');
open(my $out, '>', 'ip_del_recovered.h');

while (my $row = $csv->getline($in)) {
	next if $row->[0] eq 'Start address';
	next if $row->[5] ne 'ALLOCATED';
	my ($first_ip, $last_ip, undef, undef, $server) = @$row;

	my @networks =
		grep {
			my $server_recovered = $pt->match_string($_->ip);
			$server_recovered and $server_recovered ne $server;
		}
		map { Net::IP->new($_) }
		Net::CIDR::range2cidr($first_ip . '-' . $last_ip);
	next if not @networks;

	print $out "/* $first_ip - $last_ip */\n";
	print $out sprintf(qq|{ %sUL, %sUL, "%s" },\n|,
		$_->intip,
		((~(0xffffffff >> $_->prefixlen)) & 0xffffffff),
		$server
	) foreach @networks;
}

close($in);
close($out);
exit;

sub parse_ip_del {
	my ($file) = @_;

	my $pt = new Net::Patricia;

	open(my $in, '<', $file);
	while (<$in>) {
		# this code is copied from make_ip_del.pl
		chomp;
		s/#.*$//;
		s/^\s+//; s/\s+$//;
		next if /^$/;

		die "format error: $_" if not /^([\d\.]+)\/(\d+)\s+([\w\.]+)$/;
		my $network = "$1/$2";
		my $server = $3;

		$server = "whois.$server.net" if $server !~ /\./;

		$pt->add_string($network, $server) or die;
	}

	return $pt;
}