/
main.cpp
executable file
·67 lines (48 loc) · 1.11 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* This file is:
*
* Copyright (C) 2002 Stephen Makonin.
* All Rights Reserved.
*
*/
#include <winsock.h>
#include <string.h>
#include "ipexport.h"
#include "icmpapi.h"
#define PING_TIMEOUT 5000
#define VB_TRUE 1
#define VB_FALSE 0
long _stdcall ping(char *error, char *host)
{
IPAddr ip;
WORD w = MAKEWORD(1,1);
WSADATA wsadata;
strset(error, 0);
WSAStartup(w, &wsadata);
hostent* phostent;
if( host[0] <= '9')
ip = (IPAddr)inet_addr(host);
else
{
phostent = gethostbyname(host);
if(!phostent)
{
strcpy(error, "Unable to resolve host name.");
return -1;
}
ip = *(DWORD*)(*phostent->h_addr_list);
}
HANDLE icmphandle = IcmpCreateFile();
char reply[sizeof(icmp_echo_reply)+8];
icmp_echo_reply* iep = (icmp_echo_reply*)&reply;
iep->RoundTripTime = 0xffffffff;
IcmpSendEcho(icmphandle,ip,0,0,NULL,reply,sizeof(icmp_echo_reply)+8,PING_TIMEOUT);
IcmpCloseHandle(icmphandle);
WSACleanup();
if(iep->RoundTripTime == PING_TIMEOUT)
{
strcpy(error, "Timeout.");
return -1;
}
return iep->RoundTripTime;
}