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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# $Id$
import base64, sax_utils, resource_set
Broken = True
assert ! Broken
# This still isn't right, although it's not as broken as it was. I
# don't see any sane way to avoid keeping a stack of objects under
# construction, probably in our sax_handler object. Passing SAX
# events along to the current object is ok, but passing every SAX
# event down through the chain of objects under construction is just
# nuts. So probably want push and pop methods in the sax_handler so
# that the current object can create a child and push it onto the
# stack, and so that a current object can pop itself off the stack.
class base_elt(object):
"""
Base type for left-right message elements.
"""
def __init__(self, up=None):
self.up = up
for key in self.multivalue:
setattr(self, key, [])
multivalue = ("peer_contact", "signing_cert", "extension_preference", "resource_class",
"as_number", "as_range", "subset_as_number", "subset_as_range",
"ipv4_prefix", "ipv4_range", "subset_ipv4_prefix", "subset_ipv4_range",
"ipv6_prefix", "ipv6_range", "subset_ipv6_prefix", "subset_ipv6_range")
def store(self, key, val):
if key in self.multivalue:
getattr(self, key).append(val)
else:
setattr(self, key, val)
b64content = ("peer_ta", "pkcs10_cert_request", "public_key", "signing_cert")
def endElement(self, name, text):
if name in self.b64content:
self.store(name, base64.b64decode(text))
pdu_objects = ("self", "child", "parent", "bsc", "repository", "route_origin",
"list_resources", "report_error")
def startElement(self, name, attrs):
if name in pdu_objects:
sax_utils.snarf_attribute(self, attrs, self.attributes)
else:
getattr(self, "handle_" + name)(name, attrs)
# handle_xxx methods not yet written
class self_elt(base_elt):
attributes = ("action", "self_id")
class bsc_elt(base_elt):
attributes = ("action", "self_id", "biz_signing_context_id")
class parent_elt(base_elt):
attributes = ("action", "self_id", "parent_id")
class child_elt(base_elt):
attributes = ("action", "self_id", "child_id")
class repository_elt(base_elt):
attributes = ("action", "self_id", "repository_id")
class route_origin_elt(base_elt):
attributes = ("action", "self_id", "route_origin_id")
class list_resources_elt(base_elt):
attributes = ("self_id", "child_id")
class report_error_elt(base_elt):
attributes = ("self_id", "error_code")
class msg(list):
"""
Left-right PDU.
"""
spec_uri = "http://www.hactrn.net/uris/rpki/left-right-spec/"
version = 1
dispatch = {
"self" : self_elt,
"child" : child_elt,
"parent" : parent_elt,
"repository" : repository_elt,
"route_origin" : route_origin_elt,
"bsc" : bsc_elt,
"list_resources" : list_resources_elt,
"report_error" : report_error_elt }
def startElement(self, name, attrs):
if name == "msg":
sax_utils.snarf_attribute(self, attrs, "version", int)
sax_utils.snarf_attribute(self, attrs, "type")
assert self.version == 1
else:
if name in self.dispatch:
self.append(self.dispatch[name](self))
self[-1].startElement(name, attrs)
def endElement(self, name, text):
self[-1].endElement(name, text)
def __str__(self):
return ('<?xml version="1.0" encoding="US-ASCII" ?>\n'
'<msg xmlns="%s" version="%d" type="%s">\n' \
% (self.spec_uri, self.version, self.type)
) + "".join(map(str,self)) + "</msg>\n"
class sax_handler(sax_utils.handler):
"""
SAX handler for Left-Right protocol.
"""
def startElement(self, name, attrs):
if name == "msg":
self.set_obj(msg())
self.obj.startElement(name, attrs)
def endElement(self, name):
self.obj.endElement(name, self.get_text())
# bsc_link ; attribute-only element
# child_db_id ; attribute-only element
# repository_link ; attribute-only element
# sia_base ; attribute-only element
#
# as_number ; attribute-only element, single/multi depending on context (sigh)
# as_range ; attribute-only element, multi
# ipv4_prefix ; attribute-only element, multi
# ipv4_range ; attribute-only element, multi
# ipv6_prefix ; attribute-only element, multi
# ipv6_range ; attribute-only element, multi
# peer_contact ; attribute-only element, multi
# subset_as_number ; attribute-only element, multi
# subset_as_range ; attribute-only element, multi
# subset_ipv4_prefix ; attribute-only element, multi
# subset_ipv4_range ; attribute-only element, multi
# subset_ipv6_prefix ; attribute-only element, multi
# subset_ipv6_range ; attribute-only element, multi
#
# peer_ta ; base64 element
# pkcs10_cert_request ; base64 element
# public_key ; base64 element
# signing_cert ; base64 element, multi
#
# extension_preference ; container element, multi
# resource_class ; container element, multi
#
# generate_keypair ; attribute-only control element
# publish_world_now ; empty control element
# reissue ; empty control element
# rekey ; empty control element
# revoke ; empty control element
# run_now ; empty control element
# suppress_publication ; empty control element
#
# msg ; pdu
# bsc ; pdu element
# child ; pdu element
# list_resources ; pdu element
# parent ; pdu element
# report_error ; pdu element
# repository ; pdu element
# route_origin ; pdu element
# self ; pdu element
|