Esempio n. 1
0
    def initASAs(self, registrar, proxy):
        ###################################
        # Main thread starts here
        ###################################

        grasp.tprint("==========================")
        grasp.tprint("GRASP Service is starting up.")
        grasp.tprint("==========================")

        #grasp.test_mode = True # tell everybody it's a test, will print extra 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.

        grasp.skip_dialogue(False, False, True)
        _err, self._asa_nonce = grasp.register_asa("Pledge")
        if not _err:
            grasp.tprint("ASA Pledge registered OK")
        else:
            grasp.tprint("ASA registration failure:", grasp.etext[_err])
            exit()

        ####################################
        # Construct a GRASP objective
        ####################################

        # This is an empty GRASP objective to find the proxy
        # It's only used for get_flood so doesn't need to be filled in
        if proxy:
            proxy_obj = grasp.objective("AN_proxy")
            proxy_obj.synch = True
        else:
            proxy_obj = None

        if registrar:
            reg_obj = grasp.objective("AN_join_registrar")
            reg_obj.synch = True
        else:
            reg_obj = None
        return proxy_obj, reg_obj
Esempio n. 2
0
    grasp.tprint("Using subnet length", subnet_length)
    subnet_unit = subnet_length  #delegator counts in delegated lengths
    quickly = False
    try:
        _ = input("Run delegation extra fast? Y/N:")
        if _[0] == "Y" or _[0] == "y":
            quickly = True
    except:
        pass
    btext = "Pfxm3 Delegator"

####################################
# Register ASA/objectives
####################################

_err, asa_nonce = grasp.register_asa("pfxm3")
if not _err:
    grasp.tprint("ASA pfxm3 registered OK")

else:
    grasp.tprint("Fatal error:", grasp.etext[_err])
    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:
Esempio n. 3
0
####################################
# General initialisation
####################################

#time.sleep(8) # so the user can read the text

#this instance of GRASP must run unencrypted
grasp.skip_dialogue(selfing=True, quadsing=False)

#grasp.tprint("Encryption", grasp.crypto, "(should be False)")

####################################
# Register ASA/objectives
####################################

err, asa_nonce = grasp.register_asa("quadski")
if not err:
    grasp.tprint("ASA quadski registered OK")

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]
Esempio n. 4
0
grasp.tprint("ASA TestClient is starting up.")
grasp.tprint("==========================")
grasp.tprint("TestClient is a demonstration Autonomic Service Agent.")
grasp.tprint("It just tests out gsend() and grecv() by talking to its peer")
grasp.tprint("On Windows or Linux, there should be a nice window")
grasp.tprint("that displays the talking process.")
grasp.tprint("==========================")

time.sleep(8)  # so the user can read the text

####################################
# Register ASA/objectives
####################################

name = "TestClient"
err, asa_nonce = 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_nonce, obj3)
if not err:
    grasp.tprint("Objective EX3 registered OK")
else:
    exit()

grasp.init_bubble_text(name)
Esempio n. 5
0
    def run(self):
        global cheat_nonce

        iwant = grasp._prng.randint(10, 500)
        grasp.tprint("Asking for $", iwant)
        err, asa_nonce2 = grasp.register_asa("Neg2")  #assume it worked
        obj = grasp.objective("EX2")
        obj.neg = True
        #obj.loop_count = 2  #for testing purposes
        while cheat_nonce == None:
            time.sleep(
                1)  #we should exit after neg1 has registered the objective
        asa_nonce = cheat_nonce  #now we can pretend to own the objective
        grasp.tprint("Got nonce", asa_nonce)
        err, ll = grasp.discover(asa_nonce, obj, 5000)
        if ll == []:
            grasp.tprint("Discovery failed: exit")
            return
        grasp.tprint("Discovered locator", ll[0].locator)
        #attempt to negotiate
        obj.value = ["NZD", iwant]
        if not iwant % 7:
            obj.value = ["USD", iwant]  # for testing purposes
        err, snonce, answer = grasp.req_negotiate(asa_nonce, obj, ll[0], None)
        if err:
            if err == grasp.errors.declined and answer != "":
                _t = answer
            else:
                _t = grasp.etext[err]
            grasp.tprint("req_negotiate error:", _t)
        elif (not err) and snonce:
            grasp.tprint("requested, session_nonce:", snonce, "answer", answer)
            if answer.value[1] < 0.75 * iwant:
                answer.value[1] = int(0.75 * iwant)
                err, temp, answer2 = grasp.negotiate_step(
                    asa_nonce, snonce, answer, 1000)
                grasp.tprint("Step1 gave:", err, temp, answer2)
                if (not err) and (not temp):
                    grasp.tprint("Negotiation succeeded", answer.value)
                elif not err:
                    #not acceptable, try one more time
                    answer2.value[1] = int(0.6 * iwant)
                    #at random, throw an invalid format of message
                    if not iwant % 3:
                        grasp.tprint("Trying badmess")
                        grasp._make_badmess = True
                    err, temp, answer3 = grasp.negotiate_step(
                        asa_nonce, snonce, answer2, 1000)
                    grasp.tprint("Step2 gave:", err, temp, answer3)
                    if (not err) and (not temp):
                        grasp.tprint("Negotiation succeeded", answer3.value)
                    elif (not err):
                        #not acceptable
                        err = grasp.end_negotiate(asa_nonce,
                                                  snonce,
                                                  False,
                                                  reason="You are mean!")
                        if err:
                            grasp.tprint("end_negotiate error:",
                                         grasp.etext[err])
                    else:
                        #other end rejected
                        grasp.tprint("Peer reject:", answer3)
                else:
                    #other end rejected
                    grasp.tprint("Peer reject:", answer2)
            else:  #acceptable answer
                err = grasp.end_negotiate(asa_nonce, snonce, True)
                if not err:
                    grasp.tprint("Negotiation succeeded", answer.value)
                else:
                    grasp.tprint("end_negotiate error:", grasp.etext[err])
        else:  #acceptable answer first time
            grasp.tprint("Negotiation succeeded", answer.value)

        grasp.tprint("Exit")
Esempio n. 6
0
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()
Esempio n. 7
0
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 # tell everybody it's a test, will print extra 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.

_err,_asa_nonce = grasp.register_asa("Newby")
if not _err:
    grasp.tprint("ASA Newby registered OK")
else:
    grasp.tprint("ASA registration failure:",grasp.etext[_err])
    exit()

####################################
# Construct GRASP objectives
####################################

# These are empty GRASP objectives to find the peer(s)
# They are only used for get_flood so don't need to be filled in

acp_obj = grasp.objective("AN_ACP")
acp_obj.synch = True
Esempio n. 8
0
grasp.tprint("It acts as the banker, giving out money, and can")
grasp.tprint("handle multiple overlapping negotiations.")
grasp.tprint("The sum available is random for each negotiation,")
grasp.tprint("and the negotiation timeout is changed at random.")
grasp.tprint("On Windows or Linux, there should be a nice window")
grasp.tprint("that displays the negotiation process.")
grasp.tprint("==========================")

#grasp.test_mode = True # set if you want detailed diagnostics
time.sleep(8)  # so the user can read the text

####################################
# 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()
Esempio n. 9
0
grasp.tprint("then runs indefinitely as one side of a negotiation.")
grasp.tprint("It acts as a client, asking for money.")
grasp.tprint("The sum requested is random for each negotiation,")
grasp.tprint("and some GRASP features are used at random.")
grasp.tprint("On Windows or Linux, there should be a nice window")
grasp.tprint("that displays the negotiation process.")
grasp.tprint("========================")

time.sleep(8)  # so the user can read the text
_prng = random.SystemRandom()  # best PRNG we can get

####################################
# Register ASA/objectives
####################################

err, asa_nonce = grasp.register_asa("Gray")
if not err:
    grasp.tprint("ASA Gray registered OK")
else:
    exit()

#This objective is for the flooding test
#so doesn't need to be registered
obj1 = grasp.objective("EX1")
obj1.loop_count = 4
obj1.synch = True

#This objective is for the synchronizing test
#so doesn't need to be registered
obj2 = grasp.objective("EX2")
obj2.loop_count = 4
Esempio n. 10
0
#grasp.tprint('modulo an error in the "AN_proxy" definition')
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 # tell everybody it's a test, will print extra 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.

_err, _asa_nonce = grasp.register_asa("Procksy")
if not _err:
    grasp.tprint("ASA Procksy registered OK")
else:
    grasp.tprint("ASA registration failure:", grasp.etext[_err])
    exit()

####################################
# Construct a GRASP objective
####################################

# This is an empty GRASP objective to find the registrar
# It's only used for get_flood so doesn't need to be filled in

reg_obj = grasp.objective("AN_join_registrar")
reg_obj.synch = True
Esempio n. 11
0
grasp.tprint("On Windows or Linux, there should be a nice")
grasp.tprint("window that displays the process.")
grasp.tprint("==========================")

####################################
# General initialisation
####################################

time.sleep(8)  # so the user can read the text
grasp.skip_dialogue(selfing=True)

####################################
# 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()
Esempio n. 12
0
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
Esempio n. 13
0
grasp.tprint("that displays the process.")
grasp.tprint("========================")

####################################
# General initialisation
####################################

grasp.skip_dialogue(selfing=True)
time.sleep(8)  # so the user can read the text
failct = 0  # fail counter to control discovery retries

####################################
# 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()
Esempio n. 14
0
#grasp.tprint('modulo an error in the "AN_proxy" definition')
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 # tell everybody it's a test, will print extra 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.

_err, _asa_nonce = grasp.register_asa("Pledji")
if not _err:
    grasp.tprint("ASA Pledji registered OK")
else:
    grasp.tprint("ASA registration failure:", grasp.etext[_err])
    exit()

####################################
# Construct a GRASP objective
####################################

# This is an empty GRASP objective to find the proxy
# It's only used for get_flood so doesn't need to be filled in

proxy_obj = grasp.objective("AN_proxy")
proxy_obj.synch = True
Esempio n. 15
0
####################################

private_key = rsa.generate_private_key(
     public_exponent=65537,
     key_size=2048,
     backend=default_backend() )
public_key = private_key.public_key()
pem = public_key.public_bytes(
    encoding=serialization.Encoding.PEM,
    format=serialization.PublicFormat.SubjectPublicKeyInfo )

####################################
# Register ASA/objectives
####################################

err, asa_nonce = grasp.register_asa("qpledge")
if not err:
    grasp.tprint("QUADS pledge 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

keys_obj = grasp.objective("411:quadski")
keys_obj.neg = True
keys_obj.loop_count = 4
#The pledge sends the [encrypted_domain_password, pledge_PEM] as the
#value of this objective, as a list of bytes objects. The password is
Esempio n. 16
0
grasp.tprint("On Windows or Linux, there should be a nice")
grasp.tprint("window that displays the process.")
grasp.tprint("==========================")

####################################
# General initialisation
####################################

#grasp.test_mode = True # set if you want detailed diagnostics
time.sleep(8) # so the user can read the text

####################################
# Register ASA/objective
####################################

err,asa_nonce = grasp.register_asa("GetDNSSD2")
if not err:
    grasp.tprint("ASA GetDNSSD2 registered OK")

else:
    exit()
    
obj1 = grasp.objective("SRV.")
obj1.loop_count = 4
obj1.neg = True


err = grasp.register_obj(asa_nonce,obj1)
if not err:
    grasp.tprint("Objective", obj1.name, "registered OK")
else:
Esempio n. 17
0
grasp.tprint("that displays the process.")
grasp.tprint("========================")

####################################
# General initialisation
####################################

#grasp.test_mode = True # tell everybody it's a test
time.sleep(8)  # so the user can read the text
failct = 0  # fail counter to control discovery retries

####################################
# 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()
Esempio n. 18
0
grasp.tprint("========================")
grasp.tprint("Mudslinger is a demonstration Autonomic Service Agent.")
grasp.tprint("It acts the part of a node that needs to send its")
grasp.tprint("MUD URL to the local MUD manager.")
grasp.tprint("On Windows or Linux, there should be a nice window")
grasp.tprint("that displays the process.")
grasp.tprint("========================")

time.sleep(8)  # so the user can read the text

####################################
# Register ASA/objectives
####################################

asa_name = "Mudslinger"
err, asa_nonce = grasp.register_asa("asa_name")
if not err:
    grasp.tprint("ASA", asa_name, "registered OK")
else:
    grasp.tprint("ASA registration failure:", grasp.etext[err])
    time.sleep(60)
    exit()

obj_name = "411:MUDURL"
obj3 = grasp.objective(obj_name)
obj3.neg = True

err = grasp.register_obj(asa_nonce, obj3)
if not err:
    grasp.tprint("Objective", obj_name, "registered OK")
else:
Esempio n. 19
0
grasp.tprint("ASA TestClient is starting up.")
grasp.tprint("==========================")
grasp.tprint("TestClient is a demonstration Autonomic Service Agent.")
grasp.tprint("It just tests out gsend() and grecv() by talking to its peer")
grasp.tprint("On Windows or Linux, there should be a nice window")
grasp.tprint("that displays the talking process.")
grasp.tprint("==========================")

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)
Esempio n. 20
0
    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")
Esempio n. 21
0
grasp.tprint("It also announce itself as an EST server.")
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 # tell everybody it's a test, will print extra 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.

_err, _asa_nonce = grasp.register_asa("ACPcontainer")
if not _err:
    grasp.tprint("ASA ACPcontainer registered OK")
else:
    grasp.tprint("ASA registration failure:", grasp.etext[_err])
    exit()

####################################
# Construct GRASP objectives
####################################

acp_obj = grasp.objective("AN_ACP")
acp_obj.synch = True
acp_obj.discoverable = False  #override default
acp_obj.value = "IKEv2"
# acp_obj.loop_count not set, the API forces it to 1 for link-local use
Esempio n. 22
0
 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")