Пример #1
0
        public static CachedIP Decode(G2Header root)
        {
            CachedIP saved = new CachedIP();

            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Contact:
                        saved.Contact = DhtContact.ReadPacket(child);
                        break;

                    case Packet_LastSeen:
                        saved.LastSeen = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos));
                        break;

                    case Packet_Bootstrap:
                        saved.Bootstrap = BitConverter.ToBoolean(child.Data, child.PayloadPos);
                        break;
                }
            }

            saved.Contact.LastSeen = saved.LastSeen;

            return saved;
        }
Пример #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 static LinkData Decode(G2Header root)
        {
            LinkData link = new LinkData();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Project:
                        link.Project = BitConverter.ToUInt32(child.Data, child.PayloadPos);
                        break;

                    case Packet_Target:
                        link.Target = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        link.TargetID = Utilities.KeytoID(link.Target);
                        break;

                    case Packet_Uplink:
                        link.Uplink = BitConverter.ToBoolean(child.Data, child.PayloadPos);
                        break;

                    case Packet_Title:
                        link.Title = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return link;
        }
Пример #4
0
        public static MessageData Decode(G2Header root)
        {
            MessageData msg = new MessageData();

            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Text:
                        msg.Text = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Format:
                        msg.Format = (TextFormat)CompactNum.ToInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_TargetID:
                        msg.TargetID = BitConverter.ToUInt64(child.Data, child.PayloadPos);
                        break;
                }
            }

            return msg;
        }
Пример #5
0
        public static AudioPacket Decode(G2Header root)
        {
            AudioPacket packet = new AudioPacket();

            if (G2Protocol.ReadPayload(root))
                packet.Audio = Utilities.ExtractBytes(root.Data, root.PayloadPos, root.PayloadSize);

            G2Protocol.ResetPacket(root);

            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_FrameSize:
                        packet.FrameSize = CompactNum.ToInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return packet;
        }
Пример #6
0
        public static ChatInvite Decode(G2Header root)
        {
            ChatInvite invite = new ChatInvite();

            G2Protocol.ResetPacket(root);

            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_RoomID:
                        invite.RoomID = BitConverter.ToUInt64(child.Data, child.PayloadPos);
                        break;

                    case Packet_Title:
                        invite.Title = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Host:
                        invite.Host = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_SignedInvite:
                        invite.SignedInvite = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return invite;
        }
Пример #7
0
        public static void ResetPacket(G2Header packet)
        {
            packet.NextBytePos   = packet.InternalPos;
            packet.NextBytesLeft = packet.InternalSize;

            packet.PayloadPos  = 0;
            packet.PayloadSize = 0;
        }
Пример #8
0
        public static G2ReadResult ReadNextChild(G2Header root, G2Header child)
        {
            if (!root.HasChildren)
            {
                return(G2ReadResult.STREAM_END);
            }

            return(ReadNextPacket(child, ref root.NextBytePos, ref root.NextBytesLeft));
        }
Пример #9
0
        public static bool ReadPacket(G2Header root)
        {
            int start  = 0;
            int length = root.Data.Length;

            if (G2ReadResult.PACKET_GOOD == ReadNextPacket(root, ref start, ref length))
            {
                return(true);
            }

            return(false);
        }
Пример #10
0
        public static StorageFile Decode(byte[] data)
        {
            G2Header root = new G2Header(data);

            if (!G2Protocol.ReadPacket(root))
                return null;

            if (root.Name != StoragePacket.File)
                return null;

            return StorageFile.Decode(root);
        }
Пример #11
0
        public static FileDetails Decode(byte[] data)
        {
            G2Header root = new G2Header(data);

            if (!G2Protocol.ReadPacket(root))
                return null;

            if (root.Name != TransferPacket.Params)
                return null;

            return FileDetails.Decode(root);
        }
Пример #12
0
        public static PlanBlock Decode(byte[] data)
        {
            G2Header root = new G2Header(data);

            if (!G2Protocol.ReadPacket(root))
                return null;

            if (root.Name != PlanPacket.Block)
                return null;

            return PlanBlock.Decode(root);
        }
Пример #13
0
        public static CommClose Decode(G2ReceivedPacket packet)
        {
            CommClose close = new CommClose();

            G2Header child = new G2Header(packet.Root.Data);

            while( G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD )
            {
                if (child.Name == Packet_Message)
                    if( G2Protocol.ReadPayload(child) )
                        close.Reason = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
            }

            return close;
        }
Пример #14
0
        public static PlanBlock Decode(G2Header root)
        {
            PlanBlock block = new PlanBlock();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_ProjectID:
                        block.ProjectID = BitConverter.ToUInt32(child.Data, child.PayloadPos);
                        break;

                    case Packet_Title:
                        block.Title = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_StartTime:
                        block.StartTime = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos));
                        break;

                    case Packet_EndTime:
                        block.EndTime = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos));
                        break;

                    case Packet_Description:
                        block.Description = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Scope:
                        block.Scope = BitConverter.ToInt16(child.Data, child.PayloadPos);
                        break;

                    case Packet_Unique:
                        block.Unique = BitConverter.ToInt32(child.Data, child.PayloadPos);
                        break;
                }
            }

            return block;
        }
Пример #15
0
        public static MailAck Decode(G2Header root)
        {
            MailAck ack = new MailAck();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_MailID:
                        ack.MailID = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Source:
                        ack.Source = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        ack.SourceID = Utilities.KeytoID(ack.Source);
                        break;

                    case Packet_Target:
                        ack.Target = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        ack.TargetID = Utilities.KeytoID(ack.Target);
                        break;

                    case Packet_TargetVersion:
                        ack.TargetVersion = BitConverter.ToUInt32(child.Data, child.PayloadPos);
                        break;

                    case Packet_SourceVersion:
                        ack.SourceVersion = BitConverter.ToUInt32(child.Data, child.PayloadPos);
                        break;
                }
            }

            return ack;
        }
Пример #16
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);
        }
Пример #17
0
        public static FileDetails Decode(G2Header root)
        {
            FileDetails packet = new FileDetails();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Service:
                        packet.Service = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_DataType:
                        packet.DataType = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Hash:
                        packet.Hash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Size:
                        packet.Size = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Extra:
                        packet.Extra = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return packet;
        }
Пример #18
0
        public bool ReadPacket(ref G2Header root)
        {
            root = new G2Header(ReadBuffer);

            // continue from left off, read another goo packete
            if (ReadNext(root))
            {
                return(true);
            }

            if (ReadStatus != G2ReadResult.PACKET_INCOMPLETE)
            {
                return(false);
            }

            // re-align
            if (ReadSize > 0)
            {
                Buffer.BlockCopy(ReadBuffer, Start, ReadBuffer, 0, ReadSize);
            }

            // incomplete, or just started, read some more from file
            Start = 0;

            int read = ParentStream.Read(ReadBuffer, ReadSize, ReadBuffer.Length - ReadSize);

            Pos      += read;
            ReadSize += read;

            if (ReadNext(root))
            {
                return(true);
            }

            return(false);
        }
Пример #19
0
        public static ProfileAttachment Decode(G2Header root)
        {
            ProfileAttachment file = new ProfileAttachment();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Name:
                        file.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Size:
                        file.Size = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return file;
        }
Пример #20
0
        public static VersionedFileHeader Decode(G2Header root)
        {
            VersionedFileHeader header = new VersionedFileHeader();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Key:
                        header.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        header.KeyID = Utilities.KeytoID(header.Key);
                        break;

                    case Packet_Version:
                        header.Version = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_FileHash:
                        header.FileHash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_FileSize:
                        header.FileSize = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_FileKey:
                        header.FileKey = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Extra:
                        header.Extra = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return header;
        }
Пример #21
0
        public static LocationPing Decode(G2Header root)
        {
            LocationPing ping = new LocationPing();

            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_RemoteVersion:
                        ping.RemoteVersion = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return ping;
        }
Пример #22
0
        public static LocationData Decode(byte[] data)
        {
            G2Header root = new G2Header(data);

            G2Protocol.ReadPacket(root);

            if (root.Name != LocationPacket.Data)
                return null;

            LocationData loc = new LocationData();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Key:
                        loc.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        loc.UserID = Utilities.KeytoID(loc.Key);
                        break;

                    case Packet_Source:
                        loc.Source = DhtSource.ReadPacket(child);
                        loc.UserID = loc.Source.UserID; // encode light doesnt send full key
                        break;

                    case Packet_IP:
                        loc.IP = new IPAddress(Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize));
                        break;

                    case Packet_Proxies:
                        loc.Proxies.Add( DhtAddress.ReadPacket(child) );
                        break;

                    case Packet_Name:
                        loc.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Place:
                        loc.Place = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Version:
                        loc.Version = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_GMTOffset:
                        loc.GmtOffset = BitConverter.ToInt32(child.Data, child.PayloadPos);
                        break;

                    case Packet_Away:
                        loc.Away = BitConverter.ToBoolean(child.Data, child.PayloadPos);
                        break;

                    case Packet_AwayMsg:
                        loc.AwayMessage = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Tag:
                        loc.Tags.Add(PatchTag.FromBytes(child.Data, child.PayloadPos, child.PayloadSize));
                        break;

                    case Packet_TunnelClient:
                        loc.TunnelClient = TunnelAddress.FromBytes(child.Data, child.PayloadPos);
                        break;

                    case Packet_TunnelServers:
                        loc.TunnelServers.Add(DhtAddress.ReadPacket(child));
                        break;
                }
            }

            return loc;
        }
Пример #23
0
        public void LoadHeaders()
        {
            List<string> goodPaths = new List<string>();

            try
            {
                goodPaths.Add(HeaderPath);

                if (!File.Exists(HeaderPath))
                    return;

                using (IVCryptoStream crypto = IVCryptoStream.Load(HeaderPath, LocalKey))
                {
                    PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Read);

                    G2Header root = null;

                    while (stream.ReadPacket(ref root))
                        if (root.Name == DataPacket.SignedData)
                        {
                            SignedData signed = SignedData.Decode(root);
                            G2Header embedded = new G2Header(signed.Data);

                            // figure out data contained
                            if (G2Protocol.ReadPacket(embedded))
                                if (embedded.Name == DataPacket.VersionedFile)
                                {
                                    VersionedFileHeader header = VersionedFileHeader.Decode(embedded);

                                    if(header.FileHash != null)
                                        goodPaths.Add(GetFilePath(header));

                                    Process_VersionedFile(null, signed, header);
                                }
                        }
                }

                // remove loose files
                foreach (string testPath in Directory.GetFiles(CachePath))
                    if (!goodPaths.Contains(testPath))
                        try { File.Delete(testPath); }
                        catch { }
            }
            catch (Exception ex)
            {
                Core.Network.UpdateLog("VersionedFile", "Error loading data " + ex.Message);
            }
        }
Пример #24
0
        void Store_Local(DataReq store)
        {
            // getting published to - search results - patch

            SignedData signed = SignedData.Decode(store.Data);

            if (signed == null)
                return;

            G2Header embedded = new G2Header(signed.Data);

            // figure out data contained
            if (G2Protocol.ReadPacket(embedded))
                if (embedded.Name == DataPacket.VersionedFile)
                    Process_VersionedFile(store, signed, VersionedFileHeader.Decode(signed.Data));
        }
