解析ICMP的ping命令代碼
在ICMP的應(yīng)用中,我們可以使用ping命令來(lái)進(jìn)行操作。本文就是介紹了這部分的應(yīng)用。介紹了具體實(shí)現(xiàn)的代碼。希望對(duì)大家學(xué)習(xí)ICMP有所幫助。
原理簡(jiǎn)介:
這個(gè)例子演示了應(yīng)用微軟的ICMP.DLL怎樣"ping"另一臺(tái)機(jī)器. 這個(gè)DLL是沒(méi)有文檔話的發(fā)送ICMP回送包API接口, 也稱為"pings," 就像潛水員對(duì)聲納信號(hào)的術(shù)語(yǔ)一樣. 這段代碼出自一個(gè)被一個(gè)名叫MarkG的家伙的GUI程序, 他的網(wǎng)頁(yè)已經(jīng)消失了.
ICMP.DLL API 現(xiàn)在在Windows平臺(tái)上與微軟的Winsocks工作的很好, 但是微軟說(shuō)更好的產(chǎn)品一出來(lái)他們將替換它. 微軟說(shuō)這個(gè)自從Windows 95時(shí)代就在用, 這些功能在在Windows 2000上仍然存在.
For more information on the ICMP.DLL API, check out sockets.com's ICMP API page.
更詳細(xì)的ICMP.DLL API的信息到sockets.com的ICMP API網(wǎng)頁(yè)獲取.
- 具體實(shí)現(xiàn):
 - --------
 - // Borland C 5.0: bcc32.cpp ping.cpp
 - // Visual C 5.0: cl ping.cpp wsock32.lib
 - //
 - // This sample program is hereby placed in the public domain.
 - #include <iostream.h>
 - #include <winsock.h>
 - #include <windowsx.h>
 - #include "icmpdefs.h"
 
#p#
- ==================ping的實(shí)現(xiàn)部分==================
 - int doit(int argc, char* argv[])
 - {//[bugfree] 建議將這個(gè)argc和argv的處理拿到main函數(shù)中
 - // 檢查命令行參數(shù)
 - if (argc < 2) {
 - cerr << "usage: ping <host>" << endl;
 - return 1;
 - }
 - // 裝載ICMP.DLL連接庫(kù)
 - HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
 - if (hIcmp == 0) {
 - cerr << "Unable to locate ICMP.DLL!" << endl;
 - return 2;
 - }
 - // 查找給定機(jī)器的IP地址信息
 - struct hostent* phe;
 - if ((phe = gethostbyname(argv[1])) == 0) {
 - cerr << "Could not find IP address for " << argv[1] << endl;
 - return 3;
 - }
 - // 定義函數(shù)三個(gè)指針類型
 - typedef HANDLE (WINAPI* pfnHV)(VOID);
 - typedef BOOL (WINAPI* pfnBH)(HANDLE);
 - typedef DWORD (WINAPI* pfnDHDPWPipPDD)(HANDLE, DWORD, LPVOID, WORD,
 - PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD); // evil, no?
 - //定義三個(gè)指針函數(shù)
 - pfnHV pIcmpCreateFile;
 - pfnBH pIcmpCloseHandle;
 - pfnDHDPWPipPDD pIcmpSendEcho;
 - //從ICMP.DLL中得到函數(shù)入口地址
 - pIcmpCreateFile = (pfnHV)GetProcAddress(hIcmp, "IcmpCreateFile");
 - pIcmpCloseHandle = (pfnBH)GetProcAddress(hIcmp, "IcmpCloseHandle");
 - pIcmpSendEcho = (pfnDHDPWPipPDD)GetProcAddress(hIcmp, "IcmpSendEcho");
 - if ((pIcmpCreateFile == 0) || (pIcmpCloseHandle == 0) ||
 - (pIcmpSendEcho == 0)) {
 - cerr << "Failed to get proc addr for function." << endl;
 - return 4;
 - }
 - // 打開(kāi)ping服務(wù)
 - HANDLE hIP = pIcmpCreateFile();
 - if (hIP == INVALID_HANDLE_VALUE) {
 - cerr << "Unable to open ping service." << endl;
 - return 5;
 - }
 - // 構(gòu)造ping數(shù)據(jù)包
 - char acPingBuffer[64];
 - memset(acPingBuffer, '\xAA', sizeof(acPingBuffer));
 - PIP_ECHO_REPLY pIpe = (PIP_ECHO_REPLY)GlobalAlloc( GMEM_FIXED | GMEM_ZEROINIT,
 - sizeof(IP_ECHO_REPLY) sizeof(acPingBuffer));
 - if (pIpe == 0) {
 - cerr << "Failed to allocate global ping packet buffer." << endl;
 - return 6;
 - }
 - pIpe->Data = acPingBuffer;
 - pIpe->DataSize = sizeof(acPingBuffer);
 - // 發(fā)送ping數(shù)據(jù)包
 - DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]),
 - acPingBuffer, sizeof(acPingBuffer), NULL, pIpe,
 - sizeof(IP_ECHO_REPLY) sizeof(acPingBuffer), 5000);
 - if (dwStatus != 0) {
 - cout << "Addr: " <<
 - int(LOBYTE(LOWORD(pIpe->Address))) << "." <<
 - int(HIBYTE(LOWORD(pIpe->Address))) << "." <<
 - int(LOBYTE(HIWORD(pIpe->Address))) << "." <<
 - int(HIBYTE(HIWORD(pIpe->Address))) << ", " <<
 - "RTT: " << int(pIpe->RoundTripTime) << "ms, " <<
 - "TTL: " << int(pIpe->Options.Ttl) << endl;
 - }
 - else {
 - cerr << "Error obtaining info from ping packet." << endl;
 - }
 - // 關(guān)閉,回收資源
 - GlobalFree(pIpe);
 - FreeLibrary(hIcmp);
 - return 0;
 - }
 
#p#
- ==================主函數(shù)==================
 - int main(int argc, char* argv[])
 - {
 - WSAData wsaData;
 - if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
 - return 255;
 - }
 - int retval = doit(argc, argv);
 - WSACleanup();
 - return retval;
 - }
 
- ==================頭文件==================
 - icmpdefs.h
 - //ICMP.DLL 函數(shù)中需要的結(jié)構(gòu)
 - typedef struct {
 - unsigned char Ttl; // Time To Live
 - unsigned char Tos; // Type Of Service
 - unsigned char Flags; // IP header flags
 - unsigned char OptionsSize; // Size in bytes of options data
 - unsigned char *OptionsData; // Pointer to options data
 - } IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION;
 - typedef struct {
 - DWORD Address; // Replying address
 - unsigned long Status; // Reply status
 - unsigned long RoundTripTime; // RTT in milliseconds
 - unsigned short DataSize; // Echo data size
 - unsigned short Reserved; // Reserved for system use
 - void *Data; // Pointer to the echo data
 - IP_OPTION_INFORMATION Options; // Reply options
 - } IP_ECHO_REPLY, * PIP_ECHO_REPLY;
 















 
 
 


 
 
 
 