nat_policy.c 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
linphone
Copyright (C) 2010-2016 Belledonne Communications SARL

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 19
*/

20
#include "linphone/core.h"
21

22
#include "c-wrapper/c-wrapper.h"
23

24 25 26
// TODO: From coreapi. Remove me later.
#include "private.h"

27 28 29 30 31 32 33 34 35 36 37 38 39
static LinphoneNatPolicy * _linphone_nat_policy_new_with_ref(LinphoneCore *lc, const char *ref) {
	LinphoneNatPolicy *policy = belle_sip_object_new(LinphoneNatPolicy);
	policy->lc = lc;
	policy->ref = belle_sip_strdup(ref);
	return policy;
}

static LinphoneNatPolicy * linphone_nat_policy_new(LinphoneCore *lc) {
	char ref[17] = { 0 };
	belle_sip_random_token(ref, 16);
	return _linphone_nat_policy_new_with_ref(lc, ref);
}

40 41 42
static void linphone_nat_policy_destroy(LinphoneNatPolicy *policy) {
	if (policy->ref) belle_sip_free(policy->ref);
	if (policy->stun_server) belle_sip_free(policy->stun_server);
43
	if (policy->stun_server_username) belle_sip_free(policy->stun_server_username);
44
	if (policy->resolver_results) belle_sip_object_unref(policy->resolver_results);
45
	if (policy->stun_resolver_context) {
46 47
		belle_sip_resolver_context_cancel(policy->stun_resolver_context);
		belle_sip_object_unref(policy->stun_resolver_context);
48
	}
49 50
}

51
bool_t linphone_nat_policy_stun_server_activated(LinphoneNatPolicy *policy) {
52 53 54 55 56 57
	const char *server = linphone_nat_policy_get_stun_server(policy);
	return (server != NULL) && (server[0] != '\0')
		&& ((linphone_nat_policy_stun_enabled(policy) == TRUE) || (linphone_nat_policy_turn_enabled(policy) == TRUE));
}


58 59 60 61 62 63 64

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneNatPolicy);

BELLE_SIP_INSTANCIATE_VPTR(LinphoneNatPolicy, belle_sip_object_t,
	(belle_sip_object_destroy_t)linphone_nat_policy_destroy,
	NULL, // clone
	NULL, // marshal
65
	FALSE
66 67 68 69 70
);


static void _linphone_nat_policy_save_to_config(const LinphoneNatPolicy *policy, LpConfig *config, int index) {
	char *section;
71
	bctbx_list_t *l = NULL;
72 73 74 75

	section = belle_sip_strdup_printf("nat_policy_%i", index);
	lp_config_set_string(config, section, "ref", policy->ref);
	lp_config_set_string(config, section, "stun_server", policy->stun_server);
76
	lp_config_set_string(config, section, "stun_server_username", policy->stun_server_username);
77
	if (linphone_nat_policy_upnp_enabled(policy)) {
78
		l = bctbx_list_append(l, (void *)"upnp");
79
	} else {
80 81 82
		if (linphone_nat_policy_stun_enabled(policy)) l = bctbx_list_append(l, (void *)"stun");
		if (linphone_nat_policy_turn_enabled(policy)) l = bctbx_list_append(l, (void *)"turn");
		if (linphone_nat_policy_ice_enabled(policy)) l = bctbx_list_append(l, (void *)"ice");
83 84 85
	}
	lp_config_set_string_list(config, section, "protocols", l);
	belle_sip_free(section);
86
	bctbx_list_free(l);
87 88
}

89 90
void linphone_nat_policy_save_to_config(const LinphoneNatPolicy *policy) {
	LpConfig *config = policy->lc->config;
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
	char *section;
	int index;
	bool_t finished = FALSE;

	for (index = 0; finished != TRUE; index++) {
		section = belle_sip_strdup_printf("nat_policy_%i", index);
		if (lp_config_has_section(config, section)) {
			const char *config_ref = lp_config_get_string(config, section, "ref", NULL);
			if ((config_ref != NULL) && (strcmp(config_ref, policy->ref) == 0)) {
				_linphone_nat_policy_save_to_config(policy, config, index);
				finished = TRUE;
			}
		} else {
			_linphone_nat_policy_save_to_config(policy, config, index);
			finished = TRUE;
		}
		belle_sip_free(section);
	}
}

LinphoneNatPolicy * linphone_nat_policy_ref(LinphoneNatPolicy *policy) {
	belle_sip_object_ref(policy);
	return policy;
}

void linphone_nat_policy_unref(LinphoneNatPolicy *policy) {
	belle_sip_object_unref(policy);
}

void *linphone_nat_policy_get_user_data(const LinphoneNatPolicy *policy) {
	return policy->user_data;
}

void linphone_nat_policy_set_user_data(LinphoneNatPolicy *policy, void *ud) {
	policy->user_data = ud;
}


