VPN Status
This commit is contained in:
commit
73cbd92121
8 changed files with 348 additions and 0 deletions
78
Ping.cpp
Normal file
78
Ping.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
#include <QAbstractSocket>
|
||||
#include <QHostAddress>
|
||||
#include <QTextStream>
|
||||
#include <QHostInfo>
|
||||
#include <QList>
|
||||
#include "Ping.h"
|
||||
|
||||
extern "C" {
|
||||
#include "oping.h"
|
||||
}
|
||||
|
||||
double Ping::ping(const QString &host, int tries, double timeout)
|
||||
{
|
||||
double latency;
|
||||
pingobj_t *pingObj;
|
||||
pingobj_iter_t *pingIter;
|
||||
if ((pingObj = ping_construct()) == NULL) {
|
||||
QTextStream(stderr) << "Ping construction failed " << endl;
|
||||
return -1;
|
||||
}
|
||||
if (ping_setopt(pingObj, PING_OPT_TIMEOUT, (void*)(&timeout)) < 0) {
|
||||
QTextStream(stderr) << "Setting timeout to" << timeout << " have failed" << endl;
|
||||
ping_destroy(pingObj);
|
||||
return -1;
|
||||
}
|
||||
QHostAddress hostAddress(host);
|
||||
if (QAbstractSocket::IPv4Protocol == hostAddress.protocol()) {
|
||||
if (ping_host_add(pingObj, hostAddress.toString().toStdString().c_str()) < 0) {
|
||||
ping_destroy(pingObj);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (QAbstractSocket::IPv6Protocol == hostAddress.protocol()) {
|
||||
if (ping_host_add(pingObj, hostAddress.toString().toStdString().c_str()) < 0) {
|
||||
ping_destroy(pingObj);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
QList<QHostAddress> hostAddresses = QHostInfo::fromName(host).addresses();
|
||||
if (hostAddresses.length() >= 1) {
|
||||
QString ipStr = hostAddresses.at(0).toString();
|
||||
if (ping_host_add(pingObj, ipStr.toStdString().c_str()) < 0) {
|
||||
ping_destroy(pingObj);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
ping_destroy(pingObj);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
bool hostUp = false;
|
||||
int curTry = 0;
|
||||
while (!hostUp && curTry != tries) {
|
||||
if (ping_send(pingObj) < 0) {
|
||||
QTextStream(stderr) << "Pinging host " << host << " has failed" << endl;
|
||||
ping_destroy(pingObj);
|
||||
return -1;
|
||||
}
|
||||
bool pingSuccess = false;
|
||||
for (pingIter = ping_iterator_get(pingObj); pingIter != NULL; pingIter =
|
||||
ping_iterator_next(pingIter)) {
|
||||
size_t len;
|
||||
len = sizeof(double);
|
||||
ping_iterator_get_info(pingIter, PING_INFO_LATENCY, &latency, &len);
|
||||
pingSuccess = !(latency < 0);
|
||||
}
|
||||
if (pingSuccess) {
|
||||
hostUp = true;
|
||||
}
|
||||
curTry++;
|
||||
}
|
||||
ping_destroy(pingObj);
|
||||
if (hostUp)
|
||||
return latency;
|
||||
return -1;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue