Пример #1
0
 public void Resume(byte[] data, int size)
 {
     Start    = 0;
     ReadSize = size;
     data.CopyTo(ReadBuffer, 0);
     ReadStatus = G2ReadResult.PACKET_INCOMPLETE;
 }
Пример #2
0
        public static bool ReadPayload(G2Header packet)
        {
            ResetPacket(packet);

            G2Header child = new G2Header(packet.Data);

            G2ReadResult streamStatus = G2ReadResult.PACKET_GOOD;

            while (streamStatus == G2ReadResult.PACKET_GOOD)
            {
                streamStatus = ReadNextChild(packet, child);
            }

            if (streamStatus == G2ReadResult.STREAM_END)
            {
                if (packet.NextBytesLeft > 0)
                {
                    packet.PayloadPos  = packet.NextBytePos;
                    packet.PayloadSize = packet.NextBytesLeft;

                    return(true);
                }
            }
            else if (packet.NextBytesLeft > 0)
            {
                // Payload Read Error
                //m_pG2Comm->m_pCore->DebugLog("G2 Network", "Payload Read Error: " + HexDump(packet.Packet, packet.PacketSize));
            }

            return(false);
        }
Пример #3
0
        public byte[] Break()
        {
            byte[] remaining = Utilities.ExtractBytes(ReadBuffer, Start, ReadSize);

            ReadSize   = 0;
            ReadStatus = G2ReadResult.PACKET_INCOMPLETE;

            return(remaining);
        }
Пример #4
0
        G2ReceivedPacket LastPacket; //crit delete

        void ReceivePackets()
        {
            int          Start        = 0;
            G2ReadResult streamStatus = G2ReadResult.PACKET_GOOD;

            while (streamStatus == G2ReadResult.PACKET_GOOD)
            {
                G2ReceivedPacket packet = new G2ReceivedPacket();
                packet.Root = new G2Header(FinalRecvBuffer);

                streamStatus = G2Protocol.ReadNextPacket(packet.Root, ref Start, ref FinalRecvBuffSize);

                if (streamStatus != G2ReadResult.PACKET_GOOD)
                {
                    break;
                }

                packet.Tcp    = this;
                packet.Source = new DhtContact(this, RemoteIP);

                // extract data from final recv buffer so it can be referenced without being overwritten by this thread
                byte[] extracted = Utilities.ExtractBytes(packet.Root.Data, packet.Root.PacketPos, packet.Root.PacketSize);
                packet.Root = new G2Header(extracted);
                G2Protocol.ReadPacket(packet.Root);

                LastPacket = packet;

                PacketLogEntry logEntry = new PacketLogEntry(Core.TimeNow, TransportProtocol.Tcp, DirectionType.In, packet.Source, packet.Root.Data);
                Network.LogPacket(logEntry);

                Network.IncomingPacket(packet);
            }

            // re-align buffer
            if (Start > 0 && FinalRecvBuffSize > 0)
            {
                Buffer.BlockCopy(FinalRecvBuffer, Start, FinalRecvBuffer, 0, FinalRecvBuffSize);
                //Network.UpdateConsole(PacketBytesReady.ToString() + " bytes moved to front of receive buffer");
            }
        }
Пример #5
0
        private bool ReadNext(G2Header root)
        {
            if (ReadSize > 0)
            {
                int prevStart = Start;

                ReadStatus = G2Protocol.ReadNextPacket(root, ref Start, ref ReadSize);

                ParentPos += (Start - prevStart);

                if (ReadStatus == G2ReadResult.PACKET_GOOD)
                {
                    return(true);
                }
            }

            // hit the exact end of the buffer read in, signal to read the next buffer in
            else
            {
                ReadStatus = G2ReadResult.PACKET_INCOMPLETE;
            }

            return(false);
        }
