artifacts_tests.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include "artifacts_tests.h"
  5. using namespace Tins;
  6. /**
  7. * Creates a new artifacts_tests object.
  8. */
  9. artifacts_tests::artifacts_tests() {
  10. correctChecksum = 0;
  11. incorrectChecksum= 0;
  12. checksumIncorrectRatio= 0;
  13. noPayloadCount= 0;
  14. payloadCount= 0;
  15. }
  16. /*
  17. **************************************************************************
  18. Function: tcp_sum_calc()
  19. **************************************************************************
  20. Description:
  21. Calculate TCP checksum
  22. ***************************************************************************
  23. */
  24. typedef unsigned short u16;
  25. typedef unsigned long u32;
  26. u16 tcp_sum_calc(u16 len_tcp, u16 src_addr[],u16 dest_addr[], bool padding, u16 buff[])
  27. {
  28. u16 prot_tcp=6;
  29. u16 padd=0;
  30. u16 word16;
  31. u32 sum;
  32. // Find out if the length of data is even or odd number. If odd,
  33. // add a padding byte = 0 at the end of packet
  34. //if ((padding&1)==1){
  35. if(padding){
  36. padd=1;
  37. buff[len_tcp]=0;
  38. }
  39. //initialize sum to zero
  40. sum=0;
  41. // make 16 bit words out of every two adjacent 8 bit words and
  42. // calculate the sum of all 16 vit words
  43. for (int i=0;i<len_tcp+padd;i=i+2){
  44. word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
  45. sum = sum + (unsigned long)word16;
  46. }
  47. // add the TCP pseudo header which contains:
  48. // the IP source and destinationn addresses,
  49. for (int i=0;i<4;i=i+2){
  50. word16 =((src_addr[i]<<8)&0xFF00)+(src_addr[i+1]&0xFF);
  51. sum=sum+word16;
  52. }
  53. for (int i=0;i<4;i=i+2){
  54. word16 =((dest_addr[i]<<8)&0xFF00)+(dest_addr[i+1]&0xFF);
  55. sum=sum+word16;
  56. }
  57. // the protocol number and the length of the TCP packet
  58. sum = sum + prot_tcp + len_tcp;
  59. // keep only the last 16 bits of the 32 bit calculated sum and add the carries
  60. while (sum>>16)
  61. sum = (sum & 0xFFFF)+(sum >> 16);
  62. // Take the one's complement of sum
  63. sum = ~sum;
  64. return ((unsigned short) sum);
  65. }
  66. void convertIPv4toArray(std::string IP, unsigned short IP_bytes[]){
  67. std::vector<std::string> temp_v;
  68. split_str(IP,'.',temp_v);
  69. IP_bytes[0] = std::stoi(temp_v[0]);
  70. IP_bytes[1] = std::stoi(temp_v[1]);
  71. IP_bytes[2] = std::stoi(temp_v[2]);
  72. IP_bytes[3] = std::stoi(temp_v[3]);
  73. }
  74. /**
  75. * Checks the TCP checksum of a given packet.
  76. * @param tcpPkt The packet to get checked.
  77. */
  78. void artifacts_tests::check_checksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt){
  79. uint16_t checksum = tcpPkt.checksum();
  80. unsigned short calculatedChecsum = 0;
  81. int headerSize = tcpPkt.header_size();
  82. std::vector<uint8_t> bufferArray_8;
  83. try {
  84. bufferArray_8 = tcpPkt.serialize();
  85. } catch (serialization_error) {
  86. std::cout << "Error: Could not serialize TCP packet with sender: " << ipAddressSender << ", receiver: "
  87. << ipAddressReceiver << ", seq: " << tcpPkt.seq() << std::endl;
  88. return;
  89. }
  90. std::vector<unsigned short> bufferArray_16;
  91. for(int i=0; (unsigned)i<bufferArray_8.size();i++){
  92. bufferArray_16.push_back(bufferArray_8[i]);
  93. }
  94. /*for(int i=0; i<bufferArray_8.size();i+=2){
  95. uint8_t temp[2];
  96. temp[0] = bufferArray_8[i];
  97. if(i!=(bufferArray_8.size()-1))
  98. temp[1] = bufferArray_8[i+1];
  99. else
  100. temp[1] = 0;
  101. unsigned short n;
  102. memcpy(&n, temp, sizeof(unsigned short));
  103. bufferArray_16.push_back(n);
  104. } */
  105. unsigned short* buff_16 = &bufferArray_16[0];
  106. unsigned short ipAddressSender_bytes[4];
  107. unsigned short ipAddressReceiver_bytes[4];
  108. convertIPv4toArray(ipAddressSender, ipAddressSender_bytes);
  109. convertIPv4toArray(ipAddressReceiver, ipAddressReceiver_bytes);
  110. //tcp_sum_calc(unsigned short len_tcp, unsigned short src_addr[],unsigned short dest_addr[], bool padding, unsigned short buff[])
  111. bool padding = false;
  112. int dataSize = bufferArray_8.size() - headerSize; // TO-DO: why don't you use pkt.size()
  113. if(dataSize != 0)
  114. if(dataSize % 2 != 0)
  115. padding = true; // padding if the data size is odd
  116. calculatedChecsum = tcp_sum_calc(bufferArray_8.size(), ipAddressSender_bytes, ipAddressReceiver_bytes, padding, buff_16);
  117. if(calculatedChecsum == checksum)
  118. correctChecksum++;
  119. else{
  120. std::cout<<"Sender:"<<ipAddressSender<<", Receiver:"<<ipAddressReceiver<<"\n";
  121. std::cout<<"Packet checksum:"<<checksum<<"\n";
  122. std::cout<<"Calculated checksum:"<<calculatedChecsum<<"\n";
  123. incorrectChecksum++;
  124. }
  125. }
  126. /**
  127. * Gets the ratio of incorrect TCP checksums to total number of TCP packets.
  128. */
  129. float artifacts_tests::get_checksum_incorrect_ratio(){
  130. int totalPktsNum = incorrectChecksum+correctChecksum;
  131. float ratio = 0;
  132. if(totalPktsNum!=0)
  133. ratio = (float)incorrectChecksum/totalPktsNum;
  134. std::cout<<"Incorrect checksums: "<<incorrectChecksum<<"\n";
  135. std::cout<<"Total TCP packets: "<<totalPktsNum<<"\n";
  136. std::cout<<"get_checksum_incorrect_ratio: "<<ratio<<"\n";
  137. return ratio;
  138. }
  139. void artifacts_tests::check_payload(const PDU *pkt){
  140. int pktSize = pkt->size();
  141. int headerSize = pkt->header_size();
  142. int payloadSize = pktSize - headerSize;
  143. if(payloadSize>0)
  144. payloadCount++;
  145. else
  146. noPayloadCount++;
  147. }
  148. /**
  149. * Gets the ratio of packets that have payload to total number of packets.
  150. */
  151. float artifacts_tests::get_payload_ratio(){
  152. int totalPktsNum = noPayloadCount+payloadCount;
  153. float ratio = 0;
  154. if(totalPktsNum!=0)
  155. ratio = (float)payloadCount/totalPktsNum;
  156. std::cout<<"Payload packets: "<<payloadCount<<"\n";
  157. std::cout<<"Total packets: "<<totalPktsNum<<"\n";
  158. std::cout<<"get_payload_ratio: "<<ratio<<"\n";
  159. return ratio;
  160. }
  161. void artifacts_tests::check_tos(uint8_t ToS){
  162. //if((unsigned)ToS != 0)
  163. // std::cout<<"ToS: "<<(unsigned)ToS<<"\n";
  164. }