upnp原理与实践

WANIPConnection

参考:http://upnp.org/specs/gw/UPnP-gw-WANIPConnection-v2-Service.pdf
IGD

  1. AddPortMapping()
    AddPortMapping
  2. NAT
    NAT
  3. upnp流程
    最多进行4层映射,第一层路由通过/proc/net/route获取本地网关,也就是route -n的功能,第一层映射成功后,可以根据GetExternalIPAddress获得该层路由出口ip,如果出口ip为内网ip,说明还有多层路由,traceroute可以跟踪路由信息,如traceroute -m 4 -w 1 -q 2 www.baidu.com,设置为各层路由的网关,使用出口ip作为NewInternalClient,发送upnp映射请求给各层路由的网关,尝试多层映射。

广播过程:

1
2
3
4
1. 设备 A 向多播地址发送信息(例如,发送到组播地址 X.X.X.X)。
2. 其他监听同一组播地址的设备 B、C、D 等可能会收到这个信息。
3. 设备 B 在收到信息后,如果需要回复,会使用单播(Unicast)向设备 A 发送回复。
4. 回复消息中的来源 IP 地址是设备 B 的 IP 地址。

广播实现:

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
#define UPNP_MULTICAST "239.255.255.250"
#define UPNP_MULTICAST_PORT 1900
#define UPNP_SERVICE_WANIP "urn:schemas-upnp-org:service:WANIPConnection:1"
#define UPNP_SERVICE_WANPPP "urn:schemas-upnp-org:service:WANPPPConnection:1"

struct sockaddr_in addr = { 0 };
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(UPNP_MULTICAST);
addr.sin_port = htons(UPNP_MULTICAST_PORT);

