ostinato/common/arp.cpp
2011-01-30 17:38:03 +05:30

955 lines
30 KiB
C++

/*
Copyright (C) 2010 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/>
*/
#include <qendian.h>
#include <QHostAddress>
#include "arp.h"
ArpConfigForm::ArpConfigForm(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
opCodeCombo->setValidator(new QIntValidator(0, 0xFFFF, this));
opCodeCombo->addItem(1, "ARP Request");
opCodeCombo->addItem(2, "ARP Reply");
connect(senderHwAddrMode, SIGNAL(currentIndexChanged(int)),
SLOT(on_senderHwAddrMode_currentIndexChanged(int)));
connect(senderProtoAddrMode, SIGNAL(currentIndexChanged(int)),
SLOT(on_senderProtoAddrMode_currentIndexChanged(int)));
connect(targetHwAddrMode, SIGNAL(currentIndexChanged(int)),
SLOT(on_targetHwAddrMode_currentIndexChanged(int)));
connect(targetProtoAddrMode, SIGNAL(currentIndexChanged(int)),
SLOT(on_targetProtoAddrMode_currentIndexChanged(int)));
}
void ArpConfigForm::on_senderHwAddrMode_currentIndexChanged(int index)
{
if (index == OstProto::Arp::kFixed)
senderHwAddrCount->setDisabled(true);
else
senderHwAddrCount->setEnabled(true);
}
void ArpConfigForm::on_targetHwAddrMode_currentIndexChanged(int index)
{
if (index == OstProto::Arp::kFixed)
targetHwAddrCount->setDisabled(true);
else
targetHwAddrCount->setEnabled(true);
}
void ArpConfigForm::on_senderProtoAddrMode_currentIndexChanged(int index)
{
if (index == OstProto::Arp::kFixedHost)
{
senderProtoAddrCount->setDisabled(true);
senderProtoAddrMask->setDisabled(true);
}
else
{
senderProtoAddrCount->setEnabled(true);
senderProtoAddrMask->setEnabled(true);
}
}
void ArpConfigForm::on_targetProtoAddrMode_currentIndexChanged(int index)
{
if (index == OstProto::Arp::kFixedHost)
{
targetProtoAddrCount->setDisabled(true);
targetProtoAddrMask->setDisabled(true);
}
else
{
targetProtoAddrCount->setEnabled(true);
targetProtoAddrMask->setEnabled(true);
}
}
ArpProtocol::ArpProtocol(StreamBase *stream, AbstractProtocol *parent)
: AbstractProtocol(stream, parent)
{
_hasPayload = false;
configForm = NULL;
}
ArpProtocol::~ArpProtocol()
{
delete configForm;
}
AbstractProtocol* ArpProtocol::createInstance(StreamBase *stream,
AbstractProtocol *parent)
{
return new ArpProtocol(stream, parent);
}
quint32 ArpProtocol::protocolNumber() const
{
return OstProto::Protocol::kArpFieldNumber;
}
void ArpProtocol::protoDataCopyInto(OstProto::Protocol &protocol) const
{
protocol.MutableExtension(OstProto::arp)->CopyFrom(data);
protocol.mutable_protocol_id()->set_id(protocolNumber());
}
void ArpProtocol::protoDataCopyFrom(const OstProto::Protocol &protocol)
{
if (protocol.protocol_id().id() == protocolNumber() &&
protocol.HasExtension(OstProto::arp))
data.MergeFrom(protocol.GetExtension(OstProto::arp));
}
QString ArpProtocol::name() const
{
return QString("Address Resolution Protocol");
}
QString ArpProtocol::shortName() const
{
return QString("ARP");
}
/*!
Return the ProtocolIdType for your protocol \n
If your protocol doesn't have a protocolId field, you don't need to
reimplement this method - the base class implementation will do the
right thing
*/
#if 0
AbstractProtocol::ProtocolIdType ArpProtocol::protocolIdType() const
{
return ProtocolIdIp;
}
#endif
/*!
Return the protocolId for your protocol based on the 'type' requested \n
If not all types are valid for your protocol, handle the valid type(s)
and for the remaining fallback to the base class implementation; if your
protocol doesn't have a protocolId at all, you don't need to reimplement
this method - the base class will do the right thing
*/
quint32 ArpProtocol::protocolId(ProtocolIdType type) const
{
switch(type)
{
case ProtocolIdEth: return 0x0806;
default:break;
}
return AbstractProtocol::protocolId(type);
}
int ArpProtocol::fieldCount() const
{
return arp_fieldCount;
}
AbstractProtocol::FieldFlags ArpProtocol::fieldFlags(int index) const
{
AbstractProtocol::FieldFlags flags;
flags = AbstractProtocol::fieldFlags(index);
switch (index)
{
case arp_hwType:
case arp_protoType:
case arp_hwAddrLen:
case arp_protoAddrLen:
case arp_opCode:
case arp_senderHwAddr:
case arp_senderProtoAddr:
case arp_targetHwAddr:
case arp_targetProtoAddr:
break;
case arp_senderHwAddrMode:
case arp_senderHwAddrCount:
case arp_senderProtoAddrMode:
case arp_senderProtoAddrCount:
case arp_senderProtoAddrMask:
case arp_targetHwAddrMode:
case arp_targetHwAddrCount:
case arp_targetProtoAddrMode:
case arp_targetProtoAddrCount:
case arp_targetProtoAddrMask:
flags &= ~FrameField;
flags |= MetaField;
break;
default:
qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
index);
break;
}
return flags;
}
QVariant ArpProtocol::fieldData(int index, FieldAttrib attrib,
int streamIndex) const
{
switch (index)
{
case arp_hwType:
{
switch(attrib)
{
case FieldName:
return QString("Hardware Type");
case FieldValue:
return data.hw_type();
case FieldTextValue:
return QString("%1").arg(data.hw_type());
case FieldFrameValue:
{
QByteArray fv;
fv.resize(2);
qToBigEndian((quint16) data.hw_type(), (uchar*) fv.data());
return fv;
}
default:
break;
}
break;
}
case arp_protoType:
{
switch(attrib)
{
case FieldName:
return QString("Protocol Type");
case FieldValue:
return data.proto_type();
case FieldTextValue:
return QString("%1").arg(data.proto_type(), 4, BASE_HEX,
QChar('0'));
case FieldFrameValue:
{
QByteArray fv;
fv.resize(2);
qToBigEndian((quint16) data.proto_type(), (uchar*) fv.data());
return fv;
}
default:
break;
}
break;
}
case arp_hwAddrLen:
{
switch(attrib)
{
case FieldName:
return QString("Hardware Address Length");
case FieldValue:
return data.hw_addr_len();
case FieldTextValue:
return QString("%1").arg(data.hw_addr_len());
case FieldFrameValue:
return QByteArray(1, (char) data.hw_addr_len());
default:
break;
}
break;
}
case arp_protoAddrLen:
{
switch(attrib)
{
case FieldName:
return QString("Protocol Address Length");
case FieldValue:
return data.proto_addr_len();
case FieldTextValue:
return QString("%1").arg(data.proto_addr_len());
case FieldFrameValue:
return QByteArray(1, (char) data.proto_addr_len());
default:
break;
}
break;
}
case arp_opCode:
{
switch(attrib)
{
case FieldName:
return QString("Operation Code");
case FieldValue:
return data.op_code();
case FieldTextValue:
return QString("%1").arg(data.op_code());
case FieldFrameValue:
{
QByteArray fv;
fv.resize(2);
qToBigEndian((quint16) data.op_code(), (uchar*) fv.data());
return fv;
}
default:
break;
}
break;
}
case arp_senderHwAddr:
{
int u;
const int hwAddrStep = 1;
quint64 hwAddr = 0;
switch (data.sender_hw_addr_mode())
{
case OstProto::Arp::kFixed:
hwAddr = data.sender_hw_addr();
break;
case OstProto::Arp::kIncrement:
u = (streamIndex % data.sender_hw_addr_count()) *
hwAddrStep;
hwAddr = data.sender_hw_addr() + u;
break;
case OstProto::Arp::kDecrement:
u = (streamIndex % data.sender_hw_addr_count()) *
hwAddrStep;
hwAddr = data.sender_hw_addr() - u;
break;
default:
qWarning("Unhandled hw_addr_mode %d",
data.sender_hw_addr_mode());
}
switch(attrib)
{
case FieldName:
return QString("Sender Hardware Address");
case FieldValue:
return hwAddr;
case FieldTextValue:
return uintToHexStr(hwAddr, 6);
case FieldFrameValue:
{
QByteArray fv;
fv.resize(8);
qToBigEndian((quint64) hwAddr, (uchar*) fv.data());
fv.remove(0, 2);
return fv;
}
default:
break;
}
break;
}
case arp_senderProtoAddr:
{
int u;
quint32 subnet, host, protoAddr = 0;
switch(data.sender_proto_addr_mode())
{
case OstProto::Arp::kFixedHost:
protoAddr = data.sender_proto_addr();
break;
case OstProto::Arp::kIncrementHost:
u = streamIndex % data.sender_proto_addr_count();
subnet = data.sender_proto_addr()
& data.sender_proto_addr_mask();
host = (((data.sender_proto_addr()
& ~data.sender_proto_addr_mask()) + u)
& ~data.sender_proto_addr_mask());
protoAddr = subnet | host;
break;
case OstProto::Arp::kDecrementHost:
u = streamIndex % data.sender_proto_addr_count();
subnet = data.sender_proto_addr()
& data.sender_proto_addr_mask();
host = (((data.sender_proto_addr()
& ~data.sender_proto_addr_mask()) - u)
& ~data.sender_proto_addr_mask());
protoAddr = subnet | host;
break;
case OstProto::Arp::kRandomHost:
subnet = data.sender_proto_addr()
& data.sender_proto_addr_mask();
host = (qrand() & ~data.sender_proto_addr_mask());
protoAddr = subnet | host;
break;
default:
qWarning("Unhandled sender_proto_addr_mode = %d",
data.sender_proto_addr_mode());
}
switch(attrib)
{
case FieldName:
return QString("Sender Protocol Address");
case FieldValue:
return protoAddr;
case FieldFrameValue:
{
QByteArray fv;
fv.resize(4);
qToBigEndian((quint32) protoAddr, (uchar*) fv.data());
return fv;
}
case FieldTextValue:
return QHostAddress(protoAddr).toString();
default:
break;
}
break;
}
case arp_targetHwAddr:
{
int u;
const int hwAddrStep = 1;
quint64 hwAddr = 0;
switch (data.target_hw_addr_mode())
{
case OstProto::Arp::kFixed:
hwAddr = data.target_hw_addr();
break;
case OstProto::Arp::kIncrement:
u = (streamIndex % data.target_hw_addr_count()) *
hwAddrStep;
hwAddr = data.target_hw_addr() + u;
break;
case OstProto::Arp::kDecrement:
u = (streamIndex % data.target_hw_addr_count()) *
hwAddrStep;
hwAddr = data.target_hw_addr() - u;
break;
default:
qWarning("Unhandled hw_addr_mode %d",
data.target_hw_addr_mode());
}
switch(attrib)
{
case FieldName:
return QString("Target Hardware Address");
case FieldValue:
return hwAddr;
case FieldTextValue:
return uintToHexStr(hwAddr, 6);
case FieldFrameValue:
{
QByteArray fv;
fv.resize(8);
qToBigEndian((quint64) hwAddr, (uchar*) fv.data());
fv.remove(0, 2);
return fv;
}
default:
break;
}
break;
}
case arp_targetProtoAddr:
{
int u;
quint32 subnet, host, protoAddr = 0;
switch(data.target_proto_addr_mode())
{
case OstProto::Arp::kFixed:
protoAddr = data.target_proto_addr();
break;
case OstProto::Arp::kIncrementHost:
u = streamIndex % data.target_proto_addr_count();
subnet = data.target_proto_addr()
& data.target_proto_addr_mask();
host = (((data.target_proto_addr()
& ~data.target_proto_addr_mask()) + u)
& ~data.target_proto_addr_mask());
protoAddr = subnet | host;
break;
case OstProto::Arp::kDecrementHost:
u = streamIndex % data.target_proto_addr_count();
subnet = data.target_proto_addr()
& data.target_proto_addr_mask();
host = (((data.target_proto_addr()
& ~data.target_proto_addr_mask()) - u)
& ~data.target_proto_addr_mask());
protoAddr = subnet | host;
break;
case OstProto::Arp::kRandomHost:
subnet = data.target_proto_addr()
& data.target_proto_addr_mask();
host = (qrand() & ~data.target_proto_addr_mask());
protoAddr = subnet | host;
break;
default:
qWarning("Unhandled target_proto_addr_mode = %d",
data.target_proto_addr_mode());
}
switch(attrib)
{
case FieldName:
return QString("Target Protocol Address");
case FieldValue:
return protoAddr;
case FieldFrameValue:
{
QByteArray fv;
fv.resize(4);
qToBigEndian((quint32) protoAddr, (uchar*) fv.data());
return fv;
}
case FieldTextValue:
return QHostAddress(protoAddr).toString();
default:
break;
}
break;
}
// Meta fields
case arp_senderHwAddrMode:
switch(attrib)
{
case FieldName:
return QString("Sender Hardware Address Mode");
case FieldValue:
return data.sender_hw_addr_mode();
default:
break;
}
break;
case arp_senderHwAddrCount:
switch(attrib)
{
case FieldName:
return QString("Sender Hardware Address Count");
case FieldValue:
return data.sender_hw_addr_count();
default:
break;
}
break;
case arp_senderProtoAddrMode:
switch(attrib)
{
case FieldName:
return QString("Sender Protocol Address Mode");
case FieldValue:
return data.sender_proto_addr_mode();
default:
break;
}
break;
case arp_senderProtoAddrCount:
switch(attrib)
{
case FieldName:
return QString("Sender Protocol Address Count");
case FieldValue:
return data.sender_proto_addr_count();
default:
break;
}
break;
case arp_senderProtoAddrMask:
switch(attrib)
{
case FieldName:
return QString("Sender Protocol Address Mask");
case FieldValue:
return data.sender_proto_addr_mask();
default:
break;
}
break;
case arp_targetHwAddrMode:
switch(attrib)
{
case FieldName:
return QString("Target Hardware Address Mode");
case FieldValue:
return data.target_hw_addr_mode();
default:
break;
}
break;
case arp_targetHwAddrCount:
switch(attrib)
{
case FieldName:
return QString("Target Hardware Address Count");
case FieldValue:
return data.target_hw_addr_count();
default:
break;
}
break;
case arp_targetProtoAddrMode:
switch(attrib)
{
case FieldName:
return QString("Target Protocol Address Mode");
case FieldValue:
return data.target_proto_addr_mode();
default:
break;
}
break;
case arp_targetProtoAddrCount:
switch(attrib)
{
case FieldName:
return QString("Target Protocol Address Count");
case FieldValue:
return data.target_proto_addr_count();
default:
break;
}
break;
case arp_targetProtoAddrMask:
switch(attrib)
{
case FieldName:
return QString("Target Protocol Address Mask");
case FieldValue:
return data.target_proto_addr_mask();
default:
break;
}
break;
default:
qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
index);
break;
}
return AbstractProtocol::fieldData(index, attrib, streamIndex);
}
bool ArpProtocol::setFieldData(int index, const QVariant &value,
FieldAttrib attrib)
{
bool isOk = false;
if (attrib != FieldValue)
goto _exit;
switch (index)
{
case arp_hwType:
{
uint hwType = value.toUInt(&isOk);
if (isOk)
data.set_hw_type(hwType);
break;
}
case arp_protoType:
{
uint protoType = value.toUInt(&isOk);
if (isOk)
data.set_proto_type(protoType);
break;
}
case arp_hwAddrLen:
{
uint hwAddrLen = value.toUInt(&isOk);
if (isOk)
data.set_hw_addr_len(hwAddrLen);
break;
}
case arp_protoAddrLen:
{
uint protoAddrLen = value.toUInt(&isOk);
if (isOk)
data.set_proto_addr_len(protoAddrLen);
break;
}
case arp_opCode:
{
uint opCode = value.toUInt(&isOk);
if (isOk)
data.set_op_code(opCode);
break;
}
case arp_senderHwAddr:
{
quint64 hwAddr = value.toULongLong(&isOk);
if (isOk)
data.set_sender_hw_addr(hwAddr);
break;
}
case arp_senderHwAddrMode:
{
uint mode = value.toUInt(&isOk);
if (isOk && data.HwAddrMode_IsValid(mode))
data.set_sender_hw_addr_mode((OstProto::Arp::HwAddrMode) mode);
else
isOk = false;
break;
}
case arp_senderHwAddrCount:
{
uint count = value.toUInt(&isOk);
if (isOk)
data.set_sender_hw_addr_count(count);
break;
}
case arp_senderProtoAddr:
{
uint protoAddr = value.toUInt(&isOk);
if (isOk)
data.set_sender_proto_addr(protoAddr);
break;
}
case arp_senderProtoAddrMode:
{
uint mode = value.toUInt(&isOk);
if (isOk && data.ProtoAddrMode_IsValid(mode))
data.set_sender_proto_addr_mode(
(OstProto::Arp::ProtoAddrMode)mode);
else
isOk = false;
break;
}
case arp_senderProtoAddrCount:
{
uint count = value.toUInt(&isOk);
if (isOk)
data.set_sender_proto_addr_count(count);
break;
}
case arp_senderProtoAddrMask:
{
uint mask = value.toUInt(&isOk);
if (isOk)
data.set_sender_proto_addr_mask(mask);
break;
}
case arp_targetHwAddr:
{
quint64 hwAddr = value.toULongLong(&isOk);
if (isOk)
data.set_target_hw_addr(hwAddr);
break;
}
case arp_targetHwAddrMode:
{
uint mode = value.toUInt(&isOk);
if (isOk && data.HwAddrMode_IsValid(mode))
data.set_target_hw_addr_mode((OstProto::Arp::HwAddrMode)mode);
else
isOk = false;
break;
}
case arp_targetHwAddrCount:
{
uint count = value.toUInt(&isOk);
if (isOk)
data.set_target_hw_addr_count(count);
break;
}
case arp_targetProtoAddr:
{
uint protoAddr = value.toUInt(&isOk);
if (isOk)
data.set_target_proto_addr(protoAddr);
break;
}
case arp_targetProtoAddrMode:
{
uint mode = value.toUInt(&isOk);
if (isOk && data.ProtoAddrMode_IsValid(mode))
data.set_target_proto_addr_mode(
(OstProto::Arp::ProtoAddrMode)mode);
else
isOk = false;
break;
}
case arp_targetProtoAddrCount:
{
uint count = value.toUInt(&isOk);
if (isOk)
data.set_target_proto_addr_count(count);
break;
}
case arp_targetProtoAddrMask:
{
uint mask = value.toUInt(&isOk);
if (isOk)
data.set_target_proto_addr_mask(mask);
break;
}
default:
qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
index);
break;
}
_exit:
return isOk;
}
/*!
If your protocol has any variable fields, return true \n
Otherwise you don't need to reimplement this method - the base class always
returns false
*/
bool ArpProtocol::isProtocolFrameValueVariable() const
{
return true;
}
QWidget* ArpProtocol::configWidget()
{
if (configForm == NULL)
{
configForm = new ArpConfigForm;
loadConfigWidget();
}
return configForm;
}
void ArpProtocol::loadConfigWidget()
{
configWidget();
configForm->hwType->setText(
fieldData(arp_hwType, FieldValue).toString());
configForm->protoType->setText(uintToHexStr(
fieldData(arp_protoType, FieldValue).toUInt(), 2));
configForm->hwAddrLen->setText(
fieldData(arp_hwAddrLen, FieldValue).toString());
configForm->protoAddrLen->setText(
fieldData(arp_protoAddrLen, FieldValue).toString());
configForm->opCodeCombo->setValue(
fieldData(arp_opCode, FieldValue).toUInt());
configForm->senderHwAddr->setText(uintToHexStr(
fieldData(arp_senderHwAddr, FieldValue).toULongLong(), 6));
configForm->senderHwAddrMode->setCurrentIndex(
fieldData(arp_senderHwAddrMode, FieldValue).toUInt());
configForm->senderHwAddrCount->setText(
fieldData(arp_senderHwAddrCount, FieldValue).toString());
configForm->senderProtoAddr->setText(QHostAddress(
fieldData(arp_senderProtoAddr, FieldValue).toUInt()).toString());
configForm->senderProtoAddrMode->setCurrentIndex(
fieldData(arp_senderProtoAddrMode, FieldValue).toUInt());
configForm->senderProtoAddrCount->setText(
fieldData(arp_senderProtoAddrCount, FieldValue).toString());
configForm->senderProtoAddrMask->setText(QHostAddress(
fieldData(arp_senderProtoAddrMask, FieldValue).toUInt()).toString());
configForm->targetHwAddr->setText(uintToHexStr(
fieldData(arp_targetHwAddr, FieldValue).toULongLong(), 6));
configForm->targetHwAddrMode->setCurrentIndex(
fieldData(arp_targetHwAddrMode, FieldValue).toUInt());
configForm->targetHwAddrCount->setText(
fieldData(arp_targetHwAddrCount, FieldValue).toString());
configForm->targetProtoAddr->setText(QHostAddress(
fieldData(arp_targetProtoAddr, FieldValue).toUInt()).toString());
configForm->targetProtoAddrMode->setCurrentIndex(
fieldData(arp_targetProtoAddrMode, FieldValue).toUInt());
configForm->targetProtoAddrCount->setText(
fieldData(arp_targetProtoAddrCount, FieldValue).toString());
configForm->targetProtoAddrMask->setText(QHostAddress(
fieldData(arp_targetProtoAddrMask, FieldValue).toUInt()).toString());
}
void ArpProtocol::storeConfigWidget()
{
bool isOk;
configWidget();
setFieldData(arp_hwType, configForm->hwType->text());
setFieldData(arp_protoType, configForm->protoType->text().toUInt(
&isOk, BASE_HEX));
setFieldData(arp_hwAddrLen, configForm->hwAddrLen->text());
setFieldData(arp_protoAddrLen, configForm->protoAddrLen->text());
setFieldData(arp_opCode, configForm->opCodeCombo->currentValue());
setFieldData(arp_senderHwAddr, configForm->senderHwAddr->text()
.remove(QChar(' ')).toULongLong(&isOk, BASE_HEX));
setFieldData(arp_senderHwAddrMode,
configForm->senderHwAddrMode->currentIndex());
setFieldData(arp_senderHwAddrCount, configForm->senderHwAddrCount->text());
setFieldData(arp_senderProtoAddr, QHostAddress(
configForm->senderProtoAddr->text()).toIPv4Address());
setFieldData(arp_senderProtoAddrMode,
configForm->senderProtoAddrMode->currentIndex());
setFieldData(arp_senderProtoAddrCount,
configForm->senderProtoAddrCount->text());
setFieldData(arp_senderProtoAddrMask, QHostAddress(
configForm->senderProtoAddrMask->text()).toIPv4Address());
setFieldData(arp_targetHwAddr, configForm->targetHwAddr->text()
.remove(QChar(' ')).toULongLong(&isOk, BASE_HEX));
setFieldData(arp_targetHwAddrMode,
configForm->targetHwAddrMode->currentIndex());
setFieldData(arp_targetHwAddrCount, configForm->targetHwAddrCount->text());
setFieldData(arp_targetProtoAddr, QHostAddress(
configForm->targetProtoAddr->text()).toIPv4Address());
setFieldData(arp_targetProtoAddrMode,
configForm->targetProtoAddrMode->currentIndex());
setFieldData(arp_targetProtoAddrCount,
configForm->targetProtoAddrCount->text());
setFieldData(arp_targetProtoAddrMask, QHostAddress(
configForm->targetProtoAddrMask->text()).toIPv4Address());
}