Shop OBEX P1 Docs P2 Docs Learn Events
PING for ENC28J60 — Parallax Forums

PING for ENC28J60

localrogerlocalroger Posts: 3,451
edited 2013-02-19 19:13 in Propeller 1
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:
          '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

  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2013-02-19 07:37
    Nice!! Thanks for this.

    Jeff
  • ctwardellctwardell Posts: 1,716
    edited 2013-02-19 07:49
    So now Colossus and Guardian can ping each other...

    C.W.
  • localrogerlocalroger Posts: 3,451
    edited 2013-02-19 19:13
    ctwardell wrote: »
    So now Colossus and Guardian can ping each other...

    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.
Sign In or Register to comment.