Exemplo n.º 1
0
    def run(self):
        try:
            self.msg.emit('starting router')
            self.physicalRouterSocket = socket(AF_INET, SOCK_STREAM)
            self.physicalRouterSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
            self.physicalRouterSocket.bind(Layer.PhysicalRouter)
            self.host, self.port = Layer.PhysicalRouter
            self.physicalRouterSocket.listen(1)
            self.msg.emit("Setup:\nIP = " + str(self.host) + '\tPort = ' + str(self.port) + '\nListening...')
        except Exception as exc:
            self.errorMsg.emit('ERROR! It was not possible start the execution: \n' + str(exc))
        try:
            while True:
                if not self.mtuSent:
                    self.getMyIPMAC()
                    self.connectAsServer(self.physicalRouterSocket)
                #receive request file from client
                if self.receiveFile(self.physicalRouterSocket, 'binaryRouterClientRequest.txt'):
                    self.package = self.interpretPackage('binaryRouterClientRequest.txt', 'blue')

                    #Sending to and receiving from network layer
                    Layer.send(Layer.NetworkRouter, self.package)
                    self.msg.emit('Waiting answer...')
                    self.answer, success = Layer.receive(self.physicalRouterSocket)
                    self.msg.emit('Received package from Network layer.')
                    serverIP = json.loads(self.answer)['destiny']
                    self.getDstMAC(serverIP[0])
                    self.answer = json.loads(self.answer)['datagram']

                    #Creating binary file of request from client and sending to server
                    self.createFrame_BinaryFile(self.answer, 'binaryRouterRequest.txt','green')
                    self.msg.emit ('Sending to server IP = ' + str(Layer.PhysicalServer))
                    self.connectAsClient(Layer.PhysicalServer)
                    self.msg.emit ('Request sent.');
                    success = Layer.send(Layer.PhysicalServer, 'binaryRouterRequest.txt', self.mtu)

                    #receiving answer from server
                    if success and self.receiveFile(self.physicalRouterSocket, 'binaryRouterServerAnswer.txt'):
                        self.package = self.interpretPackage('binaryRouterServerAnswer.txt', 'blue')

                        #sending to and receiving from network layer
                        Layer.send(Layer.NetworkRouter, self.package)
                        self.msg.emit('Waiting answer...')
                        self.answer, success = Layer.receive(self.physicalServerSocket)
                        self.msg.emit('Received package from Network layer.')
                        destiny = json.loads(self.answer)['destiny']
                        self.answer = json.loads(self.answer)['datagram']

                        #creating binary file of answer from server and sending to client
                        self.createFrame_BinaryFile(self.answer, 'binaryRouterAnswer.txt','green')
                        Layer.send(Layer.PhysicalClient, 'binaryRouterAnswer.txt', self.mtu)

        except Exception as exc:
            exc_type, exc_obj, exc_tb = sys.exc_info()
            error = exc_tb.tb_frame
            line = exc_tb.tb_lineno
            fileName = error.f_code.co_filename
            self.errorMsg.emit('ERROR! It was not possible run the server: \n' + str(exc)+
                    '\nLine = ' + str(line))
            return False
Exemplo n.º 2
0
 def receiveFromInternet(self):
     try:
         self.transportServerSocket = socket(AF_INET, SOCK_STREAM)
         self.transportServerSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
         self.transportServerSocket.bind(Layer.TransportServer)
         self.transportServerSocket.listen(1)
         print 'Listening...'
         while True:
             self.segment, success = Layer.receive(self.transportServerSocket)
             self.transportProtocol = json.loads(self.segment)['transportProtocol']
             self.msg.emit('Received a ' + self.transportProtocol + ' segment.')
             if success:
                 if self.transportProtocol == 'TCP':
                     if not self.interpretTCPSegment(self.segment):
                         self.errorMsg.emit('Error trying to interpret TCP segment')
                 else: #UDP protocol
                     self.interpretUDPSegment(self.segment)
                 self.sendToApplication()
                 if self.receiveAnswer():
                     self.createSegment(self.transportProtocol, self.applicationPack)
                     sent = Layer.send(Layer.NetworkServer, self.segment)
                     print 'Answer sent to internet layer'
     except KeyboardInterrupt:
         print 'Shutting down transport server'
     except Exception as exc:
         exc_type, exc_obj, exc_tb = sys.exc_info()
         error = exc_tb.tb_frame
         line = exc_tb.tb_lineno
         fileName = error.f_code.co_filename
         print "Error! " + str(exc)
         print 'error line = ' + str(line)
     self.transportServerSocket.close()
Exemplo n.º 3
0
    def run(self):
        self.getMyIPMAC()
        self.physicalClientSocket = socket(AF_INET, SOCK_STREAM)
        self.physicalClientSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        self.physicalClientSocket.bind(Layer.PhysicalClient)
        self.physicalClientSocket.listen(1)
        while True:
            self.msg.emit('Waiting datagram from Network layer')
            self.package, success = Layer.receive(self.physicalClientSocket)
            if success:
                self.msg.emit('Received package from Network layer.')
                destiny = json.loads(self.package)['destiny']
                self.destiny = (destiny[0], destiny[1])
                self.msg.emit ('Destiny = ' + str(self.destiny))
                self.getDstMAC(self.destiny)
                self.package = json.loads(self.package)['datagram']
                if success:
                    if not self.mtuReceived:
                        self.connectAsClient(self.destiny)
                    self.createFrame_BinaryFile(self.package, 'binaryRequestClient.txt', 'blue')
                    if self.probCollision != 0:
                        while random.randint(0, 10) <= self.probCollision:
                            rand = random.randint(0, 10)
                            self.msg.emit('Collision detected, ' + str(rand) + ' seconds to retry...')
                            time.sleep(rand)
                    sent = Layer.send(self.destiny, 'binaryRequestClient.txt', self.myMTU)

                    self.msg.emit('Sent binary file to Physical server.')
                    if self.receiveFile(self.physicalClientSocket, 'binaryAnswer.txt'):
                        self.msg.emit('Received binary file from server')
                        self.answer = self.interpretPackage('binaryAnswer.txt', 'red')
                        sent = Layer.send(Layer.NetworkClient, self.answer)
Exemplo n.º 4
0
 def run(self):
     self.networkServerSocket = socket(AF_INET, SOCK_STREAM)
     self.networkServerSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
     self.networkServerSocket.bind (Layer.NetworkServer)
     self.networkServerSocket.listen(1)
     while True:
         self.package, success = Layer.receive(self.networkServerSocket)
         if success:
             self.interpretPackage(self.package, 'blue')
             sent = Layer.send(Layer.TransportServer, self.datagram['data'])
             if sent:
                 self.answer, success = Layer.receive(self.networkServerSocket)
                 self.msg.emit ('Received answer')
                 networkPackage = self.createNetworkPackage(Layer.PhysicalClient,self.createDatagram(self.answer, 'red'))
                 sent = Layer.send(Layer.PhysicalServer, networkPackage)
                 self.msg.emit ('Answer sent to physical layer')
Exemplo n.º 5
0
 def threeWayHandshake(self):
     if self.receive(self.SYN):
         if self.send_SYN_ACK():
             self.segment, success = Layer.receive(self.transportServerSocket)
             self.segment = json.loads(self.segment)
             if self.receive(self.ACK):
                 self.msg.emit('Three way handshake protocol established connection!')
                 #self.receive_Data()
                 return True
     return False
Exemplo n.º 6
0
 def receiveAnswer(self):
     self.answer, success = Layer.receive(self.transportClientSocket)
     if success:
         self.msg.emit('Received answer from Network layer.')
         if self.transportProtocol == 'UDP':
             self.interpretUDPSegment(self.answer)
         else:
             self.interpretTCPSegment(self.answer)
     print 'received answer' + str(success)
     #self.answer = json.loads(self.answer)
     return success
Exemplo n.º 7
0
 def receive_SYN_ACK (self):
     try:
         self.answer, success = Layer.receive(self.transportClientSocket)
         if success:
             self.msg.emit('Checking SYN_ACK')
             self.answer = json.loads(self.answer)
             self.html.emit(PDUPrinter.TCP(self.answer, 'blue'))
             if self.answer['flags'] == self.SYN_ACK:
                 return True
             else:
                 self.msg.emit('Acknowledge sequence number doesn\'t check')
                 return False
     except Exception as exc:
         self.errorMsg.emit('DID NOT Receive SYN_ACK\n' + str(exc))
         return False
Exemplo n.º 8
0
 def run(self):
     self.networkClientSocket = socket(AF_INET, SOCK_STREAM)
     self.networkClientSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
     self.networkClientSocket.bind (Layer.NetworkClient)
     self.networkClientSocket.listen(1)
     try:
         while True:
             self.frame, success = Layer.receive(self.networkClientSocket)
             if success:
                 self.msg.emit ('Received UDP package from Transport Layer')
                 networkPackage = self.createNetworkPackage(Layer.PhysicalServer, self.createDatagram(self.frame, 'blue'))
                 Layer.send(Layer.PhysicalClient, networkPackage)
                 self.msg.emit('Datagram sent to Physical layer.')
                 self.answer, success = Layer.receive(self.networkClientSocket)
                 if success:
                     self.msg.emit('Received answer')
                     self.interpretPackage(self.answer, 'red')
                     sent = Layer.send(Layer.TransportClient, json.loads(self.answer)['data'])
                     self.msg.emit ('Sent msg to transport client ' + str(sent))
     except KeyboardInterrupt:
         print 'Shutting down Internet Layer Client'
         self.saveRouterTable()
         #self.transportSocket.close()
         self.networkClientSocket.close()
Exemplo n.º 9
0
    def run(self):
        self.networkRouterSocket = socket(AF_INET, SOCK_STREAM)
        self.networkRouterSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        self.networkRouterSocket.bind (Layer.NetworkRouter)
        self.networkRouterSocket.listen(1)
        self.msg.emit('Starting router network layer.')
        while True:
            self.package, success = Layer.receive(self.networkRouterSocket)
            if success:
                self.interpretPackage(self.package, 'blue')
                    #maintain port
                # return to physical layer
                self.srcIP = json.loads(self.package)['srcIP']
		dstIP = self.consultTable(self.srcIP, self.mask)
                self.msg.emit ('Source IP = ' + str(self.srcIP))
		transpPackage = json.loads(self.package)['data']
		port = json.loads(transpPackage)['dstPort']
		Layer.PhysicalServer = (dstIP, int(port))
		print 'PhysicalServer = ' + str(Layer.PhysicalServer)
                destiny = self.consultTable(self.srcIP, self.mask), Layer.PhysicalServer[1]
                networkPackage = self.createNetworkPackage(destiny, self.package)
                sent = Layer.send(Layer.PhysicalRouter, networkPackage)
                self.msg.emit ('Answer sent to physical layer')
Exemplo n.º 10
0
 def run(self):
     try:
         self.physicalServerSocket = socket(AF_INET, SOCK_STREAM)
         self.physicalServerSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
         self.physicalServerSocket.bind(Layer.PhysicalServer)
         self.host, self.port = Layer.PhysicalServer
         self.physicalServerSocket.listen(1)
         self.msg.emit("Setup:\nIP = " + str(self.host) + '\tPort = ' + str(self.port) + '\nListening...')
     except Exception as exc:
         self.errorMsg.emit('ERROR! It was not possible start the execution: \n' + str(exc))
     try:
         while True:
             if not self.mtuSent:
                 self.getMyIPMAC()
                 self.connectAsServer(self.physicalServerSocket)
             if self.receiveFile(self.physicalServerSocket, 'binaryRequestServer.txt'):
                 self.package = self.interpretPackage('binaryRequestServer.txt', 'blue')
                 Layer.send(Layer.NetworkServer, self.package)
                 self.msg.emit('Waiting answer...')
                 self.answer, success = Layer.receive(self.physicalServerSocket)
                 self.msg.emit('Received package from Network layer.')
                 destiny = json.loads(self.answer)['destiny']
                 self.getDstMAC(destiny[0])
                 self.answer = json.loads(self.answer)['datagram']
                 if success:
                     self.createFrame_BinaryFile(self.answer, 'server_binary.txt', 'red')
                     #self.sendAnswer()
                     Layer.send(Layer.PhysicalClient, 'server_binary.txt', self.mtu)
     except Exception as exc:
         exc_type, exc_obj, exc_tb = sys.exc_info()
         error = exc_tb.tb_frame
         line = exc_tb.tb_lineno
         fileName = error.f_code.co_filename
         self.errorMsg.emit('ERROR! It was not possible run the server: \n' + str(exc)+
                 '\nLine = ' + str(line))
         return False
Exemplo n.º 11
0
 def receiveFile(self, socket, fileName):
     with open (fileName, "w") as self.rFile:
         data, success = Layer.receive(socket)
         self.rFile.write(data)
     self.msg.emit('Received frame')
     return success