2014-12-17 14:17:46 +01:00
|
|
|
/******************************************************************************
|
2018-02-01 08:45:09 +01:00
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN //else winsock will be included with windows.h and conflict with winsock2
|
2017-12-19 15:50:05 +01:00
|
|
|
#endif
|
2014-12-17 14:17:46 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
#include "plugins/thresholds.hpp"
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
#include <iostream>
|
2014-12-17 14:17:46 +01:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <iphlpapi.h>
|
|
|
|
#include <icmpapi.h>
|
2018-02-01 08:45:09 +01:00
|
|
|
#include <shlwapi.h>
|
2014-12-17 14:17:46 +01:00
|
|
|
#include <ws2ipdef.h>
|
2018-02-01 08:45:09 +01:00
|
|
|
#include <mstcpip.h>
|
|
|
|
#include <ws2tcpip.h>
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
#define VERSION 1.0
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
struct response
|
2014-12-17 14:17:46 +01:00
|
|
|
{
|
2018-02-01 08:45:09 +01:00
|
|
|
double avg;
|
|
|
|
unsigned int pMin = 0;
|
|
|
|
unsigned int pMax = 0;
|
|
|
|
unsigned int dropped = 0;
|
|
|
|
};
|
2014-12-17 14:17:46 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
struct printInfoStruct
|
|
|
|
{
|
|
|
|
threshold warn;
|
|
|
|
threshold crit;
|
|
|
|
threshold wpl;
|
|
|
|
threshold cpl;
|
|
|
|
std::wstring host;
|
|
|
|
std::wstring ip;
|
|
|
|
bool ipv6 = false;
|
|
|
|
int timeout = 1000;
|
|
|
|
int num = 5;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool l_Debug;
|
|
|
|
|
|
|
|
static 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")
|
2016-09-09 17:17:07 +02:00
|
|
|
("host,H", po::wvalue<std::wstring>()->required(), "Target hostname or IP. If an IPv6 address is given, the '-6' option must be set")
|
|
|
|
(",4", "--Host is an IPv4 address or if it's a hostname: Resolve it to an IPv4 address (default)")
|
|
|
|
(",6", "--Host is an IPv6 address or if it's a hostname: Resolve it to an IPv6 address")
|
2018-02-01 08:45:09 +01:00
|
|
|
("timeout,t", po::value<int>(), "Specify timeout for requests in ms (default=1000)")
|
|
|
|
("packets,p", po::value<int>(), "Declare ping count (default=5)")
|
2015-03-23 13:07:02 +01:00
|
|
|
("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
|
|
|
;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
po::wcommand_line_parser parser(ac, av);
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
po::store(
|
|
|
|
parser
|
|
|
|
.options(desc)
|
|
|
|
.style(
|
2018-02-01 08:45:09 +01:00
|
|
|
po::command_line_style::unix_style |
|
|
|
|
po::command_line_style::allow_long_disguise &
|
|
|
|
~po::command_line_style::allow_guessing
|
2014-12-17 14:17:46 +01:00
|
|
|
)
|
|
|
|
.run(),
|
|
|
|
vm);
|
|
|
|
vm.notify();
|
2018-02-01 08:45:09 +01:00
|
|
|
} catch (const 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"
|
2017-12-13 12:48:14 +01:00
|
|
|
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"
|
2017-12-13 12:48:14 +01:00
|
|
|
L"and \"RTA: 72ms Packet loss: 20%%\" the relevant information.\n"
|
2014-12-17 14:17:46 +01:00
|
|
|
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"
|
2017-12-13 12:48:14 +01:00
|
|
|
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;
|
|
|
|
}
|
2018-02-01 08:45:09 +01:00
|
|
|
|
|
|
|
printInfo.ipv6 = vm.count("-6") > 0;
|
2016-09-09 17:17:07 +02:00
|
|
|
|
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;
|
|
|
|
}
|
2018-02-01 08:45:09 +01:00
|
|
|
} catch (const 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;
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 08:45:09 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
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;
|
|
|
|
}
|
2018-02-01 08:45:09 +01:00
|
|
|
} catch (const 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"))
|
2018-02-01 08:45:09 +01:00
|
|
|
printInfo.timeout = vm["timeout"].as<int>();
|
2016-09-09 17:17:07 +02:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (vm.count("packets"))
|
|
|
|
printInfo.num = vm["packets"].as<int>();
|
2014-12-17 14:17:46 +01:00
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
printInfo.host = vm["host"].as<std::wstring>();
|
2014-12-17 14:17:46 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
l_Debug = vm.count("debug") > 0;
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
static int printOutput(printInfoStruct& printInfo, response& response)
|
2014-12-17 14:17:46 +01:00
|
|
|
{
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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";"
|
2017-12-19 15:50:05 +01:00
|
|
|
<< 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';
|
2017-05-04 09:38:08 +02:00
|
|
|
return 2;
|
2014-12-17 14:17:46 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
std::wcout << L"PING ";
|
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
switch (state) {
|
|
|
|
case OK:
|
2018-02-01 08:45:09 +01:00
|
|
|
std::wcout << L"OK";
|
2014-12-17 14:17:46 +01:00
|
|
|
break;
|
|
|
|
case WARNING:
|
2018-02-01 08:45:09 +01:00
|
|
|
std::wcout << L"WARNING";
|
2014-12-17 14:17:46 +01:00
|
|
|
break;
|
|
|
|
case CRITICAL:
|
2018-02-01 08:45:09 +01:00
|
|
|
std::wcout << L"CRITICAL";
|
2014-12-17 14:17:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
std::wcout << L" RTA: " << response.avg << L"ms Packet loss: " << removeZero(plp) << "% | " << perf.str() << '\n';
|
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
static bool resolveHostname(const std::wstring& hostname, bool ipv6, std::wstring& ipaddr)
|
2016-09-09 17:17:07 +02:00
|
|
|
{
|
|
|
|
ADDRINFOW hints;
|
|
|
|
ZeroMemory(&hints, sizeof(hints));
|
|
|
|
|
|
|
|
if (ipv6)
|
|
|
|
hints.ai_family = AF_INET6;
|
|
|
|
else
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2016-09-09 17:17:07 +02:00
|
|
|
std::wcout << L"Resolving hostname \"" << hostname << L"\"\n";
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
ADDRINFOW *result = NULL;
|
2016-09-09 17:17:07 +02:00
|
|
|
DWORD ret = GetAddrInfoW(hostname.c_str(), NULL, &hints, &result);
|
|
|
|
|
|
|
|
if (ret) {
|
2018-06-25 11:21:44 +02:00
|
|
|
std::wcout << L"Failed to resolve hostname. Error " << ret << L": " << formatErrorInfo(ret) << L"\n";
|
2016-09-09 17:17:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
wchar_t ipstringbuffer[46];
|
|
|
|
|
2016-09-09 17:17:07 +02:00
|
|
|
if (ipv6) {
|
2018-02-01 08:45:09 +01:00
|
|
|
struct sockaddr_in6 *address6 = (struct sockaddr_in6 *)result->ai_addr;
|
2016-09-09 17:17:07 +02:00
|
|
|
InetNtop(AF_INET6, &address6->sin6_addr, ipstringbuffer, 46);
|
2018-02-01 08:45:09 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct sockaddr_in *address4 = (struct sockaddr_in *)result->ai_addr;
|
2016-09-09 17:17:07 +02:00
|
|
|
InetNtop(AF_INET, &address4->sin_addr, ipstringbuffer, 46);
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2016-09-09 17:17:07 +02:00
|
|
|
std::wcout << L"Resolved to \"" << ipstringbuffer << L"\"\n";
|
|
|
|
|
|
|
|
ipaddr = ipstringbuffer;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
static int check_ping4(const printInfoStruct& pi, response& response)
|
2014-12-17 14:17:46 +01:00
|
|
|
{
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Parsing ip address" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
in_addr ipDest4;
|
|
|
|
LPCWSTR term;
|
2016-09-09 17:17:07 +02:00
|
|
|
if (RtlIpv4StringToAddress(pi.ip.c_str(), TRUE, &term, &ipDest4) == STATUS_INVALID_PARAMETER) {
|
|
|
|
std::wcout << pi.ip << " 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') {
|
2016-09-09 17:17:07 +02:00
|
|
|
std::wcout << pi.ip << " is not a valid ip address\n";
|
2014-12-19 10:21:38 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Creating Icmp File\n";
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
HANDLE hIcmp;
|
2014-12-17 14:17:46 +01:00
|
|
|
if ((hIcmp = IcmpCreateFile()) == INVALID_HANDLE_VALUE)
|
|
|
|
goto die;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
DWORD dwRepSize = sizeof(ICMP_ECHO_REPLY) + 8;
|
|
|
|
void *repBuf = reinterpret_cast<VOID *>(new BYTE[dwRepSize]);
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
if (repBuf == NULL)
|
|
|
|
goto die;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
unsigned int rtt = 0;
|
|
|
|
int num = pi.num;
|
|
|
|
|
|
|
|
LARGE_INTEGER frequency;
|
2014-12-17 14:17:46 +01:00
|
|
|
QueryPerformanceFrequency(&frequency);
|
2018-02-01 08:45:09 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
do {
|
2018-02-01 08:45:09 +01:00
|
|
|
LARGE_INTEGER timer1;
|
2014-12-17 14:17:46 +01:00
|
|
|
QueryPerformanceCounter(&timer1);
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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++;
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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++;
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
LARGE_INTEGER timer2;
|
2014-12-17 14:17:46 +01:00
|
|
|
QueryPerformanceCounter(&timer2);
|
2018-02-01 08:45:09 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
if (((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart) < pi.timeout)
|
|
|
|
Sleep(pi.timeout - ((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart));
|
|
|
|
} while (--num);
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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:
|
2018-02-01 08:45:09 +01:00
|
|
|
printErrorInfo();
|
2014-12-17 14:17:46 +01:00
|
|
|
if (hIcmp)
|
|
|
|
IcmpCloseHandle(hIcmp);
|
|
|
|
if (repBuf)
|
|
|
|
delete reinterpret_cast<VOID *>(repBuf);
|
|
|
|
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
static int check_ping6(const printInfoStruct& pi, response& response)
|
2014-12-17 14:17:46 +01:00
|
|
|
{
|
|
|
|
DWORD dwRepSize = sizeof(ICMPV6_ECHO_REPLY) + 8;
|
2018-02-01 08:45:09 +01:00
|
|
|
void *repBuf = reinterpret_cast<void *>(new BYTE[dwRepSize]);
|
2014-12-17 14:17:46 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
int num = pi.num;
|
|
|
|
unsigned int rtt = 0;
|
2014-12-17 14:17:46 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Parsing ip address" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
sockaddr_in6 ipDest6;
|
2016-09-09 17:17:07 +02:00
|
|
|
if (RtlIpv6StringToAddressEx(pi.ip.c_str(), &ipDest6.sin6_addr, &ipDest6.sin6_scope_id, &ipDest6.sin6_port)) {
|
|
|
|
std::wcout << pi.ip << " is not a valid ipv6 address" << '\n';
|
2014-12-17 14:17:46 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipDest6.sin6_family = AF_INET6;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
sockaddr_in6 ipSource6;
|
2014-12-17 14:17:46 +01:00
|
|
|
ipSource6.sin6_addr = in6addr_any;
|
|
|
|
ipSource6.sin6_family = AF_INET6;
|
|
|
|
ipSource6.sin6_flowinfo = 0;
|
|
|
|
ipSource6.sin6_port = 0;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Creating Icmp File" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
HANDLE hIcmp = Icmp6CreateFile();
|
2014-12-17 14:17:46 +01:00
|
|
|
if (hIcmp == INVALID_HANDLE_VALUE) {
|
|
|
|
goto die;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
IP_OPTION_INFORMATION ipInfo = { 30, 0, 0, 0, NULL };
|
|
|
|
|
|
|
|
LARGE_INTEGER frequency;
|
2014-12-17 14:17:46 +01:00
|
|
|
QueryPerformanceFrequency(&frequency);
|
2018-02-01 08:45:09 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
do {
|
2018-02-01 08:45:09 +01:00
|
|
|
LARGE_INTEGER timer1;
|
2014-12-17 14:17:46 +01:00
|
|
|
QueryPerformanceCounter(&timer1);
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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++;
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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++;
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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;
|
|
|
|
}
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
rtt += pEchoReply->RoundTripTime;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
LARGE_INTEGER timer2;
|
2014-12-17 14:17:46 +01:00
|
|
|
QueryPerformanceCounter(&timer2);
|
2018-02-01 08:45:09 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
if (((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart) < pi.timeout)
|
|
|
|
Sleep(pi.timeout - ((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart));
|
|
|
|
} while (--num);
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_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);
|
2018-02-01 08:45:09 +01:00
|
|
|
|
2015-02-17 15:54:23 +01:00
|
|
|
if (repBuf)
|
2018-02-01 08:45:09 +01:00
|
|
|
delete reinterpret_cast<BYTE *>(repBuf);
|
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
response.avg = ((double)rtt / pi.num);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
die:
|
2018-02-01 08:45:09 +01:00
|
|
|
printErrorInfo(GetLastError());
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
if (hIcmp)
|
|
|
|
IcmpCloseHandle(hIcmp);
|
2018-02-01 08:45:09 +01:00
|
|
|
|
2014-12-17 14:17:46 +01:00
|
|
|
if (repBuf)
|
2018-02-01 08:45:09 +01:00
|
|
|
delete reinterpret_cast<BYTE *>(repBuf);
|
2014-12-17 14:17:46 +01:00
|
|
|
|
|
|
|
return 3;
|
2015-09-18 13:04:09 +02:00
|
|
|
}
|
2018-02-01 08:45:09 +01:00
|
|
|
|
|
|
|
int wmain(int argc, WCHAR **argv)
|
|
|
|
{
|
|
|
|
WSADATA dat;
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &dat)) {
|
|
|
|
std::cout << "WSAStartup failed\n";
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
po::variables_map vm;
|
|
|
|
printInfoStruct printInfo;
|
|
|
|
if (parseArguments(argc, argv, vm, printInfo) != -1)
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
if (!resolveHostname(printInfo.host, printInfo.ipv6, printInfo.ip))
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
response response;
|
|
|
|
|
|
|
|
if (printInfo.ipv6) {
|
|
|
|
if (check_ping6(printInfo, response) != -1)
|
|
|
|
return 3;
|
|
|
|
} else {
|
|
|
|
if (check_ping4(printInfo, response) != -1)
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
WSACleanup();
|
|
|
|
|
|
|
|
return printOutput(printInfo, response);
|
|
|
|
}
|