예제 #1
0
파일: IMService.cs 프로젝트: nandub/DeOps
        void Session_Data(RudpSession session, byte[] data)
        {
            IMStatus status = OpenStatus(session.UserID);


            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                if (root.Name == IMPacket.Message)
                {
                    MessageData message = MessageData.Decode(root);

                    if (message.TargetID != 0)
                    {
                        Debug.Assert(session.UserID == Core.UserID);
                        if (session.UserID != Core.UserID)
                        {
                            return;
                        }

                        status = OpenStatus(message.TargetID);
                    }

                    ProcessMessage(status, new InstantMessage(Core, session, message));
                }

                if (root.Name == IMPacket.Alive)
                {
                    status.SetTTL(session.ClientID, SessionTimeout * 2);
                }
            }
        }
예제 #2
0
        public 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 == LocationPacket.Data)
                {
                    LocationData location = LocationData.Decode(signed.Data);

                    if (Utilities.CheckSignedData(location.Key, signed.Data, signed.Signature))
                    {
                        Process_LocationData(store, signed, location);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
            }
        }
예제 #3
0
        void Session_Data(RudpSession session, byte[] data)
        {
            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                switch (root.Name)
                {
                case ChatPacket.Data:
                    ReceiveMessage(ChatText.Decode(root), session);
                    break;

                case ChatPacket.Status:
                    ReceiveStatus(ChatStatus.Decode(root), session);
                    break;

                case ChatPacket.Invite:
                    ReceiveInvite(ChatInvite.Decode(root), session);
                    break;

                case ChatPacket.Who:
                    ReceiveWho(ChatWho.Decode(root), session);
                    break;
                }
            }
        }
예제 #4
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);
                        }
                    }
                }
            }
        }
예제 #5
0
파일: TempCache.cs 프로젝트: nandub/DeOps
        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);
        }
예제 #6
0
파일: LocalSync.cs 프로젝트: nandub/DeOps
        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);
        }
예제 #7
0
파일: DhtNetwork.cs 프로젝트: nandub/DeOps
        public void ReceiveTunnelPacket(G2ReceivedPacket raw, TunnelPacket tunnel)
        {
            if (Core.InvokeRequired) // called from  lookup core's thread
            {
                Core.RunInCoreAsync(delegate() { ReceiveTunnelPacket(raw, tunnel); });
                return;
            }

            Debug.Assert(!IsLookup);

            if (IsLookup)
            {
                return;
            }

            // decrypt public packet
            if (Core.Sim == null || Core.Sim.Internet.TestEncryption) // turn off encryption during simulation
            {
                if (tunnel.Payload.Length < 16)
                {
                    throw new Exception("Not enough data received for IV");
                }

                tunnel.Payload = Utilities.DecryptBytes(tunnel.Payload, tunnel.Payload.Length, LocalAugmentedKey);
            }

            G2ReceivedPacket opPacket = new G2ReceivedPacket();

            opPacket.Root = new G2Header(tunnel.Payload);

            // set source information
            if (G2Protocol.ReadPacket(opPacket.Root))
            {
                opPacket.Source = new DhtAddress();

                // used to add direct op contact if source firewall is open
                // or re-routing through same lookup proxy
                opPacket.Source.IP      = raw.Source.IP;
                opPacket.Source.UdpPort = raw.Source.UdpPort;

                // op user/client set by net/comm processing

                opPacket.Source.TunnelClient = tunnel.Source;
                opPacket.Source.TunnelServer = tunnel.SourceServer;

                PacketLogEntry logEntry = new PacketLogEntry(Core.TimeNow, TransportProtocol.Tunnel, DirectionType.In, opPacket.Source, opPacket.Root.Data);
                LogPacket(logEntry);

                IncomingPacket(opPacket);
            }
        }
예제 #8
0
        void Comm_ReceiveData(DhtClient client, byte[] data)
        {
            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                switch (root.Name)
                {
                case VoicePacket.Audio:
                    ReceiveAudio(AudioPacket.Decode(root), client);
                    break;
                }
            }
        }
예제 #9
0
        public static SignedData Decode(byte[] data)
        {
            G2Header root = new G2Header(data);

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

            if (root.Name != DataPacket.SignedData)
            {
                return(null);
            }

            return(SignedData.Decode(root));
        }
예제 #10
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));
        }
예제 #11
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));
        }
