diff options
Diffstat (limited to 'Mono.Nat/Upnp/UpnpNatDevice.cs')
| -rw-r--r-- | Mono.Nat/Upnp/UpnpNatDevice.cs | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/Mono.Nat/Upnp/UpnpNatDevice.cs b/Mono.Nat/Upnp/UpnpNatDevice.cs index fda990fa8..cae7f5fc8 100644 --- a/Mono.Nat/Upnp/UpnpNatDevice.cs +++ b/Mono.Nat/Upnp/UpnpNatDevice.cs @@ -56,6 +56,11 @@ namespace Mono.Nat.Upnp internal UpnpNatDevice(IPAddress localAddress, UpnpDeviceInfo deviceInfo, IPEndPoint hostEndPoint, string serviceType, ILogger logger, IHttpClient httpClient) { + if (localAddress == null) + { + throw new ArgumentNullException("localAddress"); + } + this.LastSeen = DateTime.Now; this.localAddress = localAddress; @@ -72,21 +77,19 @@ namespace Mono.Nat.Upnp // Are we going to get addresses with the "http://" attached? if (locationDetails.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { - NatUtility.Log("Found device at: {0}", locationDetails); + _logger.Debug("Found device at: {0}", locationDetails); // This bit strings out the "http://" from the string locationDetails = locationDetails.Substring(7); this.hostEndPoint = hostEndPoint; - NatUtility.Log("Parsed device as: {0}", this.hostEndPoint.ToString()); - // The service description URL is the remainder of the "locationDetails" string. The bit that was originally after the ip // and port information this.serviceDescriptionUrl = locationDetails.Substring(locationDetails.IndexOf('/')); } else { - NatUtility.Log("Couldn't decode address. Please send following string to the developer: "); + _logger.Debug("Couldn't decode address. Please send following string to the developer: "); } } @@ -113,7 +116,7 @@ namespace Mono.Nat.Upnp { if (response.StatusCode != HttpStatusCode.OK) { - NatUtility.Log("{0}: Couldn't get services list: {1}", HostEndPoint, response.StatusCode); + _logger.Debug("{0}: Couldn't get services list: {1}", HostEndPoint, response.StatusCode); return; // FIXME: This the best thing to do?? } @@ -136,12 +139,11 @@ namespace Mono.Nat.Upnp { return; } - NatUtility.Log("{0}: Couldn't parse services list", HostEndPoint); + _logger.Debug("{0}: Couldn't parse services list", HostEndPoint); System.Threading.Thread.Sleep(10); } } - NatUtility.Log("{0}: Parsed services list", HostEndPoint); XmlNamespaceManager ns = new XmlNamespaceManager(xmldoc.NameTable); ns.AddNamespace("ns", "urn:schemas-upnp-org:device-1-0"); XmlNodeList nodes = xmldoc.SelectNodes("//*/ns:serviceList", ns); @@ -153,31 +155,35 @@ namespace Mono.Nat.Upnp { //If the service is a WANIPConnection, then we have what we want string type = service["serviceType"].InnerText; - NatUtility.Log("{0}: Found service: {1}", HostEndPoint, type); - StringComparison c = StringComparison.OrdinalIgnoreCase; + _logger.Debug("{0}: Found service: {1}", HostEndPoint, type); + // TODO: Add support for version 2 of UPnP. - if (type.Equals("urn:schemas-upnp-org:service:WANPPPConnection:1", c) || - type.Equals("urn:schemas-upnp-org:service:WANIPConnection:1", c)) + if (string.Equals(type, "urn:schemas-upnp-org:service:WANPPPConnection:1", StringComparison.OrdinalIgnoreCase) || + string.Equals(type, "urn:schemas-upnp-org:service:WANIPConnection:1", StringComparison.OrdinalIgnoreCase)) { this.controlUrl = service["controlURL"].InnerText; - NatUtility.Log("{0}: Found upnp service at: {1}", HostEndPoint, controlUrl); - try + _logger.Debug("{0}: Found upnp service at: {1}", HostEndPoint, controlUrl); + + Uri u; + if (Uri.TryCreate(controlUrl, UriKind.RelativeOrAbsolute, out u)) { - Uri u = new Uri(controlUrl); if (u.IsAbsoluteUri) { EndPoint old = hostEndPoint; - this.hostEndPoint = new IPEndPoint(IPAddress.Parse(u.Host), u.Port); - NatUtility.Log("{0}: Absolute URI detected. Host address is now: {1}", old, HostEndPoint); - this.controlUrl = controlUrl.Substring(u.GetLeftPart(UriPartial.Authority).Length); - NatUtility.Log("{0}: New control url: {1}", HostEndPoint, controlUrl); + IPAddress parsedHostIpAddress; + if (IPAddress.TryParse(u.Host, out parsedHostIpAddress)) + { + this.hostEndPoint = new IPEndPoint(parsedHostIpAddress, u.Port); + //_logger.Debug("{0}: Absolute URI detected. Host address is now: {1}", old, HostEndPoint); + this.controlUrl = controlUrl.Substring(u.GetLeftPart(UriPartial.Authority).Length); + //_logger.Debug("{0}: New control url: {1}", HostEndPoint, controlUrl); + } } } - catch + else { - NatUtility.Log("{0}: Assuming control Uri is relative: {1}", HostEndPoint, controlUrl); + _logger.Debug("{0}: Assuming control Uri is relative: {1}", HostEndPoint, controlUrl); } - NatUtility.Log("{0}: Handshake Complete", HostEndPoint); return; } } |
