aboutsummaryrefslogtreecommitdiff
path: root/ca/rpkigui-query-routes
blob: 179f8c2cac7d015fd2bb03a63c98803c346265d4 (plain) (blame)
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
#!/usr/bin/env python

# Copyright (C) 2013  SPARTA, Inc. a Parsons Company
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND SPARTA DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL SPARTA BE LIABLE FOR ANY SPECIAL, DIRECT,
# 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.

__version__ = '$Id$'

import sys
import optparse

from rpki.gui.script_util import setup
setup()

from rpki.gui.routeview import models as rv
from rpki.resource_set import resource_range_ip

parser = optparse.OptionParser(
    usage='%prog [options] PREFIX',
    description='query the rpki web portal database for routes covering a '
    'prefix specified as an argument, and display the validity and covering '
    'ROAs for each route',
    version=__version__,
)
options, args = parser.parse_args()

if len(args) == 0:
    print 'error: Specify an address/prefix'
    sys.exit(1)

# allow bare IP addresses
if '/' not in args[0]:
    args[0] = args[0] + '/32'

r = resource_range_ip.parse_str(args[0])

qs = rv.RouteOrigin.objects.filter(
    prefix_min__lte=r.min,
    prefix_max__gte=r.max
)

def validity_marker(route, roa, roa_prefix):
    "Return + if the roa would cause the route to be accepted, or - if not"
    # we already know the ROA covers this route because they are returned
    # from RouteOrigin.roas, so just check the ASN and max prefix length
    return '-' if (roa.asid == 0 or route.asn != roa.asid or
                   route.prefixlen > roa_prefix.max_length) else '+'

# xxx.xxx.xxx.xxx/xx-xx is 22 characters
for route in qs:
    print route.as_resource_range(), route.asn, route.status
    for pfx in route.roa_prefixes:
        for roa in pfx.roas.all():
            print validity_marker(route, roa, pfx), pfx.as_roa_prefix(), roa.asid, roa.repo.uri
    print