예제 #12
0
        public void UnreliableReceive(byte[] data)
        {
            byte[] decrypted = Utilities.DecryptBytes(data, data.Length, InboundEnc.Key);

            G2ReceivedPacket packet = new G2ReceivedPacket();

            packet.Root = new G2Header(decrypted);

            if (G2Protocol.ReadPacket(packet.Root))
            {
                PacketLogEntry logEntry = new PacketLogEntry(Core.TimeNow, TransportProtocol.Rudp, DirectionType.In, Comm.PrimaryAddress.Address, decrypted);
                Core.Network.LogPacket(logEntry);

                ReceivePacket(packet);
            }
        }
예제 #13
0
파일: PlanPackets.cs 프로젝트: nandub/DeOps
        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));
        }
예제 #14
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));
        }
예제 #15
0
        void LightComm_ReceiveData(DhtClient client, byte[] data)
        {
            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                if (root.Name == LocationPacket.Ping)
                {
                    Receive_Ping(client, LocationPing.Decode(root));
                }

                if (root.Name == LocationPacket.Notify)
                {
                    Receive_Notify(client, LocationNotify.Decode(root));
                }
            }
        }
예제 #16
0
        void Session_Data(RudpSession session, byte[] data)
        {
            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                switch (root.Name)
                {
                case SharePacket.PublicRequest:
                    ReceivePublicRequest(session);
                    break;

                case SharePacket.Collection:
                    ReceivePublicDetails(session, ShareCollection.Decode(root, session.UserID));
                    break;
                }
            }
        }
예제 #17
0
파일: LanHandler.cs 프로젝트: nandub/DeOps
        void ParsePacket(byte[] buff, int length, IPEndPoint sender, bool copied)
        {
            G2ReceivedPacket packet = new G2ReceivedPacket();

            packet.Root = new G2Header(buff);

            if (G2Protocol.ReadPacket(packet.Root))
            {
                packet.Source = new DhtContact(0, 0, sender.Address, 0, 0);

                byte[] packetData = copied ? buff : Utilities.ExtractBytes(packet.Root.Data, packet.Root.PacketPos, packet.Root.PacketSize);

                PacketLogEntry logEntry = new PacketLogEntry(Core.TimeNow, TransportProtocol.LAN, DirectionType.In, packet.Source, packetData);
                Network.LogPacket(logEntry);


                Network.IncomingPacket(packet);
            }
        }
예제 #18
0
파일: TcpConnect.cs 프로젝트: nandub/DeOps
        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");
            }
        }
예제 #19
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));
                }
            }
        }
예제 #20
0
        void Search_Found(DhtSearch search, DhtAddress source, byte[] data)
        {
            G2Header root = new G2Header(data);

            if (!G2Protocol.ReadPacket(root))
            {
                return;
            }

            UpdateInfo info = UpdateInfo.Decode(root); // verifies signature

            if (info == null)
            {
                return;
            }

            if (Core.Context.SignedUpdate == null || Core.Context.SignedUpdate.SequentialVersion < info.SequentialVersion)
            {
                Core.Context.SignedUpdate = info;
                LookupConfig.WriteUpdateInfo(Core);
            }

            // version less than what we have
            else if (Core.Context.SignedUpdate.SequentialVersion > info.SequentialVersion)
            {
                return;
            }

            // version remote has already loaded
            if (Core.Context.SignedUpdate.Loaded)
            {
                return;
            }

            // same sources will be hit as file download search progresses
            StartDownload(search.TargetID);
        }
