EtherRaw
EtherRaw.h
1 // EtherRaw.h
2 //
3 /// \mainpage EtherRaw library for Arduino
4 ///
5 /// This is the EtherRaw library.
6 /// It provides an object-oriented interface for inspecting Ethernet, IP, ICMP, TCP, UDP and other packets
7 /// as delivered by Arduino Ethernet Shield, especially in MACRAW or IPRAW modes.
8 ///
9 /// The latest version of this documentation can be downloaded from
10 /// http://www.airspayce.com/mikem/arduino/EtherRaw
11 ///
12 /// The version of the package that this documentation refers to can be downloaded
13 /// from http://www.airspayce.com/mikem/arduino/EtherRaw/EtherRaw-1.0.zip
14 /// You can find the latest version at http://www.airspayce.com/mikem/arduino/EtherRaw
15 ///
16 /// \par Installation
17 /// Install in the usual way: unzip the distribution zip file to the libraries
18 /// sub-folder of your sketchbook.
19 ///
20 /// This software is Copyright (C) 2012 Mike McCauley. Use is subject to license
21 /// conditions. The main licensing options available are GPL V2 or Commercial:
22 ///
23 /// \par Open Source Licensing GPL V2
24 /// This is the appropriate option if you want to share the source code of your
25 /// application with everyone you distribute it to, and you also want to give them
26 /// the right to share who uses it. If you wish to use this software under Open
27 /// Source Licensing, you must contribute all your source code to the open source
28 /// community in accordance with the GPL Version 2 when your application is
29 /// distributed. See http://www.gnu.org/copyleft/gpl.html
30 ///
31 /// \par Commercial Licensing
32 /// This is the appropriate option if you are creating proprietary applications
33 /// and you are not prepared to distribute and share the source code of your
34 /// application. Contact info@airspayce.com for details.
35 ///
36 /// \par Revision History
37 /// \version 1.0 Initial release
38 /// \version 1.1 Updated author and distribution location details to airspayce.com
39 ///
40 /// \author Mike McCauley (mikem@airspayce.com)
41 // Copyright (C) 2012 Mike McCauley
42 // $Id: EtherRaw.h,v 1.1 2012/05/30 01:52:08 mikem Exp mikem $
43 
44 #ifndef EtherRaw_h
45 #define EtherRaw_h
46 
47 #include <stdlib.h>
48 #if ARDUINO >= 100
49 #include <Arduino.h>
50 #else
51 #include <WProgram.h>
52 #include <wiring.h>
53 #endif
54 #include <IPAddress.h>
55 
56 /////////////////////////////////////////////////////////////////////
57 /// \class IPv4Address EtherRaw.h <EtherRaw.h>
58 /// \brief Support for IPv4 addresses
59 ///
60 /// This class allows easy access to the contents of an IPv4 address
61 /// in on-the-wire format.
62 ///
63 /// Sigh, the arduino IPAddress class inherits from Print, and is length 6, not 4
64 /// so we cant use it in on-the-wire structures. So we define our own similar structure
65 #define IPV4_ADDRESS_LENGTH 4
67 {
68 public:
69  /// Constructor.
70  /// Clears the address to all zeros
71  IPv4Address();
72 
73  /// Constructor
74  /// Initialise the address from separate octets.
75  /// \param[in] octet1 Value for octet 1
76  /// \param[in] octet2 Value for octet 2
77  /// \param[in] octet3 Value for octet 3
78  /// \param[in] octet4 Value for octet 4
79  IPv4Address(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4);
80 
81  /// Constructor.
82  /// Initialise the address from some other array or structure.
83  /// \param [in] address Pointer to 4 octets of IPv4 address
84  IPv4Address(const uint8_t *address);
85 
86  /// Equality operator.
87  /// Tests whether 2 addresses are identical.
88  /// \param [in] addr The other address
89  /// \return true if address is identical to this address
90  bool operator==(const IPv4Address& addr);
91 
92  /// Equality operator.
93  /// Tests whether 2 addresses are identical.
94  /// \param [in] addr The other address as some raw array or structure
95  /// \return true if address is identical to this address
96  bool operator==(const uint8_t* addr);
97 
98  /// Assignment operator.
99  /// Copies the raw address pointed to by addr to this address.
100  /// \param [in] addr The raw address to be used to initialise this address
101  /// \return This address
102  IPv4Address& operator=(const uint8_t *addr);
103 
104  /// Access the raw address
105  /// as an array of octets.
106  /// \return The raw address
107  const uint8_t* address() const;
108 
109 private:
110  /// The raw address
111  uint8_t _address[IPV4_ADDRESS_LENGTH];
112 
113 };
114 
115 
116 /////////////////////////////////////////////////////////////////////
117 /// \class IPv6Address EtherRaw.h <EtherRaw.h>
118 /// \brief Support for IPv6 addresses
119 ///
120 /// This class allows easy access to the contents of an IPv6 address
121 /// in on-the-wire format.
122 ///
123 #define IPV6_ADDRESS_LENGTH 16
125 {
126 public:
127  /// Constructor.
128  /// Clears the address to all zeros
129  IPv6Address();
130 
131  /// Constructor.
132  /// Initialise the address from some other array or structure.
133  /// \param [in] address Pointer to 4 octets of IPv4 address
134  IPv6Address(const uint8_t *address);
135 
136  /// Equality operator.
137  /// Tests whether 2 addresses are identical.
138  /// \param [in] addr The other address
139  /// \return true if address is identical to this address
140  bool operator==(const IPv6Address& addr);
141 
142  /// Equality operator.
143  /// Tests whether 2 addresses are identical.
144  /// \param [in] addr The other address as some raw array or structure
145  /// \return true if address is identical to this address
146  bool operator==(const uint8_t* addr);
147 
148  /// Assignment operator.
149  /// Copies the raw address pointed to by addr to this address.
150  /// \param [in] addr The raw address to be used to initialise this address
151  /// \return This address
152  IPv6Address& operator=(const uint8_t *addr);
153 
154  /// Access the raw address
155  /// as an array of octets.
156  /// \return The raw address
157  const uint8_t* address() const;
158 
159 private:
160  /// The raw address
161  uint8_t _address[IPV6_ADDRESS_LENGTH];
162 
163 
164 };
165 
166 /////////////////////////////////////////////////////////////////////
167 /// \class MACAddress EtherRaw.h <EtherRaw.h>
168 /// \brief Support for IPv4 addresses
169 ///
170 /// This class allows easy access to the contents of a MAC address
171 /// in on-the-wire format.
172 ///
173 #define MAC_ADDRESS_LENGTH 6
175 {
176 public:
177  /// Constructor.
178  /// Clears the address to all zeros
179  MACAddress();
180 
181  /// Constructor
182  /// Initialise the address from separate octets.
183  /// \param[in] octet1 Value for octet 1
184  /// \param[in] octet2 Value for octet 2
185  /// \param[in] octet3 Value for octet 3
186  /// \param[in] octet4 Value for octet 4
187  /// \param[in] octet5 Value for octet 5
188  /// \param[in] octet6 Value for octet 6
189  MACAddress(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4, uint8_t octet5, uint8_t octet6);
190 
191  /// Constructor.
192  /// Initialise the address from some other array or structure.
193  /// \param [in] address Pointer to 6 octets of MAC address
194  MACAddress(const uint8_t *address);
195 
196  /// Equality operator.
197  /// Tests whether 2 addresses are identical.
198  /// \param [in] addr The other address
199  /// \return true if address is identical to this address
200  bool operator==(const MACAddress& addr);
201 
202  /// Equality operator.
203  /// Tests whether 2 addresses are identical.
204  /// \param [in] addr The other address as some raw array or structure
205  /// \return true if address is identical to this address
206  bool operator==(const uint8_t* addr);
207 
208  /// Overloaded index operator.
209  /// Allow getting and setting individual octets of the address
210  /// \param [in] index The index of the octet to access
211  /// \return The value of the indexed octet
212  uint8_t operator[](int index) const;
213 
214  /// Overloaded index operator.
215  /// Allow getting and setting individual octets of the address
216  /// \param [in] index The index of the octet to access
217  /// \return Reference to the value of the indexed octet
218  uint8_t& operator[](int index);
219 
220  /// Assignment operator.
221  /// Copies the raw address pointed to by addr to this address.
222  /// \param [in] addr The raw address to be used to initialise this address
223  /// \return This address
224  MACAddress& operator=(const uint8_t *addr);
225 
226  /// Access the raw address
227  /// as an array of octets.
228  /// \return The raw address
229  uint8_t* address();
230 
231  /// Print.
232  /// Prints an ASCII formatted version of the address to a Printable such as Serial.
233  /// \return The number of characters printed
234  size_t printTo(Print& p) const;
235 
236 private:
237  /// The raw address
238  uint8_t _address[MAC_ADDRESS_LENGTH];
239 
240 };
241 
242 /////////////////////////////////////////////////////////////////////
243 /// \class EthernetHeader EtherRaw.h <EtherRaw.h>
244 /// \brief Support for Ethernet Headers
245 ///
246 /// This class allows easy access to the contents of an Ethernet Header
247 /// in on-the-wire format.
248 ///
250 {
251 public:
252  /// Ethernet Protocol IDs for ethertype
253  /// Based on /usr/include/linux/if_ether.h
254  typedef enum
255  {
256  ETH_P_LOOP = 0x0060, /// Ethernet Loopback packet
257  ETH_P_PUP = 0x0200, /// Xerox PUP packet
258  ETH_P_PUPAT = 0x0201, /// Xerox PUP Addr Trans packet
259  ETH_P_IP = 0x0800, /// Internet Protocol packet
260  ETH_P_X25 = 0x0805, /// CCITT X.25
261  ETH_P_ARP = 0x0806, /// Address Resolution packet
262  ETH_P_BPQ = 0x08FF, /// G8BPQ AX.25 Ethernet Packet
263  ETH_P_IEEEPUP = 0x0a00, /// Xerox IEEE802.3 PUP packet
264  ETH_P_IEEEPUPAT = 0x0a01, /// Xerox IEEE802.3 PUP Addr Trans packet
265  ETH_P_DEC = 0x6000, /// DEC Assigned proto
266  ETH_P_DNA_DL = 0x6001, /// DEC DNA Dump/Load
267  ETH_P_DNA_RC = 0x6002, /// DEC DNA Remote Console
268  ETH_P_DNA_RT = 0x6003, /// DEC DNA Routing
269  ETH_P_LAT = 0x6004, /// DEC LAT
270  ETH_P_DIAG = 0x6005, /// DEC Diagnostics
271  ETH_P_CUST = 0x6006, /// DEC Customer use
272  ETH_P_SCA = 0x6007, /// DEC Systems Comms Arch
273  ETH_P_TEB = 0x6558, /// Trans Ether Bridging
274  ETH_P_RARP = 0x8035, /// Reverse Addr Res packet
275  ETH_P_ATALK = 0x809B, /// Appletalk DDP
276  ETH_P_AARP = 0x80F3, /// Appletalk AARP
277  ETH_P_8021Q = 0x8100, /// 802.1Q VLAN Extended Header
278  ETH_P_IPX = 0x8137, /// IPX over DIX
279  ETH_P_IPV6 = 0x86DD, /// IPv6 over bluebook
280  ETH_P_PAUSE = 0x8808, /// IEEE Pause frames. See 802.3 31B
281  ETH_P_SLOW = 0x8809, /// Slow Protocol. See 802.3ad 43B
282  ETH_P_WCCP = 0x883E, /// Web-cache coordination protocol
283  ETH_P_PPP_DISC = 0x8863, /// PPPoE discovery messages
284  ETH_P_PPP_SES = 0x8864, /// PPPoE session messages
285  ETH_P_MPLS_UC = 0x8847, /// MPLS Unicast traffic
286  ETH_P_MPLS_MC = 0x8848, /// MPLS Multicast traffic
287  ETH_P_ATMMPOA = 0x884c, /// MultiProtocol Over ATM
288  ETH_P_LINK_CTL = 0x886c, /// HPNA, wlan link local tunnel
289  ETH_P_ATMFATE = 0x8884, /// Frame-based ATM Transport over Ethernet
290  ETH_P_PAE = 0x888E, /// Port Access Entity (IEEE 802.1X)
291  ETH_P_AOE = 0x88A2, /// ATA over Ethernet
292  ETH_P_8021AD = 0x88A8, /// 802.1ad Service VLAN
293  ETH_P_TIPC = 0x88CA, /// TIPC
294  ETH_P_8021AH = 0x88E7, /// 802.1ah Backbone Service Tag
295  ETH_P_1588 = 0x88F7, /// IEEE 1588 Timesync
296  ETH_P_FCOE = 0x8906, /// Fibre Channel over Ethernet
297  ETH_P_FIP = 0x8914, /// FCoE Initialization Protocol
298  ETH_P_QINQ1 = 0x9100, /// deprecated QinQ VLAN
299  ETH_P_QINQ2 = 0x9200, /// deprecated QinQ VLAN
300  ETH_P_QINQ3 = 0x9300, /// deprecated QinQ VLAN
301  ETH_P_EDSA = 0xDADA /// Ethertype DSA
302  } EtherType;
303 
304  /// Access the source MAC address
305  /// \return Reference to the source MAC address.
306  MACAddress& source();
307 
308  /// Access the destination MAC address
309  /// \return Reference to the destination MAC address.
310  MACAddress& dest();
311 
312  /// Access the EtherType.
313  /// \return one of EtherType values in native host endianness
314  uint16_t ethertype() const;
315 
316  /// Access the payload of the packet.
317  /// \return Pointer to the payload data following the MAC Header
318  uint8_t* payload();
319 
320  /// Print.
321  /// Prints an ASCII formatted version of the header to a Printable such as Serial.
322  /// \return The number of characters printed
323  size_t printTo(Print& p) const;
324 
325 private:
326  /// Destination MAC address
327  MACAddress _dest;
328 
329  /// Source MAC address
330  MACAddress _source;
331 
332  // Optional 802.1Q header here? 4 octets
333 
334  /// EtherType in network byte order
335  uint16_t _ethertype;
336 
337  /// Following data
338  uint8_t _data[0];
339 };
340 
341 /////////////////////////////////////////////////////////////////////
342 /// \class PacketICMP EtherRaw.h <EtherRaw.h>
343 /// \brief Support for ICMP Packet Headers
344 ///
345 /// This class allows easy access to the contents of an ICMP Packet
346 /// in on-the-wire format.
347 ///
348 /// This implementation is incomplete.
349 ///
351 {
352 public:
353 
354 private:
355  uint8_t _type;
356  uint8_t _code;
357  uint16_t _checksum;
358  uint16_t _id;
359  uint16_t _seqnum;
360  uint8_t _data[0];
361 };
362 
363 /////////////////////////////////////////////////////////////////////
364 /// \class PacketIPv4 EtherRaw.h <EtherRaw.h>
365 /// \brief Support for IP PAcket Headers
366 ///
367 /// This class allows easy access to the contents of an IP Packet Header
368 /// in on-the-wire format.
369 ///
371 {
372 public:
373 
374 // Defines from /usr/include/linux/ip.h
375 #define IPTOS_TOS_MASK 0x1E
376 #define IPTOS_TOS(tos) ((tos)&IPTOS_TOS_MASK)
377 #define IPTOS_LOWDELAY 0x10
378 #define IPTOS_THROUGHPUT 0x08
379 #define IPTOS_RELIABILITY 0x04
380 #define IPTOS_MINCOST 0x02
381 
382 #define IPTOS_PREC_MASK 0xE0
383 #define IPTOS_PREC(tos) ((tos)&IPTOS_PREC_MASK)
384 #define IPTOS_PREC_NETCONTROL 0xe0
385 #define IPTOS_PREC_INTERNETCONTROL 0xc0
386 #define IPTOS_PREC_CRITIC_ECP 0xa0
387 #define IPTOS_PREC_FLASHOVERRIDE 0x80
388 #define IPTOS_PREC_FLASH 0x60
389 #define IPTOS_PREC_IMMEDIATE 0x40
390 #define IPTOS_PREC_PRIORITY 0x20
391 #define IPTOS_PREC_ROUTINE 0x00
392 
393 /* IP options */
394 #define IPOPT_COPY 0x80
395 #define IPOPT_CLASS_MASK 0x60
396 #define IPOPT_NUMBER_MASK 0x1f
397 
398 #define IPOPT_COPIED(o) ((o)&IPOPT_COPY)
399 #define IPOPT_CLASS(o) ((o)&IPOPT_CLASS_MASK)
400 #define IPOPT_NUMBER(o) ((o)&IPOPT_NUMBER_MASK)
401 
402 #define IPOPT_CONTROL 0x00
403 #define IPOPT_RESERVED1 0x20
404 #define IPOPT_MEASUREMENT 0x40
405 #define IPOPT_RESERVED2 0x60
406 
407 #define IPOPT_END (0 |IPOPT_CONTROL)
408 #define IPOPT_NOOP (1 |IPOPT_CONTROL)
409 #define IPOPT_SEC (2 |IPOPT_CONTROL|IPOPT_COPY)
410 #define IPOPT_LSRR (3 |IPOPT_CONTROL|IPOPT_COPY)
411 #define IPOPT_TIMESTAMP (4 |IPOPT_MEASUREMENT)
412 #define IPOPT_CIPSO (6 |IPOPT_CONTROL|IPOPT_COPY)
413 #define IPOPT_RR (7 |IPOPT_CONTROL)
414 #define IPOPT_SID (8 |IPOPT_CONTROL|IPOPT_COPY)
415 #define IPOPT_SSRR (9 |IPOPT_CONTROL|IPOPT_COPY)
416 #define IPOPT_RA (20|IPOPT_CONTROL|IPOPT_COPY)
417 
418 #define IPVERSION 4
419 #define MAXTTL 255
420 #define IPDEFTTL 64
421 
422 #define IPOPT_OPTVAL 0
423 #define IPOPT_OLEN 1
424 #define IPOPT_OFFSET 2
425 #define IPOPT_MINOFF 4
426 #define MAX_IPOPTLEN 40
427 #define IPOPT_NOP IPOPT_NOOP
428 #define IPOPT_EOL IPOPT_END
429 #define IPOPT_TS IPOPT_TIMESTAMP
430 
431 #define IPOPT_TS_TSONLY 0 /// timestamps only
432 #define IPOPT_TS_TSANDADDR 1 /// timestamps and addresses
433 #define IPOPT_TS_PRESPEC 3 /// specified modules only
434 
435  /// Definitions of protocol number for protocol()
436  typedef enum
437  {
438  IPPROTO_IP = 0, /// Dummy protocol for TCP.
439  IPPROTO_HOPOPTS = 0, /// IPv6 Hop-by-Hop options.
440  IPPROTO_ICMP = 1, /// Internet Control Message Protocol.
441  IPPROTO_IGMP = 2, /// Internet Group Management Protocol.
442  IPPROTO_IPIP = 4, /// IPIP tunnels (older KA9Q tunnels use 94).
443  IPPROTO_TCP = 6, /// Transmission Control Protocol.
444  IPPROTO_EGP = 8, /// Exterior Gateway Protocol.
445  IPPROTO_PUP = 12, /// PUP protocol.
446  IPPROTO_UDP = 17, /// User Datagram Protocol.
447  IPPROTO_IDP = 22, /// XNS IDP protocol.
448  IPPROTO_TP = 29, /// SO Transport Protocol Class 4.
449  IPPROTO_DCCP = 33, /// Datagram Congestion Control Protocol.
450  IPPROTO_IPV6 = 41, /// IPv6 header.
451  IPPROTO_ROUTING = 43, /// IPv6 routing header.
452  IPPROTO_FRAGMENT = 44, /// IPv6 fragmentation header.
453  IPPROTO_RSVP = 46, /// Reservation Protocol.
454  IPPROTO_GRE = 47, /// General Routing Encapsulation.
455  IPPROTO_ESP = 50, /// encapsulating security payload.
456  IPPROTO_AH = 51, /// authentication header.
457  IPPROTO_ICMPV6 = 58, /// ICMPv6.
458  IPPROTO_NONE = 59, /// IPv6 no next header.
459  IPPROTO_DSTOPTS = 60, /// IPv6 destination options.
460  IPPROTO_MTP = 92, /// Multicast Transport Protocol.
461  IPPROTO_ENCAP = 98, /// Encapsulation Header.
462  IPPROTO_PIM = 103, /// Protocol Independent Multicast.
463  IPPROTO_COMP = 108, /// Compression Header Protocol.
464  IPPROTO_SCTP = 132, /// Stream Control Transmission Protocol.
465  IPPROTO_UDPLITE = 136, /// UDP-Lite protocol.
466  IPPROTO_RAW = 255, /// Raw IP packets.
468  } IPProto;
469 
470  /// Access the packet header version.
471  /// \return The header version
472  uint8_t version() const;
473 
474  /// Access the packet header len.
475  /// \return The header length in 32bit words
476  uint8_t headerlen() const;
477 
478  /// Access the service type (also called TOS).
479  /// \return The service type, one of IPTOS_*
480  uint8_t service() const;
481 
482  /// Access the packet precendence.
483  /// \return The precendence
484  uint8_t precedence() const;
485 
486  /// Access the packet length
487  /// \return The total packet length including payload and header in native byte order.
488  uint16_t length() const;
489 
490  /// Access the packet ID.
491  /// \return The packet ID in native byte order.
492  uint16_t id() const;
493 
494  /// Access the packet flags
495  /// \return The packet flags
496  uint8_t flags() const;
497 
498  /// Access the packet fragment offset
499  /// \return The packet fragment offset in native byte order.
500  uint16_t offset() const;
501 
502  /// Access the packet time-to-live.
503  /// \return the packet time-to-live.
504  uint8_t ttl() const;
505 
506  /// Access the packet protocol
507  /// \return The packet protocol, one of IPProto.
508  uint8_t protocol() const;
509 
510  /// Access the source IP address
511  /// \return A reference to the source address
512  const IPv4Address& source() const;
513 
514  /// Access the destination IP address
515  /// \return A reference to the destination address
516  const IPv4Address& dest() const;
517 
518  /// Access the packet checksum
519  /// return the checksum in native byte order.
520  uint16_t checksum() const;
521 
522  /// Access the packet payload
523  /// \return A pointer to the packet payload data
524  uint8_t* payload();
525 
526  /// Print.
527  /// Prints an ASCII formatted version of the header to a Printable such as Serial.
528  /// \return The number of characters printed
529  size_t printTo(Print& p) const;
530 
531 private:
532  uint8_t _verslen;
533  uint8_t _service;
534  uint16_t _length;
535  uint16_t _id;
536  uint16_t _flags_offset;
537  uint8_t _ttl;
538  uint8_t _protocol;
539  uint16_t _checksum;
540  IPv4Address _source;
541  IPv4Address _dest;
542  uint8_t _options[0]; // payload follows
543 
544 };
545 
546 /////////////////////////////////////////////////////////////////////
547 /// \class PacketTCP EtherRaw.h <EtherRaw.h>
548 /// \brief Support for TCP Packet Headers
549 ///
550 /// This class allows easy access to the contents of a TCP Packet
551 /// in on-the-wire format.
552 ///
553 struct PacketTCP
554 {
555  /// Masks for code bits returned by code()
556  typedef enum
557  {
558  URG = 0x20,
559  ACK = 0x10,
560  PSH = 0x08,
561  RST = 0x04,
562  SYN = 0x02,
563  FIN = 0x01
564  } TCPCode;
565 
566 public:
567  /// Access the source port number
568  /// \return The source port number in native byte order.
569  uint16_t sourcePort() const;
570 
571  /// Access the destination port number
572  /// \return The destination port number in native byte order.
573  uint16_t destPort() const;
574 
575  /// Access the TCP sequence number
576  /// \return The TCP sequence number in native byte order.
577  uint32_t sequence() const;
578 
579  /// Access the TCP acknowledgement number
580  /// \return The TCP acknowledgement number in native byte order.
581  uint32_t acknowledgement() const;
582 
583  /// Access the header length
584  /// \return The header length in 32bit words
585  uint8_t headerlen() const;
586 
587  /// Access the packet code bits
588  /// \return the code bits a mask of TCPCode
589  uint8_t code() const;
590 
591  /// Access the TCP window
592  /// \return the window in native byte order.
593  uint16_t window() const;
594 
595  /// Access the TCP checksum
596  /// \return the checksum in native byte order.
597  uint16_t checksum() const;
598 
599  /// Access the TCP urgent pointer
600  /// \return the urgent pointer in native byte order.
601  uint16_t urgent() const;
602 
603  /// Access the packet payload
604  /// \return A pointer to the TCP packet payload data
605  uint8_t* payload();
606 
607  /// Print.
608  /// Prints an ASCII formatted version of the header to a Printable such as Serial.
609  /// \return The number of characters printed
610  size_t printTo(Print& p) const;
611 
612 private:
613  uint16_t _sourcePort;
614  uint16_t _destPort;
615  uint32_t _sequence;
616  uint32_t _acknowledgement;
617  uint16_t _hlen_code;
618  uint16_t _window;
619  uint16_t _checksum;
620  uint16_t _urgent;
621  uint8_t _options[0]; // payload follows
622 
623 };
624 
625 /////////////////////////////////////////////////////////////////////
626 /// \class PacketUDP EtherRaw.h <EtherRaw.h>
627 /// \brief Support for UDP Packet Headers
628 ///
629 /// This class allows easy access to the contents of a UDP Packet
630 /// in on-the-wire format.
631 ///
632 struct PacketUDP
633 {
634  /// Access the source port number
635  /// \return The source port number in native byte order.
636  uint16_t sourcePort() const;
637 
638  /// Access the destination port number
639  /// \return The destination port number in native byte order.
640  uint16_t destPort() const;
641 
642  /// Access the packet length.
643  /// \return The total packet length (including header and payload) in native byte order.
644  uint16_t length() const;
645 
646  /// Access the UDP checksum
647  /// \return the checksum in native byte order.
648  uint16_t checksum() const;
649 
650  /// Access the packet payload
651  /// \return A pointer to the TCP packet payload data
652  uint8_t* payload();
653 
654  /// Access thepayload length
655  /// \return the payload length in native byte order.
656  uint16_t payload_length();
657 
658  /// Print.
659  /// Prints an ASCII formatted version of the header to a Printable such as Serial.
660  /// \return The number of characters printed
661  size_t printTo(Print& p) const;
662 
663 private:
664  uint16_t _sourcePort;
665  uint16_t _destPort;
666  uint16_t _length;
667  uint16_t _checksum;
668  uint8_t _data[0];
669 
670 };
671 
672 #endif