Device Emulation (contd.): Assign a random mac address to a DeviceGroup at alloc time to ensure unique device keys when enumerating devices from device groups
This commit is contained in:
parent
600bdc1946
commit
db8ad92738
@ -250,22 +250,8 @@ void DeviceGroupDialog::loadDeviceGroup()
|
||||
devicePerVlanCount->setValue(devGrp->device_count());
|
||||
|
||||
OstEmul::MacEmulation mac = devGrp->GetExtension(OstEmul::mac);
|
||||
if (!mac.has_address()) {
|
||||
// Mac address as per RFC 4814 Sec 4.2
|
||||
// (RR & 0xFC):PP:PP:RR:RR:RR
|
||||
// where RR is a random number, PP:PP is 1-indexed port index
|
||||
// NOTE: although qrand() return type is a int, the max value
|
||||
// is RAND_MAX (stdlib.h) which is often 16-bit only, so we
|
||||
// use two random numbers
|
||||
quint32 r1 = qrand(), r2 = qrand();
|
||||
quint64 mac;
|
||||
mac = quint64(r1 & 0xfc00) << 32
|
||||
| quint64(port_->id() + 1) << 24
|
||||
| quint64((r1 & 0xff) << 16 | (r2 & 0xffff));
|
||||
macAddress->setValue(mac);
|
||||
}
|
||||
else
|
||||
macAddress->setValue(mac.address());
|
||||
Q_ASSERT(mac.has_address());
|
||||
macAddress->setValue(mac.address());
|
||||
macStep->setValue(mac.step());
|
||||
|
||||
OstEmul::Ip4Emulation ip4 = devGrp->GetExtension(OstEmul::ip4);
|
||||
|
@ -25,8 +25,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
#include "settings.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDateTime>
|
||||
#include <QFile>
|
||||
#include <QSettings>
|
||||
#include <QtGlobal>
|
||||
|
||||
#include <google/protobuf/stubs/common.h>
|
||||
|
||||
@ -68,6 +70,7 @@ int main(int argc, char* argv[])
|
||||
appSettings->value(kAwkPathKey, kAwkPathDefaultValue).toString());
|
||||
|
||||
Preferences::initDefaults();
|
||||
qsrand(QDateTime::currentDateTime().toTime_t());
|
||||
|
||||
mainWindow = new MainWindow;
|
||||
mainWindow->show();
|
||||
|
@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
#include "port.h"
|
||||
|
||||
#include "abstractfileformat.h"
|
||||
#include "emulation.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QMainWindow>
|
||||
@ -702,7 +703,7 @@ bool Port::newDeviceGroupAt(int index, const OstProto::DeviceGroup *deviceGroup)
|
||||
if (index < 0 || index > numDeviceGroups())
|
||||
return false;
|
||||
|
||||
OstProto::DeviceGroup *devGrp = new OstProto::DeviceGroup;
|
||||
OstProto::DeviceGroup *devGrp = newDeviceGroup(id());
|
||||
|
||||
if (!devGrp) {
|
||||
qWarning("failed allocating a new device group");
|
||||
@ -738,7 +739,7 @@ bool Port::insertDeviceGroup(uint deviceGroupId)
|
||||
return false;
|
||||
}
|
||||
|
||||
devGrp = new OstProto::DeviceGroup;
|
||||
devGrp = newDeviceGroup(id());
|
||||
devGrp->mutable_device_group_id()->set_id(deviceGroupId);
|
||||
deviceGroups_.append(devGrp);
|
||||
|
||||
|
55
common/emulation.h
Normal file
55
common/emulation.h
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
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 _EMULATION_H
|
||||
#define _EMULATION_H
|
||||
|
||||
#include "emulproto.pb.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
|
||||
static inline OstProto::DeviceGroup* newDeviceGroup(uint portId)
|
||||
{
|
||||
OstProto::DeviceGroup *devGrp = new OstProto::DeviceGroup;
|
||||
|
||||
// To ensure that DeviceGroup have a unique key, we assign
|
||||
// a random mac address upon creation; ideally, it would
|
||||
// have been good to inherit OstProto::DeviceGroup and define
|
||||
// a new constructor, but ProtoBuf forbids against inheriting
|
||||
// generated classes, so we use this helper function instead
|
||||
//
|
||||
// Create a mac address as per RFC 4814 Sec 4.2
|
||||
// (RR & 0xFC):PP:PP:RR:RR:RR
|
||||
// where RR is a random number, PP:PP is 1-indexed port index
|
||||
// NOTE: although qrand() return type is a int, the max value
|
||||
// is RAND_MAX (stdlib.h) which is often 16-bit only, so we
|
||||
// use two random numbers
|
||||
quint32 r1 = qrand(), r2 = qrand();
|
||||
quint64 mac;
|
||||
mac = quint64(r1 & 0xfc00) << 32
|
||||
| quint64(portId + 1) << 24
|
||||
| quint64((r1 & 0xff) << 16 | (r2 & 0xffff));
|
||||
devGrp->MutableExtension(OstEmul::mac)->set_address(mac);
|
||||
|
||||
return devGrp;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -28,6 +28,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
#include <google/protobuf/service.h>
|
||||
#include <google/protobuf/io/zero_copy_stream_impl.h>
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QHostAddress>
|
||||
#include <QString>
|
||||
#include <QTcpSocket>
|
||||
@ -82,6 +83,7 @@ void RpcConnection::start()
|
||||
return;
|
||||
}
|
||||
qDebug("clientSock Thread = %p", clientSock->thread());
|
||||
qsrand(QDateTime::currentDateTime().toTime_t());
|
||||
|
||||
connId.setLocalData(new QString(id.arg(clientSock->peerAddress().toString())
|
||||
.arg(clientSock->peerPort())));
|
||||
|
@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
#include "abstractport.h"
|
||||
#include "device.h"
|
||||
#include "../common/emulation.h"
|
||||
#include "packetbuffer.h"
|
||||
|
||||
#include "../common/emulproto.pb.h"
|
||||
@ -93,7 +94,7 @@ bool DeviceManager::addDeviceGroup(uint deviceGroupId)
|
||||
return false;
|
||||
}
|
||||
|
||||
deviceGroup = new OstProto::DeviceGroup;
|
||||
deviceGroup = newDeviceGroup(port_->id());
|
||||
deviceGroup->mutable_device_group_id()->set_id(deviceGroupId);
|
||||
deviceGroupList_.insert(deviceGroupId, deviceGroup);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user