Esempio n. 1
0
        void Search_DonePublish(DhtSearch search)
        {
            DataReq publish = (DataReq)search.Carry;

            // need to carry over componentid that wanted search also so store works

            foreach (DhtLookup node in search.LookupList)
            {
                Send_StoreReq(node.Contact, null, publish);
            }
        }
Esempio n. 2
0
        public void Receive_StoreReq(G2ReceivedPacket packet)
        {
            StoreReq store = StoreReq.Decode(packet);

            if (Core.ServiceBandwidth.ContainsKey(store.Service))
            {
                Core.ServiceBandwidth[store.Service].InPerSec += packet.Root.Data.Length;
            }

            if (store.Source.Firewall == FirewallType.Open)
            {
                // dont need to add to routing if nat/blocked because eventual routing ping by server will auto add
                Network.Routing.Add(new DhtContact(store.Source, packet.Source.IP));
            }


            // forward to proxied nodes - only replicate data to blocked nodes on operation network
            if (!Network.IsLookup)
            {
                // when we go offline it will be these nodes that update their next proxy with stored info
                foreach (TcpConnect socket in Network.TcpControl.ProxyClients)
                {
                    if (packet.Tcp != socket)
                    {
                        if (packet.ReceivedUdp)
                        {
                            store.FromAddress = packet.Source;
                        }

                        socket.SendPacket(store);
                    }
                }
            }

            // pass to components
            DataReq data = new DataReq(packet.Source, store.Key, store.Service, store.DataType, store.Data);

            if (packet.ReceivedTcp && packet.Tcp.Proxy == ProxyType.Server)
            {
                data.LocalProxy = new DhtClient(packet.Tcp);
            }

            if (data.Service == 0)
            {
                Receive_Patch(packet.Source, store.Data);
            }

            else if (StoreEvent.Contains(store.Service, store.DataType))
            {
                StoreEvent[store.Service, store.DataType].Invoke(data);
            }
        }
Esempio n. 3
0
        public void Send_StoreReq(DhtAddress address, DhtClient localProxy, DataReq publish)
        {
            if (address == null)
            {
                return;
            }

            StoreReq store = new StoreReq();

            store.Source   = Network.GetLocalSource();
            store.Key      = publish.Target;
            store.Service  = publish.Service;
            store.DataType = publish.DataType;
            store.Data     = publish.Data;

            int sentBytes = 0;

            TcpConnect direct = Network.TcpControl.GetProxy(address);

            if (direct != null)
            {
                sentBytes = direct.SendPacket(store);
            }

            else if (address.TunnelClient != null)
            {
                sentBytes = Network.SendTunnelPacket(address, store);
            }

            // if blocked send tcp with to tag
            else if (Core.Firewall == FirewallType.Blocked)
            {
                store.ToAddress = address;

                TcpConnect proxy = Network.TcpControl.GetProxy(localProxy);

                if (proxy != null)
                {
                    sentBytes = proxy.SendPacket(store);
                }
                else
                {
                    sentBytes = Network.TcpControl.SendRandomProxy(store);
                }
            }
            else
            {
                sentBytes = Network.UdpControl.SendTo(address, store);
            }

            Core.ServiceBandwidth[store.Service].OutPerSec += sentBytes;
        }
Esempio n. 4
0
        public void PublishDirect(List <LocationData> locations, ulong target, uint service, uint datatype, byte[] data)
        {
            if (Core.InvokeRequired)
            {
                Core.RunInCoreAsync(delegate() { PublishDirect(locations, target, service, datatype, data); });
                return;
            }

            DataReq req = new DataReq(null, target, service, datatype, data);

            foreach (LocationData location in locations)
            {
                DhtAddress address = new DhtAddress(location.IP, location.Source);
                Send_StoreReq(address, null, req);

                foreach (DhtAddress proxy in location.Proxies)
                {
                    Send_StoreReq(proxy, null, req);
                }
            }
        }
Esempio n. 5
0
        public void PublishNetwork(ulong target, uint service, uint datatype, byte[] data)
        {
            if (Core.InvokeRequired)
            {
                Debug.Assert(false);
            }

            string type = "Publish " + Core.GetServiceName(service);

            if (target == Core.UserID)
            {
                Network.UpdateLog("general", "Publishing " + Core.GetServiceName(service));
            }

            DataReq store = new DataReq(null, target, service, datatype, data);

            // find users closest to publish target
            if (target == Network.Local.UserID)
            {
                foreach (DhtContact closest in Network.Routing.GetCacheArea())
                {
                    Send_StoreReq(closest, null, store);
                }

                foreach (TcpConnect socket in Network.TcpControl.ProxyClients)
                {
                    Send_StoreReq(new DhtAddress(socket.RemoteIP, socket), null, store);
                }
            }
            else
            {
                DhtSearch search = Network.Searches.Start(target, type, 0, 0, null, null);

                if (search != null)
                {
                    search.DoneEvent = Search_DonePublish;
                    search.Carry     = store;
                }
            }
        }
Esempio n. 6
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));
        }
Esempio n. 7
0
        private void Process_VersionedFile(DataReq data, SignedData signed, VersionedFileHeader header)
        {
            Core.IndexKey(header.KeyID, ref header.Key);

            OpVersionedFile current = GetFile(header.KeyID);

            // if link loaded
            if (current != null)
            {
                // lower version
                if (header.Version < current.Header.Version)
                {
                    if (data != null && data.Source != null)
                        Store.Send_StoreReq(data.Source, data.LocalProxy, new DataReq(null, current.UserID, Service, DataType, current.SignedHeader));

                    return;
                }

                // higher version
                else if (header.Version > current.Header.Version)
                {
                    CacheFile(signed, header);
                }
            }

            // else load file, set new header after file loaded
            else
                CacheFile(signed, header);
        }
Esempio n. 8
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 == TrustPacket.UplinkReq)
                    Process_UplinkReq(store, signed, UplinkRequest.Decode(embedded));
        }
Esempio n. 9
0
        private void Process_UplinkReq(DataReq data, SignedData signed, UplinkRequest request)
        {
            Core.IndexKey(request.KeyID, ref request.Key);
            Core.IndexKey(request.TargetID, ref request.Target);

            if (!Utilities.CheckSignedData(request.Key, signed.Data, signed.Signature))
                return;

            OpTrust requesterTrust = GetTrust(request.KeyID);

            if (requesterTrust != null && requesterTrust.Loaded && requesterTrust.File.Header.Version > request.LinkVersion)
                return;

            // check if target in linkmap, if not add
            OpTrust targetTrust = GetTrust(request.TargetID);

            if (targetTrust == null)
            {
                targetTrust = new OpTrust(new OpVersionedFile(request.Target) );
                TrustMap.SafeAdd(request.TargetID, targetTrust);
            }

            if (targetTrust.Loaded && targetTrust.File.Header.Version > request.TargetVersion)
                return;

            request.Signed = signed.Encode(Network.Protocol); // so we can send it in results / save, later on

            // check for duplicate requests
            OpLink targetLink = targetTrust.GetLink(request.ProjectID);

            if (targetLink != null)
            {
                foreach (UplinkRequest compare in targetLink.Requests)
                    if (Utilities.MemCompare(compare.Signed, request.Signed))
                        return;
            }
            else
            {
                targetTrust.AddProject(request.ProjectID, true);
                targetLink = targetTrust.GetLink(request.ProjectID);
            }

            // add
            targetLink.Requests.Add(request);

            // if target is marked as linked or focused, update link of target and sender
            if (targetTrust.Loaded && (targetTrust.InLocalLinkTree || Core.KeepData.SafeContainsKey(targetTrust.UserID)))
            {
                if (targetTrust.File.Header.Version < request.TargetVersion)
                    Cache.Research(targetTrust.UserID);

                if (requesterTrust == null)
                {
                    requesterTrust = new OpTrust(new OpVersionedFile(request.Key));
                    TrustMap.SafeAdd(request.KeyID, requesterTrust);
                }

                // once new version of requester's link file has been downloaded, interface will be updated
                if (!requesterTrust.Loaded || (requesterTrust.File.Header.Version < request.LinkVersion))
                    Cache.Research(requesterTrust.UserID);
            }

            RunSaveUplinks = true;
        }
Esempio n. 10
0
        public void PublishNetwork(ulong target, uint service, uint datatype, byte[] data)
        {
            if (Core.InvokeRequired)
                Debug.Assert(false);

            string type = "Publish " + Core.GetServiceName(service);

            if(target == Core.UserID)
                Network.UpdateLog("general", "Publishing " + Core.GetServiceName(service));

            DataReq store = new DataReq(null, target, service, datatype, data);

            // find users closest to publish target
            if (target == Network.Local.UserID)
            {
                foreach (DhtContact closest in Network.Routing.GetCacheArea())
                    Send_StoreReq(closest, null, store);

                foreach (TcpConnect socket in Network.TcpControl.ProxyClients)
                    Send_StoreReq(new DhtAddress(socket.RemoteIP, socket), null, store);
            }
            else
            {
                DhtSearch search = Network.Searches.Start(target, type, 0, 0, null, null);

                if (search != null)
                {
                    search.DoneEvent = Search_DonePublish;
                    search.Carry = store;
                }
            }
        }
