#include #include #include #include "artifacts_tests.h" using namespace Tins; /** * Creates a new artifacts_tests object. */ artifacts_tests::artifacts_tests() { correctChecksum = 0; incorrectChecksum= 0; checksumIncorrectRatio= 0; noPayloadCount= 0; payloadCount= 0; } /* ************************************************************************** Function: tcp_sum_calc() ************************************************************************** Description: Calculate TCP checksum *************************************************************************** */ typedef unsigned short u16; typedef unsigned long u32; 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; // Find out if the length of data is even or odd number. If odd, // add a padding byte = 0 at the end of packet //if ((padding&1)==1){ if(padding){ padd=1; buff[len_tcp]=0; } //initialize sum to zero sum=0; // make 16 bit words out of every two adjacent 8 bit words and // calculate the sum of all 16 vit words for (int i=0;i>16) sum = (sum & 0xFFFF)+(sum >> 16); // Take the one's complement of sum sum = ~sum; return ((unsigned short) sum); } void convertIPv4toArray(std::string IP, unsigned short IP_bytes[]){ std::vector 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]); } /** * Checks the TCP checksum of a given packet. * @param tcpPkt The packet to get checked. */ void artifacts_tests::check_checksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt){ uint16_t checksum = tcpPkt.checksum(); unsigned short calculatedChecsum = 0; int headerSize = tcpPkt.header_size(); std::vector 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; } std::vector bufferArray_16; for(int i=0; (unsigned)isize(); int headerSize = pkt->header_size(); int payloadSize = pktSize - headerSize; if(payloadSize>0) payloadCount++; else noPayloadCount++; } /** * Gets the ratio of packets that have payload to total number of packets. */ float artifacts_tests::get_payload_ratio(){ int totalPktsNum = noPayloadCount+payloadCount; float ratio = 0; if(totalPktsNum!=0) ratio = (float)payloadCount/totalPktsNum; std::cout<<"Payload packets: "<