From 2f6507f7c527247ab5767743ef0c6c7901b1e86d Mon Sep 17 00:00:00 2001 From: Syping Date: Tue, 15 May 2018 09:46:21 +0200 Subject: [PATCH] add pipe support --- mayu.cpp | 138 +++++++++++++++++++++++++++++++------------------------ mayu.h | 3 +- 2 files changed, 81 insertions(+), 60 deletions(-) diff --git a/mayu.cpp b/mayu.cpp index 489ef09..6321ade 100644 --- a/mayu.cpp +++ b/mayu.cpp @@ -16,6 +16,8 @@ *****************************************************************************/ #include +#include +#include #include #include #include @@ -144,7 +146,7 @@ double mayu::ping(const QString &host, int tries, double timeout) return -1; } #ifdef E_DEBUG - qDebug() << "IPv4 Address" << hostAddress.toString() << "found"; + QTextStream(stderr) << "IPv4 Address " << hostAddress.toString() << " found" << endl; #endif } else if (QAbstractSocket::IPv6Protocol == hostAddress.protocol()) { @@ -153,7 +155,7 @@ double mayu::ping(const QString &host, int tries, double timeout) return -1; } #ifdef E_DEBUG - qDebug() << "IPv6 Address" << hostAddress.toString() << "found"; + QTextStream(stderr) << "IPv6 Address " << hostAddress.toString() << " found" << endl; #endif } else { @@ -165,12 +167,12 @@ double mayu::ping(const QString &host, int tries, double timeout) return -1; } #ifdef E_DEBUG - qDebug() << "Hostname" << host << "found and resolved" << ipStr; + QTextStream(stderr) << "Hostname " << host << " found and resolved " << ipStr << endl; #endif } else { #ifdef E_DEBUG - qDebug() << "Hostname" << host << "not found"; + QTextStream(stderr) << "Hostname " << host << " not found" << endl; #endif ping_destroy(pingObj); return -1; @@ -202,7 +204,7 @@ double mayu::ping(const QString &host, int tries, double timeout) else { latencyString = QString::number(latency); } - QTextStream(stdout) << "Host: " << hostname << " Ping: " << latencyString << " Status: " << (pingSuccess ? "true" : "false") << endl; + QTextStream(stderr) << "Host: " << hostname << " Ping: " << latencyString << " Status: " << (pingSuccess ? "true" : "false") << endl; #endif } if (pingSuccess) { @@ -224,7 +226,7 @@ const QList mayu::resolve(const QString &host, bool emptyWhenError) if (hostAddresses.length() >= 1) { for (const QHostAddress &hostAddress : hostAddresses) { #ifdef E_DEBUG - qDebug() << "Hostname" << host << "found and resolved" << hostAddress.toString(); + QTextStream(stderr) << "Hostname " << host << " found and resolved " << hostAddress.toString() << endl; #endif mayuResult m_result; m_result.host = host; @@ -234,7 +236,7 @@ const QList mayu::resolve(const QString &host, bool emptyWhenError) } else { #ifdef E_DEBUG - qDebug() << "Hostname" << host << "not found"; + QTextStream(stderr) << "Hostname " << host << " not found" << endl; #endif if (!emptyWhenError) { mayuResult m_result; @@ -249,75 +251,73 @@ const QList mayu::resolve(const QString &host, bool emptyWhenError) void mayu::parse_hosts() { p_hostsList.clear(); + if (p_hostsFile != "-") { #ifdef PRIVILEGE_DROP_REQUIRED - if (!p_dropPrivileges()) { - p_return = 2; - return; - } -#endif - QFile hostsFile(p_hostsFile); - if (hostsFile.open(QFile::ReadOnly)) { - const QList hostsArray = hostsFile.readAll().split('\n'); - for (const QByteArray &lineArray : hostsArray) { - QString lineStr = QString::fromUtf8(lineArray).trimmed(); - if (!lineStr.isEmpty()) { - QStringList lineStrList = lineStr.split(','); - QString hostStr; - QString alternativeStr; - if (lineStrList.length() >= 1) { - hostStr = lineStrList.at(0); - lineStrList.removeAt(0); - if (lineStrList.length() >= 1) { - alternativeStr = lineStrList.join(','); // Alternative Name in Future Version - } - p_hostsList += hostStr; - } - } + if (!p_dropPrivileges()) { + p_return = 2; + return; } - hostsFile.close(); +#endif + QFile hostsFile(p_hostsFile); + if (hostsFile.open(QFile::ReadOnly)) { + const QList hostsArray = hostsFile.readAll().split('\n'); + hostsFile.close(); + p_workHosts(hostsArray); + p_hostsParsed = true; + } + else + { + QTextStream(stderr) << "Failed read hosts from " << p_hostsFile << endl; + } +#ifdef PRIVILEGE_DROP_REQUIRED + if (!p_regainPrivileges()) { + p_return = 3; + return; + } +#endif + } + else { + QByteArray b_hostsArray = QTextStream(stdin).readAll().replace("\\n", "\n").toUtf8(); + const QList hostsArray = b_hostsArray.split('\n'); + p_workHosts(hostsArray); p_hostsParsed = true; } - else - { - QTextStream(stderr) << "Failed read hosts from " << p_hostsFile << endl; - } -#ifdef PRIVILEGE_DROP_REQUIRED - if (!p_regainPrivileges()) { - p_return = 3; - return; - } -#endif } -void mayu::p_saveWork(QJsonObject jsonObject) +void mayu::p_saveWork(const QJsonObject &jsonObject) { QJsonDocument jsonDocument; jsonDocument.setObject(jsonObject); QByteArray jsonArray = jsonDocument.toJson(); + if (p_jsonFile != "-") { #ifdef PRIVILEGE_DROP_REQUIRED - if (!p_dropPrivileges()) { - p_return = 2; - return; - } + if (!p_dropPrivileges()) { + p_return = 2; + return; + } #endif - QSaveFile jsonFile(p_jsonFile); - if (jsonFile.open(QSaveFile::WriteOnly)) { - jsonFile.write(jsonArray); - if (!jsonFile.commit()) { - QTextStream(stderr) << "Failed save result to " << p_jsonFile << " because file can't be saved!" << endl; + QSaveFile jsonFile(p_jsonFile); + if (jsonFile.open(QSaveFile::WriteOnly)) { + jsonFile.write(jsonArray); + if (!jsonFile.commit()) { + QTextStream(stderr) << "Failed save result to " << p_jsonFile << " because file can't be saved!" << endl; + p_return = 1; + } + } + else { + QTextStream(stderr) << "Failed save result to " << p_jsonFile << " because file can't be opened!" << endl; p_return = 1; } +#ifdef PRIVILEGE_DROP_REQUIRED + if (!p_regainPrivileges()) { + p_return = 3; + return; + } +#endif } else { - QTextStream(stderr) << "Failed save result to " << p_jsonFile << " because file can't be opened!" << endl; - p_return = 1; + QTextStream(stdout) << jsonArray; } -#ifdef PRIVILEGE_DROP_REQUIRED - if (!p_regainPrivileges()) { - p_return = 3; - return; - } -#endif p_return = 0; } @@ -337,6 +337,26 @@ void mayu::work() } } +void mayu::p_workHosts(const QList &hostsArray) +{ + for (const QByteArray &lineArray : hostsArray) { + QString lineStr = QString::fromUtf8(lineArray).trimmed(); + if (!lineStr.isEmpty()) { + QStringList lineStrList = lineStr.split(','); + QString hostStr; + QString alternativeStr; + if (lineStrList.length() >= 1) { + hostStr = lineStrList.at(0); + lineStrList.removeAt(0); + if (lineStrList.length() >= 1) { + alternativeStr = lineStrList.join(','); // Alternative Name in Future Version + } + p_hostsList += hostStr; + } + } + } +} + #ifdef MAYU_UNIX void mayu::p_workPing() { diff --git a/mayu.h b/mayu.h index 62081d5..2c190fa 100644 --- a/mayu.h +++ b/mayu.h @@ -65,7 +65,8 @@ private: bool p_dropPrivileges(); bool p_regainPrivileges(); #endif - void p_saveWork(QJsonObject jsonObject); + void p_saveWork(const QJsonObject &jsonObject); + void p_workHosts(const QList &hostsArray); #ifdef MAYU_UNIX void p_workPing(); #endif