Пример #1
0
    def getTCPHeader(self, localhostIP, destinationIP, user_request):
        tcpheader_obj = TCPHeader()
        tcpheader_obj.setValues(self.sourcePortNum, self.destinationPortNum, self.seq_num, self.current_ack_no, 0, 1, 0, user_request)

        tcp_header = tcpheader_obj.build(localhostIP, destinationIP)

        return tcp_header
Пример #2
0
    def getTCPHeader(self, localhostIP, destinationIP, user_request):
        tcpheader_obj = TCPHeader()
        tcpheader_obj.setValues(
            self.sourcePortNum, self.destinationPortNum, self.seq_num, self.current_ack_no, 0, 1, 0, user_request
        )

        tcp_header = tcpheader_obj.build(localhostIP, destinationIP)

        return tcp_header
Пример #3
0
    def startTearDown(self, local_current_ack_no, localhostIP, destinationIP, sendSock, receiveSock):
        # Sends ACK to server of the received packet content
        self.sendAckToServer(local_current_ack_no, localhostIP, destinationIP, sendSock)

        self.ip_packet_ID = self.ip_packet_ID + 1
        ipheader = self.getIPHeader(localhostIP, destinationIP, self.ip_packet_ID)

        tcpobj = TCPHeader()
        tcpobj.setValues(self.sourcePortNum, self.destinationPortNum, self.seq_num, local_current_ack_no, 0, 1, 1, "")
        tcpheader = tcpobj.build(localhostIP, destinationIP)

        packet = ipheader + tcpheader + ""

        sendSock.sendto(packet, (destinationIP, 80))
        start_time = time.clock()

        while True:
            try:
                receivedPacket = receiveSock.recvfrom(65565)
            except:
                print "Exception in tear down"
                sys.exit(2)

            packetData = receivedPacket[0]

            received_IPHeader = packetData[0:20]
            ipheader = IPHeader(0, 0, 0)
            ipheader.extract(received_IPHeader)

            checksum = self.calculateCheckSum(received_IPHeader)

            spent_Time = time.clock() - start_time

            # Resend if time exceeds 1min
            if spent_Time > 60:
                sendSock.sendto(packet, (destinationIP, 80))
                start_time = time.clock()
            # Checks if the protocol is 6, and checks the source and destination in the IP Packet
            if (
                ipheader.protocol == 6
                and ipheader.sourceIP == destinationIP
                and ipheader.destinationIP == localhostIP
                and checksum == 0
            ):
                get_TCPHeader = self.getTCPHeaderFromIPHeader(packetData, ipheader)
                tcpheader = TCPHeader()
                tcpheader.extract(get_TCPHeader)

                if (
                    tcpheader.sourcePort == self.destinationPortNum
                    and tcpheader.destPort == self.sourcePortNum
                    and tcpheader.ackNo == self.seq_num + 1
                ):
                    return
Пример #4
0
    def sendAckToServer(self, local_current_ack_no, localhostIP, destinationIP, sendSock):

        self.ip_packet_ID = self.ip_packet_ID + 1
        ipheader = self.getIPHeader(localhostIP, destinationIP, self.ip_packet_ID)

        tcpobj = TCPHeader()
        tcpobj.setValues(self.sourcePortNum, self.destinationPortNum, self.seq_num, local_current_ack_no, 0, 1, 0, "")
        tcpheader = tcpobj.build(localhostIP, destinationIP)

        packet = ipheader + tcpheader + ""

        sendSock.sendto(packet, (destinationIP, self.destinationPortNum))
Пример #5
0
    def sendAckToServer(self, local_current_ack_no, localhostIP, destinationIP, sendSock):

        self.ip_packet_ID = self.ip_packet_ID + 1
        ipheader = self.getIPHeader(localhostIP, destinationIP, self.ip_packet_ID)

        tcpobj = TCPHeader()
        tcpobj.setValues(self.sourcePortNum, self.destinationPortNum, self.seq_num, local_current_ack_no, 0, 1, 0, "")
        tcpheader = tcpobj.build(localhostIP, destinationIP)

        packet = ipheader + tcpheader + ""

        sendSock.sendto(packet, (destinationIP, self.destinationPortNum))
Пример #6
0
    def buildSYNPacket(self, localhostIP, destinationIP):

        IPHeaderObj = IPHeader(localhostIP, destinationIP, self.ip_packet_ID)
        ipHeader = IPHeaderObj.build()
        userData = ""

        TCPObj = TCPHeader()

        #Set the SYN Flag
        TCPObj.setValues(self.sourcePortNum, self.destinationPortNum, self.seq_num, self.current_ack_no, 1, 0, 0, userData)
        tcpHeader = TCPObj.build(localhostIP, destinationIP)

        SYNPacket = ipHeader + tcpHeader + userData
        return SYNPacket
Пример #7
0
    def buildSYNPacket(self, localhostIP, destinationIP):

        IPHeaderObj = IPHeader(localhostIP, destinationIP, self.ip_packet_ID)
        ipHeader = IPHeaderObj.build()
        userData = ""

        TCPObj = TCPHeader()

        # Set the SYN Flag
        TCPObj.setValues(
            self.sourcePortNum, self.destinationPortNum, self.seq_num, self.current_ack_no, 1, 0, 0, userData
        )
        tcpHeader = TCPObj.build(localhostIP, destinationIP)

        SYNPacket = ipHeader + tcpHeader + userData
        return SYNPacket
Пример #8
0
    def startTearDown(self, local_current_ack_no, localhostIP, destinationIP, sendSock, receiveSock):
        # Sends ACK to server of the received packet content
        self.sendAckToServer(local_current_ack_no, localhostIP, destinationIP, sendSock)

        self.ip_packet_ID = self.ip_packet_ID + 1
        ipheader = self.getIPHeader(localhostIP, destinationIP, self.ip_packet_ID)

        tcpobj = TCPHeader()
        tcpobj.setValues(self.sourcePortNum, self.destinationPortNum, self.seq_num, local_current_ack_no, 0, 1, 1, "")
        tcpheader = tcpobj.build(localhostIP, destinationIP)

        packet = ipheader + tcpheader + ""

        sendSock.sendto(packet, (destinationIP, 80))
        start_time = time.clock()

        while True:
            try:
                receivedPacket = receiveSock.recvfrom(65565)
            except:
                print "Exception in tear down"
                sys.exit(2)

            packetData = receivedPacket[0]

            received_IPHeader = packetData[0:20]
            ipheader = IPHeader(0, 0, 0)
            ipheader.extract(received_IPHeader)

            checksum = self.calculateCheckSum(received_IPHeader)

            spent_Time = time.clock() - start_time

            # Resend if time exceeds 1min
            if spent_Time > 60:
                sendSock.sendto(packet, (destinationIP, 80))
                start_time = time.clock()
            # Checks if the protocol is 6, and checks the source and destination in the IP Packet
            if ipheader.protocol == 6 and ipheader.sourceIP == destinationIP and ipheader.destinationIP == localhostIP and checksum == 0:
                get_TCPHeader = self.getTCPHeaderFromIPHeader(packetData, ipheader)
                tcpheader = TCPHeader()
                tcpheader.extract(get_TCPHeader)

                if tcpheader.sourcePort == self.destinationPortNum and tcpheader.destPort == self.sourcePortNum and tcpheader.ackNo == self.seq_num + 1:
                    return