File README of Package apache2-mod_asn
1
2
mod_asn looks up the AS and network prefix of IP address.
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
mod_asn is an Apache module doing lookups of the autonomous system (AS) and the
6
network prefix that an IP address is contained in.
7
8
It is written with scalability in mind. To do high-speed lookups, it uses the
9
PostgreSQL ip4r datatype that is indexable with a Patricia Trie algorithm to
10
store network prefixes.
11
12
It comes with script to create such a database and update it with snapshots from
13
router's "view of the world".
14
15
The module sets the looked up data as env table variables, for use by other
16
Apache module to do things with it, or for logging -- and it can add the data
17
as response headers to the client.
18
19
20
Example HTTP response headers:
21
22
HTTP/1.1 200 OK
23
Date: Thu, 12 Feb 2009 23:24:33 GMT
24
Server: Apache/2.2.11 (Linux/SUSE)
25
X-Prefix: 83.133.0.0/16
26
X-AS: 13237
27
28
29
30
Performance
31
~~~~~~~~~~~
32
33
The database with all ~250.000 prefixes is about 20-30MB in size in the form of
34
a PostgreSQL database. Without any tuning, it is able to to >3000 lookups per
35
second on a MacBook Pro (tested with random IPs, a single connection, and
36
client written in Python running on the same machine).
37
38
The Apache module is extremely lightweight.
39
40
41
42
Design notes
43
~~~~~~~~~~~~
44
45
Performed with a Patricia Trie algorithm, the lookup is very efficient. The
46
Patricia Trie is a radix tree that works it way from bit to bit, starting at
47
the most significant bit. At each bit, there are two alternative "paths". Or
48
put another way, the space of prefixes is roughly divided in two halfs at each
49
point. The ip4r datatype achieves this by implementing an index that works this
50
way. Without the index, a full table scan would be required, plus bitmask
51
prefix match for each of the ~250.000 candidate rows.
52
53
"Conventional" storage in databases is possible with a workaround, e.g. with
54
two long integers denoting each prefix in a MySQL database. But this would
55
require an SQL "between" query. An additional column would be needed to store
56
the prefix length, in order to find the closest match (the most narrow prefix).
57
The built-in inet/cidr data type in PostgreSQL doens't help either because it
58
can't be indexed. With conventional methods, only about 30 lookups per second
59
can be achieved with a database.
60
61
Having the data in a real database makes it accessible for other means as well;
62
it is easily possible to query it the list of prefixes that an AS announces,
63
for instance. In addition, the storage in the database offers the possibility
64
to change and update the data (or even completely replace it) in a simple way,
65
by doing this in transaction, without blocking running queries.
66
67
For usage outside of Apache, a small libpq-based standalone daemon could be
68
written that queries the database. Alternatively, a small handler could be
69
written for mod_asn that does nothing than read an IP address from a request
70
body (or URL) and return the result.
71
72
One argument for the ip4r data type in PostgreSQL is that it is IPv6-ready.
73
Some IPv6 autonomous systems already exist (about 800 as of the beginning of
74
2009).
75
76
77
Usage with MirrorBrain
78
~~~~~~~~~~~~~~~~~~~~~~
79
80
mod_asn can support mod_mirrorbrain (see http://mirrorbrain.org).
81
mod_mirrorbrain can use the data (set in the subprocess environment) for its
82
mirror selection algorithm.
83
84
In addition, the database can be queried with the MirrorBrain tool set:
85
86
# mb iplookup mirror.susestudio.com
87
130.57.19.0/24 (AS3680)
88
# mb iplookup mirror.susestudio.com --all-prefixes
89
130.57.19.0/24 (AS3680)
90
130.57.0.0/16, 130.57.0.0/20, 130.57.19.0/24, 130.57.32.0/21, 137.65.0.0/16,
91
147.2.0.0/17, 151.155.0.0/16, 164.99.0.0/16, 192.31.114.0/24, 192.94.118.0/24,
92
192.108.102.0/24, 192.149.26.0/24, 195.109.215.0/24, 212.153.69.0/24
93
94
95