main.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include <iostream>
  30. #include <string>
  31. #include <cstring>
  32. #include "UlsoPacket.h"
  33. using std::cout;
  34. using std::endl;
  35. using std::string;
  36. static uint8_t buf[UlsoPacket<UdpHeader>::maxSize];
  37. static uint8_t qmapIPv4UdpPacketNoSeg[] = {
  38. //C=0,Next Header=1,PAD=b'63,MUXID=0,Packet length=30
  39. 0X7f, 0x00, 0x00, 0x1e,//QMAP header
  40. //type=3,Next Header=0,IPIDCfg=1,Zero Checksum=1,Additional header size=0,segment size=2
  41. 0X06, 0xc0, 0x00, 0x02,//QMAP header extension
  42. //IPv4 header
  43. 0x45, 0x00, 0x00, 0x1e,//version=4,IHL=5,DSCP=0,ECN=0,length=30
  44. 0x00, 0x00, 0x00, 0x00,//id=0,flags=0,fragment offset=0
  45. 0xff, 0x11, 0x00, 0x00,//TTL=255,Protocol=17 (TCP),checksum=
  46. 0xc0, 0xa8, 0x02, 0x13,//IPv4 SRC Addr 192.168.2.19
  47. 0xc0, 0xa8, 0x02, 0x68,//IPv4 DST Addr 192.168.2.104
  48. //UDP header
  49. 0x04, 0x57, 0x08, 0xae,//source port=1111, destination port=2222
  50. 0x00, 0x0a, 0x00, 0x00,//length=10,checksum=
  51. //payload
  52. 0x00, 0x01
  53. };
  54. static uint8_t qmapIPv4UdpPacket[] = {
  55. //C=0,Next Header=1,PAD=b'63,MUXID=0,Packet length=30
  56. 0X7f, 0x00, 0x00, 0x1e,//QMAP header
  57. //type=3,Next Header=0,IPIDCfg=1,Zero Checksum=1,Additional header size=0,segment size=1
  58. 0X06, 0xc0, 0x00, 0x01,//QMAP header extension
  59. //IPv4 header
  60. 0x45, 0x00, 0x00, 0x1e,//version=4,IHL=5,DSCP=0,ECN=0,length=30
  61. 0x00, 0x00, 0x00, 0x00,//id=0,flags=0,fragment offset=0
  62. 0xff, 0x11, 0x00, 0x00,//TTL=255,Protocol=17 (TCP),checksum=
  63. 0xc0, 0xa8, 0x02, 0x13,//IPv4 SRC Addr 192.168.2.19
  64. 0xc0, 0xa8, 0x02, 0x68,//IPv4 DST Addr 192.168.2.104
  65. //UDP header
  66. 0x04, 0x57, 0x08, 0xae,//source port=1111, destination port=2222
  67. 0x00, 0x0a, 0x00, 0x00,//length=10,checksum=
  68. //payload
  69. 0x00, 0x01
  70. };
  71. static uint8_t qmapIPv4UdpPacket2[] = {
  72. //C=0,Next Header=1,PAD=b'63,MUXID=0,Packet length=30
  73. 0X7f, 0x00, 0x00, 0x20,//QMAP header
  74. //type=3,Next Header=0,IPIDCfg=0,Zero Checksum=1,Additional header size=0,segment size=1
  75. 0X06, 0x00, 0x00, 0x01,//QMAP header extension
  76. //IPv4 header
  77. 0x45, 0x00, 0x00, 0x20,//version=4,IHL=5,DSCP=0,ECN=0,length=30
  78. 0x00, 0x00, 0x00, 0x00,//id=0,flags=0,fragment offset=0
  79. 0xff, 0x11, 0x00, 0x00,//TTL=255,Protocol=17 (TCP),checksum=
  80. 0xc0, 0xa8, 0x02, 0x13,//IPv4 SRC Addr 192.168.2.19
  81. 0xc0, 0xa8, 0x02, 0x68,//IPv4 DST Addr 192.168.2.104
  82. //UDP header
  83. 0x04, 0x57, 0x08, 0xae,//source port=1111, destination port=2222
  84. 0x00, 0x0c, 0x00, 0x00,//length=10,checksum=
  85. //payload
  86. 0x00, 0x01, 0x02, 0x03
  87. };
  88. char sep[] = "---------------------------------------------"
  89. "---------------------------------------------"
  90. "---------------------------------------------"
  91. "---------------------------------------------"
  92. "---------------------------------------------";
  93. void printDemoHeadline(const string& name) {
  94. string full = "###################################################"
  95. "###################################################";
  96. string side = "############################";
  97. string space = " ";
  98. cout << full << endl << side << space << "Demo " + name << space
  99. << side << endl << full << endl;
  100. }
  101. void demo(UlsoPacket<>& ulsoPacket, const string& name) {
  102. printDemoHeadline(name);
  103. cout << ulsoPacket << endl;
  104. cout << sep << endl;
  105. vector<UlsoPacket<>> packets = ulsoPacket.segment();
  106. std::for_each(packets.begin(), packets.end(), [](const auto& p){cout << p << endl;
  107. cout << sep << endl;});
  108. }
  109. void testIpv4Tcp(){
  110. using L2Type = IPv4Header;
  111. using L3Type = TcpHeader;
  112. using PacketType = UlsoPacket<L3Type, L2Type>;
  113. size_t payloadSize = 91, segmentSize = 32;
  114. size_t packetSize = QmapHeader::mSize + L2Type::mSize + L3Type::mSize + payloadSize;
  115. PacketType p1(segmentSize, payloadSize);
  116. cout << p1 << endl;
  117. uint8_t ipv4HeaderBuf[packetSize];
  118. p1.asArray(ipv4HeaderBuf);
  119. uint8_t *ipv4HeaderPtr = ipv4HeaderBuf;
  120. uint8_t *goldBuf = QmapIpv4TcpPacket.l2Packet();
  121. for(unsigned int i=0; i<QmapIpv4TcpPacket.l2PacketSize(); i++){
  122. if(ipv4HeaderPtr[i] != goldBuf[i]){
  123. cout << "ipv4HeaderBuf[" << i << "]=" << std::hex << std::setfill('0') << std::setw(2) <<
  124. static_cast<int>(ipv4HeaderPtr[i]) << std::dec << ", goldBuf[" << i << "]=" << std::setfill('0') <<
  125. std::setw(2) << std::hex << static_cast<int>(goldBuf[i]) << std::dec << endl;
  126. }
  127. }
  128. }
  129. void testIpv4Udp(){
  130. using L2Type = IPv4Header;
  131. using L3Type = UdpHeader;
  132. using PacketType = UlsoPacket<L3Type, L2Type>;
  133. size_t payloadSize = 80, segmentSize = 32;
  134. size_t packetSize = QmapHeader::mSize + L2Type::mSize + L3Type::mSize + payloadSize;
  135. PacketType p1(segmentSize, payloadSize);
  136. cout << p1 << endl;
  137. uint8_t ipv4UdpHeaderBuf[packetSize];
  138. p1.asArray(ipv4UdpHeaderBuf);
  139. uint8_t *udpHeaderPtr = ipv4UdpHeaderBuf + QmapHeader::mSize + L2Type::mSize;
  140. uint8_t *goldBuf = udpPacket.l3Packet();
  141. for(unsigned int i=0; i<udpPacket.l3PacketSize(); i++){
  142. if(udpHeaderPtr[i] != goldBuf[i]){
  143. cout << "udpPayload[" << i << "]=" << std::hex << std::setfill('0') << std::setw(2) <<
  144. static_cast<int>(udpHeaderPtr[i]) << std::dec << ", goldBuf[" << i << "]=" << std::setfill('0') <<
  145. std::setw(2) << std::hex << static_cast<int>(goldBuf[i]) << std::dec << endl;
  146. }
  147. }
  148. }
  149. template<typename L3Type, typename L2Type>
  150. size_t packetTestOffset(const struct Packet& p){
  151. return QmapHeader::mSize + (p.l2Size == 0) * L2Type::mSize + (p.l3Size == 0) * L3Type::mSize;
  152. }
  153. template<typename L3Type, typename L2Type>
  154. bool testPacketConstruction(struct Packet packet, size_t segmentSize=20){
  155. auto testOffsetFunc = packetTestOffset<L3Type, L2Type>;
  156. using PacketType = UlsoPacket<L3Type, L2Type>;
  157. size_t ipPacketSize = packet.l2PacketSize();
  158. PacketType ulsoPacket(segmentSize, packet.payload(), packet.payloadSize);
  159. if(ulsoPacket.size() - testOffsetFunc(packet) != ipPacketSize){
  160. cout << "Error: ULSO packet size = " << ulsoPacket.size() << " expected size = " << ipPacketSize << endl;
  161. return false;
  162. }
  163. uint8_t ulsoPacketBuf[ulsoPacket.size()];
  164. ulsoPacket.asArray(ulsoPacketBuf);
  165. uint8_t *ulsoCmpBuf = ulsoPacketBuf + testOffsetFunc(packet);
  166. uint8_t *goldBuf = packet.l2Packet();
  167. for(unsigned int i=0; i<ipPacketSize; i++){
  168. if(ulsoCmpBuf[i] != goldBuf[i]){
  169. cout << "afterQmapPtr[" << i << "]=" << std::hex << std::setfill('0') << std::setw(2) <<
  170. static_cast<int>(ulsoCmpBuf[i]) << std::dec << ", goldBuf[" << i << "]=" << std::setfill('0') <<
  171. std::setw(2) << std::hex << static_cast<int>(goldBuf[i]) << std::dec << endl;
  172. }
  173. }
  174. return true;
  175. }
  176. int main() {
  177. uint8_t arr[UlsoPacket<>::maxSize] = {0};
  178. UlsoPacket<UdpHeader, IPv4Header> p(19, 100, false);
  179. p.mQmapHeader.setmIpIdCfg(0);
  180. cout << p << endl;
  181. auto v = p.segment();
  182. for(auto& pSeg: v){
  183. memset(arr, 0, UlsoPacket<>::maxSize);
  184. cout << pSeg << endl;
  185. }
  186. return 0;
  187. }