IPv4Packet.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifndef __IPA_TESTS_IPV4_PACKET__H__
  30. #define __IPA_TESTS_IPV4_PACKET__H__
  31. namespace IPA
  32. {
  33. #define IPV4_FLAGS_BYTE_OFFSET 6
  34. /**
  35. @brief
  36. IPv4Packet abstract class.
  37. @details
  38. new setters should call to RecalculateChecksum();
  39. */
  40. class IPv4Packet
  41. {
  42. public:
  43. IPv4Packet(unsigned int size);
  44. virtual ~IPv4Packet(void) = 0;
  45. void ToNetworkByteStream(unsigned char *addr);
  46. unsigned int GetSrcAddr(void);
  47. void SetSrcAddr(unsigned int addr);
  48. unsigned int GetDstAddr(void);
  49. void SetDstAddr(unsigned int addr);
  50. unsigned char GetProtocol(void);
  51. unsigned int GetSize(void)
  52. {
  53. return m_PacketSize;
  54. }
  55. virtual unsigned short GetSrcPort(void);
  56. virtual unsigned short GetDstPort(void);
  57. virtual void SetDstPort(unsigned short port);
  58. virtual void SetSrcPort(unsigned short port);
  59. void SetMF(bool bValue);
  60. protected:
  61. virtual void RecalculateTCPChecksum(void) {}
  62. virtual void RecalculateUDPChecksum(void) {}
  63. virtual void RecalculateICMPChecksum(void) {}
  64. unsigned char *m_Packet;
  65. private:
  66. unsigned int m_PacketSize;
  67. void RecalculateChecksum(void);
  68. void RecalculateIPChecksum(void);
  69. };
  70. /**
  71. @brief
  72. TCPPacket implementation.
  73. @details
  74. new setters should call to RecalculateChecksum();
  75. */
  76. class TCPPacket:public IPv4Packet
  77. {
  78. public:
  79. TCPPacket(void);
  80. ~TCPPacket(void) {}
  81. protected:
  82. virtual void RecalculateTCPChecksum(void);
  83. };
  84. /**
  85. @brief
  86. UDPPacket implementation.
  87. @details
  88. new setters should call to RecalculateChecksum();
  89. */
  90. class UDPPacket:public IPv4Packet
  91. {
  92. public:
  93. UDPPacket(void);
  94. ~UDPPacket(void) {}
  95. protected:
  96. virtual void RecalculateUDPChecksum(void);
  97. };
  98. /**
  99. @brief
  100. ICMPPacket implementation.
  101. @details
  102. new setters should call to RecalculateChecksum();
  103. */
  104. class ICMPPacket:public IPv4Packet
  105. {
  106. public:
  107. ICMPPacket(void);
  108. ~ICMPPacket(void) {}
  109. virtual unsigned short GetSrcPort(void);
  110. virtual unsigned short GetDstPort(void);
  111. virtual void SetDstPort(unsigned short port);
  112. virtual void SetSrcPort(unsigned short port);
  113. protected:
  114. /**
  115. @brief
  116. RecalculateICMPChecksum method.
  117. @details
  118. ICMP checksum recalculation is not needed now
  119. and by so is not implemented yet
  120. TODO: implement if needed
  121. */
  122. virtual void RecalculateICMPChecksum(void)
  123. {
  124. return;
  125. }
  126. };
  127. } /* namespace IPA */
  128. #endif