2015-09-14 07:49:52 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2015 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 _DEVICE_H
|
|
|
|
#define _DEVICE_H
|
|
|
|
|
2015-11-04 07:20:08 -06:00
|
|
|
#include "../common/emulproto.pb.h"
|
2015-09-14 07:49:52 -05:00
|
|
|
#include "../common/protocol.pb.h"
|
2015-12-31 08:47:56 -06:00
|
|
|
#include "../common/uint128.h"
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
#include <QByteArray>
|
2015-11-04 07:20:08 -06:00
|
|
|
#include <QHash>
|
2018-03-16 11:29:34 -05:00
|
|
|
#include <QString>
|
2015-09-14 07:49:52 -05:00
|
|
|
|
|
|
|
class DeviceManager;
|
|
|
|
class PacketBuffer;
|
|
|
|
|
2015-11-29 09:48:31 -06:00
|
|
|
class DeviceKey: public QByteArray
|
|
|
|
{
|
|
|
|
};
|
2015-09-20 07:19:15 -05:00
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
class Device
|
|
|
|
{
|
2015-12-20 08:03:02 -06:00
|
|
|
public:
|
|
|
|
static const quint16 kVlanTpid = 0x8100;
|
|
|
|
|
2016-03-17 09:42:13 -05:00
|
|
|
enum NeighborSet {
|
|
|
|
kAllNeighbors,
|
|
|
|
kUnresolvedNeighbors
|
|
|
|
};
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
public:
|
2015-09-20 07:19:15 -05:00
|
|
|
Device(DeviceManager *deviceManager);
|
2018-07-19 08:24:01 -05:00
|
|
|
virtual ~Device() = default;
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-12-20 08:03:02 -06:00
|
|
|
void setVlan(int index, quint16 vlan, quint16 tpid = kVlanTpid);
|
2015-11-10 08:10:32 -06:00
|
|
|
quint64 mac();
|
2015-09-20 07:19:15 -05:00
|
|
|
void setMac(quint64 mac);
|
2015-11-04 07:20:08 -06:00
|
|
|
void setIp4(quint32 address, int prefixLength, quint32 gateway);
|
2015-12-31 08:47:56 -06:00
|
|
|
void setIp6(UInt128 address, int prefixLength, UInt128 gateway);
|
2015-11-11 01:35:15 -06:00
|
|
|
void getConfig(OstEmul::Device *deviceConfig);
|
2015-09-20 07:19:15 -05:00
|
|
|
QString config();
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
DeviceKey key();
|
|
|
|
void clearKey();
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2018-07-19 08:24:01 -05:00
|
|
|
virtual void receivePacket(PacketBuffer *pktBuf) = 0;
|
2015-09-14 07:49:52 -05:00
|
|
|
void transmitPacket(PacketBuffer *pktBuf);
|
|
|
|
|
2016-01-28 08:31:19 -06:00
|
|
|
void resolveGateway();
|
|
|
|
|
2015-11-04 07:20:08 -06:00
|
|
|
void resolveNeighbor(PacketBuffer *pktBuf);
|
2018-07-19 08:24:01 -05:00
|
|
|
virtual void clearNeighbors(Device::NeighborSet set) = 0;
|
|
|
|
virtual void getNeighbors(OstEmul::DeviceNeighborList *neighbors) = 0;
|
2015-11-04 07:20:08 -06:00
|
|
|
|
2015-11-10 08:10:32 -06:00
|
|
|
bool isOrigin(const PacketBuffer *pktBuf);
|
|
|
|
quint64 neighborMac(const PacketBuffer *pktBuf);
|
|
|
|
|
2018-07-19 08:24:01 -05:00
|
|
|
protected: // methods
|
|
|
|
virtual quint64 arpLookup(quint32 ip) = 0;
|
|
|
|
virtual quint64 ndpLookup(UInt128 ip) = 0;
|
|
|
|
virtual void sendArpRequest(quint32 tgtIp) = 0;
|
|
|
|
virtual void sendNeighborSolicit(UInt128 tgtIp) = 0;
|
2016-01-06 06:40:28 -06:00
|
|
|
|
2018-07-19 08:24:01 -05:00
|
|
|
protected: // data
|
2015-11-11 01:35:15 -06:00
|
|
|
static const int kMaxVlan = 4;
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
DeviceManager *deviceManager_;
|
2015-09-20 07:19:15 -05:00
|
|
|
|
|
|
|
int numVlanTags_;
|
2015-12-20 08:03:02 -06:00
|
|
|
quint32 vlan_[kMaxVlan];
|
2015-09-20 07:19:15 -05:00
|
|
|
quint64 mac_;
|
2015-12-31 08:47:56 -06:00
|
|
|
|
2016-01-01 00:29:31 -06:00
|
|
|
bool hasIp4_;
|
2015-09-20 07:19:15 -05:00
|
|
|
quint32 ip4_;
|
|
|
|
int ip4PrefixLength_;
|
2015-11-04 07:20:08 -06:00
|
|
|
quint32 ip4Gateway_;
|
2016-09-17 04:04:44 -05:00
|
|
|
quint32 ip4Mask_;
|
|
|
|
quint32 ip4Subnet_;
|
2015-09-20 07:19:15 -05:00
|
|
|
|
2016-01-01 00:29:31 -06:00
|
|
|
bool hasIp6_;
|
2015-12-31 08:47:56 -06:00
|
|
|
UInt128 ip6_;
|
|
|
|
int ip6PrefixLength_;
|
|
|
|
UInt128 ip6Gateway_;
|
2016-09-17 04:04:44 -05:00
|
|
|
UInt128 ip6Mask_;
|
|
|
|
UInt128 ip6Subnet_;
|
2015-12-31 08:47:56 -06:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
DeviceKey key_;
|
2015-11-04 07:20:08 -06:00
|
|
|
|
2018-07-19 08:24:01 -05:00
|
|
|
private: // methods
|
|
|
|
void sendArpRequest(PacketBuffer *pktBuf);
|
|
|
|
void sendNeighborSolicit(PacketBuffer *pktBuf);
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
};
|
|
|
|
|
2015-11-29 09:48:31 -06:00
|
|
|
bool operator<(const DeviceKey &a1, const DeviceKey &a2);
|
2015-09-14 07:49:52 -05:00
|
|
|
#endif
|
|
|
|
|