예제 #21
0
파일: DhtNetwork.cs 프로젝트: nandub/DeOps
        public void ReceivePacket(G2ReceivedPacket packet)
        {
            // Network packet
            if (packet.Root.Name == RootPacket.Network)
            {
                NetworkPacket netPacket = NetworkPacket.Decode(packet.Root);

                G2ReceivedPacket embedded = new G2ReceivedPacket();
                embedded.Tcp             = packet.Tcp;
                embedded.Source          = packet.Source;
                embedded.Source.UserID   = netPacket.SourceID;
                embedded.Source.ClientID = netPacket.ClientID;
                embedded.Root            = new G2Header(netPacket.InternalData);

                // from - received from proxy server
                if (netPacket.FromAddress != null)
                {
                    if (packet.ReceivedUdp)
                    {
                        throw new Exception("From tag set on packet received udp");
                    }
                    if (packet.Tcp.Proxy != ProxyType.Server)
                    {
                        throw new Exception("From tag (" + netPacket.FromAddress.ToString() + ") set on packet not received from server (" + packet.Tcp.ToString() + ")");
                    }

                    embedded.Source = new DhtContact(netPacket.FromAddress);
                }

                // to - received from proxied node, and not for us
                if (netPacket.ToAddress != null &&
                    !(netPacket.ToAddress.UserID == Local.UserID && netPacket.ToAddress.ClientID == Local.ClientID))
                {
                    if (packet.ReceivedUdp)
                    {
                        throw new Exception("To tag set on packet received udp");
                    }
                    if (packet.Tcp.Proxy == ProxyType.Server || packet.Tcp.Proxy == ProxyType.Unset)
                    {
                        throw new Exception("To tag set on packet received from server");
                    }

                    DhtAddress address = netPacket.ToAddress;
                    netPacket.ToAddress = null;

                    TcpConnect direct = TcpControl.GetProxy(address);

                    if (direct != null)
                    {
                        direct.SendPacket(netPacket);
                    }
                    else
                    {
                        UdpControl.SendTo(address, netPacket);
                    }

                    return;
                }

                // process
                if (G2Protocol.ReadPacket(embedded.Root))
                {
                    ReceiveNetworkPacket(embedded);
                }
            }

            // Tunnel Packet
            else if (packet.Root.Name == RootPacket.Tunnel)
            {
                // can only tunnel over lookup network
                if (!IsLookup)
                {
                    return;
                }

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

                TunnelPacket tunnel = TunnelPacket.Decode(packet.Root);

                // handle locally
                if (tunnel.Target.Equals(Local))
                {
                    Core.Context.Cores.LockReading(delegate()
                    {
                        foreach (OpCore core in Core.Context.Cores)
                        {
                            if (core.TunnelID == tunnel.Target.TunnelID)
                            {
                                core.Network.ReceiveTunnelPacket(packet, tunnel);
                            }
                        }
                    });
                }
                else if (tunnel.TargetServer != null)
                {
                    TcpConnect direct = TcpControl.GetProxy(tunnel.Target);

                    // if directly connected add from and forwared
                    if (direct != null)
                    {
                        direct.SendPacket(tunnel);
                    }

                    // only forward udp if received over tcp from a proxied host
                    else if (tunnel.TargetServer != null && packet.ReceivedTcp && packet.Tcp.Proxy != ProxyType.Server)
                    {
                        UdpControl.SendTo(tunnel.TargetServer, tunnel);
                    }
                }
            }

            // Communication Packet
            else if (packet.Root.Name == RootPacket.Comm)
            {
                RudpPacket commPacket = RudpPacket.Decode(packet);

                // received direct
                packet.Source.UserID   = commPacket.SenderID;
                packet.Source.ClientID = commPacket.SenderClient;

                // remote node is proxied
                if (commPacket.RemoteProxy != null)
                {
                    packet.Source = new DhtContact(commPacket.RemoteProxy);
                }

                // For local host
                if (commPacket.TargetID == Local.UserID && commPacket.TargetClient == Local.ClientID)
                {
                    ReceiveCommPacket(packet, commPacket);
                    return;
                }

                // Also Forward to appropriate node
                TcpConnect socket = TcpControl.GetProxy(commPacket.TargetID, commPacket.TargetClient);

                if (socket != null)
                {
                    // forward to proxied node - strip TO flag, add from address
                    commPacket.ToAddress   = null;
                    commPacket.RemoteProxy = packet.Source; // if remote proxy is null, then we are setting this to the packet's original source

                    socket.SendPacket(commPacket);
                    return;
                }

                // received from a proxied node, forward udp
                if (packet.ReceivedTcp && commPacket.ToAddress != null)
                {
                    DhtAddress target = commPacket.ToAddress;

                    commPacket.ToAddress   = null; // strip TO flag
                    commPacket.RemoteProxy = new DhtAddress(Core.LocalIP, GetLocalSource());

                    UdpControl.SendTo(target, commPacket);
                }
            }
        }
예제 #22
0
파일: PacketsForm.cs 프로젝트: nandub/DeOps
        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();
        }
예제 #23
0
파일: PacketsForm.cs 프로젝트: nandub/DeOps
        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));
        }
예제 #24
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);
        }
