Device Emulation (contd.): Add default values for IPv6 fields, update IPv6 gateway if addr/pfxlen changes; define and use class Ip6Edit

This commit is contained in:
Srivats P 2016-03-02 06:13:56 +05:30
parent 0edfee8cdf
commit ff757d59c6
6 changed files with 128 additions and 30 deletions

View File

@ -24,8 +24,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "emulproto.pb.h" #include "emulproto.pb.h"
#include "uint128.h" #include "uint128.h"
#include "macedit.h"
#include "ip4edit.h"
#include <QHeaderView> #include <QHeaderView>
#include <QHostAddress> #include <QHostAddress>
@ -38,9 +36,13 @@ inline UInt128 UINT128(OstEmul::Ip6Address x)
return UInt128(x.hi(), x.lo()); return UInt128(x.hi(), x.lo());
} }
inline QString IP6STR(OstEmul::Ip6Address ip) inline OstEmul::Ip6Address IP6ADDR(UInt128 x)
{ {
return QHostAddress(UINT128(ip).toArray()).toString(); OstEmul::Ip6Address ip;
ip.set_hi(x.hi64());
ip.set_lo(x.lo64());
return ip;
} }
DeviceGroupDialog::DeviceGroupDialog( DeviceGroupDialog::DeviceGroupDialog(
@ -69,6 +71,10 @@ DeviceGroupDialog::DeviceGroupDialog(
this, SLOT(updateIp4Gateway())); this, SLOT(updateIp4Gateway()));
connect(ip4PrefixLength, SIGNAL(valueChanged(const QString&)), connect(ip4PrefixLength, SIGNAL(valueChanged(const QString&)),
this, SLOT(updateIp4Gateway())); this, SLOT(updateIp4Gateway()));
connect(ip6Address, SIGNAL(textEdited(const QString&)),
this, SLOT(updateIp6Gateway()));
connect(ip6PrefixLength, SIGNAL(valueChanged(const QString&)),
this, SLOT(updateIp6Gateway()));
loadDeviceGroup(); loadDeviceGroup();
} }
@ -120,6 +126,13 @@ void DeviceGroupDialog::updateIp4Gateway()
ip4Gateway->setValue(net | 0x01); ip4Gateway->setValue(net | 0x01);
} }
void DeviceGroupDialog::updateIp6Gateway()
{
UInt128 net = ip6Address->value()
& (~UInt128(0, 0) << (128 - ip6PrefixLength->value()));
ip6Gateway->setValue(net | UInt128(0, 1));
}
void DeviceGroupDialog::loadDeviceGroup() void DeviceGroupDialog::loadDeviceGroup()
{ {
OstProto::DeviceGroup *devGrp = port_->deviceGroupByIndex(index_); OstProto::DeviceGroup *devGrp = port_->deviceGroupByIndex(index_);
@ -168,9 +181,16 @@ void DeviceGroupDialog::loadDeviceGroup()
ip4Gateway->setValue(ip4.default_gateway()); ip4Gateway->setValue(ip4.default_gateway());
OstEmul::Ip6Emulation ip6 = devGrp->GetExtension(OstEmul::ip6); OstEmul::Ip6Emulation ip6 = devGrp->GetExtension(OstEmul::ip6);
ip6Address->setText(IP6STR(ip6.address())); // ip6 fields don't have default values defined in the .proto
ip6Step->setText(IP6STR(ip6.step())); // because protobuf doesn't allow different default values for
ip6Gateway->setText(IP6STR(ip6.default_gateway())); // embedded message fields, so assign them here
// Use address 2001:0200::/64 from the RFC 5180 range
ip6Address->setValue(ip6.has_address() ?
UINT128(ip6.address()) : UInt128(0x20010200ULL << 32, 2));
ip6PrefixLength->setValue(ip6.prefix_length());
ip6Step->setValue(ip6.has_step() ? UINT128(ip6.step()) : UInt128(0, 1));
ip6Gateway->setValue(ip6.has_default_gateway() ?
UINT128(ip6.default_gateway()) : UInt128(0x20010200ULL << 32, 1));
int stk = kIpNone; int stk = kIpNone;
if (devGrp->HasExtension(OstEmul::ip4)) if (devGrp->HasExtension(OstEmul::ip4))
@ -216,25 +236,10 @@ void DeviceGroupDialog::storeDeviceGroup()
if (ipStack->currentIndex() == kIp6 if (ipStack->currentIndex() == kIp6
|| ipStack->currentIndex() == kIpDual) { || ipStack->currentIndex() == kIpDual) {
OstEmul::Ip6Emulation *ip6 = devGrp->MutableExtension(OstEmul::ip6); OstEmul::Ip6Emulation *ip6 = devGrp->MutableExtension(OstEmul::ip6);
Q_IPV6ADDR w; ip6->mutable_address()->CopyFrom(IP6ADDR(ip6Address->value()));
UInt128 x; ip6->set_prefix_length(ip6PrefixLength->value());
ip6->mutable_step()->CopyFrom(IP6ADDR(ip6Step->value()));
w = QHostAddress(ip6Address->text()).toIPv6Address(); ip6->mutable_default_gateway()->CopyFrom(IP6ADDR(ip6Gateway->value()));
x = UInt128((quint8*)&w);
ip6->mutable_address()->set_hi(x.hi64());
ip6->mutable_address()->set_lo(x.lo64());
ip6->set_prefix_length(ip4PrefixLength->value());
w = QHostAddress(ip6Gateway->text()).toIPv6Address();
x = UInt128((quint8*)&w);
ip6->mutable_default_gateway()->set_hi(x.hi64());
ip6->mutable_default_gateway()->set_lo(x.lo64());
w = QHostAddress(ip6Step->text()).toIPv6Address();
x = UInt128((quint8*)&w);
ip6->mutable_step()->set_hi(x.hi64());
ip6->mutable_step()->set_lo(x.lo64());
if (ipStack->currentIndex() == kIp6) if (ipStack->currentIndex() == kIp6)
devGrp->ClearExtension(OstEmul::ip4); devGrp->ClearExtension(OstEmul::ip4);

View File

@ -39,6 +39,7 @@ private slots:
void on_ipStack_currentIndexChanged(int index); void on_ipStack_currentIndexChanged(int index);
void updateIp4Gateway(); void updateIp4Gateway();
void updateIp6Gateway();
void loadDeviceGroup(); void loadDeviceGroup();
void storeDeviceGroup(); void storeDeviceGroup();

View File

@ -246,7 +246,7 @@
</widget> </widget>
</item> </item>
<item row="0" column="1" > <item row="0" column="1" >
<widget class="QLineEdit" name="ip6Address" /> <widget class="Ip6Edit" name="ip6Address" />
</item> </item>
<item row="0" column="2" > <item row="0" column="2" >
<widget class="QLabel" name="label_15" > <widget class="QLabel" name="label_15" >
@ -269,7 +269,7 @@
</widget> </widget>
</item> </item>
<item row="0" column="6" > <item row="0" column="6" >
<widget class="QLineEdit" name="ip6Step" /> <widget class="Ip6Edit" name="ip6Step" />
</item> </item>
<item row="1" column="0" > <item row="1" column="0" >
<widget class="QLabel" name="label_17" > <widget class="QLabel" name="label_17" >
@ -279,7 +279,7 @@
</widget> </widget>
</item> </item>
<item row="1" column="1" > <item row="1" column="1" >
<widget class="QLineEdit" name="ip6Gateway" /> <widget class="Ip6Edit" name="ip6Gateway" />
</item> </item>
<item row="0" column="3" > <item row="0" column="3" >
<widget class="QSpinBox" name="ip6PrefixLength" > <widget class="QSpinBox" name="ip6PrefixLength" >
@ -352,6 +352,11 @@
<extends>QLineEdit</extends> <extends>QLineEdit</extends>
<header>ip4edit.h</header> <header>ip4edit.h</header>
</customwidget> </customwidget>
<customwidget>
<class>Ip6Edit</class>
<extends>QLineEdit</extends>
<header>ip6edit.h</header>
</customwidget>
</customwidgets> </customwidgets>
<tabstops> <tabstops>
<tabstop>name</tabstop> <tabstop>name</tabstop>

View File

@ -66,8 +66,10 @@ message Ip6Address {
} }
message Ip6Emulation { message Ip6Emulation {
// no defaults since we can't set different default values
// for an embedded message field
optional Ip6Address address = 1; optional Ip6Address address = 1;
optional uint32 prefix_length = 2; optional uint32 prefix_length = 2 [default = 64];
optional Ip6Address default_gateway = 3; optional Ip6Address default_gateway = 3;
optional Ip6Address step = 10; optional Ip6Address step = 10;

79
common/ip6edit.h Normal file
View File

@ -0,0 +1,79 @@
/*
Copyright (C) 2016 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 _IP6_EDIT_H
#define _IP6_EDIT_H
#include "ipv6addressvalidator.h"
#include "UInt128.h"
#include <QHostAddress>
#include <QLineEdit>
class Ip6Edit: public QLineEdit
{
public:
Ip6Edit(QWidget *parent = 0);
UInt128 value();
quint64 valueHi64();
quint64 valueLo64();
void setValue(UInt128 val);
void setValue(quint64 hi, quint64 lo);
void setValue(const QString &val);
};
inline Ip6Edit::Ip6Edit(QWidget *parent)
: QLineEdit(parent)
{
setValidator(new IPv6AddressValidator(this));
}
inline UInt128 Ip6Edit::value()
{
Q_IPV6ADDR addr = QHostAddress(text()).toIPv6Address();
return UInt128((quint8*)&addr);
}
inline quint64 Ip6Edit::valueHi64()
{
return value().hi64();
}
inline quint64 Ip6Edit::valueLo64()
{
return value().lo64();
}
inline void Ip6Edit::setValue(UInt128 val)
{
setText(QHostAddress(val.toArray()).toString());
}
inline void Ip6Edit::setValue(quint64 hi, quint64 lo)
{
UInt128 ip(hi, lo);
setValue(ip);
}
inline void Ip6Edit::setValue(const QString &val)
{
setText(QHostAddress(val).toString());
}
#endif

View File

@ -43,6 +43,7 @@ public:
UInt128 operator<<(const int &shift) const; UInt128 operator<<(const int &shift) const;
UInt128 operator~() const; UInt128 operator~() const;
UInt128 operator&(const UInt128 &other) const; UInt128 operator&(const UInt128 &other) const;
UInt128 operator|(const UInt128 &other) const;
private: private:
quint64 hi_; quint64 hi_;
@ -151,6 +152,11 @@ inline UInt128 UInt128::operator&(const UInt128 &other) const
return UInt128(hi_ & other.hi_, lo_ & other.lo_); return UInt128(hi_ & other.hi_, lo_ & other.lo_);
} }
inline UInt128 UInt128::operator|(const UInt128 &other) const
{
return UInt128(hi_ | other.hi_, lo_ | other.lo_);
}
template <> inline UInt128 qFromBigEndian<UInt128>(const uchar *src) template <> inline UInt128 qFromBigEndian<UInt128>(const uchar *src)
{ {
quint64 hi, lo; quint64 hi, lo;