parent
b71b87e6e9
commit
a1a6185fd6
10 changed files with 175 additions and 61 deletions
|
@ -50,7 +50,6 @@ bool RouterLinux::routeAdd(const QString &ipWithSubnet, const QString &gw, const
|
|||
|
||||
route.rt_flags = RTF_UP | RTF_GATEWAY;
|
||||
route.rt_metric = 0;
|
||||
//route.rt_dev = "ens33";
|
||||
|
||||
if (int err = ioctl(sock, SIOCADDRT, &route) < 0)
|
||||
{
|
||||
|
@ -60,6 +59,8 @@ bool RouterLinux::routeAdd(const QString &ipWithSubnet, const QString &gw, const
|
|||
<< " mask " << ((struct sockaddr_in *)&route.rt_genmask)->sin_addr.s_addr << " " << err;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_addedRoutes.append({ipWithSubnet, gw});
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -76,18 +77,23 @@ int RouterLinux::routeAddList(const QString &gw, const QStringList &ips)
|
|||
|
||||
bool RouterLinux::clearSavedRoutes()
|
||||
{
|
||||
// No need to delete routes after iface down
|
||||
return true;
|
||||
|
||||
// int cnt = 0;
|
||||
// for (const QString &ip: m_addedRoutes) {
|
||||
// if (routeDelete(ip)) cnt++;
|
||||
// }
|
||||
// return (cnt == m_addedRoutes.count());
|
||||
int temp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||
int cnt = 0;
|
||||
for (const Route &r: m_addedRoutes) {
|
||||
if (routeDelete(r.dst, r.gw, temp_sock)) cnt++;
|
||||
}
|
||||
bool ret = (cnt == m_addedRoutes.count());
|
||||
m_addedRoutes.clear();
|
||||
close(temp_sock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool RouterLinux::routeDelete(const QString &ipWithSubnet, const QString &gw, const int &sock)
|
||||
{
|
||||
#ifdef MZ_DEBUG
|
||||
qDebug().noquote() << "RouterMac::routeDelete: " << ipWithSubnet << gw;
|
||||
#endif
|
||||
|
||||
QString ip = Utils::ipAddressFromIpWithSubnet(ipWithSubnet);
|
||||
QString mask = Utils::netMaskFromIpWithSubnet(ipWithSubnet);
|
||||
|
||||
|
|
|
@ -15,6 +15,11 @@ class RouterLinux : public QObject
|
|||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
struct Route {
|
||||
QString dst;
|
||||
QString gw;
|
||||
};
|
||||
|
||||
static RouterLinux& Instance();
|
||||
|
||||
bool routeAdd(const QString &ip, const QString &gw, const int &sock);
|
||||
|
@ -31,7 +36,7 @@ private:
|
|||
RouterLinux(RouterLinux const &) = delete;
|
||||
RouterLinux& operator= (RouterLinux const&) = delete;
|
||||
|
||||
QList<QString> m_addedRoutes;
|
||||
QList<Route> m_addedRoutes;
|
||||
};
|
||||
|
||||
#endif // ROUTERLINUX_H
|
||||
|
|
|
@ -16,6 +16,10 @@ bool RouterMac::routeAdd(const QString &ipWithSubnet, const QString &gw)
|
|||
QString ip = Utils::ipAddressFromIpWithSubnet(ipWithSubnet);
|
||||
QString mask = Utils::netMaskFromIpWithSubnet(ipWithSubnet);
|
||||
|
||||
#ifdef MZ_DEBUG
|
||||
qDebug().noquote() << "RouterMac::routeAdd: " << ipWithSubnet << gw;
|
||||
#endif
|
||||
|
||||
if (!Utils::checkIPv4Format(ip) || !Utils::checkIPv4Format(gw)) {
|
||||
qCritical().noquote() << "Critical, trying to add invalid route: " << ip << gw;
|
||||
return false;
|
||||
|
@ -39,7 +43,9 @@ bool RouterMac::routeAdd(const QString &ipWithSubnet, const QString &gw)
|
|||
strcpy(argv[i], parts.at(i).toStdString().c_str());
|
||||
}
|
||||
|
||||
// TODO refactor
|
||||
mainRouteIface(argc, argv);
|
||||
m_addedRoutes.append({ipWithSubnet, gw});
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
delete [] argv[i];
|
||||
|
@ -59,14 +65,13 @@ int RouterMac::routeAddList(const QString &gw, const QStringList &ips)
|
|||
|
||||
bool RouterMac::clearSavedRoutes()
|
||||
{
|
||||
// No need to delete routes after iface down
|
||||
return true;
|
||||
|
||||
// int cnt = 0;
|
||||
// for (const QString &ip: m_addedRoutes) {
|
||||
// if (routeDelete(ip)) cnt++;
|
||||
// }
|
||||
// return (cnt == m_addedRoutes.count());
|
||||
int cnt = 0;
|
||||
for (const Route &r: m_addedRoutes) {
|
||||
if (routeDelete(r.dst, r.gw)) cnt++;
|
||||
}
|
||||
bool ret = (cnt == m_addedRoutes.count());
|
||||
m_addedRoutes.clear();
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool RouterMac::routeDelete(const QString &ipWithSubnet, const QString &gw)
|
||||
|
@ -74,6 +79,10 @@ bool RouterMac::routeDelete(const QString &ipWithSubnet, const QString &gw)
|
|||
QString ip = Utils::ipAddressFromIpWithSubnet(ipWithSubnet);
|
||||
QString mask = Utils::netMaskFromIpWithSubnet(ipWithSubnet);
|
||||
|
||||
#ifdef MZ_DEBUG
|
||||
qDebug().noquote() << "RouterMac::routeDelete: " << ipWithSubnet << gw;
|
||||
#endif
|
||||
|
||||
if (!Utils::checkIPv4Format(ip) || !Utils::checkIPv4Format(gw)) {
|
||||
qCritical().noquote() << "Critical, trying to remove invalid route: " << ip << gw;
|
||||
return false;
|
||||
|
|
|
@ -18,6 +18,11 @@ class RouterMac : public QObject
|
|||
public:
|
||||
static RouterMac& Instance();
|
||||
|
||||
struct Route {
|
||||
QString dst;
|
||||
QString gw;
|
||||
};
|
||||
|
||||
bool routeAdd(const QString &ip, const QString &gw);
|
||||
int routeAddList(const QString &gw, const QStringList &ips);
|
||||
bool clearSavedRoutes();
|
||||
|
@ -32,7 +37,7 @@ private:
|
|||
RouterMac(RouterMac const &) = delete;
|
||||
RouterMac& operator= (RouterMac const&) = delete;
|
||||
|
||||
QList<QString> m_addedRoutes;
|
||||
QList<Route> m_addedRoutes;
|
||||
};
|
||||
|
||||
#endif // ROUTERMAC_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue