public WhatsUpServiceBindingWrapper(NSMessageHandler nsHandler)
            : this()
        {
            this.nsHandler = nsHandler;

            SingleSignOnManager.RenewIfExpired(nsHandler, SSOTicketType.WhatsUp);
        }
Ejemplo n.º 2
0
        public ABServiceBindingWrapper(NSMessageHandler nsHandler)
            : this()
        {
            this.nsHandler = nsHandler;

            SingleSignOnManager.RenewIfExpired(nsHandler, SSOTicketType.Contact);
        }
        public DirectoryServiceWrapper(NSMessageHandler nsHandler)
            : this()
        {
            this.nsHandler = nsHandler;

            SingleSignOnManager.RenewIfExpired(nsHandler, SSOTicketType.Directory);
        }
        public DirectoryServiceWrapper(NSMessageHandler nsHandler)
            : this()
        {
            this.nsHandler = nsHandler;

            SingleSignOnManager.RenewIfExpired(nsHandler, SSOTicketType.Directory);
        }
        public SharingServiceBindingWrapper(NSMessageHandler nsHandler)
            : this()
        {
            this.nsHandler = nsHandler;

            SingleSignOnManager.RenewIfExpired(nsHandler, SSOTicketType.Contact);
        }
Ejemplo n.º 6
0
        private static TCPv1Bridge ListenForDirectConnection(
            Contact remote,
            Guid remoteGuid,
            NSMessageHandler nsMessageHandler,
            P2PVersion ver,
            P2PSession startupSession,
            IPAddress host, int port, Guid replyGuid, Guid remoteNonce, bool hashed)
        {
            ConnectivitySettings cs = new ConnectivitySettings();

            if (nsMessageHandler.ConnectivitySettings.LocalHost == string.Empty)
            {
                cs.LocalHost = host.ToString();
                cs.LocalPort = port;
            }
            else
            {
                cs.LocalHost = nsMessageHandler.ConnectivitySettings.LocalHost;
                cs.LocalPort = nsMessageHandler.ConnectivitySettings.LocalPort;
            }

            TCPv1Bridge tcpBridge = new TCPv1Bridge(cs, ver, replyGuid, remoteNonce, hashed, startupSession, nsMessageHandler, remote, remoteGuid);

            tcpBridge.Listen(IPAddress.Parse(cs.LocalHost), cs.LocalPort);

            Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, "Listening on " + cs.LocalHost + ":" + cs.LocalPort.ToString(CultureInfo.InvariantCulture));

            return(tcpBridge);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a new session initiated locally and prepares the invitation message.
        /// The next step must be Invite().
        /// </summary>
        public P2PSession(P2PApplication app)
        {
            p2pApplication = app;
            version        = app.P2PVersion;

            localContact            = app.Local;
            remoteContact           = app.Remote;
            localContactEndPointID  = NSMessageHandler.MachineGuid;
            remoteContactEndPointID = app.Remote.SelectBestEndPointId();

            nsMessageHandler = app.Local.NSMessageHandler;
            sessionId        = (uint)random.Next(50000, int.MaxValue);

            // These 2 fields are set when optimal bridge found.
            localBaseIdentifier = 0;
            localIdentifier     = 0;

            invitation                         = new SLPRequestMessage(RemoteContactEPIDString, MSNSLPRequestMethod.INVITE);
            invitation.Target                  = RemoteContactEPIDString;
            invitation.Source                  = LocalContactEPIDString;
            invitation.ContentType             = "application/x-msnmsgr-sessionreqbody";
            invitation.BodyValues["SessionID"] = SessionId.ToString(System.Globalization.CultureInfo.InvariantCulture);
            app.SetupInviteMessage(invitation);

            app.P2PSession = this; // Register events
            remoteContact.DirectBridgeEstablished += RemoteDirectBridgeEstablished;

            Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo,
                              String.Format("{0} created (Initiated locally)", SessionId), GetType().Name);

            status = P2PSessionStatus.WaitingForRemote;
            // Next step must be: Invite()
        }
Ejemplo n.º 8
0
        /// <summary>
        /// P2PBridge constructor.
        /// </summary>
        protected P2PBridge(int queueSize, NSMessageHandler nsHandler)
        {
            this.queueSize = queueSize;

            this.sequenceId       = (uint)new Random().Next(5000, int.MaxValue);
            this.nextCleanup      = NextCleanupTime();
            this.nsMessageHandler = nsHandler;

            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose,
                              String.Format("P2PBridge {0} created", this.ToString()), GetType().Name);
        }
