-
Notifications
You must be signed in to change notification settings - Fork 1
/
Icmp.cpp
170 lines (128 loc) · 4.84 KB
/
Icmp.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Icmp.cpp: implementation of the CIcmp class.
//
//////////////////////////////////////////////////////////////////////
#include "Icmp.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CIcmp::CIcmp(): m_Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)
{
}
CIcmp::~CIcmp()
{
}
DWORD CIcmp::Send(CIcmpPacket* pIcmpPacket,
IPADDR DestinationAddress,
BYTE Ttl,
BYTE Tos,
BYTE Flags,
DWORD Timeout,
CIcmpReply* pIcmpReply)
{
struct sockaddr_in saDest,saFrom;
int cbWritten;
int bStatus;
DWORD dwFromLen = sizeof(saFrom);
bStatus = setsockopt(m_Socket,IPPROTO_IP,IP_TOS,(char *) &Tos,sizeof(Tos));
if (bStatus == INVALID_SOCKET)
return m_Socket.GetLastError();
setsockopt(m_Socket,IPPROTO_IP,IP_TTL,(char *) &Ttl,sizeof(Ttl));
setsockopt(m_Socket,IPPROTO_IP,IP_DONTFRAGMENT,(char *)&Flags,sizeof(Flags));
// Bisogna settare il timeout del socket altrimenti in caso di mancata ricezione
// rimarrebbe bloccato in eterno
setsockopt(m_Socket,SOL_SOCKET,SO_RCVTIMEO,(char*)&Timeout,sizeof(Timeout));
setsockopt(m_Socket,SOL_SOCKET,SO_SNDTIMEO,(char*)&Timeout,sizeof(Timeout));
saDest.sin_addr.s_addr = DestinationAddress;
saDest.sin_family = AF_INET;
StartTimeTick = GetTickCount();
// Spediamo il pacchetto
cbWritten = sendto(m_Socket,
(const char*)pIcmpPacket->Data(),
pIcmpPacket->Size(), 0,
(struct sockaddr *)&saDest,
sizeof(saDest));
if (cbWritten == SOCKET_ERROR)
return m_Socket.GetLastError();
// Attendiamo risposta fino al timeout
BYTE RecvBuffer[2048];
//TODO: Fix dimension
int cbReceived = recvfrom(m_Socket,
(char *)RecvBuffer, sizeof(RecvBuffer), 0,
(struct sockaddr *)&saFrom, (int*)&dwFromLen);
if (cbReceived == SOCKET_ERROR)
return m_Socket.GetLastError();
return FillResp(RecvBuffer, cbReceived, &saFrom, pIcmpReply);
}
DWORD CIcmp::FillResp(PBYTE pRecvBuffer,
int cbReceived,
struct sockaddr_in* dest,
CIcmpReply* pIcmpReply)
{
PIPHEADER pIPHeader;
PICMPHEADER pIcmpHeader;
DWORD dwIPHeaderLenght;
int nSize;
pIPHeader = (PIPHEADER) pRecvBuffer;
dwIPHeaderLenght= pIPHeader->cbHeaderLen * 4;
pIcmpHeader = (PICMPHEADER) (pRecvBuffer + dwIPHeaderLenght);
pIcmpReply->Address = dest->sin_addr.s_addr;
pIcmpReply->RoundTripTime = GetTickCount() - StartTimeTick;
pIcmpReply->Ttl = pIPHeader->cbTtl;
pIcmpReply->Tos = pIPHeader->cbTOS;
pIcmpReply->Flags = pIPHeader->fNotFragment;
nSize = cbReceived -
dwIPHeaderLenght -
sizeof(ICMPHEADER); //+sizeof(LONG);
pIcmpReply->DataSize = (WORD)nSize;
pIcmpReply->Type = pIcmpHeader->Type;
if ((pIcmpHeader->Type == ICMP_REPLY_ECHO) ||
(pIcmpHeader->Type == ICMP_REPLY_SUBNET)||
(pIcmpHeader->Type == ICMP_REPLY_GETLOCALTIME))
return ERROR_SUCCESS;
// pIcmpHeader->Type == ICMP_ERROR_DEST_UNREACHABLE
// pIcmpHeader->Type == ICMP_ERROR_TIME_EXPIRED
return pIcmpHeader->Code;
}
DWORD CIcmp::SendEcho(IPADDR DestinationAddress,
PBYTE RequestData,
WORD RequestSize,
BYTE Ttl,
BYTE Tos,
BYTE Flags,
DWORD Timeout,
CIcmpReply* pIcmpReply)
{
CIcmpPacket IcmpPacket(ICMP_SEND_ECHO,
RequestData,
RequestSize);
return Send(&IcmpPacket, DestinationAddress,
Ttl, Tos, Flags, Timeout, pIcmpReply);
}
DWORD CIcmp::TraceRoute(IPADDR DestinationAddress,
DWORD Timeout,
BOOL fDNSResolution,
BYTE MaxHopNum,
LPBYTE lpData,
DWORD dwDataSize)
{
assert(dwDataSize >= MaxHopNum * sizeof(CIcmpReply));
assert(lpData);
assert(!IsBadWritePtr(lpData, dwDataSize));
if (dwDataSize < MaxHopNum * sizeof(CIcmpReply) ||
lpData == NULL ||
IsBadWritePtr(lpData, dwDataSize))
return ERROR_INSUFFICIENT_BUFFER;
CIcmpReply** lplpIcmpReply = (CIcmpReply**)lpData;
BYTE i = 1;
BOOL Finished = FALSE;
while(i < MaxHopNum && !Finished)
{
SendEcho(DestinationAddress,
NULL, 0, i, 0, 0,
1000, lplpIcmpReply[i]);
if (lplpIcmpReply[i]->Address == DestinationAddress)
Finished = TRUE;
++i;
}
return ERROR_SUCCESS;
}