EnetIcmp.Mod 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. MODULE EnetIcmp;
  2. (**
  3. AUTHOR: Alexey Morozov, HighDim GmbH, 2015
  4. PURPOSE: Ethernet networking stack, ICMP protocol
  5. *)
  6. IMPORT
  7. EnetBase, Interfaces := EnetInterfaces;
  8. CONST
  9. (** ICMP message types *)
  10. EchoReply* = 0;
  11. DestinationUnreachable* = 3;
  12. Redirect* = 5;
  13. EchoRequest* = 8;
  14. RouterAdvertisement* = 9;
  15. RouterSolicitation* = 10;
  16. TimeExceeded* = 11;
  17. ParameterProblem* = 12;
  18. Timestamp* = 13;
  19. TimestampReply* = 14;
  20. Photuris* = 40;
  21. TYPE
  22. (** ICMP echo packet header *)
  23. IcmpEchoHdr* = RECORD
  24. type*: EnetBase.Int8;
  25. code*: EnetBase.Int8;
  26. checksum*: EnetBase.Int16;
  27. id*: EnetBase.Int16;
  28. seq*: EnetBase.Int16;
  29. END;
  30. (*
  31. Handling of an ICMP packet
  32. *)
  33. PROCEDURE HandlePacket(intf: EnetBase.Interface; packet: EnetBase.Packet; flags: SET);
  34. VAR res: LONGINT;
  35. BEGIN
  36. CASE packet.icmpHdr.type OF
  37. |EchoRequest:
  38. IF packet.ipv4Hdr.dstIpAddr = intf.ipv4Addr.addr[0] THEN (*! reply only if destination address matches *)
  39. packet.icmpHdr.type := EchoReply;
  40. INC(packet.icmpHdr.checksum,8); (* modify the checksum according to the changed message type *)
  41. IF Interfaces.ReplyIp(intf,packet,flags,NIL,res) THEN
  42. END;
  43. END;
  44. ELSE
  45. END;
  46. END HandlePacket;
  47. PROCEDURE Install*(intf: EnetBase.Interface);
  48. BEGIN
  49. EnetBase.SetIpPacketHandler(intf,EnetBase.ProtoIcmp,HandlePacket);
  50. END Install;
  51. END EnetIcmp.