utilities.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "utilities.h"
  2. using namespace Tins;
  3. template<class T>
  4. std::string integral_to_binary_string(T byte)
  5. {
  6. std::bitset<sizeof(T) * CHAR_BIT> bs(byte);
  7. return bs.to_string();
  8. }
  9. /**
  10. * Split a string.
  11. * @param str string to be splitted.
  12. * @param delimiter delimiter to use in splitting.
  13. * @return vector of substrings.
  14. */
  15. void split_str(const std::string& s, char delim,std::vector<std::string>& v) {
  16. auto i = 0;
  17. auto pos = s.find(delim);
  18. while (pos != std::string::npos) {
  19. v.push_back(s.substr(i, pos-i));
  20. i = ++pos;
  21. pos = s.find(delim, pos);
  22. if (pos == std::string::npos)
  23. v.push_back(s.substr(i, s.length()));
  24. }
  25. }
  26. /**
  27. * Get the class (A,B,C,D,E) of IP address.
  28. * @param ipAddress IP that we get its class.
  29. */
  30. std::string getIPv4Class(std::string ipAddress){
  31. std::string ipClass="Unknown";
  32. std::vector<std::string> ipBytes;
  33. split_str(ipAddress, '.',ipBytes);
  34. //std::cout<< ipAddress << "\n";
  35. if(ipBytes.size()>1){
  36. int b1 = std::stoi(ipBytes[0]);
  37. int b2 = std::stoi(ipBytes[1]);
  38. if(b1 >= 1 && b1 <= 126){
  39. if(b1 == 10)
  40. ipClass = "A-private";
  41. else
  42. ipClass = "A";
  43. }
  44. else if(b1 == 127){
  45. ipClass = "A-unused"; // cannot be used and is reserved for loopback and diagnostic functions.
  46. }
  47. else if (b1 >= 128 && b1 <= 191){
  48. if(b1 == 172 && b2 >= 16 && b2 <= 31)
  49. ipClass = "B-private";
  50. else
  51. ipClass = "B";
  52. }
  53. else if (b1 >= 192 && b1 <= 223){
  54. if(b1 == 192 && b2 == 168)
  55. ipClass = "C-private";
  56. else
  57. ipClass = "C";
  58. }
  59. else if (b1 >= 224 && b1 <= 239)
  60. ipClass = "D"; // Reserved for Multicasting
  61. else if (b1 >= 240 && b1 <= 254)
  62. ipClass = "E"; // Experimental; used for research
  63. }
  64. return ipClass;
  65. }
  66. /**
  67. * Advance iterator by 10 steps.
  68. * @param iterator to advance.
  69. */
  70. void snifferIteratorIncrement(Tins::SnifferIterator& iterator){
  71. (((((((((iterator++)++)++)++)++)++)++)++)++)++;
  72. }
  73. /**
  74. * Convert IP address from string to array of bytes.
  75. * @param IP to convert.
  76. * @param IP_bytes to be filled and retrieved.
  77. */
  78. void convertIPv4toArray(std::string IP, unsigned short IP_bytes[]){
  79. std::vector<std::string> temp_v;
  80. split_str(IP,'.',temp_v);
  81. IP_bytes[0] = std::stoi(temp_v[0]);
  82. IP_bytes[1] = std::stoi(temp_v[1]);
  83. IP_bytes[2] = std::stoi(temp_v[2]);
  84. IP_bytes[3] = std::stoi(temp_v[3]);
  85. }
  86. /**
  87. * Calculate TCP checksum.
  88. * @param len_tcp TCP packet length
  89. * @param src_addr source IP address
  90. * @param dest_addr destination IP address
  91. * @param padding
  92. * @param buff
  93. * @return checksum.
  94. */
  95. u16 tcp_sum_calc(u16 len_tcp, u16 src_addr[],u16 dest_addr[], bool padding, u16 buff[])
  96. {
  97. u16 prot_tcp=6;
  98. u16 padd=0;
  99. u16 word16;
  100. u32 sum;
  101. // Find out if the length of data is even or odd number. If odd,
  102. // add a padding byte = 0 at the end of packet
  103. //if ((padding&1)==1){
  104. if(padding){
  105. padd=1;
  106. buff[len_tcp]=0;
  107. }
  108. //initialize sum to zero
  109. sum=0;
  110. // make 16 bit words out of every two adjacent 8 bit words and
  111. // calculate the sum of all 16 vit words
  112. for (int i=0;i<len_tcp+padd;i=i+2){
  113. word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
  114. sum = sum + (unsigned long)word16;
  115. }
  116. // add the TCP pseudo header which contains:
  117. // the IP source and destinationn addresses,
  118. for (int i=0;i<4;i=i+2){
  119. word16 =((src_addr[i]<<8)&0xFF00)+(src_addr[i+1]&0xFF);
  120. sum=sum+word16;
  121. }
  122. for (int i=0;i<4;i=i+2){
  123. word16 =((dest_addr[i]<<8)&0xFF00)+(dest_addr[i+1]&0xFF);
  124. sum=sum+word16;
  125. }
  126. // the protocol number and the length of the TCP packet
  127. sum = sum + prot_tcp + len_tcp;
  128. // keep only the last 16 bits of the 32 bit calculated sum and add the carries
  129. while (sum>>16)
  130. sum = (sum & 0xFFFF)+(sum >> 16);
  131. // Take the one's complement of sum
  132. sum = ~sum;
  133. return ((unsigned short) sum);
  134. }
  135. /**
  136. * Checks the TCP checksum of a given packet.
  137. * @param ipAddressSender The source IP.
  138. * @param ipAddressReceiver The destination IP.
  139. * @param tcpPkt The packet to get checked.
  140. */
  141. bool check_tcpChecksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt){
  142. uint16_t checksum = tcpPkt.checksum();
  143. unsigned short calculatedChecsum = 0;
  144. int headerSize = tcpPkt.header_size();
  145. std::vector<uint8_t> bufferArray_8;
  146. try {
  147. bufferArray_8 = tcpPkt.serialize();
  148. } catch (serialization_error) {
  149. std::cout << "Error: Could not serialize TCP packet with sender: " << ipAddressSender << ", receiver: "
  150. << ipAddressReceiver << ", seq: " << tcpPkt.seq() << std::endl;
  151. return false;
  152. }
  153. std::vector<unsigned short> bufferArray_16;
  154. for(int i=0; (unsigned)i<bufferArray_8.size();i++){
  155. bufferArray_16.push_back(bufferArray_8[i]);
  156. }
  157. unsigned short* buff_16 = &bufferArray_16[0];
  158. unsigned short ipAddressSender_bytes[4];
  159. unsigned short ipAddressReceiver_bytes[4];
  160. convertIPv4toArray(ipAddressSender, ipAddressSender_bytes);
  161. convertIPv4toArray(ipAddressReceiver, ipAddressReceiver_bytes);
  162. bool padding = false;
  163. int dataSize = bufferArray_8.size() - headerSize;
  164. if(dataSize != 0)
  165. if(dataSize % 2 != 0)
  166. padding = true; // padding if the data size is odd
  167. calculatedChecsum = tcp_sum_calc(bufferArray_8.size(), ipAddressSender_bytes, ipAddressReceiver_bytes, padding, buff_16);
  168. return (calculatedChecsum == checksum);
  169. }