3 Copyright (C) 2012 Belledonne Communications SARL
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #define UPNP_ADD_MAX_RETRY 4
25 #define UPNP_REMOVE_MAX_RETRY 4
26 #define UPNP_SECTION_NAME "uPnP"
27 #define UPNP_CORE_READY_CHECK 1
28 #define UPNP_CORE_RETRY_DELAY 4
29 #define UPNP_CALL_RETRY_DELAY 1
35 typedef struct _UpnpPortBinding {
37 LinphoneUpnpState state;
38 upnp_igd_ip_protocol protocol;
39 char local_addr[LINPHONE_IPADDR_SIZE];
41 char external_addr[LINPHONE_IPADDR_SIZE];
50 typedef struct _UpnpStream {
52 UpnpPortBinding *rtcp;
53 LinphoneUpnpState state;
60 LinphoneUpnpState state;
65 upnp_igd_context *upnp_igd_ctxt;
66 UpnpPortBinding *sip_tcp;
67 UpnpPortBinding *sip_tls;
68 UpnpPortBinding *sip_udp;
69 LinphoneUpnpState state;
70 MSList *removing_configs;
71 MSList *adding_configs;
72 MSList *pending_bindings;
77 time_t last_ready_check;
78 LinphoneUpnpState last_ready_state;
82 bool_t linphone_core_upnp_hook(void *data);
83 void linphone_core_upnp_refresh(UpnpContext *ctx);
85 UpnpPortBinding *linphone_upnp_port_binding_new();
86 UpnpPortBinding *linphone_upnp_port_binding_new_with_parameters(upnp_igd_ip_protocol protocol, int local_port, int external_port);
87 UpnpPortBinding *linphone_upnp_port_binding_new_or_collect(MSList *list, upnp_igd_ip_protocol protocol, int local_port, int external_port);
88 UpnpPortBinding *linphone_upnp_port_binding_copy(const UpnpPortBinding *port);
89 bool_t linphone_upnp_port_binding_equal(const UpnpPortBinding *port1, const UpnpPortBinding *port2);
90 UpnpPortBinding *linphone_upnp_port_binding_equivalent_in_list(MSList *list, const UpnpPortBinding *port);
91 UpnpPortBinding *linphone_upnp_port_binding_retain(UpnpPortBinding *port);
92 void linphone_upnp_update_port_binding(UpnpContext *lupnp, UpnpPortBinding **port_mapping, upnp_igd_ip_protocol protocol, int port, int retry_delay);
93 void linphone_upnp_port_binding_log(int level, const char *msg, const UpnpPortBinding *port);
94 void linphone_upnp_port_binding_release(UpnpPortBinding *port);
95 void linphone_upnp_update_config(UpnpContext *lupnp);
98 MSList *linphone_upnp_config_list_port_bindings(struct _LpConfig *lpc);
99 void linphone_upnp_config_add_port_binding(UpnpContext *lupnp, const UpnpPortBinding *port);
100 void linphone_upnp_config_remove_port_binding(UpnpContext *lupnp, const UpnpPortBinding *port);
103 int linphone_upnp_context_send_remove_port_binding(UpnpContext *lupnp, UpnpPortBinding *port, bool_t retry);
104 int linphone_upnp_context_send_add_port_binding(UpnpContext *lupnp, UpnpPortBinding *port, bool_t retry);
111 /* Convert uPnP IGD logs to ortp logs */
112 void linphone_upnp_igd_print(void *cookie, upnp_igd_print_level level, const char *fmt, va_list list) {
113 int ortp_level = ORTP_DEBUG;
115 case UPNP_IGD_MESSAGE:
116 ortp_level = ORTP_MESSAGE;
118 case UPNP_IGD_WARNING:
119 ortp_level = ORTP_DEBUG; // Too verbose otherwise
122 ortp_level = ORTP_DEBUG; // Too verbose otherwise
127 ortp_logv(ortp_level, fmt, list);
130 void linphone_upnp_igd_callback(void *cookie, upnp_igd_event event, void *arg) {
131 UpnpContext *lupnp = (UpnpContext *)cookie;
132 upnp_igd_port_mapping *mapping = NULL;
133 UpnpPortBinding *port_mapping = NULL;
134 const char *ip_address = NULL;
135 const char *connection_status = NULL;
136 bool_t nat_enabled = FALSE;
137 LinphoneUpnpState old_state;
139 if(lupnp == NULL || lupnp->upnp_igd_ctxt == NULL) {
140 ms_error("uPnP IGD: Invalid context in callback");
144 ms_mutex_lock(&lupnp->mutex);
145 old_state = lupnp->state;
148 case UPNP_IGD_DEVICE_ADDED:
149 case UPNP_IGD_DEVICE_REMOVED:
150 case UPNP_IGD_EXTERNAL_IPADDRESS_CHANGED:
151 case UPNP_IGD_NAT_ENABLED_CHANGED:
152 case UPNP_IGD_CONNECTION_STATUS_CHANGED:
153 ip_address = upnp_igd_get_external_ipaddress(lupnp->upnp_igd_ctxt);
154 connection_status = upnp_igd_get_connection_status(lupnp->upnp_igd_ctxt);
155 nat_enabled = upnp_igd_get_nat_enabled(lupnp->upnp_igd_ctxt);
157 if(ip_address == NULL || connection_status == NULL) {
158 ms_message("uPnP IGD: Pending");
159 lupnp->state = LinphoneUpnpStatePending;
160 } else if(strcasecmp(connection_status, "Connected") || !nat_enabled) {
161 ms_message("uPnP IGD: Not Available");
162 lupnp->state = LinphoneUpnpStateNotAvailable;
164 ms_message("uPnP IGD: Connected");
165 lupnp->state = LinphoneUpnpStateOk;
166 if(old_state != LinphoneUpnpStateOk) {
167 linphone_core_upnp_refresh(lupnp);
173 case UPNP_IGD_PORT_MAPPING_ADD_SUCCESS:
174 mapping = (upnp_igd_port_mapping *) arg;
175 port_mapping = (UpnpPortBinding*) mapping->cookie;
176 port_mapping->external_port = mapping->remote_port;
177 port_mapping->state = LinphoneUpnpStateOk;
178 linphone_upnp_port_binding_log(ORTP_MESSAGE, "Added port binding", port_mapping);
179 linphone_upnp_config_add_port_binding(lupnp, port_mapping);
183 case UPNP_IGD_PORT_MAPPING_ADD_FAILURE:
184 mapping = (upnp_igd_port_mapping *) arg;
185 port_mapping = (UpnpPortBinding*) mapping->cookie;
186 port_mapping->external_port = -1; //Force random external port
187 if(linphone_upnp_context_send_add_port_binding(lupnp, port_mapping, TRUE) != 0) {
188 linphone_upnp_port_binding_log(ORTP_ERROR, "Can't add port binding", port_mapping);
193 case UPNP_IGD_PORT_MAPPING_REMOVE_SUCCESS:
194 mapping = (upnp_igd_port_mapping *) arg;
195 port_mapping = (UpnpPortBinding*) mapping->cookie;
196 port_mapping->state = LinphoneUpnpStateIdle;
197 linphone_upnp_port_binding_log(ORTP_MESSAGE, "Removed port binding", port_mapping);
198 linphone_upnp_config_remove_port_binding(lupnp, port_mapping);
202 case UPNP_IGD_PORT_MAPPING_REMOVE_FAILURE:
203 mapping = (upnp_igd_port_mapping *) arg;
204 port_mapping = (UpnpPortBinding*) mapping->cookie;
205 if(linphone_upnp_context_send_remove_port_binding(lupnp, port_mapping, TRUE) != 0) {
206 linphone_upnp_port_binding_log(ORTP_ERROR, "Can't remove port binding", port_mapping);
207 linphone_upnp_config_remove_port_binding(lupnp, port_mapping);
216 if(port_mapping != NULL) {
218 * Execute delayed actions
220 if(port_mapping->to_remove) {
221 if(port_mapping->state == LinphoneUpnpStateOk) {
222 port_mapping->to_remove = FALSE;
223 linphone_upnp_context_send_remove_port_binding(lupnp, port_mapping, FALSE);
224 } else if(port_mapping->state == LinphoneUpnpStateKo) {
225 port_mapping->to_remove = FALSE;
228 if(port_mapping->to_add) {
229 if(port_mapping->state == LinphoneUpnpStateIdle || port_mapping->state == LinphoneUpnpStateKo) {
230 port_mapping->to_add = FALSE;
231 linphone_upnp_context_send_add_port_binding(lupnp, port_mapping, FALSE);
235 lupnp->pending_bindings = ms_list_remove(lupnp->pending_bindings, port_mapping);
236 linphone_upnp_port_binding_release(port_mapping);
240 * If there is no pending binding emit a signal
242 if(lupnp->pending_bindings == NULL) {
243 pthread_cond_signal(&lupnp->empty_cond);
245 ms_mutex_unlock(&lupnp->mutex);
253 UpnpContext* linphone_upnp_context_new(LinphoneCore *lc) {
254 UpnpContext *lupnp = (UpnpContext *)ms_new0(UpnpContext,1);
256 ms_mutex_init(&lupnp->mutex, NULL);
257 ms_cond_init(&lupnp->empty_cond, NULL);
259 lupnp->last_ready_check = 0;
260 lupnp->last_ready_state = LinphoneUpnpStateIdle;
263 lupnp->pending_bindings = NULL;
264 lupnp->adding_configs = NULL;
265 lupnp->removing_configs = NULL;
266 lupnp->state = LinphoneUpnpStateIdle;
267 ms_message("uPnP IGD: New %p for core %p", lupnp, lc);
270 lupnp->sip_udp = NULL;
271 lupnp->sip_tcp = NULL;
272 lupnp->sip_tls = NULL;
274 linphone_core_add_iterate_hook(lc, linphone_core_upnp_hook, lupnp);
276 lupnp->upnp_igd_ctxt = NULL;
277 lupnp->upnp_igd_ctxt = upnp_igd_create(linphone_upnp_igd_callback, linphone_upnp_igd_print, lupnp);
278 if(lupnp->upnp_igd_ctxt == NULL) {
279 lupnp->state = LinphoneUpnpStateKo;
280 ms_error("Can't create uPnP IGD context");
284 lupnp->state = LinphoneUpnpStatePending;
285 upnp_igd_start(lupnp->upnp_igd_ctxt);
290 void linphone_upnp_context_destroy(UpnpContext *lupnp) {
291 linphone_core_remove_iterate_hook(lupnp->lc, linphone_core_upnp_hook, lupnp);
293 ms_mutex_lock(&lupnp->mutex);
295 /* Send port binding removes */
296 if(lupnp->sip_udp != NULL) {
297 linphone_upnp_context_send_remove_port_binding(lupnp, lupnp->sip_udp, TRUE);
299 if(lupnp->sip_tcp != NULL) {
300 linphone_upnp_context_send_remove_port_binding(lupnp, lupnp->sip_tcp, TRUE);
302 if(lupnp->sip_tls != NULL) {
303 linphone_upnp_context_send_remove_port_binding(lupnp, lupnp->sip_tls, TRUE);
306 /* Wait all pending bindings are done */
307 if(lupnp->pending_bindings != NULL) {
308 ms_message("uPnP IGD: Wait all pending port bindings ...");
309 ms_cond_wait(&lupnp->empty_cond, &lupnp->mutex);
312 if(lupnp->upnp_igd_ctxt != NULL) {
313 upnp_igd_destroy(lupnp->upnp_igd_ctxt);
314 lupnp->upnp_igd_ctxt = NULL;
317 /* Run one more time configuration update */
318 linphone_upnp_update_config(lupnp);
320 /* Release port bindings */
321 if(lupnp->sip_udp != NULL) {
322 linphone_upnp_port_binding_release(lupnp->sip_udp);
323 lupnp->sip_udp = NULL;
325 if(lupnp->sip_tcp != NULL) {
326 linphone_upnp_port_binding_release(lupnp->sip_tcp);
327 lupnp->sip_tcp = NULL;
329 if(lupnp->sip_tls != NULL) {
330 linphone_upnp_port_binding_release(lupnp->sip_tls);
331 lupnp->sip_tcp = NULL;
335 ms_list_for_each(lupnp->adding_configs,(void (*)(void*))linphone_upnp_port_binding_release);
336 lupnp->adding_configs = ms_list_free(lupnp->adding_configs);
337 ms_list_for_each(lupnp->removing_configs,(void (*)(void*))linphone_upnp_port_binding_release);
338 lupnp->removing_configs = ms_list_free(lupnp->removing_configs);
339 ms_list_for_each(lupnp->pending_bindings,(void (*)(void*))linphone_upnp_port_binding_release);
340 lupnp->pending_bindings = ms_list_free(lupnp->pending_bindings);
342 ms_mutex_unlock(&lupnp->mutex);
344 ms_mutex_destroy(&lupnp->mutex);
345 ms_cond_destroy(&lupnp->empty_cond);
347 ms_message("uPnP IGD: destroy %p", lupnp);
351 LinphoneUpnpState linphone_upnp_context_get_state(UpnpContext *lupnp) {
352 LinphoneUpnpState state;
353 ms_mutex_lock(&lupnp->mutex);
354 state = lupnp->state;
355 ms_mutex_unlock(&lupnp->mutex);
359 bool_t _linphone_upnp_context_is_ready_for_register(UpnpContext *lupnp) {
362 // 1 Check global uPnP state
363 ready = (lupnp->state == LinphoneUpnpStateOk);
365 // 2 Check external ip address
367 if (upnp_igd_get_external_ipaddress(lupnp->upnp_igd_ctxt) == NULL) {
372 // 3 Check sip ports bindings
374 if(lupnp->sip_udp != NULL) {
375 if(lupnp->sip_udp->state != LinphoneUpnpStateOk) {
378 } else if(lupnp->sip_tcp != NULL) {
379 if(lupnp->sip_tcp->state != LinphoneUpnpStateOk) {
382 } else if(lupnp->sip_tls != NULL) {
383 if(lupnp->sip_tls->state != LinphoneUpnpStateOk) {
394 bool_t linphone_upnp_context_is_ready_for_register(UpnpContext *lupnp) {
396 ms_mutex_lock(&lupnp->mutex);
397 ready = _linphone_upnp_context_is_ready_for_register(lupnp);
398 ms_mutex_unlock(&lupnp->mutex);
402 int linphone_upnp_context_get_external_port(UpnpContext *lupnp) {
404 ms_mutex_lock(&lupnp->mutex);
406 if(lupnp->sip_udp != NULL) {
407 if(lupnp->sip_udp->state == LinphoneUpnpStateOk) {
408 port = lupnp->sip_udp->external_port;
410 } else if(lupnp->sip_tcp != NULL) {
411 if(lupnp->sip_tcp->state == LinphoneUpnpStateOk) {
412 port = lupnp->sip_tcp->external_port;
414 } else if(lupnp->sip_tls != NULL) {
415 if(lupnp->sip_tls->state == LinphoneUpnpStateOk) {
416 port = lupnp->sip_tls->external_port;
420 ms_mutex_unlock(&lupnp->mutex);
424 const char* linphone_upnp_context_get_external_ipaddress(UpnpContext *lupnp) {
425 const char* addr = NULL;
426 ms_mutex_lock(&lupnp->mutex);
427 addr = upnp_igd_get_external_ipaddress(lupnp->upnp_igd_ctxt);
428 ms_mutex_unlock(&lupnp->mutex);
432 int linphone_upnp_context_send_add_port_binding(UpnpContext *lupnp, UpnpPortBinding *port, bool_t retry) {
433 upnp_igd_port_mapping mapping;
434 char description[128];
437 if(lupnp->state != LinphoneUpnpStateOk) {
441 // Compute port binding state
442 if(port->state != LinphoneUpnpStateAdding) {
443 port->to_remove = FALSE;
444 switch(port->state) {
445 case LinphoneUpnpStateKo:
446 case LinphoneUpnpStateIdle: {
448 port->state = LinphoneUpnpStateAdding;
451 case LinphoneUpnpStateRemoving: {
461 // No retry if specified
462 if(port->retry != 0 && !retry) {
466 if(port->retry >= UPNP_ADD_MAX_RETRY) {
469 mapping.cookie = linphone_upnp_port_binding_retain(port);
470 lupnp->pending_bindings = ms_list_append(lupnp->pending_bindings, mapping.cookie);
472 mapping.local_port = port->local_port;
473 mapping.local_host = port->local_addr;
474 if(port->external_port == -1)
475 mapping.remote_port = rand()%(0xffff - 1024) + 1024;
477 mapping.remote_port = port->external_port;
478 mapping.remote_host = "";
479 snprintf(description, 128, "%s %s at %s:%d",
481 (port->protocol == UPNP_IGD_IP_PROTOCOL_TCP)? "TCP": "UDP",
482 port->local_addr, port->local_port);
483 mapping.description = description;
484 mapping.protocol = port->protocol;
487 linphone_upnp_port_binding_log(ORTP_MESSAGE, "Try to add port binding", port);
488 ret = upnp_igd_add_port_mapping(lupnp->upnp_igd_ctxt, &mapping);
491 port->state = LinphoneUpnpStateKo;
496 int linphone_upnp_context_send_remove_port_binding(UpnpContext *lupnp, UpnpPortBinding *port, bool_t retry) {
497 upnp_igd_port_mapping mapping;
500 if(lupnp->state != LinphoneUpnpStateOk) {
504 // Compute port binding state
505 if(port->state != LinphoneUpnpStateRemoving) {
506 port->to_add = FALSE;
507 switch(port->state) {
508 case LinphoneUpnpStateOk: {
510 port->state = LinphoneUpnpStateRemoving;
513 case LinphoneUpnpStateAdding: {
514 port->to_remove = TRUE;
523 // No retry if specified
524 if(port->retry != 0 && !retry) {
528 if(port->retry >= UPNP_REMOVE_MAX_RETRY) {
531 mapping.cookie = linphone_upnp_port_binding_retain(port);
532 lupnp->pending_bindings = ms_list_append(lupnp->pending_bindings, mapping.cookie);
534 mapping.remote_port = port->external_port;
535 mapping.remote_host = "";
536 mapping.protocol = port->protocol;
538 linphone_upnp_port_binding_log(ORTP_MESSAGE, "Try to remove port binding", port);
539 ret = upnp_igd_delete_port_mapping(lupnp->upnp_igd_ctxt, &mapping);
542 port->state = LinphoneUpnpStateKo;
548 * uPnP Core interfaces
551 int linphone_core_update_upnp_audio_video(LinphoneCall *call, bool_t audio, bool_t video) {
552 LinphoneCore *lc = call->core;
553 UpnpContext *lupnp = lc->upnp;
560 ms_mutex_lock(&lupnp->mutex);
562 // Don't handle when the call
563 if(lupnp->state == LinphoneUpnpStateOk && call->upnp_session != NULL) {
569 linphone_upnp_update_port_binding(lupnp, &call->upnp_session->audio->rtp,
570 UPNP_IGD_IP_PROTOCOL_UDP, (audio)? call->audio_port:0, UPNP_CALL_RETRY_DELAY);
572 linphone_upnp_update_port_binding(lupnp, &call->upnp_session->audio->rtcp,
573 UPNP_IGD_IP_PROTOCOL_UDP, (audio)? call->audio_port+1:0, UPNP_CALL_RETRY_DELAY);
578 linphone_upnp_update_port_binding(lupnp, &call->upnp_session->video->rtp,
579 UPNP_IGD_IP_PROTOCOL_UDP, (video)? call->video_port:0, UPNP_CALL_RETRY_DELAY);
581 linphone_upnp_update_port_binding(lupnp, &call->upnp_session->video->rtcp,
582 UPNP_IGD_IP_PROTOCOL_UDP, (video)? call->video_port+1:0, UPNP_CALL_RETRY_DELAY);
585 ms_mutex_unlock(&lupnp->mutex);
588 * Update uPnP call state
590 linphone_upnp_call_process(call);
597 int linphone_core_update_upnp_from_remote_media_description(LinphoneCall *call, const SalMediaDescription *md) {
598 bool_t audio = FALSE;
599 bool_t video = FALSE;
601 const SalStreamDescription *stream;
603 for (i = 0; i < md->n_total_streams; i++) {
604 stream = &md->streams[i];
605 if(stream->type == SalAudio) {
607 } else if(stream->type == SalVideo) {
612 return linphone_core_update_upnp_audio_video(call, audio, video);
615 int linphone_core_update_upnp(LinphoneCore *lc, LinphoneCall *call) {
616 return linphone_core_update_upnp_audio_video(call, call->audiostream!=NULL, call->videostream!=NULL);
619 void linphone_core_update_upnp_state_in_call_stats(LinphoneCall *call) {
620 call->stats[LINPHONE_CALL_STATS_AUDIO].upnp_state = call->upnp_session->audio->state;
621 call->stats[LINPHONE_CALL_STATS_VIDEO].upnp_state = call->upnp_session->video->state;
624 void linphone_upnp_update_stream_state(UpnpStream *stream) {
625 if((stream->rtp == NULL || stream->rtp->state == LinphoneUpnpStateOk || stream->rtp->state == LinphoneUpnpStateIdle) &&
626 (stream->rtcp == NULL || stream->rtcp->state == LinphoneUpnpStateOk || stream->rtcp->state == LinphoneUpnpStateIdle)) {
627 stream->state = LinphoneUpnpStateOk;
628 } else if((stream->rtp != NULL &&
629 (stream->rtp->state == LinphoneUpnpStateAdding || stream->rtp->state == LinphoneUpnpStateRemoving)) ||
630 (stream->rtcp != NULL &&
631 (stream->rtcp->state == LinphoneUpnpStateAdding || stream->rtcp->state == LinphoneUpnpStateRemoving))) {
632 stream->state = LinphoneUpnpStatePending;
633 } else if((stream->rtp != NULL && stream->rtp->state == LinphoneUpnpStateKo) ||
634 (stream->rtcp != NULL && stream->rtcp->state == LinphoneUpnpStateKo)) {
635 stream->state = LinphoneUpnpStateKo;
637 ms_error("Invalid stream %p state", stream);
641 int linphone_upnp_call_process(LinphoneCall *call) {
642 LinphoneCore *lc = call->core;
643 UpnpContext *lupnp = lc->upnp;
645 LinphoneUpnpState oldState = 0, newState = 0;
651 ms_mutex_lock(&lupnp->mutex);
653 // Don't handle when the call
654 if(lupnp->state == LinphoneUpnpStateOk && call->upnp_session != NULL) {
660 linphone_upnp_update_stream_state(call->upnp_session->audio);
665 linphone_upnp_update_stream_state(call->upnp_session->video);
670 linphone_core_update_upnp_state_in_call_stats(call);
673 * Update session state
675 oldState = call->upnp_session->state;
676 if(call->upnp_session->audio->state == LinphoneUpnpStateOk &&
677 call->upnp_session->video->state == LinphoneUpnpStateOk) {
678 call->upnp_session->state = LinphoneUpnpStateOk;
679 } else if(call->upnp_session->audio->state == LinphoneUpnpStatePending ||
680 call->upnp_session->video->state == LinphoneUpnpStatePending) {
681 call->upnp_session->state = LinphoneUpnpStatePending;
682 } else if(call->upnp_session->audio->state == LinphoneUpnpStateKo ||
683 call->upnp_session->video->state == LinphoneUpnpStateKo) {
684 call->upnp_session->state = LinphoneUpnpStateKo;
686 call->upnp_session->state = LinphoneUpnpStateIdle;
688 newState = call->upnp_session->state;
691 ms_mutex_unlock(&lupnp->mutex);
693 /* When change is done proceed update */
694 if(oldState != LinphoneUpnpStateOk && oldState != LinphoneUpnpStateKo &&
695 (newState == LinphoneUpnpStateOk || newState == LinphoneUpnpStateKo)) {
696 if(call->upnp_session->state == LinphoneUpnpStateOk)
697 ms_message("uPnP IGD: uPnP for Call %p is ok", call);
699 ms_message("uPnP IGD: uPnP for Call %p is ko", call);
701 switch (call->state) {
702 case LinphoneCallUpdating:
703 linphone_core_start_update_call(lc, call);
705 case LinphoneCallUpdatedByRemote:
706 linphone_core_start_accept_call_update(lc, call);
708 case LinphoneCallOutgoingInit:
709 linphone_core_proceed_with_invite_if_ready(lc, call, NULL);
711 case LinphoneCallIdle:
712 linphone_core_notify_incoming_call(lc, call);
722 void linphone_core_upnp_refresh(UpnpContext *lupnp) {
723 MSList *global_list = NULL;
727 UpnpPortBinding *port_mapping, *port_mapping2;
729 ms_message("uPnP IGD: Refresh mappings");
731 if(lupnp->sip_udp != NULL) {
732 global_list = ms_list_append(global_list, lupnp->sip_udp);
734 if(lupnp->sip_tcp != NULL) {
735 global_list = ms_list_append(global_list, lupnp->sip_tcp);
737 if(lupnp->sip_tls != NULL) {
738 global_list = ms_list_append(global_list, lupnp->sip_tls);
741 list = lupnp->lc->calls;
742 while(list != NULL) {
743 call = (LinphoneCall *)list->data;
744 if(call->upnp_session != NULL) {
745 if(call->upnp_session->audio->rtp != NULL) {
746 global_list = ms_list_append(global_list, call->upnp_session->audio->rtp);
748 if(call->upnp_session->audio->rtcp != NULL) {
749 global_list = ms_list_append(global_list, call->upnp_session->audio->rtcp);
751 if(call->upnp_session->video->rtp != NULL) {
752 global_list = ms_list_append(global_list, call->upnp_session->video->rtp);
754 if(call->upnp_session->video->rtcp != NULL) {
755 global_list = ms_list_append(global_list, call->upnp_session->video->rtcp);
761 list = linphone_upnp_config_list_port_bindings(lupnp->lc->config);
762 for(item = list;item != NULL; item = item->next) {
763 port_mapping = (UpnpPortBinding *)item->data;
764 port_mapping2 = linphone_upnp_port_binding_equivalent_in_list(global_list, port_mapping);
765 if(port_mapping2 == NULL) {
766 linphone_upnp_context_send_remove_port_binding(lupnp, port_mapping, TRUE);
767 } else if(port_mapping2->state == LinphoneUpnpStateIdle){
768 /* Force to remove */
769 port_mapping2->state = LinphoneUpnpStateOk;
772 ms_list_for_each(list, (void (*)(void*))linphone_upnp_port_binding_release);
773 list = ms_list_free(list);
776 // (Re)Add removed port bindings
778 while(list != NULL) {
779 port_mapping = (UpnpPortBinding *)list->data;
780 linphone_upnp_context_send_remove_port_binding(lupnp, port_mapping, TRUE);
781 linphone_upnp_context_send_add_port_binding(lupnp, port_mapping, TRUE);
784 global_list = ms_list_free(global_list);
787 void linphone_upnp_update_port_binding(UpnpContext *lupnp, UpnpPortBinding **port_mapping, upnp_igd_ip_protocol protocol, int port, int retry_delay) {
788 const char *local_addr, *external_addr;
789 time_t now = time(NULL);
791 if(*port_mapping != NULL) {
792 if(port != (*port_mapping)->local_port) {
793 linphone_upnp_context_send_remove_port_binding(lupnp, *port_mapping, FALSE);
794 *port_mapping = NULL;
797 if(*port_mapping == NULL) {
798 *port_mapping = linphone_upnp_port_binding_new_or_collect(lupnp->pending_bindings, protocol, port, port);
802 local_addr = upnp_igd_get_local_ipaddress(lupnp->upnp_igd_ctxt);
803 external_addr = upnp_igd_get_external_ipaddress(lupnp->upnp_igd_ctxt);
805 // Force binding update on local address change
806 if(local_addr != NULL) {
807 if(strncmp((*port_mapping)->local_addr, local_addr, sizeof((*port_mapping)->local_addr))) {
808 linphone_upnp_context_send_remove_port_binding(lupnp, *port_mapping, FALSE);
809 strncpy((*port_mapping)->local_addr, local_addr, sizeof((*port_mapping)->local_addr));
812 if(external_addr != NULL) {
813 strncpy((*port_mapping)->external_addr, external_addr, sizeof((*port_mapping)->external_addr));
816 // Add (if not already done) the binding
817 if(now - (*port_mapping)->last_update >= retry_delay) {
818 (*port_mapping)->last_update = now;
819 linphone_upnp_context_send_add_port_binding(lupnp, *port_mapping, FALSE);
822 if(*port_mapping != NULL) {
823 linphone_upnp_context_send_remove_port_binding(lupnp, *port_mapping, FALSE);
824 *port_mapping = NULL;
829 void linphone_upnp_update_config(UpnpContext* lupnp) {
832 UpnpPortBinding *port_mapping;
835 for(item = lupnp->adding_configs;item!=NULL;item=item->next) {
836 port_mapping = (UpnpPortBinding *)item->data;
837 snprintf(key, sizeof(key), "%s-%d-%d",
838 (port_mapping->protocol == UPNP_IGD_IP_PROTOCOL_TCP)? "TCP":"UDP",
839 port_mapping->external_port,
840 port_mapping->local_port);
841 lp_config_set_string(lupnp->lc->config, UPNP_SECTION_NAME, key, "uPnP");
842 linphone_upnp_port_binding_log(ORTP_DEBUG, "Configuration: Added port binding", port_mapping);
844 ms_list_for_each(lupnp->adding_configs,(void (*)(void*))linphone_upnp_port_binding_release);
845 lupnp->adding_configs = ms_list_free(lupnp->adding_configs);
848 for(item = lupnp->removing_configs;item!=NULL;item=item->next) {
849 port_mapping = (UpnpPortBinding *)item->data;
850 snprintf(key, sizeof(key), "%s-%d-%d",
851 (port_mapping->protocol == UPNP_IGD_IP_PROTOCOL_TCP)? "TCP":"UDP",
852 port_mapping->external_port,
853 port_mapping->local_port);
854 lp_config_set_string(lupnp->lc->config, UPNP_SECTION_NAME, key, NULL);
855 linphone_upnp_port_binding_log(ORTP_DEBUG, "Configuration: Removed port binding", port_mapping);
857 ms_list_for_each(lupnp->removing_configs,(void (*)(void*))linphone_upnp_port_binding_release);
858 lupnp->removing_configs = ms_list_free(lupnp->removing_configs);
861 bool_t linphone_core_upnp_hook(void *data) {
862 LCSipTransports transport;
863 LinphoneUpnpState ready_state;
865 time_t now = time(NULL);
866 UpnpContext *lupnp = (UpnpContext *)data;
868 ms_mutex_lock(&lupnp->mutex);
871 if(lupnp->state == LinphoneUpnpStateOk) {
872 linphone_core_get_sip_transports(lupnp->lc, &transport);
873 linphone_upnp_update_port_binding(lupnp, &lupnp->sip_udp, UPNP_IGD_IP_PROTOCOL_UDP, transport.udp_port, UPNP_CORE_RETRY_DELAY);
874 linphone_upnp_update_port_binding(lupnp, &lupnp->sip_tcp, UPNP_IGD_IP_PROTOCOL_TCP, transport.tcp_port, UPNP_CORE_RETRY_DELAY);
875 linphone_upnp_update_port_binding(lupnp, &lupnp->sip_tls, UPNP_IGD_IP_PROTOCOL_TCP, transport.tls_port, UPNP_CORE_RETRY_DELAY);
878 /* Refresh registers if we are ready */
879 if(now - lupnp->last_ready_check >= UPNP_CORE_READY_CHECK) {
880 lupnp->last_ready_check = now;
881 ready_state = (_linphone_upnp_context_is_ready_for_register(lupnp))? LinphoneUpnpStateOk: LinphoneUpnpStateKo;
882 if(ready_state != lupnp->last_ready_state) {
883 for(item=linphone_core_get_proxy_config_list(lupnp->lc);item!=NULL;item=item->next) {
884 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)item->data;
885 if (linphone_proxy_config_register_enabled(cfg)) {
886 if (ready_state != LinphoneUpnpStateOk) {
887 // Only reset ithe registration if we require that upnp should be ok
888 if(lupnp->lc->sip_conf.register_only_when_upnp_is_ok) {
889 linphone_proxy_config_set_state(cfg, LinphoneRegistrationNone, "Registration impossible (uPnP not ready)");
896 lupnp->last_ready_state = ready_state;
900 linphone_upnp_update_config(lupnp);
902 ms_mutex_unlock(&lupnp->mutex);
906 int linphone_core_update_local_media_description_from_upnp(SalMediaDescription *desc, UpnpSession *session) {
908 SalStreamDescription *stream;
909 UpnpStream *upnpStream;
911 for (i = 0; i < desc->n_active_streams; i++) {
912 stream = &desc->streams[i];
914 if(stream->type == SalAudio) {
915 upnpStream = session->audio;
916 } else if(stream->type == SalVideo) {
917 upnpStream = session->video;
919 if(upnpStream != NULL) {
920 if(upnpStream->rtp != NULL && upnpStream->rtp->state == LinphoneUpnpStateOk) {
921 strncpy(stream->rtp_addr, upnpStream->rtp->external_addr, LINPHONE_IPADDR_SIZE);
922 stream->rtp_port = upnpStream->rtp->external_port;
924 if(upnpStream->rtcp != NULL && upnpStream->rtcp->state == LinphoneUpnpStateOk) {
925 strncpy(stream->rtcp_addr, upnpStream->rtcp->external_addr, LINPHONE_IPADDR_SIZE);
926 stream->rtcp_port = upnpStream->rtcp->external_port;
938 UpnpPortBinding *linphone_upnp_port_binding_new() {
939 UpnpPortBinding *port = NULL;
940 port = ms_new0(UpnpPortBinding,1);
941 ms_mutex_init(&port->mutex, NULL);
942 port->state = LinphoneUpnpStateIdle;
943 port->protocol = UPNP_IGD_IP_PROTOCOL_UDP;
944 port->local_addr[0] = '\0';
945 port->local_port = -1;
946 port->external_addr[0] = '\0';
947 port->external_port = -1;
948 port->to_remove = FALSE;
949 port->to_add = FALSE;
951 port->last_update = 0;
955 UpnpPortBinding *linphone_upnp_port_binding_new_with_parameters(upnp_igd_ip_protocol protocol, int local_port, int external_port) {
956 UpnpPortBinding *port_binding = linphone_upnp_port_binding_new();
957 port_binding->protocol = protocol;
958 port_binding->local_port = local_port;
959 port_binding->external_port = external_port;
963 UpnpPortBinding *linphone_upnp_port_binding_new_or_collect(MSList *list, upnp_igd_ip_protocol protocol, int local_port, int external_port) {
964 UpnpPortBinding *tmp_binding;
965 UpnpPortBinding *end_binding;
966 end_binding = linphone_upnp_port_binding_new_with_parameters(protocol, local_port, external_port);
967 tmp_binding = linphone_upnp_port_binding_equivalent_in_list(list, end_binding);
968 if(tmp_binding != NULL) {
969 linphone_upnp_port_binding_release(end_binding);
970 end_binding = tmp_binding;
975 UpnpPortBinding *linphone_upnp_port_binding_copy(const UpnpPortBinding *port) {
976 UpnpPortBinding *new_port = NULL;
977 new_port = ms_new0(UpnpPortBinding,1);
978 memcpy(new_port, port, sizeof(UpnpPortBinding));
979 ms_mutex_init(&new_port->mutex, NULL);
984 void linphone_upnp_port_binding_log(int level, const char *msg, const UpnpPortBinding *port) {
985 if(strlen(port->local_addr)) {
986 ortp_log(level, "uPnP IGD: %s %s|%d->%s:%d (retry %d)", msg,
987 (port->protocol == UPNP_IGD_IP_PROTOCOL_TCP)? "TCP":"UDP",
993 ortp_log(level, "uPnP IGD: %s %s|%d->%d (retry %d)", msg,
994 (port->protocol == UPNP_IGD_IP_PROTOCOL_TCP)? "TCP":"UDP",
1001 bool_t linphone_upnp_port_binding_equal(const UpnpPortBinding *port1, const UpnpPortBinding *port2) {
1002 return port1->protocol == port2->protocol &&
1003 port1->local_port == port2->local_port &&
1004 port1->external_port == port2->external_port;
1007 UpnpPortBinding *linphone_upnp_port_binding_equivalent_in_list(MSList *list, const UpnpPortBinding *port) {
1008 UpnpPortBinding *port_mapping;
1009 while(list != NULL) {
1010 port_mapping = (UpnpPortBinding *)list->data;
1011 if(linphone_upnp_port_binding_equal(port, port_mapping)) {
1012 return port_mapping;
1020 UpnpPortBinding *linphone_upnp_port_binding_retain(UpnpPortBinding *port) {
1021 ms_mutex_lock(&port->mutex);
1023 ms_mutex_unlock(&port->mutex);
1027 void linphone_upnp_port_binding_release(UpnpPortBinding *port) {
1028 ms_mutex_lock(&port->mutex);
1029 if(--port->ref == 0) {
1030 ms_mutex_unlock(&port->mutex);
1031 ms_mutex_destroy(&port->mutex);
1035 ms_mutex_unlock(&port->mutex);
1043 UpnpStream* linphone_upnp_stream_new() {
1044 UpnpStream *stream = ms_new0(UpnpStream,1);
1045 stream->state = LinphoneUpnpStateIdle;
1047 stream->rtcp = NULL;
1051 void linphone_upnp_stream_destroy(UpnpStream* stream) {
1052 if(stream->rtp != NULL) {
1053 linphone_upnp_port_binding_release(stream->rtp);
1056 if(stream->rtcp != NULL) {
1057 linphone_upnp_port_binding_release(stream->rtcp);
1058 stream->rtcp = NULL;
1068 UpnpSession* linphone_upnp_session_new(LinphoneCall* call) {
1069 UpnpSession *session = ms_new0(UpnpSession,1);
1070 session->call = call;
1071 session->state = LinphoneUpnpStateIdle;
1072 session->audio = linphone_upnp_stream_new();
1073 session->video = linphone_upnp_stream_new();
1077 void linphone_upnp_session_destroy(UpnpSession *session) {
1078 LinphoneCore *lc = session->call->core;
1080 if(lc->upnp != NULL) {
1081 /* Remove bindings */
1082 if(session->audio->rtp != NULL) {
1083 linphone_upnp_context_send_remove_port_binding(lc->upnp, session->audio->rtp, TRUE);
1085 if(session->audio->rtcp != NULL) {
1086 linphone_upnp_context_send_remove_port_binding(lc->upnp, session->audio->rtcp, TRUE);
1088 if(session->video->rtp != NULL) {
1089 linphone_upnp_context_send_remove_port_binding(lc->upnp, session->video->rtp, TRUE);
1091 if(session->video->rtcp != NULL) {
1092 linphone_upnp_context_send_remove_port_binding(lc->upnp, session->video->rtcp, TRUE);
1096 linphone_upnp_stream_destroy(session->audio);
1097 linphone_upnp_stream_destroy(session->video);
1101 LinphoneUpnpState linphone_upnp_session_get_state(UpnpSession *session) {
1102 return session->state;
1110 struct linphone_upnp_config_list_port_bindings_struct {
1111 struct _LpConfig *lpc;
1115 static void linphone_upnp_config_list_port_bindings_cb(const char *entry, struct linphone_upnp_config_list_port_bindings_struct *cookie) {
1116 char protocol_str[4]; // TCP or UDP
1117 upnp_igd_ip_protocol protocol;
1120 bool_t valid = TRUE;
1121 UpnpPortBinding *port;
1122 if(sscanf(entry, "%3s-%i-%i", protocol_str, &external_port, &local_port) == 3) {
1123 if(strcasecmp(protocol_str, "TCP") == 0) {
1124 protocol = UPNP_IGD_IP_PROTOCOL_TCP;
1125 } else if(strcasecmp(protocol_str, "UDP") == 0) {
1126 protocol = UPNP_IGD_IP_PROTOCOL_UDP;
1131 port = linphone_upnp_port_binding_new();
1132 port->state = LinphoneUpnpStateOk;
1133 port->protocol = protocol;
1134 port->external_port = external_port;
1135 port->local_port = local_port;
1136 cookie->retList = ms_list_append(cookie->retList, port);
1142 ms_warning("uPnP configuration invalid line: %s", entry);
1146 MSList *linphone_upnp_config_list_port_bindings(struct _LpConfig *lpc) {
1147 struct linphone_upnp_config_list_port_bindings_struct cookie = {lpc, NULL};
1148 lp_config_for_each_entry(lpc, UPNP_SECTION_NAME, (void(*)(const char *, void*))linphone_upnp_config_list_port_bindings_cb, &cookie);
1149 return cookie.retList;
1152 void linphone_upnp_config_add_port_binding(UpnpContext *lupnp, const UpnpPortBinding *port) {
1154 UpnpPortBinding *list_port;
1156 list = lupnp->removing_configs;
1157 while(list != NULL) {
1158 list_port = (UpnpPortBinding *)list->data;
1159 if(linphone_upnp_port_binding_equal(list_port, port) == TRUE) {
1160 lupnp->removing_configs = ms_list_remove(lupnp->removing_configs, list_port);
1161 linphone_upnp_port_binding_release(list_port);
1164 list = ms_list_next(list);
1167 list = lupnp->adding_configs;
1168 while(list != NULL) {
1169 list_port = (UpnpPortBinding *)list->data;
1170 if(linphone_upnp_port_binding_equal(list_port, port) == TRUE) {
1173 list = ms_list_next(list);
1176 list_port = linphone_upnp_port_binding_copy(port);
1177 lupnp->adding_configs = ms_list_append(lupnp->adding_configs, list_port);
1180 void linphone_upnp_config_remove_port_binding(UpnpContext *lupnp, const UpnpPortBinding *port) {
1182 UpnpPortBinding *list_port;
1184 list = lupnp->adding_configs;
1185 while(list != NULL) {
1186 list_port = (UpnpPortBinding *)list->data;
1187 if(linphone_upnp_port_binding_equal(list_port, port) == TRUE) {
1188 lupnp->adding_configs = ms_list_remove(lupnp->adding_configs, list_port);
1189 linphone_upnp_port_binding_release(list_port);
1192 list = ms_list_next(list);
1195 list = lupnp->removing_configs;
1196 while(list != NULL) {
1197 list_port = (UpnpPortBinding *)list->data;
1198 if(linphone_upnp_port_binding_equal(list_port, port) == TRUE) {
1201 list = ms_list_next(list);
1204 list_port = linphone_upnp_port_binding_copy(port);
1205 lupnp->removing_configs = ms_list_append(lupnp->removing_configs, list_port);