Plasma Stream that requires the user to manually flush the output buffer
This is most useful in network situations because MOUL expects the entire message buffer to be on the wire when it reads it in. EAP was silly and used memcpy, strdup, and all kinds of silly devilry.
Inheritance: Plasma.hsStream
Example #1
0
 /// <summary>
 /// Sends a NetStruct over some (network backed) stream
 /// </summary>
 /// <param name="s">Stream to write to</param>
 public void Send(plBufferedStream s)
 {
     if (MsgID is UInt32)
         s.WriteUInt((uint)MsgID);
     else if (MsgID is UInt16)
         s.WriteUShort((ushort)MsgID);
     else
         throw new NotSupportedException();
     Write(s);
     s.Flush();
 }
Example #2
0
        protected override void IOnConnect()
        {
            fStream = new plBufferedStream(new NetworkStream(fSocket, false));
            fConnHdr.Write(fStream);
            fStream.WriteUInt(12); // Complete BS below, but who cares?
            fStream.WriteUInt(0);
            fStream.WriteUInt(0);
            fStream.Flush();

            // File connections send an Int32 length, then the Int32 msgID
            fStream.PrependLength = true;
            base.IOnConnect();
        }
Example #3
0
        protected override void IOnConnect()
        {
            plBufferedStream bs = new plBufferedStream(new NetworkStream(fSocket, false));
            fConnHdr.Write(bs);
            bs.WriteInt(20);
            pnHelpers.WriteUuid(bs, Guid.Empty);
            bs.Flush();

            // Encryption
            if (!base.INetCliConnect(bs, 4))
                Close();
            bs.Close();

            // Listen
            base.IOnConnect();
        }
Example #4
0
 private void IWriteNetClientConnect(plBufferedStream s, byte[] seed)
 {
     s.WriteByte(plNetCore.kNetCliConnect);
     s.WriteByte(66);
     s.WriteBytes(seed);
     s.Flush();
 }
Example #5
0
        private byte[] ISetupKeys(plBufferedStream s, int gval)
        {
            BigNum b = BigNum.Random(512);
            BigNum n = new BigNum(fN);
            BigNum x = new BigNum(fX);

            // Calculate seeds
            BigNum client_seed = x.PowMod(b, n);
            BigNum server_seed = new BigNum(gval).PowMod(b, n);
            byte[] cliSeed = client_seed.ToLittleArray();
            IWriteNetClientConnect(s, server_seed.ToLittleArray());

            // Dispose of this crap...
            b.Dispose();
            n.Dispose();
            x.Dispose();
            client_seed.Dispose();
            server_seed.Dispose();

            return cliSeed;
        }
Example #6
0
        private bool ISetupEncryption(byte[] srv, byte[] cli)
        {
            if (srv == null || cli == null)
                return false;

            byte[] key = new byte[7];
            for (int i = 0; i < 7; i++) {
                if (i >= cli.Length) key[i] = srv[i];
                else key[i] = (byte)(cli[i] ^ srv[i]);
            }

            fStream = new plBufferedStream(new pnSocketStream(fSocket, key));
            return true;
        }
Example #7
0
 private byte[] IReadNetClientEncrypt(plBufferedStream s)
 {
     try {
         if (s.ReadByte() != plNetCore.kNetCliEncrypt) return null;
         if (s.ReadByte() != 9) return null;
         return s.ReadBytes(7);
     } catch (EndOfStreamException) {
         return null;
     }
 }
Example #8
0
 protected bool INetCliConnect(plBufferedStream bs, int gval)
 {
     byte[] cli = ISetupKeys(bs, gval);
     byte[] srv = IReadNetClientEncrypt(bs);
     if (srv == null) return false;
     return ISetupEncryption(srv, cli);
 }
Example #9
0
 public void Close()
 {
     if (fStream != null) {
         lock (fStream) {
             fStream.Close();
             fStream = null;
         }
     }
     if (fSocket != null) {
         if (fSocket.Connected)
             fSocket.Shutdown(SocketShutdown.Both); // Be nice
         fSocket.Close();
         fSocket = null;
     }
     fCallbacks.Clear();
 }
Example #10
0
        protected override void IOnConnect()
        {
            // Write out the lobby header & the server header
            plBufferedStream bs = new plBufferedStream(new NetworkStream(fSocket, false));
            fConnHdr.Write(bs);
            bs.Flush();

            // Encryption
            if (!base.INetCliConnect(bs, 2011))
                Close();
            bs.Close();

            // Listen
            base.IOnConnect();
        }
Example #11
-1
        protected bool SetupEncryption(byte[] pubKey, byte[] privKey)
        {
            NetworkStream ns = new NetworkStream(fSocket, false);
            plBufferedStream temp = new plBufferedStream(ns);
            byte[] yData = IReadNetCliConnect(temp);

            if (yData != null) {
                BigNum Y = new BigNum(yData);
                BigNum N = new BigNum(pubKey);
                BigNum K = new BigNum(privKey);

                // Generate some randomness and do Y**K%N to get the key components
                byte[] server_seed = RNG.Random(7);
                BigNum client_seed = Y.PowMod(K, N);
                byte[] seed_data = client_seed.ToLittleArray();

                // Grab the first 7 bytes and xor it with some randomness to make our key
                byte[] key = new byte[7];
                for (int i = 0; i < key.Length; i++)
                    if (i >= seed_data.Length) key[i] = server_seed[i];
                    else key[i] = (byte)(seed_data[i] ^ server_seed[i]);

                // Final setup
                fStream = new plBufferedStream(new pnSocketStream(fSocket, key));
                IWriteNetCliEncrypt(temp, server_seed);

                // Explicitly clean up some resources so that we don't pressure the GC too much.
                // Those explicit finalizers really suck.
                Y.Dispose();
                N.Dispose();
                K.Dispose();
                client_seed.Dispose();
            } else
                // Write the error code to the unencrypted buffer
                temp.WriteByte(plNetCore.kNetCliError);

            temp.Flush();
            temp.Close();
            ns.Close();

            return (yData != null);
        }