예제 #25
0
        public static UpdateInfo Decode(G2Header root)
        {
            // embedded public key of source we allow updates from
            RSACryptoServiceProvider UpdateSourcePublicKey = new RSACryptoServiceProvider();
            //UpdateSourcePublicKey.FromXmlString("<RSAKeyValue><Modulus>pTmHLSxyM9TDOM4tZzI5dld9JvPsHlHC/M5i0+Qtjid1DiefGAVubPToEhK9Im4Ohy37h5Ax6J3vt2pxLG4rnIDuKBJt70YH6W6XrJewQ6tid5BvVnNEzPUOIJHGMpOnyi0VjPpzZzWgp4JK6Yuh6LtsYwCyqIIJIBt9iQ/9XN0=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");


            UpdateInfo info = new UpdateInfo();

            if (G2Protocol.ReadPayload(root))
            {
                info.Embedded = 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))
                {
                    if (child.Name == Packet_Signature)
                    {
                        info.Signature = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    }
                }
            }


            // verify signature
            byte[] pubKey = UpdateSourcePublicKey.ExportParameters(false).Modulus;
            if (!Utilities.CheckSignedData(pubKey, info.Embedded, info.Signature))
            {
                return(null);
            }


            root = new G2Header(info.Embedded);
            if (!G2Protocol.ReadPacket(root))
            {
                return(null);
            }

            child = new G2Header(info.Embedded);

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

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

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

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

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

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

                case Packet_Beginning:
                    info.Beginning = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

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

                case Packet_SequentialVersion:
                    info.SequentialVersion = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                    break;
                }
            }

            info.TempName = Utilities.ToBase64String(info.Hash);

            return(info);
        }
