透析ICMP协议(3): 应用篇ping(ICMP.dll)

liuchaijin80

liuchaijin80

2016-02-19 17:13

下面图老师小编要向大家介绍下透析ICMP协议(3): 应用篇ping(ICMP.dll),看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!

  原理简介:

  --------

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  这个例子演示了应用微软的ICMP.DLL怎样"ping"另一台机器. 这个DLL是没有文档话的发送ICMP回送包API接口, 也称为"pings," 就像潜水员对声纳信号的术语一样. 这段代码出自一个被一个名叫MarkG的家伙的GUI程序, 他的网页已经消失了.

  ICMP.DLL API 现在在Windows平台上与微软的Winsocks工作的很好, 但是微软说更好的产品一出来他们将替换它. 微软说这个自从Windows 95时代就在用, 这些功能在在Windows 2000上仍然存在.

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  For more information on the ICMP.DLL API, check out sockets.com's ICMP API page.

  更详细的ICMP.DLL API的信息到sockets.com的ICMP API网页获取.

  具体实现:

  --------

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  

// 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"

  ==================ping的实现部分==================

  

int doit(int argc, char* argv[]){//[bugfree] 建议将这个argc和argv的处理拿到main函数中// 检查命令行参数if (argc  2) {cerr  "usage: ping host"  endl;return 1;}// 装载ICMP.DLL连接库HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");if (hIcmp == 0) {cerr  "Unable to locate ICMP.DLL!"  endl;return 2;}// 查找给定机器的IP地址信息struct hostent* phe;if ((phe = gethostbyname(argv[1])) == 0) {cerr  "Could not find IP address for "  argv[1]  endl;return 3;}// 定义函数三个指针类型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?//定义三个指针函数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 (WSAStartup(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 Liveunsigned char Tos; // Type Of Serviceunsigned char Flags; // IP header flagsunsigned char OptionsSize; // Size in bytes of options dataunsigned char *OptionsData; // Pointer to options data} IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION;typedef struct {DWORD Address; // Replying addressunsigned long Status; // Reply statusunsigned long RoundTripTime; // RTT in millisecondsunsigned short DataSize; // Echo data sizeunsigned short Reserved; // Reserved for system usevoid *Data; // Pointer to the echo dataIP_OPTION_INFORMATION Options; // Reply options} IP_ECHO_REPLY, * PIP_ECHO_REPLY;

联系方式: zhangliangsd@hotmail.com

展开更多 50%)
分享

猜你喜欢

透析ICMP协议(3): 应用篇ping(ICMP.dll)

编程语言 网络编程
透析ICMP协议(3): 应用篇ping(ICMP.dll)

透析ICMP协议(4): 应用篇ping(RAW Socket)

编程语言 网络编程
透析ICMP协议(4): 应用篇ping(RAW Socket)

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

透析ICMP协议(1): 协议原理

编程语言 网络编程
透析ICMP协议(1): 协议原理

透析ICMP协议(2): Windows Socket 简介

编程语言 网络编程
透析ICMP协议(2): Windows Socket 简介

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

利用Visual C#实现ICMP网络协议

编程语言 网络编程
利用Visual C#实现ICMP网络协议

Delphi中用ICMP探测远程主机是否存活

编程语言 网络编程
Delphi中用ICMP探测远程主机是否存活

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

续写时光的QQ分组设计图案_欠你一份情一份爱 - QQ情侣分组

续写时光的QQ分组设计图案_欠你一份情一份爱 - QQ情侣分组

网站氛围的营造技巧

网站氛围的营造技巧
下拉加载更多内容 ↓