1 | /* |
---|
2 | * This file Copyright (C) 2007-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU GPL versions 2 or 3 |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: upnp.c 14565 2015-10-04 04:53:18Z mikedld $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include <assert.h> |
---|
11 | #include <errno.h> |
---|
12 | |
---|
13 | #ifdef SYSTEM_MINIUPNP |
---|
14 | #include <miniupnpc/miniupnpc.h> |
---|
15 | #include <miniupnpc/upnpcommands.h> |
---|
16 | #else |
---|
17 | #include <miniupnp/miniupnpc.h> |
---|
18 | #include <miniupnp/upnpcommands.h> |
---|
19 | #endif |
---|
20 | |
---|
21 | #include "transmission.h" |
---|
22 | #include "log.h" |
---|
23 | #include "port-forwarding.h" |
---|
24 | #include "session.h" |
---|
25 | #include "upnp.h" |
---|
26 | #include "utils.h" |
---|
27 | |
---|
28 | static const char * |
---|
29 | getKey (void) { return _("Port Forwarding (UPnP)"); } |
---|
30 | |
---|
31 | typedef enum |
---|
32 | { |
---|
33 | TR_UPNP_IDLE, |
---|
34 | TR_UPNP_ERR, |
---|
35 | TR_UPNP_DISCOVER, |
---|
36 | TR_UPNP_MAP, |
---|
37 | TR_UPNP_UNMAP |
---|
38 | } |
---|
39 | tr_upnp_state; |
---|
40 | |
---|
41 | struct tr_upnp |
---|
42 | { |
---|
43 | bool hasDiscovered; |
---|
44 | struct UPNPUrls urls; |
---|
45 | struct IGDdatas data; |
---|
46 | int port; |
---|
47 | char lanaddr[16]; |
---|
48 | unsigned int isMapped; |
---|
49 | tr_upnp_state state; |
---|
50 | }; |
---|
51 | |
---|
52 | /** |
---|
53 | *** |
---|
54 | **/ |
---|
55 | |
---|
56 | tr_upnp* |
---|
57 | tr_upnpInit (void) |
---|
58 | { |
---|
59 | tr_upnp * ret = tr_new0 (tr_upnp, 1); |
---|
60 | |
---|
61 | ret->state = TR_UPNP_DISCOVER; |
---|
62 | ret->port = -1; |
---|
63 | return ret; |
---|
64 | } |
---|
65 | |
---|
66 | void |
---|
67 | tr_upnpClose (tr_upnp * handle) |
---|
68 | { |
---|
69 | assert (!handle->isMapped); |
---|
70 | assert ((handle->state == TR_UPNP_IDLE) |
---|
71 | || (handle->state == TR_UPNP_ERR) |
---|
72 | || (handle->state == TR_UPNP_DISCOVER)); |
---|
73 | |
---|
74 | if (handle->hasDiscovered) |
---|
75 | FreeUPNPUrls (&handle->urls); |
---|
76 | tr_free (handle); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | *** Wrappers for miniupnpc functions |
---|
81 | **/ |
---|
82 | |
---|
83 | static struct UPNPDev * |
---|
84 | tr_upnpDiscover (int msec) |
---|
85 | { |
---|
86 | struct UPNPDev * ret; |
---|
87 | bool have_err; |
---|
88 | |
---|
89 | #if (MINIUPNPC_API_VERSION >= 8) /* adds ipv6 and error args */ |
---|
90 | int err = UPNPDISCOVER_SUCCESS; |
---|
91 | #if (MINIUPNPC_API_VERSION >= 14) /* adds ttl */ |
---|
92 | ret = upnpDiscover (msec, NULL, NULL, 0, 0, 2, &err); |
---|
93 | #else |
---|
94 | ret = upnpDiscover (msec, NULL, NULL, 0, 0, &err); |
---|
95 | #endif |
---|
96 | have_err = err != UPNPDISCOVER_SUCCESS; |
---|
97 | #else |
---|
98 | ret = upnpDiscover (msec, NULL, NULL, 0); |
---|
99 | have_err = ret == NULL; |
---|
100 | #endif |
---|
101 | |
---|
102 | if (have_err) |
---|
103 | tr_logAddNamedDbg (getKey (), "upnpDiscover failed (errno %d - %s)", errno, tr_strerror (errno)); |
---|
104 | |
---|
105 | return ret; |
---|
106 | } |
---|
107 | |
---|
108 | static int |
---|
109 | tr_upnpGetSpecificPortMappingEntry (tr_upnp * handle, const char * proto) |
---|
110 | { |
---|
111 | int err; |
---|
112 | char intClient[16]; |
---|
113 | char intPort[16]; |
---|
114 | char portStr[16]; |
---|
115 | |
---|
116 | *intClient = '\0'; |
---|
117 | *intPort = '\0'; |
---|
118 | |
---|
119 | tr_snprintf (portStr, sizeof(portStr), "%d", (int)handle->port); |
---|
120 | |
---|
121 | #if (MINIUPNPC_API_VERSION >= 10) /* adds remoteHost arg */ |
---|
122 | |
---|
123 | err = UPNP_GetSpecificPortMappingEntry (handle->urls.controlURL, |
---|
124 | handle->data.first.servicetype, |
---|
125 | portStr, |
---|
126 | proto, |
---|
127 | NULL /*remoteHost*/, |
---|
128 | intClient, |
---|
129 | intPort, |
---|
130 | NULL /*desc*/, |
---|
131 | NULL /*enabled*/, |
---|
132 | NULL /*duration*/); |
---|
133 | |
---|
134 | #elif (MINIUPNPC_API_VERSION >= 8) /* adds desc, enabled and leaseDuration args */ |
---|
135 | |
---|
136 | err = UPNP_GetSpecificPortMappingEntry (handle->urls.controlURL, |
---|
137 | handle->data.first.servicetype, |
---|
138 | portStr, |
---|
139 | proto, |
---|
140 | intClient, |
---|
141 | intPort, |
---|
142 | NULL /*desc*/, |
---|
143 | NULL /*enabled*/, |
---|
144 | NULL /*duration*/); |
---|
145 | |
---|
146 | #else |
---|
147 | |
---|
148 | err = UPNP_GetSpecificPortMappingEntry (handle->urls.controlURL, |
---|
149 | handle->data.first.servicetype, |
---|
150 | portStr, |
---|
151 | proto, |
---|
152 | intClient, |
---|
153 | intPort); |
---|
154 | |
---|
155 | #endif |
---|
156 | |
---|
157 | return err; |
---|
158 | } |
---|
159 | |
---|
160 | static int |
---|
161 | tr_upnpAddPortMapping (const tr_upnp * handle, const char * proto, tr_port port, const char * desc) |
---|
162 | { |
---|
163 | int err; |
---|
164 | const int old_errno = errno; |
---|
165 | char portStr[16]; |
---|
166 | errno = 0; |
---|
167 | |
---|
168 | tr_snprintf (portStr, sizeof (portStr), "%d", (int)port); |
---|
169 | |
---|
170 | #if (MINIUPNPC_API_VERSION >= 8) |
---|
171 | err = UPNP_AddPortMapping (handle->urls.controlURL, handle->data.first.servicetype, portStr, portStr, handle->lanaddr, desc, proto, NULL, NULL); |
---|
172 | #else |
---|
173 | err = UPNP_AddPortMapping (handle->urls.controlURL, handle->data.first.servicetype, portStr, portStr, handle->lanaddr, desc, proto, NULL); |
---|
174 | #endif |
---|
175 | |
---|
176 | if (err) |
---|
177 | tr_logAddNamedDbg (getKey (), "%s Port forwarding failed with error %d (errno %d - %s)", proto, err, errno, tr_strerror (errno)); |
---|
178 | |
---|
179 | errno = old_errno; |
---|
180 | return err; |
---|
181 | } |
---|
182 | |
---|
183 | static void |
---|
184 | tr_upnpDeletePortMapping (const tr_upnp * handle, const char * proto, tr_port port) |
---|
185 | { |
---|
186 | char portStr[16]; |
---|
187 | |
---|
188 | tr_snprintf (portStr, sizeof (portStr), "%d", (int)port); |
---|
189 | |
---|
190 | UPNP_DeletePortMapping (handle->urls.controlURL, |
---|
191 | handle->data.first.servicetype, |
---|
192 | portStr, proto, NULL); |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | *** |
---|
197 | **/ |
---|
198 | |
---|
199 | enum |
---|
200 | { |
---|
201 | UPNP_IGD_NONE = 0, |
---|
202 | UPNP_IGD_VALID_CONNECTED = 1, |
---|
203 | UPNP_IGD_VALID_NOT_CONNECTED = 2, |
---|
204 | UPNP_IGD_INVALID = 3 |
---|
205 | }; |
---|
206 | |
---|
207 | int |
---|
208 | tr_upnpPulse (tr_upnp * handle, |
---|
209 | int port, |
---|
210 | int isEnabled, |
---|
211 | int doPortCheck) |
---|
212 | { |
---|
213 | int ret; |
---|
214 | |
---|
215 | if (isEnabled && (handle->state == TR_UPNP_DISCOVER)) |
---|
216 | { |
---|
217 | struct UPNPDev * devlist; |
---|
218 | |
---|
219 | devlist = tr_upnpDiscover (2000); |
---|
220 | |
---|
221 | errno = 0; |
---|
222 | if (UPNP_GetValidIGD (devlist, &handle->urls, &handle->data, |
---|
223 | handle->lanaddr, sizeof (handle->lanaddr)) == UPNP_IGD_VALID_CONNECTED) |
---|
224 | { |
---|
225 | tr_logAddNamedInfo (getKey (), _( |
---|
226 | "Found Internet Gateway Device \"%s\""), |
---|
227 | handle->urls.controlURL); |
---|
228 | tr_logAddNamedInfo (getKey (), _( |
---|
229 | "Local Address is \"%s\""), handle->lanaddr); |
---|
230 | handle->state = TR_UPNP_IDLE; |
---|
231 | handle->hasDiscovered = true; |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | handle->state = TR_UPNP_ERR; |
---|
236 | tr_logAddNamedDbg ( |
---|
237 | getKey (), "UPNP_GetValidIGD failed (errno %d - %s)", |
---|
238 | errno, |
---|
239 | tr_strerror (errno)); |
---|
240 | tr_logAddNamedDbg ( |
---|
241 | getKey (), |
---|
242 | "If your router supports UPnP, please make sure UPnP is enabled!"); |
---|
243 | } |
---|
244 | freeUPNPDevlist (devlist); |
---|
245 | } |
---|
246 | |
---|
247 | if (handle->state == TR_UPNP_IDLE) |
---|
248 | { |
---|
249 | if (handle->isMapped && (!isEnabled || (handle->port != port))) |
---|
250 | handle->state = TR_UPNP_UNMAP; |
---|
251 | } |
---|
252 | |
---|
253 | if (isEnabled && handle->isMapped && doPortCheck) |
---|
254 | { |
---|
255 | if ((tr_upnpGetSpecificPortMappingEntry (handle, "TCP") != UPNPCOMMAND_SUCCESS) || |
---|
256 | (tr_upnpGetSpecificPortMappingEntry (handle, "UDP") != UPNPCOMMAND_SUCCESS)) |
---|
257 | { |
---|
258 | tr_logAddNamedInfo (getKey (), _("Port %d isn't forwarded"), handle->port); |
---|
259 | handle->isMapped = false; |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | if (handle->state == TR_UPNP_UNMAP) |
---|
264 | { |
---|
265 | tr_upnpDeletePortMapping (handle, "TCP", handle->port); |
---|
266 | tr_upnpDeletePortMapping (handle, "UDP", handle->port); |
---|
267 | |
---|
268 | tr_logAddNamedInfo (getKey (), |
---|
269 | _("Stopping port forwarding through \"%s\", service \"%s\""), |
---|
270 | handle->urls.controlURL, handle->data.first.servicetype); |
---|
271 | |
---|
272 | handle->isMapped = 0; |
---|
273 | handle->state = TR_UPNP_IDLE; |
---|
274 | handle->port = -1; |
---|
275 | } |
---|
276 | |
---|
277 | if (handle->state == TR_UPNP_IDLE) |
---|
278 | { |
---|
279 | if (isEnabled && !handle->isMapped) |
---|
280 | handle->state = TR_UPNP_MAP; |
---|
281 | } |
---|
282 | |
---|
283 | if (handle->state == TR_UPNP_MAP) |
---|
284 | { |
---|
285 | int err_tcp = -1; |
---|
286 | int err_udp = -1; |
---|
287 | errno = 0; |
---|
288 | |
---|
289 | if (!handle->urls.controlURL) |
---|
290 | handle->isMapped = 0; |
---|
291 | else |
---|
292 | { |
---|
293 | char desc[64]; |
---|
294 | tr_snprintf (desc, sizeof (desc), "%s at %d", TR_NAME, port); |
---|
295 | |
---|
296 | err_tcp = tr_upnpAddPortMapping (handle, "TCP", port, desc); |
---|
297 | err_udp = tr_upnpAddPortMapping (handle, "UDP", port, desc); |
---|
298 | |
---|
299 | handle->isMapped = !err_tcp | !err_udp; |
---|
300 | } |
---|
301 | tr_logAddNamedInfo (getKey (), |
---|
302 | _("Port forwarding through \"%s\", service \"%s\". (local address: %s:%d)"), |
---|
303 | handle->urls.controlURL, handle->data.first.servicetype, |
---|
304 | handle->lanaddr, port); |
---|
305 | if (handle->isMapped) |
---|
306 | { |
---|
307 | tr_logAddNamedInfo (getKey (), "%s", _("Port forwarding successful!")); |
---|
308 | handle->port = port; |
---|
309 | handle->state = TR_UPNP_IDLE; |
---|
310 | } |
---|
311 | else |
---|
312 | { |
---|
313 | tr_logAddNamedDbg (getKey (), "If your router supports UPnP, please make sure UPnP is enabled!"); |
---|
314 | handle->port = -1; |
---|
315 | handle->state = TR_UPNP_ERR; |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | switch (handle->state) |
---|
320 | { |
---|
321 | case TR_UPNP_DISCOVER: |
---|
322 | ret = TR_PORT_UNMAPPED; break; |
---|
323 | |
---|
324 | case TR_UPNP_MAP: |
---|
325 | ret = TR_PORT_MAPPING; break; |
---|
326 | |
---|
327 | case TR_UPNP_UNMAP: |
---|
328 | ret = TR_PORT_UNMAPPING; break; |
---|
329 | |
---|
330 | case TR_UPNP_IDLE: |
---|
331 | ret = handle->isMapped ? TR_PORT_MAPPED |
---|
332 | : TR_PORT_UNMAPPED; break; |
---|
333 | |
---|
334 | default: |
---|
335 | ret = TR_PORT_ERROR; break; |
---|
336 | } |
---|
337 | |
---|
338 | return ret; |
---|
339 | } |
---|
340 | |
---|