123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- #include "TestsUtils.h"
- #include "IPv4Packet.h"
- #include "memory.h"
- using namespace IPA;
- static const unsigned char TCP_IP_PACKET_DUMP[] = { 0x45,
- 0x00,
- 0x00, 0x28,
- 0x11, 0xc2,
- 0x40, 0x00,
- 0x80,
- 0x06,
- 0x70, 0x3a,
- 0x0a, 0x05, 0x07, 0x46,
- 0x81, 0x2e, 0xe6, 0x5a,
- 0xf3, 0xa2,
- 0x01, 0xbd,
- 0x26, 0x26, 0x1d, 0x7d,
- 0x15, 0xaa, 0xbc, 0xdb,
- 0x50, 0x10, 0x80, 0xd4,
- 0xaa, 0xa3,
- 0x00, 0x00
- };
- static const unsigned char UDP_IP_PACKET_DUMP[] = {
- 0x45,
- 0x00,
- 0x00,
- 0x34,
- 0x12,
- 0xa2,
- 0x00,
- 0x00,
- 0x80,
- 0x11,
- 0xe4,
- 0x92,
- 0x0a, 0x05, 0x07,
- 0x46,
- 0x0a, 0x2b, 0x28,
- 0x0f,
- 0x03,
- 0x4a,
- 0x1b,
- 0x4f,
- 0x00,
- 0x20,
- 0x36,
- 0xac,
- 0x00, 0x05, 0x20,
- 0x6d,
- 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x01, 0x13, 0x05, 0x20, 0x6c };
- static unsigned char ICMP_IP_PACKET_DUMP[] = {
-
- 0x45, 0x00, 0x00, 0xdc, 0x03, 0xfe, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00,
- 0x81, 0x2e, 0xe4, 0xf6, 0x81, 0x2e, 0xe6, 0xd4,
-
- 0x00, 0x00, 0xa9, 0xcd, 0x28, 0xa3, 0x01, 0x00,
-
- 0xee, 0x7c, 0xf7, 0x90, 0x39, 0x06, 0xd4, 0x41, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
- 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51 };
- static void LittleToBigEndianUint32(unsigned char *pBigEndianBuffer,
- unsigned int nUint32LittleEndianValue) {
- unsigned char nLsb = nUint32LittleEndianValue & 0xff;
- unsigned char nLsbMsbLow = (nUint32LittleEndianValue >> 8) & 0xff;
- unsigned char nLsbMsbHigh = (nUint32LittleEndianValue >> 16) & 0xff;
- unsigned char nMsb = (nUint32LittleEndianValue >> 24) & 0xff;
- pBigEndianBuffer[0] = nMsb;
- pBigEndianBuffer[1] = nLsbMsbHigh;
- pBigEndianBuffer[2] = nLsbMsbLow;
- pBigEndianBuffer[3] = nLsb;
- }
- static unsigned short BigToLittleEndianUint16(unsigned char *pBigEndianStart) {
- unsigned char nMsb = pBigEndianStart[0];
- unsigned char nLsb = pBigEndianStart[1];
- return (nMsb << 8 | nLsb << 0);
- }
- static unsigned int BigToLittleEndianUint32(unsigned char *pBigEndianStart) {
- unsigned char nMsb = pBigEndianStart[0];
- unsigned char nMsbLsbHigh = pBigEndianStart[1];
- unsigned char nMsbLsbLow = pBigEndianStart[2];
- unsigned char nLsb = pBigEndianStart[3];
- return (nMsb << 24 | nMsbLsbHigh << 16 | nMsbLsbLow << 8 | nLsb << 0);
- }
- static void LittleToBigEndianUint16(unsigned char *pBigEndianBuffer,
- unsigned int nUint16LittleEndianValue) {
- unsigned char nLsb = nUint16LittleEndianValue & 0xff;
- unsigned char nMsb = (nUint16LittleEndianValue >> 8) & 0xff;
- pBigEndianBuffer[0] = nMsb;
- pBigEndianBuffer[1] = nLsb;
- }
- static unsigned short Get2BBIGEndian(const unsigned char *pBuff, int offset) {
- unsigned char upperByte = 0;
- unsigned char lowerByte = 0;
- memcpy(&upperByte, pBuff + offset, 1);
- memcpy(&lowerByte, pBuff + offset + 1, 1);
- return (upperByte << 8 | lowerByte);
- }
- IPv4Packet::IPv4Packet(unsigned int size) :
- m_PacketSize(size) {
- }
- IPv4Packet::~IPv4Packet(void) {
- if (0 != m_Packet) {
- delete[] m_Packet;
- m_Packet = 0;
- }
- }
- void IPv4Packet::ToNetworkByteStream(unsigned char *buffer) {
- if (0 == buffer) {
- LOG_MSG_ERROR("IPv4Packet::ToNetworkByteStream : NULL arguments");
- return;
- }
- memcpy(buffer, m_Packet, GetSize());
- }
- unsigned int IPv4Packet::GetSrcAddr(void) {
- return BigToLittleEndianUint32(m_Packet + 12);
- }
- void IPv4Packet::SetSrcAddr(unsigned int addr) {
- LittleToBigEndianUint32(m_Packet + 12, addr);
- RecalculateChecksum();
- }
- unsigned int IPv4Packet::GetDstAddr(void) {
- return BigToLittleEndianUint32(m_Packet + 16);
- }
- void IPv4Packet::SetDstAddr(unsigned int addr) {
- LittleToBigEndianUint32(m_Packet + 16, addr);
- RecalculateChecksum();
- }
- unsigned char IPv4Packet::GetProtocol(void) {
- unsigned char retVal = 0;
- memcpy(&retVal, m_Packet + 9, sizeof(unsigned char));
- return retVal;
- }
- unsigned short IPv4Packet::GetSrcPort(void) {
- return BigToLittleEndianUint16(m_Packet + 20);
- }
- unsigned short IPv4Packet::GetDstPort(void) {
- return BigToLittleEndianUint16(m_Packet + 22);
- }
- void IPv4Packet::SetDstPort(unsigned short port) {
- LittleToBigEndianUint16(m_Packet + 22, port);
- RecalculateChecksum();
- }
- void IPv4Packet::SetSrcPort(unsigned short port) {
- LittleToBigEndianUint16(m_Packet + 20, port);
- RecalculateChecksum();
- }
- void IPv4Packet::SetMF(bool bValue) {
- Byte * pFlags = m_Packet + IPV4_FLAGS_BYTE_OFFSET;
-
- if (true == bValue) {
- *pFlags |= (0x20);
- } else {
- *pFlags &= (~0x20);
- }
- }
- void IPv4Packet::RecalculateChecksum(void) {
- RecalculateIPChecksum();
- RecalculateTCPChecksum();
- RecalculateUDPChecksum();
- }
- void IPv4Packet::RecalculateIPChecksum(void) {
- unsigned short pUint16[100];
- int headerLen = (m_Packet[0] & 0x0F) * 2;
- int checksum = 0;
- unsigned short result = 0;
- memset(&pUint16, 0, 100 * sizeof(unsigned short));
-
- memset(m_Packet + 10, 0, sizeof(unsigned short));
- memcpy(&pUint16, m_Packet, headerLen * sizeof(unsigned short));
- for (int i = 0; i < headerLen; i++) {
- checksum += pUint16[i];
- checksum = (checksum & 0xFFFF) + (checksum >> 16);
- }
- result = (~checksum & 0xFFFF);
- memcpy(m_Packet + 10, &result, sizeof(unsigned short));
- return;
- }
- void TCPPacket::RecalculateTCPChecksum(void) {
- unsigned short *pUint16 = new unsigned short[100];
- int checksum = 0;
- int headerLen = 0;
- unsigned short *pTemp = 0;
- unsigned short result = 0;
- headerLen = Get2BBIGEndian(m_Packet, 2) - (m_Packet[0] & 0x0F) * 4;
- memset(pUint16, 0, 100);
-
- memset(m_Packet + 36, 0, sizeof(unsigned short));
- memcpy(pUint16, m_Packet, headerLen * sizeof(unsigned short));
- pTemp = pUint16;
-
- pUint16 += 6;
- for (int i = 0; i < 4; i++) {
- checksum += pUint16[i];
- checksum = (checksum & 0xFFFF) + (checksum >> 16);
- }
- checksum += 0x0600;
- checksum += Get2BBIGEndian((unsigned char*) &headerLen, 0);
- pUint16 = pTemp + (m_Packet[0] & 0x0F) * 2;
- headerLen /= 2;
- for (int i = 0; i < headerLen; i++) {
- checksum += pUint16[i];
- checksum = (checksum & 0xFFFF) + (checksum >> 16);
- }
- result = (~checksum & 0xFFFF);
- memcpy(m_Packet + 36, &result, sizeof(unsigned short));
- delete[] pTemp;
- return;
- }
- void UDPPacket::RecalculateUDPChecksum(void) {
- unsigned short *pUint16 = new unsigned short[100];
- int checksum = 0;
- int headerLen = 0;
- unsigned short *pTemp = 0;
- unsigned short result = 0;
- headerLen = Get2BBIGEndian(m_Packet, (m_Packet[0] & 0x0F) * 4 + 4);
- memset(pUint16, 0, 100);
-
- memset(m_Packet + 26, 0, sizeof(unsigned short));
- memcpy(pUint16, m_Packet, headerLen * sizeof(unsigned short));
- pTemp = pUint16;
-
- pUint16 += 6;
- for (int i = 0; i < 4; i++) {
- checksum += pUint16[i];
- checksum = (checksum & 0xFFFF) + (checksum >> 16);
- }
- checksum += 0x1100;
- checksum += Get2BBIGEndian((unsigned char*) &headerLen, 0);
- pUint16 = pTemp + (m_Packet[0] & 0x0F) * 2;
- headerLen /= 2;
- for (int i = 0; i < headerLen; i++) {
- checksum += pUint16[i];
- checksum = (checksum & 0xFFFF) + (checksum >> 16);
- }
- result = (~checksum & 0xFFFF);
- memcpy(m_Packet + 26, &result, sizeof(unsigned short));
- delete[] pTemp;
- return;
- }
- TCPPacket::TCPPacket(void) :
- IPv4Packet(sizeof(TCP_IP_PACKET_DUMP)) {
- size_t length = GetSize();
- m_Packet = new unsigned char[length];
- if (0 == m_Packet) {
- LOG_MSG_ERROR("TCPPacket : packet allocation failed");
- return;
- }
- memcpy(m_Packet, TCP_IP_PACKET_DUMP, length);
- }
- UDPPacket::UDPPacket(void) :
- IPv4Packet(sizeof(UDP_IP_PACKET_DUMP)) {
- size_t length = GetSize();
- m_Packet = new unsigned char[length];
- if (0 == m_Packet) {
- LOG_MSG_ERROR("UDPPacket : packet allocation failed");
- return;
- }
- memcpy(m_Packet, UDP_IP_PACKET_DUMP, length);
- }
- ICMPPacket::ICMPPacket(void) :
- IPv4Packet(sizeof(ICMP_IP_PACKET_DUMP)) {
- size_t length = GetSize();
- m_Packet = new unsigned char[length];
- if (0 == m_Packet) {
- LOG_MSG_ERROR("ICMPPacket : packet allocation failed");
- return;
- }
- memcpy(m_Packet, ICMP_IP_PACKET_DUMP, length);
- }
- unsigned short ICMPPacket::GetSrcPort(void) {
- return 0;
- }
- unsigned short ICMPPacket::GetDstPort(void) {
- return 0;
- }
- void ICMPPacket::SetDstPort(unsigned short port) {
- (void) port;
- return;
- }
- void ICMPPacket::SetSrcPort(unsigned short port) {
- (void) port;
- return;
- }
|