Device Emulation (contd.): Add default values for IPv4 address/gateway, update gateway as address/pfxlen are edited; define and use a Ip4Edit class

This commit is contained in:
Srivats P 2016-03-01 18:48:35 +05:30
parent 9619439e6a
commit 0edfee8cdf
6 changed files with 87 additions and 18 deletions

View File

@ -25,6 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "uint128.h"
#include "macedit.h"
#include "ip4edit.h"
#include <QHeaderView>
#include <QHostAddress>
@ -37,11 +38,6 @@ inline UInt128 UINT128(OstEmul::Ip6Address x)
return UInt128(x.hi(), x.lo());
}
inline QString IP4STR(quint32 ip)
{
return QHostAddress(ip).toString();
}
inline QString IP6STR(OstEmul::Ip6Address ip)
{
return QHostAddress(UINT128(ip).toArray()).toString();
@ -69,6 +65,11 @@ DeviceGroupDialog::DeviceGroupDialog(
layout()->setSizeConstraint(QLayout::SetFixedSize);
connect(ip4Address, SIGNAL(textEdited(const QString&)),
this, SLOT(updateIp4Gateway()));
connect(ip4PrefixLength, SIGNAL(valueChanged(const QString&)),
this, SLOT(updateIp4Gateway()));
loadDeviceGroup();
}
@ -113,6 +114,12 @@ void DeviceGroupDialog::on_ipStack_currentIndexChanged(int index)
}
}
void DeviceGroupDialog::updateIp4Gateway()
{
quint32 net = ip4Address->value() & (~0 << (32 - ip4PrefixLength->value()));
ip4Gateway->setValue(net | 0x01);
}
void DeviceGroupDialog::loadDeviceGroup()
{
OstProto::DeviceGroup *devGrp = port_->deviceGroupByIndex(index_);
@ -155,9 +162,10 @@ void DeviceGroupDialog::loadDeviceGroup()
macStep->setValue(mac.step());
OstEmul::Ip4Emulation ip4 = devGrp->GetExtension(OstEmul::ip4);
ip4Address->setText(IP4STR(ip4.address()));
ip4Step->setText(IP4STR(ip4.step()));
ip4Gateway->setText(IP4STR(ip4.default_gateway()));
ip4Address->setValue(ip4.address());
ip4PrefixLength->setValue(ip4.prefix_length());
ip4Step->setValue(ip4.step());
ip4Gateway->setValue(ip4.default_gateway());
OstEmul::Ip6Emulation ip6 = devGrp->GetExtension(OstEmul::ip6);
ip6Address->setText(IP6STR(ip6.address()));
@ -196,11 +204,10 @@ void DeviceGroupDialog::storeDeviceGroup()
if (ipStack->currentIndex() == kIp4
|| ipStack->currentIndex() == kIpDual) {
OstEmul::Ip4Emulation *ip4 = devGrp->MutableExtension(OstEmul::ip4);
ip4->set_address(QHostAddress(ip4Address->text()).toIPv4Address());
ip4->set_address(ip4Address->value());
ip4->set_prefix_length(ip4PrefixLength->value());
ip4->set_default_gateway(
QHostAddress(ip4Gateway->text()).toIPv4Address());
ip4->set_step(QHostAddress(ip4Step->text()).toIPv4Address());
ip4->set_default_gateway(ip4Gateway->value());
ip4->set_step(ip4Step->value());
if (ipStack->currentIndex() == kIp4)
devGrp->ClearExtension(OstEmul::ip6);

View File

@ -38,6 +38,8 @@ private slots:
void on_vlanTagCount_valueChanged(int value);
void on_ipStack_currentIndexChanged(int index);
void updateIp4Gateway();
void loadDeviceGroup();
void storeDeviceGroup();
private:

View File

@ -181,7 +181,7 @@
</widget>
</item>
<item row="0" column="1" >
<widget class="QLineEdit" name="ip4Address" />
<widget class="Ip4Edit" name="ip4Address" />
</item>
<item row="0" column="2" >
<widget class="QLabel" name="label_12" >
@ -204,7 +204,7 @@
</widget>
</item>
<item row="0" column="6" >
<widget class="QLineEdit" name="ip4Step" />
<widget class="Ip4Edit" name="ip4Step" />
</item>
<item row="1" column="0" >
<widget class="QLabel" name="label_7" >
@ -214,7 +214,7 @@
</widget>
</item>
<item row="1" column="1" >
<widget class="QLineEdit" name="ip4Gateway" />
<widget class="Ip4Edit" name="ip4Gateway" />
</item>
<item row="0" column="3" >
<widget class="QSpinBox" name="ip4PrefixLength" >
@ -347,6 +347,11 @@
<extends>QLineEdit</extends>
<header>macedit.h</header>
</customwidget>
<customwidget>
<class>Ip4Edit</class>
<extends>QLineEdit</extends>
<header>ip4edit.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>name</tabstop>

View File

@ -51,9 +51,10 @@ message MacEmulation {
}
message Ip4Emulation {
optional uint32 address = 1;
optional uint32 prefix_length = 2;
optional uint32 default_gateway = 3;
// Use RFC 2544 reserved address for default - 198.18.0.2/24
optional uint32 address = 1 [default = 0xc6120002];
optional uint32 prefix_length = 2 [default = 24];
optional uint32 default_gateway = 3 [default = 0xc6120001];
optional uint32 step = 10 [default = 1];
// FIXME: step for gateway?

52
common/ip4edit.h Normal file
View File

@ -0,0 +1,52 @@
/*
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 _IP4_EDIT_H
#define _IP4_EDIT_H
#include <QHostAddress>
#include <QLineEdit>
class Ip4Edit: public QLineEdit
{
public:
Ip4Edit(QWidget *parent = 0);
quint32 value();
void setValue(quint32 val);
};
inline Ip4Edit::Ip4Edit(QWidget *parent)
: QLineEdit(parent)
{
setInputMask(QString("000.000.000.000; "));
}
inline quint32 Ip4Edit::value()
{
return QHostAddress(text()).toIPv4Address();
}
inline void Ip4Edit::setValue(quint32 val)
{
setText(QHostAddress(val).toString());
}
#endif

View File

@ -20,6 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#ifndef _MAC_EDIT_H
#define _MAC_EDIT_H
#include <QLineEdit>
class MacEdit: public QLineEdit
{
public: