2014-12-17 14:17:46 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2014-12-17 14:17:46 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN //else winsock will be included with windows.h and conflict with winsock2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <iphlpapi.h>
|
|
|
|
#include <icmpapi.h>
|
|
|
|
#include <Shlwapi.h>
|
|
|
|
#include <ws2ipdef.h>
|
|
|
|
#include <Mstcpip.h>
|
|
|
|
#include <Ws2tcpip.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
#include "check_ping.h"
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
#define VERSION 1.0
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
static BOOL debug = FALSE;
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
INT wmain(INT argc, WCHAR **argv)
|
2014-12-17 14:17:46 +01:00
|
|
|
{
|
|
|
|
po::variables_map vm;
|
|
|
|
printInfoStruct printInfo;
|
|
|
|
response response;
|
|
|
|
|
|
|
|
WSADATA dat;
|
|
|
|
WORD req = MAKEWORD(1, 1);
|
|
|
|
|
|
|
|
WSAStartup(req, &dat);
|
|
|
|
|
|
|
|
if (parseArguments(argc, argv, vm, printInfo) != -1)
|
|
|
|
return 3;
|
|
|
|
if (printInfo.ipv4) {
|
|
|
|
if (check_ping4(printInfo, response) != -1)
|
|
|
|
return 3;
|
|
|
|
} else {
|
|
|
|
if (check_ping6(printInfo, response) != -1)
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
WSACleanup();
|
|
|
|
return printOutput(printInfo, response);
|
|
|
|
}
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
INT parseArguments(INT ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo)
|
2014-12-17 14:17:46 +01:00
|
|
|
{
|
2015-03-23 13:07:02 +01:00
|
|
|
WCHAR namePath[MAX_PATH];
|
2014-12-17 14:17:46 +01:00
|
|
|
GetModuleFileName(NULL, namePath, MAX_PATH);
|
2015-03-23 13:07:02 +01:00
|
|
|
WCHAR *progName = PathFindFileName(namePath);
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
po::options_description desc;
|
|
|
|
|
|
|
|
desc.add_options()
|
2015-03-23 13:07:02 +01:00
|
|
|
("help,h", "Print usage message and exit")
|
|
|
|
("version,V", "Print version and exit")
|
2015-01-12 15:53:25 +01:00
|
|
|
("debug,d", "Verbose/Debug output")
|
2015-03-23 13:07:02 +01:00
|
|
|
("host,H", po::wvalue<std::wstring>()->required(), "Host ip to ping")
|
|
|
|
(",4", "--Host is an ipv4 address (default)")
|
|
|
|
(",6", "--Host is an ipv6 address")
|
|
|
|
("timeout,t", po::value<INT>(), "Specify timeout for requests in ms (default=1000)")
|
|
|
|
("packets,p", po::value<INT>(), "Declare ping count (default=5)")
|
|
|
|
("warning,w", po::wvalue<std::wstring>(), "Warning values: rtt,package loss")
|
|
|
|
("critical,c", po::wvalue<std::wstring>(), "Critical values: rtt,package loss")
|
2014-12-17 14:17:46 +01:00
|
|
|
;
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
po::basic_command_line_parser<WCHAR> parser(ac, av);
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
po::store(
|
|
|
|
parser
|
|
|
|
.options(desc)
|
|
|
|
.style(
|
|
|
|
po::command_line_style::unix_style |
|
|
|
|
po::command_line_style::allow_long_disguise &
|
|
|
|
~po::command_line_style::allow_guessing
|
|
|
|
)
|
|
|
|
.run(),
|
|
|
|
vm);
|
|
|
|
vm.notify();
|
|
|
|
} catch (std::exception& e) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << e.what() << '\n' << desc << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("help")) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
wprintf(
|
|
|
|
L"%s is a simple program to ping an ip4 address.\n"
|
|
|
|
L"You can use the following options to define its behaviour:\n\n", progName);
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << desc;
|
2014-12-17 14:17:46 +01:00
|
|
|
wprintf(
|
|
|
|
L"\nIt will take at least timeout times number of pings to run\n"
|
|
|
|
L"Then it will output a string looking something like this:\n\n"
|
|
|
|
L"\tPING WARNING RTA: 72ms Packet loss: 20% | ping=72ms;40;80;71;77 pl=20%;20;50;0;100\n\n"
|
2015-03-06 12:14:09 +01:00
|
|
|
L"\"PING\" being the type of the check, \"WARNING\" the returned status\n"
|
2014-12-17 14:17:46 +01:00
|
|
|
L"and \"RTA: 72ms Packet loss: 20%\" the relevant information.\n"
|
|
|
|
L"The performance data is found behind the \"|\", in order:\n"
|
|
|
|
L"returned value, warning threshold, critical threshold, minimal value and,\n"
|
|
|
|
L"if applicable, the maximal value. \n\n"
|
|
|
|
L"%s' exit codes denote the following:\n"
|
|
|
|
L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
|
|
|
|
L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
|
|
|
|
L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
|
|
|
|
L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
|
|
|
|
L"Threshold syntax:\n\n"
|
|
|
|
L"-w THRESHOLD\n"
|
|
|
|
L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
|
|
|
|
L"(unless stated differently)\n\n"
|
|
|
|
L"-w !THRESHOLD\n"
|
|
|
|
L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
|
|
|
|
L"-w [THR1-THR2]\n"
|
|
|
|
L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
|
|
|
|
L"-w ![THR1-THR2]\n"
|
|
|
|
L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
|
|
|
|
L"-w THRESHOLD%%\n"
|
|
|
|
L"if the plugin accepts percentage based thresholds those will be used.\n"
|
|
|
|
L"Does nothing if the plugin does not accept percentages, or only uses\n"
|
|
|
|
L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
|
|
|
|
L"to end with a percentage sign.\n\n"
|
|
|
|
L"All of these options work with the critical threshold \"-c\" too."
|
|
|
|
, progName);
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("version")) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << progName << " Version: " << VERSION << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-19 10:21:38 +01:00
|
|
|
if (vm.count("-4") && vm.count("-6")) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << "Conflicting options \"4\" and \"6\"" << '\n';
|
2014-12-19 10:21:38 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
if (vm.count("warning")) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::vector<std::wstring> sVec = splitMultiOptions(vm["warning"].as<std::wstring>());
|
2014-12-17 14:17:46 +01:00
|
|
|
if (sVec.size() != 2) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << "Wrong format for warning thresholds" << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
printInfo.warn = threshold(*sVec.begin());
|
|
|
|
printInfo.wpl = threshold(sVec.back());
|
|
|
|
if (!printInfo.wpl.perc) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << "Packet loss must be percentage" << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
} catch (std::invalid_argument& e) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << e.what() << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (vm.count("critical")) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::vector<std::wstring> sVec = splitMultiOptions(vm["critical"].as<std::wstring>());
|
2014-12-17 14:17:46 +01:00
|
|
|
if (sVec.size() != 2) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << "Wrong format for critical thresholds" << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
printInfo.crit = threshold(*sVec.begin());
|
|
|
|
printInfo.cpl = threshold(sVec.back());
|
|
|
|
if (!printInfo.wpl.perc) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << "Packet loss must be percentage" << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
} catch (std::invalid_argument& e) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << e.what() << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("timeout"))
|
2015-03-23 13:07:02 +01:00
|
|
|
printInfo.timeout = vm["timeout"].as<INT>();
|
2015-07-16 13:07:19 +02:00
|
|
|
if (vm.count("packets"))
|
|
|
|
printInfo.num = vm["packets"].as<INT>();
|
2014-12-17 14:17:46 +01:00
|
|
|
if (vm.count("-6"))
|
|
|
|
printInfo.ipv4 = FALSE;
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
printInfo.host = vm["host"].as<std::wstring>();
|
2014-12-17 14:17:46 +01:00
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (vm.count("debug"))
|
|
|
|
debug = TRUE;
|
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
INT printOutput(printInfoStruct& printInfo, response& response)
|
2014-12-17 14:17:46 +01:00
|
|
|
{
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Constructing output string" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
state state = OK;
|
|
|
|
|
|
|
|
double plp = ((double)response.dropped / printInfo.num) * 100.0;
|
2015-02-09 09:03:03 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
if (printInfo.warn.rend(response.avg) || printInfo.wpl.rend(plp))
|
|
|
|
state = WARNING;
|
|
|
|
|
|
|
|
if (printInfo.crit.rend(response.avg) || printInfo.cpl.rend(plp))
|
|
|
|
state = CRITICAL;
|
|
|
|
|
|
|
|
std::wstringstream perf;
|
|
|
|
perf << L"rta=" << response.avg << L"ms;" << printInfo.warn.pString() << L";"
|
|
|
|
<< printInfo.crit.pString() << L";0;" << " pl=" << removeZero(plp) << "%;"
|
2015-02-09 09:03:03 +01:00
|
|
|
<< printInfo.wpl.pString() << ";" << printInfo.cpl.pString() << ";0;100";
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
if (response.dropped == printInfo.num) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"PING CRITICAL ALL CONNECTIONS DROPPED | " << perf.str() << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case OK:
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"PING OK RTA: " << response.avg << L"ms Packet loss: " << removeZero(plp) << "% | " << perf.str() << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
break;
|
|
|
|
case WARNING:
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"PING WARNING RTA: " << response.avg << L"ms Packet loss: " << removeZero(plp) << "% | " << perf.str() << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
break;
|
|
|
|
case CRITICAL:
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"PING CRITICAL RTA: " << response.avg << L"ms Packet loss: " << removeZero(plp) << "% | " << perf.str() << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
INT check_ping4(const printInfoStruct& pi, response& response)
|
2014-12-17 14:17:46 +01:00
|
|
|
{
|
|
|
|
in_addr ipDest4;
|
|
|
|
HANDLE hIcmp;
|
|
|
|
DWORD dwRet = 0, dwRepSize = 0;
|
|
|
|
LPVOID repBuf = NULL;
|
|
|
|
UINT rtt = 0;
|
2015-03-23 13:07:02 +01:00
|
|
|
INT num = pi.num;
|
2014-12-17 14:17:46 +01:00
|
|
|
LARGE_INTEGER frequency, timer1, timer2;
|
|
|
|
LPCWSTR term;
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Parsing ip address" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
if (RtlIpv4StringToAddress(pi.host.c_str(), TRUE, &term, &ipDest4) == STATUS_INVALID_PARAMETER) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << pi.host << " is not a valid ip address\n";
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2014-12-19 10:21:38 +01:00
|
|
|
if (*term != L'\0') {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << pi.host << " is not a valid ip address\n";
|
2014-12-19 10:21:38 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Creating Icmp File\n";
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
if ((hIcmp = IcmpCreateFile()) == INVALID_HANDLE_VALUE)
|
|
|
|
goto die;
|
|
|
|
|
|
|
|
dwRepSize = sizeof(ICMP_ECHO_REPLY) + 8;
|
|
|
|
repBuf = reinterpret_cast<VOID *>(new BYTE[dwRepSize]);
|
|
|
|
|
|
|
|
if (repBuf == NULL)
|
|
|
|
goto die;
|
|
|
|
|
|
|
|
QueryPerformanceFrequency(&frequency);
|
|
|
|
do {
|
|
|
|
QueryPerformanceCounter(&timer1);
|
2015-01-12 15:53:25 +01:00
|
|
|
|
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Sending Icmp echo\n";
|
2015-01-12 15:53:25 +01:00
|
|
|
|
|
|
|
if (!IcmpSendEcho2(hIcmp, NULL, NULL, NULL, ipDest4.S_un.S_addr,
|
|
|
|
NULL, 0, NULL, repBuf, dwRepSize, pi.timeout)) {
|
2014-12-17 14:17:46 +01:00
|
|
|
response.dropped++;
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Dropped: Response was 0" << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Ping recieved" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
PICMP_ECHO_REPLY pEchoReply = static_cast<PICMP_ECHO_REPLY>(repBuf);
|
|
|
|
|
|
|
|
if (pEchoReply->Status != IP_SUCCESS) {
|
|
|
|
response.dropped++;
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Dropped: echo reply status " << pEchoReply->Status << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Recorded rtt of " << pEchoReply->RoundTripTime << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
rtt += pEchoReply->RoundTripTime;
|
|
|
|
if (response.pMin == 0 || pEchoReply->RoundTripTime < response.pMin)
|
|
|
|
response.pMin = pEchoReply->RoundTripTime;
|
|
|
|
else if (pEchoReply->RoundTripTime > response.pMax)
|
|
|
|
response.pMax = pEchoReply->RoundTripTime;
|
|
|
|
|
|
|
|
QueryPerformanceCounter(&timer2);
|
|
|
|
if (((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart) < pi.timeout)
|
|
|
|
Sleep(pi.timeout - ((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart));
|
|
|
|
} while (--num);
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"All pings sent. Cleaning up and returning" << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
|
2015-02-17 15:54:23 +01:00
|
|
|
if (hIcmp)
|
|
|
|
IcmpCloseHandle(hIcmp);
|
|
|
|
if (repBuf)
|
|
|
|
delete reinterpret_cast<VOID *>(repBuf);
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
response.avg = ((double)rtt / pi.num);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
die:
|
2015-02-17 15:54:23 +01:00
|
|
|
die();
|
2014-12-17 14:17:46 +01:00
|
|
|
if (hIcmp)
|
|
|
|
IcmpCloseHandle(hIcmp);
|
|
|
|
if (repBuf)
|
|
|
|
delete reinterpret_cast<VOID *>(repBuf);
|
|
|
|
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
INT check_ping6(const printInfoStruct& pi, response& response)
|
2014-12-17 14:17:46 +01:00
|
|
|
{
|
|
|
|
sockaddr_in6 ipDest6, ipSource6;
|
|
|
|
IP_OPTION_INFORMATION ipInfo = { 30, 0, 0, 0, NULL };
|
|
|
|
DWORD dwRepSize = sizeof(ICMPV6_ECHO_REPLY) + 8;
|
|
|
|
LPVOID repBuf = reinterpret_cast<VOID *>(new BYTE[dwRepSize]);
|
2015-02-17 15:54:23 +01:00
|
|
|
HANDLE hIcmp = NULL;
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
LARGE_INTEGER frequency, timer1, timer2;
|
2015-03-23 13:07:02 +01:00
|
|
|
INT num = pi.num;
|
2014-12-17 14:17:46 +01:00
|
|
|
UINT rtt = 0;
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Parsing ip address" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
if (RtlIpv6StringToAddressEx(pi.host.c_str(), &ipDest6.sin6_addr, &ipDest6.sin6_scope_id, &ipDest6.sin6_port)) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << pi.host << " is not a valid ipv6 address" << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipDest6.sin6_family = AF_INET6;
|
|
|
|
|
|
|
|
ipSource6.sin6_addr = in6addr_any;
|
|
|
|
ipSource6.sin6_family = AF_INET6;
|
|
|
|
ipSource6.sin6_flowinfo = 0;
|
|
|
|
ipSource6.sin6_port = 0;
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Creating Icmp File" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2015-02-17 15:54:23 +01:00
|
|
|
hIcmp = Icmp6CreateFile();
|
2014-12-17 14:17:46 +01:00
|
|
|
if (hIcmp == INVALID_HANDLE_VALUE) {
|
|
|
|
goto die;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryPerformanceFrequency(&frequency);
|
|
|
|
do {
|
|
|
|
QueryPerformanceCounter(&timer1);
|
2015-01-12 15:53:25 +01:00
|
|
|
|
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Sending Icmp echo" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
if (!Icmp6SendEcho2(hIcmp, NULL, NULL, NULL, &ipSource6, &ipDest6,
|
|
|
|
NULL, 0, &ipInfo, repBuf, dwRepSize, pi.timeout)) {
|
|
|
|
response.dropped++;
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Dropped: Response was 0" << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Ping recieved" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
Icmp6ParseReplies(repBuf, dwRepSize);
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
ICMPV6_ECHO_REPLY *pEchoReply = static_cast<ICMPV6_ECHO_REPLY *>(repBuf);
|
2014-12-17 14:17:46 +01:00
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (pEchoReply->Status != IP_SUCCESS) {
|
2014-12-17 14:17:46 +01:00
|
|
|
response.dropped++;
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Dropped: echo reply status " << pEchoReply->Status << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
rtt += pEchoReply->RoundTripTime;
|
|
|
|
|
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Recorded rtt of " << pEchoReply->RoundTripTime << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
|
|
|
if (response.pMin == 0 || pEchoReply->RoundTripTime < response.pMin)
|
|
|
|
response.pMin = pEchoReply->RoundTripTime;
|
|
|
|
else if (pEchoReply->RoundTripTime > response.pMax)
|
|
|
|
response.pMax = pEchoReply->RoundTripTime;
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
QueryPerformanceCounter(&timer2);
|
|
|
|
if (((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart) < pi.timeout)
|
|
|
|
Sleep(pi.timeout - ((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart));
|
|
|
|
} while (--num);
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"All pings sent. Cleaning up and returning" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2015-02-17 15:54:23 +01:00
|
|
|
if (hIcmp)
|
|
|
|
IcmpCloseHandle(hIcmp);
|
|
|
|
if (repBuf)
|
|
|
|
delete reinterpret_cast<VOID *>(repBuf);
|
2014-12-17 14:17:46 +01:00
|
|
|
response.avg = ((double)rtt / pi.num);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
die:
|
|
|
|
die(GetLastError());
|
|
|
|
|
|
|
|
if (hIcmp)
|
|
|
|
IcmpCloseHandle(hIcmp);
|
|
|
|
if (repBuf)
|
|
|
|
delete reinterpret_cast<VOID *>(repBuf);
|
|
|
|
|
|
|
|
return 3;
|
2015-09-18 13:04:09 +02:00
|
|
|
}
|