Ejemplo n.º 9
0
        private static void ProcessDirectAddrUpdate(
            SLPMessage message,
            NSMessageHandler nsMessageHandler,
            P2PSession startupSession)
        {
            Contact from = nsMessageHandler.ContactList.GetContactWithCreate(message.FromEmailAccount, IMAddressInfoType.WindowsLive);

            IPEndPoint[] ipEndPoints = SelectIPEndPoint(message.BodyValues, nsMessageHandler);

            if (from.DirectBridge != null && ipEndPoints != null && ipEndPoints.Length > 0)
            {
                ((TCPv1Bridge)from.DirectBridge).OnDestinationAddressUpdated(new DestinationAddressUpdatedEventArgs(ipEndPoints));
            }
        }
Ejemplo n.º 10
0
        private static string ConnectionType(NSMessageHandler nsMessageHandler, out int netId)
        {
            string connectionType = "Unknown-Connect";

            netId = 0;
            IPEndPoint localEndPoint    = nsMessageHandler.LocalEndPoint;
            IPEndPoint externalEndPoint = nsMessageHandler.ExternalEndPoint;

            if (localEndPoint == null || externalEndPoint == null)
            {
            }
            else
            {
                netId = BitConverter.ToInt32(localEndPoint.Address.GetAddressBytes(), 0);

                if (Settings.DisableP2PDirectConnections)
                {
                    connectionType = "Firewall";
                }
                else
                {
                    if (localEndPoint.Address.Equals(externalEndPoint.Address))
                    {
                        if (localEndPoint.Port == externalEndPoint.Port)
                        {
                            connectionType = "Direct-Connect";
                        }
                        else
                        {
                            connectionType = "Port-Restrict-NAT";
                        }
                    }
                    else
                    {
                        if (localEndPoint.Port == externalEndPoint.Port)
                        {
                            netId          = 0;
                            connectionType = "IP-Restrict-NAT";
                        }
                        else
                        {
                            connectionType = "Symmetric-NAT";
                        }
                    }
                }
            }

            return(connectionType);
        }
Ejemplo n.º 11
0
        internal static void ProcessDirectInvite(SLPMessage slp, NSMessageHandler ns, P2PSession startupSession)
        {
            switch (slp.ContentType)
            {
            case "application/x-msnmsgr-transreqbody":
                ProcessDCReqInvite(slp, ns, startupSession);
                break;

            case "application/x-msnmsgr-transrespbody":
                ProcessDCRespInvite(slp, ns, startupSession);
                break;

            case "application/x-msnmsgr-transdestaddrupdate":
                ProcessDirectAddrUpdate(slp, ns, startupSession);
                break;
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public P2PDirectProcessor(
            ConnectivitySettings connectivitySettings,
            P2PVersion p2pVersion,
            Guid reply, Guid authNonce, bool isNeedHash,
            P2PSession p2pMessageSession,
            NSMessageHandler nsMessageHandler)
        {
            Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, "Constructing object - " + p2pVersion, GetType().Name);

            Processor = new TcpSocketMessageProcessor(connectivitySettings, new P2PDCPool());

            this.version          = p2pVersion;
            this.nonce            = authNonce;
            this.reply            = reply;
            this.needHash         = isNeedHash;
            this.startupSession   = p2pMessageSession;
            this.nsMessageHandler = nsMessageHandler;
        }
Ejemplo n.º 13
0
        protected P2PApplication(P2PVersion ver, Contact remote, Guid remoteEP)
        {
            if (ver == P2PVersion.None)
            {
                throw new InvalidOperationException(remote.ToString() + " doesn't support P2P");
            }

            this.version = ver;

            this.local   = remote.NSMessageHandler.Owner;
            this.localEP = NSMessageHandler.MachineGuid;

            this.remote   = remote;
            this.remoteEP = remoteEP;

            this.nsMessageHandler = remote.NSMessageHandler;

            Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo,
                              "Application created: " + ver, GetType().Name);
        }