Пример #6
0
        public void OnReceive()
        {
            if (ReceiveBuffer == null)
            {
                ReceiveBuffer = new byte[BUFF_SIZE];
            }

            int recvd = Comm.Receive(ReceiveBuffer, RecvBuffSize, BUFF_SIZE - RecvBuffSize);

            if (recvd <= 0)
            {
                return;
            }

            int start = 0;

            RecvBuffSize += recvd;

            // get next packet
            G2ReadResult streamStatus = G2ReadResult.PACKET_GOOD;

            while (streamStatus == G2ReadResult.PACKET_GOOD)
            {
                G2ReceivedPacket packet = new G2ReceivedPacket();

                start = 0;

                // if encryption off
                if (RecvDecryptor == null)
                {
                    packet.Root  = new G2Header(ReceiveBuffer);
                    streamStatus = G2Protocol.ReadNextPacket(packet.Root, ref start, ref RecvBuffSize);

                    if (streamStatus != G2ReadResult.PACKET_GOOD)
                    {
                        break;
                    }

                    PacketLogEntry logEntry = new PacketLogEntry(Core.TimeNow, TransportProtocol.Rudp, DirectionType.In, Comm.PrimaryAddress.Address, Utilities.ExtractBytes(packet.Root.Data, packet.Root.PacketPos, packet.Root.PacketSize));
                    Core.Network.LogPacket(logEntry);

                    ReceivePacket(packet);

                    if (start > 0 && RecvBuffSize > 0)
                    {
                        Buffer.BlockCopy(ReceiveBuffer, start, ReceiveBuffer, 0, RecvBuffSize);
                    }
                }

                // else if encryption on
                else
                {
                    // if data needs to be decrypted from receive buffer
                    if (RecvBuffSize >= RecvBlockSize)
                    {
                        int transLength = RecvBuffSize - (RecvBuffSize % RecvBlockSize);
                        int spaceAvail  = BUFF_SIZE - DecryptBuffSize;

                        if (spaceAvail < transLength)
                        {
                            transLength = spaceAvail - (spaceAvail % RecvBlockSize);
                        }

                        if (transLength >= RecvBlockSize)
                        {
                            if (DecryptBuffer == null)
                            {
                                DecryptBuffer = new byte[BUFF_SIZE];
                            }

                            int transformed = RecvDecryptor.TransformBlock(ReceiveBuffer, 0, transLength, DecryptBuffer, DecryptBuffSize);
                            Debug.Assert(transformed == transLength);

                            DecryptBuffSize += transformed;
                            RecvBuffSize    -= transLength;

                            Buffer.BlockCopy(ReceiveBuffer, transLength, ReceiveBuffer, 0, RecvBuffSize);
                        }
                    }

                    // read packets from decrypt buffer
                    packet.Root = new G2Header(DecryptBuffer);

                    //crit - delete
                    //int lastStart = start;
                    //int lastBuffSize = DecryptBuffSize;
                    //byte[] lastBuffer = Utilities.ExtractBytes(DecryptBuffer, 0, DecryptBuffSize);

                    streamStatus = G2Protocol.ReadNextPacket(packet.Root, ref start, ref DecryptBuffSize);

                    if (streamStatus == G2ReadResult.PACKET_ERROR)
                    {
                        //crit - debug this
                        Send_Close("Session Packet Error");
                        break;
                    }

                    if (streamStatus != G2ReadResult.PACKET_GOOD)
                    {
                        break;
                    }

                    PacketLogEntry logEntry = new PacketLogEntry(Core.TimeNow, TransportProtocol.Rudp, DirectionType.In, Comm.PrimaryAddress.Address, Utilities.ExtractBytes(packet.Root.Data, packet.Root.PacketPos, packet.Root.PacketSize));
                    Core.Network.LogPacket(logEntry);

                    ReceivePacket(packet);

                    if (start > 0 && DecryptBuffSize > 0)
                    {
                        Buffer.BlockCopy(DecryptBuffer, start, DecryptBuffer, 0, DecryptBuffSize);
                    }
                }
            }
        }
