Ejemplo n.º 1
0
        public static ProxyAck Decode(G2ReceivedPacket packet)
        {
            ProxyAck pa = new ProxyAck();

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

            while (G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD)
            {
                switch (child.Name)
                {
                case Packet_Source:
                    if (G2Protocol.ReadPayload(child))
                    {
                        pa.Source = DhtSource.ReadPacket(child);
                    }
                    break;

                case Packet_Accept:
                    pa.Accept = true;
                    break;

                case Packet_Contacts:
                    if (G2Protocol.ReadPayload(child))
                    {
                        pa.ContactList.Add(DhtContact.ReadPacket(child));
                    }
                    break;
                }
            }

            return(pa);
        }
Ejemplo n.º 2
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame bdy = protocol.WritePacket(null, RootPacket.Comm, Payload);

                protocol.WritePacket(bdy, Packet_SenderDht, BitConverter.GetBytes(SenderID));
                protocol.WritePacket(bdy, Packet_SenderClient, BitConverter.GetBytes(SenderClient));
                protocol.WritePacket(bdy, Packet_TargetDht, BitConverter.GetBytes(TargetID));
                protocol.WritePacket(bdy, Packet_TargetClient, BitConverter.GetBytes(TargetClient));
                protocol.WritePacket(bdy, Packet_Type, BitConverter.GetBytes(PacketType));
                protocol.WritePacket(bdy, Packet_ID, BitConverter.GetBytes(PeerID));
                protocol.WritePacket(bdy, Packet_Seq, BitConverter.GetBytes(Sequence));

                if (Ident != 0)
                {
                    protocol.WritePacket(bdy, Packet_Ident, BitConverter.GetBytes(Ident));
                }

                if (ToAddress != null)
                {
                    ToAddress.WritePacket(protocol, bdy, Packet_To);
                }
                if (RemoteProxy != null)
                {
                    RemoteProxy.WritePacket(protocol, bdy, Packet_Proxy);
                }

                return(protocol.WriteFinish());
            }
        }
Ejemplo n.º 3
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame goal = protocol.WritePacket(null, PlanPacket.Goal, null);

                protocol.WritePacket(goal, Packet_Ident, BitConverter.GetBytes(Ident));
                protocol.WritePacket(goal, Packet_Project, BitConverter.GetBytes(Project));

                protocol.WritePacket(goal, Packet_BranchUp, BitConverter.GetBytes(BranchUp));
                protocol.WritePacket(goal, Packet_BranchDown, BitConverter.GetBytes(BranchDown));

                protocol.WritePacket(goal, Packet_Title, UTF8Encoding.UTF8.GetBytes(Title));
                protocol.WritePacket(goal, Packet_End, BitConverter.GetBytes(End.ToBinary()));
                protocol.WritePacket(goal, Packet_Desc, UTF8Encoding.UTF8.GetBytes(Description));

                protocol.WritePacket(goal, Packet_Person, BitConverter.GetBytes(Person));

                protocol.WritePacket(goal, Packet_Archived, BitConverter.GetBytes(Archived));

                protocol.WritePacket(goal, Packet_EstCompleted, BitConverter.GetBytes(EstCompleted));
                protocol.WritePacket(goal, Packet_EstTotal, BitConverter.GetBytes(EstTotal));

                return(protocol.WriteFinish());
            }
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
0
        public static LargeDataPacket Decode(G2Header root)
        {
            LargeDataPacket packet = new LargeDataPacket(root.Name, 0, null);

            // size packet
            G2Header child = new G2Header(root.Data);

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

                if (child.Name == Packet_Size)
                {
                    packet.Size = BitConverter.ToInt32(child.Data, child.PayloadPos);
                }

                if (child.Name == Packet_Hash)
                {
                    packet.Hash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                }
            }

            return(packet);
        }
Ejemplo n.º 6
0
        public static InvitePackage OpenInvite(byte[] decrypted, G2Protocol protocol)
        {
            // if we get down here, opening invite was success

            MemoryStream mem    = new MemoryStream(decrypted);
            PacketStream stream = new PacketStream(mem, protocol, FileAccess.Read);

            InvitePackage package = new InvitePackage();

            G2Header root = null;

            while (stream.ReadPacket(ref root))
            {
                if (root.Name == InvitePacket.Info)
                {
                    package.Info = OneWayInvite.Decode(root);
                }

                if (root.Name == InvitePacket.Contact)
                {
                    package.Contacts.Add(DhtContact.ReadPacket(root));
                }

                if (root.Name == InvitePacket.WebCache)
                {
                    package.Caches.Add(WebCache.Decode(root));
                }
            }

            return(package);
        }
Ejemplo n.º 7
0
        public static OneWayInvite Decode(G2Header root)
        {
            OneWayInvite invite = new OneWayInvite();

            G2Header child = new G2Header(root.Data);

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

                switch (child.Name)
                {
                case Packet_UserName:
                    invite.UserName = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

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

                case Packet_OpAccess:
                    invite.OpAccess = (AccessType)child.Data[child.PayloadPos];
                    break;

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

            return(invite);
        }
Ejemplo n.º 8
0
        public static SubHashPacket Decode(G2Header root)
        {
            SubHashPacket subhash = new SubHashPacket();

            G2Header child = new G2Header(root.Data);

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

                switch (child.Name)
                {
                case Packet_ChunkSize:
                    subhash.ChunkSize = BitConverter.ToInt32(child.Data, child.PayloadPos);
                    break;

                case Packet_TotalCount:
                    subhash.TotalCount = BitConverter.ToInt32(child.Data, child.PayloadPos);
                    break;

                case Packet_SubHashes:
                    subhash.SubHashes = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;
                }
            }

            return(subhash);
        }
Ejemplo n.º 9
0
        // cant write local info (read) because post is re-transmitted

        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame header = protocol.WritePacket(null, BoardPacket.PostHeader, null);

                protocol.WritePacket(header, Packet_Source, Source);
                protocol.WritePacket(header, Packet_Target, Target);

                protocol.WritePacket(header, Packet_ProjectID, BitConverter.GetBytes(ProjectID));
                protocol.WritePacket(header, Packet_PostID, BitConverter.GetBytes(PostID));
                protocol.WritePacket(header, Packet_ParentID, BitConverter.GetBytes(ParentID));
                protocol.WritePacket(header, Packet_Time, BitConverter.GetBytes(Time.ToBinary()));
                protocol.WritePacket(header, Packet_Scope, new byte[] { (byte)Scope });

                protocol.WritePacket(header, Packet_Version, BitConverter.GetBytes(Version));
                protocol.WritePacket(header, Packet_EditTime, BitConverter.GetBytes(EditTime.ToBinary()));
                protocol.WritePacket(header, Packet_Archived, BitConverter.GetBytes(Archived));

                protocol.WritePacket(header, Packet_FileKey, FileKey);
                protocol.WritePacket(header, Packet_FileHash, FileHash);
                protocol.WritePacket(header, Packet_FileSize, BitConverter.GetBytes(FileSize));
                protocol.WritePacket(header, Packet_FileStart, BitConverter.GetBytes(FileStart));

                return(protocol.WriteFinish());
            }
        }
Ejemplo n.º 10
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame invite = protocol.WritePacket(null, ChatPacket.Invite, null);

                protocol.WritePacket(invite, Packet_RoomID, BitConverter.GetBytes(RoomID));

                if (Host != null)
                {
                    protocol.WritePacket(invite, Packet_Host, Host);
                }

                if (SignedInvite != null)
                {
                    protocol.WritePacket(invite, Packet_SignedInvite, SignedInvite);
                }

                if (Title.Length > 0)
                {
                    protocol.WritePacket(invite, Packet_Title, UTF8Encoding.UTF8.GetBytes(Title));
                }

                return(protocol.WriteFinish());
            }
        }
Ejemplo n.º 11
0
        public static PostFile Decode(G2Header root)
        {
            PostFile file  = new PostFile();
            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);
        }