Ejemplo n.º 14
0
        public TCPv1Bridge(
            ConnectivitySettings connectivitySettings,
            P2PVersion p2pVersion,
            Guid replyNonce, Guid remoteNonce, bool isNeedHash,
            P2PSession p2pSession,
            NSMessageHandler ns,
            Contact remote, Guid remoteEpId)
            : base(0, ns)
        {
            this.startupSession = p2pSession;
            this.remote         = remote;
            this.remoteEpId     = remoteEpId;

            directConnection = new P2PDirectProcessor(connectivitySettings, p2pVersion, replyNonce, remoteNonce, isNeedHash, p2pSession, ns);
            directConnection.HandshakeCompleted += new EventHandler <EventArgs>(directConnection_HandshakeCompleted);
            directConnection.P2PMessageReceived += new EventHandler <P2PMessageEventArgs>(directConnection_P2PMessageReceived);
            directConnection.SendCompleted      += new EventHandler <MSNPSharp.Core.ObjectEventArgs>(directConnection_SendCompleted);

            directConnection.DirectNegotiationTimedOut += new EventHandler <EventArgs>(directConnection_DirectNegotiationTimedOut);
            directConnection.ConnectionClosed          += new EventHandler <EventArgs>(directConnection_ConnectionClosed);
            directConnection.ConnectingException       += new EventHandler <ExceptionEventArgs>(directConnection_ConnectingException);
            directConnection.ConnectionException       += new EventHandler <ExceptionEventArgs>(directConnection_ConnectionException);
        }
Ejemplo n.º 15
0
        public TCPv1Bridge(
            ConnectivitySettings connectivitySettings,
            P2PVersion p2pVersion,
            Guid replyNonce, Guid remoteNonce, bool isNeedHash,
            P2PSession p2pSession,
            NSMessageHandler ns,
            Contact remote, Guid remoteEpId)
            : base(0, ns)
        {
            this.startupSession = p2pSession;
            this.remote = remote;
            this.remoteEpId = remoteEpId;

            directConnection = new P2PDirectProcessor(connectivitySettings, p2pVersion, replyNonce, remoteNonce, isNeedHash, p2pSession, ns);
            directConnection.HandshakeCompleted += new EventHandler<EventArgs>(directConnection_HandshakeCompleted);
            directConnection.P2PMessageReceived += new EventHandler<P2PMessageEventArgs>(directConnection_P2PMessageReceived);
            directConnection.SendCompleted += new EventHandler<MSNPSharp.Core.ObjectEventArgs>(directConnection_SendCompleted);

            directConnection.DirectNegotiationTimedOut += new EventHandler<EventArgs>(directConnection_DirectNegotiationTimedOut);
            directConnection.ConnectionClosed += new EventHandler<EventArgs>(directConnection_ConnectionClosed);
            directConnection.ConnectingException += new EventHandler<ExceptionEventArgs>(directConnection_ConnectingException);
            directConnection.ConnectionException += new EventHandler<ExceptionEventArgs>(directConnection_ConnectionException);
        }
 public SecurityTokenServiceWrapper(NSMessageHandler nsHandler)
     : base()
 {
     this.nsHandler = nsHandler;
 }
Ejemplo n.º 17
0
 public static XMLContactList LoadFromFile(string filename, MclSerialization st, NSMessageHandler nsHandler, bool useCache)
 {
     XMLContactList contactlist = (XMLContactList)LoadFromFile(filename, st, typeof(XMLContactList), nsHandler.Credentials.Password, useCache);
     contactlist.NSMessageHandler = nsHandler;
     return contactlist;
 }
 public SecurityTokenServiceWrapper(NSMessageHandler nsHandler)
     : base()
 {
     this.nsHandler = nsHandler;
 }
Ejemplo n.º 19
0
        private static IPEndPoint[] SelectIPEndPoint(MimeDictionary bodyValues, NSMessageHandler nsMessageHandler)
        {
            List <IPEndPoint> externalPoints = new List <IPEndPoint>();
            List <IPEndPoint> internalPoints = new List <IPEndPoint>();
            bool nat = false;

            #region External

            if (bodyValues.ContainsKey("IPv4External-Addrs") || bodyValues.ContainsKey("srddA-lanretxE4vPI") ||
                bodyValues.ContainsKey("IPv4ExternalAddrsAndPorts") || bodyValues.ContainsKey("stroPdnAsrddAlanretxE4vPI"))
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, "Using external IP addresses");

                if (bodyValues.ContainsKey("IPv4External-Addrs"))
                {
                    string[] addrs = bodyValues["IPv4External-Addrs"].Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    string[] ports = bodyValues["IPv4External-Port"].Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (addrs.Length > 0 && ports.Length > 0)
                    {
                        IPAddress ip;
                        int       port = 0;
                        int.TryParse(ports[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out port);

                        for (int i = 0; i < addrs.Length; i++)
                        {
                            if (IPAddress.TryParse(addrs[i], out ip))
                            {
                                if (i < ports.Length)
                                {
                                    int.TryParse(ports[i], NumberStyles.Integer, CultureInfo.InvariantCulture, out port);
                                }

                                if (port > 0)
                                {
                                    externalPoints.Add(new IPEndPoint(ip, port));
                                }
                            }
                        }
                    }
                }
                else if (bodyValues.ContainsKey("IPv4ExternalAddrsAndPorts"))
                {
                    string[]  addrsAndPorts = bodyValues["IPv4ExternalAddrsAndPorts"].Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    IPAddress ip;
                    foreach (string str in addrsAndPorts)
                    {
                        string[] addrAndPort = str.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                        if (IPAddress.TryParse(addrAndPort[0], out ip))
                        {
                            externalPoints.Add(new IPEndPoint(ip, int.Parse(addrAndPort[1])));
                        }
                    }
                }
                else if (bodyValues.ContainsKey("srddA-lanretxE4vPI"))
                {
                    nat = true;

                    char[] revHost = bodyValues["srddA-lanretxE4vPI"].ToString().ToCharArray();
                    Array.Reverse(revHost);
                    string[] addrs = new string(revHost).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    char[] revPort = bodyValues["troP-lanretxE4vPI"].ToString().ToCharArray();
                    Array.Reverse(revPort);
                    string[] ports = new string(revPort).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (addrs.Length > 0 && ports.Length > 0)
                    {
                        IPAddress ip;
                        int       port = 0;
                        int.TryParse(ports[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out port);

                        for (int i = 0; i < addrs.Length; i++)
                        {
                            if (IPAddress.TryParse(addrs[i], out ip))
                            {
                                if (i < ports.Length)
                                {
                                    int.TryParse(ports[i], NumberStyles.Integer, CultureInfo.InvariantCulture, out port);
                                }

                                if (port > 0)
                                {
                                    externalPoints.Add(new IPEndPoint(ip, port));
                                }
                            }
                        }
                    }
                }
                else if (bodyValues.ContainsKey("stroPdnAsrddAlanretxE4vPI"))
                {
                    nat = true;

                    char[] rev = bodyValues["stroPdnAsrddAlanretxE4vPI"].ToString().ToCharArray();
                    Array.Reverse(rev);
                    string[] addrsAndPorts = new string(rev).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    IPAddress ip;
                    foreach (string str in addrsAndPorts)
                    {
                        string[] addrAndPort = str.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

                        if (IPAddress.TryParse(addrAndPort[0], out ip))
                        {
                            externalPoints.Add(new IPEndPoint(ip, int.Parse(addrAndPort[1])));
                        }
                    }
                }

                Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning,
                                  String.Format("{0} external IP addresses found:", externalPoints.Count));

                foreach (IPEndPoint ipep in externalPoints)
                {
                    Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "\t" + ipep.ToString());
                }
            }

            #endregion

            #region Internal

            if (bodyValues.ContainsKey("IPv4Internal-Addrs") || bodyValues.ContainsKey("srddA-lanretnI4vPI") ||
                bodyValues.ContainsKey("IPv4InternalAddrsAndPorts") || bodyValues.ContainsKey("stroPdnAsrddAlanretnI4vPI"))
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, "Using internal IP addresses");

                if (bodyValues.ContainsKey("IPv4Internal-Addrs"))
                {
                    string[] addrs = bodyValues["IPv4Internal-Addrs"].Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    string[] ports = bodyValues["IPv4Internal-Port"].Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (addrs.Length > 0 && ports.Length > 0)
                    {
                        IPAddress ip;
                        int       port = 0;
                        int.TryParse(ports[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out port);

                        for (int i = 0; i < addrs.Length; i++)
                        {
                            if (IPAddress.TryParse(addrs[i], out ip))
                            {
                                if (i < ports.Length)
                                {
                                    int.TryParse(ports[i], NumberStyles.Integer, CultureInfo.InvariantCulture, out port);
                                }

                                if (port > 0)
                                {
                                    internalPoints.Add(new IPEndPoint(ip, port));
                                }
                            }
                        }
                    }
                }
                else if (bodyValues.ContainsKey("IPv4InternalAddrsAndPorts"))
                {
                    string[]  addrsAndPorts = bodyValues["IPv4InternalAddrsAndPorts"].Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    IPAddress ip;
                    foreach (string str in addrsAndPorts)
                    {
                        string[] addrAndPort = str.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                        if (IPAddress.TryParse(addrAndPort[0], out ip))
                        {
                            internalPoints.Add(new IPEndPoint(ip, int.Parse(addrAndPort[1])));
                        }
                    }
                }
                else if (bodyValues.ContainsKey("srddA-lanretnI4vPI"))
                {
                    nat = true;

                    char[] revHost = bodyValues["srddA-lanretnI4vPI"].ToString().ToCharArray();
                    Array.Reverse(revHost);
                    string[] addrs = new string(revHost).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    char[] revPort = bodyValues["troP-lanretnI4vPI"].ToString().ToCharArray();
                    Array.Reverse(revPort);
                    string[] ports = new string(revPort).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (addrs.Length > 0 && ports.Length > 0)
                    {
                        IPAddress ip;
                        int       port = 0;
                        int.TryParse(ports[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out port);

                        for (int i = 0; i < addrs.Length; i++)
                        {
                            if (IPAddress.TryParse(addrs[i], out ip))
                            {
                                if (i < ports.Length)
                                {
                                    int.TryParse(ports[i], NumberStyles.Integer, CultureInfo.InvariantCulture, out port);
                                }

                                if (port > 0)
                                {
                                    internalPoints.Add(new IPEndPoint(ip, port));
                                }
                            }
                        }
                    }
                }
                else if (bodyValues.ContainsKey("stroPdnAsrddAlanretnI4vPI"))
                {
                    nat = true;

                    char[] rev = bodyValues["stroPdnAsrddAlanretnI4vPI"].ToString().ToCharArray();
                    Array.Reverse(rev);
                    string[] addrsAndPorts = new string(rev).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    IPAddress ip;
                    foreach (string str in addrsAndPorts)
                    {
                        string[] addrAndPort = str.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

                        if (IPAddress.TryParse(addrAndPort[0], out ip))
                        {
                            internalPoints.Add(new IPEndPoint(ip, int.Parse(addrAndPort[1])));
                        }
                    }
                }

                Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning,
                                  String.Format("{0} internal IP addresses found:", internalPoints.Count));

                foreach (IPEndPoint ipep in internalPoints)
                {
                    Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "\t" + ipep.ToString());
                }
            }

            #endregion

            if (externalPoints.Count == 0 && internalPoints.Count == 0)
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceError, "Unable to find any remote IP addresses");

                // Failed
                return(null);
            }

            Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, "Is NAT: " + nat);

            List <IPEndPoint> ret = new List <IPEndPoint>();

            // Try to find the correct IP
            byte[] localBytes = nsMessageHandler.LocalEndPoint.Address.GetAddressBytes();

            foreach (IPEndPoint ipep in internalPoints)
            {
                if (ipep.Address.AddressFamily == AddressFamily.InterNetwork)
                {
                    // This is an IPv4 address
                    // Check if the first 3 octets match our local IP address
                    // If so, make use of that address (it's on our LAN)

                    byte[] bytes = ipep.Address.GetAddressBytes();

                    if ((bytes[0] == localBytes[0]) && (bytes[1] == localBytes[1]) && (bytes[2] == localBytes[2]))
                    {
                        ret.Add(ipep);
                        break;
                    }
                }
            }

            if (ret.Count == 0)
            {
                foreach (IPEndPoint ipep in internalPoints)
                {
                    if (!ret.Contains(ipep))
                    {
                        ret.Add(ipep);
                    }
                }
            }

            foreach (IPEndPoint ipep in externalPoints)
            {
                if (!ret.Contains(ipep))
                {
                    ret.Add(ipep);
                }
            }

            return(ret.ToArray());
        }