//发送组播搜索路由器
char buf[1024] = { 0 };
snprintf(buf, sizeof(buf), "M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 6\r\nST: %s\r\n\r\n", UPNP_SERVICE_WANPPP);
sendto(sock, buf, strlen(buf), 0, (sockaddr*)&addr, sizeof(addr);

memset(buf, 0, 1024);
snprintf(buf, sizeof(buf), "M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 6\r\nST: %s\r\n\r\n", UPNP_SERVICE_WANIP);
sendto(sock, buf, strlen(buf), 0, (sockaddr*)&addr, sizeof(addr))

//还要循环发给各级网关,第二层nat打洞只需要往该层网关发就行
for (auto it = gatewaySet.begin(); it != gatewaySet.end(); ++it) {
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(it->c_str());
addr.sin_port = htons(UPNP_MULTICAST_PORT);

snprintf(buf, sizeof(buf), "M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 6\r\nST: %s\r\n\r\n", UPNP_SERVICE_WANPPP);
sendto(sock, buf, strlen(buf), 0, (sockaddr*)&addr, sizeof(addr));

memset(buf, 0, 1024);
snprintf(buf, sizeof(buf), "M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 6\r\nST: %s\r\n\r\n", UPNP_SERVICE_WANIP);
sendto(sock, buf, strlen(buf), 0, (sockaddr*)&addr, sizeof(addr))
}

//接收路由器的响应
char buf[4096] = { 0 };
socklen_t addrLen = sizeof(sockaddr_in);
recvfrom(sock, buf, sizeof(buf) - 1, 0, (sockaddr*)&addr, &addrLen);

UPNP解析流程如下:

  • GetExternalIPAddress,获取externalIp,作为下次映射的localIp
  • GetGenericPortMappingEntry,从0开始遍历,获取已映射的端口,避免端口冲突
  • CheckPortMapping,检查需要映射的端口是否已映射,避免重复映射
  • DelPortMapping,如果端口已映射,删除旧映射
  • AddPortMapping,添加新映射
  • CheckPortMapping,检查映射是否成功

具体过程可以参考日志:

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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
2025-07-28 09:52:06.822 local ip is 192.168.1.4,rawNat = 3
2025-07-28 09:52:06.822 insert gateway: 192.168.1.1
2025-07-28 09:52:06.822 DoUpnp begin port = 1872,m_localIp = 192.168.1.4,gateway = 192.168.1.1
2025-07-28 09:52:06.825 DoUpnp_891 recvfrom 192.168.1.1 = HTTP/1.1 200 OK
Server: Custom/1.0 UPnP/1.0 Proc/Ver
EXT:
Location: http://192.168.1.1:5431/dyndev/uuid:4f8c8842-6733-4e8b-806f-c1dfa99deb5a
Cache-Control:max-age=1800
ST:urn:schemas-upnp-org:service:WANPPPConnection:1
USN:uuid:b90ebdfa-439c-4411-9c20-79a88e224a26::urn:schemas-upnp-org:service:WANPPPConnection:1

2025-07-28 09:52:06.826 SimpleHttp send->192.168.1.1:5431 = GET /dyndev/uuid:4f8c8842-6733-4e8b-806f-c1dfa99deb5a HTTP/1.1
Host: 192.168.1.1:5431

2025-07-28 09:52:06.831 SimpleHttp response = HTTP/1.0 200 OK
SERVER: LINUX/2.4 UPnP/1.0 BRCM400/1.0
DATE: Mon, 28 Jul 2025 17:52:06 GMT
CONTENT-TYPE: application/octet-stream
Cache-Control: max-age=1
PRAGMA: no-cache
Connection: Close

2025-07-28 09:52:06.837 GetExternalIpAddress send POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 275
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetExternalIPAddress"

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
</u:GetExternalIPAddress>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.838 SimpleHttp send->192.168.1.1:5431 = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 275
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetExternalIPAddress"

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
</u:GetExternalIPAddress>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.843 SimpleHttp response = HTTP/1.1 200 OK
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 361
Content-Type: text/xml; charset="utf-8"
EXT:

2025-07-28 09:52:06.844 GetExternalIpAddress recv = HTTP/1.1 200 OK
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 361
Content-Type: text/xml; charset="utf-8"
EXT:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><m:GetExternalIPAddressResponse xmlns:m="urn:schemas-upnp-org:service:WANPPPConnection:1"><NewExternalIPAddress>100.81.111.102</NewExternalIPAddress></m:GetExternalIPAddressResponse></s:Body></s:Envelope>
2025-07-28 09:52:06.844 router ip = 192.168.1.1,external ip = 100.81.111.102
2025-07-28 09:52:06.845 GetGenericPortMappingEntry send = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 356
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetGenericPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewPortMappingIndex>0</NewPortMappingIndex>
</u:GetGenericPortMappingEntry>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.846 SimpleHttp send->192.168.1.1:5431 = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 356
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetGenericPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewPortMappingIndex>0</NewPortMappingIndex>
</u:GetGenericPortMappingEntry>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.851 SimpleHttp response = HTTP/1.1 200 OK
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 602
Content-Type: text/xml; charset="utf-8"
EXT:

2025-07-28 09:52:06.852 GetGenericPortMappingEntry recv = HTTP/1.1 200 OK
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 602
Content-Type: text/xml; charset="utf-8"
EXT:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><m:GetGenericPortMappingEntryResponse xmlns:m="urn:schemas-upnp-org:service:WANPPPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>0</NewExternalPort><NewProtocol></NewProtocol><NewInternalPort>0</NewInternalPort><NewInternalClient></NewInternalClient><NewEnabled>0</NewEnabled><NewPortMappingDescription></NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></m:GetGenericPortMappingEntryResponse></s:Body></s:Envelope>
2025-07-28 09:52:06.852 GetGenericPortMappingEntry send = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 356
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetGenericPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewPortMappingIndex>1</NewPortMappingIndex>
</u:GetGenericPortMappingEntry>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.853 SimpleHttp send->192.168.1.1:5431 = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 356
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetGenericPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewPortMappingIndex>1</NewPortMappingIndex>
</u:GetGenericPortMappingEntry>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.857 SimpleHttp response = HTTP/1.1 200 OK
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 631
Content-Type: text/xml; charset="utf-8"
EXT:

2025-07-28 09:52:06.857 GetGenericPortMappingEntry recv = HTTP/1.1 200 OK
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 631
Content-Type: text/xml; charset="utf-8"
EXT:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><m:GetGenericPortMappingEntryResponse xmlns:m="urn:schemas-upnp-org:service:WANPPPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>3</NewExternalPort><NewProtocol>TCP</NewProtocol><NewInternalPort>7851</NewInternalPort><NewInternalClient>192.168.1.4</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>KCG-UPNP</NewPortMappingDescription><NewLeaseDuration>16905</NewLeaseDuration></m:GetGenericPortMappingEntryResponse></s:Body></s:Envelope>
2025-07-28 09:52:06.858 GetGenericPortMappingEntry send = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 356
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetGenericPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewPortMappingIndex>2</NewPortMappingIndex>
</u:GetGenericPortMappingEntry>
</s:Body>
</s:Envelope>

... // 一直遍历,直到获取到第一个未映射的端口

2025-07-28 09:52:06.958 GetGenericPortMappingEntry send = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 356
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetGenericPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewPortMappingIndex>6</NewPortMappingIndex>
</u:GetGenericPortMappingEntry>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.959 SimpleHttp send->192.168.1.1:5431 = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 356
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetGenericPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewPortMappingIndex>6</NewPortMappingIndex>
</u:GetGenericPortMappingEntry>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.961 SimpleHttp response = HTTP/1.1 200 OK
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 632
Content-Type: text/xml; charset="utf-8"
EXT:

2025-07-28 09:52:06.962 GetGenericPortMappingEntry recv = HTTP/1.1 200 OK
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 632
Content-Type: text/xml; charset="utf-8"
EXT:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><m:GetGenericPortMappingEntryResponse xmlns:m="urn:schemas-upnp-org:service:WANPPPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>1</NewExternalPort><NewProtocol>TCP</NewProtocol><NewInternalPort>44079</NewInternalPort><NewInternalClient>192.168.1.4</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>KCG-UPNP</NewPortMappingDescription><NewLeaseDuration>38325</NewLeaseDuration></m:GetGenericPortMappingEntryResponse></s:Body></s:Envelope>
2025-07-28 09:52:06.962 GetGenericPortMappingEntry send = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 356
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetGenericPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewPortMappingIndex>7</NewPortMappingIndex>
</u:GetGenericPortMappingEntry>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.964 SimpleHttp send->192.168.1.1:5431 = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 356
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetGenericPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewPortMappingIndex>7</NewPortMappingIndex>
</u:GetGenericPortMappingEntry>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.971 SimpleHttp response = HTTP/1.1 500 Internal Server Error
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 474
Content-Type: text/xml; charset="utf-8"
EXT:

2025-07-28 09:52:06.972 GetGenericPortMappingEntry recv = HTTP/1.1 500 Internal Server Error
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 474
Content-Type: text/xml; charset="utf-8"
EXT:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring>UPnPError</faultstring>
<detail>
<UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
<errorCode>713</errorCode>
<errorDescription>SpecifiedArrayIndexInvalid</errorDescription>
</UPnPError>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
2025-07-28 09:52:06.972 CheckPortMapping send = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 418
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetSpecificPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetSpecificPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewRemoteHost></NewRemoteHost>
<NewExternalPort>1872</NewExternalPort>
<NewProtocol>TCP</NewProtocol>
</u:GetSpecificPortMappingEntry>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.973 SimpleHttp send->192.168.1.1:5431 = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 418
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#GetSpecificPortMappingEntry"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetSpecificPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewRemoteHost></NewRemoteHost>
<NewExternalPort>1872</NewExternalPort>
<NewProtocol>TCP</NewProtocol>
</u:GetSpecificPortMappingEntry>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.977 SimpleHttp response = HTTP/1.1 500 Internal Server Error
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 466
Content-Type: text/xml; charset="utf-8"
EXT:

2025-07-28 09:52:06.978 CheckPortMapping recv = HTTP/1.1 500 Internal Server Error
DATE: Mon, 28 Jul 2025 17:52:06 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 466
Content-Type: text/xml; charset="utf-8"
EXT:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring>UPnPError</faultstring>
<detail>
<UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
<errorCode>714</errorCode>
<errorDescription>NoSuchEntryInArray</errorDescription>
</UPnPError>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
2025-07-28 09:52:06.978 AddPortMapping send = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 635
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#AddPortMapping"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewRemoteHost></NewRemoteHost>
<NewExternalPort>1872</NewExternalPort>
<NewProtocol>TCP</NewProtocol>
<NewInternalPort>1872</NewInternalPort>
<NewInternalClient>192.168.1.4</NewInternalClient>
<NewEnabled>1</NewEnabled>
<NewPortMappingDescription>yftest_1.0.36_1753696326</NewPortMappingDescription>
<NewLeaseDuration>60</NewLeaseDuration>
</u:AddPortMapping>
</s:Body>
</s:Envelope>

2025-07-28 09:52:06.979 SimpleHttp send->192.168.1.1:5431 = POST /uuid:b90ebdfa-439c-4411-9c20-79a88e224a26/WANPPPConnection:1 HTTP/1.1
HOST: 192.168.1.1:5431
Content-Length: 635
CONTENT-TYPE: text/xml;charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANPPPConnection:1#AddPortMapping"

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">
<NewRemoteHost></NewRemoteHost>
<NewExternalPort>1872</NewExternalPort>
<NewProtocol>TCP</NewProtocol>
<NewInternalPort>1872</NewInternalPort>
<NewInternalClient>192.168.1.4</NewInternalClient>
<NewEnabled>1</NewEnabled>
<NewPortMappingDescription>yftest_1.0.36_1753696326</NewPortMappingDescription>
<NewLeaseDuration>60</NewLeaseDuration>
</u:AddPortMapping>
</s:Body>
</s:Envelope>

2025-07-28 09:52:07.215 SimpleHttp response = HTTP/1.1 200 OK
DATE: Mon, 28 Jul 2025 17:52:07 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 290
Content-Type: text/xml; charset="utf-8"
EXT:

2025-07-28 09:52:07.216 AddPortMapping recv = HTTP/1.1 200 OK
DATE: Mon, 28 Jul 2025 17:52:07 GMT
Connection: Keep-Alive
Server: LINUX/2.4 UPnP/1.0 BRCM400/1.0
Content-Length: 290
Content-Type: text/xml; charset="utf-8"
EXT:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><m:AddPortMappingResponse xmlns:m="urn:schemas-upnp-org:service:WANPPPConnection:1"></m:AddPortMappingResponse></s:Body></s:Envelope>
2025-07-28 09:52:07.216 DoUpnp end result = 0,taketime = 394 ms

WANIPv6FirewallControl

参考:http://upnp.org/specs/gw/UPnP-gw-WANIPv6FirewallControl-v1-Service.pdf
具体流程如下:

  1. AddPinhole()
  2. UpdatePinhole()
  3. DeletePinhole()
  4. GetOutboundPinholeTimeout()
  5. GetFirewallStatus()
  6. GetPinholePackets()
  7. CheckPinholeWorking()

在IPv4中,NAT(网络地址转换)是缓解IPv4地址短缺的流行服务。例如,NAT允许私有网络上的多台主机使用单个公共IP地址访问Internet。IPv6的设计部分是为了纠正IPv4中的某些缺陷。特别是,IPv6地址空间比IPv4地址空间大得多,因此希望不存在IPv6地址短缺的风险。
因此,IPv6不再需要NAT,恢复了端到端的通信范式。然而,使用IPv4 NAT的副作用之一是许多供应商和用户认为NAT通过隐藏设备的IP地址来为设备提供一些基本的安全性,从而保护设备免受外部攻击。
许多专家对NAT的假定安全声明提出异议。然而,许多供应商和用户可能希望防火墙服务保护内部主机免受外部访问,以便内部设备继续具有抵御外部攻击的基本安全性。
在IPv6防火墙很常见的可能场景中,如果有一种类似于NAT穿越的方式来动态控制防火墙,那么对某些应用程序将是有益的。在这种情况下,IPv6 IGD供应商可以实施WANIPv6FirewallControl服务(而不是 WANIPConnection 服务)以允许UPnP控制点控制IGD的防火墙。
对于使用以前的WANIPConnection服务 UPnP CP,使用WANIPv6FirewallControl服务所需的返工量有望减少,因为这两种服务非常相似。