IdRawHeaders.pas 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.3 2/8/2004 12:59:40 PM JPMugaas
  18. Start on DotNET port.
  19. Rev 1.2 10/16/2003 11:05:38 PM SPerry
  20. Reorganization
  21. Rev 1.1 2003.09.30 1:23:02 PM czhower
  22. Stack split for DotNet
  23. Rev 1.0 11/13/2002 08:45:44 AM JPMugaas
  24. }
  25. unit IdRawHeaders;
  26. interface
  27. {$I IdCompilerDefines.inc}
  28. uses
  29. {$IFDEF DOTNET}
  30. System.Net,
  31. {$ENDIF}
  32. IdGlobal,
  33. IdStruct;
  34. // TODO: research subtypes of ICMP header
  35. type
  36. //RFC 3542 definitions
  37. //IPv6 Extension Headers
  38. // types redeclared to avoid dependencies on stack declarations
  39. TIdSunB = class(TIdStruct)
  40. protected
  41. Fs_b1,
  42. Fs_b2,
  43. Fs_b3,
  44. Fs_b4: UInt8;
  45. function GetBytesLen: UInt32; override;
  46. public
  47. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  48. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  49. property s_b1 : UInt8 read Fs_b1 write Fs_b1;
  50. property s_b2 : UInt8 read Fs_b2 write Fs_b2;
  51. property s_b3 : UInt8 read Fs_b3 write Fs_b3;
  52. property s_b4 : UInt8 read Fs_b4 write Fs_b4;
  53. end;
  54. TIdSunW = class(TIdStruct)
  55. protected
  56. Fs_w1, Fs_w2: UInt16;
  57. function GetBytesLen: UInt32; override;
  58. public
  59. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  60. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  61. property s_w1 : UInt16 read Fs_w1 write Fs_w1;
  62. property s_w2 : UInt16 read Fs_w2 write Fs_w2;
  63. end;
  64. TIdInAddr = class(TIdLongWord)
  65. public
  66. procedure CopyFrom(const ASource : TIdInAddr);
  67. end;
  68. { PIdInAddr = ^TIdInAddr;
  69. TIdInAddr = record
  70. case integer of
  71. 0: (S_un_b: TIdSunB);
  72. 1: (S_un_w: TIdSunW);
  73. 2: (S_addr: UInt32);
  74. end; }
  75. TIdNetTime = UInt32; // network byte order
  76. const
  77. //header sizes----------------------------------------------------------------//
  78. Id_ARP_HSIZE = $1C; // ARP header: 28 bytes
  79. Id_DNS_HSIZE = $0C; // DNS header base: 12 bytes
  80. Id_ETH_HSIZE = $0E; // Etherner header: 14 bytes
  81. Id_ICMP_HSIZE = $04; // ICMP header base: 4 bytes
  82. Id_ICMP_ECHO_HSIZE = $08; // ICMP_ECHO header: 8 bytes
  83. Id_ICMP6_ECHO_HSIZE = $08; // ICMPv6_ECHO header: 8 bytes icmp echo header len excluding time */
  84. Id_ICMP_MASK_HSIZE = $0C; // ICMP_MASK header: 12 bytes
  85. Id_ICMP_UNREACH_HSIZE = $08; // ICMP_UNREACH header: 8 bytes
  86. Id_ICMP_TIMEXCEED_HSIZE = $08; // ICMP_TIMXCEED header: 8 bytes
  87. Id_ICMP_REDIRECT_HSIZE = $08; // ICMP_REDIRECT header: 8 bytes
  88. Id_ICMP_TS_HSIZE = $14; // ICMP_TIMESTAMP header: 20 bytes
  89. Id_IGMP_HSIZE = $08; // IGMP header: 8 bytes
  90. Id_IP_HSIZE = $14; // IP header: 20 bytes
  91. Id_IPv6_HSIZE = $28; // IPv6 header
  92. Id_RIP_HSIZE = $18; // RIP header base: 24 bytes
  93. Id_TCP_HSIZE = $14; // TCP header: 20 bytes
  94. Id_UDP_HSIZE = $08; // UDP header: 8 bytes
  95. //fragmentation flags---------------------------------------------------------//
  96. Id_IP_RF = $8000; // reserved fragment flag
  97. Id_IP_DF = $4000; // dont fragment flag
  98. Id_IP_MF = $2000; // more fragments flag
  99. Id_IP_OFFMASK = $1FFF; // mask for fragmenting bits
  100. //TCP control flags-----------------------------------------------------------//
  101. Id_TCP_FIN = $01;
  102. Id_TCP_SYN = $02;
  103. Id_TCP_RST = $04;
  104. Id_TCP_PUSH = $08;
  105. Id_TCP_ACK = $10;
  106. Id_TCP_URG = $20;
  107. //ICMP types------------------------------------------------------------------//
  108. Id_ICMP_ECHOREPLY = 0;
  109. Id_ICMP_UNREACH = 3;
  110. Id_ICMP_SOURCEQUENCH = 4;
  111. Id_ICMP_REDIRECT = 5;
  112. Id_ICMP_ECHO = 8;
  113. Id_ICMP_ROUTERADVERT = 9;
  114. Id_ICMP_ROUTERSOLICIT = 10;
  115. Id_ICMP_TIMXCEED = 11;
  116. Id_ICMP_PARAMPROB = 12;
  117. Id_ICMP_TSTAMP = 13;
  118. Id_ICMP_TSTAMPREPLY = 14;
  119. Id_ICMP_IREQ = 15;
  120. Id_ICMP_IREQREPLY = 16;
  121. Id_ICMP_MASKREQ = 17;
  122. Id_ICMP_MASKREPLY = 18;
  123. Id_ICMP_TRACEROUTE = 30; // RFC1393 Traceroute
  124. Id_ICMP_DATAGRAM_CONV = 31; // RFC1475
  125. Id_ICMP_MOB_HOST_REDIR = 32; // Mobile Host Redirect
  126. Id_ICMP_IPv6_WHERE_ARE_YOU = 33;
  127. Id_ICMP_IPv6_I_AM_HERE = 34;
  128. Id_ICMP_MOB_REG_REQ = 35;
  129. Id_ICMP_MOB_REG_REPLY = 36;
  130. Id_ICMP_SKIP = 39;
  131. Id_ICMP_PHOTURIS = 40; // Photuris [RFC2521]
  132. //ICMP codes------------------------------------------------------------------//
  133. Id_ICMP_UNREACH_NET = 0;
  134. Id_ICMP_UNREACH_HOST = 1;
  135. Id_ICMP_UNREACH_PROTOCOL = 2;
  136. Id_ICMP_UNREACH_PORT = 3;
  137. Id_ICMP_UNREACH_NEEDFRAG = 4;
  138. Id_ICMP_UNREACH_SRCFAIL = 5;
  139. Id_ICMP_UNREACH_NET_UNKNOWN = 6;
  140. Id_ICMP_UNREACH_HOST_UNKNOWN = 7;
  141. Id_ICMP_UNREACH_ISOLATED = 8;
  142. Id_ICMP_UNREACH_NET_PROHIB = 9;
  143. Id_ICMP_UNREACH_HOST_PROHIB = 10;
  144. Id_ICMP_UNREACH_TOSNET = 11;
  145. Id_ICMP_UNREACH_TOSHOST = 12;
  146. Id_ICMP_UNREACH_FILTER_PROHIB = 13;
  147. Id_ICMP_UNREACH_HOST_PRECEDENCE = 14;
  148. Id_ICMP_UNREACH_PRECEDENCE_CUTOFF = 15;
  149. Id_ICMP_REDIRECT_NET = 0;
  150. Id_ICMP_REDIRECT_HOST = 1;
  151. Id_ICMP_REDIRECT_TOSNET = 2;
  152. Id_ICMP_REDIRECT_TOSHOST = 3;
  153. Id_ICMP_TIMXCEED_INTRANS = 0;
  154. Id_ICMP_TIMXCEED_REASS = 1;
  155. Id_ICMP_PARAMPROB_OPTABSENT = 1;
  156. // RFC 1393
  157. Id_ICMP_TRACEROUTE_PACKET_FORWARDED = 0;
  158. Id_ICMP_TRACEROUTE_NO_ROUTE = 1;
  159. Id_ICMP_BAD_SPI = 0; //security parameter error 40
  160. Id_ICMP_AUTH_FAILED = 1;
  161. Id_ICMP_DECOMPRESS_FAILED = 2;
  162. Id_ICMP_DECRYPTION_FAILED = 3;
  163. Id_ICMP_NEED_AUTHENTICATION = 4;
  164. Id_ICMP_NEED_AUTHORIZATION = 5;
  165. // RFC 1475 error codes
  166. // The type for Conversion Failed is 31
  167. Id_ICMP_CONV_UNSPEC = 0;
  168. Id_ICMP_CONV_DONTCONV_OPTION = 1;
  169. Id_ICMP_CONV_UNKNOWN_MAN_OPTION = 2;
  170. Id_ICMP_CONV_UNKNWON_UNSEP_OPTION = 3;
  171. Id_ICMP_CONV_UNSEP_TRANSPORT = 4;
  172. Id_ICMP_CONV_OVERALL_LENGTH_EXCEEDED = 5;
  173. Id_ICMP_CONV_IP_HEADER_LEN_EXCEEDED = 6;
  174. Id_ICMP_CONV_TRANS_PROT_255 = 7; // transport protocol > 255
  175. Id_ICMP_CONV_PORT_OUT_OF_RANGE = 8;
  176. Id_ICMP_CONV_TRANS_HEADER_LEN_EXCEEDED = 9;
  177. Id_ICMP_CONV_32BIT_ROLLOVER_AND_ACK = 10; // 32 Bit Rollover missing and ACK set
  178. Id_ICMP_CONV_UNKNOWN_MAN_TRANS_OPTION = 11;
  179. ICMP_MIN = 8;
  180. //ICMPv6 types----------------------------------------------------------------//
  181. ICMP6_DST_UNREACH = 1;
  182. ICMP6_PACKET_TOO_BIG = 2;
  183. ICMP6_TIME_EXCEEDED = 3;
  184. ICMP6_PARAM_PROB = 4;
  185. // Informational Messages
  186. ICMP6_INFOMSG_MASK = $80; //* all informational messages */
  187. ICMP6_ECHO_REQUEST = 128;
  188. ICMP6_ECHO_REPLY = 129;
  189. ICMP6_MEMBERSHIP_QUERY = 130;
  190. ICMP6_MEMBERSHIP_REPORT = 131;
  191. ICMP6_MEMBERSHIP_REDUCTION = 132;
  192. //ICMPv6 codes----------------------------------------------------------------//
  193. ICMP6_DST_UNREACH_NOROUTE = 0; //* no route to destination */
  194. ICMP6_DST_UNREACH_ADMIN = 1; //* communication with */
  195. //* destination */
  196. //* administratively */
  197. //* prohibited */
  198. ICMP6_DST_UNREACH_NOTNEIGHBOR = 2; //* not a neighbor */
  199. ICMP6_DST_UNREACH_ADDR = 3; //* address unreachable */
  200. ICMP6_DST_UNREACH_NOPORT = 4; //* bad port */
  201. ICMP6_DST_UNREACH_SOURCE_FILTERING = 5; //source address failed ingress/egress policy
  202. ICMP6_DST_UNREACH_REJCT_DST = 6; //reject route to destination
  203. ICMP6_TIME_EXCEED_TRANSIT = 0; //* Hop Limit == 0 in transit */
  204. ICMP6_TIME_EXCEED_REASSEMBLY = 1; //* Reassembly time out */
  205. ICMP6_PARAMPROB_HEADER = 0; //* erroneous header field */
  206. ICMP6_PARAMPROB_NEXTHEADER = 1; //* unrecognized Next Header */
  207. ICMP6_PARAMPROB_OPTION = 2; //* unrecognized IPv6 option */
  208. // ICMPv6 Neighbor Discovery Definitions
  209. ND_ROUTER_SOLICIT = 133;
  210. ND_ROUTER_ADVERT = 134;
  211. ND_NEIGHBOR_SOLICIT = 135;
  212. ND_NEIGHBOR_ADVERT = 136;
  213. ND_REDIRECT = 137;
  214. //IGMP types------------------------------------------------------------------//
  215. Id_IGMP_MEMBERSHIP_QUERY = $11; // membership query
  216. Id_IGMP_V1_MEMBERSHIP_REPORT = $12; // v1 membership report
  217. Id_IGMP_V2_MEMBERSHIP_REPORT = $16; // v2 membership report
  218. Id_IGMP_LEAVE_GROUP = $17; // leave-group message
  219. //ethernet packet types-------------------------------------------------------//
  220. Id_ETHERTYPE_PUP = $0200; // PUP protocol
  221. Id_ETHERTYPE_IP = $0800; // IP protocol
  222. Id_ETHERTYPE_ARP = $0806; // ARP protocol
  223. Id_ETHERTYPE_REVARP = $8035; // reverse ARP protocol
  224. Id_ETHERTYPE_VLAN = $8100; // IEEE 802.1Q VLAN tagging
  225. Id_ETHERTYPE_LOOPBACK = $9000; // used to test interfaces
  226. //hardware address formats----------------------------------------------------//
  227. Id_ARPHRD_ETHER = 1; // ethernet hardware format
  228. //ARP operation types---------------------------------------------------------//
  229. Id_ARPOP_REQUEST = 1; // req to resolve address
  230. Id_ARPOP_REPLY = 2; // resp to previous request
  231. Id_ARPOP_REVREQUEST = 3; // req protocol address given hardware
  232. Id_ARPOP_REVREPLY = 4; // resp giving protocol address
  233. Id_ARPOP_INVREQUEST = 8; // req to identify peer
  234. Id_ARPOP_INVREPLY = 9; // resp identifying peer
  235. //RIP commands----------------------------------------------------------------//
  236. Id_RIPCMD_REQUEST = 1; // want info
  237. Id_RIPCMD_RESPONSE = 2; // responding to request
  238. Id_RIPCMD_TRACEON = 3; // turn tracing on
  239. Id_RIPCMD_TRACEOFF = 4; // turn it off
  240. Id_RIPCMD_POLL = 5; // like request, but anyone answers
  241. Id_RIPCMD_POLLENTRY = 6; // like poll, but for entire entry
  242. Id_RIPCMD_MAX = 7;
  243. //RIP versions----------------------------------------------------------------//
  244. Id_RIPVER_0 = 0;
  245. Id_RIPVER_1 = 1;
  246. Id_RIPVER_2 = 2;
  247. //----------------------------------------------------------------------------//
  248. Id_MAX_IPOPTLEN = 40;
  249. Id_IP_MAXPACKET = 65535;
  250. Id_ETHER_ADDR_LEN = 6;
  251. type
  252. ////////////////////////////////////////////////////////////////////////////////
  253. //ICMP//////////////////////////////////////////////////////////////////////////
  254. ////////////////////////////////////////////////////////////////////////////////
  255. TIdICMPEcho = class(TIdStruct)
  256. protected
  257. Fid: UInt16; // identifier to match requests with replies
  258. Fseq: UInt16; // sequence number to match requests with replies
  259. function GetBytesLen: UInt32; override;
  260. public
  261. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  262. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  263. property id: UInt16 read Fid write FId; // identifier to match requests with replies
  264. property seq: UInt16 read Fseq write FSeq; // sequence number to match requests with replies
  265. end;
  266. TIdICMPFrag = class(TIdStruct)
  267. protected
  268. Fpad: UInt16;
  269. Fmtu: UInt16;
  270. function GetBytesLen: UInt32; override;
  271. public
  272. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  273. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  274. property pad: UInt16 read Fpad write Fpad;
  275. property mtu: UInt16 read Fmtu write Fmtu;
  276. end;
  277. TIdICMPTs = class(TIdStruct)
  278. protected
  279. Fotime: TIdNetTime; // time message was sent, to calc roundtrip time
  280. Frtime: TIdNetTime;
  281. Fttime: TIdNetTime;
  282. function GetBytesLen: UInt32; override;
  283. public
  284. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  285. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  286. property otime: TIdNetTime read Fotime write Fotime; // time message was sent, to calc roundtrip time
  287. property rtime: TIdNetTime read Frtime write Frtime;
  288. property ttime: TIdNetTime read Fttime write Fttime;
  289. end;
  290. { packet header }
  291. TIdicmp_hun = class(TIdUnion)
  292. protected
  293. function Getecho_id: UInt16;
  294. function Getecho_seq: UInt16;
  295. function Getfrag_mtu: UInt16;
  296. function Getfrag_pad: UInt16;
  297. function Getgateway_s_b1: UInt8;
  298. function Getgateway_s_b2: UInt8;
  299. function Getgateway_s_b3: UInt8;
  300. function Getgateway_s_b4: UInt8;
  301. function Getgateway_s_l: UInt32;
  302. function Getgateway_s_w1: UInt16;
  303. function Getgateway_s_w2: UInt16;
  304. procedure Setecho_id(const Value: UInt16);
  305. procedure Setecho_seq(const Value: UInt16);
  306. procedure Setfrag_mtu(const Value: UInt16);
  307. procedure Setfrag_pad(const Value: UInt16);
  308. procedure Setgateway_s_b1(const Value: UInt8);
  309. procedure Setgateway_s_b2(const Value: UInt8);
  310. procedure Setgateway_s_b3(const Value: UInt8);
  311. procedure Setgateway_s_b4(const Value: UInt8);
  312. procedure Setgateway_s_l(const Value: UInt32);
  313. procedure Setgateway_s_w1(const Value: UInt16);
  314. procedure Setgateway_s_w2(const Value: UInt16);
  315. public
  316. constructor Create; override;
  317. property echo_id: UInt16 read Getecho_id write Setecho_id; // identifier to match requests with replies
  318. property echo_seq: UInt16 read Getecho_seq write Setecho_seq;
  319. property gateway_s_b1 : UInt8 read Getgateway_s_b1 write Setgateway_s_b1;
  320. property gateway_s_b2 : UInt8 read Getgateway_s_b2 write Setgateway_s_b2;
  321. property gateway_s_b3 : UInt8 read Getgateway_s_b3 write Setgateway_s_b3;
  322. property gateway_s_b4 : UInt8 read Getgateway_s_b4 write Setgateway_s_b4;
  323. property gateway_s_w1 : UInt16 read Getgateway_s_w1 write Setgateway_s_w1;
  324. property gateway_s_w2 : UInt16 read Getgateway_s_w2 write Setgateway_s_w2;
  325. property gateway_s_l : UInt32 read Getgateway_s_l write Setgateway_s_l;
  326. property frag_pad: UInt16 read Getfrag_pad write Setfrag_pad;
  327. property frag_mtu: UInt16 read Getfrag_mtu write Setfrag_mtu;
  328. end;
  329. TIdicmp_dun = class(TIdUnion)
  330. protected
  331. function Getdata: UInt8;
  332. function Getmask: UInt32;
  333. procedure setdata(const Value: UInt8);
  334. procedure Setmask(const Value: UInt32);
  335. function Getts_otime: TIdNetTime;
  336. function Getts_rtime: TIdNetTime;
  337. function Getts_ttime: TIdNetTime;
  338. procedure Setts_otime(const Value: TIdNetTime);
  339. procedure Setts_rtime(const Value: TIdNetTime);
  340. procedure Setts_ttime(const Value: TIdNetTime);
  341. public
  342. constructor Create; override;
  343. property ts_otime: TIdNetTime read Getts_otime write Setts_otime; // time message was sent, to calc roundtrip time
  344. property ts_rtime: TIdNetTime read Getts_rtime write Setts_rtime;
  345. property ts_ttime: TIdNetTime read Getts_ttime write Setts_ttime;
  346. property mask : UInt32 read Getmask write Setmask;
  347. property data : UInt8 read Getdata write setdata;
  348. end;
  349. TIdICMPHdr = class(TIdStruct)
  350. protected
  351. Ficmp_type: UInt8; // message type
  352. Ficmp_code: UInt8; // error code
  353. Ficmp_sum: UInt16; // one's complement checksum {Do not Localize}
  354. Ficmp_hun: TIdicmp_hun;
  355. function GetBytesLen: UInt32; override;
  356. public
  357. constructor Create; override;
  358. destructor Destroy; override;
  359. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  360. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  361. property icmp_type: UInt8 read Ficmp_type write Ficmp_type; // message type
  362. property icmp_code: UInt8 read Ficmp_code write Ficmp_code; // error code
  363. property icmp_sum: UInt16 read Ficmp_sum write Ficmp_sum; // one's complement checksum
  364. property icmp_hun: TIdicmp_hun read Ficmp_hun;
  365. end;
  366. //ICMPv6
  367. TIdicmp6_un = class(TIdUnion)
  368. protected
  369. function Geticmp6_data16: UInt16;
  370. function Geticmp6_data8: UInt8;
  371. procedure Seticmp6_data16(const Value: UInt16);
  372. procedure Seticmp6_data8(const Value: UInt8);
  373. function Geticmp6_seq: UInt16;
  374. procedure Seticmp6_seq(const Value: UInt16);
  375. function Geticmp6_un_data16(Index: Integer): UInt16;
  376. function Geticmp6_un_data32: UInt32;
  377. function Geticmp6_un_data8(Index: Integer): UInt8;
  378. procedure Seticmp6_un_data16(Index: Integer; const Value: UInt16);
  379. procedure Seticmp6_un_data32(const Value: UInt32);
  380. procedure Seticmp6_un_data8(Index: Integer; const Value: UInt8);
  381. {
  382. Ficmp6_un_data32 : UInt32; //* type-specific field */
  383. Ficmp6_un_data16 : array[0..1] of UInt16; //* type-specific field */
  384. icmp6_un_data8 : array[0..3] of UInt8); //* type-specific field */
  385. }
  386. public
  387. constructor Create; override;
  388. property icmp6_un_data32 : UInt32 read Geticmp6_un_data32 write Seticmp6_un_data32; //* type-specific field */
  389. property icmp6_un_data16[Index:Integer] : UInt16 read Geticmp6_un_data16 write Seticmp6_un_data16; //array 0-1 * type-specific field */
  390. property icmp6_un_data8[Index:Integer] : UInt8 read Geticmp6_un_data8 write Seticmp6_un_data8; //array[0-3] * type-specific field */
  391. property icmp6_data32 : UInt32 read Geticmp6_un_data32 write Seticmp6_un_data32;
  392. property icmp6_data16 : UInt16 read Geticmp6_data16 write Seticmp6_data16;
  393. property icmp6_data8 : UInt8 read Geticmp6_data8 write Seticmp6_data8;
  394. property icmp6_pptr : UInt32 read Geticmp6_un_data32 write Seticmp6_un_data32;
  395. property icmp6_mtu : UInt32 read Geticmp6_un_data32 write Seticmp6_un_data32;
  396. property icmp6_id : UInt16 read Geticmp6_data16 write Seticmp6_data16;
  397. property icmp6_seq : UInt16 read Geticmp6_seq write Seticmp6_seq;
  398. property icmp6_maxdelay : UInt16 read Geticmp6_data16 write Seticmp6_data16;
  399. end;
  400. TIdicmp6_hdr = class(TIdStruct)
  401. protected
  402. Ficmp6_type : UInt8; //* type field */
  403. FIcmp6_code : UInt8; //* code field */
  404. Ficmp6_cksum : UInt16; //* checksum field */
  405. Fdata : TIdicmp6_un;
  406. function GetBytesLen: UInt32; override;
  407. public
  408. constructor Create; override;
  409. destructor Destroy; override;
  410. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  411. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  412. property icmp6_type : UInt8 read Ficmp6_type write Ficmp6_type; //* type field */
  413. property icmp6_code : UInt8 read Ficmp6_code write Ficmp6_code; //* code field */
  414. property icmp6_cksum : UInt16 read Ficmp6_cksum write Ficmp6_cksum; //* checksum field */
  415. property data : TIdicmp6_un read Fdata;
  416. { case Integer of
  417. 1: (icmp6_un_data32 : UInt32); //* type-specific field */
  418. 2: (icmp6_un_data16 : array[0..1] of UInt16); //* type-specific field */
  419. 3: (icmp6_un_data8 : array[0..3] of UInt8); //* type-specific field */
  420. }
  421. end;
  422. ////////////////////////////////////////////////////////////////////////////////
  423. //IP////////////////////////////////////////////////////////////////////////////
  424. ////////////////////////////////////////////////////////////////////////////////
  425. { options struct }
  426. TIdIPOptions = class(TIdUnion)
  427. public
  428. constructor Create; override;
  429. //Delphi outputs warnings such as:
  430. //[Hint] H2368 Visibility of property accessor method TIdIPOptions.get_ipopt_list should match property TIdIPOptions.ipopt_list
  431. //[Hint] H2368 Visibility of property accessor method TIdIPOptions.set_ipopt_list should match property TIdIPOptions.ipopt_list
  432. //if these aren't public
  433. function get_ipopt_list(Index: Integer): UInt8;
  434. procedure set_ipopt_list(Index: Integer; const Value: UInt8);
  435. property ipopt_list[Index : Integer] : UInt8 read get_ipopt_list write set_ipopt_list; default; //options proper
  436. end;
  437. { packet header }
  438. TIdIPHdr = class(TIdStruct)
  439. protected
  440. Fip_verlen: UInt8; // 1st nibble version, 2nd nibble header length div 4 (little-endian)
  441. Fip_tos: UInt8; // type of service
  442. Fip_len: UInt16; // total length
  443. Fip_id: UInt16; // identification
  444. Fip_off: UInt16; // 1st nibble flags, next 3 nibbles fragment offset (little-endian)
  445. Fip_ttl: UInt8; // time to live
  446. Fip_p: UInt8; // protocol
  447. Fip_sum: UInt16; // checksum
  448. Fip_src: TIdInAddr; // source address
  449. Fip_dst: TIdInAddr; // dest address
  450. Fip_options: UInt32; // options + padding
  451. function GetBytesLen: UInt32; override;
  452. public
  453. constructor Create; override;
  454. destructor Destroy; override;
  455. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  456. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  457. procedure CopyFrom(const ASource : TIdIPHdr);
  458. property ip_verlen: UInt8 read Fip_verlen write Fip_verlen; // 1st nibble version, 2nd nibble header length div 4 (little-endian)
  459. property ip_tos: UInt8 read Fip_tos write Fip_tos; // type of service
  460. property ip_len: UInt16 read Fip_len write Fip_len; // total length
  461. property ip_id: UInt16 read Fip_id write Fip_id; // identification
  462. property ip_off: UInt16 read Fip_off write Fip_off; // 1st nibble flags, next 3 nibbles fragment offset (little-endian)
  463. property ip_ttl: UInt8 read Fip_ttl write Fip_ttl; // time to live
  464. property ip_p: UInt8 read Fip_p write Fip_p; // protocol
  465. property ip_sum: UInt16 read Fip_sum write Fip_sum; // checksum
  466. property ip_src: TIdInAddr read Fip_src; // source address
  467. property ip_dst: TIdInAddr read Fip_dst; // dest address
  468. property ip_options: UInt32 read Fip_options write Fip_options; // options + padding
  469. end;
  470. ////////////////////////////////////////////////////////////////////////////////
  471. //TCP///////////////////////////////////////////////////////////////////////////
  472. ////////////////////////////////////////////////////////////////////////////////
  473. { options structure }
  474. TIdTCPOptions = class(TIdUnion)
  475. public
  476. constructor Create; override;
  477. //Delphi outputs warnings such as:
  478. //[Hint] H2368 Visibility of property accessor method TIdTCPOptions.gettcpopt_list should match property TIdTCPOptions.tcpopt_list
  479. //[Hint] H2368 Visibility of property accessor method TIdIPOptions.settcpopt_list should match property TIdTCPOptions.tcpopt_list
  480. //if these aren't public
  481. function gettcpopt_list(Index: Integer): UInt8;
  482. procedure settcpopt_list(Index: Integer; const Value: UInt8);
  483. property tcpopt_list[Index : Integer] : UInt8 read gettcpopt_list write settcpopt_list; default;
  484. end;
  485. { packet header }
  486. TIdTCPHdr = class(TIdStruct)
  487. protected
  488. Ftcp_sport: UInt16; // source port
  489. Ftcp_dport: UInt16; // destination port
  490. Ftcp_seq: UInt32; // sequence number
  491. Ftcp_ack: UInt32; // acknowledgement number
  492. Ftcp_x2off: UInt8; // data offset
  493. Ftcp_flags: UInt8; // control flags
  494. Ftcp_win: UInt16; // window
  495. Ftcp_sum: UInt16; // checksum
  496. Ftcp_urp: UInt16; // urgent pointer
  497. function GetBytesLen: UInt32; override;
  498. public
  499. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  500. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  501. property tcp_sport: UInt16 read Ftcp_sport write Ftcp_sport; // source port
  502. property tcp_dport: UInt16 read Ftcp_dport write Ftcp_dport; // destination port
  503. property tcp_seq: UInt32 read Ftcp_seq write Ftcp_seq; // sequence number
  504. property tcp_ack: UInt32 read Ftcp_ack write Ftcp_ack; // acknowledgement number
  505. property tcp_x2off: UInt8 read Ftcp_x2off write Ftcp_x2off; // data offset
  506. property tcp_flags: UInt8 read Ftcp_flags write Ftcp_flags; // control flags
  507. property tcp_win: UInt16 read Ftcp_win write Ftcp_win; // window
  508. property tcp_sum: UInt16 read Ftcp_sum write Ftcp_sum; // checksum
  509. property tcp_urp: UInt16 read Ftcp_urp write Ftcp_urp; // urgent pointer
  510. end;
  511. ////////////////////////////////////////////////////////////////////////////////
  512. //UDP///////////////////////////////////////////////////////////////////////////
  513. ////////////////////////////////////////////////////////////////////////////////
  514. { packet header }
  515. TIdUDPHdr = class(TIdStruct)
  516. protected
  517. Fudp_sport: UInt16; // source port
  518. Fudp_dport: UInt16; // destination port
  519. Fudp_ulen: UInt16; // length
  520. Fudp_sum: UInt16; // checksum
  521. function GetBytesLen: UInt32; override;
  522. public
  523. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  524. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  525. property udp_sport: UInt16 read Fudp_sport write Fudp_sport; // source port
  526. property udp_dport: UInt16 read Fudp_dport write Fudp_dport; // destination port
  527. property udp_ulen: UInt16 read Fudp_ulen write Fudp_ulen; // length
  528. property udp_sum: UInt16 read Fudp_sum write Fudp_sum; // checksum
  529. end;
  530. ////////////////////////////////////////////////////////////////////////////////
  531. //IGMP//////////////////////////////////////////////////////////////////////////
  532. ////////////////////////////////////////////////////////////////////////////////
  533. { packet header }
  534. TIdIGMPHdr = class(TIdStruct)
  535. protected
  536. Figmp_type: UInt8;
  537. Figmp_code: UInt8;
  538. Figmp_sum: UInt16;
  539. Figmp_group: TIdInAddr;
  540. function GetBytesLen: UInt32; override;
  541. public
  542. constructor Create; override;
  543. destructor Destroy; override;
  544. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  545. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  546. property igmp_type: UInt8 read Figmp_type write Figmp_type;
  547. property igmp_code: UInt8 read Figmp_code write Figmp_code;
  548. property igmp_sum: UInt16 read Figmp_sum write Figmp_sum;
  549. property igmp_group: TIdInAddr read Figmp_group;
  550. end;
  551. ////////////////////////////////////////////////////////////////////////////////
  552. //ETHERNET//////////////////////////////////////////////////////////////////////
  553. ////////////////////////////////////////////////////////////////////////////////
  554. TIdEtherAddr = class(TIdUnion)
  555. public
  556. constructor Create; override;
  557. procedure CopyFrom(const ASource : TIdEtherAddr);
  558. procedure SetData(const Value: TIdBytes);
  559. //Delphi outputs warnings such as:
  560. //[Hint] H2368 Visibility of property accessor method TIdEtherAddr.getether_addr_octet should match property TIdEtherAddr.ether_addr_octet
  561. //[Hint] H2368 Visibility of property accessor method TIdEtherAddr.setether_addr_octet should match property TIdEtherAddr.ether_addr_octet
  562. //if these aren't public
  563. function getether_addr_octet(Index: Integer): UInt8;
  564. procedure setether_addr_octet(Index: Integer; const Value: UInt8);
  565. property ether_addr_octet[Index: Integer] : UInt8 read getether_addr_octet write setether_addr_octet; default;
  566. property Data: TIdBytes read FBuffer write SetData;
  567. end;
  568. { packet header }
  569. TIdEthernetHdr = class(TIdStruct)
  570. protected
  571. Fether_dhost: TIdEtherAddr; // destination ethernet address
  572. Fether_shost: TIdEtherAddr; // source ethernet address
  573. Fether_type: UInt16; // packet type ID
  574. function GetBytesLen: UInt32; override;
  575. public
  576. constructor Create; override;
  577. destructor Destroy; override;
  578. procedure CopyFrom(const ASource : TIdEthernetHdr);
  579. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  580. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  581. property ether_dhost: TIdEtherAddr read Fether_dhost; // destination ethernet address
  582. property ether_shost: TIdEtherAddr read Fether_shost; // source ethernet address
  583. property ether_type: UInt16 read Fether_type write Fether_type; // packet type ID
  584. end;
  585. ////////////////////////////////////////////////////////////////////////////////
  586. //ARP///////////////////////////////////////////////////////////////////////////
  587. ////////////////////////////////////////////////////////////////////////////////
  588. { packet header }
  589. TIdARPHdr = class(TIdStruct)
  590. protected
  591. Farp_hrd: UInt16; // format of hardware address
  592. Farp_pro: UInt16; // format of protocol address
  593. Farp_hln: UInt8; // length of hardware address
  594. Farp_pln: UInt8; // length of protocol addres
  595. Farp_op: UInt16; // operation type
  596. // following hardcoded for ethernet/IP
  597. Farp_sha: TIdEtherAddr; // sender hardware address
  598. Farp_spa: TIdInAddr; // sender protocol address
  599. Farp_tha: TIdEtherAddr; // target hardware address
  600. Farp_tpa: TIdInAddr; // target protocol address
  601. function GetBytesLen: UInt32; override;
  602. public
  603. constructor Create; override;
  604. destructor Destroy; override;
  605. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  606. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  607. property arp_hrd: UInt16 read Farp_hrd write Farp_hrd; // format of hardware address
  608. property arp_pro: UInt16 read Farp_pro write Farp_pro; // format of protocol address
  609. property arp_hln: UInt8 read Farp_hln write Farp_hln; // length of hardware address
  610. property arp_pln: UInt8 read Farp_pln write Farp_pln; // length of protocol addres
  611. property arp_op: UInt16 read Farp_op write Farp_op; // operation type
  612. // following hardcoded for ethernet/IP
  613. property arp_sha: TIdEtherAddr read Farp_sha; // sender hardware address
  614. property arp_spa: TIdInAddr read Farp_spa; // sender protocol address
  615. property arp_tha: TIdEtherAddr read Farp_tha; // target hardware address
  616. property arp_tpa: TIdInAddr read Farp_tpa; // target protocol address
  617. end;
  618. ////////////////////////////////////////////////////////////////////////////////
  619. //DNS///////////////////////////////////////////////////////////////////////////
  620. ////////////////////////////////////////////////////////////////////////////////
  621. { header }
  622. TIdDNSHdr = class(TIdStruct)
  623. protected
  624. Fdns_id: UInt16; // DNS packet ID
  625. Fdns_flags: UInt16; // DNS flags
  626. Fdns_num_q: UInt16; // number of questions
  627. Fdns_num_answ_rr: UInt16; // number of answer resource records
  628. Fdns_num_auth_rr: UInt16; // number of authority resource records
  629. Fdns_num_addi_rr: UInt16; // number of additional resource records
  630. function GetBytesLen: UInt32; override;
  631. public
  632. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  633. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  634. property dns_id: UInt16 read Fdns_id write Fdns_id; // DNS packet ID
  635. property dns_flags: UInt16 read Fdns_flags write Fdns_flags; // DNS flags
  636. property dns_num_q: UInt16 read Fdns_num_q write Fdns_num_q; // number of questions
  637. property dns_num_answ_rr: UInt16 read Fdns_num_answ_rr write Fdns_num_answ_rr; // number of answer resource records
  638. property dns_num_auth_rr: UInt16 read Fdns_num_auth_rr write Fdns_num_auth_rr; // number of authority resource records
  639. property dns_num_addi_rr: UInt16 read Fdns_num_addi_rr write Fdns_num_addi_rr; // number of additional resource records
  640. end;
  641. ////////////////////////////////////////////////////////////////////////////////
  642. //RIP///////////////////////////////////////////////////////////////////////////
  643. ////////////////////////////////////////////////////////////////////////////////
  644. { header }
  645. TIdRIPHdr = class(TIdStruct)
  646. protected
  647. Frip_cmd: UInt8; // RIP command
  648. Frip_ver: UInt8; // RIP version
  649. Frip_rd: UInt16; // zero (v1) or routing domain (v2)
  650. Frip_af: UInt16; // address family
  651. Frip_rt: UInt16; // zero (v1) or route tag (v2)
  652. Frip_addr: UInt32; // IP address
  653. Frip_mask: UInt32; // zero (v1) or subnet mask (v2)
  654. Frip_next_hop: UInt32; // zero (v1) or next hop IP address (v2)
  655. Frip_metric: UInt32; // metric
  656. function GetBytesLen: UInt32; override;
  657. public
  658. procedure ReadStruct(const ABytes : TIdBytes; var VIndex : UInt32); override;
  659. procedure WriteStruct(var VBytes : TIdBytes; var VIndex : UInt32); override;
  660. property rip_cmd: UInt8 read Frip_cmd write Frip_cmd; // RIP command
  661. property rip_ver: UInt8 read Frip_ver write Frip_ver; // RIP version
  662. property rip_rd: UInt16 read Frip_rd write Frip_rd; // zero (v1) or routing domain (v2)
  663. property rip_af: UInt16 read Frip_af write Frip_af; // address family
  664. property rip_rt: UInt16 read Frip_rt write Frip_rt; // zero (v1) or route tag (v2)
  665. property rip_addr: UInt32 read Frip_addr write Frip_addr; // IP address
  666. property rip_mask: UInt32 read Frip_mask write Frip_mask; // zero (v1) or subnet mask (v2)
  667. property rip_next_hop: UInt32 read Frip_next_hop write Frip_next_hop; // zero (v1) or next hop IP address (v2)
  668. property rip_metric: UInt32 read Frip_metric write Frip_metric; // metric
  669. end;
  670. ////////////////////////////////////////////////////////////////////////////////
  671. implementation
  672. uses
  673. SysUtils;
  674. { TIdSunB }
  675. function TIdSunB.GetBytesLen: UInt32;
  676. begin
  677. Result := inherited GetBytesLen + 4;
  678. end;
  679. procedure TIdSunB.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  680. begin
  681. inherited ReadStruct(ABytes, VIndex);
  682. Fs_b1 := ABytes[VIndex];
  683. Inc(VIndex);
  684. Fs_b2 := ABytes[VIndex];
  685. Inc(VIndex);
  686. Fs_b3 := ABytes[VIndex];
  687. Inc(VIndex);
  688. Fs_b4 := ABytes[VIndex];
  689. Inc(VIndex);
  690. end;
  691. procedure TIdSunB.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  692. begin
  693. inherited WriteStruct(VBytes, VIndex);
  694. VBytes[VIndex] := Fs_b1;
  695. Inc(VIndex);
  696. VBytes[VIndex] := Fs_b2;
  697. Inc(VIndex);
  698. VBytes[VIndex] := Fs_b3;
  699. Inc(VIndex);
  700. VBytes[VIndex] := Fs_b4;
  701. Inc(VIndex);
  702. end;
  703. { TIdSunW }
  704. function TIdSunW.GetBytesLen: UInt32;
  705. begin
  706. Result := inherited GetBytesLen + 4;
  707. end;
  708. procedure TIdSunW.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  709. begin
  710. inherited ReadStruct(ABytes, VIndex);
  711. Fs_w1 := BytesToUInt16(ABytes, VIndex);
  712. Inc(VIndex, 2);
  713. Fs_w2 := BytesToUInt16(ABytes, VIndex);
  714. Inc(VIndex, 2);
  715. end;
  716. procedure TIdSunW.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  717. begin
  718. inherited WriteStruct(VBytes, VIndex);
  719. CopyTIdUInt16(HostToLittleEndian(Fs_w1), VBytes, VIndex);
  720. Inc(VIndex, 2);
  721. CopyTIdUInt16(HostToLittleEndian(Fs_w2), VBytes, VIndex);
  722. Inc(VIndex, 2);
  723. end;
  724. { TIdICMPEcho }
  725. function TIdICMPEcho.GetBytesLen: UInt32;
  726. begin
  727. Result := inherited GetBytesLen + 4;
  728. end;
  729. procedure TIdICMPEcho.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  730. begin
  731. inherited ReadStruct(ABytes, VIndex);
  732. Fid := BytesToUInt16(ABytes, VIndex);
  733. Inc(VIndex, 2);
  734. seq := BytesToUInt16(ABytes, VIndex);
  735. Inc(VIndex, 2);
  736. end;
  737. procedure TIdICMPEcho.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  738. begin
  739. inherited WriteStruct(VBytes, VIndex);
  740. CopyTIdUInt16(HostToLittleEndian(Fid), VBytes, VIndex);
  741. Inc(VIndex, 2);
  742. CopyTIdUInt16(HostToLittleEndian(seq), VBytes, VIndex);
  743. Inc(VIndex, 2);
  744. end;
  745. { TIdICMPFrag }
  746. function TIdICMPFrag.GetBytesLen: UInt32;
  747. begin
  748. Result := inherited GetBytesLen + 4;
  749. end;
  750. procedure TIdICMPFrag.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  751. begin
  752. inherited ReadStruct(ABytes, VIndex);
  753. Fpad := BytesToUInt16(ABytes, VIndex);
  754. Inc(VIndex, 2);
  755. Fmtu := BytesToUInt16(ABytes, VIndex);
  756. Inc(VIndex, 2);
  757. end;
  758. procedure TIdICMPFrag.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  759. begin
  760. inherited WriteStruct(VBytes, VIndex);
  761. CopyTIdUInt16(HostToLittleEndian(Fpad), VBytes, VIndex);
  762. Inc(VIndex, 2);
  763. CopyTIdUInt16(HostToLittleEndian(Fmtu), VBytes, VIndex);
  764. Inc(VIndex, 2);
  765. end;
  766. { TIdICMPTs }
  767. function TIdICMPTs.GetBytesLen: UInt32;
  768. begin
  769. Result := inherited GetBytesLen + 12;
  770. end;
  771. procedure TIdICMPTs.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  772. begin
  773. inherited ReadStruct(ABytes, VIndex);
  774. Fotime := BytesToUInt32(ABytes, VIndex); // time message was sent, to calc roundtrip time
  775. Inc(VIndex, 4);
  776. Frtime := BytesToUInt32(ABytes, VIndex);
  777. Inc(VIndex, 4);
  778. Fttime := BytesToUInt32(ABytes, VIndex);
  779. Inc(VIndex, 4);
  780. end;
  781. procedure TIdICMPTs.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  782. begin
  783. inherited WriteStruct(VBytes, VIndex);
  784. CopyTIdUInt16(HostToLittleEndian(Fotime), VBytes, VIndex); // time message was sent, to calc roundtrip time
  785. Inc(VIndex, 4);
  786. CopyTIdUInt16(HostToLittleEndian(Frtime), VBytes, VIndex);
  787. Inc(VIndex, 4);
  788. CopyTIdUInt16(HostToLittleEndian(Fttime), VBytes, VIndex);
  789. Inc(VIndex, 4);
  790. end;
  791. { TIdicmp_hun }
  792. constructor TIdicmp_hun.Create;
  793. begin
  794. inherited Create;
  795. SetBytesLen(4);
  796. end;
  797. function TIdicmp_hun.Getecho_id: UInt16;
  798. begin
  799. Result := Getgateway_s_w1;
  800. end;
  801. procedure TIdicmp_hun.Setecho_id(const Value: UInt16);
  802. begin
  803. Setgateway_s_w1(Value);
  804. end;
  805. function TIdicmp_hun.Getecho_seq: UInt16;
  806. begin
  807. Result := Getgateway_s_w2;
  808. end;
  809. procedure TIdicmp_hun.Setecho_seq(const Value: UInt16);
  810. begin
  811. Setgateway_s_w2(Value);
  812. end;
  813. function TIdicmp_hun.Getgateway_s_w1: UInt16;
  814. begin
  815. Result := BytesToUInt32(FBuffer, 0);
  816. end;
  817. procedure TIdicmp_hun.Setgateway_s_w1(const Value: UInt16);
  818. begin
  819. CopyTIdUInt32(Value, FBuffer, 0);
  820. end;
  821. function TIdicmp_hun.Getgateway_s_w2: UInt16;
  822. begin
  823. Result := BytesToUInt16(FBuffer, 2);
  824. end;
  825. procedure TIdicmp_hun.Setgateway_s_w2(const Value: UInt16);
  826. begin
  827. CopyTIdUInt16(HostToLittleEndian(Value), FBuffer, 2);
  828. end;
  829. function TIdicmp_hun.Getgateway_s_b1: UInt8;
  830. begin
  831. Result := FBuffer[0];
  832. end;
  833. procedure TIdicmp_hun.Setgateway_s_b1(const Value: UInt8);
  834. begin
  835. FBuffer[0] := Value;
  836. end;
  837. function TIdicmp_hun.Getgateway_s_b2: UInt8;
  838. begin
  839. Result := FBuffer[1];
  840. end;
  841. procedure TIdicmp_hun.Setgateway_s_b2(const Value: UInt8);
  842. begin
  843. FBuffer[1] := Value;
  844. end;
  845. function TIdicmp_hun.Getgateway_s_b3: UInt8;
  846. begin
  847. Result := FBuffer[2];
  848. end;
  849. procedure TIdicmp_hun.Setgateway_s_b3(const Value: UInt8);
  850. begin
  851. FBuffer[2] := Value;
  852. end;
  853. function TIdicmp_hun.Getgateway_s_b4: UInt8;
  854. begin
  855. Result := FBuffer[3];
  856. end;
  857. procedure TIdicmp_hun.Setgateway_s_b4(const Value: UInt8);
  858. begin
  859. FBuffer[3] := Value;
  860. end;
  861. function TIdicmp_hun.Getgateway_s_l: UInt32;
  862. begin
  863. Result := BytesToUInt32(FBuffer, 0);
  864. end;
  865. procedure TIdicmp_hun.Setgateway_s_l(const Value: UInt32);
  866. begin
  867. CopyTIdUInt32(Value, FBuffer, 0);
  868. end;
  869. function TIdicmp_hun.Getfrag_pad: UInt16;
  870. begin
  871. Result := Getgateway_s_w1;
  872. end;
  873. procedure TIdicmp_hun.Setfrag_pad(const Value: UInt16);
  874. begin
  875. Setgateway_s_w1(Value);
  876. end;
  877. function TIdicmp_hun.Getfrag_mtu: UInt16;
  878. begin
  879. Result := Getgateway_s_w2;
  880. end;
  881. procedure TIdicmp_hun.Setfrag_mtu(const Value: UInt16);
  882. begin
  883. Setgateway_s_w2(Value);
  884. end;
  885. { TIdicmp_dun }
  886. constructor TIdicmp_dun.Create;
  887. begin
  888. inherited Create;
  889. SetBytesLen(12);
  890. end;
  891. function TIdicmp_dun.Getts_otime: TIdNetTime;
  892. begin
  893. Result := BytesToUInt32(FBuffer, 0);
  894. end;
  895. procedure TIdicmp_dun.Setts_otime(const Value: TIdNetTime);
  896. begin
  897. CopyTIdUInt32(Value, FBuffer, 0);
  898. end;
  899. function TIdicmp_dun.Getts_rtime: TIdNetTime;
  900. begin
  901. Result := BytesToUInt32(FBuffer, 4);
  902. end;
  903. procedure TIdicmp_dun.Setts_rtime(const Value: TIdNetTime);
  904. begin
  905. CopyTIdUInt32(Value, FBuffer, 4);
  906. end;
  907. function TIdicmp_dun.Getts_ttime: TIdNetTime;
  908. begin
  909. Result := BytesToUInt32(FBuffer, 4);
  910. end;
  911. procedure TIdicmp_dun.Setts_ttime(const Value: TIdNetTime);
  912. begin
  913. CopyTIdUInt32(Value, FBuffer, 8);
  914. end;
  915. function TIdicmp_dun.Getmask: UInt32;
  916. begin
  917. Result := Getts_otime;
  918. end;
  919. procedure TIdicmp_dun.Setmask(const Value: UInt32);
  920. begin
  921. Setts_otime(Value);
  922. end;
  923. function TIdicmp_dun.Getdata: UInt8;
  924. begin
  925. Result := FBuffer[0];
  926. end;
  927. procedure TIdicmp_dun.setdata(const Value: UInt8);
  928. begin
  929. FBuffer[0] := Value;
  930. end;
  931. { TIdICMPHdr }
  932. constructor TIdICMPHdr.Create;
  933. begin
  934. inherited Create;
  935. Ficmp_hun := TIdicmp_hun.Create;
  936. end;
  937. destructor TIdICMPHdr.Destroy;
  938. begin
  939. FreeAndNil(Ficmp_hun);
  940. inherited Destroy;
  941. end;
  942. function TIdICMPHdr.GetBytesLen: UInt32;
  943. begin
  944. Result := inherited GetBytesLen + 4 + Ficmp_hun.BytesLen;
  945. end;
  946. procedure TIdICMPHdr.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  947. begin
  948. inherited ReadStruct(ABytes, VIndex);
  949. Ficmp_type := ABytes[VIndex];
  950. Inc(VIndex);
  951. Ficmp_code := ABytes[Vindex];
  952. Inc(VIndex);
  953. Ficmp_sum := BytesToUInt16(ABytes, VIndex);
  954. Inc(VIndex, 2);
  955. Ficmp_hun.ReadStruct(ABytes, VIndex);
  956. end;
  957. procedure TIdICMPHdr.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  958. begin
  959. inherited WriteStruct(VBytes, VIndex);
  960. VBytes[VIndex] := Ficmp_type;
  961. Inc(VIndex);
  962. VBytes[Vindex] := Ficmp_code;
  963. Inc(VIndex);
  964. CopyTIdUInt16(Ficmp_sum, VBytes, VIndex);
  965. Inc(VIndex, 2);
  966. Ficmp_hun.WriteStruct(VBytes, VIndex);
  967. end;
  968. { TIdIPOptions }
  969. constructor TIdIPOptions.Create;
  970. begin
  971. inherited Create;
  972. SetBytesLen(Id_MAX_IPOPTLEN);
  973. end;
  974. function TIdIPOptions.get_ipopt_list(Index: Integer): UInt8;
  975. begin
  976. Assert(Index < Id_MAX_IPOPTLEN, 'Out of range'); {do not localize}
  977. Result := FBuffer[Index];
  978. end;
  979. procedure TIdIPOptions.set_ipopt_list(Index: Integer; const Value: UInt8);
  980. begin
  981. Assert(Index < Id_MAX_IPOPTLEN, 'Out of range'); {do not localize}
  982. FBuffer[Index] := Value;
  983. end;
  984. { TIdIPHdr }
  985. constructor TIdIPHdr.Create;
  986. begin
  987. inherited Create;
  988. Fip_src := TIdInAddr.Create;
  989. Fip_dst := TIdInAddr.Create;
  990. end;
  991. destructor TIdIPHdr.Destroy;
  992. begin
  993. FreeAndNil(Fip_src);
  994. FreeAndNil(Fip_dst);
  995. inherited Destroy;
  996. end;
  997. function TIdIPHdr.GetBytesLen: UInt32;
  998. begin
  999. Result := inherited GetBytesLen + 12 + Fip_src.BytesLen + Fip_dst.BytesLen + 4;
  1000. end;
  1001. procedure TIdIPHdr.CopyFrom(const ASource: TIdIPHdr);
  1002. begin
  1003. Fip_verlen := ASource.ip_verlen;
  1004. Fip_tos := ASource.ip_tos;
  1005. Fip_len := ASource.ip_len;
  1006. Fip_id := ASource.ip_id;
  1007. Fip_off := ASource.ip_off;
  1008. Fip_ttl := ASource.ip_ttl;
  1009. Fip_p := ASource.ip_p;
  1010. Fip_sum := ASource.ip_sum;
  1011. Fip_src.CopyFrom(ASource.ip_src);
  1012. Fip_dst.CopyFrom(ASource.ip_dst);
  1013. Fip_options := ASource.ip_options;
  1014. end;
  1015. procedure TIdIPHdr.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  1016. var LIpHeaderLen : UInt32;
  1017. begin
  1018. inherited ReadStruct(ABytes, VIndex);
  1019. Fip_verlen := ABytes[VIndex]; // 1st nibble version, 2nd nibble header length div 4 (little-endian)
  1020. Inc(VIndex);
  1021. LIpHeaderLen := (Fip_verlen and $0F) * 4;
  1022. Fip_tos := ABytes[VIndex]; // type of service
  1023. Inc(VIndex);
  1024. Fip_len := BytesToUInt16(ABytes, VIndex); // total length
  1025. Inc(VIndex, 2);
  1026. Fip_id := BytesToUInt16(ABytes, VIndex); // identification
  1027. Inc(VIndex, 2);
  1028. Fip_off := BytesToUInt16(ABytes, VIndex); // 1st nibble flags, next 3 nibbles fragment offset (little-endian)
  1029. Inc(VIndex, 2);
  1030. Fip_ttl := ABytes[VIndex]; // time to live
  1031. Inc(VIndex);
  1032. Fip_p := ABytes[VIndex]; // protocol
  1033. Inc(VIndex);
  1034. Fip_sum := BytesToUInt16(ABytes, VIndex); // checksum
  1035. Inc(VIndex, 2);
  1036. Fip_src.ReadStruct(ABytes, VIndex); // source address
  1037. Fip_dst.ReadStruct(ABytes, VIndex); // dest address
  1038. //Fip_options may not be present in the packet
  1039. if VIndex >= LIpHeaderLen then
  1040. begin
  1041. Fip_options := BytesToUInt32(ABytes, VIndex); // options + padding
  1042. end;
  1043. //be sure that we indicate we read the entire packet in case
  1044. //the size varies.
  1045. VIndex := LIpHeaderLen;
  1046. end;
  1047. procedure TIdIPHdr.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  1048. begin
  1049. inherited WriteStruct(VBytes, VIndex);
  1050. VBytes[VIndex] := Fip_verlen; // 1st nibble version, 2nd nibble header length div 4 (little-endian)
  1051. Inc(VIndex);
  1052. VBytes[VIndex] := Fip_tos; // type of service
  1053. Inc(VIndex);
  1054. CopyTIdUInt16(Fip_len, VBytes, VIndex); // total length
  1055. Inc(VIndex, 2);
  1056. CopyTIdUInt16(Fip_id, VBytes, VIndex); // identification
  1057. Inc(VIndex, 2);
  1058. CopyTIdUInt16(Fip_off, VBytes, VIndex); // 1st nibble flags, next 3 nibbles fragment offset (little-endian)
  1059. Inc(VIndex, 2);
  1060. Fip_ttl := VBytes[VIndex]; // time to live
  1061. Inc(VIndex);
  1062. Fip_p := VBytes[VIndex]; // protocol
  1063. Inc(VIndex);
  1064. CopyTIdUInt16(Fip_sum, VBytes, VIndex); // checksum
  1065. Inc(VIndex, 2);
  1066. Fip_src.WriteStruct(VBytes, VIndex); // source address
  1067. Fip_dst.WriteStruct(VBytes, VIndex); // dest address
  1068. CopyTIdUInt32(Fip_options, VBytes, VIndex); // options + padding
  1069. Inc(VIndex, 4);
  1070. end;
  1071. { TIdTCPOptions }
  1072. constructor TIdTCPOptions.Create;
  1073. begin
  1074. inherited Create;
  1075. SetBytesLen(Id_MAX_IPOPTLEN);
  1076. end;
  1077. function TIdTCPOptions.gettcpopt_list(Index: Integer): UInt8;
  1078. begin
  1079. Assert(Index < Id_MAX_IPOPTLEN, 'Out of range');
  1080. Result := FBuffer[Index];
  1081. end;
  1082. procedure TIdTCPOptions.settcpopt_list(Index: Integer; const Value: UInt8);
  1083. begin
  1084. Assert(Index < Id_MAX_IPOPTLEN, 'Out of range');
  1085. FBuffer[Index] := Value;
  1086. end;
  1087. { TIdTCPHdr }
  1088. function TIdTCPHdr.GetBytesLen: UInt32;
  1089. begin
  1090. Result := inherited GetBytesLen + 20;
  1091. end;
  1092. procedure TIdTCPHdr.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  1093. begin
  1094. inherited ReadStruct(ABytes, VIndex);
  1095. Ftcp_sport := BytesToUInt16(ABytes, VIndex); // source port
  1096. Inc(VIndex, 2);
  1097. Ftcp_dport := BytesToUInt16(ABytes, VIndex); // destination port
  1098. Inc(VIndex, 2);
  1099. Ftcp_seq := BytesToUInt32(ABytes, VIndex); // sequence number
  1100. Inc(VIndex, 4);
  1101. Ftcp_ack := BytesToUInt32(ABytes, VIndex); // acknowledgement number
  1102. Inc(VIndex, 4);
  1103. Ftcp_x2off := ABytes[VIndex]; // data offset
  1104. Inc(VIndex);
  1105. Ftcp_flags := ABytes[VIndex]; // control flags
  1106. Inc(VIndex);
  1107. Ftcp_win := BytesToUInt16(ABytes, VIndex); // window
  1108. Inc(VIndex, 2);
  1109. Ftcp_sum := BytesToUInt16(ABytes, VIndex); // checksum
  1110. Inc(VIndex, 2);
  1111. Ftcp_urp := BytesToUInt16(ABytes, VIndex); // urgent pointer
  1112. Inc(VIndex, 2);
  1113. end;
  1114. procedure TIdTCPHdr.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  1115. begin
  1116. inherited WriteStruct(VBytes, VIndex);
  1117. CopyTIdUInt16(Ftcp_sport, VBytes, VIndex); // source port
  1118. Inc(VIndex, 2);
  1119. CopyTIdUInt16(Ftcp_dport, VBytes, VIndex); // destination port
  1120. Inc(VIndex, 2);
  1121. CopyTIdUInt32(Ftcp_seq, VBytes, VIndex); // sequence number
  1122. Inc(VIndex, 4);
  1123. CopyTIdUInt32(Ftcp_ack, VBytes, VIndex); // acknowledgement number
  1124. Inc(VIndex, 4);
  1125. VBytes[VIndex] := Ftcp_x2off; // data offset
  1126. Inc(VIndex);
  1127. VBytes[VIndex] := Ftcp_flags; // control flags
  1128. Inc(VIndex);
  1129. CopyTIdUInt16(Ftcp_win, VBytes, VIndex); // window
  1130. Inc(VIndex, 2);
  1131. CopyTIdUInt16(Ftcp_sum, VBytes, VIndex); // checksum
  1132. Inc(VIndex, 2);
  1133. CopyTIdUInt16(Ftcp_urp, VBytes, VIndex); // urgent pointer
  1134. Inc(VIndex, 2);
  1135. end;
  1136. { TIdUDPHdr }
  1137. function TIdUDPHdr.GetBytesLen: UInt32;
  1138. begin
  1139. Result := inherited GetBytesLen + 8;
  1140. end;
  1141. procedure TIdUDPHdr.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  1142. begin
  1143. inherited ReadStruct(ABytes, VIndex);
  1144. Fudp_sport := BytesToUInt16(ABytes, VIndex); // source port
  1145. Inc(VIndex, 2);
  1146. Fudp_dport := BytesToUInt16(ABytes, VIndex); // destination port
  1147. Inc(VIndex, 2);
  1148. Fudp_ulen := BytesToUInt16(ABytes, VIndex); // length
  1149. Inc(VIndex, 2);
  1150. Fudp_sum := BytesToUInt16(ABytes, VIndex); // checksum
  1151. Inc(VIndex, 2);
  1152. end;
  1153. procedure TIdUDPHdr.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  1154. begin
  1155. inherited WriteStruct(VBytes, VIndex);
  1156. CopyTIdUInt16(Fudp_sport, VBytes, VIndex); // source port
  1157. Inc(VIndex, 2);
  1158. CopyTIdUInt16(Fudp_dport, VBytes, VIndex); // destination port
  1159. Inc(VIndex, 2);
  1160. CopyTIdUInt16(Fudp_ulen, VBytes, VIndex); // length
  1161. Inc(VIndex, 2);
  1162. CopyTIdUInt16(Fudp_sum, VBytes, VIndex); // checksum
  1163. Inc(VIndex, 2);
  1164. end;
  1165. { TIdIGMPHdr }
  1166. constructor TIdIGMPHdr.Create;
  1167. begin
  1168. inherited Create;
  1169. Figmp_group := TIdInAddr.Create;
  1170. end;
  1171. destructor TIdIGMPHdr.Destroy;
  1172. begin
  1173. FreeAndNil(Figmp_group);
  1174. inherited Destroy;
  1175. end;
  1176. function TIdIGMPHdr.GetBytesLen: UInt32;
  1177. begin
  1178. Result := inherited GetBytesLen + 4 + Figmp_group.BytesLen;
  1179. end;
  1180. procedure TIdIGMPHdr.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  1181. begin
  1182. inherited ReadStruct(ABytes, VIndex);
  1183. Figmp_type := ABytes[VIndex];
  1184. Inc(VIndex);
  1185. Figmp_code := ABytes[VIndex];
  1186. Inc(VIndex);
  1187. Figmp_sum := BytesToUInt16(ABytes, VIndex);
  1188. Inc(VIndex, 2);
  1189. Figmp_group.ReadStruct(ABytes, VIndex);
  1190. end;
  1191. procedure TIdIGMPHdr.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  1192. begin
  1193. inherited WriteStruct(VBytes, VIndex);
  1194. VBytes[VIndex] := Figmp_type;
  1195. Inc(VIndex);
  1196. VBytes[VIndex] := Figmp_code;
  1197. Inc(VIndex);
  1198. CopyTIdUInt16(Figmp_sum, VBytes, VIndex);
  1199. Inc(VIndex, 2);
  1200. Figmp_group.WriteStruct(VBytes, VIndex);
  1201. end;
  1202. { TIdEtherAddr }
  1203. constructor TIdEtherAddr.Create;
  1204. begin
  1205. inherited Create;
  1206. SetBytesLen(Id_ETHER_ADDR_LEN);
  1207. end;
  1208. procedure TIdEtherAddr.setether_addr_octet(Index: Integer; const Value: UInt8);
  1209. begin
  1210. Assert(Index < Id_ETHER_ADDR_LEN, 'Out of range');
  1211. FBuffer[Index] := Value;
  1212. end;
  1213. function TIdEtherAddr.getether_addr_octet(Index: Integer): UInt8;
  1214. begin
  1215. Assert(Index < Id_ETHER_ADDR_LEN, 'Out of range');
  1216. Result := FBuffer[Index];
  1217. end;
  1218. procedure TIdEtherAddr.CopyFrom(const ASource: TIdEtherAddr);
  1219. begin
  1220. SetData(ASource.Data);
  1221. end;
  1222. procedure TIdEtherAddr.SetData(const Value: TIdBytes);
  1223. begin
  1224. CopyTIdBytes(Value, 0, FBuffer, 0, Id_ETHER_ADDR_LEN);
  1225. end;
  1226. { TIdEthernetHdr }
  1227. constructor TIdEthernetHdr.Create;
  1228. begin
  1229. inherited Create;
  1230. Fether_dhost := TIdEtherAddr.Create;
  1231. Fether_shost := TIdEtherAddr.Create;
  1232. end;
  1233. destructor TIdEthernetHdr.Destroy;
  1234. begin
  1235. FreeAndNil(Fether_dhost);
  1236. FreeAndNil(Fether_shost);
  1237. inherited Destroy;
  1238. end;
  1239. function TIdEthernetHdr.GetBytesLen: UInt32;
  1240. begin
  1241. Result := inherited GetBytesLen + Fether_dhost.BytesLen + Fether_shost.BytesLen + 2;
  1242. end;
  1243. procedure TIdEthernetHdr.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  1244. begin
  1245. inherited ReadStruct(ABytes, VIndex);
  1246. Fether_dhost.ReadStruct(ABytes, VIndex); // destination ethernet address
  1247. Fether_shost.ReadStruct(ABytes, VIndex); // source ethernet address
  1248. Fether_type := BytesToUInt16(ABytes, VIndex); // packet type ID
  1249. Inc(VIndex, 2);
  1250. end;
  1251. procedure TIdEthernetHdr.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  1252. begin
  1253. inherited WriteStruct(VBytes, VIndex);
  1254. Fether_dhost.WriteStruct(VBytes, VIndex); // destination ethernet address
  1255. Fether_shost.WriteStruct(VBytes, VIndex); // source ethernet address
  1256. CopyTIdUInt16(Fether_type, VBytes, VIndex); // packet type ID
  1257. Inc(VIndex, 2);
  1258. end;
  1259. procedure TIdEthernetHdr.CopyFrom(const ASource: TIdEthernetHdr);
  1260. begin
  1261. Fether_dhost.CopyFrom(ASource.Fether_dhost);
  1262. Fether_shost.CopyFrom(ASource.Fether_shost);
  1263. Fether_type := ASource.Fether_type;
  1264. end;
  1265. { TIdARPHdr }
  1266. constructor TIdARPHdr.Create;
  1267. begin
  1268. inherited Create;
  1269. Farp_sha := TIdEtherAddr.Create; // sender hardware address
  1270. Farp_spa := TIdInAddr.Create; // sender protocol address
  1271. Farp_tha := TIdEtherAddr.Create; // target hardware address
  1272. Farp_tpa := TIdInAddr.Create; // target protocol address
  1273. end;
  1274. destructor TIdARPHdr.Destroy;
  1275. begin
  1276. FreeAndNil(Farp_sha);
  1277. FreeAndNil(Farp_spa);
  1278. FreeAndNil(Farp_tha);
  1279. FreeAndNil(Farp_tpa);
  1280. inherited Destroy;
  1281. end;
  1282. function TIdARPHdr.GetBytesLen: UInt32;
  1283. begin
  1284. Result := inherited GetBytesLen + 8 + Farp_sha.BytesLen + Farp_spa.BytesLen + Farp_tha.BytesLen + Farp_tpa.BytesLen;
  1285. end;
  1286. procedure TIdARPHdr.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  1287. begin
  1288. inherited ReadStruct(ABytes, VIndex);
  1289. Farp_hrd := BytesToUInt16(ABytes, VIndex); // format of hardware address
  1290. Inc(VIndex, 2);
  1291. Farp_pro := BytesToUInt16(ABytes, VIndex); // format of protocol address
  1292. Inc(VIndex, 2);
  1293. Farp_hln := ABytes[VIndex]; // length of hardware address
  1294. Inc(VIndex);
  1295. Farp_pln := ABytes[VIndex]; // length of protocol addres
  1296. Inc(VIndex);
  1297. Farp_op := BytesToUInt16(ABytes, VIndex); // operation type
  1298. Inc(VIndex, 2);
  1299. // following hardcoded for ethernet/IP
  1300. Farp_sha.ReadStruct(ABytes, VIndex); // sender hardware address
  1301. Farp_spa.ReadStruct(ABytes, VIndex); // sender protocol address
  1302. Farp_tha.ReadStruct(ABytes, VIndex); // target hardware address
  1303. Farp_tpa.ReadStruct(ABytes, VIndex); // target protocol address
  1304. end;
  1305. procedure TIdARPHdr.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  1306. begin
  1307. inherited WriteStruct(VBytes, VIndex);
  1308. CopyTIdUInt16(Farp_hrd, VBytes, VIndex); // format of hardware address
  1309. Inc(VIndex, 2);
  1310. CopyTIdUInt16(Farp_pro, VBytes, VIndex); // format of protocol address
  1311. Inc(VIndex, 2);
  1312. VBytes[VIndex] := Farp_hln; // length of hardware address
  1313. Inc(VIndex);
  1314. VBytes[VIndex] := Farp_pln; // length of protocol addres
  1315. Inc(VIndex);
  1316. CopyTIdUInt16(Farp_op, VBytes, VIndex); // operation type
  1317. Inc(VIndex, 2);
  1318. // following hardcoded for ethernet/IP
  1319. Farp_sha.WriteStruct(VBytes, VIndex); // sender hardware address
  1320. Farp_spa.WriteStruct(VBytes, VIndex); // sender protocol address
  1321. Farp_tha.WriteStruct(VBytes, VIndex); // target hardware address
  1322. Farp_tpa.WriteStruct(VBytes, VIndex); // target protocol address
  1323. end;
  1324. { TIdDNSHdr }
  1325. function TIdDNSHdr.GetBytesLen: UInt32;
  1326. begin
  1327. Result := inherited GetBytesLen + 12;
  1328. end;
  1329. procedure TIdDNSHdr.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  1330. begin
  1331. inherited ReadStruct(ABytes, VIndex);
  1332. Fdns_id := BytesToUInt16(ABytes, VIndex); // DNS packet ID
  1333. Inc(VIndex, 2);
  1334. Fdns_flags := BytesToUInt16(ABytes, VIndex); // DNS flags
  1335. Inc(VIndex, 2);
  1336. Fdns_num_q := BytesToUInt16(ABytes, VIndex); // number of questions
  1337. Inc(VIndex, 2);
  1338. Fdns_num_answ_rr := BytesToUInt16(ABytes, VIndex);// number of answer resource records
  1339. Inc(VIndex, 2);
  1340. Fdns_num_auth_rr := BytesToUInt16(ABytes, VIndex); // number of authority resource records
  1341. Inc(VIndex, 2);
  1342. Fdns_num_addi_rr := BytesToUInt16(ABytes, VIndex); // number of additional resource records
  1343. Inc(VIndex, 2);
  1344. end;
  1345. procedure TIdDNSHdr.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  1346. begin
  1347. inherited WriteStruct(VBytes, VIndex);
  1348. CopyTIdUInt16(Fdns_id, VBytes, VIndex); // DNS packet ID
  1349. Inc(VIndex, 2);
  1350. CopyTIdUInt16(Fdns_flags, VBytes, VIndex); // DNS flags
  1351. Inc(VIndex, 2);
  1352. CopyTIdUInt16(Fdns_num_q, VBytes, VIndex); // number of questions
  1353. Inc(VIndex, 2);
  1354. CopyTIdUInt16(Fdns_num_answ_rr, VBytes, VIndex); // number of answer resource records
  1355. Inc(VIndex, 2);
  1356. CopyTIdUInt16(Fdns_num_auth_rr, VBytes, VIndex); // number of authority resource records
  1357. Inc(VIndex, 2);
  1358. CopyTIdUInt16(Fdns_num_addi_rr, VBytes, VIndex); // number of additional resource records
  1359. Inc(VIndex, 2);
  1360. end;
  1361. { TIdRIPHdr }
  1362. function TIdRIPHdr.GetBytesLen: UInt32;
  1363. begin
  1364. Result := inherited GetBytesLen + 24;
  1365. end;
  1366. procedure TIdRIPHdr.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  1367. begin
  1368. inherited ReadStruct(ABytes, VIndex);
  1369. Frip_cmd := ABytes[VIndex]; // RIP command
  1370. Inc(VIndex);
  1371. Frip_ver := ABytes[VIndex]; // RIP version
  1372. Inc(VIndex);
  1373. Frip_rd := BytesToUInt16(ABytes, VIndex); // zero (v1) or routing domain (v2)
  1374. Inc(VIndex, 2);
  1375. Frip_af := BytesToUInt16(ABytes, VIndex); // address family
  1376. Inc(VIndex, 2);
  1377. Frip_rt := BytesToUInt16(ABytes, VIndex); // zero (v1) or route tag (v2)
  1378. Inc(VIndex, 2);
  1379. Frip_addr := BytesToUInt32(ABytes, VIndex); // IP address
  1380. Inc(VIndex, 4);
  1381. Frip_mask := BytesToUInt32(ABytes, VIndex); // zero (v1) or subnet mask (v2)
  1382. Inc(VIndex, 4);
  1383. Frip_next_hop := BytesToUInt32(ABytes, VIndex); // zero (v1) or next hop IP address (v2)
  1384. Inc(VIndex, 4);
  1385. Frip_metric := BytesToUInt32(ABytes, VIndex); // metric
  1386. Inc(VIndex, 4);
  1387. end;
  1388. procedure TIdRIPHdr.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  1389. begin
  1390. inherited WriteStruct(VBytes, VIndex);
  1391. VBytes[VIndex] := Frip_cmd; // RIP command
  1392. Inc(VIndex);
  1393. VBytes[VIndex] := Frip_ver; // RIP version
  1394. Inc(VIndex);
  1395. CopyTIdUInt16(Frip_rd, VBytes, VIndex); // zero (v1) or routing domain (v2)
  1396. Inc(VIndex, 2);
  1397. CopyTIdUInt16(Frip_af, VBytes, VIndex); // address family
  1398. Inc(VIndex, 2);
  1399. CopyTIdUInt16(Frip_rt, VBytes, VIndex); // zero (v1) or route tag (v2)
  1400. Inc(VIndex, 2);
  1401. CopyTIdUInt32(Frip_addr, VBytes, VIndex); // IP address
  1402. Inc(VIndex, 4);
  1403. CopyTIdUInt32(Frip_mask, VBytes, VIndex); // zero (v1) or subnet mask (v2)
  1404. Inc(VIndex, 4);
  1405. CopyTIdUInt32(Frip_next_hop, VBytes, VIndex); // zero (v1) or next hop IP address (v2)
  1406. Inc(VIndex, 4);
  1407. CopyTIdUInt32(Frip_metric, VBytes, VIndex); // metric
  1408. Inc(VIndex, 4);
  1409. end;
  1410. { TIdInAddr }
  1411. procedure TIdInAddr.CopyFrom(const ASource: TIdInAddr);
  1412. begin
  1413. s_l := ASource.s_l;
  1414. end;
  1415. { TIdicmp6_un }
  1416. constructor TIdicmp6_un.Create;
  1417. begin
  1418. inherited Create;
  1419. SetBytesLen(4);
  1420. end;
  1421. function TIdicmp6_un.Geticmp6_un_data16(Index: Integer): UInt16;
  1422. begin
  1423. Result := 0;
  1424. case Index of
  1425. 0 : Result := BytesToUInt16(FBuffer, 0);
  1426. 1 : Result := BytesToUInt16(FBuffer, 2);
  1427. end;
  1428. end;
  1429. procedure TIdicmp6_un.Seticmp6_un_data16(Index: Integer; const Value: UInt16);
  1430. begin
  1431. case Index of
  1432. 0 : CopyTIdUInt16(Value, FBuffer, 0);
  1433. 1 : CopyTIdUInt16(Value, FBuffer, 2);
  1434. end;
  1435. end;
  1436. function TIdicmp6_un.Geticmp6_un_data32: UInt32;
  1437. begin
  1438. Result := BytesToUInt32(FBuffer, 0);
  1439. end;
  1440. procedure TIdicmp6_un.Seticmp6_un_data32(const Value: UInt32);
  1441. begin
  1442. CopyTIdUInt32(Value, FBuffer, 0);
  1443. end;
  1444. function TIdicmp6_un.Geticmp6_un_data8(Index: Integer): UInt8;
  1445. begin
  1446. Assert((Index>-1) and (Index<4), 'Out of range');
  1447. Result := FBuffer[Index];
  1448. end;
  1449. procedure TIdicmp6_un.Seticmp6_un_data8(Index: Integer; const Value: UInt8);
  1450. begin
  1451. Assert((Index>-1) and (Index<4), 'Out of range');
  1452. FBuffer[Index] := Value;
  1453. end;
  1454. function TIdicmp6_un.Geticmp6_data8: UInt8;
  1455. begin
  1456. Result := FBuffer[0];
  1457. end;
  1458. procedure TIdicmp6_un.Seticmp6_data8(const Value: UInt8);
  1459. begin
  1460. FBuffer[0] := Value;
  1461. end;
  1462. function TIdicmp6_un.Geticmp6_data16: UInt16;
  1463. begin
  1464. Result := BytesToUInt16(FBuffer, 0);
  1465. end;
  1466. procedure TIdicmp6_un.Seticmp6_data16(const Value: UInt16);
  1467. begin
  1468. CopyTIdUInt16(Value, FBuffer, 0);
  1469. end;
  1470. function TIdicmp6_un.Geticmp6_seq: UInt16;
  1471. begin
  1472. Result := Geticmp6_un_data16(1);
  1473. end;
  1474. procedure TIdicmp6_un.Seticmp6_seq(const Value: UInt16);
  1475. begin
  1476. Seticmp6_un_data16(1, Value);
  1477. end;
  1478. { TIdicmp6_hdr }
  1479. constructor TIdicmp6_hdr.Create;
  1480. begin
  1481. inherited Create;
  1482. Fdata := TIdicmp6_un.Create;
  1483. end;
  1484. destructor TIdicmp6_hdr.Destroy;
  1485. begin
  1486. FreeAndNil(Fdata);
  1487. inherited Destroy;
  1488. end;
  1489. function TIdicmp6_hdr.GetBytesLen: UInt32;
  1490. begin
  1491. Result := inherited GetBytesLen + 4 + Fdata.BytesLen;
  1492. end;
  1493. procedure TIdicmp6_hdr.ReadStruct(const ABytes: TIdBytes; var VIndex: UInt32);
  1494. begin
  1495. inherited ReadStruct(ABytes, VIndex);
  1496. Ficmp6_type := ABytes[VIndex];
  1497. Inc(VIndex);
  1498. FIcmp6_code := ABytes[VIndex];
  1499. Inc(VIndex);
  1500. Ficmp6_cksum := BytesToUInt16(ABytes, VIndex);
  1501. Inc(VIndex, 2);
  1502. Fdata.ReadStruct(ABytes, VIndex);
  1503. end;
  1504. procedure TIdicmp6_hdr.WriteStruct(var VBytes: TIdBytes; var VIndex: UInt32);
  1505. begin
  1506. inherited WriteStruct(VBytes, VIndex);
  1507. VBytes[VIndex] := Ficmp6_type;
  1508. Inc(VIndex);
  1509. VBytes[VIndex] := FIcmp6_code;
  1510. Inc(VIndex);
  1511. CopyTIdUInt16(Ficmp6_cksum, VBytes, VIndex);
  1512. Inc(VIndex, 2);
  1513. Fdata.WriteStruct(VBytes, VIndex);
  1514. end;
  1515. end.