public static Guid ParseDCNonce(MimeDictionary bodyValues, out DCNonceType dcNonceType) { dcNonceType = DCNonceType.None; Guid nonce = Guid.Empty; if (bodyValues.ContainsKey("Hashed-Nonce")) { nonce = new Guid(bodyValues["Hashed-Nonce"].Value); dcNonceType = DCNonceType.Sha1; } else if (bodyValues.ContainsKey("Nonce")) { nonce = new Guid(bodyValues["Nonce"].Value); dcNonceType = DCNonceType.Plain; } return nonce; }
public static Guid ParseDCNonce(MimeDictionary bodyValues, out DCNonceType dcNonceType) { dcNonceType = DCNonceType.None; Guid nonce = Guid.Empty; if (bodyValues.ContainsKey("Hashed-Nonce")) { nonce = new Guid(bodyValues["Hashed-Nonce"].Value); dcNonceType = DCNonceType.Sha1; } else if (bodyValues.ContainsKey("Nonce")) { nonce = new Guid(bodyValues["Nonce"].Value); dcNonceType = DCNonceType.Plain; } return(nonce); }
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(); }
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()); }
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(); } }