-
Notifications
You must be signed in to change notification settings - Fork 4
/
icmpecho.cpp
117 lines (80 loc) · 2.36 KB
/
icmpecho.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
//icmpecho.cpp
//-------------------------------------------------------------------------------------------------------
// IcmpEcho.cpp : implementation file
//
#include "stdafx.h"
#include "IcmpEcho.h"
extern "C" {
#include "ipexport.h"
#include "icmpapi.h"
}
#define PING_TIMEOUT 100
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CIcmpEcho
CIcmpEcho::CIcmpEcho()
{
}
CIcmpEcho::~CIcmpEcho()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CIcmpEcho, CSocket)
//{{AFX_MSG_MAP(CIcmpEcho)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CIcmpEcho member functions
unsigned long CIcmpEcho::ResolveIP(CString strIP)
{
//Task 1: Given IP Address i.e. "111.111.111.111",
// Return Network byte ordered address (ulIP)
unsigned long ulIP;
ulIP =(IPAddr)inet_addr(strIP);
return ulIP;
}
unsigned long CIcmpEcho::ResolveName(CString strHostName)
{
//Task 1: Resolve HostName (through DNS or WINS, whichever appropriate)
//Task 2: Return network byte ordered address (ulIP)
unsigned long ulIP;
hostent* phostent;
phostent = gethostbyname(strHostName);
if (phostent == NULL)
return 0;
ulIP = *(DWORD*)(*phostent->h_addr_list);
return ulIP;
}
DWORD CIcmpEcho::PingHost(unsigned long ulIP, int iPingTimeout)
{
//Task 1: Open ICMP Handle
//Task 2: Create Structure to receive ping reply
//Task 3: SendPing (SendEcho)
//Task 4: Close ICMP Handle
//Task 5: Return RoundTripTime
unsigned long ip = ulIP;
HANDLE icmphandle = IcmpCreateFile();
char reply[sizeof(icmp_echo_reply)+8];
icmp_echo_reply* iep=(icmp_echo_reply*)&reply;
iep->RoundTripTime = 0xffffffff;
IcmpSendEcho(icmphandle,ip,0,0,NULL,reply,sizeof(icmp_echo_reply)+8,iPingTimeout);
IcmpCloseHandle(icmphandle);
return iep->RoundTripTime;
}
CString CIcmpEcho::GetIP(unsigned long ulIP)
{
//Task 1: Given a host order ulIP Address
// Return a IP address in format of xxx.xxx.xxx.xxx
LPSTR szAddr;
struct in_addr inetAddr;
inetAddr.s_addr = (IPAddr)ulIP;
szAddr = inet_ntoa(inetAddr);
CString csIP = szAddr;
return csIP;
}