IPv4Packet.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2017 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * Changes from Qualcomm Innovation Center are provided under the following license:
  30. *
  31. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  32. *
  33. * Redistribution and use in source and binary forms, with or without
  34. * modification, are permitted (subject to the limitations in the
  35. * disclaimer below) provided that the following conditions are met:
  36. *
  37. * * Redistributions of source code must retain the above copyright
  38. * notice, this list of conditions and the following disclaimer.
  39. *
  40. * * Redistributions in binary form must reproduce the above
  41. * copyright notice, this list of conditions and the following
  42. * disclaimer in the documentation and/or other materials provided
  43. * with the distribution.
  44. *
  45. * * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
  46. * contributors may be used to endorse or promote products derived
  47. * from this software without specific prior written permission.
  48. *
  49. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  50. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  51. * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  52. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  53. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  54. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  55. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  56. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  57. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  58. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  59. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  60. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  61. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  62. */
  63. #ifndef __IPA_TESTS_IPV4_PACKET__H__
  64. #define __IPA_TESTS_IPV4_PACKET__H__
  65. namespace IPA
  66. {
  67. #define IPV4_FLAGS_BYTE_OFFSET 6
  68. /**
  69. @brief
  70. IPv4Packet abstract class.
  71. @details
  72. new setters should call to RecalculateChecksum();
  73. */
  74. class IPv4Packet
  75. {
  76. public:
  77. IPv4Packet(unsigned int size);
  78. virtual ~IPv4Packet(void);
  79. void FromByteStream(unsigned char *buffer, unsigned int length);
  80. void ToNetworkByteStream(unsigned char *addr);
  81. unsigned int GetSrcAddr(void);
  82. void SetSrcAddr(unsigned int addr);
  83. unsigned int GetDstAddr(void);
  84. void SetDstAddr(unsigned int addr);
  85. unsigned char GetProtocol(void);
  86. unsigned int GetSize(void)
  87. {
  88. return m_PacketSize;
  89. }
  90. virtual unsigned short GetSrcPort(void);
  91. virtual unsigned short GetDstPort(void);
  92. virtual void SetDstPort(unsigned short port);
  93. virtual void SetSrcPort(unsigned short port);
  94. void SetMF(bool bValue);
  95. protected:
  96. virtual void RecalculateTCPChecksum(void) {}
  97. virtual void RecalculateUDPChecksum(void) {}
  98. virtual void RecalculateICMPChecksum(void) {}
  99. unsigned char *m_Packet;
  100. private:
  101. unsigned int m_PacketSize;
  102. void RecalculateChecksum(void);
  103. void RecalculateIPChecksum(void);
  104. };
  105. /**
  106. @brief
  107. TCPPacket implementation.
  108. @details
  109. new setters should call to RecalculateChecksum();
  110. */
  111. class TCPPacket:public IPv4Packet
  112. {
  113. public:
  114. TCPPacket(void);
  115. virtual ~TCPPacket(void) {}
  116. protected:
  117. virtual void RecalculateTCPChecksum(void);
  118. };
  119. /**
  120. @brief
  121. UDPPacket implementation.
  122. @details
  123. new setters should call to RecalculateChecksum();
  124. */
  125. class UDPPacket:public IPv4Packet
  126. {
  127. public:
  128. UDPPacket(void);
  129. virtual ~UDPPacket(void) {}
  130. protected:
  131. virtual void RecalculateUDPChecksum(void);
  132. };
  133. /**
  134. @brief
  135. ICMPPacket implementation.
  136. @details
  137. new setters should call to RecalculateChecksum();
  138. */
  139. class ICMPPacket:public IPv4Packet
  140. {
  141. public:
  142. ICMPPacket(void);
  143. virtual ~ICMPPacket(void) {}
  144. virtual unsigned short GetSrcPort(void);
  145. virtual unsigned short GetDstPort(void);
  146. virtual void SetDstPort(unsigned short port);
  147. virtual void SetSrcPort(unsigned short port);
  148. protected:
  149. /**
  150. @brief
  151. RecalculateICMPChecksum method.
  152. @details
  153. ICMP checksum recalculation is not needed now
  154. and by so is not implemented yet
  155. TODO: implement if needed
  156. */
  157. virtual void RecalculateICMPChecksum(void)
  158. {
  159. return;
  160. }
  161. };
  162. } /* namespace IPA */
  163. #endif