UdpHeader.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_UDPHEADER_H
  30. #define NETWORK_TRAFFIC_UDPHEADER_H
  31. #include "TransportHeader.h"
  32. class UdpHeader: public TransportHeader {
  33. public:
  34. const static unsigned int mSize {8};
  35. DECLARE_BITSET(SourcePort, 16, 1985);
  36. DECLARE_BITSET(DestPort, 16, 1985);
  37. DECLARE_BITSET(Length, 16, 0);
  38. DECLARE_BITSET(Checksum, 16, 0);
  39. explicit UdpHeader(const uint8_t *start) {
  40. unsigned int bufIndex = 0;
  41. setBitsFromArray<uint8_t, 16>(mSourcePort, start, bufIndex);
  42. setBitsFromArray<uint8_t, 16>(mDestPort, start, bufIndex);
  43. setBitsFromArray<uint8_t, 16>(mLength, start, bufIndex);
  44. setBitsFromArray<uint8_t, 16>(mChecksum, start, bufIndex);
  45. }
  46. UdpHeader(uint16_t sourcePort, uint16_t destPort, uint16_t length, uint16_t checksum) :
  47. mSourcePort(sourcePort),
  48. mDestPort(destPort),
  49. mLength(length),
  50. mChecksum(checksum) {}
  51. UdpHeader(const UdpHeader& udpHeader) = default;
  52. UdpHeader() = default;
  53. vector<bool> asVector() const override {
  54. vector<bool> outVec;
  55. auto inserter2 = [&outVec](bitset<16> val){
  56. vector<bool> valAsVector = bitsetToVector<16>(val);
  57. toggleLsbMsb(valAsVector, CHAR_BIT);
  58. outVec.insert(outVec.end(), valAsVector.begin(), valAsVector.end());};
  59. inserter2(mSourcePort);
  60. inserter2(mDestPort);
  61. inserter2(mLength);
  62. inserter2(mChecksum);
  63. return outVec;
  64. }
  65. size_t size() const override {
  66. return mSize;
  67. }
  68. static uint8_t protocolNum(){
  69. return 17;
  70. }
  71. void adjust(const size_t payloadSize){
  72. mLength = mSize + payloadSize;
  73. }
  74. void adjust(uint8_t *checksumBuf, size_t count, size_t payloadSize){
  75. mLength = mSize + payloadSize;
  76. fixChecksum(checksumBuf, count);
  77. }
  78. void zeroChecksum(){
  79. mChecksum = 0;
  80. }
  81. string name() const override {
  82. return string("UDP");
  83. }
  84. void streamFields(std::ostream &out) const override {
  85. out << "Source port: " << mSourcePort.to_ulong() << ", "
  86. << "Destination port: " << mDestPort.to_ulong() << ", "
  87. << "Length: " << mLength.to_ulong() << ", "
  88. << "Checksum: " << mChecksum.to_ulong() << "\n";
  89. }
  90. private:
  91. void fixChecksum(uint8_t *checksumBuf, size_t count){
  92. mChecksum = 0;
  93. mChecksum = computeChecksum(reinterpret_cast<uint16_t*>(checksumBuf), count);
  94. }
  95. };
  96. #endif //NETWORK_TRAFFIC_UDPHEADER_H