time.sleep(8) # so the user can read the text #################################### # Register ASA/objectives #################################### name = "TestClient" err,asa_handle = grasp.register_asa(name) if err: exit() grasp.tprint("ASA", name, "registered OK") obj3 = grasp.objective("EX3") obj3.neg = True err = grasp.register_obj(asa_handle,obj3) if not err: grasp.tprint("Objective EX3 registered OK") else: exit() grasp.init_bubble_text(name) grasp.tprint("Ready to negotiate EX3 as client") failct = 0 while True: #start of a negotiating session obj3.value = "Start" #discover a peer
# Create an asa_locator for EST-TLS # communication with peers #################################### est_address = grasp._unspec_address # This is the unspecified address, # which signals link-local address to API est_ttl = 120000 #milliseconds to live of the announcement est_locator = grasp.asa_locator(est_address, 0, False) est_locator.is_ipaddress = True est_locator.protocol = socket.IPPROTO_TCP #################################### # Register the objectives #################################### _err = grasp.register_obj(_asa_nonce, acp_obj) if not _err: grasp.tprint("Objective", acp_obj.name, "registered OK") else: grasp.tprint("Objective registration failure:", grasp.etext[_err]) exit() # demo code doesn't handle registration errors _err = grasp.register_obj(_asa_nonce, est_obj) if not _err: grasp.tprint("Objective", est_obj.name, "registered OK") else: grasp.tprint("Objective registration failure:", grasp.etext[_err]) exit() # demo code doesn't handle registration errors #################################### # Start pretty printing
#################################### # Register ASA/objectives #################################### err, asa_nonce = grasp.register_asa("AskDNSSD") if not err: grasp.tprint("ASA AskDNSSD registered OK") else: exit() #This objective is for the negotiating test obj3 = grasp.objective("411:DNSSD") obj3.neg = True obj3.loop_count = 4 err = grasp.register_obj(asa_nonce, obj3) if not err: grasp.tprint("Objective", obj3.name, "registered OK") else: exit() ################################### # Set up pretty printing ################################### grasp.init_bubble_text("AskDNSSD") grasp.tprint("Ready to negotiate", obj3.name, "as requester") #main loop while True: #insert test cases here
#################################### # Register ASA/objectives #################################### err, asa_nonce = grasp.register_asa("Briggs") if not err: grasp.tprint("ASA Briggs registered OK") else: exit() obj1 = grasp.objective("EX1") obj1.loop_count = 4 obj1.synch = True err = grasp.register_obj(asa_nonce, obj1) if not err: grasp.tprint("Objective EX1 registered OK") else: exit() obj2 = grasp.objective("EX2") obj2.loop_count = 4 obj2.synch = True err = grasp.register_obj(asa_nonce, obj2, rapid=True) if not err: grasp.tprint("Objective EX2 registered OK") else: exit()
else: exit() keys_obj = grasp.objective("411:quadski") keys_obj.loop_count = 4 keys_obj.neg = True #The pledge sends the [encrypted_domain_password, pledge_PEM] as the #value of this objective, as a list of bytes objects. The password is #RSA encrypted with quadski's public key. #quadski sends the key bytes and iv (initialisation vector) bytes #file as the return value of this objective, as an array [key, iv] #encoded in CBOR and then encrypted with the pledge's public key. err = grasp.register_obj(asa_nonce, keys_obj) if not err: grasp.tprint("Objective", keys_obj.name, "registered OK") else: exit() flood_obj = grasp.objective("411:quadskip") flood_obj.loop_count = 4 flood_obj.synch = True #The value of this objective is the quadski public key PEM string err = grasp.register_obj(asa_nonce, flood_obj) if not err: grasp.tprint("Objective", flood_obj.name, "registered OK") else: exit()
obj1 = grasp.objective("PrefixManager") obj1.loop_count = 4 obj1.neg = True obj1.value = None # Value is defined as a list [IPversion, length, prefix] ## Value is defined as a list [IPversion, PDcapable, length, prefix] # Offset definitions: ipv = 0 PD = ipv #not used in this version lgth = PD + 1 pfx = lgth + 1 _err = grasp.register_obj(asa_nonce, obj1) if not _err: grasp.tprint("Objective", obj1.name, "registered OK") else: grasp.tprint("Fatal error:", grasp.etext[_err]) exit() obj2 = grasp.objective("PrefixManager.Params") obj2.loop_count = 4 obj2.synch = True if master: _err = grasp.register_obj(asa_nonce, obj2) if not _err: grasp.tprint("Objective", obj2.name, "registered OK") else:
def run(self): time.sleep(1) # avoid printing glitch grasp.tprint( "WARNING: you can't run this test suite more than once without restarting the Python context; it leaves GRASP data structures dirty!\n" ) grasp.skip_dialogue(testing=True, selfing=True, diagnosing=True) time.sleep(1) # just to avoid mixed up print output #################################### # Test code: register ASA/objective# #################################### test_obj = grasp.objective("EX1") test_obj.loop_count = 2 test_obj.synch = True err, test_nonce = grasp.register_asa("ASA-1") if not err: err = grasp.register_obj(test_nonce, test_obj, ttl=10000) if not err: grasp.tprint("ASA-1 and EX1 Registered OK") #################################### # Test code: discover EX1 # #################################### err, test_ll = grasp.discover(test_nonce, test_obj, 10000) if len(test_ll) > 0: grasp.tprint("EX1 discovery result", test_ll[0].locator) else: grasp.tprint("No EX1 discovery response") #################################### # Test code: register and discover # # Boot ASA/objective # #################################### err, boot_nonce = grasp.register_asa("Boot") if err: #we've got a problem... raise RuntimeError("Can't register Boot as ASA") boot_obj = grasp.objective("Boot") boot_obj.loop_count = 2 boot_obj.synch = True err = grasp.register_obj(boot_nonce, boot_obj, discoverable=True) if err: #we've got a different problem... raise RuntimeError("Can't register Boot objective") for i in range(3): #test discovery 3 times, including artificial Divert grasp.tprint("Test ASA: grasp.test_divert", grasp.test_divert) err, boot_ll = grasp.discover(boot_nonce, boot_obj, 5000) if len(boot_ll) > 0: grasp.tprint("Boot discovery result", boot_ll[0].locator) grasp.test_divert = True else: grasp.tprint("No Boot discovery response") time.sleep(5) grasp.test_divert = False #################################### # Test code: send Flood messages #################################### obj1 = grasp.objective("Money") obj1.synch = True obj1.loop_count = 2 err = grasp.register_obj(test_nonce, obj1) obj1.value = [100, "NZD"] obj2 = grasp.objective("Bling") obj2.synch = True obj2.loop_count = 2 err = grasp.register_obj(test_nonce, obj2) obj2.value = ["Diamonds", "Rubies"] err = grasp.register_obj(test_nonce, obj2) if err: grasp.tprint(grasp.etext[err]) obj3 = grasp.objective("Intent.PrefixManager") obj3.synch = True obj3.loop_count = 2 err = grasp.register_obj(test_nonce, obj3) #obj3.value = '{"autonomic_intent":[{"model_version": "1.0"},{"intent_type": "Network management"},{"autonomic_domain": "Customer_X_intranet"},{"intent_name": "Prefix management"},{"intent_version": 73},{"Timestamp": "20150606 00:00:00"},{"Lifetime": "Permanent"},{"signature":"XXXXXXXXXXXXXXXXXXX"},{"content":[{"role": [{"role_name": "RSG"},{"role_characteristic":[{"prefix_length":"34"}]}]},{"role": [{"role_name": "ASG"},{"role_characteristic":[{"prefix_length": "44"}]}]},{"role": [{"role_name": "CSG"},{"role_characteristic":[{"prefix_length": "56"}]}]}]}]}' #obj3.value = '{"autonomic_intent dummy text"}' #obj3.value = bytes.fromhex('48deadbeefdeadbeef') #dummy CBOR obj3.value = cbor.dumps(["Some", "embedded", "CBOR", [1, 2, 3]]) grasp.flood(test_nonce, 0, grasp.tagged_objective(obj1, None), grasp.tagged_objective(obj2, None), grasp.tagged_objective(obj3, None)) ################################### # Test code: Listen Synchronize as from Boot ASA ################################### boot_obj.value = [1, "two", 3] boot_obj.synch = True err = grasp.listen_synchronize(boot_nonce, boot_obj) grasp.tprint("Listen synch", grasp.etext[err]) grasp.tprint(grasp._obj_registry[1].objective.name, "value", grasp._obj_registry[1].objective.value) ################################### # Test code: call Synchronize as from EX1 ################################### time.sleep(5) err, result = grasp.synchronize(test_nonce, obj2, None, 5000) if not err: grasp.tprint("Flooded synch obj2", result.value) else: grasp.tprint("Synch fail obj2", grasp.etext[err]) err, result = grasp.synchronize(test_nonce, obj3, None, 5000) if not err: grasp.tprint("Flooded synch obj3", result.value) else: grasp.tprint("Synch fail obj3", grasp.etext[err]) #this should fail as test_obj was neither flooded or listened for. err, result = grasp.synchronize(test_nonce, test_obj, None, 5000) if not err: grasp.tprint("Synch test_obj (should fail)", result.value) else: grasp.tprint("Synch fail test_obj (should fail)", grasp.etext[err]) boot2_obj = grasp.objective("Boot") boot2_obj.synch = True err, result = grasp.synchronize(test_nonce, boot2_obj, None, 5000) if not err: grasp.tprint("Synch boot2_obj", result.name, result.value) else: grasp.tprint("Synch fail boot2_obj", grasp.etext[err]) ################################### # Test code: print obj_registry # and flood cache ################################### 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) time.sleep(5) ################################### # Test code: check flood cache for Tag 24 case ################################### flobj = grasp.objective("Intent.PrefixManager") flobj.synch = True err, tobs = grasp.get_flood(test_nonce, flobj) if not err: for x in tobs: try: grasp.tprint("Flooded CBOR", x.objective.name, cbor.loads(x.objective.value)) except: grasp.tprint("Flooded raw", x.objective.name, x.objective.value) ################################### # Test code: deregister and then # attempt synch ################################### err = grasp.deregister_obj(boot_nonce, boot_obj) if not err: grasp.tprint("Deregistered Boot", ) else: grasp.tprint("Deregister failure", grasp.etext[err]) err, result = grasp.synchronize(test_nonce, boot2_obj, None, 5000) if not err: grasp.tprint("Synch boot2_obj (should fail)", result.name, result.value) else: grasp.tprint("Synch fail boot2_obj (should fail)", grasp.etext[err]) ################################### # Test code: start in-host negotiate test ################################### cheat_nonce = None Neg1().start() time.sleep(10) Neg2().start() ##################################### # Test code: # test deregistering an ASA, # and exit the thread ##################################### grasp.deregister_asa(test_nonce, "ASA-1") grasp.tprint("Exiting ASA test thread")
def run(self): global cheat_nonce reserves = grasp._prng.randint(100, 400) wt = grasp._prng.randint(15000, 40000) grasp.tprint("Reserves: $", reserves, "wait:", wt) err, asa_nonce = grasp.register_asa("Neg1") #assume it worked obj = grasp.objective("EX2") obj.neg = True #obj.loop_count = 2 #for testing purposes grasp.register_obj(asa_nonce, obj) #assume it worked cheat_nonce = asa_nonce #pass the nonce to the other negotiator! #attempt to listen err, snonce, answer = grasp.listen_negotiate(asa_nonce, obj) if err: grasp.tprint("listen_negotiate error:", grasp.etext[err]) else: grasp.tprint("listened, answer", answer.name, answer.value) grasp.tprint("Source was", snonce.id_source) result = True reason = None concluded = False if answer.value[0] != "NZD": result = False reason = "Invalid currency" elif answer.value[1] > reserves / 2: answer.value = ["NZD", int(reserves / 2)] err, temp, answer2 = grasp.negotiate_step( asa_nonce, snonce, answer, 1000) grasp.tprint("Step1 gave:", err, temp, answer2) if (not err) and (not temp): concluded = True grasp.tprint("Negotiation succeeded", answer2.value) elif not err: err1 = grasp.negotiate_wait(asa_nonce, snonce, wt) grasp.tprint("Tried wait:", grasp.etext[err1]) time.sleep( 20) #note - if wt<20000 this tests anomaly handling grasp.tprint("Woke up") answer2.value = ["NZD", int(0.75 * reserves)] err2, temp, answer3 = grasp.negotiate_step( asa_nonce, snonce, answer2, 1000) grasp.tprint("Step2 gave:", err2, temp, answer3) if (not err2) and (not temp): concluded = True grasp.tprint("Negotiation succeeded", answer3.value) elif not err2: result = False if reserves % 2: reason = "Insufficient funds" else: reason = u"Недостаточно средств" else: #other end rejected concluded = True grasp.tprint("Peer reject2:", answer3) else: #other end rejected concluded = True grasp.tprint("Peer reject1:", answer2) else: #can accept the requested value pass if not concluded: err = grasp.end_negotiate(asa_nonce, snonce, result, reason=reason) if err: grasp.tprint("end_negotiate error:", grasp.etext[err]) grasp.tprint("Exit")
proxy_locator.is_ipaddress = True #################################### # Construct the GRASP objective to announce the proxy #################################### proxy_obj = grasp.objective("AN_proxy") proxy_obj.synch = True proxy_obj.value = "" # proxy_obj.loop_count not set, the API forces it to 1 for link-local use #################################### # Register the GRASP objective #################################### _err = grasp.register_obj(_asa_nonce, proxy_obj) if not _err: grasp.tprint("Objective", proxy_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 Proxy") grasp.tprint("Proxy starting now") ################################### # Now find the registrar and pick one or two methods
#################################### # Register ASA/objective #################################### err, asa_nonce = grasp.register_asa("pusher") if not err: grasp.tprint("ASA pusher registered OK") else: exit() supported_obj = grasp.objective("411:mvFile") supported_obj.loop_count = 4 supported_obj.neg = True err = grasp.register_obj(asa_nonce, supported_obj) if not err: grasp.tprint("Objective", supported_obj.name, "registered OK") else: exit() ################################### # Set up pretty printing ################################### grasp.init_bubble_text("pusher") grasp.tprint("pusher is listening") ################################### # Negotiate as listener for ever ###################################
#################################### # 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
#################################### # Register ASA/objectives #################################### err, asa_nonce = grasp.register_asa("getter") if not err: grasp.tprint("ASA getter registered OK") else: exit() #This objective is for the negotiating test requested_obj = grasp.objective("411:mvFile") requested_obj.neg = True requested_obj.loop_count = 4 err = grasp.register_obj(asa_nonce, requested_obj) if not err: grasp.tprint("Objective", requested_obj.name, "registered OK") else: exit() ################################### # Set up pretty printing ################################### grasp.init_bubble_text("getter") grasp.tprint("Ready to negotiate", requested_obj.name, "as requester") if os.name == "nt": directory = "got\\" else:
def main(args): ################################### # Main thread starts here ################################### grasp.tprint("==========================") grasp.tprint("Registrar initializing") grasp.tprint("==========================") grasp.test_mode = False # set if you want detailed diagnostics #time.sleep(1) # 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. grasp.skip_dialogue(False, False, True) _err, asa_nonce = grasp.register_asa("Reggie") #grasp.tprint("TYPE: ", type(asa_nonce)) #.skip_dialogue(False,False,True) if not _err: grasp.tprint("ASA Registrar registered OK") else: grasp.tprint("ASA Registrar failure:", grasp.etext[_err]) exit() # demo code doesn't handle registration errors #grasp.skip_dialogue(False,False,True) #################################### # 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 IPv4 port for BRSKI-IPv4 #################################### # For this demo, we just make up some numbers: ipv4_port = 80 ipv4_proto = socket.IPPROTO_TCP #ipv4_address = '172.2.13.0' #grasp._my_address # current address determined by GRASP kernel ipv4_address = findOwnIPv4() # current address determined by GRASP kernel #################################### # Construct a correponding GRASP ASA locator #################################### ipv4_locator = grasp.asa_locator(ipv4_address, None, False) ipv4_locator.protocol = ipv4_proto ipv4_locator.port = ipv4_port #ipv4_locator.is_ipaddress = True ipv4_locator.is_fqdn = 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("==========================") grasp.tprint("Registrar starting now") grasp.tprint("==========================") #################################### # Start flooding thread #################################### f = flooder(reg_obj, ipv4_locator, asa_nonce) f.start() #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... try: while True: time.sleep(5) #grasp.tprint("Registrar main loop diagnostic dump:") #dump_some() except KeyboardInterrupt: print('interrupted!') grasp.tprint("EXITTING") f.stop() exit()