def sendAckOrFin(ackNumber,seqNum,ackFlag,finFlag): # Call the function in rawIp.py to get the IP header based on the arguments of this function ipHeader = make_ip_header(sourceIp=sourceIp,destinationIp = destinationIp) #there is no user data in the ACK packets since the client is not gonna send any more data to the server user_data = '' # tcp header fields # Call the function in rawTcp.py to get the TCP header based on the arguments of this function tcpHeader = make_tcp_packet(sourceIp ,destinationIp,tcp_source_global,tcp_destination_global, seqNum,ackNumber,finFlag,0,0,ackFlag,user_data) #Making the final packet packet = ipHeader + tcpHeader + user_data #Send the ACK or FIN Packet sendSocket.sendto(packet,(destinationIp,0))
def tcp_handshake_syn(): # Call the function in rawIp.py to get the IP header based on the arguments of this function ip_header = make_ip_header(sourceIp=sourceIp,destinationIp = destinationIp) #there is no user data in the first SYN packet since the client is initiating the three way handshake user_data = '' # Call the function in rawTcp.py to get the TCP header based on the arguments of this function tcp_header = make_tcp_packet(sourceIp,destinationIp,tcp_source_global, tcp_destination_global,tcp_seq_global,0,0,1,0,0,user_data) #creating the final packet packet = ip_header + tcp_header + user_data #Send the packet finally - the port specified has no effect sendSocket.sendto(packet, (destinationIp , 0 )) # receive a packet packetReceived = True; while packetReceived: packet = receiveSocket.recvfrom(tcp_source_global) packet = packet[0] ip_header = packet[0:20] ipHeader = unpack('!BBHHHBBH4s4s' , ip_header) version_ihl = ipHeader[0] version = version_ihl >> 4 ihl = version_ihl & 0xF iph_length = ihl * 4 tcp_header = packet[iph_length:iph_length+20] tcpHeader = unpack('!HHLLBBHHH' , tcp_header) source_port = tcpHeader[0] dest_port = tcpHeader[1] sequence = tcpHeader[2] acknowledgement = tcpHeader[3] dataOffset_reserved = tcpHeader[4] tcpHeader_length = dataOffset_reserved >> 4 if (acknowledgement == tcp_seq_global + 1) and (dest_port == tcp_source_global) : headerSize = iph_length + tcpHeader_length * 4 data_size = len(packet) - headerSize #get data from the packet data = packet[headerSize:] packetReceived = False; #calling the function to perform handshakee by sending ACK for the SYN ACK packet received #the sequence will be increased by 1 and the ackNumber will be same as the seq number of the #received packet send_req(ackNum = sequence + 1,seqNum = acknowledgement)
def send_req(ackNum,seqNum): # Call the function in rawIp.py to get the IP header based on the arguments of this function ip_header = make_ip_header(sourceIp=sourceIp,destinationIp = destinationIp) # tcp data field user_data ='GET '+ filePath +' HTTP/1.0\r\nHost: '+ address +'\r\nUser-Agent: HTTPTool/1.1\r\nAccept-Language: en-US,en;q=0.5\r\nConnection: keep-alive\r\n\r\n'; # Call the function in rawTcp.py to get the TCP header based on the arguments of this function tcp_header = make_tcp_packet(sourceIp ,destinationIp,tcp_source_global,tcp_destination_global, seqNum ,ackNum,0,0,1,1,user_data) # this is the final packet that needs to be sent to the destination packet = ip_header + tcp_header + user_data sendSocket.sendto(packet, (destinationIp , 0 )) # receive the ACK packet from the server .. this packet contains the sequence number for the receiver used for # the rest of the communication #sending the seq number of the last acked packet get_file(nextSeq = ackNum,clientSeqNum = seqNum + len(user_data))