Macos fixes for route functions

This commit is contained in:
pokamest 2021-05-27 15:01:15 -07:00
parent 6c74f30d79
commit 34b97bdc24
2 changed files with 36 additions and 59 deletions

View file

@ -316,6 +316,7 @@ mainRouteIface(argc, argv)
break; break;
/* NOTREACHED */ /* NOTREACHED */
} }
close(s);
fflush(stdout); fflush(stdout);
return 0; return 0;
//usage(*argv); //usage(*argv);
@ -859,17 +860,17 @@ newroute(argc, argv)
if (*cmd == 'g') if (*cmd == 'g')
return; return;
oerrno = errno; oerrno = errno;
(void) printf("%s %s %s", cmd, ishost? "host" : "net", dest); // (void) printf("%s %s %s", cmd, ishost? "host" : "net", dest);
if (*gateway) { // if (*gateway) {
(void) printf(": gateway %s", gateway); // (void) printf(": gateway %s", gateway);
if (attempts > 1 && ret == 0 && af == AF_INET) // if (attempts > 1 && ret == 0 && af == AF_INET)
(void) printf(" (%s)", inet_ntoa(so_gate.sin.sin_addr)); // (void) printf(" (%s)", inet_ntoa(so_gate.sin.sin_addr));
} // }
if (ret == 0) // if (ret == 0)
(void) printf("\n"); // (void) printf("\n");
else { // else {
(void)printf(": %s\n", route_strerror(oerrno)); // (void)printf(": %s\n", route_strerror(oerrno));
} // }
} }
static void static void
@ -1252,7 +1253,6 @@ rtmsg(cmd, flags)
#define NEXTADDR(w, u) \ #define NEXTADDR(w, u) \
if (rtm_addrs & (w)) {\ if (rtm_addrs & (w)) {\
l = ROUNDUP(u.sa.sa_len); bcopy((char *)&(u), cp, l); cp += l;\ l = ROUNDUP(u.sa.sa_len); bcopy((char *)&(u), cp, l); cp += l;\
if (verbose) sodump(&(u),"u");\
} }
errno = 0; errno = 0;
@ -1603,24 +1603,6 @@ keyword(cp)
return kt->kt_i; return kt->kt_i;
} }
void
sodump(su, which)
register sup su;
char *which;
{
switch (su->sa.sa_family) {
case AF_LINK:
(void) printf("%s: link %s; ",
which, link_ntoa(&su->sdl));
break;
case AF_INET:
(void) printf("%s: inet %s; ",
which, inet_ntoa(su->sin.sin_addr));
break;
}
(void) fflush(stdout);
}
/* States*/ /* States*/
#define VIRGIN 0 #define VIRGIN 0
#define GOTONE 1 #define GOTONE 1

View file

@ -2,6 +2,8 @@
#include "helper_route_mac.h" #include "helper_route_mac.h"
#include <QProcess> #include <QProcess>
#include <QThread>
#include <utils.h>
RouterMac &RouterMac::Instance() RouterMac &RouterMac::Instance()
{ {
@ -9,25 +11,30 @@ RouterMac &RouterMac::Instance()
return s; return s;
} }
bool RouterMac::routeAdd(const QString &ip, const QString &gw) bool RouterMac::routeAdd(const QString &ipWithSubnet, const QString &gw)
{ {
int argc = 5;
QString ip = Utils::ipAddressFromIpWithSubnet(ipWithSubnet);
QString mask = Utils::netMaskFromIpWithSubnet(ipWithSubnet);
QString cmd;
if (mask == "255.255.255.255") {
cmd = QString("route add -host %1 %2").arg(ip).arg(gw);
}
else {
cmd = QString("route add -net %1 %2 %3").arg(ip).arg(gw).arg(mask);
}
QStringList parts = cmd.split(" ");
int argc = parts.size();
char **argv = new char*[argc]; char **argv = new char*[argc];
argv[0] = new char[std::string("route").length() + 1]; for (int i = 0; i < argc; i++) {
strcpy(argv[0], std::string("route").c_str()); argv[i] = new char[parts.at(i).toStdString().length() + 1];
strcpy(argv[i], parts.at(i).toStdString().c_str());
argv[1] = new char[std::string("add").length() + 1]; }
strcpy(argv[1], std::string("add").c_str());
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); mainRouteIface(argc, argv);
@ -36,19 +43,6 @@ bool RouterMac::routeAdd(const QString &ip, const QString &gw)
} }
delete[] argv; delete[] argv;
return true; 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)
@ -56,6 +50,7 @@ int RouterMac::routeAddList(const QString &gw, const QStringList &ips)
int cnt = 0; int cnt = 0;
for (const QString &ip: ips) { for (const QString &ip: ips) {
if (routeAdd(ip, gw)) cnt++; if (routeAdd(ip, gw)) cnt++;
//QThread::msleep(10);
} }
return cnt; return cnt;
} }