aboutsummaryrefslogtreecommitdiff
path: root/Mono.Nat/Upnp/UpnpNatDevice.cs
diff options
context:
space:
mode:
authorstefan <stefan@hegedues.at>2018-09-12 19:26:21 +0200
committerstefan <stefan@hegedues.at>2018-09-12 19:26:21 +0200
commit48facb797ed912e4ea6b04b17d1ff190ac2daac4 (patch)
tree8dae77a31670a888d733484cb17dd4077d5444e8 /Mono.Nat/Upnp/UpnpNatDevice.cs
parentc32d8656382a0eacb301692e0084377fc433ae9b (diff)
Update to 3.5.2 and .net core 2.1
Diffstat (limited to 'Mono.Nat/Upnp/UpnpNatDevice.cs')
-rw-r--r--Mono.Nat/Upnp/UpnpNatDevice.cs48
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;
}
}