Esempio n. 11
0
        public void PublishDirect(List<LocationData> locations, ulong target, uint service, uint datatype, byte[] data)
        {
            if (Core.InvokeRequired)
            {
                Core.RunInCoreAsync(delegate() { PublishDirect(locations, target, service, datatype, data); });
                return;
            }

            DataReq req = new DataReq(null, target, service, datatype, data);

            foreach (LocationData location in locations)
            {
                DhtAddress address = new DhtAddress(location.IP, location.Source);
                Send_StoreReq(address, null, req);

                foreach (DhtAddress proxy in location.Proxies)
                    Send_StoreReq(proxy, null, req);
            }
        }
Esempio n. 12
0
        public void Send_StoreReq(DhtAddress address, DhtClient localProxy, DataReq publish)
        {
            if (address == null)
                return;

            StoreReq store = new StoreReq();
            store.Source    = Network.GetLocalSource();
            store.Key       = publish.Target;
            store.Service   = publish.Service;
            store.DataType  = publish.DataType;
            store.Data      = publish.Data;

            int sentBytes = 0;

            TcpConnect direct = Network.TcpControl.GetProxy(address);

            if (direct != null)
                sentBytes = direct.SendPacket(store);

            else if (address.TunnelClient != null)
                sentBytes = Network.SendTunnelPacket(address, store);

            // if blocked send tcp with to tag
            else if (Core.Firewall == FirewallType.Blocked)
            {
                store.ToAddress = address;

                TcpConnect proxy = Network.TcpControl.GetProxy(localProxy);

                if (proxy != null)
                    sentBytes = proxy.SendPacket(store);
                else
                    sentBytes = Network.TcpControl.SendRandomProxy(store);
            }
            else
                sentBytes = Network.UdpControl.SendTo(address, store);

            Core.ServiceBandwidth[store.Service].OutPerSec += sentBytes;
        }
Esempio n. 13
0
        public void Receive_StoreReq(G2ReceivedPacket packet)
        {
            StoreReq store = StoreReq.Decode(packet);

            if (Core.ServiceBandwidth.ContainsKey(store.Service))
                Core.ServiceBandwidth[store.Service].InPerSec += packet.Root.Data.Length;

            if (store.Source.Firewall == FirewallType.Open )
                    // dont need to add to routing if nat/blocked because eventual routing ping by server will auto add
                    Network.Routing.Add(new DhtContact(store.Source, packet.Source.IP));

            // forward to proxied nodes - only replicate data to blocked nodes on operation network
            if (!Network.IsLookup)
                // when we go offline it will be these nodes that update their next proxy with stored info
                foreach (TcpConnect socket in Network.TcpControl.ProxyClients)
                    if (packet.Tcp != socket)
                    {
                        if (packet.ReceivedUdp)
                            store.FromAddress = packet.Source;

                        socket.SendPacket(store);
                    }

            // pass to components
            DataReq data = new DataReq(packet.Source, store.Key, store.Service, store.DataType, store.Data);

            if (packet.ReceivedTcp && packet.Tcp.Proxy == ProxyType.Server)
                data.LocalProxy = new DhtClient(packet.Tcp);

            if(data.Service == 0)
                Receive_Patch(packet.Source, store.Data);

            else if (StoreEvent.Contains(store.Service, store.DataType))
                StoreEvent[store.Service, store.DataType].Invoke(data);
        }
Esempio n. 14
0
        void Store_Local(DataReq store)
        {
            // location being published to hashid so others can get sources

            TempData temp = TempData.Decode(store.Target, store.Data);

            if (temp == null)
                return;

            TempData dupe = CachedData.Where(l => l.TargetID == temp.TargetID && Utilities.MemCompare(l.Data, temp.Data)).FirstOrDefault();

            if (dupe != null)
            {
                if(temp.TTL > dupe.TTL)
                    dupe.TTL = temp.TTL;

                return;
            }

            CachedData.Add(temp);
        }
Esempio n. 15
0
        private void Process_PostHeader(DataReq data, SignedData signed, PostHeader header)
        {
            Core.IndexKey(header.SourceID, ref header.Source);
            Core.IndexKey(header.TargetID, ref header.Target);

            PostUID uid = new PostUID(header);

            OpPost current = GetPost(header.TargetID, uid);

            // if link loaded
            if (current != null)
            {
                // lower version, send update
                if (header.Version < current.Header.Version)
                {
                    if (data != null && data.Source != null)
                        Store.Send_StoreReq(data.Source, data.LocalProxy, new DataReq(null, header.TargetID, ServiceID, 0, current.SignedHeader));

                    return;
                }

                // higher version
                else if (header.Version > current.Header.Version)
                {
                    CachePost(signed, header);
                }

                // equal version do nothing
            }

            // else load file, set new header after file loaded
            else
                CachePost(signed, header);
        }