Ping.cpp
上传用户:yfy060102
上传日期:2021-05-22
资源大小:60k
文件大小:4k
- #include "stdafx.h"
- #include "winsock2.h"
- typedef struct IPINFO
- {
- unsigned char Ttl; // Time To Live
- unsigned char Tos; // Type Of Service
- unsigned char IPFlags; // IP flags
- unsigned char OptSize; // Size of options data
- unsigned char FAR *Options; // Options data buffer
- }IPINFO;
- typedef IPINFO* PIPINFO;
- typedef struct ICMPECHO
- {
- unsigned long Source; // Source address
- unsigned long Status; // IP status
- unsigned long RTTime; // Round trip time in milliseconds
- unsigned short DataSize; // Reply data size
- unsigned short Reserved; // Unknown
- void FAR *pData; // Reply data buffer
- IPINFO ipInfo; // Reply options
- }ICMPECHO;
- typedef ICMPECHO* PICMPECHO;
- // ICMP.DLL Export Function Pointers
- HANDLE (WINAPI *pIcmpCreateFile)(VOID);
- BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
- DWORD (WINAPI *pIcmpSendEcho)
- (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);
- HANDLE hndlIcmp; // LoadLibrary() handle to ICMP.DLL
- BOOL InitPing()
- {
- WSADATA wsaData; // WSADATA
- // Dynamically load the ICMP.DLL
- hndlIcmp = LoadLibrary("ICMP.DLL");
- if (hndlIcmp == NULL)
- {
- AfxMessageBox("Could not load ICMP.DLL!");
- return FALSE;
- }
- // Retrieve ICMP function pointers
- pIcmpCreateFile = (HANDLE (WINAPI *)(void))
- GetProcAddress((HMODULE)hndlIcmp,"IcmpCreateFile");
- pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
- GetProcAddress((HMODULE)hndlIcmp,"IcmpCloseHandle");
- pIcmpSendEcho = (DWORD (WINAPI *)
- (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
- GetProcAddress((HMODULE)hndlIcmp,"IcmpSendEcho");
- // Check all the function pointers
- if (pIcmpCreateFile == NULL ||
- pIcmpCloseHandle == NULL ||
- pIcmpSendEcho == NULL)
- {
- AfxMessageBox("Error loading ICMP.DLL!");
- FreeLibrary((HMODULE)hndlIcmp);
- return FALSE;
- }
- // Init WinSock
- BOOL nRet = WSAStartup(0x0101, &wsaData);
- if (nRet)
- {
- AfxMessageBox("WSAStartup() error:");
- WSACleanup();
- FreeLibrary((HMODULE)hndlIcmp);
- return FALSE;
- }
- // Check WinSock version
- if (0x0101 != wsaData.wVersion)
- {
- AfxMessageBox("No WinSock version 1.1 support found!");
- WSACleanup();
- FreeLibrary((HMODULE)hndlIcmp);
- return FALSE;
- }
- return TRUE;
- }
- BOOL Ping(char* sHost)
- {
- struct in_addr iaDest; // Internet address structure
- DWORD dwAddress; // IP Address
- IPINFO ipInfo; // IP Options structure
- ICMPECHO icmpEcho; // ICMP Echo reply buffer
- HANDLE hndlFile; // Handle for IcmpCreateFile()
- // Lookup destination
- // Use inet_addr() to determine if we're dealing with a name
- // or an address
- iaDest.s_addr = inet_addr(sHost);
- dwAddress = iaDest.s_addr;
- // Get an ICMP echo request handle
- hndlFile = pIcmpCreateFile();
- // Set some reasonable default values
- ipInfo.Ttl = 255;
- ipInfo.Tos = 0;
- ipInfo.IPFlags = 0;
- ipInfo.OptSize = 0;
- ipInfo.Options = NULL;
- icmpEcho.Status = 0;
- // Reqest an ICMP echo
- pIcmpSendEcho(
- hndlFile, // Handle from IcmpCreateFile()
- dwAddress, // Destination IP address
- NULL, // Pointer to buffer to send
- 0, // Size of buffer in bytes
- &ipInfo, // Request options
- &icmpEcho, // Reply buffer
- sizeof(ICMPECHO),
- 1000); // Time to wait in milliseconds
- // Print the results
- iaDest.s_addr = icmpEcho.Source;
- if (icmpEcho.Status)
- {
- return FALSE;
- }
- // Close the echo request file handle
- pIcmpCloseHandle(hndlFile);
- return TRUE;
- }
- void ReleasePing()
- {
- WSACleanup();
- FreeLibrary((HMODULE)hndlIcmp);
- }