PING for ENC28J60
localroger
Posts: 3,451
Are you using one of Harrison's ENC28J60 ethernet drivers? Are you tired of not being able to ping your prop? I thought so. Just add this to driver_socket.spin:
This is not the most efficient way to do it; some longs could be shaved by sharing the checksum code with the existing TCP handler, but this should work with any of the several versions of the driver that are out there. This was tested with the example_httpserver object.
Now that I've got this working, my real objective will be to implement UDP...
'The following line is commented out in PRI service_packet: PROT_ICMP : handle_ping 'And just add the handler somewhere handy: PRI handle_ping | swapbuf[2], cklen, cksum, pktlen, i ' pktlen := packet[ip_pktlen] << 8 + packet[constant(ip_pktlen+1)] + 14 ' 'swap mac addreses ' bytemove(@swapbuf, @packet+enetpacketsrc0, 6) bytemove(@packet+enetpacketsrc0, @packet+enetpacketdest0, 6) bytemove(@packet+enetpacketdest0, @swapbuf, 6) ' ' set ip id ' ++pkt_id packet[ip_id] := pkt_id >> 8 packet[constant(ip_id + 1)] := pkt_id ' 'swap IP addresses ' bytemove(@swapbuf, @packet+ip_srcaddr, 4) bytemove(@packet+ip_srcaddr, @packet+ip_destaddr, 4) bytemove(@packet+ip_destaddr, @swapbuf, 4) ' ' calc ip header checksum ' packet[ip_hdr_cksum] := $00 packet[constant(ip_hdr_cksum + 1)] := $00 cklen := (packet[ip_vers_len] & $0F) * 4 cksum := calc_chksum(@packet[ip_vers_len], cklen) packet[ip_hdr_cksum] := cksum >> 8 packet[constant(ip_hdr_cksum + 1)] := cksum ' ' set ICMP type to echo reply ' packet[icmp_type] := 0 ' 'zero out original ICMP checksum fields ' packet[icmp_cksum] := 0 packet[constant(icmp_cksum+1)] := 0 ' 'build new checksum ' cksum := calc_chksum(@packet + ip_data, pktlen - ip_data) packet[icmp_cksum] := cksum >> 8 packet[constant(icmp_cksum+1)] := cksum ' ' send the packet nic.start_frame nic.wr_block(@packet, pktlen) nic.send_frame
This is not the most efficient way to do it; some longs could be shaved by sharing the checksum code with the existing TCP handler, but this should work with any of the several versions of the driver that are out there. This was tested with the example_httpserver object.
Now that I've got this working, my real objective will be to implement UDP...
Comments
Jeff
C.W.
THERE IS ANOTHER SYSTEM.
Fortunately, it is running Windows so it's slow, buggy, and can't be reinstalled without an authentication code once it gets hosed by malware. The missiles are OK.