Vedhæftet er en code snip som laver en ping af en given host adresse. Udfra hostnavnet så finder den IP adressen og pinger den.
I koden bliver der foretaget en logging af en central komponenten hvis kode ikke er inkluderet her. Jeg går udfra at du godt kan abstrahere det kode væk som foretager den logning og erstatte den med dit eget.
#include <iostream.h>
#include <winsock.h>
#include <windowsx.h>
#include "icmpdefs.h"
#include "stdafx.h"
#include "FileLogging.h"
#include "importado.h"
char * gpConfigFile = NULL;
char * gpDestination = NULL;
long gWaiting = 0;
int gRoundTrip = 0;
int gLogAll = 0;
int doit(int argc, char* argv[])
{
// Check for correct command-line args
if (argc < 2) {
CFileLogging::get_FileLogging()->WriteToFile("usage: ping <host>");
return 1;
}
// Load the ICMP.DLL
HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
if (hIcmp == 0) {
CFileLogging::get_FileLogging()->WriteToFile("Unable to locate ICMP.DLL!");
return 2;
}
// Look up an IP address for the given host name
struct hostent* phe;
if ((phe = gethostbyname(argv[1])) == 0) {
char temp[255];
strcpy(temp, "Could not find IP address for ");
strcat(temp, argv[1]);
CFileLogging::get_FileLogging()->WriteToFile(temp, TRUE);
return 3;
}
// 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 == 0) || (pIcmpCloseHandle == 0) ||
(pIcmpSendEcho == 0)) {
CFileLogging::get_FileLogging()->WriteToFile("Failed to get proc addr for function.");
return 4;
}
// Open the ping service
HANDLE hIP = pIcmpCreateFile();
if (hIP == INVALID_HANDLE_VALUE) {
CFileLogging::get_FileLogging()->WriteToFile("Unable to open ping service.");
return 5;
}
// 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 == 0) {
CFileLogging::get_FileLogging()->WriteToFile("Failed to allocate global ping packet buffer.");
return 6;
}
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 != 0) {
char value[16];
char temp[255 * 255];
memset(temp, 0, 255 * 255);
strcpy(temp, "Reached addr: ");
strcat(temp, argv[1]);
strcat(temp, " RTT: ");
itoa(int(pIpe->RoundTripTime), value, 10);
strcat(temp, value);
strcat(temp, " ms, ");
strcat(temp, " TTL: ");
itoa(int(pIpe->Options.Ttl), value, 10);
strcat(temp, value);
CFileLogging::get_FileLogging()->WriteToFile(temp);
}
// Shut down...
GlobalFree(pIpe);
FreeLibrary(hIcmp);
return 0;
}
Hth