- UID
- 852722
|
#include<windows.h>
#include<WinSock.h>
#include<stdio.h>
#include<string.h>
#pragma comment(lib,"WSOCK32")
typedef struct tagIPINFO
{
u_charTtl; //Time To Live
u_charTos; //Type Of Service
u_charIPFlags; //IP flags
u_charOptSize; //Size of options data
u_charFAR *Options; //Options data buffer
}IPINFO,*PIPINFO;
typedef struct tagICMPECHO
{
u_longSource; //Source address
u_longStatus; //IP status
u_longRTTime; //Round trip timein milliseconds
u_shortDataSize; //Reply data size
u_shortReserved; //Unknown
voidFAR *pData; //Reply data buffer
IPINFOipInfo; //Reply options
}ICMPECHO, *PICMPECHO;
//ICMP.DLL Export Function Pointers
HANDLE(WINAPI *pIcmpCreateFile)(VOID);
BOOL(WINAPI *pIcmpCloseHandle)(HANDLE);
DWORD(WINAPI *pIcmpSendEcho)
(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);
//
void main(int argc, char **argv)
{
WSADATAwsaData; //WSADATA
ICMPECHOicmpEcho; //ICMP Echo replybuffer
HANDLE hndlIcmp; //LoadLibrary()handleto ICMP.DLL
HANDLE hndlFile; //Handlefor IcmpCreateFile()
LPHOSTENTpHost; //Pointer to host entrystructure
structin_addr iaDest; //Internet address structure
DWORD *dwAddress; //IPAddress
IPINFO ipInfo; //IPOptions structure
intnRet; //Generaluse return code
DWORDdwRet; //DWORDreturn code
intx;
//Checkarguments
if(argc!=1)
{
fprintf(stderr,"\nSyntax:pingHostNameOrIPAddress\n");
return;
}
//Dynamicallyload the ICMP.DLL
hndlIcmp=LoadLibrary("ICMP.DLL");
if(hndlIcmp==NULL)
{
fprintf(stderr,"\nCouldnot load ICMP.DLL\n");
return;
}
//RetrieveICMP function pointers
pIcmpCreateFile=(HANDLE(WINAPI*)(void))
GetProcAddress(hndlIcmp,"IcmpCreateFile");
pIcmpCloseHandle=(BOOL(WINAPI*)(HANDLE))
GetProcAddress(hndlIcmp,"IcmpCloseHandle");
pIcmpSendEcho=(DWORD(WINAPI*)
(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
GetProcAddress(hndlIcmp,"IcmpSendEcho");
//Cheakall the function pointers
if(pIcmpCreateFile==NULL||
pIcmpCloseHandle==NULL||
pIcmpSendEcho==NULL)
{
fprintf(stderr,"\nErrorgetting ICMP proc address\n");
FreeLibrary(hndlIcmp);
return;
}
//InitWinSock
nRet=WSAStartup(0x0101,&wsaData);
if(nRet)
{
fprintf(stderr,"\nWSAStartup()error:%d\n",nRet);
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}
//CheakWinSock version
if(0x0101!=wsaData.wVersion)
{
fprintf(stderr,"\nWinSockversion 1.1 not supported\n");
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}
//Lookupdestination
//Useinet_addr() to determine if we're dealing with a name
//oran address
iaDest.s_addr=inet_addr(argv[1]);
if(iaDest.s_addr==INADDR_NONE)
pHost=gethostbyname(argv[1]);
else
pHost=gethostbyaddr((constchar*)&iaDest,
sizeof(structin_addr),AF_INET);
if(pHost==NULL)
{
fprintf(stderr,"\n%snot found\n",argv[1]);
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}
//Tellthe user what we're doing
printf("\nPinging%s[%s]",pHost->h_name,
inet_ntoa((*(LPIN_ADDR)pHost->h_addr_list[0])));
//Copythe address
dwAddress=(DWORD*)(*pHost->h_addr_list);
//Getan ICMP echo request handle
hndlFile=pIcmpCreateFile();
for(x=0;x<4;x++)
{
//Setsome reasonable default valuse
ipInfo.Ttl=255;
ipInfo.Tos=0;
ipInfo.IPFlags=0;
ipInfo.OptSize=0;
ipInfo.Options=NULL;
//icmpEcho.ipInfo.Ttl=256;
//Reqestan ICMP echo
dwRet=pIcmpSendEcho(
hndlFile, //Handle from IcmpCreateFile()
*dwAddress, //Destination IP address
NULL, //Pointer to buffer tosend
0, //Size ofbuffer in bytes
&ipInfo, //Request options
&icmpEcho, //Request options
sizeof(structtagICMPECHO),
5000); //Time to wait inmilliseconds
//Printthe results
iaDest.s_addr=icmpEcho.Source;
printf("\nReplyfrom%s Time=%ldms TTL=%d",
inet_ntoa(iaDest),
icmpEcho.RTTime,
icmpEcho.ipInfo.Ttl);
if(icmpEcho.Status)
{
printf("\nError:icmpEcho.Status=%ld",
icmpEcho.Status);
break;
}
}
printf("\n");
//Closethe echo request file handle
pIcmpCloseHandle(hndlFile);
FreeLibrary(hndlIcmp);
WSACleanup();
} |
|