123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #include "utilities.h"
- using namespace Tins;
- template<class T>
- std::string integral_to_binary_string(T byte)
- {
- std::bitset<sizeof(T) * CHAR_BIT> bs(byte);
- return bs.to_string();
- }
- void split_str(const std::string& s, char delim,std::vector<std::string>& v) {
- auto i = 0;
- auto pos = s.find(delim);
- while (pos != std::string::npos) {
- v.push_back(s.substr(i, pos-i));
- i = ++pos;
- pos = s.find(delim, pos);
- if (pos == std::string::npos)
- v.push_back(s.substr(i, s.length()));
- }
- }
- std::string getIPv4Class(std::string ipAddress){
- std::string ipClass="Unknown";
-
- std::vector<std::string> ipBytes;
- split_str(ipAddress, '.',ipBytes);
-
-
-
- if(ipBytes.size()>1){
- int b1 = std::stoi(ipBytes[0]);
- int b2 = std::stoi(ipBytes[1]);
-
- if(b1 >= 1 && b1 <= 126){
- if(b1 == 10)
- ipClass = "A-private";
- else
- ipClass = "A";
- }
- else if(b1 == 127){
- ipClass = "A-unused";
- }
- else if (b1 >= 128 && b1 <= 191){
- if(b1 == 172 && b2 >= 16 && b2 <= 31)
- ipClass = "B-private";
- else
- ipClass = "B";
- }
- else if (b1 >= 192 && b1 <= 223){
- if(b1 == 192 && b2 == 168)
- ipClass = "C-private";
- else
- ipClass = "C";
- }
- else if (b1 >= 224 && b1 <= 239)
- ipClass = "D";
- else if (b1 >= 240 && b1 <= 254)
- ipClass = "E";
- }
- return ipClass;
- }
- int getClosestIndex(std::vector<std::chrono::microseconds> v, std::chrono::microseconds refElem)
- {
- auto i = std::min_element(begin(v), end(v), [=] (std::chrono::microseconds x, std::chrono::microseconds y)
- {
- return std::abs((x - refElem).count()) < std::abs((y - refElem).count());
- });
- return std::distance(begin(v), i);
- }
- void snifferIteratorIncrement(Tins::SnifferIterator& iterator){
- (((((((((iterator++)++)++)++)++)++)++)++)++)++;
- }
- void convertIPv4toArray(std::string IP, unsigned short IP_bytes[]){
- std::vector<std::string> temp_v;
- split_str(IP,'.',temp_v);
- IP_bytes[0] = std::stoi(temp_v[0]);
- IP_bytes[1] = std::stoi(temp_v[1]);
- IP_bytes[2] = std::stoi(temp_v[2]);
- IP_bytes[3] = std::stoi(temp_v[3]);
- }
- u16 tcp_sum_calc(u16 len_tcp, u16 src_addr[],u16 dest_addr[], bool padding, u16 buff[])
- {
- u16 prot_tcp=6;
- u16 padd=0;
- u16 word16;
- u32 sum;
-
-
-
- if(padding){
- padd=1;
- buff[len_tcp]=0;
- }
-
- sum=0;
-
-
- for (int i=0;i<len_tcp+padd;i=i+2){
- word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
- sum = sum + (unsigned long)word16;
- }
-
-
- for (int i=0;i<4;i=i+2){
- word16 =((src_addr[i]<<8)&0xFF00)+(src_addr[i+1]&0xFF);
- sum=sum+word16;
- }
- for (int i=0;i<4;i=i+2){
- word16 =((dest_addr[i]<<8)&0xFF00)+(dest_addr[i+1]&0xFF);
- sum=sum+word16;
- }
-
- sum = sum + prot_tcp + len_tcp;
-
- while (sum>>16)
- sum = (sum & 0xFFFF)+(sum >> 16);
-
- sum = ~sum;
- return ((unsigned short) sum);
- }
- bool check_tcpChecksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt){
- uint16_t checksum = tcpPkt.checksum();
- unsigned short calculatedChecsum = 0;
- int headerSize = tcpPkt.header_size();
- std::vector<uint8_t> bufferArray_8;
- try {
- bufferArray_8 = tcpPkt.serialize();
- } catch (serialization_error) {
- std::cout << "Error: Could not serialize TCP packet with sender: " << ipAddressSender << ", receiver: "
- << ipAddressReceiver << ", seq: " << tcpPkt.seq() << std::endl;
- return false;
- }
- std::vector<unsigned short> bufferArray_16;
- for(int i=0; (unsigned)i<bufferArray_8.size();i++){
- bufferArray_16.push_back(bufferArray_8[i]);
- }
- unsigned short* buff_16 = &bufferArray_16[0];
- unsigned short ipAddressSender_bytes[4];
- unsigned short ipAddressReceiver_bytes[4];
- convertIPv4toArray(ipAddressSender, ipAddressSender_bytes);
- convertIPv4toArray(ipAddressReceiver, ipAddressReceiver_bytes);
-
- bool padding = false;
- int dataSize = bufferArray_8.size() - headerSize;
- if(dataSize != 0)
- if(dataSize % 2 != 0)
- padding = true;
- calculatedChecsum = tcp_sum_calc(bufferArray_8.size(), ipAddressSender_bytes, ipAddressReceiver_bytes, padding, buff_16);
- return (calculatedChecsum == checksum);
- }
|