Ejemplo n.º 20
0
 public SDGBridge(NSMessageHandler nsHandler)
     : base(QueueSize, nsHandler)
 {
     nsHandler.MessageProcessorChanged += nsHandler_MessageProcessorChanged;
 }
Ejemplo n.º 21
0
 public SDGBridge(NSMessageHandler nsHandler)
     : base(QueueSize, nsHandler)
 {
     nsHandler.MessageProcessorChanged += nsHandler_MessageProcessorChanged;
 }
Ejemplo n.º 22
0
        private static void ProcessDCRespInvite(SLPMessage message, NSMessageHandler ns, P2PSession startupSession)
        {
            MimeDictionary bodyValues = message.BodyValues;

            // Check the protocol
            if (bodyValues.ContainsKey("Bridge") &&
                bodyValues["Bridge"].ToString() == "TCPv1" &&
                bodyValues.ContainsKey("Listening") &&
                bodyValues["Listening"].ToString().ToLowerInvariant().IndexOf("true") >= 0)
            {
                Contact remote     = ns.ContactList.GetContactWithCreate(message.FromEmailAccount, IMAddressInfoType.WindowsLive);
                Guid    remoteGuid = message.FromEndPoint;

                DCNonceType dcNonceType;
                Guid        remoteNonce = ParseDCNonce(message.BodyValues, out dcNonceType);
                bool        hashed      = (dcNonceType == DCNonceType.Sha1);
                Guid        replyGuid   = hashed ? remote.dcPlainKey : remoteNonce;

                IPEndPoint[] selectedPoint = SelectIPEndPoint(bodyValues, ns);

                if (selectedPoint != null && selectedPoint.Length > 0)
                {
                    P2PVersion ver = message.P2PVersion;

                    // We must connect to the remote client
                    ConnectivitySettings settings = new ConnectivitySettings();
                    settings.EndPoints  = selectedPoint;
                    remote.DirectBridge = CreateDirectConnection(remote, remoteGuid, ver, settings, replyGuid, remoteNonce, hashed, ns, startupSession);

                    bool needConnectingEndpointInfo;
                    if (bodyValues.ContainsKey("NeedConnectingEndpointInfo") &&
                        bool.TryParse(bodyValues["NeedConnectingEndpointInfo"], out needConnectingEndpointInfo) &&
                        needConnectingEndpointInfo == true)
                    {
                        IPEndPoint ipep = ((TCPv1Bridge)remote.DirectBridge).LocalEndPoint;

                        string desc = "stroPdnAsrddAlanretnI4vPI";
                        char[] rev  = ipep.ToString().ToCharArray();
                        Array.Reverse(rev);
                        string ipandport = new string(rev);

                        SLPRequestMessage slpResponseMessage = new SLPRequestMessage(message.Source, MSNSLPRequestMethod.ACK);
                        slpResponseMessage.Source      = message.Target;
                        slpResponseMessage.Via         = message.Via;
                        slpResponseMessage.CSeq        = 0;
                        slpResponseMessage.CallId      = Guid.Empty;
                        slpResponseMessage.MaxForwards = 0;
                        slpResponseMessage.ContentType = @"application/x-msnmsgr-transdestaddrupdate";

                        slpResponseMessage.BodyValues[desc] = ipandport;
                        slpResponseMessage.BodyValues["Nat-Trav-Msg-Type"] = "WLX-Nat-Trav-Msg-Updated-Connecting-Port";

                        P2PMessage msg = new P2PMessage(ver);
                        msg.InnerMessage = slpResponseMessage;

                        ns.SDGBridge.Send(null, remote, remoteGuid, msg);
                    }

                    return;
                }
            }

            if (startupSession != null)
            {
                startupSession.DirectNegotiationFailed();
            }
        }
