-
Notifications
You must be signed in to change notification settings - Fork 2
/
arp.cpp
825 lines (753 loc) · 25.1 KB
/
arp.cpp
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
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:https://www.gnu.org/licenses/>
*/
#include "arp.h"
#include <QHostAddress>
#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()
ArpProtocol::ArpProtocol(StreamBase *stream, AbstractProtocol *parent)
: AbstractProtocol(stream, parent)
{
_hasPayload = false;
}
ArpProtocol::~ArpProtocol()
{
}
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 uintToMacStr(hwAddr);
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 uintToMacStr(hwAddr);
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;
}
bool ArpProtocol::isProtocolFrameValueVariable() const
{
if (fieldData(arp_senderHwAddrMode, FieldValue).toUInt()
!= uint(OstProto::Arp::kFixed)
|| fieldData(arp_senderProtoAddrMode, FieldValue).toUInt()
!= uint(OstProto::Arp::kFixed)
|| fieldData(arp_targetHwAddrMode, FieldValue).toUInt()
!= uint(OstProto::Arp::kFixed)
|| fieldData(arp_targetProtoAddrMode, FieldValue).toUInt()
!= uint(OstProto::Arp::kFixed))
return true;
return false;
}
int ArpProtocol::protocolFrameVariableCount() const
{
int count = 1;
if (fieldData(arp_senderHwAddrMode, FieldValue).toUInt()
!= uint(OstProto::Arp::kFixed))
{
count = AbstractProtocol::lcm(count,
fieldData(arp_senderHwAddrCount, FieldValue).toUInt());
}
if (fieldData(arp_senderProtoAddrMode, FieldValue).toUInt()
!= uint(OstProto::Arp::kFixed))
{
count = AbstractProtocol::lcm(count,
fieldData(arp_senderProtoAddrCount, FieldValue).toUInt());
}
if (fieldData(arp_targetHwAddrMode, FieldValue).toUInt()
!= uint(OstProto::Arp::kFixed))
{
count = AbstractProtocol::lcm(count,
fieldData(arp_targetHwAddrCount, FieldValue).toUInt());
}
if (fieldData(arp_targetProtoAddrMode, FieldValue).toUInt()
!= uint(OstProto::Arp::kFixed))
{
count = AbstractProtocol::lcm(count,
fieldData(arp_targetProtoAddrCount, FieldValue).toUInt());
}
return count;
}