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
def receiveSYNACKPacket(self, sendSock, SYNPacket, localhostIP, destinationIP): self.ack_packet = "" self.cwnd = 1 self.startTime = 0 user_request = "" receiveSock = self.getReceiveRawSocket() self.startTime = time.clock() # Receives packet while True: receiveSock.settimeout(180) try: receivedPacket = receiveSock.recvfrom(65565) except: print "Exception in receiving SYN/ACK packet" sendSock.close() receiveSock.close() sys.exit(2) packet = receivedPacket[0] received_IPHeader = packet[0:20] ipheader = IPHeader(0, 0, 0) ipheader.extract(received_IPHeader) checksum = self.calculateCheckSum(received_IPHeader) time_elapsed = time.clock() - self.startTime if time_elapsed > 60: sendSock.sendto(SYNPacket, (destinationIP, self.destinationPortNum)) self.startTime = time.clock() if (ipheader.protocol == 6) and (checksum == 0) and (ipheader.sourceIP == destinationIP) and (ipheader.destinationIP == localhostIP): get_TCPHeader = self.getTCPHeaderFromIPHeader(packet, ipheader) tcpheader = TCPHeader() tcpheader.extract(get_TCPHeader) if (tcpheader.sourcePort == self.destinationPortNum and tcpheader.destPort == self.sourcePortNum and tcpheader.ack_flg == 1 and tcpheader.ackNo == self.seq_num + 1 and not tcpheader.rst_flg): self.seq_num = tcpheader.ackNo self.current_ack_no = tcpheader.sequenceNo + 1 self.ip_packet_ID = self.ip_packet_ID + 1 ipheader1 = self.getIPHeader(localhostIP, destinationIP, self.ip_packet_ID) user_request = self.getHttpGETRequest(self.url, self.urlHostName) tcp_header1 = self.getTCPHeader(localhostIP, destinationIP, user_request) self.ack_packet = ipheader1 + tcp_header1 + user_request sendSock.sendto(self.ack_packet, (destinationIP, 80)) break # Handshake Completed, start downloading file from the server self.getMessagesFromServer(time.clock(), user_request, localhostIP, destinationIP, sendSock, receiveSock)
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
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
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
def getMessagesFromServer(self, start_Time, user_request, localhostIP, destinationIP, sendSock, receiveSock): self.page_content_dict = {} roundTripTime = time.clock() http_ack_flg = False self.seq_num = self.seq_num + len(user_request) while True: receiveSock.settimeout(180) try: receivedPacket = receiveSock.recvfrom(65565) except receiveSock.timeout: print "Socket timed out. No packet received in 3 minutes" sendSock.close() receiveSock.close() sys.exit(2) spent_Time = time.clock() - start_Time # Resend if the packet is not received before 1min if spent_Time > 60 and not http_ack_flg: sendSock.sendto(self.ack_packet, (destinationIP, 80)) start_Time = time.clock() packet = receivedPacket[0] received_IPHeader = packet[0:20] ipheader = IPHeader(0, 0, 0) ipheader.extract(received_IPHeader) checksum = self.calculateCheckSum(received_IPHeader) # Checks if the protocol is TCP, if the server IP is the source IP in the packet, if the localhostIP is the destination in the packet if ipheader.protocol == 6 and ipheader.sourceIP == destinationIP and ipheader.destinationIP == localhostIP and checksum == 0: get_TCPHeader = self.getTCPHeaderFromIPHeader(packet, ipheader) tcpheader = TCPHeader() tcpheader.extract(get_TCPHeader) # Strip of header length, and gets the actual data header_length = (tcpheader.HL_Offset * 4) + (ipheader.ihl * 4) message_length = ipheader.totalLength - header_length actual_page_content = packet[header_length:] self.checkcwnd(tcpheader) # Checks valid sourcePort,destPort in TCP header if tcpheader.sourcePort == self.destinationPortNum and tcpheader.destPort == self.sourcePortNum and tcpheader.ackNo == self.seq_num: time_spent = time.clock() - roundTripTime if (time_spent > 180): print "No packet received from server for 3 minutes. Terminating program. " sendSock.close() receiveSock.close() sys.exit(2) roundTripTime = time.clock() # Indicates end of data if tcpheader.fin_flg == 1: self.current_ack_no += 1 self.page_content_dict[tcpheader.sequenceNo] = actual_page_content self.startTearDown(self.current_ack_no, localhostIP, destinationIP, sendSock, receiveSock) break # Continue to receive data, indicates this is an ACK if tcpheader.ack_flg == 1 and http_ack_flg == False and tcpheader.ackNo == self.seq_num: http_ack_flg = True continue if tcpheader.syn_flg == 1 and tcpheader.ack_flg == 1: continue else: # Stores Data in the dictionary in sequenceNo:data format if self.page_content_dict.has_key(tcpheader.sequenceNo): self.sendAckToServer(message_length + tcpheader.sequenceNo, localhostIP, destinationIP, sendSock) else: self.current_ack_no = self.current_ack_no + message_length self.page_content_dict[tcpheader.sequenceNo] = actual_page_content self.sendAckToServer(self.current_ack_no, localhostIP, destinationIP, sendSock)
def getIPHeader(self, localhostIP, destinationIP, ip_packet_ID): ipheader_obj = IPHeader(localhostIP, destinationIP, ip_packet_ID) ipheader = ipheader_obj.build() return ipheader
def getMessagesFromServer(self, start_Time, user_request, localhostIP, destinationIP, sendSock, receiveSock): self.page_content_dict = {} roundTripTime = time.clock() http_ack_flg = False self.seq_num = self.seq_num + len(user_request) while True: receiveSock.settimeout(180) try: receivedPacket = receiveSock.recvfrom(65565) except receiveSock.timeout: print "Socket timed out. No packet received in 3 minutes" sendSock.close() receiveSock.close() sys.exit(2) spent_Time = time.clock() - start_Time # Resend if the packet is not received before 1min if spent_Time > 60 and not http_ack_flg: sendSock.sendto(self.ack_packet, (destinationIP, 80)) start_Time = time.clock() packet = receivedPacket[0] received_IPHeader = packet[0:20] ipheader = IPHeader(0, 0, 0) ipheader.extract(received_IPHeader) checksum = self.calculateCheckSum(received_IPHeader) # Checks if the protocol is TCP, if the server IP is the source IP in the packet, if the localhostIP is the destination in the packet if ( ipheader.protocol == 6 and ipheader.sourceIP == destinationIP and ipheader.destinationIP == localhostIP and checksum == 0 ): get_TCPHeader = self.getTCPHeaderFromIPHeader(packet, ipheader) tcpheader = TCPHeader() tcpheader.extract(get_TCPHeader) # Strip of header length, and gets the actual data header_length = (tcpheader.HL_Offset * 4) + (ipheader.ihl * 4) message_length = ipheader.totalLength - header_length actual_page_content = packet[header_length:] self.checkcwnd(tcpheader) # Checks valid sourcePort,destPort in TCP header if ( tcpheader.sourcePort == self.destinationPortNum and tcpheader.destPort == self.sourcePortNum and tcpheader.ackNo == self.seq_num ): time_spent = time.clock() - roundTripTime if time_spent > 180: print "No packet received from server for 3 minutes. Terminating program. " sendSock.close() receiveSock.close() sys.exit(2) roundTripTime = time.clock() # Indicates end of data if tcpheader.fin_flg == 1: self.current_ack_no += 1 self.page_content_dict[tcpheader.sequenceNo] = actual_page_content self.startTearDown(self.current_ack_no, localhostIP, destinationIP, sendSock, receiveSock) break # Continue to receive data, indicates this is an ACK if tcpheader.ack_flg == 1 and http_ack_flg == False and tcpheader.ackNo == self.seq_num: http_ack_flg = True continue if tcpheader.syn_flg == 1 and tcpheader.ack_flg == 1: continue else: # Stores Data in the dictionary in sequenceNo:data format if self.page_content_dict.has_key(tcpheader.sequenceNo): self.sendAckToServer( message_length + tcpheader.sequenceNo, localhostIP, destinationIP, sendSock ) else: self.current_ack_no = self.current_ack_no + message_length self.page_content_dict[tcpheader.sequenceNo] = actual_page_content self.sendAckToServer(self.current_ack_no, localhostIP, destinationIP, sendSock)
def receiveSYNACKPacket(self, sendSock, SYNPacket, localhostIP, destinationIP): self.ack_packet = "" self.cwnd = 1 self.startTime = 0 user_request = "" receiveSock = self.getReceiveRawSocket() self.startTime = time.clock() # Receives packet while True: receiveSock.settimeout(180) try: receivedPacket = receiveSock.recvfrom(65565) except: print "Exception in receiving SYN/ACK packet" sendSock.close() receiveSock.close() sys.exit(2) packet = receivedPacket[0] received_IPHeader = packet[0:20] ipheader = IPHeader(0, 0, 0) ipheader.extract(received_IPHeader) checksum = self.calculateCheckSum(received_IPHeader) time_elapsed = time.clock() - self.startTime if time_elapsed > 60: sendSock.sendto(SYNPacket, (destinationIP, self.destinationPortNum)) self.startTime = time.clock() if ( (ipheader.protocol == 6) and (checksum == 0) and (ipheader.sourceIP == destinationIP) and (ipheader.destinationIP == localhostIP) ): get_TCPHeader = self.getTCPHeaderFromIPHeader(packet, ipheader) tcpheader = TCPHeader() tcpheader.extract(get_TCPHeader) if ( tcpheader.sourcePort == self.destinationPortNum and tcpheader.destPort == self.sourcePortNum and tcpheader.ack_flg == 1 and tcpheader.ackNo == self.seq_num + 1 and not tcpheader.rst_flg ): self.seq_num = tcpheader.ackNo self.current_ack_no = tcpheader.sequenceNo + 1 self.ip_packet_ID = self.ip_packet_ID + 1 ipheader1 = self.getIPHeader(localhostIP, destinationIP, self.ip_packet_ID) user_request = self.getHttpGETRequest(self.url, self.urlHostName) tcp_header1 = self.getTCPHeader(localhostIP, destinationIP, user_request) self.ack_packet = ipheader1 + tcp_header1 + user_request sendSock.sendto(self.ack_packet, (destinationIP, 80)) break # Handshake Completed, start downloading file from the server self.getMessagesFromServer(time.clock(), user_request, localhostIP, destinationIP, sendSock, receiveSock)