-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIcmpPacket.cpp
74 lines (58 loc) · 1.81 KB
/
IcmpPacket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// IcmpPacket.cpp: implementation of the CIcmpPacket class.
//
//////////////////////////////////////////////////////////////////////
#include "IcmpPacket.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CIcmpPacket::CIcmpPacket(BYTE cbIcmpType,
PBYTE pIcmpData,
int nBytes)
{
m_pPacket = new BYTE[nBytes + sizeof(ICMPHEADER)];
PICMPHEADER IcmpHeader = (PICMPHEADER)m_pPacket;
IcmpHeader->Type = cbIcmpType;
IcmpHeader->Code = 0;
IcmpHeader->CheckSum = 0;
IcmpHeader->Id =(USHORT) GetCurrentProcessId();
IcmpHeader->Seq = 0;
if (pIcmpData == NULL)
nBytes = 0;
if (nBytes != 0)
{
PBYTE pUserData = m_pPacket + sizeof(ICMPHEADER);
CopyMemory(pUserData, pIcmpData, nBytes);
}
dwSize = sizeof(ICMPHEADER) + nBytes;
IcmpHeader->CheckSum = Checksum((PWORD)m_pPacket, sizeof(ICMPHEADER) + nBytes);
}
CIcmpPacket::~CIcmpPacket()
{
delete[] m_pPacket;
}
WORD CIcmpPacket::Checksum(PWORD pData, int nBytes)
{
DWORD dwSum;
/*
* Our algorithm is simple, using a 32-bit accumulator (sum),
* we add sequential 16-bit words to it, and at the end, fold back
* all the carry bits from the top 16 bits into the lower 16 bits.
*/
dwSum = 0;
while (nBytes > 1)
{
dwSum += *pData++;
nBytes -= sizeof(WORD);
}
/* mop up an odd byte, if necessary */
if (nBytes == 1)
{
dwSum = *(PBYTE)pData; /* one byte only */
}
/*
* Add back carry outs from top 16 bits to low 16 bits.
*/
dwSum = (dwSum >> 16) + (dwSum & 0xffff); /* add high-16 to low-16 */
dwSum += (dwSum >> 16); /* add carry */
return ( (USHORT)(~dwSum) ); /* ones-complement, then truncate to 16 bits */
}