예제 #26
0
        private void DrawGroup(Graphics buffer, Dictionary <ulong, Dictionary <ulong, PacketGroup> > TrafficGroup, bool tcp)
        {
            foreach (Dictionary <ulong, PacketGroup> destination in TrafficGroup.Values)
            {
                foreach (PacketGroup group in destination.Values)
                {
                    if (!NodePoints.ContainsKey(group.SourceID) || !NodePoints.ContainsKey(group.DestID))
                    {
                        continue;
                    }

                    group.SetPoints(NodePoints[group.SourceID], NodePoints[group.DestID]);

                    TrafficPen.Width = 1;
                    group.LineSize   = 200 + 20;

                    if (group.TotalSize > 200)
                    {
                        TrafficPen.Width = 2;
                        group.LineSize   = 1000 + 100;
                    }

                    if (group.TotalSize > 1000)
                    {
                        TrafficPen.Width = 3;
                        group.LineSize   = group.TotalSize + 500;
                    }

                    // calc break size
                    double breakSize = (group.LineSize - group.TotalSize) / (group.Packets.Count + 1);
                    double pos       = breakSize;

                    Color bgColor = Color.WhiteSmoke;

                    //if (SelectedID != 0)
                    //    bgColor = group.SourceID == SelectedID ? Color.LightCoral : Color.LightBlue;
                    //else
                    //    bgColor = tcp ? Color.LightBlue : Color.WhiteSmoke;

                    TrafficPen.Color = bgColor;
                    buffer.DrawLine(TrafficPen, group.GetPoint(0), group.GetPoint(pos));

                    foreach (byte[] packet in group.Packets)
                    {
                        if (Sim.TestEncryption || Sim.TestTcpFullBuffer)
                        {
                            TrafficPen.Color = Legend.PicUnk.BackColor;
                            buffer.DrawLine(TrafficPen, group.GetPoint(pos), group.GetPoint(pos + packet.Length));
                        }

                        else
                        {
                            G2Header root = new G2Header(packet);
                            G2Protocol.ReadPacket(root);

                            double controlLen = (root.InternalPos > 0) ? root.InternalPos - root.PacketPos : packet.Length;

                            // net packet
                            if (root.Name == RootPacket.Network)
                            {
                                TrafficPen.Color = Legend.PicNet.BackColor;
                                buffer.DrawLine(TrafficPen, group.GetPoint(pos), group.GetPoint(pos + controlLen));

                                NetworkPacket netPacket    = NetworkPacket.Decode(root);
                                G2Header      internalRoot = new G2Header(netPacket.InternalData);
                                G2Protocol.ReadPacket(internalRoot);

                                G2ReceivedPacket recvedPacket = new G2ReceivedPacket();
                                recvedPacket.Root = internalRoot;

                                // draw internal
                                TrafficPen.Color = Legend.PicUnk.BackColor;

                                if (internalRoot.Name == NetworkPacket.SearchRequest)
                                {
                                    SearchReq req = SearchReq.Decode(recvedPacket);

                                    int paramLen = req.Parameters == null ? 10 : req.Parameters.Length;

                                    TrafficPen.Color = Legend.PicSrchReq.BackColor;
                                    buffer.DrawLine(TrafficPen, group.GetPoint(pos + controlLen), group.GetPoint(pos + controlLen + internalRoot.PacketSize - paramLen));

                                    TrafficPen.Color = GetComponentColor(req.Service);
                                    buffer.DrawLine(TrafficPen, group.GetPoint(pos + controlLen + internalRoot.PacketSize - paramLen), group.GetPoint(pos + controlLen + internalRoot.PacketSize));
                                }

                                else if (internalRoot.Name == NetworkPacket.SearchAck)
                                {
                                    SearchAck ack = SearchAck.Decode(recvedPacket);

                                    int valLen = 10;

                                    if (ack.ValueList.Count > 0)
                                    {
                                        valLen = 0;
                                        foreach (byte[] val in ack.ValueList)
                                        {
                                            valLen += val.Length;
                                        }
                                    }

                                    TrafficPen.Color = Legend.PicSrchAck.BackColor;
                                    buffer.DrawLine(TrafficPen, group.GetPoint(pos + controlLen), group.GetPoint(pos + controlLen + internalRoot.PacketSize - valLen));

                                    TrafficPen.Color = GetComponentColor(ack.Service);
                                    buffer.DrawLine(TrafficPen, group.GetPoint(pos + controlLen + internalRoot.PacketSize - valLen), group.GetPoint(pos + controlLen + internalRoot.PacketSize));
                                }

                                else if (internalRoot.Name == NetworkPacket.StoreRequest)
                                {
                                    StoreReq req = StoreReq.Decode(recvedPacket);

                                    int dataLen = req.Data == null ? 10 : req.Data.Length;

                                    TrafficPen.Color = Legend.PicStore.BackColor;
                                    buffer.DrawLine(TrafficPen, group.GetPoint(pos + controlLen), group.GetPoint(pos + controlLen + internalRoot.PacketSize - dataLen));

                                    TrafficPen.Color = GetComponentColor(req.Service);
                                    buffer.DrawLine(TrafficPen, group.GetPoint(pos + controlLen + internalRoot.PacketSize - dataLen), group.GetPoint(pos + controlLen + internalRoot.PacketSize));
                                }

                                else
                                {
                                    if (internalRoot.Name == NetworkPacket.Ping)
                                    {
                                        TrafficPen.Color = Legend.PicPing.BackColor;
                                    }

                                    else if (internalRoot.Name == NetworkPacket.Pong)
                                    {
                                        TrafficPen.Color = Legend.PicPong.BackColor;
                                    }

                                    else if (internalRoot.Name == NetworkPacket.ProxyRequest)
                                    {
                                        TrafficPen.Color = Legend.PicPxyReq.BackColor;
                                    }

                                    else if (internalRoot.Name == NetworkPacket.ProxyAck)
                                    {
                                        TrafficPen.Color = Legend.PicPxyAck.BackColor;
                                    }

                                    buffer.DrawLine(TrafficPen, group.GetPoint(pos + controlLen), group.GetPoint(pos + packet.Length));
                                }
                            }

                            // comm packet
                            if (root.Name == RootPacket.Comm)
                            {
                                TrafficPen.Color = Legend.PicComm.BackColor;
                                buffer.DrawLine(TrafficPen, group.GetPoint(pos), group.GetPoint(pos + controlLen));

                                TrafficPen.Color = Legend.PicUnk.BackColor;
                                buffer.DrawLine(TrafficPen, group.GetPoint(pos + controlLen), group.GetPoint(pos + packet.Length));
                            }
                        }

                        if (SelectedID != 0)
                        {
                            buffer.DrawString(group.TotalSize.ToString(), TahomaFont, BlackBrush, group.GetPoint(group.LineSize / 4));
                        }

                        pos += packet.Length;

                        TrafficPen.Color = bgColor;
                        buffer.DrawLine(TrafficPen, group.GetPoint(pos), group.GetPoint(pos + breakSize));
                        pos += breakSize;
                    }
                }
            }
        }
예제 #27
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);
            }
        }