透析ICMP协议(三): 牛刀初试之一 应用篇ping(ICMP.dll)[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 bugfree 的 blog

透析icmp协议(三): 牛刀初试之一
 应用篇ping(icmp.dll)
===============================
这篇文章出自:http://tangentsoft.net/wskfaq/examples/dllping.html
翻译: bugfree/csdn, 对原始代码加了些注释
平台: vc6 windows xp

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

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

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

本文关键:ICMP ping tracert
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top