Ejemplo n.º 12
0
        public static PostInfo Decode(G2Header root)
        {
            PostInfo info  = new PostInfo();
            G2Header child = new G2Header(root.Data);

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

                switch (child.Name)
                {
                case Packet_Subject:
                    info.Subject = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

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

                case Packet_Quip:
                    info.Quip = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

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

            return(info);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
        public static TempData Decode(ulong target, byte[] data)
        {
            G2Header root = new G2Header(data);

            G2Protocol.ReadPacket(root);

            if (root.Name != TempPacket.Data)
            {
                return(null);
            }

            TempData temp  = new TempData(target);
            G2Header child = new G2Header(root.Data);

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

                switch (child.Name)
                {
                case Packet_TTL:
                    temp.TTL = CompactNum.ToInt32(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

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

            return(temp);
        }
Ejemplo n.º 15
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame ack = protocol.WritePacket(null, NetworkPacket.CrawlAck, null);

                Source.WritePacket(protocol, ack, Packet_Source);
                protocol.WritePacket(ack, Packet_Version, UTF8Encoding.UTF8.GetBytes(Version));
                protocol.WritePacket(ack, Packet_Uptime, BitConverter.GetBytes(Uptime));

                foreach (DhtContact proxy in ProxyServers)
                {
                    proxy.WritePacket(protocol, ack, Packet_ProxyServers);
                }

                foreach (DhtContact proxy in ProxyClients)
                {
                    proxy.WritePacket(protocol, ack, Packet_ProxyClients);
                }

                // network packet
                InternalData = protocol.WriteFinish();

                return(base.Encode(protocol));
            }
        }
Ejemplo n.º 16
0
        public byte[] Encode(G2Protocol protocol, bool local)
        {
            lock (protocol.WriteSection)
            {
                G2Frame header = protocol.WritePacket(null, MailPacket.MailHeader, null);

                protocol.WritePacket(header, Packet_Source, Source);
                protocol.WritePacket(header, Packet_Target, Target);
                protocol.WritePacket(header, Packet_FileKey, FileKey);
                protocol.WritePacket(header, Packet_FileHash, FileHash);
                protocol.WritePacket(header, Packet_FileSize, BitConverter.GetBytes(FileSize));
                protocol.WritePacket(header, Packet_SourceVersion, BitConverter.GetBytes(SourceVersion));
                protocol.WritePacket(header, Packet_TargetVersion, BitConverter.GetBytes(TargetVersion));
                protocol.WritePacket(header, Packet_MailID, MailID);
                protocol.WritePacket(header, Packet_ThreadID, BitConverter.GetBytes(ThreadID));

                if (local)
                {
                    protocol.WritePacket(header, Packet_LocalKey, LocalKey);
                    protocol.WritePacket(header, Packet_FileStart, BitConverter.GetBytes(FileStart));
                    protocol.WritePacket(header, Packet_Read, BitConverter.GetBytes(Read));
                    protocol.WritePacket(header, Packet_Received, BitConverter.GetBytes(Received.ToBinary()));
                }

                return(protocol.WriteFinish());
            }
        }
Ejemplo n.º 17
0
        public static void DecryptTagFile(string source, string destination, byte[] key, OpCore core)
        {
            int bufferSize = 4096;

            byte[] buffer = new byte[4096]; // needs to be 4k to packet stream break/resume work

            string     tempPath = (core != null) ? core.GetTempPath() : destination;
            G2Protocol protocol = (core != null) ? core.Network.Protocol : new G2Protocol();

            using (FileStream tempFile = new FileStream(tempPath, FileMode.Create))
                using (TaggedStream encFile = new TaggedStream(source, protocol))
                    using (IVCryptoStream stream = IVCryptoStream.Load(encFile, key))
                    {
                        int read = bufferSize;
                        while (read == bufferSize)
                        {
                            read = stream.Read(buffer, 0, bufferSize);
                            tempFile.Write(buffer, 0, read);
                        }
                    }

            // move to official path
            if (core == null)
            {
                return;
            }

            File.Copy(tempPath, destination, true);
            File.Delete(tempPath);
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
0
        public static ServiceData Decode(byte[] data)
        {
            G2Header root = new G2Header(data);

            G2Protocol.ReadPacket(root);

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

            ServiceData packet = new ServiceData();
            G2Header    child  = new G2Header(root.Data);

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

                switch (child.Name)
                {
                case Packet_Date:
                    packet.Date = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos));
                    break;

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

            return(packet);
        }
Ejemplo n.º 20
0
        public byte[] EncodeLight(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame loc = protocol.WritePacket(null, LocationPacket.Data, null);

                Source.WritePacket(protocol, loc, Packet_Source);
                protocol.WritePacket(loc, Packet_IP, IP.GetAddressBytes());

                foreach (DhtAddress proxy in Proxies)
                {
                    proxy.WritePacket(protocol, loc, Packet_Proxies);
                }

                if (TunnelClient != null)
                {
                    protocol.WritePacket(loc, Packet_TunnelClient, TunnelClient.ToBytes());
                }

                foreach (DhtAddress server in TunnelServers)
                {
                    server.WritePacket(protocol, loc, Packet_TunnelServers);
                }

                return(protocol.WriteFinish());
            }
        }
Ejemplo n.º 21
0
        public static ShareCollection Decode(G2Header header, ulong user)
        {
            ShareCollection root  = new ShareCollection(user);
            G2Header        child = new G2Header(header.Data);

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

                switch (child.Name)
                {
                case Packet_Hash:
                    root.Hash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

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

                case Packet_Key:
                    root.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;
                }
            }

            return(root);
        }
Ejemplo n.º 22
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame root = protocol.WritePacket(null, SharePacket.File, null);

                protocol.WritePacket(root, Packet_Name, UTF8Encoding.UTF8.GetBytes(Name));
                protocol.WritePacket(root, Packet_Hash, Hash);
                protocol.WritePacket(root, Packet_Size, CompactNum.GetBytes(Size));
                protocol.WritePacket(root, Packet_FileKey, FileKey);

                if (SaveLocal)
                {
                    if (SystemPath != null)
                    {
                        protocol.WritePacket(root, Packet_SystemPath, UTF8Encoding.UTF8.GetBytes(SystemPath));
                    }

                    if (Public)
                    {
                        protocol.WritePacket(root, Packet_Public, null);
                    }
                }

                return(protocol.WriteFinish());
            }
        }
Ejemplo n.º 23
0
        public static ProxyReq Decode(G2ReceivedPacket packet)
        {
            ProxyReq pr = new ProxyReq();

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

            while (G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD)
            {
                switch (child.Name)
                {
                case Packet_Source:
                    if (G2Protocol.ReadPayload(child))
                    {
                        pr.SenderID = BitConverter.ToUInt64(child.Data, child.PayloadPos);
                    }
                    break;

                case Packet_Blocked:
                    pr.Type = ProxyType.ClientBlocked;
                    break;

                case Packet_NAT:
                    pr.Type = ProxyType.ClientNAT;
                    break;
                }
            }

            return(pr);
        }
Ejemplo n.º 24
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame po = protocol.WritePacket(null, NetworkPacket.Pong, null);

                if (Source != null)
                {
                    Source.WritePacket(protocol, po, Packet_Source);
                }

                if (RemoteIP != null)
                {
                    protocol.WritePacket(po, Packet_RemoteIP, RemoteIP.GetAddressBytes());
                }

                if (Direct)
                {
                    protocol.WritePacket(po, Packet_Direct, null);
                }

                if (Version != 0)
                {
                    protocol.WritePacket(po, Packet_Version, CompactNum.GetBytes(Version));
                }


                // network packet
                InternalData = protocol.WriteFinish();

                return(base.Encode(protocol));
            }
        }
Ejemplo n.º 25
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame bye = protocol.WritePacket(null, NetworkPacket.Bye, null);

                protocol.WritePacket(bye, Packet_Source, BitConverter.GetBytes(SenderID));

                foreach (DhtContact contact in ContactList)
                {
                    contact.WritePacket(protocol, bye, Packet_Contacts);
                }

                if (Message != null)
                {
                    protocol.WritePacket(bye, Packet_Message, UTF8Encoding.UTF8.GetBytes(Message));
                }

                if (Reconnect)
                {
                    protocol.WritePacket(bye, Packet_Reconnect, null);
                }

                // network packet
                InternalData = protocol.WriteFinish();

                return(base.Encode(protocol));
            }
        }
