1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
/*
Copyright (C) 2014 PLVision.
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/>
This module is developed by PLVision <developers@plvision.eu>
*/
#include "stp.h"
#include <QRegExp>
#define uintToMacStr(num) \
QString("%1").arg(num, 6 * 2, BASE_HEX, QChar('0')) \
.replace(QRegExp("([0-9a-fA-F]{2}\\B)"), "\\1:").toUpper()
#define ONE_BIT(pos) ((unsigned int)(1 << (pos)))
#define BITS(bit) (bit)
#define BYTES(byte) (byte)
#define BYTES_TO_BITS(byte) (byte * 8)
#define STP_LLC 0x424203
StpProtocol::StpProtocol(StreamBase *stream, AbstractProtocol *parent)
: AbstractProtocol(stream, parent)
{
}
StpProtocol::~StpProtocol()
{
}
AbstractProtocol* StpProtocol::createInstance(StreamBase *stream,
AbstractProtocol *parent)
{
return new StpProtocol(stream, parent);
}
quint32 StpProtocol::protocolNumber() const
{
return OstProto::Protocol::kStpFieldNumber;
}
void StpProtocol::protoDataCopyInto(OstProto::Protocol &protocol) const
{
protocol.MutableExtension(OstProto::stp)->CopyFrom(data_);
protocol.mutable_protocol_id()->set_id(protocolNumber());
}
void StpProtocol::protoDataCopyFrom(const OstProto::Protocol &protocol)
{
if (protocol.protocol_id().id() == protocolNumber() &&
protocol.HasExtension(OstProto::stp))
data_.MergeFrom(protocol.GetExtension(OstProto::stp));
}
QString StpProtocol::name() const
{
return QString("Spanning Tree Protocol");
}
QString StpProtocol::shortName() const
{
return QString("STP");
}
quint32 StpProtocol::protocolId(ProtocolIdType type) const
{
switch(type)
{
case ProtocolIdLlc:
return STP_LLC;
default:
break;
}
return AbstractProtocol::protocolId(type);
}
int StpProtocol::fieldCount() const
{
return stp_fieldCount;
}
AbstractProtocol::FieldFlags StpProtocol::fieldFlags(int index) const
{
AbstractProtocol::FieldFlags flags;
flags = AbstractProtocol::fieldFlags(index);
switch (index)
{
case stp_protocol_id:
case stp_version_id:
case stp_bpdu_type:
case stp_flags:
case stp_root_id:
case stp_root_path_cost:
case stp_bridge_id:
case stp_port_id:
case stp_message_age:
case stp_max_age:
case stp_hello_time:
case stp_forward_delay:
break;
default:
qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
index);
break;
}
return flags;
}
QVariant StpProtocol::fieldData(int index, FieldAttrib attrib,
int streamIndex) const
{
QString str[] = {"Topology Change", "Topology Change Acknowledgment"};
switch (index)
{
case stp_protocol_id:
{
switch (attrib)
{
case FieldName:
return QString("Protocol Identifier");
case FieldValue:
return data_.protocol_id();
case FieldTextValue:
return QString("0x%1").arg(data_.protocol_id(),
4, BASE_HEX, QChar('0'));
case FieldFrameValue:
{
QByteArray fv;
fv.resize(BYTES(2));
qToBigEndian((quint16)data_.protocol_id(),
(uchar*)fv.data());
return fv;
}
case FieldBitSize:
return BYTES_TO_BITS(2);
default:
break;
}
break;
}
case stp_version_id:
{
switch (attrib)
{
case FieldName:
return QString("Version Identifier");
case FieldValue:
return data_.protocol_version_id();
case FieldTextValue:
return QString("%1").arg(
data_.protocol_version_id());
case FieldFrameValue:
return QByteArray(1,
(char)data_.protocol_version_id());
case FieldBitSize:
return BYTES_TO_BITS(1);
default:
break;
}
break;
}
case stp_bpdu_type:
{
switch (attrib)
{
case FieldName:
return QString("BPDU Type");
case FieldValue:
return data_.bpdu_type();
case FieldTextValue:
return QString("0x%1").arg(data_.bpdu_type(),
2, BASE_HEX, QChar('0'));
case FieldFrameValue:
return QByteArray(1, (char)data_.bpdu_type());
case FieldBitSize:
return BYTES_TO_BITS(1);
default:
break;
}
break;
}
case stp_flags:
{
switch (attrib)
{
case FieldName:
return QString("BPDU Flags");
case FieldValue:
return data_.flags();
case FieldTextValue:
{
QString strTemp = "(";
if((data_.flags() & ONE_BIT(0))) strTemp += str[0] + ", ";
if((data_.flags() & ONE_BIT(7))) strTemp += str[1] + ", ";
strTemp += ")";
strTemp.replace(", )", ")");
return strTemp;
}
case FieldFrameValue:
return QByteArray(1, (char)data_.flags());
case FieldBitSize:
return BYTES_TO_BITS(1);
default:
break;
}
break;
}
case stp_root_id:
{
switch (attrib)
{
case FieldName:
return QString("Root Identifier");
case FieldValue:
return (quint64) data_.root_id();
case FieldTextValue:
{
// Root ID contain two value:
// Root ID Priority(first 2 bytes)
// and Root ID MAC (last 6 bytes). (IEEE802.1D-2008)
quint16 priority = (
data_.root_id() & 0xFFFF000000000000ULL) >> (BYTES_TO_BITS(6));
quint64 mac = data_.root_id() & 0x0000FFFFFFFFFFFFULL;
return QString("Priority: %1 / MAC: %2")
.arg(QString::number(priority),
uintToMacStr(mac));
}
case FieldFrameValue:
{
QByteArray fv;
fv.resize(BYTES(8));
qToBigEndian((quint64)data_.root_id(), (uchar*)fv.data());
return fv;
}
case FieldBitSize:
return BYTES_TO_BITS(8);
default:
break;
}
break;
}
case stp_root_path_cost:
{
switch (attrib)
{
case FieldName:
return QString("Root Path Cost");
case FieldValue:
return data_.root_path_cost();
case FieldTextValue:
return QString("%1").arg(data_.root_path_cost());
case FieldFrameValue:
{
QByteArray fv;
fv.resize(BYTES(4));
qToBigEndian(data_.root_path_cost(), (uchar*)fv.data());
return fv;
}
case FieldBitSize:
return BYTES_TO_BITS(4);
default:
break;
}
break;
}
case stp_bridge_id:
{
switch (attrib)
{
case FieldName:
return QString("Bridge Identifier");
case FieldValue:
return (quint64) data_.bridge_id();
case FieldTextValue:
{
// Bridge ID contain two value:
// Bridge ID Priority(first 2 bytes)
// and Bridge ID MAC (last 6 bytes). (IEEE802.1D-2008)
quint16 priority = (data_.bridge_id() & 0xFFFF000000000000ULL
) >> (BYTES_TO_BITS(6));
quint64 mac = data_.bridge_id() & 0x0000FFFFFFFFFFFFULL;
return QString("Priority: %1 / MAC: %2").arg(QString::number(priority),
uintToMacStr(mac));
}
case FieldFrameValue:
{
QByteArray fv;
fv.resize(BYTES(8));
qToBigEndian((quint64)data_.bridge_id(), (uchar*)fv.data());
return fv;
}
case FieldBitSize:
return BYTES_TO_BITS(8);
default:
break;
}
break;
}
case stp_port_id:
{
switch (attrib)
{
case FieldName:
return QString("Port Identifier");
case FieldValue:
return data_.port_id();
case FieldTextValue:
return QString("0x%1").arg(data_.port_id(), 4,
BASE_HEX, QChar('0'));
case FieldFrameValue:
{
QByteArray fv;
fv.resize(BYTES(2));
qToBigEndian((quint16)data_.port_id(), (uchar*)fv.data());
return fv;
}
case FieldBitSize:
return BYTES_TO_BITS(2);
default:
break;
}
break;
}
case stp_message_age:
{
switch (attrib)
{
case FieldName:
return QString("Message Age");
case FieldValue:
return data_.message_age();
case FieldTextValue:
return QString("%1").arg(data_.message_age());
case FieldFrameValue:
{
QByteArray fv;
fv.resize(BYTES(2));
qToBigEndian((quint16)(data_.message_age()),
(uchar*)fv.data());
return fv;
}
case FieldBitSize:
return BYTES_TO_BITS(2);
default:
break;
}
break;
}
case stp_max_age:
{
switch (attrib)
{
case FieldName:
return QString("Max Age");
case FieldValue:
return data_.max_age();
case FieldTextValue:
return QString("%1").arg(data_.max_age());
case FieldFrameValue:
{
QByteArray fv;
fv.resize(BYTES(2));
qToBigEndian((quint16)data_.max_age(), (uchar*)fv.data());
return fv;
}
case FieldBitSize:
return BYTES_TO_BITS(2);
default:
break;
}
break;
}
case stp_hello_time:
{
switch (attrib)
{
case FieldName:
return QString("Hello Time");
case FieldValue:
return data_.hello_time();
case FieldTextValue:
return QString("%1").arg(data_.hello_time());
case FieldFrameValue:
{
QByteArray fv;
fv.resize(BYTES(2));
qToBigEndian((quint16)data_.hello_time(), (uchar*)fv.data());
return fv;
}
case FieldBitSize:
return BYTES_TO_BITS(2);
default:
break;
}
break;
}
case stp_forward_delay:
{
switch (attrib)
{
case FieldName:
return QString("Forward Delay");
case FieldValue:
return data_.forward_delay();
case FieldTextValue:
return QString("%1").arg(data_.forward_delay());
case FieldFrameValue:
{
QByteArray fv;
fv.resize(BYTES(2));
qToBigEndian((quint16)data_.forward_delay(),
(uchar*)fv.data());
return fv;
}
case FieldBitSize:
return BYTES_TO_BITS(2);
default:
break;
}
break;
}
default:
break;
}
return AbstractProtocol::fieldData(index, attrib, streamIndex);
}
bool StpProtocol::setFieldData(int index, const QVariant &value,
FieldAttrib attrib)
{
bool isOk = false;
if (attrib != FieldValue)
return isOk;
switch (index)
{
case stp_protocol_id:
{
quint16 protoId = value.toUInt(&isOk);
if (isOk)
data_.set_protocol_id(protoId);
break;
}
case stp_version_id:
{
quint8 versionId = value.toUInt(&isOk);
if (isOk)
data_.set_protocol_version_id(versionId);
break;
}
case stp_bpdu_type:
{
quint8 bpdu = value.toUInt(&isOk);
if (isOk)
data_.set_bpdu_type(bpdu);
break;
}
case stp_flags:
{
quint8 flags = value.toUInt(&isOk);
if (isOk)
data_.set_flags(flags);
break;
}
case stp_root_id:
{
quint64 rootId = value.toULongLong(&isOk);
if (isOk)
data_.set_root_id(rootId);
break;
}
case stp_root_path_cost:
{
quint32 pathCost = value.toUInt(&isOk);
if (isOk)
data_.set_root_path_cost(pathCost);
break;
}
case stp_bridge_id:
{
quint64 bridgeId = value.toULongLong(&isOk);
if (isOk)
data_.set_bridge_id(bridgeId);
break;
}
case stp_port_id:
{
quint32 port_id = value.toUInt(&isOk);
if (isOk)
data_.set_port_id(port_id);
break;
}
case stp_message_age:
{
quint32 messageAge = value.toUInt(&isOk);
if (isOk)
data_.set_message_age(messageAge);
break;
}
case stp_max_age:
{
quint32 maxAge = value.toUInt(&isOk);
if (isOk)
data_.set_max_age(maxAge);
break;
}
case stp_hello_time:
{
quint32 helloTime = value.toUInt(&isOk);
if (isOk)
data_.set_hello_time(helloTime);
break;
}
case stp_forward_delay:
{
quint32 forwardDelay = value.toUInt(&isOk);
if (isOk)
data_.set_forward_delay(forwardDelay);
break;
}
default:
break;
}
return isOk;
}
|