Exemple #1
0
        public bool Connect(LocationData location)
        {
            if (location.UserID == Network.Local.UserID && location.Source.ClientID == Network.Local.ClientID)
                return false;

            ulong id = location.UserID ^ location.Source.ClientID;

            if (SessionMap.ContainsKey(id))
                return SessionMap[id].Status != SessionStatus.Closed;

            RudpSession session = new RudpSession(this, location.UserID, location.Source.ClientID, false);
            SessionMap[id] = session;

            session.Comm.AddAddress(new RudpAddress(new DhtAddress(location.IP, location.Source)));

            foreach (DhtAddress address in location.Proxies)
                session.Comm.AddAddress(new RudpAddress(address));

            foreach (DhtAddress server in location.TunnelServers)
                session.Comm.AddAddress(new RudpAddress(new DhtContact(location.Source, location.IP, location.TunnelClient, server)));

            session.Connect();

            return true;
        }
Exemple #2
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);
        }
Exemple #3
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;
        }
Exemple #4
0
        public void Location_Update(LocationData location)
        {
            if (!ActiveUsers.Contains(location.UserID))
                return;

            IMStatus status = null;
            if (!IMMap.SafeTryGetValue(location.UserID, out status))
                return;

            Network.RudpControl.Connect(location);

            Update(status);
        }
Exemple #5
0
        }                       // used only for InternalData debugging

        public ClientInfo(LocationData data) :
            base(data.UserID, data.Source.ClientID)
        {
        }
Exemple #6
0
        private void Process_LocationData(DataReq data, SignedData signed, LocationData location)
        {
            Core.IndexKey(location.UserID, ref location.Key);

            Debug.Assert(location.UserID == location.Source.UserID);
            if (location.UserID != location.Source.UserID)
            {
                return;
            }


            ClientInfo client = GetLocationInfo(location.UserID, location.Source.ClientID);

            // check location version
            if (client != null)
            {
                if (location.Version == client.Data.Version)
                {
                    return;
                }

                else if (location.Version < client.Data.Version)
                {
                    if (data != null && data.Source != null)
                    {
                        Network.Store.Send_StoreReq(data.Source, data.LocalProxy, new DataReq(null, client.Data.UserID, ServiceID, 0, client.SignedData));
                    }

                    return;
                }
            }

            Core.IndexName(location.UserID, location.Name);

            // notify components of new versions (usually just localsync service signed up for this)
            DhtAddress address = new DhtAddress(location.IP, location.Source);

            foreach (PatchTag tag in location.Tags)
            {
                if (TagReceived.Contains(tag.Service, tag.DataType))
                {
                    TagReceived[tag.Service, tag.DataType].Invoke(address, location.UserID, tag.Tag);
                }
            }


            // add location
            if (client == null)
            {
                // if too many clients, and not us, return
                if (location.UserID != Core.UserID && ActiveClientCount(location.UserID) > MaxClientsperUser)
                {
                    return;
                }

                client = new ClientInfo(location);

                Clients.SafeAdd(client.RoutingID, client);

                // dont need to worry about remote caching old locs indefinitely because if a loc is cached remotely
                // that means the remote is being continuall pinged, or else the loc would expire
                // if we're still interested in loc after a min, it will be pinged locally
            }

            client.Data       = location;
            client.SignedData = signed.Encode(Network.Protocol);

            if (client.Data.UserID == Core.UserID && client.Data.Source.ClientID == Network.Local.ClientID)
            {
                LocalClient = client;
            }


            AddRoutingData(location);

            // only get down here if loc was new version in first place (recently published)
            // with live comm trickle down this prevents highers from being direct ping flooded to find their
            // online status
            client.LastSeen = Core.TimeNow;

            SignalUpdate(client, true);
        }