Fix compiler warnings.
[oweals/tinc.git] / src / upnp.c
1 /*
2     upnp.c -- UPnP-IGD client
3     Copyright (C) 2015 Guus Sliepen <guus@tinc-vpn.org>,
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "upnp.h"
21
22 #include <pthread.h>
23
24 #include "miniupnpc/miniupnpc.h"
25 #include "miniupnpc/upnpcommands.h"
26 #include "miniupnpc/upnperrors.h"
27
28 #include "system.h"
29 #include "logger.h"
30 #include "names.h"
31 #include "net.h"
32 #include "netutl.h"
33 #include "utils.h"
34
35 static bool upnp_tcp;
36 static bool upnp_udp;
37 static int upnp_discover_wait = 5;
38 static int upnp_refresh_period = 60;
39
40 // Unfortunately, libminiupnpc devs don't seem to care about API compatibility,
41 // and there are slight changes to function signatures between library versions.
42 // Well, at least they publish a "MINIUPNPC_API_VERSION" constant, so we got that going for us, which is nice.
43 // Differences between API versions are documented in "apiversions.txt" in the libminiupnpc distribution.
44
45 #ifndef MINIUPNPC_API_VERSION
46 #define MINIUPNPC_API_VERSION 0
47 #endif
48
49 static struct UPNPDev *upnp_discover(int delay, int *error) {
50 #if MINIUPNPC_API_VERSION <= 13
51
52 #if MINIUPNPC_API_VERSION < 8
53 #warning "The version of libminiupnpc you're building against seems to be too old. Expect trouble."
54 #endif
55
56         return upnpDiscover(delay, NULL, NULL, false, false, error);
57
58 #elif MINIUPNPC_API_VERSION <= 14
59
60         return upnpDiscover(delay, NULL, NULL, false, false, 2, error);
61
62 #else
63
64 #if MINIUPNPC_API_VERSION > 17
65 #warning "The version of libminiupnpc you're building against seems to be too recent. Expect trouble."
66 #endif
67
68         return upnpDiscover(delay, NULL, NULL, UPNP_LOCAL_PORT_ANY, false, 2, error);
69
70 #endif
71 }
72
73 static void upnp_add_mapping(struct UPNPUrls *urls, struct IGDdatas *data, const char *myaddr, int socket, const char *proto) {
74         // Extract the port from the listening socket.
75         // Note that we can't simply use listen_socket[].sa because this won't have the port
76         // if we're running with Port=0 (dynamically assigned port).
77         sockaddr_t sa;
78         socklen_t salen = sizeof(sa);
79
80         if(getsockname(socket, &sa.sa, &salen)) {
81                 logger(DEBUG_PROTOCOL, LOG_ERR, "[upnp] Unable to get socket address: [%d] %s", sockerrno, sockstrerror(sockerrno));
82                 return;
83         }
84
85         char *port;
86         sockaddr2str(&sa, NULL, &port);
87
88         if(!port) {
89                 logger(DEBUG_PROTOCOL, LOG_ERR, "[upnp] Unable to get socket port");
90                 return;
91         }
92
93         // Use a lease twice as long as the refresh period so that the mapping won't expire before we refresh.
94         char lease_duration[16];
95         snprintf(lease_duration, sizeof(lease_duration), "%d", upnp_refresh_period * 2);
96
97         int error = UPNP_AddPortMapping(urls->controlURL, data->first.servicetype, port, port, myaddr, identname, proto, NULL, lease_duration);
98
99         if(error == 0) {
100                 logger(DEBUG_PROTOCOL, LOG_INFO, "[upnp] Successfully set port mapping (%s:%s %s for %s seconds)", myaddr, port, proto, lease_duration);
101         } else {
102                 logger(DEBUG_PROTOCOL, LOG_ERR, "[upnp] Failed to set port mapping (%s:%s %s for %s seconds): [%d] %s", myaddr, port, proto, lease_duration, error, strupnperror(error));
103         }
104
105         free(port);
106 }
107
108 static void upnp_refresh() {
109         logger(DEBUG_PROTOCOL, LOG_INFO, "[upnp] Discovering IGD devices");
110
111         int error;
112         struct UPNPDev *devices = upnp_discover(upnp_discover_wait * 1000, &error);
113
114         if(!devices) {
115                 logger(DEBUG_PROTOCOL, LOG_WARNING, "[upnp] Unable to find IGD devices: [%d] %s", error, strupnperror(error));
116                 freeUPNPDevlist(devices);
117                 return;
118         }
119
120         struct UPNPUrls urls;
121
122         struct IGDdatas data;
123
124         char myaddr[64];
125
126         int result = UPNP_GetValidIGD(devices, &urls, &data, myaddr, sizeof(myaddr));
127
128         if(result <= 0) {
129                 logger(DEBUG_PROTOCOL, LOG_WARNING, "[upnp] No IGD found");
130                 freeUPNPDevlist(devices);
131                 return;
132         }
133
134         logger(DEBUG_PROTOCOL, LOG_INFO, "[upnp] IGD found: [%d] %s (local address: %s, service type: %s)", result, urls.controlURL, myaddr, data.first.servicetype);
135
136         for(int i = 0; i < listen_sockets; i++) {
137                 if(upnp_tcp) {
138                         upnp_add_mapping(&urls, &data, myaddr, listen_socket[i].tcp.fd, "TCP");
139                 }
140
141                 if(upnp_udp) {
142                         upnp_add_mapping(&urls, &data, myaddr, listen_socket[i].udp.fd, "UDP");
143                 }
144         }
145
146         FreeUPNPUrls(&urls);
147         freeUPNPDevlist(devices);
148 }
149
150 static void *upnp_thread(void *data) {
151         while(true) {
152                 time_t start = time(NULL);
153                 upnp_refresh();
154
155                 // Make sure we'll stick to the refresh period no matter how long upnp_refresh() takes.
156                 time_t refresh_time = start + upnp_refresh_period;
157                 time_t now = time(NULL);
158
159                 if(now < refresh_time) {
160                         sleep(refresh_time - now);
161                 }
162         }
163
164         // TODO: we don't have a clean thread shutdown procedure, so we can't remove the mapping.
165         //       this is probably not a concern as long as the UPnP device honors the lease duration,
166         //       but considering how bug-riddled these devices often are, that's a big "if".
167         return NULL;
168 }
169
170 void upnp_init(bool tcp, bool udp) {
171         upnp_tcp = tcp;
172         upnp_udp = udp;
173
174         get_config_int(lookup_config(config_tree, "UPnPDiscoverWait"), &upnp_discover_wait);
175         get_config_int(lookup_config(config_tree, "UPnPRefreshPeriod"), &upnp_refresh_period);
176
177         pthread_t thread;
178         int error = pthread_create(&thread, NULL, upnp_thread, NULL);
179
180         if(error) {
181                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to start UPnP-IGD client thread: [%d] %s", error, strerror(error));
182         }
183 }