forked from becarpenter/graspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReggie.py
218 lines (165 loc) · 6.58 KB
/
Reggie.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""This is some demo code showing how a BRSKI registrar would
provide its contact details to an ANIMA network using GRASP. The
actual BRSKI transactions are not included. Flooding
version, per draft-ietf-anima-bootstrapping-keyinfra-09.
"""
import sys
sys.path.insert(0, '..') # in case grasp.py is one level up
import grasp
import threading
import time
import socket
try:
socket.IPPROTO_IPV6
except:
socket.IPPROTO_IPV6 = 41
###################################
# Utility routine for debugging:
# Print out the GRASP objective registry
# and flood cache
###################################
def dump_some():
grasp.tprint("Objective registry contents:")
for x in grasp._obj_registry:
o= x.objective
grasp.tprint(o.name,"ASA:",x.asa_id,"Listen:",x.listening,"Neg:",o.neg,
"Synch:",o.synch,"Count:",o.loop_count,"Value:",o.value)
grasp.tprint("Flood cache contents:")
for x in grasp._flood_cache:
grasp.tprint(x.objective.name,"count:",x.objective.loop_count,"value:",
x.objective.value,"source",x.source.locator, x.source.protocol,
x.source.port,"expiry",x.source.expire)
####################################
# Thread to flood the objective repeatedly
####################################
class flooder(threading.Thread):
"""Thread to flood objectve repeatedly"""
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
time.sleep(60)
reg_obj.value = "EST-TLS"
grasp.flood(asa_nonce, 120000,
grasp.tagged_objective(reg_obj,tcp_locator))
#not using grasp.tagged_objective(reg_obj,udp_locator),
#not using grasp.tagged_objective(reg_obj,ipip_locator))
###################################
# Main thread starts here
###################################
grasp.tprint("==========================")
grasp.tprint("ASA Reggie is starting up.")
grasp.tprint("==========================")
grasp.tprint("Reggie is a demonstration Autonomic Service Agent.")
grasp.tprint("It mimics a BRSKI Join Registrar by providing")
grasp.tprint("the methods it supports, with associated locators,")
grasp.tprint("as synchronized GRASP objectives.")
grasp.tprint("Then it pretends to wait for BRSKI traffic.")
grasp.tprint("This version supports flooding,")
grasp.tprint("per draft-ietf-anima-bootstrapping-keyinfra-12")
grasp.tprint("On Windows or Linux, there should soon be")
grasp.tprint("a nice window that displays the process.")
grasp.tprint("==========================")
#grasp.test_mode = True # set if you want detailed diagnostics
time.sleep(8) # time to read the text
####################################
# Register this ASA
####################################
# The ASA name is arbitrary - it just needs to be
# unique in the GRASP instance. If you wanted to
# run two registrars in one GRASP instance, they
# would need different names. For example the name
# could include a timestamp.
_err, asa_nonce = grasp.register_asa("Reggie")
if not _err:
grasp.tprint("ASA Reggie registered OK")
else:
grasp.tprint("ASA registration failure:",grasp.etext[_err])
exit() # demo code doesn't handle registration errors
####################################
# Create a TCP port for BRSKI-TCP
####################################
# For this demo, we just make up some numbers:
tcp_port = 80
tcp_proto = socket.IPPROTO_TCP
tcp_address = grasp._my_address # current address determined by GRASP kernel
####################################
# Construct a correponding GRASP ASA locator
####################################
tcp_locator = grasp.asa_locator(tcp_address, None, False)
tcp_locator.protocol = tcp_proto
tcp_locator.port = tcp_port
tcp_locator.is_ipaddress = True
####################################
# Create a UDP port for BRSKI-UDP
####################################
# For this demo, we just make up some numbers:
udp_port = 880
udp_proto = socket.IPPROTO_UDP
udp_address = grasp._my_address # current address determined by GRASP kernel
####################################
# Construct a correponding GRASP ASA locator
####################################
udp_locator = grasp.asa_locator(udp_address, None, False)
udp_locator.protocol = udp_proto
udp_locator.port = udp_port
udp_locator.is_ipaddress = True
####################################
# Create a dummy IP-in-IP port for BRSKI-IPIP
####################################
ipip_port = 0
ipip_proto = socket.IPPROTO_IPV6
ipip_address = grasp._my_address # current address determined by GRASP kernel
####################################
# Construct a correponding GRASP ASA locator
####################################
ipip_locator = grasp.asa_locator(ipip_address, None, False)
ipip_locator.protocol = ipip_proto
ipip_locator.port = ipip_port
ipip_locator.is_ipaddress = True
####################################
# Construct the GRASP objective
####################################
radius = 255 # Limit the radius of flooding
reg_obj = grasp.objective("AN_join_registrar")
reg_obj.loop_count = radius
reg_obj.synch = True # needed for flooding
reg_obj.value = None
####################################
# Register the GRASP objective
####################################
_err = grasp.register_obj(asa_nonce,reg_obj)
if not _err:
grasp.tprint("Objective", reg_obj.name, "registered OK")
else:
grasp.tprint("Objective registration failure:", grasp.etext[_err])
exit() # demo code doesn't handle registration errors
####################################
# Start pretty printing
####################################
grasp.init_bubble_text("BRSKI Join Registrar (flooding method)")
grasp.tprint("Registrar starting now")
####################################
# Start flooding thread
####################################
flooder().start()
grasp.tprint("Flooding", reg_obj.name, "for ever")
###################################
# Listen for requests
###################################
# Here, launch a thread to do the real work of the registrar
# via the various ports But for the demo, we just pretend...
grasp.tprint("Pretending to listen to ports", tcp_port,",", udp_port,
"and for IP-in-IP")
###################################
# Do whatever needs to be done in the main thread
###################################
# At a minimum, the main thread should keep an eye
# on the other threads and restart them if needed.
# For the demo, we just dump some diagnostic data...
while True:
time.sleep(30)
grasp.tprint("Registrar main loop diagnostic dump:")
dump_some()