通过icmp.dll编程实现ping命令

这篇博客展示了如何通过编程实现ping命令,利用Windows API加载ICMP.DLL库,获取函数入口地址,打开ping服务,构造并发送ping数据包,最后接收并解析回复信息。
摘要由CSDN通过智能技术生成

//main.cpp 

#include "stdafx.h"
#include <winsock.h>
#include <windowsx.h>
#include "icmpdefs.h"

using namespace std;

int doit(int argc, char* argv[])
{//[bugfree] 建议将这个argc和argv的处理拿到main函数中
    // 检查命令行参数
    if (argc < 2) {
        cerr << "未写IP地址。" << endl;
  exit(1);
        //return 1;
    }
   
    // 装载ICMP.DLL连接库
    HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
    if (hIcmp == 0) {
        cerr << "不能打开ICMP.DLL!" << endl;
        exit(1);
  //return 2;
    }

    // 查找给定机器的IP地址信息
    struct hostent* phe;
    if ((phe = gethostbyname(argv[1])) == 0) {
        cerr << "不能找到IP地址: " << argv[1] << endl;
        exit(1);
  //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 << "获取失败。" << endl;
        return 4;
    }

    // 打开ping服务
    HANDLE hIP = pIcmpCreateFile();
    if (hIP == INVALID_HANDLE_VALUE) {
        cerr << "不能打开ping服务。" << endl;
        exit(1);
  //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;
        exit(1);
  //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 << "IP地址: " <<
                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 << "不能获取ping信息。" << endl;
    }

    // 关闭,回收资源
    GlobalFree(pIpe);
    FreeLibrary(hIcmp);
    return 0;
}
int main(int argc, char* argv[])
{
    WSAData wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        return 255;
    }
 while(1)
 {
  doit(argc, argv);
  Sleep(2000);
 }
    //int retval = doit(argc, argv);
    WSACleanup();
    return 0;
}

//icmpdefs.h

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值