void linphone_nat_policy_clear(LinphoneNatPolicy *policy) {
	linphone_nat_policy_enable_stun(policy, FALSE);
	linphone_nat_policy_enable_turn(policy, FALSE);
	linphone_nat_policy_enable_ice(policy, FALSE);
	linphone_nat_policy_enable_upnp(policy, FALSE);
	linphone_nat_policy_set_stun_server(policy, NULL);
135
	linphone_nat_policy_set_stun_server_username(policy, NULL);
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
}

bool_t linphone_nat_policy_stun_enabled(const LinphoneNatPolicy *policy) {
	return policy->stun_enabled;
}

void linphone_nat_policy_enable_stun(LinphoneNatPolicy *policy, bool_t enable) {
	policy->stun_enabled = enable;
}

bool_t linphone_nat_policy_turn_enabled(const LinphoneNatPolicy *policy) {
	return policy->turn_enabled;
}

void linphone_nat_policy_enable_turn(LinphoneNatPolicy *policy, bool_t enable) {
	policy->turn_enabled = enable;
}

bool_t linphone_nat_policy_ice_enabled(const LinphoneNatPolicy *policy) {
	return policy->ice_enabled;
}

void linphone_nat_policy_enable_ice(LinphoneNatPolicy *policy, bool_t enable) {
	policy->ice_enabled = enable;
}

bool_t linphone_nat_policy_upnp_enabled(const LinphoneNatPolicy *policy) {
	return policy->upnp_enabled;
}

void linphone_nat_policy_enable_upnp(LinphoneNatPolicy *policy, bool_t enable) {
	policy->upnp_enabled = enable;
	if (enable) {
169
		ms_warning("uPnP NAT policy is no longer supported");
170 171 172 173 174 175 176 177
	}
}

const char * linphone_nat_policy_get_stun_server(const LinphoneNatPolicy *policy) {
	return policy->stun_server;
}

