Upnp.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // Authors:
  3. // Alan McGovern alan.mcgovern@gmail.com
  4. // Ben Motmans <ben.motmans@gmail.com>
  5. // Nicholas Terry <nick.i.terry@gmail.com>
  6. //
  7. // Copyright (C) 2006 Alan McGovern
  8. // Copyright (C) 2007 Ben Motmans
  9. // Copyright (C) 2014 Nicholas Terry
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Net;
  34. using System.Text;
  35. namespace Mono.Nat.Upnp
  36. {
  37. internal class Upnp
  38. {
  39. public UpnpNatDevice Handle(IPAddress localAddress, byte[] response, IPEndPoint endpoint)
  40. {
  41. // Convert it to a string for easy parsing
  42. string dataString = null;
  43. string urn;
  44. dataString = Encoding.UTF8.GetString(response);
  45. if (NatUtility.Verbose)
  46. NatUtility.Log("UPnP Response: {0}", dataString);
  47. /* For UPnP Port Mapping we need ot find either WANPPPConnection or WANIPConnection.
  48. Any other device type is no good to us for this purpose. See the IGP overview paper
  49. page 5 for an overview of device types and their hierarchy.
  50. http://upnp.org/specs/gw/UPnP-gw-InternetGatewayDevice-v1-Device.pdf */
  51. /* TODO: Currently we are assuming version 1 of the protocol. We should figure out which
  52. version it is and apply the correct URN. */
  53. /* Some routers don't correctly implement the version ID on the URN, so we only search for the type
  54. prefix. */
  55. string log = "UPnP Response: Router advertised a '{0}' service";
  56. StringComparison c = StringComparison.OrdinalIgnoreCase;
  57. if (dataString.IndexOf("urn:schemas-upnp-org:service:WANIPConnection:", c) != -1)
  58. {
  59. urn = "urn:schemas-upnp-org:service:WANIPConnection:1";
  60. NatUtility.Log(log, "urn:schemas-upnp-org:service:WANIPConnection:1");
  61. }
  62. else if (dataString.IndexOf("urn:schemas-upnp-org:service:WANPPPConnection:", c) != -1)
  63. {
  64. urn = "urn:schemas-upnp-org:service:WANPPPConnection:1";
  65. NatUtility.Log(log, "urn:schemas-upnp-org:service:WANPPPConnection:");
  66. }
  67. else
  68. throw new NotSupportedException("Received non-supported device type");
  69. // We have an internet gateway device now
  70. return new UpnpNatDevice(localAddress, dataString, urn);
  71. }
  72. }
  73. }