protected override byte[] EncodeDataBytes(ProtocolVersion ver)
{
if (CipherSuites.Count == 0 || CompressionMethods.Count == 0) {
throw new Exception("No cipher suites or compression methods defined");
}
MemoryStream memStream = new MemoryStream();
HandshakeStream stream = new HandshakeStream(memStream);
stream.WriteUInt8(ClientVersion.Major);
stream.WriteUInt8(ClientVersion.Minor);
stream.WriteBytes(Random.GetBytes());
stream.WriteUInt8((Byte) SessionID.Length);
stream.WriteBytes(SessionID);
if (ClientVersion.IsUsingDatagrams) {
stream.WriteUInt8((Byte) Cookie.Length);
stream.WriteBytes(Cookie);
}
stream.WriteUInt16((UInt16) (2*CipherSuites.Count));
foreach (UInt16 cipher in CipherSuites) {
stream.WriteUInt16(cipher);
}
stream.WriteUInt8((Byte) CompressionMethods.Count);
foreach (Byte compression in CompressionMethods) {
stream.WriteUInt8(compression);
}
if (Extensions.Count > 0) {
int length = 0;
foreach (HelloExtension ext in Extensions) {
if (!ext.SupportsProtocolVersion(ClientVersion))
continue;
length += 4 + ext.Data.Length;
}
stream.WriteUInt16((UInt16) length);
foreach (HelloExtension ext in Extensions) {
if (!ext.SupportsProtocolVersion(ClientVersion))
continue;
UInt16 type = ext.Type;
byte[] data = ext.Data;
stream.WriteUInt16(type);
stream.WriteUInt16((UInt16) data.Length);
stream.WriteBytes(data);
}
}
return memStream.ToArray();
}