HostDev: Fixed a bunch of FIXME/TODOs

This commit is contained in:
Srivats P 2018-12-11 18:53:07 +05:30
parent 12d6713491
commit 545f740676
13 changed files with 86 additions and 39 deletions

29
common/netdefs.h Normal file
View File

@ -0,0 +1,29 @@
/*
Copyright (C) 2018 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef _NET_DEFS_H
static const quint64 kBcastMac = 0xffffffffffffULL;
static const quint16 kEthTypeIp4 = 0x0800;
static const quint16 kEthTypeArp = 0x0806;
static const quint16 kEthTypeIp6 = 0x86dd;
static const int kIp6HdrLen = 40;
static const quint8 kIpProtoIcmp6 = 58;
#endif

View File

@ -29,6 +29,7 @@ class UInt128
{
public:
UInt128();
UInt128(int lo);
UInt128(quint64 hi, quint64 lo);
UInt128(quint8 *value);
@ -36,6 +37,7 @@ public:
quint64 lo64() const;
quint8* toArray() const;
bool operator!() const;
bool operator==(const UInt128 &other) const;
bool operator!=(const UInt128 &other) const;
UInt128 operator+(const UInt128 &other) const;
@ -56,6 +58,12 @@ inline UInt128::UInt128()
// Do nothing - value will be garbage like any other uint
}
inline UInt128::UInt128(int lo)
{
hi_ = 0;
lo_ = lo;
}
inline UInt128::UInt128(quint64 hi, quint64 lo)
{
hi_ = hi;
@ -101,6 +109,11 @@ inline quint8* UInt128::toArray() const
return (quint8*)array_;
}
inline bool UInt128::operator!() const
{
return (hi_ == 0) && (lo_ == 0);
}
inline bool UInt128::operator==(const UInt128 &other) const
{
return ((hi_ == other.hi_) && (lo_ == other.lo_));

View File

@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "bsdhostdevice.h"
#include "netdefs.h"
#include "packetbuffer.h"
#include <QHostAddress>
@ -43,13 +44,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
#endif
// FIXME: also defined in emuldevice.cpp - move to common .h
static const quint64 kBcastMac = 0xffffffffffffULL;
static const quint16 kEthTypeArp = 0x0806;
static const quint16 kEthTypeIp6 = 0x86dd;
static const int kIp6HdrLen = 40;
static const quint8 kIpProtoIcmp6 = 58;
quint32 sumUInt128(UInt128 value);
BsdHostDevice::BsdHostDevice(QString portName,

View File

@ -129,7 +129,7 @@ BsdPort::~BsdPort()
}
}
void BsdPort::classInit() // FIXME: rename
void BsdPort::fetchHostNetworkInfo()
{
if (getifaddrs(&addressList_) < 0)
{
@ -153,12 +153,12 @@ void BsdPort::classInit() // FIXME: rename
}
}
void BsdPort::classDone() // FIXME: rename
void BsdPort::freeHostNetworkInfo()
{
freeifaddrs(addressList_);
addressList_ = nullptr;
routeListBuffer_.resize(0); // FIXME: does this actually free memory?
routeListBuffer_.resize(0); // release allocated memory
}
void BsdPort::init()
@ -215,10 +215,10 @@ void BsdPort::populateInterfaceInfo()
static_assert(RTA_DST == 0x1, "RTA_DST is not 0x1"); // Validate assumption
static_assert(RTA_GATEWAY == 0x2, "RTA_GATEWAY is not 0x2"); // Validate assumption
quint32 gw4 = 0;
UInt128 gw6 = UInt128(0,0); // FIXME - just 0
UInt128 gw6 = 0;
const char *p = routeListBuffer_.constData();
const char *end = p + routeListBuffer_.size();
while (!gw4 || gw6 == UInt128(0,0)) // FIXME: !UInt128
while (!gw4 || !gw6)
{
const struct rt_msghdr *rt = (const struct rt_msghdr*) p;
const struct sockaddr *sa = (const struct sockaddr*)(rt + 1); // RTA_DST = 0x1
@ -234,7 +234,7 @@ void BsdPort::populateInterfaceInfo()
((sockaddr_in*)sa)->sin_addr.s_addr); // RTA_GW = 0x2
}
}
if (gw6 == UInt128(0,0) && sa->sa_family == AF_INET6) // FIXME: !UInt128
if (!gw6 && sa->sa_family == AF_INET6)
{
if (UInt128((quint8*)(((sockaddr_in6*)sa)->sin6_addr.s6_addr))
== UInt128(0,0)) // default route ::

View File

@ -37,8 +37,8 @@ public:
virtual bool hasExclusiveControl();
virtual bool setExclusiveControl(bool exclusive);
static void classInit();
static void classDone();
static void fetchHostNetworkInfo();
static void freeHostNetworkInfo();
protected:
class StatsMonitor: public QThread

View File

@ -64,7 +64,7 @@ void DeviceManager::createHostDevices(void)
int count = ifInfo->ip4.size();
for (int i = 0; i < count; i++) {
// FIXME: Since we can't support multiple IPs with same mac,
// XXX: Since we can't support multiple IPs with same mac,
// skip link-local IP - unless it is the only one
if (((ifInfo->ip4.at(i).address & 0xffff0000) == 0xa9fe0000)
&& (count > 1))
@ -88,12 +88,12 @@ void DeviceManager::createHostDevices(void)
bcastList_.insert(bcastDevice.key(), device);
qDebug("host(add): %s", qPrintable(device->config()));
break; // FIXME: support multiple IPs with same mac
break; // TODO: support multiple IPs with same mac
}
count = ifInfo->ip6.size();
for (int i = 0; i < count; i++) {
// FIXME: Since we can't support multiple IPs with same mac,
// XXX: Since we can't support multiple IPs with same mac,
// skip link-local IP - unless it is the only one
if (((ifInfo->ip6.at(i).address.hi64() >> 48) == 0xfe80)
&& (count > 1))
@ -121,7 +121,7 @@ void DeviceManager::createHostDevices(void)
qDebug("host(update): %s", qPrintable(device->config()));
}
break; // FIXME: support multiple IPs with same mac
break; // TODO: support multiple IPs with same mac
}
}

View File

@ -4,6 +4,7 @@ QT += network script xml
QT -= gui
linux*:system(grep -q IFLA_STATS64 /usr/include/linux/if_link.h): \
DEFINES += HAVE_IFLA_STATS64
INCLUDEPATH += "../common"
INCLUDEPATH += "../rpc"
win32 {
# Support Windows Vista and above only

View File

@ -20,17 +20,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "emuldevice.h"
#include "devicemanager.h"
#include "netdefs.h"
#include "packetbuffer.h"
#include <QHostAddress>
const quint64 kBcastMac = 0xffffffffffffULL;
const quint16 kEthTypeIp4 = 0x0800;
const quint16 kEthTypeArp = 0x0806;
const quint16 kEthTypeIp6 = 0x86dd;
const int kIp6HdrLen = 40;
const quint8 kIpProtoIcmp6 = 58;
inline quint32 sumUInt128(UInt128 value)
{
quint8 *arr = value.toArray();

View File

@ -131,7 +131,7 @@ LinuxPort::~LinuxPort()
}
}
void LinuxPort::classInit() // FIXME: rename
void LinuxPort::fetchHostNetworkInfo()
{
netSock_ = nl_socket_alloc();
if (!netSock_) {
@ -160,6 +160,14 @@ void LinuxPort::classInit() // FIXME: rename
}
}
void LinuxPort::freeHostNetworkInfo()
{
nl_cache_put(routeCache_);
nl_cache_put(addressCache_);
nl_cache_put(linkCache_);
nl_socket_free(netSock_);
}
void LinuxPort::init()
{
if (!monitor_->isRunning())
@ -238,9 +246,9 @@ void LinuxPort::populateInterfaceInfo()
// Find gateways
//
quint32 gw4 = 0;
UInt128 gw6 = UInt128(0,0); // FIXME - just 0
UInt128 gw6 = 0;
for (rtnl_route *rt = routeCache_ ? (rtnl_route*) nl_cache_get_first(routeCache_) : 0;
rt && (!gw4 || gw6 == UInt128(0,0)); // FIXME: !UInt128
rt && (!gw4 || !gw6));
rt = (rtnl_route*) nl_cache_get_next(OBJ_CAST(rt))) {
if (rtnl_route_get_table(rt) != RT_TABLE_MAIN) // we want only main RTT
continue;
@ -260,7 +268,7 @@ void LinuxPort::populateInterfaceInfo()
gw4 = qFromBigEndian<quint32>(
nl_addr_get_binary_addr(rtnl_route_nh_get_gateway(nh)));
}
else if (gw6 == UInt128(0,0) && rtnl_route_get_family(rt) == AF_INET6) { // FIXME: !gw6
else if (!gw6 && rtnl_route_get_family(rt) == AF_INET6) {
gw6 = UInt128((quint8*)
nl_addr_get_binary_addr(rtnl_route_nh_get_gateway(nh)));
}

View File

@ -38,7 +38,8 @@ public:
virtual bool hasExclusiveControl();
virtual bool setExclusiveControl(bool exclusive);
static void classInit();
static void fetchHostNetworkInfo();
static void freeHostNetworkInfo();
protected:
class StatsMonitor: public QThread

View File

@ -52,11 +52,11 @@ PortManager::PortManager()
qDebug("Retrieving the device list from the local machine\n");
#if defined(Q_OS_WIN32)
WinPcapPort::populateAdapterList();
WinPcapPort::fetchHostNetworkInfo();
#elif defined(Q_OS_LINUX)
LinuxPort::classInit();
LinuxPort::fetchHostNetworkInfo();
#elif defined(Q_OS_BSD4)
BsdPort::classInit();
BsdPort::fetchHostNetworkInfo();
#endif
txRateAccuracy = rateAccuracy();
@ -114,11 +114,11 @@ PortManager::PortManager()
port->init();
#if defined(Q_OS_WIN32)
// TODO: WinPcapPort::freeAdapterList();
WinPcapPort::freeHostNetworkInfo();
#elif defined(Q_OS_LINUX)
// TODO: LinuxPort::classDone();
LinuxPort::freeHostNetworkInfo();
#elif defined(Q_OS_BSD4)
BsdPort::classDone();
BsdPort::freeHostNetworkInfo();
#endif
return;

View File

@ -309,7 +309,7 @@ void WinPcapPort::populateInterfaceInfo()
#undef SOCKET_ADDRESS_IP6
}
void WinPcapPort::populateAdapterList()
void WinPcapPort::fetchHostNetworkInfo()
{
DWORD ret;
ULONG bufLen = 15*1024; // MS recommended starting size
@ -332,4 +332,10 @@ void WinPcapPort::populateAdapterList()
}
}
void WinPcapPort::freeHostNetworkInfo()
{
free(adapterList_);
adapterList_ = NULL;
}
#endif

View File

@ -40,7 +40,8 @@ public:
virtual bool hasExclusiveControl();
virtual bool setExclusiveControl(bool exclusive);
static void populateAdapterList();
static void fetchHostNetworkInfo();
static void freeHostNetworkInfo();
protected:
class PortMonitor: public PcapPort::PortMonitor