def server(port, homeAgentAddress, homeAgentPort, foreignAgent1Address,
           foreignAgent1Port, foreignAgent1BindPort, foreignAgent2Address,
           foreignAgent2Port, foreignAgent2BindPort):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #s.settimeout(5)
    s.bind(("", port))
    sockName = s.getsockname()
    print sockName
    utils.sendData(homeAgentAddress, homeAgentPort,
                   "2|" + foreignAgent1Address + "|" + str(foreignAgent1Port))
    nextSwitchTime = time.time() + 5
    while True:

        data, addressPair = s.recvfrom(1024)
        address, port = addressPair
        if address == socket.gethostbyname(foreignAgent1Address):
            status = "Accepted"
        else:
            status = "Rejected"
        print "Sequence Number = %s Time = %s FA = %s/%d %s" % (
            data, time.time(), address, port, status)

        if time.time() >= nextSwitchTime:
            foreignAgent1Address, foreignAgent2Address = foreignAgent2Address, foreignAgent1Address
            foreignAgent1Port, foreignAgent2Port = foreignAgent2Port, foreignAgent1Port
            foreignAgent1BindPort, foreignAgent2BindPort = foreignAgent2BindPort, foreignAgent1BindPort
            utils.sendData(
                homeAgentAddress, homeAgentPort,
                "2|" + foreignAgent1Address + "|" + str(foreignAgent1Port))
            print "Registration sent.  Time = %s  New care-of address = %s/%d " % (
                time.time(), foreignAgent1Address, foreignAgent1Port)
            nextSwitchTime = time.time() + 5

    s.close()
def server(port,homeAgentAddress, homeAgentPort,foreignAgent1Address,foreignAgent1Port,foreignAgent1BindPort,foreignAgent2Address, foreignAgent2Port,foreignAgent2BindPort):
    s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #s.settimeout(5)
    s.bind(("",port))
    sockName = s.getsockname()
    print sockName
    utils.sendData(homeAgentAddress,homeAgentPort,"2|"+foreignAgent1Address+"|"+str(foreignAgent1Port))
    nextSwitchTime = time.time()+5
    while True:
        
        data,addressPair = s.recvfrom(1024)
        address,port = addressPair
        if address == socket.gethostbyname(foreignAgent1Address):
            status = "Accepted"
        else:
            status = "Rejected"
        print "Sequence Number = %s Time = %s FA = %s/%d %s" %(data,time.time(),address,port, status)
            
        if time.time() >= nextSwitchTime:
            foreignAgent1Address,foreignAgent2Address = foreignAgent2Address,foreignAgent1Address
            foreignAgent1Port,foreignAgent2Port = foreignAgent2Port,foreignAgent1Port
            foreignAgent1BindPort,foreignAgent2BindPort=foreignAgent2BindPort,foreignAgent1BindPort
            utils.sendData(homeAgentAddress,homeAgentPort,"2|"+foreignAgent1Address+"|"+str(foreignAgent1Port))
            print "Registration sent.  Time = %s  New care-of address = %s/%d " %(time.time(),foreignAgent1Address,foreignAgent1Port)
            nextSwitchTime = time.time()+5
            
    s.close()  
def main(host,port):
    seqNumber = 1
    while True:
        utils.sendData(host,port,"1|"+ str(seqNumber))
        print "Sequence Number = %d Time = %s Dest = %s/%d" %(seqNumber, time.time(),socket.gethostbyname(host),port)
        time.sleep(1)
        seqNumber += 1