Ejemplo n.º 26
0
        public static Ping Decode(G2ReceivedPacket packet)
        {
            Ping pi = new Ping();

            if (G2Protocol.ReadPayload(packet.Root))
            {
                pi.Ident = BitConverter.ToUInt16(packet.Root.Data, packet.Root.PayloadPos);
            }

            G2Protocol.ResetPacket(packet.Root);

            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_Source:
                    pi.Source = DhtSource.ReadPacket(child);
                    break;

                case Packet_RemoteIP:
                    pi.RemoteIP = new IPAddress(Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize));
                    break;
                }
            }

            return(pi);
        }
Ejemplo n.º 27
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame ack = protocol.WritePacket(null, NetworkPacket.SearchAck, null);

                Source.WritePacket(protocol, ack, Packet_Source);

                protocol.WritePacket(ack, Packet_SearchID, BitConverter.GetBytes(SearchID));
                protocol.WritePacket(ack, Packet_Service, CompactNum.GetBytes(Service));

                if (Proxied)
                {
                    protocol.WritePacket(ack, Packet_Proxied, null);
                }

                foreach (DhtContact contact in ContactList)
                {
                    contact.WritePacket(protocol, ack, Packet_Contacts);
                }

                foreach (byte[] value in ValueList)
                {
                    protocol.WritePacket(ack, Packet_Values, value);
                }

                InternalData = protocol.WriteFinish();

                return(base.Encode(protocol));
            }
        }
Ejemplo n.º 28
0
        public static DhtAddress ReadPacket(G2Header root)
        {
            // read payload
            DhtAddress address = new DhtAddress();

            address.UserID   = BitConverter.ToUInt64(root.Data, root.PayloadPos);
            address.ClientID = BitConverter.ToUInt16(root.Data, root.PayloadPos + 8);
            address.UdpPort  = BitConverter.ToUInt16(root.Data, root.PayloadPos + 10);

            // read packets
            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_IP:
                    address.IP = new IPAddress(Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize));
                    break;
                }
            }

            return(address);
        }
Ejemplo n.º 29
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);
        }
Ejemplo n.º 30
0
        private void Receive_Patch(DhtAddress source, byte[] data)
        {
            // invoke patch
            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                if (root.Name == StorePacket.Patch)
                {
                    PatchPacket packet = PatchPacket.Decode(root);

                    if (packet == null)
                    {
                        return;
                    }

                    foreach (PatchTag patch in packet.PatchData)
                    {
                        if (PatchEvent.Contains(patch.Service, patch.DataType))
                        {
                            PatchEvent[patch.Service, patch.DataType].Invoke(source, patch.Tag);
                        }
                    }
                }
            }
        }
Ejemplo n.º 31
0
 internal override byte[] Encode(G2Protocol protocol)
 {
     lock (protocol.WriteSection)
     {
         G2Frame root = protocol.WritePacket(null, SharingPacket.PublicRequest, null);
         return protocol.WriteFinish();
     }
 }
Ejemplo n.º 32
0
        internal override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame root = protocol.WritePacket(null, SharingPacket.File, null);

                protocol.WritePacket(root, Packet_Name, UTF8Encoding.UTF8.GetBytes(Name));
                protocol.WritePacket(root, Packet_Hash, Hash);
                protocol.WritePacket(root, Packet_Size, CompactNum.GetBytes(Size));
                protocol.WritePacket(root, Packet_FileKey, FileKey);

                if (SaveLocal)
                {
                    if (SystemPath != null)
                        protocol.WritePacket(root, Packet_SystemPath, UTF8Encoding.UTF8.GetBytes(SystemPath));

                    if (Public)
                        protocol.WritePacket(root, Packet_Public, null);
                }

                return protocol.WriteFinish();
            }
        }
Ejemplo n.º 33
0
        internal override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame root = protocol.WritePacket(null, SharingPacket.Collection, null);

                protocol.WritePacket(root, Packet_Hash, Hash);
                protocol.WritePacket(root, Packet_Size, CompactNum.GetBytes(Size));
                protocol.WritePacket(root, Packet_Key,  Key);

                return protocol.WriteFinish();
            }
        }