Skip to content

Commit b5a8e8f

Browse files
committed
First byte changes (#2006)
1 parent d46ac00 commit b5a8e8f

File tree

8 files changed

+114
-80
lines changed

8 files changed

+114
-80
lines changed

QuicNet.Infrastructure/PacketType.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66

77
namespace QuicNet.Infrastructure
88
{
9-
public enum PacketType
9+
public enum PacketType : UInt16
1010
{
11-
InitialPacket,
12-
HandshakePacket,
13-
ShortHeaderPacket,
14-
LongHeaderPacket,
15-
RetryPacket,
16-
VersionNegotiationPacket,
17-
BrokenPacket
11+
Initial = 0x0,
12+
ZeroRTTProtected = 0x1,
13+
Handshake = 0x2,
14+
RetryPacket = 0x3
1815
}
1916
}

QuicNet.Infrastructure/Packets/HandshakePacket.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,91 @@
1-
using System;
1+
using QuickNet.Utilities;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

56
namespace QuicNet.Infrastructure.Packets
67
{
78
public class LongHeaderPacket : Packet
89
{
9-
public override byte Type => throw new NotImplementedException();
10+
public override byte Type => 0xC0;
11+
12+
public byte DCIL_SCIL { get; set; }
13+
public byte DestinationConnectionId { get; set; }
14+
public byte SourceConnectionId { get; set; }
15+
public VariableInteger TokenLength { get; set; }
16+
public byte[] Token { get; set; }
17+
public VariableInteger Length { get; set; }
18+
public UInt32 PacketNumber { get; set; }
19+
20+
public PacketType PacketType { get; set; }
21+
public LongHeaderPacket(PacketType packetType)
22+
{
23+
DCIL_SCIL = 0;
24+
PacketType = packetType;
25+
}
1026

1127
public override void Decode(byte[] packet)
1228
{
13-
throw new NotImplementedException();
29+
ByteArray array = new ByteArray(packet);
30+
byte type = array.ReadByte();
31+
PacketType = (PacketType)(type & 0x30);
32+
33+
Version = array.ReadUInt32();
34+
DCIL_SCIL = array.ReadByte();
35+
36+
if ((DCIL_SCIL & 0xF0) != 0)
37+
DestinationConnectionId = array.ReadByte();
38+
if ((DCIL_SCIL & 0x0F) != 0)
39+
SourceConnectionId = array.ReadByte();
40+
41+
TokenLength = array.ReadVariableInteger();
42+
if (TokenLength != 0)
43+
Token = array.ReadBytes((int)TokenLength.Value);
44+
45+
Length = array.ReadVariableInteger();
46+
PacketNumber = array.ReadUInt32();
47+
48+
Length = Length - 4;
49+
50+
this.DecodeFrames(array);
1451
}
1552

1653
public override byte[] Encode()
1754
{
18-
throw new NotImplementedException();
55+
byte[] frames = EncodeFrames();
56+
57+
List<byte> result = new List<byte>();
58+
result.Add(EncodeTypeField());
59+
result.AddRange(ByteUtilities.GetBytes(Version));
60+
61+
if (DestinationConnectionId > 0)
62+
DCIL_SCIL = (byte)(DCIL_SCIL | 0x50);
63+
if (SourceConnectionId > 0)
64+
DCIL_SCIL = (byte)(DCIL_SCIL | 0x05);
65+
66+
result.Add(DCIL_SCIL);
67+
68+
if (DestinationConnectionId > 0)
69+
result.Add(DestinationConnectionId);
70+
if (SourceConnectionId > 0)
71+
result.Add(SourceConnectionId);
72+
73+
byte[] tokenLength = new VariableInteger(0);
74+
byte[] length = new VariableInteger(4 + (UInt64)frames.Length);
75+
76+
result.AddRange(tokenLength);
77+
result.AddRange(length);
78+
result.AddRange(ByteUtilities.GetBytes(PacketNumber));
79+
result.AddRange(frames);
80+
81+
return result.ToArray();
82+
}
83+
84+
private byte EncodeTypeField()
85+
{
86+
byte type = (byte)(Type | (byte)PacketType | 0x03);
87+
88+
return type;
1989
}
2090
}
2191
}

QuicNet.Infrastructure/Packets/RetryPacket.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

QuicNet.Infrastructure/Packets/ShortHeaderPacket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace QuicNet.Infrastructure.Packets
77
{
88
public class ShortHeaderPacket : Packet
99
{
10-
public override byte Type => 0x8E; // 1000 1110;
10+
public override byte Type => 0x43; // 0100 0011;
1111
public byte DestinationConnectionId { get; set; }
1212
public UInt32 PacketNumber { get; set; }
1313

QuicNet.Infrastructure/Packets/Unpacker.cs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,15 @@ namespace QuicNet.Infrastructure.Packets
88
{
99
public class Unpacker
1010
{
11-
private Dictionary<byte, PacketType> _typeMap = new Dictionary<byte, PacketType>()
12-
{
13-
{ 0xFF, PacketType.InitialPacket },
14-
{ 0xFE, PacketType.RetryPacket },
15-
{ 0x8E, PacketType.ShortHeaderPacket },
16-
{ 0x77, PacketType.LongHeaderPacket },
17-
{ 0x80, PacketType.VersionNegotiationPacket },
18-
{ 0x7D, PacketType.HandshakePacket }
19-
};
20-
21-
public Unpacker()
22-
{
23-
24-
}
25-
2611
public Packet Unpack(byte[] data)
2712
{
2813
Packet result = null;
2914

30-
PacketType type = GetPacketType(data);
15+
QuicPacketType type = GetPacketType(data);
3116
switch(type)
3217
{
33-
case PacketType.InitialPacket: result = new InitialPacket(); break;
34-
case PacketType.ShortHeaderPacket: result = new ShortHeaderPacket(); break;
18+
case QuicPacketType.Initial: result = new InitialPacket(); break;
19+
case QuicPacketType.ShortHeader: result = new ShortHeaderPacket(); break;
3520
}
3621

3722
if (result == null)
@@ -42,16 +27,24 @@ public Packet Unpack(byte[] data)
4227
return result;
4328
}
4429

45-
public PacketType GetPacketType(byte[] data)
30+
public QuicPacketType GetPacketType(byte[] data)
4631
{
4732
if (data == null || data.Length <= 0)
48-
return PacketType.BrokenPacket;
33+
return QuicPacketType.Broken;
34+
4935
byte type = data[0];
5036

51-
if (_typeMap.ContainsKey(type) == false)
52-
return PacketType.BrokenPacket;
37+
// TODO: Understand how to differentiate between Initial packet and Long Header Packet,
38+
// and if there is any need to do so, or the LHP is just used for deriving the Initial packet.
5339

54-
return _typeMap[type];
40+
if ((type & 0xC0) == 0xC0)
41+
return QuicPacketType.Initial;
42+
if ((type & 0x40) == 0x40)
43+
return QuicPacketType.ShortHeader;
44+
if ((type & 0x80) == 0x80)
45+
return QuicPacketType.VersionNegotiation;
46+
47+
return QuicPacketType.Broken;
5548
}
5649
}
5750
}

QuicNet.Infrastructure/QuicNet.Infrastructure.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,15 @@
6666
<Compile Include="Frames\StreamDataBlockedFrame.cs" />
6767
<Compile Include="NumberSpace.cs" />
6868
<Compile Include="PacketCreator.cs" />
69-
<Compile Include="Packets\HandshakePacket.cs" />
7069
<Compile Include="Packets\InitialPacket.cs" />
7170
<Compile Include="Packets\LongHeaderPacket.cs" />
7271
<Compile Include="Packets\Packet.cs" />
7372
<Compile Include="Packets\Unpacker.cs" />
74-
<Compile Include="Packets\RetryPacket.cs" />
7573
<Compile Include="Packets\ShortHeaderPacket.cs" />
7674
<Compile Include="Packets\VersionNegotiationPacket.cs" />
7775
<Compile Include="PacketType.cs" />
7876
<Compile Include="Properties\AssemblyInfo.cs" />
77+
<Compile Include="QuicPacketType.cs" />
7978
<Compile Include="Settings\QuicSettings.cs" />
8079
<Compile Include="Settings\QuicVersion.cs" />
8180
</ItemGroup>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace QuicNet.Infrastructure
8+
{
9+
public enum QuicPacketType
10+
{
11+
Initial,
12+
LongHeader,
13+
ShortHeader,
14+
VersionNegotiation,
15+
Broken
16+
}
17+
}

0 commit comments

Comments
 (0)