TNB Library
TnbIcmp.h
[詳解]
1#pragma once
15#include "TnbException.h"
16
17
18
19//T-TestCaseコードカバレッジDisable
20#pragma comment(user,"T-Coverage Disable")
21
22
23
24#ifndef _TnbDOXYGEN //Document作成用シンボル
25
26#ifndef _TnbWINSOCK2_ENABLE
27 #include <Winsock.h>
28 #define SD_BOTH 0x02
29 #ifndef _WIN32_WCE
30 #pragma comment(lib,"Wsock32.lib")
31 #else
32 #pragma comment(lib,"Winsock.lib")
33 #endif
34#else
35 #pragma message("message : Winsock2.0 を使用します。")
36 #include <Winsock2.h>
37 #define _WSPIAPI_COUNTOF
38 #include <Ws2Tcpip.h>
39 #ifndef _WIN32_WCE
40 #pragma comment(lib,"WS2_32.lib")
41 #else
42 #pragma comment(lib,"WS2.lib")
43 #endif
44 #include <mswsock.h>
45#endif
46
47#ifndef _WIN32_WCE
48 #define IPAddr ULONG // An IP address.
49 #include <IPEXPORT.H>
50 #if 0
51 struct IP_OPTION_INFORMATION {
52 UCHAR Ttl; // Time To Live
53 UCHAR Tos; // Type Of Service
54 UCHAR Flags; // IP header flags
55 UCHAR OptionsSize; // Size in bytes of options data
56 PUCHAR OptionsData; // Pointer to options data
57 };
58 struct ICMP_ECHO_REPLY {
59 IPAddr Address; // Replying address
60 ULONG Status; // Reply IP_STATUS
61 ULONG RoundTripTime; // RTT in milliseconds
62 USHORT DataSize; // Reply data size in bytes
63 USHORT Reserved; // Reserved for system use
64 PVOID Data; // Pointer to the reply data
65 struct IP_OPTION_INFORMATION Options; // Reply options
66 };
67 #endif
68#else
69 #include <Icmpapi.h>
70#endif
71
72#endif // _TnbDOXYGEN
73
74
75
76//TNB Library
77namespace TNB
78{
79
80
81
94{
95public:
96
98 CIcmpFunction(void) : m_hModule(NULL), m_pCreateFile(NULL), m_pCloseHandle(NULL), m_pSendEcho(NULL)
99 {
100 }
101
104 {
105 if ( m_hModule != NULL )
106 {
107 ::FreeLibrary(m_hModule);
108 m_hModule = NULL;
109 }
110 }
111
117 bool HasModule(void) const
118 {
119 return m_hModule != NULL;
120 }
121
128 bool LoadModule(void)
129 {
130 if ( m_hModule != NULL ){ return true; }
131 #ifndef _WIN32_WCE
132 m_hModule = ::LoadLibraryW(L"ICMP.dll");
133 #else
134 m_hModule = ::LoadLibraryW(L"Iphlpapi.dll");
135 #endif
136 if ( m_hModule != NULL )
137 {
138 m_pCreateFile = reinterpret_cast<P_IcmpCreateFile>(::GetProcAddressA(m_hModule, "IcmpCreateFile"));
139 m_pCloseHandle = reinterpret_cast<P_IcmpCloseHandle>(::GetProcAddressA(m_hModule, "IcmpCloseHandle"));
140 m_pSendEcho = reinterpret_cast<P_IcmpSendEcho>(::GetProcAddressA(m_hModule, "IcmpSendEcho"));
141 }
142 return m_pCreateFile != NULL && m_pCloseHandle != NULL && m_pSendEcho != NULL;
143 }
144
151 HANDLE CreateFile(void)
152 {
153 if ( m_pCreateFile == NULL ){ throw CNullPointerException(); }
154 return m_pCreateFile();
155 }
156
165 bool CloseHandle(HANDLE hHandle)
166 {
167 if ( m_pCloseHandle == NULL ){ throw CNullPointerException(); }
168 return !! m_pCloseHandle(hHandle);
169 }
170
185 DWORD SendEcho(
186 HANDLE icmpHandle,
187 IPAddr destinationAddress,
188 LPVOID pRequestData,
189 WORD requestSize,
190 IP_OPTION_INFORMATION* pRequestOptions,
191 LPVOID pReplyBuffer,
192 DWORD replySize,
193 DWORD timeout )
194 {
195 if ( m_pSendEcho == NULL ){ throw CNullPointerException(); }
196 return m_pSendEcho(
197 icmpHandle,
198 destinationAddress,
199 pRequestData,
200 requestSize,
201 pRequestOptions,
202 pReplyBuffer,
203 replySize,
204 timeout
205 );
206 }
207
208private:
209 typedef HANDLE (WINAPI *P_IcmpCreateFile)(VOID);
210 typedef BOOL (WINAPI *P_IcmpCloseHandle)(HANDLE);
211 typedef DWORD (WINAPI *P_IcmpSendEcho)(HANDLE, IPAddr, LPVOID, WORD, IP_OPTION_INFORMATION*, void*, DWORD, DWORD);
212 HMODULE m_hModule;
213 P_IcmpCreateFile m_pCreateFile;
214 P_IcmpCloseHandle m_pCloseHandle;
215 P_IcmpSendEcho m_pSendEcho;
216};
217
218
219
230{
231public:
232
234 CPing(void) : m_hPing(INVALID_HANDLE_VALUE), m_buf(BUFFER_LEN)
235 {
236 }
237
239 ~CPing(void)
240 {
241 Close();
242 }
243
245 void Close(void)
246 {
247 if ( m_hPing != INVALID_HANDLE_VALUE )
248 {
249 m_icmp.CloseHandle(m_hPing);
250 m_hPing = INVALID_HANDLE_VALUE;
251 }
252 }
253
266 int Echo(IPAddr ipAddr, DWORD timeout = 1000, int tryCount = 5)
267 {
268 int r = m_Open();
269 if ( r < 0 )
270 {
271 return r;
272 }
273 int c = 0;
274 loop ( i, tryCount )
275 {
276 if ( m_SendEcho(ipAddr, timeout) )
277 {
278 c++;
279 }
280 }
281 return c;
282 }
283
284private:
285 enum
286 {
287 BUFFER_LEN = sizeof(ICMP_ECHO_REPLY) + sizeof(DWORD)
288 };
290 int m_Open(void)
291 {
292 if ( m_hPing == INVALID_HANDLE_VALUE )
293 {
294 if ( ! m_icmp.LoadModule() )
295 {
296 return -1; //ICMPのハンドルが使用できないエラー。
297 }
298 m_hPing = m_icmp.CreateFile();
299 if ( m_hPing == INVALID_HANDLE_VALUE )
300 {
301 return -2; //ICMPモジュールが見つからないエラー
302 }
303 }
304 return 0; //成功
305 }
314 bool m_SendEcho(IPAddr ipAddr, DWORD timeout)
315 {
316 DWORD dummy;
317 DWORD dwRetVal = m_icmp.SendEcho(
318 m_hPing, ipAddr, &dummy, sizeof(DWORD),
319 NULL, m_buf.Ref(), BUFFER_LEN, timeout
320 );
321 return dwRetVal != 0;
322 }
323 CIcmpFunction m_icmp;
324 HANDLE m_hPing;
325 CWorkMemT<char> m_buf;
326};
327
328
329
330}; //TNB
331
332
333
334//T-TestCaseコードカバレッジEnable
335#pragma comment(user,"T-Coverage Enable")
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
例外状態管理関係のヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
ICMP API管理クラス
Definition: TnbIcmp.h:94
CIcmpFunction(void)
コンストラクタ
Definition: TnbIcmp.h:98
bool LoadModule(void)
[確認] モジュールロード.
Definition: TnbIcmp.h:128
HANDLE CreateFile(void)
[実行] IcmpCreateFileの実行
Definition: TnbIcmp.h:151
DWORD SendEcho(HANDLE icmpHandle, IPAddr destinationAddress, LPVOID pRequestData, WORD requestSize, IP_OPTION_INFORMATION *pRequestOptions, LPVOID pReplyBuffer, DWORD replySize, DWORD timeout)
[実行] IcmpSendEchoの実行
Definition: TnbIcmp.h:185
~CIcmpFunction(void)
デストラクタ
Definition: TnbIcmp.h:103
bool CloseHandle(HANDLE hHandle)
[実行] IcmpCloseHandleの実行
Definition: TnbIcmp.h:165
bool HasModule(void) const
[確認] ロード済み確認
Definition: TnbIcmp.h:117
NULLポインタ例外
Definition: TnbException.h:172
PING処理クラス
Definition: TnbIcmp.h:230
CPing(void)
コンストラクタ
Definition: TnbIcmp.h:234
void Close(void)
[操作] ハンドルクローズ
Definition: TnbIcmp.h:245
~CPing(void)
デストラクタ
Definition: TnbIcmp.h:239
int Echo(IPAddr ipAddr, DWORD timeout=1000, int tryCount=5)
[実行] ECHO送信
Definition: TnbIcmp.h:266
const TYP * Ref(void) const
[取得] ポインタ取得
Definition: TnbDef.h:712
TNB Library
Definition: TnbDoxyTitle.txt:2