Пример #25
0
        public static G2ReadResult ReadNextPacket(G2Header packet, ref int readPos, ref int readSize)
        {
            if (readSize == 0)
            {
                return(G2ReadResult.PACKET_INCOMPLETE);
            }

            int beginPos  = readPos;
            int beginSize = readSize;

            packet.PacketPos = readPos;

            // Read Control Byte
            byte control = Buffer.GetByte(packet.Data, readPos);

            readPos  += 1;
            readSize -= 1;

            if (control == 0)
            {
                return(G2ReadResult.STREAM_END);
            }

            byte lenLen  = (byte)((control & 0xE0) >> 5);               // 11100000
            byte nameLen = (byte)((control & 0x18) >> 3);               // 00011000
            byte flags   = (byte)(control & 0x07);                      // 00000111

            bool bigEndian  = (flags & 0x02) != 0;
            bool isCompound = (flags & 0x04) != 0;

            if (bigEndian)
            {
                return(G2ReadResult.PACKET_ERROR);
            }

            packet.HasChildren = isCompound;

            // Read Packet Length
            packet.InternalSize = 0;
            if (lenLen != 0)
            {
                if (readSize < lenLen)
                {
                    readPos  = beginPos;
                    readSize = beginSize;
                    return(G2ReadResult.PACKET_INCOMPLETE);
                }

                byte[] lenData = new byte[8];                 // create here because lenLen is less than 8 in size
                Buffer.BlockCopy(packet.Data, readPos, lenData, 0, lenLen);

                packet.InternalSize = BitConverter.ToInt32(lenData, 0);                 // only 4 bytes supported so far

                Debug.Assert(MAX_FINAL_SIZE < G2_PACKET_BUFF);
                if (packet.InternalSize < 0 || MAX_FINAL_SIZE < packet.InternalSize)
                {
                    Debug.Assert(false);
                    return(G2ReadResult.PACKET_ERROR);
                }

                readPos  += lenLen;
                readSize -= lenLen;
            }

            // Read Packet Name
            if (readSize < nameLen)
            {
                readPos  = beginPos;
                readSize = beginSize;
                return(G2ReadResult.PACKET_INCOMPLETE);
            }

            if (nameLen != 1)
            {
                return(G2ReadResult.PACKET_ERROR);
            }

            /*if(packet.Name.Length + 1 + nameLen > MAX_NAME_SIZE - 1)
             * {
             *      Debug.Assert(false);
             *      packet.Name = "ERROR";
             * }
             * else
             * {
             *      packet.Name += "/" + StringEnc.GetString(packet.Data, readPos, nameLen);
             * }*/

            packet.Name = packet.Data[readPos];

            readPos  += nameLen;
            readSize -= nameLen;

            // Check if full packet length available in stream
            if (readSize < packet.InternalSize)
            {
                readPos  = beginPos;
                readSize = beginSize;
                return(G2ReadResult.PACKET_INCOMPLETE);
            }

            packet.InternalPos = (packet.InternalSize > 0) ? readPos : 0;

            packet.NextBytePos   = packet.InternalPos;
            packet.NextBytesLeft = packet.InternalSize;

            readPos  += packet.InternalSize;
            readSize -= packet.InternalSize;

            packet.PacketSize = 1 + lenLen + nameLen + packet.InternalSize;

            return(G2ReadResult.PACKET_GOOD);
        }
Пример #26
0
        public static ProfileField Decode(G2Header root)
        {
            ProfileField field = new ProfileField();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Type:
                        field.FieldType = (ProfileFieldType)child.Data[child.PayloadPos];
                        break;

                    case Packet_Name:
                        field.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Value:
                        field.Value = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return field;
        }
Пример #27
0
        public static SessionAck Decode(G2ReceivedPacket packet)
        {
            SessionAck sa = new SessionAck();

            G2Header child = new G2Header(packet.Root.Data);

            while( G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD )
            {
                if (child.Name == Packet_Name)
                    if (G2Protocol.ReadPayload(child))
                        sa.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
            }

            return sa;
        }
Пример #28
0
        public static SessionRequest Decode(G2ReceivedPacket packet)
        {
            SessionRequest sr = new SessionRequest();

            G2Header child = new G2Header(packet.Root.Data);

            while( G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD )
            {
                if (child.Name == Packet_Key)
                    if (G2Protocol.ReadPayload(child))
                        sr.EncryptedKey = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
            }

            return sr;
        }
