Macos route add reimplemented using system call
This commit is contained in:
parent
491a09b175
commit
acf878c8dd
4 changed files with 1728 additions and 13 deletions
1675
service/server/helper_route_mac.c
Normal file
1675
service/server/helper_route_mac.c
Normal file
File diff suppressed because it is too large
Load diff
4
service/server/helper_route_mac.h
Normal file
4
service/server/helper_route_mac.h
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
void mainRouteIface(int argc, char **argv);
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
#include "router_mac.h"
|
#include "router_mac.h"
|
||||||
|
#include "helper_route_mac.h"
|
||||||
|
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
|
|
||||||
|
@ -10,18 +11,44 @@ RouterMac &RouterMac::Instance()
|
||||||
|
|
||||||
bool RouterMac::routeAdd(const QString &ip, const QString &gw, QString mask)
|
bool RouterMac::routeAdd(const QString &ip, const QString &gw, QString mask)
|
||||||
{
|
{
|
||||||
// route add -host ip gw
|
int argc = 5;
|
||||||
QProcess p;
|
char **argv = new char*[argc];
|
||||||
p.setProcessChannelMode(QProcess::MergedChannels);
|
|
||||||
|
|
||||||
p.start("route", QStringList() << "add" << "-host" << ip << gw);
|
argv[0] = new char[std::string("route").length() + 1];
|
||||||
p.waitForFinished();
|
strcpy(argv[0], std::string("route").c_str());
|
||||||
qDebug().noquote() << "OUTPUT routeAdd: " + p.readAll();
|
|
||||||
bool ok = (p.exitCode() == 0);
|
argv[1] = new char[std::string("add").length() + 1];
|
||||||
if (ok) {
|
strcpy(argv[1], std::string("add").c_str());
|
||||||
m_addedRoutes.append(ip);
|
|
||||||
|
argv[2] = new char[6];
|
||||||
|
strcpy(argv[2], std::string("-host").c_str());
|
||||||
|
|
||||||
|
argv[3] = new char[ip.toStdString().length() + 1];
|
||||||
|
strcpy(argv[3], ip.toStdString().c_str());
|
||||||
|
|
||||||
|
argv[4] = new char[gw.toStdString().length() + 1];
|
||||||
|
strcpy(argv[4], gw.toStdString().c_str());
|
||||||
|
|
||||||
|
mainRouteIface(argc, argv);
|
||||||
|
|
||||||
|
for (int i = 0; i < argc; i++) {
|
||||||
|
delete [] argv[i];
|
||||||
}
|
}
|
||||||
return ok;
|
delete[] argv;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// // route add -host ip gw
|
||||||
|
// QProcess p;
|
||||||
|
// p.setProcessChannelMode(QProcess::MergedChannels);
|
||||||
|
|
||||||
|
// p.start("route", QStringList() << "add" << "-host" << ip << gw);
|
||||||
|
// p.waitForFinished();
|
||||||
|
// qDebug().noquote() << "OUTPUT routeAdd: " + p.readAll();
|
||||||
|
// bool ok = (p.exitCode() == 0);
|
||||||
|
// if (ok) {
|
||||||
|
// m_addedRoutes.append(ip);
|
||||||
|
// }
|
||||||
|
// return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
int RouterMac::routeAddList(const QString &gw, const QStringList &ips)
|
int RouterMac::routeAddList(const QString &gw, const QStringList &ips)
|
||||||
|
@ -47,13 +74,20 @@ bool RouterMac::clearSavedRoutes()
|
||||||
|
|
||||||
bool RouterMac::routeDelete(const QString &ip, const QString &gw)
|
bool RouterMac::routeDelete(const QString &ip, const QString &gw)
|
||||||
{
|
{
|
||||||
|
if (ip == "0.0.0.0") {
|
||||||
|
qDebug().noquote() << "Warning, trying to remove default route, skipping: " << ip << gw;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// route delete ip gw
|
// route delete ip gw
|
||||||
QProcess p;
|
QProcess p;
|
||||||
p.setProcessChannelMode(QProcess::MergedChannels);
|
p.setProcessChannelMode(QProcess::MergedChannels);
|
||||||
|
|
||||||
p.start("route", QStringList() << "delete" << ip << gw);
|
p.start("route", QStringList() << "delete" << ip);
|
||||||
p.waitForFinished();
|
p.waitForFinished();
|
||||||
qDebug().noquote() << "OUTPUT routeDelete: " + p.readAll();
|
// skipping gw
|
||||||
|
qDebug().noquote() << "routeDelete, skipping gw`: " << ip;
|
||||||
|
qDebug().noquote() << "OUTPUT routeDelete: " << p.readAll();
|
||||||
|
|
||||||
return p.exitCode() == 0;
|
return p.exitCode() == 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,9 +45,11 @@ LIBS += \
|
||||||
macx {
|
macx {
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
router_mac.h
|
router_mac.h
|
||||||
|
helper_route_mac.h
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
router_mac.cpp
|
router_mac.cpp \
|
||||||
|
helper_route_mac.c
|
||||||
}
|
}
|
||||||
|
|
||||||
include(../src/qtservice.pri)
|
include(../src/qtservice.pri)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue