void TempClient_Connected(Client instance, ClientEventArgs e) { string strData = Encoding.ASCII.GetString(e.Data); string cmd = strData.Split('_')[0]; if (cmd == "connected") { // Connection was accepted. instance.MessageReceived += Instance_MessageReceived; instance.ConnectionLost += Instance_ConnectionLost; ClientInstances.Add(new ClientInstance { ClientNetworking = instance, RemoteEntity = strData.Split('_')[1] }); Connected(e); } else { throw new Exception("An error ocurred: " + strData.Split('_')[0]); } }
/// <summary> /// Starts listening for messages from the user /// </summary> void StartListening() { int requestCount = 0; requestCount = 0; while (!stop) { try { requestCount = requestCount + 1; NetworkStream stream = Socket.GetStream(); byte[] dataLength = new byte[4]; stream.Read(dataLength, 0, 4); int dLength = BitConverter.ToInt32(dataLength, 0); byte[] packetType = new byte[2]; stream.Read(packetType, 0, 2); Int16 pType = BitConverter.ToInt16(packetType, 0); byte[] isEnc = new byte[1]; stream.Read(isEnc, 0, 1); bool isEncrypted = isEnc[0] != 0b0; byte[] hasMac = new byte[1]; stream.Read(hasMac, 0, 1); bool hasMessageAuth = isEnc[0] != 0b0; Int16 cipherAlgorithm = 0; byte[] cipherIV = null; if (isEncrypted) { byte[] ciptherAlgo = new byte[2]; stream.Read(ciptherAlgo, 0, 2); cipherAlgorithm = BitConverter.ToInt16(ciptherAlgo, 0); byte[] cipherIvSize = new byte[4]; stream.Read(cipherIvSize, 0, 4); cipherIV = new byte[BitConverter.ToInt32(cipherIvSize, 0)]; stream.Read(cipherIV, 0, BitConverter.ToInt32(cipherIvSize, 0)); } CipherAlgo cAlgo = CipherAlgo.NOALGO; if (isEncrypted) { switch (cipherAlgorithm) { case 0: cAlgo = CipherAlgo.AES256; break; } } Int16 macAlgorithm = 0; byte[] msgMac = null; if (hasMessageAuth) { byte[] macAlgo = new byte[2]; stream.Read(macAlgo, 0, 2); macAlgorithm = BitConverter.ToInt16(macAlgo, 0); byte[] macSize = new byte[4]; stream.Read(macSize, 0, 4); msgMac = new byte[BitConverter.ToInt32(macSize, 0)]; stream.Read(msgMac, 0, BitConverter.ToInt32(macSize, 0)); } MessageAuthAlgo algo = MessageAuthAlgo.NOALGO; if (hasMessageAuth) { switch (macAlgorithm) { case 0: algo = MessageAuthAlgo.HMACSHA256; break; } } byte[] data = new byte[dLength]; stream.Read(data, 0, dLength); ClientEventArgs e = new ClientEventArgs(pType, isEncrypted, hasMessageAuth, cAlgo, cipherIV, algo, msgMac, data); MessageReceived(this, e); } catch (Exception) { if (!stop) { ConnectionLost(this, null); stop = true; } } } }
/// <summary> /// Starts listening for incoming connections /// </summary> /// <param name="ip">Ip.</param> /// <param name="port">Port.</param> void StartListening(IPAddress ip, int port) { TcpListener serverSocket = new TcpListener(ip, port); TcpClient clientSocket = default(TcpClient); int counter = 0; serverSocket.Start(); while (!stop) { if (serverSocket.Pending()) { counter += 1; clientSocket = serverSocket.AcceptTcpClient(); NetworkStream stream = clientSocket.GetStream(); byte[] dataLength = new byte[4]; stream.Read(dataLength, 0, 4); int dLength = BitConverter.ToInt32(dataLength, 0); byte[] packetType = new byte[2]; stream.Read(packetType, 0, 2); Int16 pType = BitConverter.ToInt16(packetType, 0); byte[] isEnc = new byte[1]; stream.Read(isEnc, 0, 1); bool isEncrypted = isEnc[0] != 0b0; byte[] hasMac = new byte[1]; stream.Read(hasMac, 0, 1); bool hasMessageAuth = isEnc[0] != 0b0; Int16 cipherAlgorithm = 0; byte[] cipherIV = null; if (isEncrypted) { byte[] ciptherAlgo = new byte[2]; stream.Read(ciptherAlgo, 0, 2); cipherAlgorithm = BitConverter.ToInt16(ciptherAlgo, 0); byte[] cipherIvSize = new byte[4]; stream.Read(cipherIvSize, 0, 4); cipherIV = new byte[BitConverter.ToInt32(cipherIvSize, 0)]; stream.Read(cipherIV, 0, BitConverter.ToInt32(cipherIvSize, 0)); } CipherAlgo cAlgo = CipherAlgo.NOALGO; if (isEncrypted) { switch (cipherAlgorithm) { case 0: cAlgo = CipherAlgo.AES256; break; } } Int16 macAlgorithm = 0; byte[] msgMac = null; if (hasMessageAuth) { byte[] macAlgo = new byte[2]; stream.Read(macAlgo, 0, 2); macAlgorithm = BitConverter.ToInt16(macAlgo, 0); byte[] macSize = new byte[4]; stream.Read(macSize, 0, 4); msgMac = new byte[BitConverter.ToInt32(macSize, 0)]; stream.Read(msgMac, 0, BitConverter.ToInt32(macSize, 0)); } MessageAuthAlgo algo = MessageAuthAlgo.NOALGO; if (hasMessageAuth) { switch (macAlgorithm) { case 0: algo = MessageAuthAlgo.HMACSHA256; break; } } byte[] data = new byte[dLength]; stream.Read(data, 0, dLength); ClientEventArgs _e = new ClientEventArgs(pType, isEncrypted, hasMessageAuth, cAlgo, cipherIV, algo, msgMac, data); ClientConnected(data, clientSocket, _e); } } /*BroadcastShutdownToClients(); * foreach (DictionaryEntry item in clientHandlers) * ((ClientHandler)item.Value).Disconnect(); * if (clientSocket != null) * clientSocket.Close(); * serverSocket.Stop();*/ }
private void Client_MessageReceived(ClientHandler instance, ClientEventArgs e) { MessageReceived(null, instance, e); }
private void Client_ConnectionLost(ClientHandler instance, ClientEventArgs e) { ConnectionLost(null, instance, e); }
private void Server_ConnectionLost(byte[] m, ClientHandler clientHandler, ClientEventArgs e) { ConnectionLost(null, clientHandler, e); }
void Server_MessageReceived(byte[] m, ClientHandler clientHandler, ClientEventArgs e) { SVMessageReceived(m, clientHandler, e); }
void Instance_MessageReceived(Client instance, ClientEventArgs e) { MessageReceived(e.Data, ClientInstances.First(m => m.ClientNetworking == instance), e); }
void Instance_ConnectionLost(Client instance, ClientEventArgs e) { CLConnectionLost(null, ClientInstances.First(m => m.ClientNetworking == instance), null); }
void GetMessage() { while (IsListening) { try { serverStream = clientSocket.GetStream(); byte[] dataLength = new byte[4]; serverStream.Read(dataLength, 0, 4); int dLength = BitConverter.ToInt32(dataLength, 0); byte[] packetType = new byte[2]; serverStream.Read(packetType, 0, 2); Int16 pType = BitConverter.ToInt16(packetType, 0); byte[] isEnc = new byte[1]; serverStream.Read(isEnc, 0, 1); bool isEncrypted = isEnc[0] != 0b0; byte[] hasMac = new byte[1]; serverStream.Read(hasMac, 0, 1); bool hasMessageAuth = isEnc[0] != 0b0; Int16 cipherAlgorithm = 0; byte[] cipherIV = null; if (isEncrypted) { byte[] ciptherAlgo = new byte[2]; serverStream.Read(ciptherAlgo, 0, 2); cipherAlgorithm = BitConverter.ToInt16(ciptherAlgo, 0); byte[] cipherIvSize = new byte[4]; serverStream.Read(cipherIvSize, 0, 4); cipherIV = new byte[BitConverter.ToInt32(cipherIvSize, 0)]; serverStream.Read(cipherIV, 0, BitConverter.ToInt32(cipherIvSize, 0)); } CipherAlgo cAlgo = CipherAlgo.NOALGO; if (isEncrypted) { switch (cipherAlgorithm) { case 0: cAlgo = CipherAlgo.AES256; break; } } Int16 macAlgorithm = 0; byte[] msgMac = null; if (hasMessageAuth) { byte[] macAlgo = new byte[2]; serverStream.Read(macAlgo, 0, 2); macAlgorithm = BitConverter.ToInt16(macAlgo, 0); byte[] macSize = new byte[4]; serverStream.Read(macSize, 0, 4); msgMac = new byte[BitConverter.ToInt32(macSize, 0)]; serverStream.Read(msgMac, 0, BitConverter.ToInt32(macSize, 0)); } MessageAuthAlgo algo = MessageAuthAlgo.NOALGO; if (hasMessageAuth) { switch (macAlgorithm) { case 0: algo = MessageAuthAlgo.HMACSHA256; break; } } byte[] data = new byte[dLength]; serverStream.Read(data, 0, dLength); ClientEventArgs e = new ClientEventArgs(pType, isEncrypted, hasMessageAuth, cAlgo, cipherIV, algo, msgMac, data); if (pType == -1234) { Connected(this, e); } else { MessageReceived(this, e); } } catch (Exception) { IsListening = false; ConnectionLost(this, null); } } }