QmapHeader.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_QMAPHEADER_H
  30. #define NETWORK_TRAFFIC_QMAPHEADER_H
  31. #include "Header.h"
  32. using std::bitset;
  33. class QmapHeader: public Header {
  34. public:
  35. const static size_t mSize {8};
  36. DECLARE_BITSET(Pad, 6, 0);
  37. DECLARE_BITSET(NextHdr, 1, 1);
  38. DECLARE_BITSET(Cd, 1, 0);
  39. DECLARE_BITSET(MuxId, 8, 0);
  40. DECLARE_BITSET(PacketLength, 16, 0);
  41. DECLARE_BITSET(ExtensionNextHeader, 1, 0);
  42. DECLARE_BITSET(HeaderType, 7, 3);
  43. DECLARE_BITSET(AdditionalHdrSize, 5, 0);
  44. DECLARE_BITSET(Res, 1, 0);
  45. DECLARE_BITSET(ZeroChecksum, 1, 0);
  46. DECLARE_BITSET(IpIdCfg, 1, 0);
  47. DECLARE_BITSET(SegmentSize, 16, 1);
  48. explicit QmapHeader(const uint8_t *start) {
  49. unsigned int bufIndex = 0;
  50. setBitsFromArray<uint8_t, 6>(mPad, start, bufIndex);
  51. setBitsFromArray<uint8_t, 1>(mNextHdr, start, bufIndex);
  52. setBitsFromArray<uint8_t, 1>(mCd, start, bufIndex);
  53. setBitsFromArray<uint8_t, 8>(mMuxId, start, bufIndex);
  54. setBitsFromArray<uint8_t, 16>(mPacketLength, start, bufIndex);
  55. setBitsFromArray<uint8_t, 1>(mExtensionNextHeader, start, bufIndex);
  56. setBitsFromArray<uint8_t, 7>(mHeaderType, start, bufIndex);
  57. setBitsFromArray<uint8_t, 5>(mAdditionalHdrSize, start, bufIndex);
  58. setBitsFromArray<uint8_t, 1>(mRes, start, bufIndex);
  59. setBitsFromArray<uint8_t, 1>(mZeroChecksum, start, bufIndex);
  60. setBitsFromArray<uint8_t, 1>(mIpIdCfg, start, bufIndex);
  61. setBitsFromArray<uint8_t, 16>(mSegmentSize, start, bufIndex);
  62. }
  63. QmapHeader() = default;
  64. vector<bool> asVector() const override {
  65. vector<bool> outVec;
  66. auto inserter = [](vector<bool>& vec, auto val){
  67. vector<bool> valAsVector = bitsetToVector<val.size()>(val);
  68. vec.insert(vec.end(), valAsVector.begin(), valAsVector.end());};
  69. inserter(outVec, mPad);
  70. inserter(outVec, mNextHdr);
  71. inserter(outVec, mCd);
  72. inserter(outVec, mMuxId);
  73. vector<bool> v = bitsetToVector<16>(mPacketLength);
  74. toggleLsbMsb(v, v.size());
  75. toggleEndianness(v, v.size());
  76. outVec.insert(outVec.end(), v.begin(), v.end());
  77. inserter(outVec, mExtensionNextHeader);
  78. v = bitsetToVector<7>(mHeaderType);
  79. toggleLsbMsb(v, v.size());
  80. outVec.insert(outVec.end(), v.begin(), v.end());
  81. inserter(outVec, mAdditionalHdrSize);
  82. inserter(outVec, mRes);
  83. inserter(outVec, mZeroChecksum);
  84. inserter(outVec, mIpIdCfg);
  85. v = bitsetToVector<16>(mSegmentSize);
  86. toggleLsbMsb(v, v.size());
  87. toggleEndianness(v, v.size());
  88. outVec.insert(outVec.end(), v.begin(), v.end());
  89. return outVec;
  90. }
  91. size_t size() const override {
  92. return mSize;
  93. }
  94. string name() const override {
  95. return string("QMAP");
  96. }
  97. void streamFields(std::ostream &out) const override {
  98. out << "Padding: " << mPad.to_ulong() << ", "
  99. << "Next Header: " << mNextHdr.to_ulong() << ", "
  100. << "C\\D: " << mCd.to_ulong() << ", "
  101. << "Mux ID: " << mMuxId.to_ulong() << ", "
  102. << "Packet Length With Padding: " << mPacketLength.to_ulong() << ", "
  103. << "Extension Next Header: " << mExtensionNextHeader.to_ulong() << ", "
  104. << "Header Type: " << mHeaderType.to_ulong() << ", "
  105. << "Additional Header Size: " << mAdditionalHdrSize.to_ulong() << ", "
  106. << "Res: " << mRes.to_ulong() << ", "
  107. << "Zero Checksum: " << mZeroChecksum.to_ulong() << ", "
  108. << "IPID Cfg: " << mIpIdCfg.to_ulong() << ", "
  109. << "Segment Size: " << mSegmentSize.to_ulong() << "\n";
  110. }
  111. };
  112. #endif //NETWORK_TRAFFIC_QMAPHEADER_H