// 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(); } } }
public void ReceivedPacketHandler(ConnectionInfo connection, byte[] packet, int nextposition, int length) { //object connection = e.Connection; //byte[] packet = e.Data; //int nextposition = e.DataStartIndex; lasttimestamp = DateTime.Now; if (packet.Length >= 4 + 2 + 1) { int packetkey = (int)binarypacker.ReadValueFromBuffer(packet, ref nextposition, typeof(int)); short packetref = (short)binarypacker.ReadValueFromBuffer(packet, ref nextposition, typeof(short)); char packetcode = (char)binarypacker.ReadValueFromBuffer(packet, ref nextposition, typeof(char)); //LogFile.WriteLine( "Packet key: " + packetkey.ToString() + " packetref: " + packetref.ToString() + " packetcode: " + packetcode ); if (unsafepackethandlers.Contains(packetcode)) { //LogFile.WriteLine("calling unsafepackethandler..."); if (packetreferencecontroller.ValidateIncomingReference(packetref)) { //LogFile.WriteLine("Incoming reference validated"); ((PacketHandler)unsafepackethandlers[packetcode])(this, new PacketHandlerArgs( packetkey, packetcode, packetref, packet, nextposition)); } } else { if (sharedsecretexchange.ValidateIncomingPacketKey(packetkey)) { if (packetreferencecontroller.ValidateIncomingReference(packetref)) { if (packethandlers.Contains(packetcode)) { ((PacketHandler)packethandlers[packetcode])(this, new PacketHandlerArgs( packetkey, packetcode, packetref, packet, nextposition)); } else if (parent.packetconsumers.ContainsKey(packetcode)) { parent.packetconsumers[packetcode](this, packet, nextposition, length - nextposition); } else { LogFile.WriteLine("Warning: unknown packet code " + packetcode.ToString() + " " + Encoding.ASCII.GetString(packet, 0, packet.Length)); } }// else silently ignore duplicate packet } else { if (isserver) { LogFile.WriteLine("WARNING: server received potentially spoofed packet allegedly from " + connection.ToString() + " " + Encoding.ASCII.GetString(packet, 0, packet.Length)); } else { LogFile.WriteLine("WARNING: client received potentially spoofed packet allegedly from " + connection.ToString() + " " + Encoding.ASCII.GetString(packet, 0, packet.Length)); } } } } }
void network_ReceivedPacket(NetworkLevel2Connection connection, byte[] data, int offset, int length) { //LogFile.WriteLine("rpc received packet " + Encoding.UTF8.GetString(data, offset, length)); try { if (length > 1) { int position = offset; //char type = (char)binarypacker.ReadValueFromBuffer(data, ref position, typeof(Char)); //if (type == RpcType) //{ string typename = (string)binarypacker.ReadValueFromBuffer(data, ref position, typeof(string)); string methodname = (string)binarypacker.ReadValueFromBuffer(data, ref position, typeof(string)); // LogFile.WriteLine("Got rpc [" + typename + "] [" + methodname + "]"); if (TypeIsAllowed(typename)) // security check to prevent arbitrary activation //if (ArrayHelper.IsInArray(allowedtypes, typename)) { int dotpos = typename.LastIndexOf("."); string namespacename = ""; string interfacename; if (dotpos >= 0) { namespacename = typename.Substring(0, dotpos); interfacename = typename.Substring(dotpos + 1); } else { interfacename = typename; } // LogFile.WriteLine("[" + namespacename + "][" + interfacename + "]"); string serverwrapperclassname = "OSMP." + interfacename.Substring(1) + ""; // LogFile.WriteLine("serverwrapperclassname [" + serverwrapperclassname + "]"); //if (namespacename != "") //{ // serverwrapperclassname = namespacename + "." + serverwrapperclassname; //} Type interfacetype = Type.GetType(typename); string typenametoinstantiate = serverwrapperclassname + ", " + TargetAssemblyName; LogFile.WriteLine("typenametoinstantiate: [" + typenametoinstantiate + "]"); Type serverwrapperttype = Type.GetType(serverwrapperclassname + ", " + TargetAssemblyName); if (isserver) { LogFile.WriteLine("server RpcController, instantiating [" + serverwrapperttype + "]"); } else { LogFile.WriteLine("client RpcController, instantiating [" + serverwrapperttype + "]"); } object serverwrapperobject = Activator.CreateInstance(serverwrapperttype, new object[] { connection.connectioninfo.Connection }); MethodInfo methodinfo = serverwrapperttype.GetMethod(methodname); ParameterInfo[] parameterinfos = methodinfo.GetParameters(); object[] parameters = new object[parameterinfos.GetLength(0)]; for (int i = 0; i < parameters.GetLength(0); i++) { parameters[i] = binarypacker.ReadValueFromBuffer(data, ref position, parameterinfos[i].ParameterType); } //foreach (object parameter in parameters) //{ // LogFile.WriteLine(parameter.GetType().ToString() + " " + parameter.ToString()); //} methodinfo.Invoke(serverwrapperobject, parameters); } else { LogFile.WriteLine("Warning: unauthorized RPC type " + typename + ". Check has attribute [AuthorizedRpcInterface]"); } //} } } catch (Exception ex) { LogFile.WriteLine(ex); } }