透析ICMP協議(三): 牛刀初試之一 應用篇ping(ICMP.dll) (轉)
透析ICMP(三): 牛刀初試之一
應用篇(ICMP.dll)
===============================
這篇文章出自:
翻譯: free/CSDN, 對原始程式碼加了些註釋
平臺: VC6 XP
原理簡介:
--------
這個例子演示了應用的ICMP.DLL怎樣"ping"另一臺機器. 這個DLL是沒有文件話的傳送ICMP回送包介面, 也稱為"pings," 就像潛水員對聲納訊號的術語一樣. 這段程式碼出自一個被一個名叫MarkG的傢伙的GUI, 他的網頁已經消失了.
ICMP.DLL API 現在在Windows平臺上與微軟的Winsocks工作的很好, 但是微軟說更好的產品一出來他們將替換它. 微軟說這個自從Windows 95時代就在用, 這些功能在在上仍然存在.
For more information on the ICMP.DLL API, check out sockets.com's ICMP API page.
更詳細的ICMP.DLL API的資訊到sockets.com的ICMP API網頁獲取.
具體實現:
--------
// 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 ain.
#include
#include
#include
#include "icmpdefs.h"
==================ping的實現部分==================
int doit(int argc, char* argv[])
{//[bugfree] 建議將這個argc和argv的處理拿到main中
// 檢查命令列引數
if (argc < 2) {
cerr << "usage: ping
return 1;
}
// 裝載ICMP.DLL連線庫
HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
if (hIcmp == 0) {
cerr << "Unable to locate ICMP.DLL!" << endl;
return 2;
}
// 查詢給定機器的資訊
struct hostent* phe;
if ((phe = gethostbyname(argv[1])) == 0) {
cerr << "Could not find address for " << argv[1] << endl;
return 3;
}
// 定義函式三個指標型別
typedef HANDLE (WINAPI* pfnHV)(VOID);
typedef BOOL (WINAPI* pfnBH)(HANDLE);
typedef D (WINAPI* pfnDHDPWPipPDD)(HANDLE, DWORD, LPVOID, WORD,
PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD); // evil, no?
//定義三個指標函式
pfnHV pIcmpCreateFile;
pfnBH pIcmpCloseHandle;
pfnDHDPWPipPDD pIcmpSendEcho;
//從ICMP.DLL中得到函式入口地址
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;
}
// 開啟ping服務
HANDLE hIP = pIcmpCreateFile();
if (hIP == INVALID_HANDLE_VALUE) {
cerr << "Unable to open ping service." << endl;
return 5;
}
// 構造ping資料包
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);
// 傳送ping資料包
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;
}
// 關閉,回收資源
GlobalFree(pIpe);
FreeLibrary(hIcmp);
return 0;
}
==================主函式==================
int main(int argc, char* argv[])
{
WSAData wsaData;
if (Wtartup(MAKEWORD(1, 1), &wsaData) != 0) {
return 255;
}
int retval = doit(argc, argv);
WSACleanup();
return retval;
}
==================頭==================
icmpdefs.h
//ICMP.DLL 函式中需要的結構
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;
連結:
-------
我的其它文章,<>, 和其它文章參見:
http://www.csdn.net/develop/author/netauthor/bugfree/
聯絡方式:
-------
to:zhangliangsd@.com">zhangliangsd@hotmail.com
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-993156/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ICMP協議協議
- icmp和arp協議協議
- 淺談TCP、UDP、ICMP三種常見協議TCPUDP協議
- Linux C++ 實現一個簡易版的ping (也就是ICMP協議)LinuxC++協議
- 網路通訊協議-ICMP協議詳解!協議
- ICMP Internet控制資訊協議協議
- WireShark——IP協議包分析(Ping分析IP協議包)協議
- spring篇(一)牛刀小試Spring
- 計算機網路之四:ICMP協議計算機網路協議
- 應用層協議協議
- [雪峰磁針石部落格]python網路作業:使用python的socket庫實現ICMP協議的pingPython協議
- Windows7/10實現ICMP(ping命令)Windows
- TCP應用層協議TCP協議
- HTTP協議學習---(三)進階篇HTTP協議
- 通過ICMP協議反彈SHELL並執行命令協議
- Ipv6轉換難點分析之一:協議轉換技術協議
- raft協議初識Raft協議
- 初識MQTT協議MQQT協議
- CCNA - Part7:網路層 - ICMP 應該是你最熟悉的協議了協議
- TCP與應用層協議TCP協議
- HCIA—應用層常用協議協議
- Protobuf協議應用乾貨協議
- web應用與http協議WebHTTP協議
- 應用隱私政策協議協議
- Ping 的工作原理你懂了,那 ICMP 你懂不懂?
- 應用層相關協議分析協議
- 為什麼說 ICMP 協議是網路最強輔助協議
- 常用物聯網應用層協議(1)——先說HTTP協議協議HTTP
- 頁面連結跳轉--指定協議,半協議,無協議協議
- Zookeeper應用場景和ZAB協議協議
- Swoole 多協議 多埠 的應用協議
- Django之web應用與http協議DjangoWebHTTP協議
- 應用層協議:DNS域名系統協議DNS
- Jenkins 使用指南 之 初級應用篇Jenkins
- Celery:小試牛刀
- UDP 協議簡單瞭解及應用UDP協議
- python初試三Python
- 測試開發之網路篇-常用服務協議協議
- Centos 6、7 作業系統中,如何開啟或關閉ICMP協議CentOS作業系統協議