Esempio n. 1
0
        /*
         * public static void LogBytes (byte[] buf, int len)
         * {
         *      byte[] ba = new byte[len];
         *      Array.Copy (buf, ba, len);
         *      SystemLogger.Log (SystemLogger.Module.CORE, ByteBuilder.FormatParameter (new Parameter (ba, ParameterType.Byte)));
         * }
         */

        void ReadInternal(byte[] buf, int read, bool alreadyEncrypted)
        {
            if ((!alreadyEncrypted) && (encType != EncryptionType.None))
            {
                if (encComplete)
                {
                    //SystemLogger.Log (SystemLogger.Module.CORE, ID + " Received: ");
                    //LogBytes (buf, read);
                    //buf = decryptor.TransformFinalBlock (buf, 0, read);
                    //SystemLogger.Log (SystemLogger.Module.CORE, ID + " Decrypted: ");
                    //LogBytes (buf, read);
                }
                else
                {
                    // Client side key exchange
                    int ofs = 0;
                    if (encExpected < 0)
                    {
                        encStage++;
                        ofs++;
                        read--;
                        encExpected = buf[0];
                        // length of key to come
                        encKey  = new byte[encExpected];
                        encRead = 0;
                    }
                    if (read >= encExpected)
                    {
                        Array.Copy(buf, ofs, encKey, encRead, encExpected);
                        int togo = read - encExpected;
                        encExpected = -1;
                        SystemLogger.Log(SystemLogger.Module.CORE, ID + " Read encryption key: " + ByteBuilder.FormatParameter(new Parameter(encKey, ParameterType.Byte)));

                        /*
                         * if (server == null)
                         *      ClientEncryptionTransferComplete ();
                         * else
                         *      ServerEncryptionTransferComplete ();
                         */
                        if (togo > 0)
                        {
                            byte[] newbuf = new byte[togo];
                            Array.Copy(buf, read + ofs - togo, newbuf, 0, togo);
                            ReadInternal(newbuf, togo, false);
                        }
                    }
                    else
                    {
                        Array.Copy(buf, ofs, encKey, encRead, read);
                        encExpected -= read;
                        encRead     += read;
                    }
                    return;
                }
            }

            if ((!alreadyEncrypted) && (OnReadBytes != null))
            {
                OnReadBytes(this, buf, read);
            }

            if ((OnReadMessage != null) && (MessageType != MessageType.Unmessaged))
            {
                // Messaged mode
                int  copied;
                uint code = 0;
                switch (MessageType)
                {
                case MessageType.CodeAndLength:
                case MessageType.Length:
                    int length;
                    if (MessageType == MessageType.Length)
                    {
                        copied = FillHeader(ref buf, 4, read);
                        if (headerread < 4)
                        {
                            break;
                        }
                        length = GetInt(msgheader, 0, 4);
                    }
                    else
                    {
                        copied = FillHeader(ref buf, 8, read);
                        if (headerread < 8)
                        {
                            break;
                        }
                        code   = (uint)GetInt(msgheader, 0, 4);
                        length = GetInt(msgheader, 4, 4);
                    }
                    if (read == copied)
                    {
                        break;
                    }
                    // If encryption is on, the next byte is a checksum of the header
                    int ofs = 0;
                    if (wantingChecksum && (encType != EncryptionType.None))
                    {
                        byte checksum = buf[0];
                        ofs++;
                        wantingChecksum = false;
                        byte headersum = 0;
                        for (int i = 0; i < 8; i++)
                        {
                            headersum += msgheader[i];
                        }
                        if (checksum != headersum)
                        {
                            Close();
                            throw new IOException("Header checksum failed! (was " + checksum + ", calculated " + headersum + ")");
                        }
                    }
                    bytes.Add(buf, ofs, read - ofs - copied);
                    if (encType != EncryptionType.None)
                    {
                        length++;
                    }
                    // checksum byte
                    // Now we know we are reading into the body of the message
                    SystemLogger.Log(SystemLogger.Module.CORE, ID + " Added " + (read - ofs - copied) + " bytes, have " + bytes.Length + " of " + length);

                    if (OnPartialMessage != null)
                    {
                        OnPartialMessage(this, code, buf, ofs, read - ofs - copied, bytes.Length, length);
                    }

                    if (bytes.Length >= length)
                    {
                        // A message was received!
                        headerread      = 0;
                        wantingChecksum = true;
                        byte[] msg = bytes.Read(0, length);
                        if (encType != EncryptionType.None)
                        {
                            byte checksum = msg[length - 1], msgsum = 0;
                            for (int i = 0; i < length - 1; i++)
                            {
                                msgsum += msg[i];
                            }
                            if (checksum != msgsum)
                            {
                                Close();
                                throw new IOException("Content checksum failed! (was " + checksum + ", calculated " + msgsum + ")");
                            }
                            OnReadMessage(this, code, msg, length - 1);
                        }
                        else
                        {
                            OnReadMessage(this, code, msg, length);
                        }
                        // Don't forget to put the rest through the mill
                        int togo = bytes.Length - length;
                        if (togo > 0)
                        {
                            byte[] whatsleft = bytes.Read(length, togo);
                            bytes.Clear();
                            ReadInternal(whatsleft, whatsleft.Length, true);
                        }
                        else
                        {
                            bytes.Clear();
                        }
                    }
                    //if(OnStatus != null) OnStatus(this, bytes.Length, length);
                    break;
                }
            }
        }
