IPv4Header.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2021 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 NETWORK_TRAFFIC_IPV4HEADER_H
  30. #define NETWORK_TRAFFIC_IPV4HEADER_H
  31. #include "InternetHeader.h"
  32. using std::bitset;
  33. class IPv4Header: public InternetHeader {
  34. public:
  35. const static unsigned int mSize {20};
  36. explicit IPv4Header(const uint8_t *start) {
  37. unsigned int bufIndex = 0;
  38. setBitsFromArray<uint8_t, 4>(mVersion, start, bufIndex);
  39. setBitsFromArray<uint8_t, 4>(mIhl, start, bufIndex);
  40. setBitsFromArray<uint8_t, 6>(mDscp, start, bufIndex);
  41. setBitsFromArray<uint8_t, 2>(mEcn, start, bufIndex);
  42. setBitsFromArray<uint8_t, 16>(mTotalLength, start, bufIndex);
  43. setBitsFromArray<uint8_t, 16>(mId, start, bufIndex);
  44. setBitsFromArray<uint8_t, 3>(mFlags, start, bufIndex);
  45. setBitsFromArray<uint8_t, 13>(mFragmentOffset, start, bufIndex);
  46. setBitsFromArray<uint8_t, 8>(mTimeToLive, start, bufIndex);
  47. setBitsFromArray<uint8_t, 8>(mProtocol, start, bufIndex);
  48. setBitsFromArray<uint8_t, 16>(mHeaderChecksum, start, bufIndex);
  49. setBitsFromArray<uint8_t, 32>(mSourceIpAddress, start, bufIndex);
  50. setBitsFromArray<uint8_t, 32>(mDestIpAddress, start, bufIndex);
  51. }
  52. DECLARE_BITSET(Version, 4, 4);
  53. DECLARE_BITSET(Ihl, 4, 5);
  54. DECLARE_BITSET(Dscp, 6, 0);
  55. DECLARE_BITSET(Ecn, 2, 0);
  56. DECLARE_BITSET(TotalLength, 16, mSize);
  57. DECLARE_BITSET(Id, 16, 65530);
  58. DECLARE_BITSET(Flags, 3, 2);
  59. DECLARE_BITSET(FragmentOffset, 13, 0);
  60. DECLARE_BITSET(TimeToLive, 8, 64);
  61. DECLARE_BITSET(Protocol, 8, 0);
  62. DECLARE_BITSET(HeaderChecksum, 16, 0);
  63. DECLARE_BITSET(SourceIpAddress, 32, 3232235793);
  64. DECLARE_BITSET(DestIpAddress, 32, 3232235816);
  65. IPv4Header() = default;
  66. IPv4Header(const IPv4Header& iPv4Header) = default;
  67. vector<bool> asVector() const override {
  68. vector<bool> outVec;
  69. auto inserter = [](vector<bool>& vec, auto val){
  70. vector<bool> valAsVector = bitsetToVector<val.size()>(val);
  71. vec.insert(vec.end(), valAsVector.begin(), valAsVector.end());};
  72. inserter(outVec, mVersion);
  73. inserter(outVec, mIhl);
  74. inserter(outVec, mDscp);
  75. inserter(outVec, mEcn);
  76. inserter(outVec, mTotalLength);
  77. inserter(outVec, mId);
  78. inserter(outVec, mFlags);
  79. inserter(outVec, mFragmentOffset);
  80. inserter(outVec, mTimeToLive);
  81. inserter(outVec, mProtocol);
  82. inserter(outVec, mHeaderChecksum);
  83. inserter(outVec, mSourceIpAddress);
  84. inserter(outVec, mDestIpAddress);
  85. toggleLsbMsb(outVec, 8);
  86. return outVec;
  87. }
  88. size_t size() const override {
  89. return mSize;
  90. }
  91. void adjust(size_t payloadSize, uint8_t protocol){
  92. mTotalLength = size() + payloadSize;
  93. mProtocol = protocol;
  94. fixChecksum();
  95. }
  96. static size_t getEtherType(){
  97. return 0x0800;
  98. }
  99. string name() const override {
  100. return string("IPV4");
  101. }
  102. void streamFields(std::ostream &out) const override {
  103. out << "Version: " << mVersion.to_ulong() << ", "
  104. << "IHL: " << mIhl.to_ulong() << ", "
  105. << "DSCP: " << mDscp.to_ulong() << ", "
  106. << "ECN: " << mEcn.to_ulong() << ", "
  107. << "Total Length: " << mTotalLength.to_ulong() << ", "
  108. << "ID: " << mId.to_ulong() << ", "
  109. << "Flags: " << mFlags.to_ulong() << ", "
  110. << "Fragment Offset: " << mFragmentOffset.to_ulong() << ", "
  111. << "TTL: " << mTimeToLive.to_ulong() << ", "
  112. << "Protocol: " << mProtocol.to_ulong() << ", "
  113. << "Checksum: " << mHeaderChecksum.to_ulong() << ", "
  114. << "Source IP Address: " << expandIpv4(mSourceIpAddress.to_ulong()) << ", "
  115. << "Destination IP Address: " << expandIpv4(mDestIpAddress.to_ulong()) << "\n";
  116. }
  117. void tcpChecksumPseudoHeader(uint8_t *pseudoHeaderBuf, const uint8_t *ipHeader) const {
  118. memcpy(pseudoHeaderBuf, ipHeader + 12, 8);
  119. pseudoHeaderBuf[8] = 0;
  120. pseudoHeaderBuf[9] = 6;
  121. *reinterpret_cast<uint16_t*>(pseudoHeaderBuf + 10) =
  122. htons(static_cast<uint16_t>(mTotalLength.to_ulong() - (mIhl.to_ulong() << 2u)));
  123. }
  124. static size_t l3ChecksumPseudoHeaderSize(){
  125. return 12;
  126. }
  127. void udpChecksumPseudoHeader(uint8_t *pseudoHeaderBuf, const uint8_t *ipHeader) const {
  128. memcpy(pseudoHeaderBuf, ipHeader + 12, 8);
  129. pseudoHeaderBuf[8] = 0;
  130. pseudoHeaderBuf[9] = 17;
  131. *reinterpret_cast<uint16_t*>(pseudoHeaderBuf + 10) =
  132. htons(static_cast<uint16_t>(mTotalLength.to_ulong() - (mIhl.to_ulong() << 2u)));
  133. }
  134. private:
  135. void fixChecksum(){
  136. mHeaderChecksum = 0;
  137. uint8_t arr[mSize];
  138. asArray(arr);
  139. mHeaderChecksum = computeChecksum(reinterpret_cast<uint16_t*>(arr), mSize);
  140. }
  141. };
  142. #endif //NETWORK_TRAFFIC_IPV4HEADER_H