Пример #29
0
        public static RudpPacket Decode(G2Header root)
        {
            RudpPacket gc = new RudpPacket();

            if( G2Protocol.ReadPayload(root) )
                gc.Payload = Utilities.ExtractBytes(root.Data, root.PayloadPos, root.PayloadSize);

            G2Protocol.ResetPacket(root);

            G2Header child = new G2Header(root.Data);

            while( G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD )
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_SenderDht:
                        gc.SenderID = BitConverter.ToUInt64(child.Data, child.PayloadPos);
                        break;

                    case Packet_SenderClient:
                        gc.SenderClient = BitConverter.ToUInt16(child.Data, child.PayloadPos);
                        break;

                    case Packet_TargetDht:
                        gc.TargetID = BitConverter.ToUInt64(child.Data, child.PayloadPos);
                        break;

                    case Packet_TargetClient:
                        gc.TargetClient = BitConverter.ToUInt16(child.Data, child.PayloadPos);
                        break;

                    case Packet_Type:
                        gc.PacketType = child.Data[child.PayloadPos];
                        break;

                    case Packet_ID:
                        gc.PeerID = BitConverter.ToUInt16(child.Data, child.PayloadPos);
                        break;

                    case Packet_Seq:
                        gc.Sequence = (byte)child.Data[child.PayloadPos];
                        break;

                    case Packet_Ident:
                        gc.Ident = BitConverter.ToUInt32(child.Data, child.PayloadPos);
                        break;

                    case Packet_To:
                        gc.ToAddress = DhtAddress.ReadPacket(child);
                        break;

                    case Packet_Proxy:
                        gc.RemoteProxy = DhtAddress.ReadPacket(child);
                        break;
                }
            }

            return gc;
        }
Пример #30
0
        public static ProxyUpdate Decode(G2ReceivedPacket packet)
        {
            ProxyUpdate update = new ProxyUpdate();

            G2Header child = new G2Header(packet.Root.Data);

            while (G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Proxy:
                        update.Proxy = DhtAddress.ReadPacket(child);
                        break;
                }
            }

            return update;
        }
Пример #31
0
        public static VersionedFileHeader Decode(byte[] data)
        {
            G2Header root = new G2Header(data);

            if (!G2Protocol.ReadPacket(root))
                return null;

            if (root.Name != DataPacket.VersionedFile)
                return null;

            return VersionedFileHeader.Decode(root);
        }
Пример #32
0
        public static KeyRequest Decode(G2ReceivedPacket packet)
        {
            KeyRequest kr = new KeyRequest();

            G2Header child = new G2Header(packet.Root.Data);

            while( G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD )
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                if (child.Name == Packet_Encryption)
                    kr.Encryption = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);

                else if (child.Name == Packet_Key)
                    kr.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);

                else if (child.Name == Packet_IV)
                    kr.IV = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
            }

            return kr;
        }
Пример #33
0
        public void ReadChildren(G2Header rootPacket, TreeNode rootNode, Type packetType)
        {
            G2Header child = new G2Header(rootPacket.Data);

            while( G2Protocol.ReadNextChild(rootPacket, child) == G2ReadResult.PACKET_GOOD )
            {
                string name = child.Name.ToString();

                if (packetType != null)
                    name += " : " + GetVariableName(packetType, child.Name);

                TreeNode childNode = rootNode.Nodes.Add(name);

                if(G2Protocol.ReadPayload(child))
                {
                    //childNode.Nodes.Add( "Payload Ascii: " + Utilities.BytestoAscii(childPacket.Data, childPacket.PayloadPos, childPacket.PayloadSize));
                    childNode.Nodes.Add(new DataNode(child.Data, child.PayloadPos, child.PayloadSize));
                }

                G2Protocol.ResetPacket(child);

                ReadChildren(child, childNode, null);
            }
        }