def server(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(("", port))
    sockName = s.getsockname()
    print sockName
    foreignAgentAddress = None
    foreignAgentPort = None
    while True:
        data, address = s.recvfrom(1024)
        dataArray = string.split(data, "|")
        if dataArray[0] == "1":
            if foreignAgentAddress is None:
                print "Foreign Agent Address is unknown"
            else:
                utils.sendData(foreignAgentAddress, foreignAgentPort,
                               dataArray[1])
                print "Sequence Number = %s Time = %s Forwarded to %s/%d " % (
                    dataArray[1], time.time(), foreignAgentAddress,
                    foreignAgentPort)
        elif dataArray[0] == "2":
            foreignAgentAddress = dataArray[1]
            foreignAgentPort = int(dataArray[2])
            print " Registration packet received.  Time =%s  Changing care-of address to %s/%d" % (
                time.time(), foreignAgentAddress, foreignAgentPort)
        else:
            print dataArray
    s.close()
Пример #5
0
def main(host, port):
    seqNumber = 1
    while True:
        utils.sendData(host, port, "1|" + str(seqNumber))
        print "Sequence Number = %d Time = %s Dest = %s/%d" % (
            seqNumber, time.time(), socket.gethostbyname(host), port)
        time.sleep(1)
        seqNumber += 1
def server(foreignAgentPort, bindPort, mobileNodeAddress, mobileNodePort):
    s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(("",foreignAgentPort))
    sockName = s.getsockname()
    print sockName
    while True:
        data,address = s.recvfrom(1024)
        utils.sendData(mobileNodeAddress,mobileNodePort,data,bindPort )
        print "Sequence Number = %s Time = %s Forwarded to %s/%d" %(data, time.time(),socket.gethostbyname(mobileNodeAddress),mobileNodePort)
    s.close()  
def server(port):
    s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(("",port))
    sockName = s.getsockname()
    print sockName
    foreignAgentAddress = None
    foreignAgentPort = None
    while True:
        data,address = s.recvfrom(1024)
        dataArray = string.split(data,"|")
        if dataArray[0] == "1":
            if foreignAgentAddress is None:
                print "Foreign Agent Address is unknown"
            else:
                utils.sendData(foreignAgentAddress,foreignAgentPort,dataArray[1])                   
                print "Sequence Number = %s Time = %s Forwarded to %s/%d " %(dataArray[1], time.time(),foreignAgentAddress,foreignAgentPort)
        elif dataArray[0] =="2":
            foreignAgentAddress = dataArray[1]
            foreignAgentPort = int(dataArray[2])
            print " Registration packet received.  Time =%s  Changing care-of address to %s/%d" %(time.time(),foreignAgentAddress,foreignAgentPort) 
        else:
            print dataArray
    s.close()  
Пример #8
0
def iniPortCommunication():
    # Create a socket object
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Register KDC To this Port
    # sock.bind(server_address)

    # connect to the server on local computer
    sock.connect(server_address)

    # exctract Key from string
    key = utils.extractKey(ALICE_INITIAL_SECRET_KEY)

    print(f"Key => {key}\n")

    # Sending Permission Message
    cipher = utils.encryptionProcessData(MESSAGE, key, "_")

    cipherSecretbyte = bytes(cipher, 'utf-8')

    print(f"Sending \' {MESSAGE} \' to Server ...")
    time.sleep(SLEEPING_TIME)

    # Send cipher
    initMsgSender(sock, cipherSecretbyte)

    # Get Listen from KDC
    while True:
        # put the socket into listening mode
        # sock.listen(5)
        print(f"Listening...")

        # Receiving Server Message
        # receive data from the server
        data = sock.recv(1024)

        #decode Message
        decodedMsg = data.decode()

        print(f"\n Message From Server => {decodedMsg}")

        # Decrypts Session Key Message Cipher from KDC
        plaintext = utils.decryptionProcess(decodedMsg, key, '_')

        print(f"\n Session Key => {plaintext}\n")

        sessionKey = utils.extractKey(plaintext)

        print(f"\n Extracted Session Key => {sessionKey}\n")

        time.sleep(SLEEPING_TIME * 3)

        break

    # close the connection
    sock.close()

    # From Alice Sending to BOB
    print(f"Preparing my message...")

    # Sending Permission Message
    cipherMsgBob = utils.encryptionProcessData(TO_BOB_MESSAGE, sessionKey, "-")

    time.sleep(SLEEPING_TIME)

    print(f"Sending \' {TO_BOB_MESSAGE} \' to Bob...")

    utils.sendData(HOST, cipherMsgBob, BOB_PORT)
    time.sleep(SLEEPING_TIME)

    print('\n Done Sending Hello Message to Bob... ')
    time.sleep(SLEEPING_TIME)
Пример #9
0
def receiveMsg(s, listKeys):
    # put the socket into listening mode
    s.listen(5)
    print(" Socket is listening...")
    # brand()

    time.sleep(SLEEPING_TIME)

    # a forever loop until we interrupt it or
    # an error occurs
    while True:

        # Establish connection with client.
        conn, addr = s.accept()
        print('\n Got connection from', addr)
        time.sleep(SLEEPING_TIME)

        while True:
            data = conn.recv(1024)
            print('\n Received : {!r}'.format(data))
            time.sleep(SLEEPING_TIME)

            # decode Message
            decodedMsg = data.decode()
            if data:
                # Alice initial encryption
                if (decodedMsg.find('_') != -1):
                    # Get Keys
                    aliceKey = utils.extractKey(ALICE_INITIAL_SECRET_KEY)
                    bobkey = utils.extractKey(BOB_INITIAL_SECRET_KEY)

                    print(f"\n Message from Alice => {decodedMsg}")

                    # Decrypts Message
                    plaintext = utils.decryptionProcess(
                        decodedMsg, aliceKey, '_')

                    print(f"\n plaintext => {plaintext}\n")

                    # Alice Confirmation
                    if (plaintext.find('Alice') != -1):
                        print(f"\n Alice Confirmed! ")
                        # GENERATE SESSION KEYS
                        sessionKey = genAuthKey()

                        # Sending Permission Message to Alice
                        aliceCipher = utils.encryptionProcessData(
                            sessionKey, aliceKey, "_")
                        bytMsg = bytes(aliceCipher, 'utf-8')
                        # send a thank you message to the Alice.
                        # conn.sendall(bytMsg)
                        print('\n Sending Session Key to Alice... ')
                        time.sleep(SLEEPING_TIME)
                        conn.sendto(bytMsg, server_address)
                        print('\n Done Sending Session Key to Alice... ')
                        time.sleep(SLEEPING_TIME)
                        conn.close()

                        # Sending Permission Message to Bob
                        bobCipher = utils.encryptionProcessData(
                            sessionKey, bobkey, "_")
                        # bytMsgAlice = bytes(bobCipher, 'utf-8')
                        print('\n Sending Session Key to Bob... ')
                        time.sleep(SLEEPING_TIME)
                        utils.sendData(HOST, bobCipher, BOB_PORT)
                        time.sleep(SLEEPING_TIME)
                        print('\n Done Sending Session Key to Bob... ')
                        break

            else:
                print(' No data from client with Address : ', addr)

                break