Ejemplo n.º 1
0
        public void Msg_Clone_RouteAdvertiseMsg()
        {
            RouterAdvertiseMsg msgIn, msgOut;

            Msg.ClearTypes();
            Msg.LoadTypes(Assembly.GetExecutingAssembly());

            msgOut         = new RouterAdvertiseMsg(MsgEP.Parse("physical://root.com:80/hub/leaf"), "appname", "appdescription", new MsgRouterInfo(new Version(1000, 0)), 10, 20, Helper.NewGuid(), false, true);
            msgOut._FromEP = MsgEP.Parse("physical://root.com:80/hub/leaf?c=mcast://1.2.3.4:57");
            msgIn          = (RouterAdvertiseMsg)msgOut.Clone();

            Assert.AreEqual("physical://root.com:80/hub/leaf", msgIn.RouterEP.ToString());
            Assert.AreEqual("appname", msgIn.AppName);
            Assert.AreEqual("appdescription", msgIn.AppDescription);
            Assert.AreEqual(new Version(1000, 0), msgIn.RouterInfo.ProtocolVersion);
            Assert.AreEqual(IPAddress.Parse("1.2.3.4"), msgIn.IPAddress);
            Assert.AreEqual(10, msgIn.UdpPort);
            Assert.AreEqual(20, msgIn.TcpPort);
            Assert.AreEqual(msgOut.LogicalEndpointSetID, msgIn.LogicalEndpointSetID);
            Assert.IsFalse(msgIn.ReplyAdvertise);
            Assert.IsTrue(msgIn.DiscoverLogical);
            Assert.IsTrue(msgIn.RouterInfo.IsP2P);

            TestBaseCloning(msgOut);
        }
Ejemplo n.º 2
0
        public void OnMsg(RouterAdvertiseMsg msg)
        {
            PhysicalRoute physRoute;
            bool          discoverLogical;

            if (this.RouterEP == null)
            {
                return;     // Router is not fully initialized
            }
            if (this.RouterEP.IsPhysicalMatch(msg.RouterEP))
            {
                // I've noticed that on multi-homed machines, we can see source IP addresses for
                // multicast messages from ourself that differ from what we think our IP address
                // is.  I'm going to check the source IP address against all of the local IP
                // addresses to avoid issuing invalid warnings.

                var isLocalIPAddress = NetHelper.IsLocalAddress(msg.IPAddress);

                if (msg.TcpPort != this.TcpEP.Port || !isLocalIPAddress)
                {
                    this.dupLeafDetected = true;
                    SysLog.LogWarning("Duplicate router [{0}] appears to be advertising on TCP[{1}:{2}].  Local endpoint is TCP[{3}:{4}].",
                                      msg.RouterEP.ToString(-1, false),
                                      msg.IPAddress, msg.TcpPort,
                                      this.TcpEP.Address, this.TcpEP.Port);
                }

                if (msg.UdpPort != this.UdpEP.Port || !isLocalIPAddress)
                {
                    this.dupLeafDetected = true;
                    SysLog.LogWarning("Duplicate router [{0}] appears to be advertising on UDP[{1}:{2}].  Local endpoint is UDP[{3}:{4}].",
                                      msg.RouterEP.ToString(-1, false),
                                      msg.IPAddress, msg.UdpPort,
                                      this.UdpEP.Address, this.UdpEP.Port);
                }

                return;     // Don't add routes to self to the routing table
            }

            // If the source router is this router's hub then send it a RouterAdvertiseMsg
            // so the hub can continue the route discovery process.

            if (msg.ReplyAdvertise && this.RouterEP.GetPhysicalParent().Equals(msg.RouterEP))
            {
                // Set up a temporary route to the hub so the RouterAdvertiseMsg can be delivered.
                // These values will be finalized when the LeafSettingsMsg is received.

                this.hubEP        = msg.RouterEP;
                this.hubChannelEP = new ChannelEP(Transport.Tcp, new IPEndPoint(msg.IPAddress, msg.TcpPort));

                // Send the RouterAdvertiseMsg to the hub.

                SendTo(msg.RouterEP, new RouterAdvertiseMsg(this.RouterEP, this.AppName, this.AppDescription, this.RouterInfo,
                                                            this.UdpEP.Port, this.TcpEP.Port, this.Dispatcher.LogicalEndpointSetID, false, false));
                return;
            }

            // Ignore the message if either this router or the advertised router is not
            // peer-to-peer enabled.  In these cases, messages will be forwarded to
            // the hub for delivery.

            if (!base.EnableP2P || !msg.RouterInfo.IsP2P)
            {
                return;
            }

            // Add/update the physical route to the advertised router.

            using (TimedLock.Lock(this.SyncRoot))
            {
                if (!isRunning)
                {
                    return;
                }

                // Handle route table management for peer routers.

                if (!this.RouterEP.IsPhysicalPeer(msg.RouterEP))
                {
                    const string format =
                        @"RouterEP: {0}
Route:    {1}";
                    NetTrace.Write(MsgRouter.TraceSubsystem, 1, "Ignore route", this.GetType().Name + ": " + msg.RouterEP.ToString(), string.Format(null, format, this.RouterEP.ToString(), msg.RouterEP.ToString()));
                    return;
                }

                // If this is the first time we've seen this router or if the router's set
                // of handled logical endpoints has changed then we'll need to set the
                // DiscoverLogical=true on the RouterAdvertiseMsg so the other will send
                // us its logical endpoints.

                physRoute       = base.PhysicalRoutes[msg.RouterEP];
                discoverLogical = physRoute == null || physRoute.LogicalEndpointSetID != msg.LogicalEndpointSetID;

                // Send a RouterAdvertiseMsg for this router back to the source router if that
                // router is P2P enabled and a reply is requested.  This will give that router
                // a chance to learn about this router.

                if (msg.RouterInfo.IsP2P && (msg.ReplyAdvertise || discoverLogical))
                {
                    SendTo(msg.RouterEP, new RouterAdvertiseMsg(this.RouterEP, this.AppName, this.AppDescription, this.RouterInfo,
                                                                this.UdpEP.Port, this.TcpEP.Port, this.Dispatcher.LogicalEndpointSetID, false, discoverLogical));
                }

                AddPhysicalRoute(msg.RouterEP, msg.AppName, msg.AppDescription, msg.RouterInfo, msg.LogicalEndpointSetID,
                                 new IPEndPoint(msg.IPAddress, msg.UdpPort), new IPEndPoint(msg.IPAddress, msg.TcpPort));

                // Send LogicalAdvertiseMsgs back to the sender if requested.

                if (msg.DiscoverLogical)
                {
                    SendLogicalAdvertiseMsgs(msg.RouterEP);
                }
            }
        }