15. juli 2003 - 13:21
Der er
7 kommentarer og
1 løsning
Brug af PING, ping IP/host
Hej,
Jeg søger en souce som pinger en host og fortæller om der er "hul" igennem eller ej. uden brugeren skal fortælle den noget.
eller links til, hvor jeg evt. kan finde hjælp eller se andre eksempler på brug af PING.
Du har ikke skrevet hvilket Operativ System det skal fungere under...
Nedenstående fungerer i Windows og bruger icmp.dll til hjælp.
------------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;
------------main.cpp
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include "icmpdefs.h"
int main(int argc, char** argv)
{
WSAData wsaData;
if(!WSAStartup(MAKEWORD(2, 2), &wsaData))
{
if(argc == 2)
{
// Load the ICMP.DLL
HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
if(hIcmp)
{
// Look up an IP address for the given host name
struct hostent* phe;
if((phe = gethostbyname(argv[1])))
{
// Get handles to the functions inside ICMP.DLL that we'll need
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;
pIcmpCreateFile = (pfnHV)GetProcAddress(hIcmp, "IcmpCreateFile");
pIcmpCloseHandle = (pfnBH)GetProcAddress(hIcmp, "IcmpCloseHandle");
pIcmpSendEcho = (pfnDHDPWPipPDD)GetProcAddress(hIcmp, "IcmpSendEcho");
if(pIcmpCreateFile && (pIcmpCloseHandle && pIcmpSendEcho))
{
// Open the ping service
HANDLE hIP = pIcmpCreateFile();
if(hIP != INVALID_HANDLE_VALUE)
{
// Build ping packet
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)
{
pIpe->Data = acPingBuffer;
pIpe->DataSize = sizeof(acPingBuffer);
// Send the ping packet
DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]), acPingBuffer, sizeof(acPingBuffer), NULL, pIpe, sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer), 5000);
if(dwStatus)
{
printf("Host: %s Addr: %i.%i.%i.%i RTT: %ims TTL: %i\n", argv[1], int(LOBYTE(LOWORD(pIpe->Address))), int(HIBYTE(LOWORD(pIpe->Address))), int(LOBYTE(HIWORD(pIpe->Address))), int(HIBYTE(HIWORD(pIpe->Address))), int(pIpe->RoundTripTime), int(pIpe->Options.Ttl));
}
else
{
printf("Error obtaining info from ping packet.\n");
}
}
else
{
printf("Failed to allocate global ping packet buffer.\n");
return 6;
}
GlobalFree(pIpe);
}
else
{
printf("Unable to open ping service.\n");
return 5;
}
}
else
{
printf("Failed to get proc addr for function.\n");
}
}
else
{
printf("Could not find IP address for %s\n",argv[1]);
}
FreeLibrary(hIcmp);
}
else
{
printf("Unable to locate ICMP.DLL!\n");
}
}
else
{
printf("usage: ping <host>\n");
}
WSACleanup();
}
else
{
printf("Could not initialize socket\n");
}
return 0;
}
Kompileres sådan (MinGW):
g++ -c main.cpp -s
g++ -o pingtool.exe main.o -lws2_32 -s
F.eks.
pingtool.exe eksperten.dk
Host: eksperten.dk Addr: 195.184.98.141 RTT: 20ms TTL: 56