Ejemplo n.º 23
0
 protected internal P2PHandler(NSMessageHandler nsHandler)
 {
     this.nsMessageHandler = nsHandler;
     this.sdgBridge        = new SDGBridge(nsHandler);
     this.slpHandler       = new SLPHandler(nsHandler);
 }
Ejemplo n.º 24
0
        /// <summary>
        /// Creates a new session initiated remotely. If the application cannot be handled, decline message will be sent.
        /// The application will handle all messages automatically and no user interaction is required if AutoAccept is true.
        /// If AutoAccept is false, <see cref="P2PHandler.InvitationReceived"/> event will be fired.
        /// </summary>
        public P2PSession(SLPRequestMessage slp, P2PMessage msg, NSMessageHandler ns, P2PBridge bridge)
        {
            nsMessageHandler = ns;
            invitation       = slp;
            version          = slp.P2PVersion;

            if (version == P2PVersion.P2PV1)
            {
                localContact = (slp.ToEmailAccount == ns.Owner.Account) ?
                               ns.Owner : ns.ContactList.GetContactWithCreate(slp.ToEmailAccount, IMAddressInfoType.WindowsLive);

                remoteContact = ns.ContactList.GetContactWithCreate(slp.FromEmailAccount, IMAddressInfoType.WindowsLive);
            }
            else
            {
                localContact = (slp.ToEmailAccount == ns.Owner.Account) ?
                               ns.Owner : ns.ContactList.GetContactWithCreate(slp.ToEmailAccount, IMAddressInfoType.WindowsLive);
                localContactEndPointID = slp.ToEndPoint;

                remoteContact           = ns.ContactList.GetContactWithCreate(slp.FromEmailAccount, IMAddressInfoType.WindowsLive);
                remoteContactEndPointID = slp.FromEndPoint;
            }

            if (!uint.TryParse(slp.BodyValues["SessionID"].Value, out sessionId))
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning,
                                  "Can't parse SessionID: " + SessionId, GetType().Name);
            }

            p2pBridge           = bridge;
            localBaseIdentifier = bridge.SequenceId;
            localIdentifier     = localBaseIdentifier;
            status = P2PSessionStatus.WaitingForLocal;

            Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo,
                              String.Format("{0} created (Initiated remotely)", SessionId), GetType().Name);

            remoteContact.DirectBridgeEstablished += RemoteDirectBridgeEstablished;

            if (msg != null)
            {
                // Set remote baseID
                remoteBaseIdentifier = msg.Header.Identifier;
                remoteIdentifier     = remoteBaseIdentifier;

                if (version == P2PVersion.P2PV2)
                {
                    remoteIdentifier += msg.V2Header.MessageSize;
                }
            }

            // Create application based on invitation
            uint appId   = slp.BodyValues.ContainsKey("AppID") ? uint.Parse(slp.BodyValues["AppID"].Value) : 0;
            Guid eufGuid = slp.BodyValues.ContainsKey("EUF-GUID") ? new Guid(slp.BodyValues["EUF-GUID"].Value) : Guid.Empty;

            p2pApplication = P2PApplication.CreateInstance(eufGuid, appId, this);

            if (p2pApplication != null && p2pApplication.ValidateInvitation(invitation))
            {
                if (p2pApplication.AutoAccept)
                {
                    Accept();
                }
                else
                {
                    ns.P2PHandler.OnInvitationReceived(new P2PSessionEventArgs(this));
                }
            }
            else
            {
                Decline();
            }
        }
