Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProtocolDecoders.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /* Protocol decoders for Ethernet, TCP, IP, ICMP and ARP. Each of these routines
  27. accepts a header to the appropriate protocol and prints out pertinent information
  28. on the packet through the serial port.
  29. To disable printing of a specific protocol, define the token NO_DECODE_{Protocol}
  30. in the project makefile, and pass it to the compiler using the -D switch.
  31. */
  32. /** \file
  33. *
  34. * Protocol decoding routines, for the plain-text decoding of Ethernet frames for debugging purposes.
  35. * Enabled protocol decoders will print incoming Ethernet frame contents through the USART in a human
  36. * readable format.
  37. *
  38. * Note that the USART is a slow transmission medium, and will slow down packet processing considerably.
  39. * Packet decoding routines can be disabled by defining NO_DECODE_{Protocol Name} in the project makefile
  40. * and passing it to the compiler via the -D switch.
  41. */
  42. #include "ProtocolDecoders.h"
  43. /** Decodes an Ethernet frame header and prints its contents to through the USART in a human readable format.
  44. *
  45. * \param[in] InDataStart Pointer to the start of an Ethernet frame of data
  46. */
  47. void DecodeEthernetFrameHeader(void* InDataStart)
  48. {
  49. #if !defined(NO_DECODE_ETHERNET)
  50. Ethernet_Frame_Header_t* FrameHeader = (Ethernet_Frame_Header_t*)InDataStart;
  51. printf_P(PSTR("\r\n"));
  52. printf_P(PSTR(" ETHERNET\r\n"));
  53. if (!(MAC_COMPARE(&FrameHeader->Destination, &ServerMACAddress)) &&
  54. !(MAC_COMPARE(&FrameHeader->Destination, &BroadcastMACAddress)))
  55. {
  56. printf_P(PSTR(" + NOT ADDRESSED TO DEVICE\r\n"));
  57. return;
  58. }
  59. printf_P(PSTR(" + MAC Source : %02X:%02X:%02X:%02X:%02X:%02X\r\n"), FrameHeader->Source.Octets[0],
  60. FrameHeader->Source.Octets[1],
  61. FrameHeader->Source.Octets[2],
  62. FrameHeader->Source.Octets[3],
  63. FrameHeader->Source.Octets[4],
  64. FrameHeader->Source.Octets[5]);
  65. printf_P(PSTR(" + MAC Dest: %02X:%02X:%02X:%02X:%02X:%02X\r\n"), FrameHeader->Destination.Octets[0],
  66. FrameHeader->Destination.Octets[1],
  67. FrameHeader->Destination.Octets[2],
  68. FrameHeader->Destination.Octets[3],
  69. FrameHeader->Destination.Octets[4],
  70. FrameHeader->Destination.Octets[5]);
  71. printf_P(PSTR(" + Protocol: 0x%04x\r\n"), SwapEndian_16(FrameHeader->EtherType));
  72. #endif
  73. }
  74. /** Decodes an ARP header and prints its contents to through the USART in a human readable format.
  75. *
  76. * \param[in] InDataStart Pointer to the start of an ARP packet header
  77. */
  78. void DecodeARPHeader(void* InDataStart)
  79. {
  80. #if !defined(NO_DECODE_ARP)
  81. ARP_Header_t* ARPHeader = (ARP_Header_t*)InDataStart;
  82. printf_P(PSTR(" \\\r\n ARP\r\n"));
  83. if (!(IP_COMPARE(&ARPHeader->TPA, &ServerIPAddress)) &&
  84. !(MAC_COMPARE(&ARPHeader->THA, &ServerMACAddress)))
  85. {
  86. printf_P(PSTR(" + NOT ADDRESSED TO DEVICE\r\n"));
  87. return;
  88. }
  89. printf_P(PSTR(" + Protocol: %x\r\n"), SwapEndian_16(ARPHeader->ProtocolType));
  90. printf_P(PSTR(" + Operation: %u\r\n"), SwapEndian_16(ARPHeader->Operation));
  91. if (SwapEndian_16(ARPHeader->ProtocolType) == ETHERTYPE_IPV4)
  92. {
  93. printf_P(PSTR(" + SHA MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n"), ARPHeader->SHA.Octets[0],
  94. ARPHeader->SHA.Octets[1],
  95. ARPHeader->SHA.Octets[2],
  96. ARPHeader->SHA.Octets[3],
  97. ARPHeader->SHA.Octets[4],
  98. ARPHeader->SHA.Octets[5]);
  99. printf_P(PSTR(" + SPA IP: %u.%u.%u.%u\r\n"), ARPHeader->SPA.Octets[0],
  100. ARPHeader->SPA.Octets[1],
  101. ARPHeader->SPA.Octets[2],
  102. ARPHeader->SPA.Octets[3]);
  103. printf_P(PSTR(" + THA MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n"), ARPHeader->THA.Octets[0],
  104. ARPHeader->THA.Octets[1],
  105. ARPHeader->THA.Octets[2],
  106. ARPHeader->THA.Octets[3],
  107. ARPHeader->THA.Octets[4],
  108. ARPHeader->THA.Octets[5]);
  109. printf_P(PSTR(" + TPA IP: %u.%u.%u.%u\r\n"), ARPHeader->TPA.Octets[0],
  110. ARPHeader->TPA.Octets[1],
  111. ARPHeader->TPA.Octets[2],
  112. ARPHeader->TPA.Octets[3]);
  113. }
  114. #endif
  115. }
  116. /** Decodes an IP header and prints its contents to through the USART in a human readable format.
  117. *
  118. * \param[in] InDataStart Pointer to the start of an IP packet header
  119. */
  120. void DecodeIPHeader(void* InDataStart)
  121. {
  122. #if !defined(NO_DECODE_IP)
  123. IP_Header_t* IPHeader = (IP_Header_t*)InDataStart;
  124. uint16_t HeaderLengthBytes = (IPHeader->HeaderLength * sizeof(uint32_t));
  125. printf_P(PSTR(" \\\r\n IP\r\n"));
  126. if (!(IP_COMPARE(&IPHeader->DestinationAddress, &ServerIPAddress)))
  127. {
  128. printf_P(PSTR(" + NOT ADDRESSED TO DEVICE\r\n"));
  129. return;
  130. }
  131. printf_P(PSTR(" + Header Length: %u Bytes\r\n"), HeaderLengthBytes);
  132. printf_P(PSTR(" + Packet Version: %u\r\n"), IPHeader->Version);
  133. printf_P(PSTR(" + Total Length: %u\r\n"), SwapEndian_16(IPHeader->TotalLength));
  134. printf_P(PSTR(" + Protocol: %u\r\n"), IPHeader->Protocol);
  135. printf_P(PSTR(" + TTL: %u\r\n"), IPHeader->TTL);
  136. printf_P(PSTR(" + IP Src: %u.%u.%u.%u\r\n"), IPHeader->SourceAddress.Octets[0],
  137. IPHeader->SourceAddress.Octets[1],
  138. IPHeader->SourceAddress.Octets[2],
  139. IPHeader->SourceAddress.Octets[3]);
  140. printf_P(PSTR(" + IP Dst: %u.%u.%u.%u\r\n"), IPHeader->DestinationAddress.Octets[0],
  141. IPHeader->DestinationAddress.Octets[1],
  142. IPHeader->DestinationAddress.Octets[2],
  143. IPHeader->DestinationAddress.Octets[3]);
  144. #endif
  145. }
  146. /** Decodes an ICMP header and prints its contents to through the USART in a human readable format.
  147. *
  148. * \param[in] InDataStart Pointer to the start of an ICMP packet header
  149. */
  150. void DecodeICMPHeader(void* InDataStart)
  151. {
  152. #if !defined(NO_DECODE_ICMP)
  153. ICMP_Header_t* ICMPHeader = (ICMP_Header_t*)InDataStart;
  154. printf_P(PSTR(" \\\r\n ICMP\r\n"));
  155. printf_P(PSTR(" + Type: %u\r\n"), ICMPHeader->Type);
  156. printf_P(PSTR(" + Code: %u\r\n"), ICMPHeader->Code);
  157. #endif
  158. }
  159. /** Decodes a TCP header and prints its contents to through the USART in a human readable format.
  160. *
  161. * \param[in] InDataStart Pointer to the start of a TCP packet header
  162. */
  163. void DecodeTCPHeader(void* InDataStart)
  164. {
  165. #if !defined(NO_DECODE_TCP)
  166. TCP_Header_t* TCPHeader = (TCP_Header_t*)InDataStart;
  167. uint16_t HeaderLengthBytes = (TCPHeader->DataOffset * sizeof(uint32_t));
  168. printf_P(PSTR(" \\\r\n TCP\r\n"));
  169. printf_P(PSTR(" + Header Length: %u Bytes\r\n"), HeaderLengthBytes);
  170. printf_P(PSTR(" + Source Port: %u\r\n"), SwapEndian_16(TCPHeader->SourcePort));
  171. printf_P(PSTR(" + Destination Port: %u\r\n"), SwapEndian_16(TCPHeader->DestinationPort));
  172. printf_P(PSTR(" + Sequence Number: %lu\r\n"), SwapEndian_32(TCPHeader->SequenceNumber));
  173. printf_P(PSTR(" + Acknowledgment Number: %lu\r\n"), SwapEndian_32(TCPHeader->AcknowledgmentNumber));
  174. printf_P(PSTR(" + Flags: 0x%02X\r\n"), TCPHeader->Flags);
  175. if (TCP_GetPortState(TCPHeader->DestinationPort) == TCP_Port_Closed)
  176. printf_P(PSTR(" + NOT LISTENING ON DESTINATION PORT\r\n"));
  177. #endif
  178. }
  179. /** Decodes an UDP header and prints its contents to through the USART in a human readable format.
  180. *
  181. * \param[in] InDataStart Pointer to the start of a UDP packet header
  182. */
  183. void DecodeUDPHeader(void* InDataStart)
  184. {
  185. #if !defined(NO_DECODE_UDP)
  186. UDP_Header_t* UDPHeader = (UDP_Header_t*)InDataStart;
  187. printf_P(PSTR(" \\\r\n UDP\r\n"));
  188. printf_P(PSTR(" + Source Port: %u\r\n"), SwapEndian_16(UDPHeader->SourcePort));
  189. printf_P(PSTR(" + Destination Port: %u\r\n"), SwapEndian_16(UDPHeader->DestinationPort));
  190. printf_P(PSTR(" + Data Length: %d\r\n"), SwapEndian_16(UDPHeader->Length));
  191. #endif
  192. }
  193. /** Decodes an DHCP header and prints its contents to through the USART in a human readable format.
  194. *
  195. * \param[in] InDataStart Pointer to the start of a DHCP packet header
  196. */
  197. void DecodeDHCPHeader(void* InDataStart)
  198. {
  199. #if !defined(NO_DECODE_DHCP)
  200. uint8_t* DHCPOptions = (InDataStart + sizeof(DHCP_Header_t));
  201. printf_P(PSTR(" \\\r\n DHCP\r\n"));
  202. while (DHCPOptions[0] != DHCP_OPTION_END)
  203. {
  204. if (DHCPOptions[0] == DHCP_OPTION_MESSAGETYPE)
  205. {
  206. switch (DHCPOptions[2])
  207. {
  208. case DHCP_MESSAGETYPE_DISCOVER:
  209. printf_P(PSTR(" + DISCOVER\r\n"));
  210. break;
  211. case DHCP_MESSAGETYPE_REQUEST:
  212. printf_P(PSTR(" + REQUEST\r\n"));
  213. break;
  214. case DHCP_MESSAGETYPE_RELEASE:
  215. printf_P(PSTR(" + RELEASE\r\n"));
  216. break;
  217. case DHCP_MESSAGETYPE_DECLINE:
  218. printf_P(PSTR(" + DECLINE\r\n"));
  219. break;
  220. }
  221. }
  222. DHCPOptions += ((DHCPOptions[0] == DHCP_OPTION_PAD) ? 1 : (DHCPOptions[1] + 2));
  223. }
  224. #endif
  225. }