1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
using System;
using System.IO;
using System.Xml;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Hazel.UPnP
{
/// <summary>
/// Status of the UPnP capabilities
/// </summary>
public enum UPnPStatus
{
/// <summary>
/// Still discovering UPnP capabilities
/// </summary>
Discovering,
/// <summary>
/// UPnP is not available
/// </summary>
NotAvailable,
/// <summary>
/// UPnP is available and ready to use
/// </summary>
Available
}
public class UPnPHelper : IDisposable
{
private const int DiscoveryTimeOutMs = 1000;
private string serviceUrl;
private string serviceName = "";
private ManualResetEvent discoveryComplete = new ManualResetEvent(false);
private Socket socket;
private DateTime discoveryResponseDeadline;
private EndPoint ep;
private byte[] buffer;
private ILogger logger;
/// <summary>
/// Status of the UPnP capabilities of this NetPeer
/// </summary>
public UPnPStatus Status { get; private set; }
public UPnPHelper(ILogger logger)
{
this.logger = logger;
this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
this.socket.EnableBroadcast = true;
this.socket.MulticastLoopback = false;
this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
this.socket.Bind(new IPEndPoint(IPAddress.Any, 0));
this.ep = new IPEndPoint(IPAddress.Any, 1900);
this.buffer = new byte[ushort.MaxValue];
ListenForUPnP();
this.Discover();
}
private void ListenForUPnP()
{
try
{
socket.BeginReceiveFrom(this.buffer, 0, this.buffer.Length, SocketFlags.None, ref ep, HandleMessage, null);
}
catch(Exception e)
{
this.logger.WriteInfo("Exception listening for UPnP: " + e.Message);
}
}
private void HandleMessage(IAsyncResult ar)
{
int len;
try
{
len = this.socket.EndReceiveFrom(ar, ref ep);
}
catch
{
return;
}
string resp = System.Text.Encoding.UTF8.GetString(buffer, 0, len);
if (resp.Contains("upnp:rootdevice") || resp.Contains("UPnP/1.0"))
{
var locationStart = resp.IndexOf("location:", StringComparison.OrdinalIgnoreCase);
if (locationStart >= 0)
{
locationStart += 10;
var locationEnd = resp.IndexOf("\r", locationStart);
resp = resp.Substring(locationStart, locationEnd - locationStart);
if (!ExtractServiceUrl(resp))
{
ListenForUPnP();
}
}
else
{
ListenForUPnP();
}
}
else
{
ListenForUPnP();
}
}
internal void Discover()
{
string str =
"M-SEARCH * HTTP/1.1\r\n" +
"HOST: 239.255.255.250:1900\r\n" +
"ST:upnp:rootdevice\r\n" +
"MAN:\"ssdp:discover\"\r\n" +
"MX:3\r\n\r\n";
discoveryResponseDeadline = DateTime.UtcNow.AddSeconds(6);
Status = UPnPStatus.Discovering;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
this.logger.WriteInfo("Attempting UPnP discovery");
socket.SendTo(buffer, new IPEndPoint(NetUtility.GetBroadcastAddress(), 1900));
}
internal bool ExtractServiceUrl(string resp)
{
try
{
XmlDocument desc = new XmlDocument();
using (var response = WebRequest.Create(resp).GetResponse())
{
desc.Load(response.GetResponseStream());
}
XmlNamespaceManager nsMgr = new XmlNamespaceManager(desc.NameTable);
nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
XmlNode typen = desc.SelectSingleNode("//tns:device/tns:deviceType/text()", nsMgr);
if (!typen.Value.Contains("InternetGatewayDevice"))
return false;
serviceName = "WANIPConnection";
XmlNode node = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:" + serviceName + ":1\"]/tns:controlURL/text()", nsMgr);
if (node == null)
{
//try another service name
serviceName = "WANPPPConnection";
node = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:" + serviceName + ":1\"]/tns:controlURL/text()", nsMgr);
if (node == null)
return false;
}
serviceUrl = CombineUrls(resp, node.Value);
this.logger.WriteInfo("UPnP service ready");
Status = UPnPStatus.Available;
discoveryComplete.Set();
return true;
}
catch (Exception e)
{
this.logger.WriteError("Exception while parsing UPnP Service URL: " + e.Message);
return false;
}
}
private static string CombineUrls(string gatewayURL, string subURL)
{
// Is Control URL an absolute URL?
if (subURL.Contains("http:") || subURL.Contains("."))
return subURL;
gatewayURL = gatewayURL.Replace("http://", ""); // strip any protocol
int n = gatewayURL.IndexOf("/");
if (n >= 0)
{
gatewayURL = gatewayURL.Substring(0, n); // Use first portion of URL
}
return "http://" + gatewayURL + subURL;
}
private bool CheckAvailability()
{
switch (Status)
{
case UPnPStatus.NotAvailable:
return false;
case UPnPStatus.Available:
return true;
case UPnPStatus.Discovering:
while (!discoveryComplete.WaitOne(DiscoveryTimeOutMs))
{
if (DateTime.UtcNow > discoveryResponseDeadline)
{
Status = UPnPStatus.NotAvailable;
return false;
}
}
return true;
}
return false;
}
/// <summary>
/// Add a forwarding rule to the router using UPnP
/// </summary>
/// <param name="externalPort">The external, WAN facing, port</param>
/// <param name="description">A description for the port forwarding rule</param>
/// <param name="internalPort">The port on the client machine to send traffic to</param>
/// <param name="durationSeconds">The lease duration on the port forwarding rule, in seconds. 0 for indefinite.</param>
public bool ForwardPort(int externalPort, string description, int internalPort = 0, int durationSeconds = 0)
{
if (!CheckAvailability())
return false;
if (internalPort == 0)
internalPort = externalPort;
try
{
var client = NetUtility.GetMyAddress(out _);
if (client == null)
return false;
SOAPRequest(serviceUrl,
$"<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:{serviceName}:1\">" +
"<NewRemoteHost></NewRemoteHost>" +
$"<NewExternalPort>{externalPort}</NewExternalPort>" +
"<NewProtocol>UDP</NewProtocol>" +
$"<NewInternalPort>{internalPort}</NewInternalPort>" +
$"<NewInternalClient>{client}</NewInternalClient>" +
"<NewEnabled>1</NewEnabled>" +
$"<NewPortMappingDescription>{description}</NewPortMappingDescription>" +
$"<NewLeaseDuration>{durationSeconds}</NewLeaseDuration>" +
"</u:AddPortMapping>",
"AddPortMapping");
this.logger.WriteInfo("Sent UPnP port forward request.");
return true;
}
catch (Exception ex)
{
this.logger.WriteError("UPnP port forward failed: " + ex.Message);
return false;
}
}
/// <summary>
/// Delete a forwarding rule from the router using UPnP
/// </summary>
/// <param name="externalPort">The external, 'internet facing', port</param>
public bool DeleteForwardingRule(int externalPort)
{
if (!CheckAvailability())
return false;
try
{
SOAPRequest(serviceUrl,
$"<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:{serviceName}:1\">" +
"<NewRemoteHost></NewRemoteHost>" +
$"<NewExternalPort>{externalPort}</NewExternalPort>" +
$"<NewProtocol>UDP</NewProtocol>" +
"</u:DeletePortMapping>", "DeletePortMapping");
return true;
}
catch (Exception ex)
{
// m_peer.LogWarning("UPnP delete forwarding rule failed: " + ex.Message);
return false;
}
}
/// <summary>
/// Retrieve the extern ip using UPnP
/// </summary>
public IPAddress GetExternalIP()
{
if (!CheckAvailability())
return null;
try
{
XmlDocument xdoc = SOAPRequest(serviceUrl, "<u:GetExternalIPAddress xmlns:u=\"urn:schemas-upnp-org:service:" + serviceName + ":1\">" +
"</u:GetExternalIPAddress>", "GetExternalIPAddress");
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xdoc.NameTable);
nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
string IP = xdoc.SelectSingleNode("//NewExternalIPAddress/text()", nsMgr).Value;
return IPAddress.Parse(IP);
}
catch (Exception ex)
{
// m_peer.LogWarning("Failed to get external IP: " + ex.Message);
return null;
}
}
private XmlDocument SOAPRequest(string url, string soap, string function)
{
string req =
"<?xml version=\"1.0\"?>" +
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
$"<s:Body>{soap}</s:Body>" +
"</s:Envelope>";
WebRequest r = HttpWebRequest.Create(url);
r.Headers.Add("SOAPACTION", $"\"urn:schemas-upnp-org:service:{serviceName}:1#{function}\"");
r.ContentType = "text/xml; charset=\"utf-8\"";
r.Method = "POST";
byte[] b = System.Text.Encoding.UTF8.GetBytes(req);
r.ContentLength = b.Length;
r.GetRequestStream().Write(b, 0, b.Length);
using (WebResponse wres = r.GetResponse())
{
XmlDocument resp = new XmlDocument();
Stream ress = wres.GetResponseStream();
resp.Load(ress);
return resp;
}
}
public void Dispose()
{
this.discoveryComplete.Dispose();
try { this.socket.Shutdown(SocketShutdown.Both); } catch { }
this.socket.Dispose();
}
}
}
|