'********************************* ' Very basic IP implementation ' ' by john.timmins@link.co.nz ' ' This file is IP_ICMP_Reply.BAS ' ' Please let me know the bugs! ' '********************************* ' ' 'ICMP / PING reply 'Responds well to 32-byte pings 'Or other even byte datagrams in the 20-38 byte range. 'Needs more work to see why larger datagrams don't get a successful reply ' Icmp_reply: 'Copt RX frame to TX frame as they are very similar. For Temp16 = 0 To Frame_length 'This gets an extra word, but that's OK. See ICMP checksum calculation below. Value16 = Rx_packet(temp16) Tx_packet(temp16) = Value16 Next Temp16 'Modify the Ethernet header for return frame 'Get source MAC Srcmac1 = Tx_packet(offset_frame_src_mac_1) Srcmac2 = Tx_packet(offset_frame_src_mac_2) Srcmac3 = Tx_packet(offset_frame_src_mac_3) 'Copy DEST MAC to SRC MAC Temp16 = Tx_packet(offset_frame_dst_mac_1) Tx_packet(offset_frame_src_mac_1) = Temp16 Temp16 = Tx_packet(offset_frame_dst_mac_2) Tx_packet(offset_frame_src_mac_2) = Temp16 Temp16 = Tx_packet(offset_frame_dst_mac_3) Tx_packet(offset_frame_src_mac_3) = Temp16 'Copy SRC MAC to DEST MAC Tx_packet(offset_frame_dst_mac_1) = Srcmac1 Tx_packet(offset_frame_dst_mac_2) = Srcmac2 Tx_packet(offset_frame_dst_mac_3) = Srcmac3 'Now set up the IP frame 'Save source IP Address Srcip1 = Tx_packet(offset_ip_srcadd_1) Srcip2 = Tx_packet(offset_ip_srcadd_2) 'Copy dest IP to source IP Temp16 = Tx_packet(offset_ip_dstadd_1) Tx_packet(offset_ip_srcadd_1) = Temp16 Temp16 = Tx_packet(offset_ip_dstadd_2) Tx_packet(offset_ip_srcadd_2) = Temp16 'Copy source IP to dest IP Tx_packet(offset_ip_dstadd_1) = Srcip1 Tx_packet(offset_ip_dstadd_2) = Srcip2 'Now do the ICMP (data) area of IP frame 'Change Type to '0' (reply) Value16 = Tx_packet(offset_icmp_typecode) 'Get TYPE/CODE word Value16 = Value16 And &B0000000011111111 'Change ICMP type to '0' (reply) Tx_packet(offset_icmp_typecode) = Value16 'Now calculate new ICMP checksum Tx_packet(offset_icmp_checksum) = 0 'Make checksum Zero (for including in calculation) Ip_data_length = Tx_packet(offset_ip_len) 'Get IP data length (in bytes) Ip_data_length = Ip_data_length / 2 'Adjust for Word transfers Ip_data_length = Ip_data_length + Offset_icmp_typecode 'Point to start of IP data Checksum = 0 'Reset checksum accumulator... For Word_count = Offset_icmp_typecode To Ip_data_length 'Loop through IP Data Value16 = Tx_packet(word_count) 'Get data word ' Value16 = Not Value16 '1's Complement the data Checksum = Checksum + Value16 'Add to checksum Next Word_count 'Now add the carry-over from the 16-bit addition Checksum16 = Loww(checksum) 'Get sum Temp16 = Highw(checksum) 'Get carry's Checksum16 = Checksum16 + Temp16 'Add carry's Checksum16 = Not Checksum16 '1's Complement the Checksum Tx_packet(offset_icmp_checksum) = Checksum16 Value16 = Frame_length 'Reply is the same length as request Transmit_frame 'Send it! Return