Esempio n. 2
0
        public void SendMessage(uint code, byte[] bytes, byte paramType, int len)
        {
            if (paramType > 0) {
                ByteBuilder b = new ByteBuilder (3);
                b.AddParameter (bytes, paramType);
                bytes = b.Read (0, b.Length);
                len = bytes.Length;
            }

            lock (sock) {
                byte checksum = 0;
                byte[] ba;
                switch (MessageType) {
                case MessageType.CodeAndLength:

                    Send (ba = UintToBytes (code));

                    for (int i = 0; i < 4; i++) checksum += ba[i];
                    Send (ba = IntToBytes (len));

                    for (int i = 0; i < 4; i++) checksum += ba[i];
                    if (encType != EncryptionType.None) {
                        Send (new byte[] { checksum });
                    }
                    break;
                case MessageType.Length:
                    Send (ba = IntToBytes (len));
                    for (int i = 0; i < 4; i++) {
                        checksum += ba[i];
                    }
                    if (encType != EncryptionType.None) {
                        Send (new byte[] { checksum });
                    }
                    break;
                }
                Send (bytes, len);

                if (encType != EncryptionType.None) {
                    checksum = 0;
                    for (int i = 0; i < len; i++) {
                        checksum += bytes[i];
                    }
                    Send (new byte[] { checksum });
                }
            }
        }
Esempio n. 3
0
        public void SendMessage(uint code, byte[] bytes, byte paramType, int len)
        {
            if (paramType > 0)
            {
                ByteBuilder b = new ByteBuilder(3);
                b.AddParameter(bytes, paramType);
                bytes = b.Read(0, b.Length);
                len   = bytes.Length;
            }


            lock (sock) {
                byte   checksum = 0;
                byte[] ba;
                switch (MessageType)
                {
                case MessageType.CodeAndLength:

                    Send(ba = UintToBytes(code));

                    for (int i = 0; i < 4; i++)
                    {
                        checksum += ba[i];
                    }
                    Send(ba = IntToBytes(len));

                    for (int i = 0; i < 4; i++)
                    {
                        checksum += ba[i];
                    }
                    if (encType != EncryptionType.None)
                    {
                        Send(new byte[] { checksum });
                    }
                    break;

                case MessageType.Length:
                    Send(ba = IntToBytes(len));
                    for (int i = 0; i < 4; i++)
                    {
                        checksum += ba[i];
                    }
                    if (encType != EncryptionType.None)
                    {
                        Send(new byte[] { checksum });
                    }
                    break;
                }
                Send(bytes, len);

                if (encType != EncryptionType.None)
                {
                    checksum = 0;
                    for (int i = 0; i < len; i++)
                    {
                        checksum += bytes[i];
                    }
                    Send(new byte[] { checksum });
                }
            }
        }