void linphone_nat_policy_set_stun_server(LinphoneNatPolicy *policy, const char *stun_server) {
178 179 180
	char *new_stun_server = NULL;

	if (stun_server != NULL) new_stun_server = belle_sip_strdup(stun_server);
181 182 183 184
	if (policy->stun_server != NULL) {
		belle_sip_free(policy->stun_server);
		policy->stun_server = NULL;
	}
185 186 187
	if (new_stun_server != NULL) {
		policy->stun_server = new_stun_server;
	}
188 189 190
	if (policy->resolver_results) {
		belle_sip_object_unref(policy->resolver_results);
		policy->resolver_results = NULL;
191 192 193 194 195 196 197 198
	}
	if (policy->stun_resolver_context){
		belle_sip_resolver_context_cancel(policy->stun_resolver_context);
		belle_sip_object_unref(policy->stun_resolver_context);
		policy->stun_resolver_context = NULL;
		
	}
	linphone_nat_policy_resolve_stun_server(policy);
199 200
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
const char * linphone_nat_policy_get_stun_server_username(const LinphoneNatPolicy *policy) {
	return policy->stun_server_username;
}

void linphone_nat_policy_set_stun_server_username(LinphoneNatPolicy *policy, const char *username) {
	char *new_username = NULL;

	if (username != NULL) new_username = belle_sip_strdup(username);
	if (policy->stun_server_username != NULL) {
		belle_sip_free(policy->stun_server_username);
		policy->stun_server_username = NULL;
	}
	if (new_username != NULL) policy->stun_server_username = new_username;
}

216
static void stun_server_resolved(void *data, belle_sip_resolver_results_t *results) {
217
	LinphoneNatPolicy *policy = (LinphoneNatPolicy *)data;
218 219 220 221

	if (policy->resolver_results) {
		belle_sip_object_unref(policy->resolver_results);
		policy->resolver_results = NULL;
222
	}
223 224
	
	if (belle_sip_resolver_results_get_addrinfos(results)) {
225
		ms_message("Stun server resolution successful.");
226 227
		belle_sip_object_ref(results);
		policy->resolver_results = results;
228 229 230
	} else {
		ms_warning("Stun server resolution failed.");
	}
231
	if (policy->stun_resolver_context){
232
		belle_sip_object_unref(policy->stun_resolver_context);
233 234
		policy->stun_resolver_context = NULL;
	}
235 236 237 238 239
}

void linphone_nat_policy_resolve_stun_server(LinphoneNatPolicy *policy) {
	const char *service = NULL;

240
	if (linphone_nat_policy_stun_server_activated(policy) && (policy->lc->sal != NULL) && !policy->stun_resolver_context) {
241
		char host[NI_MAXHOST];
242
		int port = 0;
243 244 245 246
		linphone_parse_host_port(policy->stun_server, host, sizeof(host), &port);
		if (linphone_nat_policy_turn_enabled(policy)) service = "turn";
		else if (linphone_nat_policy_stun_enabled(policy)) service = "stun";
		if (service != NULL) {
247 248
			int family = AF_INET;
			if (linphone_core_ipv6_enabled(policy->lc) == TRUE) family = AF_INET6;
249
			ms_message("Starting stun server resolution [%s]", host);
250 251 252 253 254 255
			if (port == 0) {
				port = 3478;
				policy->stun_resolver_context = policy->lc->sal->resolve(service, "udp", host, port, family, stun_server_resolved, policy);
			} else {
				policy->stun_resolver_context = policy->lc->sal->resolveA(host, port, family, stun_server_resolved, policy);
			}
256
			if (policy->stun_resolver_context) belle_sip_object_ref(policy->stun_resolver_context);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
		}
	}
}

const struct addrinfo * linphone_nat_policy_get_stun_server_addrinfo(LinphoneNatPolicy *policy) {
	/*
	 * It is critical not to block for a long time if it can't be resolved, otherwise this stucks the main thread when making a call.
	 * On the contrary, a fully asynchronous call initiation is complex to develop.
	 * The compromise is then:
	 *  - have a cache of the stun server addrinfo
	 *  - this cached value is returned when it is non-null
	 *  - an asynchronous resolution is asked each time this function is called to ensure frequent refreshes of the cached value.
	 *  - if no cached value exists, block for a short time; this case must be unprobable because the resolution will be asked each
	 *    time the stun server value is changed.
	 */
272
	if (linphone_nat_policy_stun_server_activated(policy) && (policy->resolver_results == NULL)) {
273 274 275
		int wait_ms = 0;
		int wait_limit = 1000;
		linphone_nat_policy_resolve_stun_server(policy);
276
		while ((policy->resolver_results == NULL) && (policy->stun_resolver_context != NULL) && (wait_ms < wait_limit)) {
277
			policy->lc->sal->iterate();
278 279 280 281
			ms_usleep(50000);
			wait_ms += 50;
		}
	}
282
	return policy->resolver_results ? belle_sip_resolver_results_get_addrinfos(policy->resolver_results) : NULL;
283 284 285 286 287 288
}

LinphoneNatPolicy * linphone_core_create_nat_policy(LinphoneCore *lc) {
	return linphone_nat_policy_new(lc);
}

289 290 291 292 293 294 295 296 297 298
LinphoneNatPolicy * linphone_config_create_nat_policy_from_section(const LinphoneConfig *config, const char* section) {
	const char *config_ref = lp_config_get_string(config, section, "ref", NULL);
	const char *server = lp_config_get_string(config, section, "stun_server", NULL);
	const char *username = lp_config_get_string(config, section, "stun_server_username", NULL);
	bctbx_list_t *l = lp_config_get_string_list(config, section, "protocols", NULL);
	LinphoneNatPolicy *policy;
	if (config_ref)
		policy = _linphone_nat_policy_new_with_ref(NULL, config_ref);
	else
		policy = linphone_nat_policy_new(NULL);
299

300 301 302 303 304 305 306 307 308 309 310 311 312
	if (server != NULL) linphone_nat_policy_set_stun_server(policy, server);
	if (username != NULL) linphone_nat_policy_set_stun_server_username(policy, username);
	if (l != NULL) {
		bool_t upnp_enabled = FALSE;
		bctbx_list_t *elem;
		for (elem = l; elem != NULL; elem = elem->next) {
			const char *value = (const char *)elem->data;
			if (strcmp(value, "stun") == 0) linphone_nat_policy_enable_stun(policy, TRUE);
			else if (strcmp(value, "turn") == 0) linphone_nat_policy_enable_turn(policy, TRUE);
			else if (strcmp(value, "ice") == 0) linphone_nat_policy_enable_ice(policy, TRUE);
			else if (strcmp(value, "upnp") == 0) upnp_enabled = TRUE;
		}
		if (upnp_enabled) linphone_nat_policy_enable_upnp(policy, TRUE);
Ghislain MARY's avatar
Ghislain MARY committed
313
		bctbx_list_free_with_data(l, (bctbx_list_free_func)ms_free);
314 315 316
	}
	return policy;
}
317 318 319 320 321 322
LinphoneNatPolicy * linphone_core_create_nat_policy_from_config(LinphoneCore *lc, const char *ref) {
	LpConfig *config = lc->config;
	LinphoneNatPolicy *policy = NULL;
	char *section;
	int index;
	bool_t finished = FALSE;
323

324 325 326 327 328
	for (index = 0; finished != TRUE; index++) {
		section = belle_sip_strdup_printf("nat_policy_%i", index);
		if (lp_config_has_section(config, section)) {
			const char *config_ref = lp_config_get_string(config, section, "ref", NULL);
			if ((config_ref != NULL) && (strcmp(config_ref, ref) == 0)) {
329 330
				policy = linphone_config_create_nat_policy_from_section(config, section);
				policy->lc = lc;
331 332 333
				finished = TRUE;
			}
		} else finished = TRUE;
334
	belle_sip_free(section);
335
	}
336
return policy;
337
}
338 339 340 341

LinphoneCore *linphone_nat_policy_get_core(const LinphoneNatPolicy *policy) {
	return policy->lc;
}