/
TcpPacket.cs
795 lines (691 loc) · 31.8 KB
/
TcpPacket.cs
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
/*
This file is part of PacketDotNet.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/*
* Copyright 2010 Chris Morgan <chmorgan@gmail.com>
*/
using System;
using System.Collections.Generic;
using System.Text;
using PacketDotNet.Tcp;
using PacketDotNet.Utils;
using PacketDotNet.Utils.Converters;
#if DEBUG
using log4net;
using System.Reflection;
#endif
namespace PacketDotNet;
/// <summary>
/// TcpPacket
/// See: http://en.wikipedia.org/wiki/Transmission_Control_Protocol
/// </summary>
public sealed class TcpPacket : TransportPacket
{
#if DEBUG
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
#else
// NOTE: No need to warn about lack of use, the compiler won't
// put any calls to 'log' here but we need 'log' to exist to compile
#pragma warning disable 0169, 0649
private static readonly ILogInactive Log;
#pragma warning restore 0169, 0649
#endif
// 20 bytes is the smallest tcp header
public const int HeaderMinimumLength = 20;
/// <summary>
/// Create a new TCP packet from values
/// </summary>
public TcpPacket(ushort sourcePort, ushort destinationPort)
{
Log.Debug("");
// allocate memory for this packet
var length = TcpFields.HeaderLength;
var headerBytes = new byte[length];
Header = new ByteArraySegment(headerBytes, 0, length);
// make this packet valid
DataOffset = length / 4;
// set instance values
SourcePort = sourcePort;
DestinationPort = destinationPort;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="byteArraySegment">
/// A <see cref="ByteArraySegment" />
/// </param>
public TcpPacket(ByteArraySegment byteArraySegment)
{
Log.Debug("");
// set the header field, header field values are retrieved from this byte array
// ReSharper disable once UseObjectOrCollectionInitializer
Header = new ByteArraySegment(byteArraySegment);
Header.Length = DataOffset * 4;
// NOTE: we update the Length field AFTER the header field because
// we need the header to be valid to retrieve the value of DataOffset
// store the payload bytes
PayloadPacketOrData = new LazySlim<PacketOrByteArraySegment>(() =>
{
PacketOrByteArraySegment result;
var payload = Header.NextSegment();
if (CustomPayloadDecoder != null && (result = CustomPayloadDecoder(payload, this)) != null)
{
Log.Debug("Use CustomPayloadDecoder");
return result;
}
result = new PacketOrByteArraySegment { ByteArraySegment = payload };
return result;
});
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="byteArraySegment">
/// A <see cref="ByteArraySegment" />
/// </param>
/// <param name="parentPacket">
/// A <see cref="Packet" />
/// </param>
public TcpPacket
(
ByteArraySegment byteArraySegment,
Packet parentPacket)
{
Log.Debug("");
// set the header field, header field values are retrieved from this byte array
// ReSharper disable once UseObjectOrCollectionInitializer
Header = new ByteArraySegment(byteArraySegment);
// NOTE: we update the Length field AFTER the header field because
// we need the header to be valid to retrieve the value of DataOffset
Header.Length = DataOffset * 4;
// store the payload bytes
PayloadPacketOrData = new LazySlim<PacketOrByteArraySegment>(() =>
{
PacketOrByteArraySegment result;
var payload = Header.NextSegment();
if (CustomPayloadDecoder != null && (result = CustomPayloadDecoder(payload, this)) != null)
{
Log.Debug("Use CustomPayloadDecoder");
return result;
}
result = new PacketOrByteArraySegment { ByteArraySegment = payload };
// if the parent packet is an IPv4Packet we need to adjust
// the payload length because it is possible for us to have
// X bytes of data but only (X - Y) bytes are actually valid
if (ParentPacket is IPv4Packet ipv4Parent)
{
// actual total length (tcp header + tcp payload)
var ipPayloadTotalLength = ipv4Parent.TotalLength - (ipv4Parent.HeaderLength * 4);
Log.DebugFormat("ipv4Parent.TotalLength {0}, ipv4Parent.HeaderLength {1}",
ipv4Parent.TotalLength,
ipv4Parent.HeaderLength * 4);
var newTcpPayloadLength = ipPayloadTotalLength - Header.Length;
Log.DebugFormat("Header.Length {0}, Current payload length: {1}, new payload length {2}",
Header.Length,
result.ByteArraySegment.Length,
newTcpPayloadLength);
// the length of the payload is the total payload length
// above, minus the length of the tcp header
result.ByteArraySegment.Length = newTcpPayloadLength;
DecodePayload(result);
}
return result;
});
Log.DebugFormat("ParentPacket.GetType() {0}", parentPacket.GetType());
ParentPacket = parentPacket;
}
/// <summary>Gets or sets the acknowledgment (ACK) flag, which indicates if the <see cref="AcknowledgmentNumber" /> is valid.</summary>
public bool Acknowledgment
{
get => (Flags & TcpFields.TCPAckMask) != 0;
set => SetFlag(value, TcpFields.TCPAckMask);
}
/// <summary>Fetch the packet acknowledgment number.</summary>
public uint AcknowledgmentNumber
{
get => EndianBitConverter.Big.ToUInt32(Header.Bytes,
Header.Offset + TcpFields.AcknowledgmentNumberPosition);
set => EndianBitConverter.Big.CopyBytes(value,
Header.Bytes,
Header.Offset + TcpFields.AcknowledgmentNumberPosition);
}
/// <value>
/// Tcp checksum field value of type UInt16
/// </value>
public override ushort Checksum
{
get => EndianBitConverter.Big.ToUInt16(Header.Bytes,
Header.Offset + TcpFields.ChecksumPosition);
set
{
var v = value;
EndianBitConverter.Big.CopyBytes(v,
Header.Bytes,
Header.Offset + TcpFields.ChecksumPosition);
}
}
/// <summary>Fetch ascii escape sequence of the color associated with this packet type.</summary>
public override string Color => AnsiEscapeSequences.Yellow;
/// <value>
/// Congestion Window Reduced flag
/// </value>
public bool CongestionWindowReduced
{
get => (Flags & TcpFields.CongestionWindowReducedMask) != 0;
set => SetFlag(value, TcpFields.CongestionWindowReducedMask);
}
/// <summary>The size of the tcp header in 32bit words </summary>
public int DataOffset
{
get
{
var dataOffset = (byte) ((DataOffsetAndFlags >> 12) & 0xF);
return dataOffset;
}
set
{
var dataOffset = DataOffsetAndFlags;
dataOffset = (ushort) ((dataOffset & 0x0FFF) | ((value << 12) & 0xF000));
// write the value back
DataOffsetAndFlags = dataOffset;
}
}
/// <summary>Fetches the port number on the destination host.</summary>
public override ushort DestinationPort
{
get => EndianBitConverter.Big.ToUInt16(Header.Bytes,
Header.Offset + TcpFields.DestinationPortPosition);
set
{
var v = value;
EndianBitConverter.Big.CopyBytes(v,
Header.Bytes,
Header.Offset + TcpFields.DestinationPortPosition);
}
}
/// <value>
/// Gets or sets the explicit congestion notification echo (ECE) flag, which indicates that the TCP peer is ECN capable if the <see cref="Synchronize" /> flag is <c>true</c>,
/// or otherwise that a packet with Congestion Experienced flag set in the IP header was received during normal transmission.
/// </value>
public bool ExplicitCongestionNotificationEcho
{
get => (Flags & TcpFields.ExplicitCongestionNotificationEchoMask) != 0;
set => SetFlag(value, TcpFields.ExplicitCongestionNotificationEchoMask);
}
/// <summary>Gets or sets the finished (FIN) flag, which indicates whether the sender has finished sending.</summary>
public bool Finished
{
get => (Flags & TcpFields.FinishedMask) != 0;
set => SetFlag(value, TcpFields.FinishedMask);
}
/// <summary>
/// The flags consisting of 9 bits.
/// </summary>
public ushort Flags
{
get
{
var flags = DataOffsetAndFlags & 0x1FF;
return (ushort) flags;
}
set
{
var flags = DataOffsetAndFlags;
flags = (ushort) ((flags & 0xFE00) | (value & 0x1FF));
DataOffsetAndFlags = flags;
}
}
/// <summary>
/// Gets or sets the nonce sum (NS) flag.
/// </summary>
public bool NonceSum
{
get => (Flags & TcpFields.NonceSumMask) != 0;
set => SetFlag(value, TcpFields.NonceSumMask);
}
/// <summary>
/// Gets the bytes that represent the TCP options.
/// </summary>
public byte[] Options
{
get
{
if (Urgent)
ThrowHelper.ThrowNotImplementedException(ExceptionDescription.UrgentPointerSet);
var optionsOffset = TcpFields.UrgentPointerPosition + TcpFields.UrgentPointerLength;
var optionsLength = (DataOffset * 4) - optionsOffset;
var optionBytes = new byte[optionsLength];
Array.Copy(Header.Bytes,
Header.Offset + optionsOffset,
optionBytes,
0,
optionsLength);
return optionBytes;
}
set
{
var optionsOffset = Header.Offset + TcpFields.UrgentPointerPosition + TcpFields.UrgentPointerLength;
Array.Copy(value, 0, Header.Bytes, optionsOffset, value.Length);
}
}
/// <summary>
/// Contains the Options list attached to the TCP header
/// </summary>
public List<TcpOption> OptionsCollection
{
get => ParseOptions(OptionsSegment);
set
{
var optionsOffset = TcpFields.UrgentPointerPosition + TcpFields.UrgentPointerLength;
foreach (var option in value)
{
var optionBytes = option.Bytes;
Array.Copy(optionBytes, 0, Header.Bytes, Header.Offset + optionsOffset, optionBytes.Length);
optionsOffset += optionBytes.Length;
}
}
}
/// <summary>
/// Gets the TCP options as a <see cref="ByteArraySegment" />.
/// </summary>
/// <exception cref="NotImplementedException">Urgent == true not implemented yet</exception>
public ByteArraySegment OptionsSegment
{
get
{
if (Urgent)
ThrowHelper.ThrowNotImplementedException(ExceptionDescription.UrgentPointerSet);
var optionsOffset = TcpFields.UrgentPointerPosition + TcpFields.UrgentPointerLength;
var optionsLength = (DataOffset * 4) - optionsOffset;
return new ByteArraySegment(Header.Bytes, Header.Offset + optionsOffset, optionsLength);
}
}
/// <summary>
/// Gets or sets the push (PSH) flag, which indicates the receiver should pass the data to the application as soon as possible.
/// </summary>
public bool Push
{
get => (Flags & TcpFields.PushMask) != 0;
set => SetFlag(value, TcpFields.PushMask);
}
/// <summary>
/// Gets or sets the reset (RST) flag, which indicates the session should be reset between
/// the sender and the receiver.
/// </summary>
public bool Reset
{
get => (Flags & TcpFields.ResetMask) != 0;
set => SetFlag(value, TcpFields.ResetMask);
}
/// <summary>Fetch the packet sequence number.</summary>
public uint SequenceNumber
{
get => EndianBitConverter.Big.ToUInt32(Header.Bytes,
Header.Offset + TcpFields.SequenceNumberPosition);
set => EndianBitConverter.Big.CopyBytes(value,
Header.Bytes,
Header.Offset + TcpFields.SequenceNumberPosition);
}
/// <summary>Fetch the port number on the source host.</summary>
public override ushort SourcePort
{
get => EndianBitConverter.Big.ToUInt16(Header.Bytes,
Header.Offset + TcpFields.SourcePortPosition);
set
{
var v = value;
EndianBitConverter.Big.CopyBytes(v,
Header.Bytes,
Header.Offset + TcpFields.SourcePortPosition);
}
}
/// <summary>
/// Gets or sets the synchronize (SYN flag), which indicates the sequence numbers should be synchronized between the sender and receiver to initiate a connection.
/// </summary>
public bool Synchronize
{
get => (Flags & TcpFields.SynchronizationMask) != 0;
set => SetFlag(value, TcpFields.SynchronizationMask);
}
/// <summary>Gets or sets the urgent (URG) flag, which indicates if the <see cref="UrgentPointer" /> is valid.</summary>
public bool Urgent
{
get => (Flags & TcpFields.UrgentMask) != 0;
set => SetFlag(value, TcpFields.UrgentMask);
}
/// <summary>Fetch the urgent pointer.</summary>
public int UrgentPointer
{
get => EndianBitConverter.Big.ToInt16(Header.Bytes,
Header.Offset + TcpFields.UrgentPointerPosition);
set
{
var v = (short) value;
EndianBitConverter.Big.CopyBytes(v,
Header.Bytes,
Header.Offset + TcpFields.UrgentPointerPosition);
}
}
/// <summary>Check if the TCP packet is valid, checksum-wise.</summary>
public bool ValidChecksum
{
get
{
// IPv6 has no checksum so only the TCP checksum needs evaluation
if (ParentPacket is IPv6Packet)
return ValidTcpChecksum;
// For IPv4 both the IP layer and the TCP layer contain checksums
return ((IPv4Packet) ParentPacket).ValidIPChecksum && ValidTcpChecksum;
}
}
/// <summary>
/// Gets or sets a value indicating whether the TCP checksum is valid.
/// </summary>
public bool ValidTcpChecksum
{
get
{
Log.Debug("ValidTcpChecksum");
var result = IsValidChecksum(TransportChecksumOption.IncludePseudoIPHeader);
Log.DebugFormat("ValidTcpChecksum {0}", result);
return result;
}
}
/// <summary>
/// The size of the receive window, which specifies the number of
/// bytes (beyond the sequence number in the acknowledgment field) that
/// the receiver is currently willing to receive.
/// </summary>
public ushort WindowSize
{
get => EndianBitConverter.Big.ToUInt16(Header.Bytes,
Header.Offset + TcpFields.WindowSizePosition);
set => EndianBitConverter.Big.CopyBytes(value,
Header.Bytes,
Header.Offset + TcpFields.WindowSizePosition);
}
/// <summary>
/// Gets or sets the data offset and flags.
/// </summary>
private ushort DataOffsetAndFlags
{
get => EndianBitConverter.Big.ToUInt16(Header.Bytes,
Header.Offset + TcpFields.DataOffsetAndFlagsPosition);
set => EndianBitConverter.Big.CopyBytes(value,
Header.Bytes,
Header.Offset + TcpFields.DataOffsetAndFlagsPosition);
}
private void SetFlag(bool on, int mask)
{
if (on)
Flags = (ushort) (Flags | mask);
else
Flags = (ushort) (Flags & ~mask);
}
/// <summary>
/// Decode payload to support Drda protocol
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
public TcpPacket DecodePayload(PacketOrByteArraySegment result)
{
if ((result.ByteArraySegment == null) || (result.ByteArraySegment.Length < DrdaDdmFields.DDMHeadTotalLength))
return this;
// Based on https://github.com/wireshark/wireshark/blob/fe219637a6748130266a0b0278166046e60a2d68/epan/dissectors/packet-drda.c#L757.
// The first header is 6 bytes long, so the length in the second header should be 6 bytes less.
if (result.ByteArraySegment.Bytes[result.ByteArraySegment.Offset + 2] == 0xD0)
{
var outerLength = EndianBitConverter.Big.ToUInt16(result.ByteArraySegment.Bytes,
result.ByteArraySegment.Offset + 0);
var innerLength = EndianBitConverter.Big.ToUInt16(result.ByteArraySegment.Bytes,
result.ByteArraySegment.Offset + 6);
if (outerLength - innerLength == 6)
{
var drdaPacket = new DrdaPacket(result.ByteArraySegment, this);
result.Packet = drdaPacket;
}
}
return this;
}
/// <summary>
/// Computes the TCP checksum. Does not update the current checksum value
/// </summary>
/// <returns> The calculated TCP checksum.</returns>
public ushort CalculateTcpChecksum()
{
return (ushort) CalculateChecksum(TransportChecksumOption.IncludePseudoIPHeader);
}
/// <summary>
/// Update the checksum value.
/// </summary>
public void UpdateTcpChecksum()
{
Checksum = CalculateTcpChecksum();
}
/// <summary>
/// Parses options, pointed to by optionBytes into an array of Options
/// </summary>
/// <param name="optionBytes">
/// A <see cref="T:System.Byte[]" />
/// </param>
/// <returns>
/// A <see cref="List{Option}" />
/// </returns>
private static List<TcpOption> ParseOptions(ByteArraySegment optionBytes)
{
var offset = optionBytes.Offset;
if (optionBytes.Length == 0)
return null;
// Reset the OptionsCollection list to prepare to be re-populated with new data.
var options = new List<TcpOption>();
// The TCP options should be bound by their options offset + length.
var maxOffset = optionBytes.Offset + optionBytes.Length;
// Include a basic check against the available length of the options buffer for invalid TCP packet data in the data offset field.
while ((offset < maxOffset) && (offset < optionBytes.Bytes.Length - TcpOption.LengthFieldOffset))
{
var type = (OptionTypes) optionBytes.Bytes[offset + TcpOption.KindFieldOffset];
// Some options have no length field, we cannot read the length field if it isn't present or we risk out-of-bounds issues.
var length = type is OptionTypes.EndOfOptionList or OptionTypes.NoOperation ? (byte) 1 : optionBytes.Bytes[offset + TcpOption.LengthFieldOffset];
switch (type)
{
case OptionTypes.EndOfOptionList:
{
options.Add(new EndOfOptionsOption(optionBytes.Bytes, offset, length));
offset += EndOfOptionsOption.OptionLength;
break;
}
case OptionTypes.NoOperation:
{
options.Add(new NoOperationOption(optionBytes.Bytes, offset, length));
offset += NoOperationOption.OptionLength;
break;
}
case OptionTypes.MaximumSegmentSize:
{
options.Add(new MaximumSegmentSizeOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
case OptionTypes.WindowScaleFactor:
{
options.Add(new WindowScaleFactorOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
case OptionTypes.SelectiveAcknowledgmentPermitted:
{
options.Add(new SelectiveAcknowledgmentPermittedOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
case OptionTypes.SelectiveAcknowledgment:
{
options.Add(new SelectiveAcknowledgmentOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
case OptionTypes.Echo:
{
options.Add(new EchoOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
case OptionTypes.EchoReply:
{
options.Add(new EchoReplyOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
case OptionTypes.Timestamp:
{
options.Add(new TimeStampOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
case OptionTypes.AlternateChecksumRequest:
{
options.Add(new AlternateChecksumRequestOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
case OptionTypes.AlternateChecksumData:
{
options.Add(new AlternateChecksumDataOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
case OptionTypes.MD5Signature:
{
options.Add(new MD5SignatureOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
case OptionTypes.UserTimeout:
{
options.Add(new UserTimeoutOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
// These fields aren't supported because they're still considered experimental in their respective RFC specifications.
//case OptionTypes.PartialOrderConnectionPermitted:
//case OptionTypes.PartialOrderServiceProfile:
//case OptionTypes.ConnectionCount:
//case OptionTypes.ConnectionCountNew:
//case OptionTypes.ConnectionCountEcho:
//case OptionTypes.QuickStartResponse:
default:
{
options.Add(new UnsupportedOption(optionBytes.Bytes, offset, length));
offset += length;
break;
}
}
}
return options;
}
/// <inheritdoc cref="Packet.ToString(StringOutputType)" />
public override string ToString(StringOutputType outputFormat)
{
var buffer = new StringBuilder();
var color = "";
var colorEscape = "";
if (outputFormat is StringOutputType.Colored or StringOutputType.VerboseColored)
{
color = Color;
colorEscape = AnsiEscapeSequences.Reset;
}
switch (outputFormat)
{
case StringOutputType.Normal:
case StringOutputType.Colored:
{
// build flag string
var flags = "{";
if (Urgent)
flags += "urg[0x" + Convert.ToString(UrgentPointer, 16) + "]|";
if (Acknowledgment)
flags += "ack[" + AcknowledgmentNumber + " (0x" + Convert.ToString(AcknowledgmentNumber, 16) + ")]|";
if (Push)
flags += "psh|";
if (Reset)
flags += "rst|";
if (Synchronize)
flags += "syn[0x" + Convert.ToString(SequenceNumber, 16) + "," + SequenceNumber + "]|";
flags = flags.TrimEnd('|');
flags += "}";
// build the output string
buffer.AppendFormat("{0}[TCPPacket: SourcePort={2}, DestinationPort={3}, Flags={4}]{1}",
color,
colorEscape,
SourcePort,
DestinationPort,
flags);
break;
}
case StringOutputType.Verbose:
case StringOutputType.VerboseColored:
{
// collect the properties and their value
var properties = new Dictionary<string, string>
{
{ "source port", SourcePort.ToString() },
{ "destination port", DestinationPort.ToString() },
{ "sequence number", SequenceNumber + " (0x" + SequenceNumber.ToString("x") + ")" },
{ "acknowledgement number", AcknowledgmentNumber + " (0x" + AcknowledgmentNumber.ToString("x") + ")" },
// TODO: Implement a HeaderLength property for TCPPacket
//properties.Add("header length", HeaderLength.ToString());
{ "flags", "(0x" + Flags.ToString("x") + ")" }
};
var flags = Convert.ToString(Flags, 2).PadLeft(8, '0');
properties.Add("", flags[0] + "... .... = [" + flags[0] + "] congestion window reduced");
properties.Add(" ", "." + flags[1] + ".. .... = [" + flags[1] + "] Ecn - echo");
properties.Add(" ", ".." + flags[2] + ". .... = [" + flags[2] + "] urgent");
properties.Add(" ", "..." + flags[3] + " .... = [" + flags[3] + "] acknowledgement");
properties.Add(" ", ".... " + flags[4] + "... = [" + flags[4] + "] push");
properties.Add(" ", ".... ." + flags[5] + ".. = [" + flags[5] + "] reset");
properties.Add(" ", ".... .." + flags[6] + ". = [" + flags[6] + "] syn");
properties.Add(" ", ".... ..." + flags[7] + " = [" + flags[7] + "] fin");
properties.Add("window size", WindowSize.ToString());
properties.Add("checksum", "0x" + Checksum.ToString("x4") + " [" + (ValidChecksum ? "valid" : "invalid") + "]");
properties.Add("options", "0x" + BitConverter.ToString(Options).Replace("-", "").PadLeft(12, '0'));
var parsedOptions = OptionsCollection;
if (parsedOptions != null)
for (var i = 0; i < parsedOptions.Count; i++)
properties.Add("option" + (i + 1), parsedOptions[i].ToString());
// calculate the padding needed to right-justify the property names
var padLength = RandomUtils.LongestStringLength(new List<string>(properties.Keys));
// build the output string
buffer.AppendLine("TCP: ******* TCP - \"Transmission Control Protocol\" - offset=? length=" + TotalPacketLength);
buffer.AppendLine("TCP:");
foreach (var property in properties)
if (property.Key.Trim() != "")
buffer.AppendLine("TCP: " + property.Key.PadLeft(padLength) + " = " + property.Value);
else
buffer.AppendLine("TCP: " + property.Key.PadLeft(padLength) + " " + property.Value);
buffer.AppendLine("TCP:");
break;
}
}
// append the base class output
buffer.Append(base.ToString(outputFormat));
return buffer.ToString();
}
/// <summary>
/// Create a randomized tcp packet with the given ip version
/// </summary>
/// <returns>
/// A <see cref="Packet" />
/// </returns>
public static TcpPacket RandomPacket()
{
var rnd = new Random();
// create a randomized TcpPacket
var srcPort = (ushort) rnd.Next(UInt16.MinValue, UInt16.MaxValue);
var dstPort = (ushort) rnd.Next(UInt16.MinValue, UInt16.MaxValue);
var tcpPacket = new TcpPacket(srcPort, dstPort);
return tcpPacket;
}
}