/// <summary> /// Creates a network message by decoding a NetMessage /// </summary> /// <param name="msg">The NetMessage to be decoded</param> public SpiderMessage(NetMessage msg) { //This is the exception we throw if something goes wrong Exception e = new Exception("Could not read message"); byte[] contents = msg.ReadBytes(msg.Length); type = (SpiderMessageType)contents[0]; int dataOffset = BitConverter.ToInt32(contents, 1); label = Encoding.UTF8.GetString(contents, 1 + sizeof(int), dataOffset - 1 - sizeof(int)); senderIP = msg.Sender.RemoteEndpoint.Address; connection = msg.Sender; if (type == SpiderMessageType.Bytes) { data = new byte[contents.Length - dataOffset]; for (int i = 0; i < contents.Length - dataOffset; i++) { ((byte[])data)[i] = contents[i + dataOffset]; } } else { String tempData = Encoding.UTF8.GetString(contents, dataOffset, contents.Length - dataOffset); switch (type) { case SpiderMessageType.Double: data = Convert.ToDouble(tempData); break; case SpiderMessageType.Int: data = Convert.ToInt32(tempData); break; default: data = tempData; break; } } }
internal static void HandleConnect(NetMessage connectMsg, NetServer server, IPEndPoint senderEndpoint) { NetMessage response; if (server.NumConnected >= server.Configuration.MaximumConnections) { // server full response = new NetMessage(NetMessageType.Handshake, "Server full".Length + 1); response.Write((byte)NetHandshakeType.Disconnected); response.Write("Server full"); server.SendSingleMessageAtOnce(response, null, senderEndpoint); return; } ushort remoteNow = connectMsg.ReadUInt16(); //server.Log.Debug("Setting remote clock based on guess of 50 ms lag..."); int remoteClockOffset = NetTime.CalculateOffset(NetTime.Now, remoteNow, 0.05); // assume 50ms... // read symmetric key int encSymKeyLen = connectMsg.ReadUInt16(); byte[] encSymKey = null; if (encSymKeyLen > 0) { encSymKey = connectMsg.ReadBytes(encSymKeyLen); } // read custom data int cdLen = (int)connectMsg.Read7BitEncodedUInt(); byte[] customData = null; if (cdLen > 0) { customData = connectMsg.ReadBytes(cdLen); } string failReason = null; bool ok = server.ApproveConnection(senderEndpoint, customData, out failReason); if (!ok) { if (!string.IsNullOrEmpty(failReason)) { // send disconnect reason; unencrypted, client can handle it because status is connecting response = new NetMessage(NetMessageType.Handshake, failReason.Length + 3); response.Write((byte)NetHandshakeType.Disconnected); response.Write(failReason); server.SendSingleMessageAtOnce(response, null, senderEndpoint); } // connection not approved return; } NetConnection connection = server.AddConnection(senderEndpoint, remoteClockOffset); if (connection == null) { return; // uh oh } if (encSymKeyLen > 0) { byte[] symKey = connection.m_encryption.DecryptRSA(encSymKey); if (symKey == null) { // send disconnect unencrypted, client can handle it because status is connecting string bye = "RSA failed; are you using correct public key?"; response = new NetMessage(NetMessageType.Handshake, bye.Length + 3); response.Write((byte)NetHandshakeType.Disconnected); response.Write(bye); server.SendSingleMessageAtOnce(response, null, senderEndpoint); server.Log.Warning("Failed to decrypt RSA encrypted symmetric key from " + senderEndpoint); return; } connection.m_encryption.SetSymmetricKey(symKey); server.Log.Debug("Received Connect containing key: " + Convert.ToBase64String(symKey)); } else { if (server.Configuration.UsesEncryption) { server.Log.Warning("Client tried to connect without encryption from " + senderEndpoint); // denied response = new NetMessage(NetMessageType.Handshake, "Encryption required".Length + 1); response.Write((byte)NetHandshakeType.Disconnected); response.Write("Encryption required"); server.SendSingleMessageAtOnce(response, null, senderEndpoint); return; } server.Log.Debug("Received Connect - using unencrypted connection!"); } // send connect response int bytesSent = SendConnectResponse(server, connection, senderEndpoint); if (connection != null) { // account for connectresponse connection.Statistics.PacketsSent++; connection.Statistics.MessagesSent++; connection.Statistics.BytesSent += bytesSent; } }
internal static void HandleConnect(NetMessage connectMsg, NetServer server, IPEndPoint senderEndpoint) { NetMessage response; if (server.NumConnected >= server.Configuration.MaximumConnections) { // server full response = new NetMessage(NetMessageType.Handshake, "Server full".Length + 1); response.Write((byte)NetHandshakeType.Disconnected); response.Write("Server full"); server.SendSingleMessageAtOnce(response, null, senderEndpoint); return; } ushort remoteNow = connectMsg.ReadUInt16(); //server.Log.Debug("Setting remote clock based on guess of 50 ms lag..."); int remoteClockOffset = NetTime.CalculateOffset(NetTime.Now, remoteNow, 0.05); // assume 50ms... // read symmetric key int encSymKeyLen = connectMsg.ReadUInt16(); byte[] encSymKey = null; if (encSymKeyLen > 0) encSymKey = connectMsg.ReadBytes(encSymKeyLen); // read custom data int cdLen = (int)connectMsg.Read7BitEncodedUInt(); byte[] customData = null; if (cdLen > 0) customData = connectMsg.ReadBytes(cdLen); string failReason = null; bool ok = server.ApproveConnection(senderEndpoint, customData, out failReason); if (!ok) { if (!string.IsNullOrEmpty(failReason)) { // send disconnect reason; unencrypted, client can handle it because status is connecting response = new NetMessage(NetMessageType.Handshake, failReason.Length + 3); response.Write((byte)NetHandshakeType.Disconnected); response.Write(failReason); server.SendSingleMessageAtOnce(response, null, senderEndpoint); } // connection not approved return; } NetConnection connection = server.AddConnection(senderEndpoint, remoteClockOffset); if (connection == null) return; // uh oh if (encSymKeyLen > 0) { byte[] symKey = connection.m_encryption.DecryptRSA(encSymKey); if (symKey == null) { // send disconnect unencrypted, client can handle it because status is connecting string bye = "RSA failed; are you using correct public key?"; response = new NetMessage(NetMessageType.Handshake, bye.Length + 3); response.Write((byte)NetHandshakeType.Disconnected); response.Write(bye); server.SendSingleMessageAtOnce(response, null, senderEndpoint); server.Log.Warning("Failed to decrypt RSA encrypted symmetric key from " + senderEndpoint); return; } connection.m_encryption.SetSymmetricKey(symKey); server.Log.Debug("Received Connect containing key: " + Convert.ToBase64String(symKey)); } else { if (server.Configuration.UsesEncryption) { server.Log.Warning("Client tried to connect without encryption from " + senderEndpoint); // denied response = new NetMessage(NetMessageType.Handshake, "Encryption required".Length + 1); response.Write((byte)NetHandshakeType.Disconnected); response.Write("Encryption required"); server.SendSingleMessageAtOnce(response, null, senderEndpoint); return; } server.Log.Debug("Received Connect - using unencrypted connection!"); } // send connect response int bytesSent = SendConnectResponse(server, connection, senderEndpoint); if (connection != null) { // account for connectresponse connection.Statistics.PacketsSent++; connection.Statistics.MessagesSent++; connection.Statistics.BytesSent += bytesSent; } }
public static BBMessage FromNetMessage(NetMessage netmsg) { BBMessage msg = new BBMessage(); MemoryStream s = new MemoryStream(netmsg.ReadBytes(netmsg.Length)); s.Seek(0, SeekOrigin.Begin); BinaryFormatter b = new BinaryFormatter(); msg = (BBMessage)b.Deserialize(s); return msg; }