Пример #34
0
        public ListViewItem PackettoItem(PacketLogEntry logEntry)
        {
            // hash, protocol, direction, address, type, size
            string hash      = Utilities.BytestoHex(sha.ComputeHash( logEntry.Data), 0, 2, false);
            string protocol = logEntry.Protocol.ToString();

            // Network - Search / Search Req / Store ... - Component

            // Comm - Data / Ack / Syn

            // Rudp - Type - Component

            string name      = "?";

            G2Header root = new G2Header(logEntry.Data);

            if (G2Protocol.ReadPacket(root))
            {
                if(logEntry.Protocol == TransportProtocol.Rudp)
                {
                    name = TransportProtocol.Rudp.ToString() + " - ";

                    name += GetVariableName(typeof(CommPacket), root.Name);

                    if (root.Name == CommPacket.Data)
                    {
                        CommData data = CommData.Decode(root);

                        name += " - " + Network.Core.GetServiceName(data.Service);
                    }

                }
                else
                {
                    name = GetVariableName(typeof(RootPacket), root.Name) + " - ";

                    if (root.Name == RootPacket.Comm)
                    {
                        RudpPacket commPacket = RudpPacket.Decode(root);

                        name += GetVariableName(typeof(RudpPacketType), commPacket.PacketType);
                    }

                    if (root.Name == RootPacket.Network)
                    {
                        NetworkPacket netPacket = NetworkPacket.Decode(root);

                        G2Header internalRoot = new G2Header(netPacket.InternalData);
                        if (G2Protocol.ReadPacket(internalRoot))
                        {
                            name += GetVariableName(typeof(NetworkPacket), internalRoot.Name);

                            uint id = 0;
                            G2ReceivedPacket wrap = new G2ReceivedPacket();
                            wrap.Root = internalRoot;

                            // search request / search acks / stores have component types
                            if (internalRoot.Name == NetworkPacket.SearchRequest)
                            {
                                SearchReq req = SearchReq.Decode(wrap);
                                id = req.Service;
                            }

                            if (internalRoot.Name == NetworkPacket.SearchAck)
                            {
                                SearchAck ack = SearchAck.Decode(wrap);
                                id = ack.Service;
                            }

                            if (internalRoot.Name == NetworkPacket.StoreRequest)
                            {
                                StoreReq store = StoreReq.Decode(wrap);
                                id = store.Service;
                            }

                            if(id != 0)
                                name += " - " + Network.Core.GetServiceName(id); // GetVariableName(typeof(ServiceID), id);
                        }
                    }
                }
            }

            string time = logEntry.Time.ToString("HH:mm:ss:ff");

            string address = (logEntry.Address == null) ? "Broadcast" : logEntry.Address.ToString();
            return new PacketListViewItem(logEntry, new string[] { time, protocol, address, name, logEntry.Data.Length.ToString(), hash }, logEntry.Direction == DirectionType.In );
        }
Пример #35
0
        public static CommData Decode(G2Header root)
        {
            CommData data = new CommData();

            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Service:
                        data.Service = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_DataType:
                        data.DataType = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Data:
                        data.Data = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return data;
        }