Пример #7
0
        private static string LoadProfile(ProfileService service, OpProfile profile, string tempPath, Dictionary <string, string> textFields, Dictionary <string, string> fileFields)
        {
            string template = null;

            textFields.Clear();
            textFields["local_help"] = (profile.UserID == service.Core.UserID) ? "<font size=2>Right-click or click <a href='http://edit'>here</a> to Edit</font>" : "";

            if (fileFields != null)
            {
                fileFields.Clear();
            }

            if (!profile.Loaded)
            {
                service.LoadProfile(profile.UserID);
            }

            try
            {
                using (TaggedStream stream = new TaggedStream(service.GetFilePath(profile), service.Core.GuiProtocol))
                    using (IVCryptoStream crypto = IVCryptoStream.Load(stream, profile.File.Header.FileKey))
                    {
                        int    buffSize  = 4096;
                        byte[] buffer    = new byte[4096];
                        long   bytesLeft = profile.EmbeddedStart;
                        while (bytesLeft > 0)
                        {
                            int readSize = (bytesLeft > (long)buffSize) ? buffSize : (int)bytesLeft;
                            int read     = crypto.Read(buffer, 0, readSize);
                            bytesLeft -= (long)read;
                        }

                        // load file
                        foreach (ProfileAttachment attached in profile.Attached)
                        {
                            if (attached.Name.StartsWith("template"))
                            {
                                byte[] html = new byte[attached.Size];
                                crypto.Read(html, 0, (int)attached.Size);

                                UTF8Encoding utf = new UTF8Encoding();
                                template = utf.GetString(html);
                            }

                            else if (attached.Name.StartsWith("fields"))
                            {
                                byte[] data = new byte[attached.Size];
                                crypto.Read(data, 0, (int)attached.Size);

                                int          start = 0, length = data.Length;
                                G2ReadResult streamStatus = G2ReadResult.PACKET_GOOD;

                                while (streamStatus == G2ReadResult.PACKET_GOOD)
                                {
                                    G2ReceivedPacket packet = new G2ReceivedPacket();
                                    packet.Root = new G2Header(data);

                                    streamStatus = G2Protocol.ReadNextPacket(packet.Root, ref start, ref length);

                                    if (streamStatus != G2ReadResult.PACKET_GOOD)
                                    {
                                        break;
                                    }

                                    if (packet.Root.Name == ProfilePacket.Field)
                                    {
                                        ProfileField field = ProfileField.Decode(packet.Root);

                                        if (field.Value == null)
                                        {
                                            continue;
                                        }

                                        if (field.FieldType == ProfileFieldType.Text)
                                        {
                                            textFields[field.Name] = UTF8Encoding.UTF8.GetString(field.Value);
                                        }
                                        else if (field.FieldType == ProfileFieldType.File && fileFields != null)
                                        {
                                            fileFields[field.Name] = UTF8Encoding.UTF8.GetString(field.Value);
                                        }
                                    }
                                }
                            }

                            else if (attached.Name.StartsWith("file=") && fileFields != null)
                            {
                                string name = attached.Name.Substring(5);

                                try
                                {
                                    string fileKey = null;
                                    foreach (string key in fileFields.Keys)
                                    {
                                        if (name == fileFields[key])
                                        {
                                            fileKey = key;
                                            break;
                                        }
                                    }

                                    fileFields[fileKey] = tempPath + Path.DirectorySeparatorChar + name;

                                    using (FileStream extract = new FileStream(fileFields[fileKey], FileMode.CreateNew, FileAccess.Write))
                                    {
                                        long   remaining = attached.Size;
                                        byte[] buff      = new byte[2096];

                                        while (remaining > 0)
                                        {
                                            int read = (remaining > 2096) ? 2096 : (int)remaining;
                                            remaining -= read;

                                            crypto.Read(buff, 0, read);
                                            extract.Write(buff, 0, read);
                                        }
                                    }
                                }
                                catch
                                { }
                            }
                        }
                    }
            }
            catch (Exception)
            {
            }

            return(template);
        }
Пример #8
0
        private bool ReadNext(G2Header root)
        {
            if (ReadSize > 0)
            {
                int prevStart = Start;

                ReadStatus = G2Protocol.ReadNextPacket(root, ref Start, ref ReadSize);

                ParentPos += (Start - prevStart);

                if (ReadStatus == G2ReadResult.PACKET_GOOD)
                    return true;
            }

            // hit the exact end of the buffer read in, signal to read the next buffer in
            else
                ReadStatus = G2ReadResult.PACKET_INCOMPLETE;

            return false;
        }
Пример #9
0
 public void Resume(byte[] data, int size)
 {
     Start = 0;
     ReadSize = size;
     data.CopyTo(ReadBuffer, 0);
     ReadStatus = G2ReadResult.PACKET_INCOMPLETE;
 }
Пример #10
0
        public byte[] Break()
        {
            byte[] remaining = Utilities.ExtractBytes(ReadBuffer, Start, ReadSize);

            ReadSize = 0;
            ReadStatus = G2ReadResult.PACKET_INCOMPLETE;

            return remaining;
        }