#include <iostream.h>
#include <winsock.h>
#include <windowsx.h>
#include "icmpdefs.h"
#pragma comment(lib,"ws2_32.lib")
void PX(int *input, int inputlen, int *output)
{
int I;
int min=input[0];
int max=input[0];
int Num=input[0];
for(I=1; I<inputlen; I++)
{
if(min > input[I])
{
min = input[I];
}
if(max < input[I])
{
max = input[I];
}
Num = Num + input[I];
}
output[0] = max;
output[1] = min;
output[2] = (Num/inputlen);
}
int doit(int argc, char* argv)
{
int PingNum=0;
int TIME[4];
int ST[3];
int Recv=0;
//[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 1;
}
// 查找给定机器的IP地址信息
struct hostent* phe;
if ((phe = gethostbyname(argv)) == 0)
{
cerr << "Could not find IP address for " << argv << endl;
return 1;
}
// 定义函数三个指针类型
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 1;
}
// 打开ping服务
HANDLE hIP = pIcmpCreateFile();
if (hIP == INVALID_HANDLE_VALUE)
{
cerr << "Unable to open ping service." << endl;
return 1;
}
// 构造ping数据包
char acPingBuffer[1000];
memset(acPingBuffer, '\0', 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 1;
}
pIpe->Data = acPingBuffer;
pIpe->DataSize = sizeof(acPingBuffer);
SENDPING:
// 发送ping数据包
DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]),acPingBuffer, sizeof(acPingBuffer), NULL, pIpe,sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer), 000);
if (dwStatus != 0)
{
Recv++;
TIME[PingNum] = int(pIpe->RoundTripTime);
if(PingNum == 0)
cout << "\n" << "Pinging " << argv << "[" << int(LOBYTE(LOWORD(pIpe->Address))) << "." <<
int(HIBYTE(LOWORD(pIpe->Address))) << "." << int(LOBYTE(HIWORD(pIpe->Address))) << "." <<
int(HIBYTE(HIWORD(pIpe->Address))) << "]" << "with 32 bytes of data:" << "\n" <<endl;
cout << "Reply from " <<
int(LOBYTE(LOWORD(pIpe->Address))) << "." <<
int(HIBYTE(LOWORD(pIpe->Address))) << "." <<
int(LOBYTE(HIWORD(pIpe->Address))) << "." <<
int(HIBYTE(HIWORD(pIpe->Address))) << ": " << "bytes=32 " << "time = " << int(pIpe->RoundTripTime) << "ms " << "TTL=" << int(pIpe->Options.Ttl) << endl;
}
else
{
cerr << "Error obtaining info from ping packet." << endl;
}
PingNum++;
if(PingNum < 4)
{
Sleep(1500);
goto SENDPING;
}
PX(TIME, 4, ST);
cout << "\nPing statistics for "<< int(LOBYTE(LOWORD(pIpe->Address))) << "." <<
int(HIBYTE(LOWORD(pIpe->Address))) << "." <<
int(LOBYTE(HIWORD(pIpe->Address))) << "." <<
int(HIBYTE(HIWORD(pIpe->Address))) << ":\n" <<
" Packets: Sent = 4, Received = "<< Recv <<", Lost = 0 (0% loss)," << "\n" <<
"Approximate round trip times in milli-seconds:\n" << " Minimum = "<< ST[1] << "ms, Maximum = " << ST[0] << "ms, Average = " << ST[2] << "ms" << endl;
// 关闭,回收资源
GlobalFree(pIpe);
FreeLibrary(hIcmp);
return 0;
}
int main(int argc, char** argv)
{
WSAData wsaData;
if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
{
return 1;
}
int retval = doit(argc, argv[1]);
WSACleanup();
return retval;
}
需要的头文件:"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;
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/luckywubi/archive/2009/05/04/4149955.aspx
|