Пример #36
0
        private void ListViewPackets_Click(object sender, System.EventArgs e)
        {
            if(ListViewPackets.SelectedItems.Count == 0)
                return;

            PacketListViewItem selected = (PacketListViewItem) ListViewPackets.SelectedItems[0];

            // build tree
            TreeViewPacket.Nodes.Clear();

            G2Header root = new G2Header(selected.LogEntry.Data);

            if (G2Protocol.ReadPacket(root))
            {
                if (selected.LogEntry.Protocol == TransportProtocol.Rudp)
                {
                    string name = root.Name.ToString() + " : " + GetVariableName(typeof(CommPacket), root.Name);
                    TreeNode rootNode = TreeViewPacket.Nodes.Add(name);

                    if (G2Protocol.ReadPayload(root))
                        rootNode.Nodes.Add(new DataNode(root.Data, root.PayloadPos, root.PayloadSize));

                    G2Protocol.ResetPacket(root);

                    // get type
                    Type packetType = null;
                    switch (root.Name)
                    {
                        case CommPacket.SessionRequest:
                            packetType = typeof(SessionRequest);
                            break;
                        case CommPacket.SessionAck:
                            packetType = typeof(SessionAck);
                            break;
                        case CommPacket.KeyRequest:
                            packetType = typeof(KeyRequest);
                            break;
                        case CommPacket.KeyAck:
                            packetType = typeof(KeyAck);
                            break;
                        case CommPacket.Data:
                            packetType = typeof(CommData);
                            break;
                        case CommPacket.Close:
                            packetType = typeof(CommClose);
                            break;
                    }

                    ReadChildren(root, rootNode, packetType);
                }
                else
                {
                    string name = root.Name.ToString() + " : " + GetVariableName(typeof(RootPacket), root.Name);
                    TreeNode rootNode = TreeViewPacket.Nodes.Add(name);

                    DataNode payloadNode = null;
                    if (G2Protocol.ReadPayload(root))
                    {
                        payloadNode = new DataNode(root.Data, root.PayloadPos, root.PayloadSize);
                        rootNode.Nodes.Add(payloadNode);
                    }
                    G2Protocol.ResetPacket(root);

                    if (root.Name == RootPacket.Comm)
                    {
                        ReadChildren(root, rootNode, typeof(RudpPacket));
                    }

                    if (root.Name == RootPacket.Tunnel)
                    {
                        ReadChildren(root, rootNode, typeof(RudpPacket));
                    }

                    if (root.Name == RootPacket.Network)
                    {
                        ReadChildren(root, rootNode, typeof(NetworkPacket));

                        G2Header payloadRoot = new G2Header(payloadNode.Data);
                        if (payloadNode.Data != null && G2Protocol.ReadPacket(payloadRoot))
                        {
                            name = payloadRoot.Name.ToString() + " : " + GetVariableName(typeof(NetworkPacket), payloadRoot.Name);
                            TreeNode netNode = payloadNode.Nodes.Add(name);

                            if (G2Protocol.ReadPayload(payloadRoot))
                                netNode.Nodes.Add(new DataNode(payloadRoot.Data, payloadRoot.PayloadPos, payloadRoot.PayloadSize));
                            G2Protocol.ResetPacket(payloadRoot);

                            Type packetType = null;
                            switch (payloadRoot.Name)
                            {
                                case NetworkPacket.SearchRequest:
                                    packetType = typeof(SearchReq);
                                    break;
                                case NetworkPacket.SearchAck:
                                    packetType = typeof(SearchAck);
                                    break;
                                case NetworkPacket.StoreRequest:
                                    packetType = typeof(StoreReq);
                                    break;
                                case NetworkPacket.Ping:
                                    packetType = typeof(Ping);
                                    break;
                                case NetworkPacket.Pong:
                                    packetType = typeof(Pong);
                                    break;
                                case NetworkPacket.Bye:
                                    packetType = typeof(Bye);
                                    break;
                                case NetworkPacket.ProxyRequest:
                                    packetType = typeof(ProxyReq);
                                    break;
                                case NetworkPacket.ProxyAck:
                                    packetType = typeof(ProxyAck);
                                    break;
                                case NetworkPacket.CrawlRequest:
                                    packetType = typeof(CrawlRequest);
                                    break;
                                case NetworkPacket.CrawlAck:
                                    packetType = typeof(CrawlAck);
                                    break;

                            }

                            ReadChildren(payloadRoot, netNode, packetType);
                        }
                    }
                }
            }

            /*G2Header rootPacket = new G2Header(selected.LogEntry.Data);

            int start = 0;
            int size  = selected.LogEntry.Data.Length;
            if(G2ReadResult.PACKET_GOOD == Protocol.ReadNextPacket(rootPacket, ref start, ref size) )
            {
                TreeNode rootNode = TreeViewPacket.Nodes.Add( TrimName(rootPacket.Name.ToString()) );

                if(G2Protocol.ReadPayload(rootPacket))
                {
                    //rootNode.Nodes.Add( Utilities.BytestoAscii(rootPacket.Data, rootPacket.PayloadPos, rootPacket.PayloadSize));
                    rootNode.Nodes.Add( Utilities.BytestoHex(rootPacket.Data, rootPacket.PayloadPos, rootPacket.PayloadSize, true));
                }

                G2Protocol.ResetPacket(rootPacket);

                ReadChildren(rootPacket, rootNode, null);
            }*/

            TreeViewPacket.ExpandAll();
        }
Пример #37
0
        public static KeyAck Decode(G2ReceivedPacket packet)
        {
            KeyAck ka = new KeyAck();

            G2Header child = new G2Header(packet.Root.Data);

            while (G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue ;

                if (child.Name == Packet_Key)
                    ka.PublicKey = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
            }

            return ka;
        }