private byte PerformHandshake(byte id, N2HBinaryReader reader, long oldPos)
{
//Logger.Debug("PerformHandshake{0}", id);
switch (id)
{
case 0x30:
reader.ReadByte();
var epdLen = reader.ReadByte() - 1;
var type = reader.ReadByte();
var epd = reader.ReadBytes(epdLen);
var tag = reader.ReadBytes(16);
Writer.Write((byte)tag.Length);
Writer.Write(tag);
if (type == 0x0F)
return Handler.PerformHandshake(tag, Writer,Peer.Address, epd);
if (type == 0x0a)
{
var tagstr = tag.BytesToString();
var attempt = GetHelloAttempt<HelloAttempt>(tagstr);
ushort port;
string host;
RtmfpUtils.UnpackUrl(epd.BytesToString(), out host, out port, out Peer.Path,out Peer.Properties);
var addresses = new List<string>();
Peer.OnHandshake(attempt.Count + 1, addresses);
if (addresses.Count > 0)
{
for (var i = 0; i < addresses.Count; i++)
{
if (addresses[i] == "again")
{
addresses[i] = host + ":" + port;
Writer.WriteAddress(new IPEndPoint(IPAddress.Parse(host),port),i==0 );
}
}
return 0x71;
}
CreateCookie(Writer, attempt, tag, epd.BytesToString());
Writer.Write(_certificat);
return 0x70;
}
else
{
Logger.FATAL("Unkown handshake first way with '{0}' type", type);
}
return 0;
case 0x38:
FarId = reader.ReadUInt32();
if (reader.Read7BitLongValue() != CookieComputing.COOKIE_SIZE)
{
return 0;
}
var cookieKey = reader.ReadBytes(CookieComputing.COOKIE_SIZE).BytesToString();
reader.BaseStream.Position -= CookieComputing.COOKIE_SIZE;
if (!_cookies.ContainsKey(cookieKey))
{
Logger.WARN("Cookie {0} unknown, maybe already connected (udpBuffer congested?)", cookieKey);
return 0;
}
var cookie = _cookies[cookieKey];
cookie.PeerAddress = Peer.Address;
if (cookie.FarId == 0)
{
cookie.FarId = FarId;
reader.BaseStream.Position += CookieComputing.COOKIE_SIZE;
var size = reader.Read7BitLongValue();
var buffer = reader.ReadBytes((int) size);
uint tempSize = 0;
cookie.PeerId = Target.Sha256.ComputeHash(buffer, 0, (int)size);
//Native.EVP_Digest(buffer, (uint)size, cookie.PeerId, ref tempSize, Native.EVP_sha256(), IntPtr.Zero);
reader.BaseStream.Position -= (long) size;
var initiatorKeySize = (int) (reader.Read7BitValue() - 2);
reader.BaseStream.Position += 2;
cookie.CookieComputing.InitiatorKey = new byte[initiatorKeySize];
reader.BaseStream.Read(cookie.CookieComputing.InitiatorKey, 0, initiatorKeySize);
//cookie.CookieComputing.InitiatorKey = reader.ReadBytes((int) initiatorKeySize);
cookie.CookieComputing.InitiatorNonce = reader.ReadBytes((int) reader.Read7BitValue());
Writer.BaseStream.Position = oldPos;
tempSize = reader.ReadByte();//0x58
if(tempSize!=0x58)Logger.WARN("not 0x58!!");
cookie.ComputeKeys();
}
else if(cookie.Id>0)
{
cookie.Read(Writer.BaseStream);
return 0x78;
}
return 0;
default:
Logger.FATAL("Unkown handshake packet id {0}", id);
return 0;
}
}