public void CPacketHandler(object source, PacketHandlerArgs e) { // client recieves C packet, only accepts C packets with the correct tempclientkey // client marks connection verified on receipt of C packet // client sends D packet to the server, which is empty packet // Client now considers connection "open" if (!isserver) // client { byte[] packet = e.Data; int tempnextposition = e.NextPosition; int tempclientkey = (int)binarypacker.ReadValueFromBuffer(packet, ref tempnextposition, typeof(int)); LogFile.WriteLine("C packet received, tempclientkey: " + tempclientkey.ToString() + " our key: " + this.tempclientkey); if (tempclientkey == this.tempclientkey) { SharedSecretKey = (int)binarypacker.ReadValueFromBuffer(packet, ref tempnextposition, typeof(int)); LogFile.WriteLine("Connection to server confirmed, sharedkey: " + SharedSecretKey.ToString()); Validated = true; parent.OnConnectionValidated(); //parent.Send('D', new byte[] { }); } else { LogFile.WriteLine("SharedSecretExchange. WARNING: potential spoof packet detected, allegedly from server " + Encoding.ASCII.GetString(e.Data, 0, e.Data.Length)); } } }
// handles incoming ack packets ('A' packets) public void APacketHandler(object source, PacketHandlerArgs e) { //LogFile.WriteLine( "Processing 'A' packet: " ); byte[] packet = e.Data; int nextposition = e.NextPosition; while (nextposition < packet.Length) { short ackedpacketreference = (short)binarypacker.ReadValueFromBuffer(packet, ref nextposition, typeof(short)); // LogFile.WriteLine( " ... Packet " + ackedpacketreference.ToString() + " acked" ); sentpacketsawaitingack.Remove(ackedpacketreference); } DumpSentPacketsAwaitingAck(); }
// server sends C packet to client, which contains the shared key, and also the temporary client key the client sent // server knows key now, client will receive it // Packet format: [int32 xxx][short xxx][char 'C'][temporary client key][shared key] // server replies to any R packet, generating the shared key if the connection didnt exist before public void RPacketHandler(object source, PacketHandlerArgs e) { if (isserver) { byte[] packet = e.Data; int tempnextposition = e.NextPosition; int tempclientkey = (int)binarypacker.ReadValueFromBuffer(packet, ref tempnextposition, typeof(int)); LogFile.WriteLine("R packet received, sending C packet, tempclientkey: " + tempclientkey.ToString() + " sharedkey: " + SharedSecretKey.ToString()); byte[] newpacket = new byte[8]; tempnextposition = 0; binarypacker.WriteValueToBuffer(newpacket, ref tempnextposition, tempclientkey); binarypacker.WriteValueToBuffer(newpacket, ref tempnextposition, SharedSecretKey); parent.SendNonAckable('C', newpacket); if (!Validated) { Validated = true; LogFile.WriteLine("Shared key sent; marking connection open"); parent.OnConnectionValidated(); } } }
// handles incoming ack packets ('A' packets) public void APacketHandler( object source, PacketHandlerArgs e ) { //LogFile.WriteLine( "Processing 'A' packet: " ); byte[] packet = e.Data; int nextposition = e.NextPosition; while( nextposition < packet.Length ) { short ackedpacketreference = (short)binarypacker.ReadValueFromBuffer(packet, ref nextposition, typeof(short)); // LogFile.WriteLine( " ... Packet " + ackedpacketreference.ToString() + " acked" ); sentpacketsawaitingack.Remove( ackedpacketreference ); } DumpSentPacketsAwaitingAck(); }