Ejemplo n.º 25
0
 protected internal SLPHandler(NSMessageHandler nsHandler)
 {
     this.nsMessageHandler = nsHandler;
 }
Ejemplo n.º 26
0
        private static TCPv1Bridge CreateDirectConnection(Contact remote, Guid remoteGuid, P2PVersion ver, ConnectivitySettings cs, Guid replyGuid, Guid remoteNonce, bool hashed, NSMessageHandler nsMessageHandler, P2PSession startupSession)
        {
            string[] points = new string[cs.EndPoints.Length];

            for (int i = 0; i < points.Length; i++)
            {
                points[i] = cs.EndPoints[i].ToString();
            }

            TCPv1Bridge tcpBridge = new TCPv1Bridge(cs, ver, replyGuid, remoteNonce, hashed, startupSession, nsMessageHandler, remote, remoteGuid);

            Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, "Trying to setup direct connection with remote hosts " + string.Join(",", points));

            tcpBridge.Connect();

            return(tcpBridge);
        }
Ejemplo n.º 27
0
 protected internal SLPHandler(NSMessageHandler nsHandler)
 {
     this.nsMessageHandler = nsHandler;
 }
Ejemplo n.º 28
0
        /// <summary>
        /// P2PBridge constructor.
        /// </summary>
        protected P2PBridge(int queueSize, NSMessageHandler nsHandler)
        {
            this.queueSize = queueSize;
            this.sequenceId = (uint)new Random().Next(5000, int.MaxValue);
            this.nextCleanup = NextCleanupTime();
            this.nsMessageHandler = nsHandler;

            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose,
                String.Format("P2PBridge {0} created", this.ToString()), GetType().Name);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public P2PDirectProcessor(
            ConnectivitySettings connectivitySettings,
            P2PVersion p2pVersion,
            Guid reply, Guid authNonce, bool isNeedHash,
            P2PSession p2pMessageSession,
            NSMessageHandler nsMessageHandler)
        {
            Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, "Constructing object - " + p2pVersion, GetType().Name);

            Processor = new TcpSocketMessageProcessor(connectivitySettings, new P2PDCPool());

            this.version = p2pVersion;
            this.nonce = authNonce;
            this.reply = reply;
            this.needHash = isNeedHash;
            this.startupSession = p2pMessageSession;
            this.nsMessageHandler = nsMessageHandler;
        }
Ejemplo n.º 30
0
        private static void ProcessDCReqInvite(SLPMessage message, NSMessageHandler ns, P2PSession startupSession)
        {
            if (startupSession != null && startupSession.Bridge != null &&
                startupSession.Bridge is TCPv1Bridge)
            {
                return; // We are using a dc bridge already. Don't allow second one.
            }

            if (message.BodyValues.ContainsKey("Bridges") &&
                message.BodyValues["Bridges"].ToString().Contains("TCPv1"))
            {
                SLPStatusMessage slpMessage = new SLPStatusMessage(message.Source, 200, "OK");
                slpMessage.Target               = message.Source;
                slpMessage.Source               = message.Target;
                slpMessage.Branch               = message.Branch;
                slpMessage.CSeq                 = 1;
                slpMessage.CallId               = message.CallId;
                slpMessage.MaxForwards          = 0;
                slpMessage.ContentType          = "application/x-msnmsgr-transrespbody";
                slpMessage.BodyValues["Bridge"] = "TCPv1";

                Guid    remoteGuid = message.FromEndPoint;
                Contact remote     = ns.ContactList.GetContactWithCreate(message.FromEmailAccount, IMAddressInfoType.WindowsLive);

                DCNonceType dcNonceType;
                Guid        remoteNonce = ParseDCNonce(message.BodyValues, out dcNonceType);
                if (remoteNonce == Guid.Empty) // Plain
                {
                    remoteNonce = remote.dcPlainKey;
                }

                bool   hashed         = (dcNonceType == DCNonceType.Sha1);
                string nonceFieldName = hashed ? "Hashed-Nonce" : "Nonce";
                Guid   myHashedNonce  = hashed ? remote.dcLocalHashedNonce : remoteNonce;
                Guid   myPlainNonce   = remote.dcPlainKey;
                if (dcNonceType == DCNonceType.Sha1)
                {
                    // Remote contact supports Hashed-Nonce
                    remote.dcType = dcNonceType;
                    remote.dcRemoteHashedNonce = remoteNonce;
                }
                else
                {
                    remote.dcType = DCNonceType.Plain;
                    myPlainNonce  = remote.dcPlainKey = remote.dcLocalHashedNonce = remote.dcRemoteHashedNonce = remoteNonce;
                }

                // Find host by name
                IPAddress ipAddress = ns.LocalEndPoint.Address;
                int       port;

                P2PVersion ver = message.P2PVersion;

                if (Settings.DisableP2PDirectConnections ||
                    false == ipAddress.Equals(ns.ExternalEndPoint.Address) ||
                    (0 == (port = GetNextDirectConnectionPort(ipAddress))))
                {
                    slpMessage.BodyValues["Listening"]    = "false";
                    slpMessage.BodyValues[nonceFieldName] = Guid.Empty.ToString("B").ToUpper(CultureInfo.InvariantCulture);
                }
                else
                {
                    // Let's listen
                    remote.DirectBridge = ListenForDirectConnection(remote, remoteGuid, ns, ver, startupSession, ipAddress, port, myPlainNonce, remoteNonce, hashed);

                    slpMessage.BodyValues["Listening"]          = "true";
                    slpMessage.BodyValues["Capabilities-Flags"] = "1";
                    slpMessage.BodyValues["IPv6-global"]        = string.Empty;
                    slpMessage.BodyValues["Nat-Trav-Msg-Type"]  = "WLX-Nat-Trav-Msg-Direct-Connect-Resp";
                    slpMessage.BodyValues["UPnPNat"]            = "false";

                    slpMessage.BodyValues["NeedConnectingEndpointInfo"] = "true";

                    slpMessage.BodyValues["Conn-Type"]          = "Direct-Connect";
                    slpMessage.BodyValues["TCP-Conn-Type"]      = "Direct-Connect";
                    slpMessage.BodyValues[nonceFieldName]       = myHashedNonce.ToString("B").ToUpper(CultureInfo.InvariantCulture);
                    slpMessage.BodyValues["IPv4Internal-Addrs"] = ipAddress.ToString();
                    slpMessage.BodyValues["IPv4Internal-Port"]  = port.ToString(CultureInfo.InvariantCulture);

                    // check if client is behind firewall (NAT-ted)
                    // if so, send the public ip also the client, so it can try to connect to that ip
                    if (!ns.ExternalEndPoint.Address.Equals(ns.LocalEndPoint.Address))
                    {
                        slpMessage.BodyValues["IPv4External-Addrs"] = ns.ExternalEndPoint.Address.ToString();
                        slpMessage.BodyValues["IPv4External-Port"]  = port.ToString(CultureInfo.InvariantCulture);
                    }
                }

                P2PMessage p2pMessage = new P2PMessage(ver);
                p2pMessage.InnerMessage = slpMessage;

                if (ver == P2PVersion.P2PV2)
                {
                    p2pMessage.V2Header.TFCombination = TFCombination.First;
                }
                else if (ver == P2PVersion.P2PV1)
                {
                    p2pMessage.V1Header.Flags = P2PFlag.MSNSLPInfo;
                }

                if (startupSession != null)
                {
                    startupSession.SetupDCTimer();
                    startupSession.Bridge.Send(null, startupSession.Remote, startupSession.RemoteContactEndPointID, p2pMessage);
                }
                else
                {
                    ns.SDGBridge.Send(null, remote, remoteGuid, p2pMessage);
                }
            }
            else
            {
                if (startupSession != null)
                {
                    startupSession.DirectNegotiationFailed();
                }
            }
        }