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);
96 void linphone_upnp_update_proxy(UpnpContext *lupnp, bool_t force);
99 MSList *linphone_upnp_config_list_port_bindings(struct _LpConfig *lpc);
100 void linphone_upnp_config_add_port_binding(UpnpContext *lupnp, const UpnpPortBinding *port);
101 void linphone_upnp_config_remove_port_binding(UpnpContext *lupnp, const UpnpPortBinding *port);
104 int linphone_upnp_context_send_remove_port_binding(UpnpContext *lupnp, UpnpPortBinding *port, bool_t retry);
105 int linphone_upnp_context_send_add_port_binding(UpnpContext *lupnp, UpnpPortBinding *port, bool_t retry);
112 /* Convert uPnP IGD logs to ortp logs */
113 void linphone_upnp_igd_print(void *cookie, upnp_igd_print_level level, const char *fmt, va_list list) {
114 int ortp_level = ORTP_DEBUG;
116 case UPNP_IGD_MESSAGE:
117 ortp_level = ORTP_MESSAGE;
119 case UPNP_IGD_WARNING:
120 ortp_level = ORTP_DEBUG; // Too verbose otherwise
123 ortp_level = ORTP_DEBUG; // Too verbose otherwise
128 ortp_logv(ortp_level, fmt, list);
131 void linphone_upnp_igd_callback(void *cookie, upnp_igd_event event, void *arg) {
132 UpnpContext *lupnp = (UpnpContext *)cookie;
133 upnp_igd_port_mapping *mapping = NULL;
134 UpnpPortBinding *port_mapping = NULL;
135 const char *ip_address = NULL;
136 const char *connection_status = NULL;
137 bool_t nat_enabled = FALSE;
138 LinphoneUpnpState old_state;
140 if(lupnp == NULL || lupnp->upnp_igd_ctxt == NULL) {
141 ms_error("uPnP IGD: Invalid context in callback");
145 ms_mutex_lock(&lupnp->mutex);
146 old_state = lupnp->state;
149 case UPNP_IGD_DEVICE_ADDED:
150 case UPNP_IGD_DEVICE_REMOVED:
151 case UPNP_IGD_EXTERNAL_IPADDRESS_CHANGED:
152 case UPNP_IGD_NAT_ENABLED_CHANGED:
153 case UPNP_IGD_CONNECTION_STATUS_CHANGED:
154 ip_address = upnp_igd_get_external_ipaddress(lupnp->upnp_igd_ctxt);
155 connection_status = upnp_igd_get_connection_status(lupnp->upnp_igd_ctxt);
156 nat_enabled = upnp_igd_get_nat_enabled(lupnp->upnp_igd_ctxt);
158 if(ip_address == NULL || connection_status == NULL) {
159 ms_message("uPnP IGD: Pending");
160 lupnp->state = LinphoneUpnpStatePending;
161 } else if(strcasecmp(connection_status, "Connected") || !nat_enabled) {
162 ms_message("uPnP IGD: Not Available");
163 lupnp->state = LinphoneUpnpStateNotAvailable;
165 ms_message("uPnP IGD: Connected");
166 lupnp->state = LinphoneUpnpStateOk;
167 if(old_state != LinphoneUpnpStateOk) {
168 linphone_core_upnp_refresh(lupnp);
174 case UPNP_IGD_PORT_MAPPING_ADD_SUCCESS:
175 mapping = (upnp_igd_port_mapping *) arg;
176 port_mapping = (UpnpPortBinding*) mapping->cookie;
177 port_mapping->external_port = mapping->remote_port;
178 port_mapping->state = LinphoneUpnpStateOk;
179 linphone_upnp_port_binding_log(ORTP_MESSAGE, "Added port binding", port_mapping);
180 linphone_upnp_config_add_port_binding(lupnp, port_mapping);
184 case UPNP_IGD_PORT_MAPPING_ADD_FAILURE:
185 mapping = (upnp_igd_port_mapping *) arg;
186 port_mapping = (UpnpPortBinding*) mapping->cookie;
187 port_mapping->external_port = -1; //Force random external port
188 if(linphone_upnp_context_send_add_port_binding(lupnp, port_mapping, TRUE) != 0) {
189 linphone_upnp_port_binding_log(ORTP_ERROR, "Can't add port binding", port_mapping);
194 case UPNP_IGD_PORT_MAPPING_REMOVE_SUCCESS:
195 mapping = (upnp_igd_port_mapping *) arg;
196 port_mapping = (UpnpPortBinding*) mapping->cookie;
197 port_mapping->state = LinphoneUpnpStateIdle;
198 linphone_upnp_port_binding_log(ORTP_MESSAGE, "Removed port binding", port_mapping);
199 linphone_upnp_config_remove_port_binding(lupnp, port_mapping);
203 case UPNP_IGD_PORT_MAPPING_REMOVE_FAILURE:
204 mapping = (upnp_igd_port_mapping *) arg;
205 port_mapping = (UpnpPortBinding*) mapping->cookie;
206 if(linphone_upnp_context_send_remove_port_binding(lupnp, port_mapping, TRUE) != 0) {
207 linphone_upnp_port_binding_log(ORTP_ERROR, "Can't remove port binding", port_mapping);
208 linphone_upnp_config_remove_port_binding(lupnp, port_mapping);
217 if(port_mapping != NULL) {
219 * Execute delayed actions
221 if(port_mapping->to_remove) {
222 if(port_mapping->state == LinphoneUpnpStateOk) {
223 port_mapping->to_remove = FALSE;
224 linphone_upnp_context_send_remove_port_binding(lupnp, port_mapping, FALSE);
225 } else if(port_mapping->state == LinphoneUpnpStateKo) {
226 port_mapping->to_remove = FALSE;
229 if(port_mapping->to_add) {
230 if(port_mapping->state == LinphoneUpnpStateIdle || port_mapping->state == LinphoneUpnpStateKo) {
231 port_mapping->to_add = FALSE;
232 linphone_upnp_context_send_add_port_binding(lupnp, port_mapping, FALSE);
236 lupnp->pending_bindings = ms_list_remove(lupnp->pending_bindings, port_mapping);
237 linphone_upnp_port_binding_release(port_mapping);
241 * If there is no pending binding emit a signal
243 if(lupnp->pending_bindings == NULL) {
244 pthread_cond_signal(&lupnp->empty_cond);
246 ms_mutex_unlock(&lupnp->mutex);
254 UpnpContext* linphone_upnp_context_new(LinphoneCore *lc) {
255 UpnpContext *lupnp = (UpnpContext *)ms_new0(UpnpContext,1);
257 ms_mutex_init(&lupnp->mutex, NULL);
258 ms_cond_init(&lupnp->empty_cond, NULL);
260 lupnp->last_ready_check = 0;
261 lupnp->last_ready_state = LinphoneUpnpStateIdle;
264 lupnp->pending_bindings = NULL;
265 lupnp->adding_configs = NULL;
266 lupnp->removing_configs = NULL;
267 lupnp->state = LinphoneUpnpStateIdle;
268 ms_message("uPnP IGD: New %p for core %p", lupnp, lc);
271 lupnp->sip_udp = NULL;
272 lupnp->sip_tcp = NULL;
273 lupnp->sip_tls = NULL;
275 linphone_core_add_iterate_hook(lc, linphone_core_upnp_hook, lupnp);
277 lupnp->upnp_igd_ctxt = NULL;
278 lupnp->upnp_igd_ctxt = upnp_igd_create(linphone_upnp_igd_callback, linphone_upnp_igd_print, lupnp);
279 if(lupnp->upnp_igd_ctxt == NULL) {
280 lupnp->state = LinphoneUpnpStateKo;
281 ms_error("Can't create uPnP IGD context");
285 lupnp->state = LinphoneUpnpStatePending;
286 upnp_igd_start(lupnp->upnp_igd_ctxt);
291 void linphone_upnp_context_destroy(UpnpContext *lupnp) {
292 linphone_core_remove_iterate_hook(lupnp->lc, linphone_core_upnp_hook, lupnp);
294 ms_mutex_lock(&lupnp->mutex);
296 /* Send port binding removes */
297 if(lupnp->sip_udp != NULL) {
298 linphone_upnp_context_send_remove_port_binding(lupnp, lupnp->sip_udp, TRUE);
300 if(lupnp->sip_tcp != NULL) {
301 linphone_upnp_context_send_remove_port_binding(lupnp, lupnp->sip_tcp, TRUE);
303 if(lupnp->sip_tls != NULL) {
304 linphone_upnp_context_send_remove_port_binding(lupnp, lupnp->sip_tls, TRUE);
307 /* Wait all pending bindings are done */
308 if(lupnp->pending_bindings != NULL) {
309 ms_message("uPnP IGD: Wait all pending port bindings ...");
310 ms_cond_wait(&lupnp->empty_cond, &lupnp->mutex);
313 if(lupnp->upnp_igd_ctxt != NULL) {
314 // upnp_igd_destroy is synchronous so the callbacks will be called in the same thread.
315 // So release the mutex before upnp_igd_destroy call.
316 ms_mutex_unlock(&lupnp->mutex);
317 upnp_igd_destroy(lupnp->upnp_igd_ctxt);
318 ms_mutex_lock(&lupnp->mutex);
319 lupnp->upnp_igd_ctxt = NULL;
322 /* Run one more time configuration update and proxy */
323 linphone_upnp_update_config(lupnp);
324 linphone_upnp_update_proxy(lupnp, TRUE);
326 /* Release port bindings */
327 if(lupnp->sip_udp != NULL) {
328 linphone_upnp_port_binding_release(lupnp->sip_udp);
329 lupnp->sip_udp = NULL;
331 if(lupnp->sip_tcp != NULL) {
332 linphone_upnp_port_binding_release(lupnp->sip_tcp);
333 lupnp->sip_tcp = NULL;
335 if(lupnp->sip_tls != NULL) {
336 linphone_upnp_port_binding_release(lupnp->sip_tls);
337 lupnp->sip_tcp = NULL;
341 ms_list_for_each(lupnp->adding_configs,(void (*)(void*))linphone_upnp_port_binding_release);
342 lupnp->adding_configs = ms_list_free(lupnp->adding_configs);
343 ms_list_for_each(lupnp->removing_configs,(void (*)(void*))linphone_upnp_port_binding_release);
344 lupnp->removing_configs = ms_list_free(lupnp->removing_configs);
345 ms_list_for_each(lupnp->pending_bindings,(void (*)(void*))linphone_upnp_port_binding_release);
346 lupnp->pending_bindings = ms_list_free(lupnp->pending_bindings);
348 ms_mutex_unlock(&lupnp->mutex);
350 ms_mutex_destroy(&lupnp->mutex);
351 ms_cond_destroy(&lupnp->empty_cond);
353 ms_message("uPnP IGD: destroy %p", lupnp);
357 LinphoneUpnpState linphone_upnp_context_get_state(UpnpContext *lupnp) {
358 LinphoneUpnpState state;
359 ms_mutex_lock(&lupnp->mutex);
360 state = lupnp->state;
361 ms_mutex_unlock(&lupnp->mutex);
365 bool_t _linphone_upnp_context_is_ready_for_register(UpnpContext *lupnp) {
368 // 1 Check global uPnP state
369 ready = (lupnp->state == LinphoneUpnpStateOk);
371 // 2 Check external ip address
373 if (upnp_igd_get_external_ipaddress(lupnp->upnp_igd_ctxt) == NULL) {
378 // 3 Check sip ports bindings
380 if(lupnp->sip_udp != NULL) {
381 if(lupnp->sip_udp->state != LinphoneUpnpStateOk) {
384 } else if(lupnp->sip_tcp != NULL) {
385 if(lupnp->sip_tcp->state != LinphoneUpnpStateOk) {
388 } else if(lupnp->sip_tls != NULL) {
389 if(lupnp->sip_tls->state != LinphoneUpnpStateOk) {
400 bool_t linphone_upnp_context_is_ready_for_register(UpnpContext *lupnp) {
402 ms_mutex_lock(&lupnp->mutex);
403 ready = _linphone_upnp_context_is_ready_for_register(lupnp);
404 ms_mutex_unlock(&lupnp->mutex);
408 int linphone_upnp_context_get_external_port(UpnpContext *lupnp) {
410 ms_mutex_lock(&lupnp->mutex);
412 if(lupnp->sip_udp != NULL) {
413 if(lupnp->sip_udp->state == LinphoneUpnpStateOk) {
414 port = lupnp->sip_udp->external_port;
416 } else if(lupnp->sip_tcp != NULL) {
417 if(lupnp->sip_tcp->state == LinphoneUpnpStateOk) {
418 port = lupnp->sip_tcp->external_port;
420 } else if(lupnp->sip_tls != NULL) {
421 if(lupnp->sip_tls->state == LinphoneUpnpStateOk) {
422 port = lupnp->sip_tls->external_port;
426 ms_mutex_unlock(&lupnp->mutex);
430 const char* linphone_upnp_context_get_external_ipaddress(UpnpContext *lupnp) {
431 const char* addr = NULL;
432 ms_mutex_lock(&lupnp->mutex);
433 addr = upnp_igd_get_external_ipaddress(lupnp->upnp_igd_ctxt);
434 ms_mutex_unlock(&lupnp->mutex);
438 int linphone_upnp_context_send_add_port_binding(UpnpContext *lupnp, UpnpPortBinding *port, bool_t retry) {
439 upnp_igd_port_mapping mapping;
440 char description[128];
443 if(lupnp->state != LinphoneUpnpStateOk) {
447 // Compute port binding state
448 if(port->state != LinphoneUpnpStateAdding) {
449 port->to_remove = FALSE;
450 switch(port->state) {
451 case LinphoneUpnpStateKo:
452 case LinphoneUpnpStateIdle: {
454 port->state = LinphoneUpnpStateAdding;
457 case LinphoneUpnpStateRemoving: {
467 // No retry if specified
468 if(port->retry != 0 && !retry) {
472 if(port->retry >= UPNP_ADD_MAX_RETRY) {
475 mapping.cookie = linphone_upnp_port_binding_retain(port);
476 lupnp->pending_bindings = ms_list_append(lupnp->pending_bindings, mapping.cookie);
478 mapping.local_port = port->local_port;
479 mapping.local_host = port->local_addr;
480 if(port->external_port == -1)
481 mapping.remote_port = rand()%(0xffff - 1024) + 1024;
483 mapping.remote_port = port->external_port;
484 mapping.remote_host = "";
485 snprintf(description, 128, "%s %s at %s:%d",
487 (port->protocol == UPNP_IGD_IP_PROTOCOL_TCP)? "TCP": "UDP",
488 port->local_addr, port->local_port);
489 mapping.description = description;
490 mapping.protocol = port->protocol;
493 linphone_upnp_port_binding_log(ORTP_MESSAGE, "Try to add port binding", port);
494 ret = upnp_igd_add_port_mapping(lupnp->upnp_igd_ctxt, &mapping);
497 port->state = LinphoneUpnpStateKo;
502 int linphone_upnp_context_send_remove_port_binding(UpnpContext *lupnp, UpnpPortBinding *port, bool_t retry) {
503 upnp_igd_port_mapping mapping;
506 if(lupnp->state != LinphoneUpnpStateOk) {
510 // Compute port binding state
511 if(port->state != LinphoneUpnpStateRemoving) {
512 port->to_add = FALSE;
513 switch(port->state) {
514 case LinphoneUpnpStateOk: {
516 port->state = LinphoneUpnpStateRemoving;
519 case LinphoneUpnpStateAdding: {
520 port->to_remove = TRUE;
529 // No retry if specified
530 if(port->retry != 0 && !retry) {
534 if(port->retry >= UPNP_REMOVE_MAX_RETRY) {
537 mapping.cookie = linphone_upnp_port_binding_retain(port);
538 lupnp->pending_bindings = ms_list_append(lupnp->pending_bindings, mapping.cookie);
540 mapping.remote_port = port->external_port;
541 mapping.remote_host = "";
542 mapping.protocol = port->protocol;
544 linphone_upnp_port_binding_log(ORTP_MESSAGE, "Try to remove port binding", port);
545 ret = upnp_igd_delete_port_mapping(lupnp->upnp_igd_ctxt, &mapping);
548 port->state = LinphoneUpnpStateKo;
554 * uPnP Core interfaces
557 int linphone_core_update_upnp_audio_video(LinphoneCall *call, bool_t audio, bool_t video) {
558 LinphoneCore *lc = call->core;
559 UpnpContext *lupnp = lc->upnp;
566 ms_mutex_lock(&lupnp->mutex);
568 // Don't handle when the call
569 if(lupnp->state == LinphoneUpnpStateOk && call->upnp_session != NULL) {
575 linphone_upnp_update_port_binding(lupnp, &call->upnp_session->audio->rtp,
576 UPNP_IGD_IP_PROTOCOL_UDP, (audio)? call->audio_port:0, UPNP_CALL_RETRY_DELAY);
578 linphone_upnp_update_port_binding(lupnp, &call->upnp_session->audio->rtcp,
579 UPNP_IGD_IP_PROTOCOL_UDP, (audio)? call->audio_port+1:0, UPNP_CALL_RETRY_DELAY);
584 linphone_upnp_update_port_binding(lupnp, &call->upnp_session->video->rtp,
585 UPNP_IGD_IP_PROTOCOL_UDP, (video)? call->video_port:0, UPNP_CALL_RETRY_DELAY);
587 linphone_upnp_update_port_binding(lupnp, &call->upnp_session->video->rtcp,
588 UPNP_IGD_IP_PROTOCOL_UDP, (video)? call->video_port+1:0, UPNP_CALL_RETRY_DELAY);
591 ms_mutex_unlock(&lupnp->mutex);
594 * Update uPnP call state
596 linphone_upnp_call_process(call);
603 int linphone_core_update_upnp_from_remote_media_description(LinphoneCall *call, const SalMediaDescription *md) {
604 bool_t audio = FALSE;
605 bool_t video = FALSE;
607 const SalStreamDescription *stream;
609 for (i = 0; i < md->n_total_streams; i++) {
610 stream = &md->streams[i];
611 if(stream->type == SalAudio) {
613 } else if(stream->type == SalVideo) {
618 return linphone_core_update_upnp_audio_video(call, audio, video);
621 int linphone_core_update_upnp(LinphoneCore *lc, LinphoneCall *call) {
622 return linphone_core_update_upnp_audio_video(call, call->audiostream!=NULL, call->videostream!=NULL);
625 void linphone_core_update_upnp_state_in_call_stats(LinphoneCall *call) {
626 call->stats[LINPHONE_CALL_STATS_AUDIO].upnp_state = call->upnp_session->audio->state;
627 call->stats[LINPHONE_CALL_STATS_VIDEO].upnp_state = call->upnp_session->video->state;
630 void linphone_upnp_update_stream_state(UpnpStream *stream) {
631 if((stream->rtp == NULL || stream->rtp->state == LinphoneUpnpStateOk || stream->rtp->state == LinphoneUpnpStateIdle) &&
632 (stream->rtcp == NULL || stream->rtcp->state == LinphoneUpnpStateOk || stream->rtcp->state == LinphoneUpnpStateIdle)) {
633 stream->state = LinphoneUpnpStateOk;
634 } else if((stream->rtp != NULL &&
635 (stream->rtp->state == LinphoneUpnpStateAdding || stream->rtp->state == LinphoneUpnpStateRemoving)) ||
636 (stream->rtcp != NULL &&
637 (stream->rtcp->state == LinphoneUpnpStateAdding || stream->rtcp->state == LinphoneUpnpStateRemoving))) {
638 stream->state = LinphoneUpnpStatePending;
639 } else if((stream->rtp != NULL && stream->rtp->state == LinphoneUpnpStateKo) ||
640 (stream->rtcp != NULL && stream->rtcp->state == LinphoneUpnpStateKo)) {
641 stream->state = LinphoneUpnpStateKo;
643 ms_error("Invalid stream %p state", stream);
647 int linphone_upnp_call_process(LinphoneCall *call) {
648 LinphoneCore *lc = call->core;
649 UpnpContext *lupnp = lc->upnp;
651 LinphoneUpnpState oldState = 0, newState = 0;
657 ms_mutex_lock(&lupnp->mutex);
659 // Don't handle when the call
660 if(lupnp->state == LinphoneUpnpStateOk && call->upnp_session != NULL) {
666 linphone_upnp_update_stream_state(call->upnp_session->audio);
671 linphone_upnp_update_stream_state(call->upnp_session->video);
676 linphone_core_update_upnp_state_in_call_stats(call);
679 * Update session state
681 oldState = call->upnp_session->state;
682 if(call->upnp_session->audio->state == LinphoneUpnpStateOk &&
683 call->upnp_session->video->state == LinphoneUpnpStateOk) {
684 call->upnp_session->state = LinphoneUpnpStateOk;
685 } else if(call->upnp_session->audio->state == LinphoneUpnpStatePending ||
686 call->upnp_session->video->state == LinphoneUpnpStatePending) {
687 call->upnp_session->state = LinphoneUpnpStatePending;
688 } else if(call->upnp_session->audio->state == LinphoneUpnpStateKo ||
689 call->upnp_session->video->state == LinphoneUpnpStateKo) {
690 call->upnp_session->state = LinphoneUpnpStateKo;
692 call->upnp_session->state = LinphoneUpnpStateIdle;
694 newState = call->upnp_session->state;
697 ms_mutex_unlock(&lupnp->mutex);
699 /* When change is done proceed update */
700 if(oldState != LinphoneUpnpStateOk && oldState != LinphoneUpnpStateKo &&
701 (newState == LinphoneUpnpStateOk || newState == LinphoneUpnpStateKo)) {
702 if(call->upnp_session->state == LinphoneUpnpStateOk)
703 ms_message("uPnP IGD: uPnP for Call %p is ok", call);
705 ms_message("uPnP IGD: uPnP for Call %p is ko", call);
707 switch (call->state) {
708 case LinphoneCallUpdating:
709 linphone_core_start_update_call(lc, call);
711 case LinphoneCallUpdatedByRemote:
712 linphone_core_start_accept_call_update(lc, call);
714 case LinphoneCallOutgoingInit:
715 linphone_core_proceed_with_invite_if_ready(lc, call, NULL);
717 case LinphoneCallIdle:
718 linphone_core_notify_incoming_call(lc, call);
728 void linphone_core_upnp_refresh(UpnpContext *lupnp) {
729 MSList *global_list = NULL;
733 UpnpPortBinding *port_mapping, *port_mapping2;
735 ms_message("uPnP IGD: Refresh mappings");
737 if(lupnp->sip_udp != NULL) {
738 global_list = ms_list_append(global_list, lupnp->sip_udp);
740 if(lupnp->sip_tcp != NULL) {
741 global_list = ms_list_append(global_list, lupnp->sip_tcp);
743 if(lupnp->sip_tls != NULL) {
744 global_list = ms_list_append(global_list, lupnp->sip_tls);
747 list = lupnp->lc->calls;
748 while(list != NULL) {
749 call = (LinphoneCall *)list->data;
750 if(call->upnp_session != NULL) {
751 if(call->upnp_session->audio->rtp != NULL) {
752 global_list = ms_list_append(global_list, call->upnp_session->audio->rtp);
754 if(call->upnp_session->audio->rtcp != NULL) {
755 global_list = ms_list_append(global_list, call->upnp_session->audio->rtcp);
757 if(call->upnp_session->video->rtp != NULL) {
758 global_list = ms_list_append(global_list, call->upnp_session->video->rtp);
760 if(call->upnp_session->video->rtcp != NULL) {
761 global_list = ms_list_append(global_list, call->upnp_session->video->rtcp);
767 list = linphone_upnp_config_list_port_bindings(lupnp->lc->config);
768 for(item = list;item != NULL; item = item->next) {
769 port_mapping = (UpnpPortBinding *)item->data;
770 port_mapping2 = linphone_upnp_port_binding_equivalent_in_list(global_list, port_mapping);
771 if(port_mapping2 == NULL) {
772 linphone_upnp_context_send_remove_port_binding(lupnp, port_mapping, TRUE);
773 } else if(port_mapping2->state == LinphoneUpnpStateIdle){
774 /* Force to remove */
775 port_mapping2->state = LinphoneUpnpStateOk;
778 ms_list_for_each(list, (void (*)(void*))linphone_upnp_port_binding_release);
779 list = ms_list_free(list);
782 // (Re)Add removed port bindings
784 while(list != NULL) {
785 port_mapping = (UpnpPortBinding *)list->data;
786 linphone_upnp_context_send_remove_port_binding(lupnp, port_mapping, TRUE);
787 linphone_upnp_context_send_add_port_binding(lupnp, port_mapping, TRUE);
790 global_list = ms_list_free(global_list);
793 void linphone_upnp_update_port_binding(UpnpContext *lupnp, UpnpPortBinding **port_mapping, upnp_igd_ip_protocol protocol, int port, int retry_delay) {
794 const char *local_addr, *external_addr;
795 time_t now = time(NULL);
797 if(*port_mapping != NULL) {
798 if(port != (*port_mapping)->local_port) {
799 linphone_upnp_context_send_remove_port_binding(lupnp, *port_mapping, FALSE);
800 *port_mapping = NULL;
803 if(*port_mapping == NULL) {
804 *port_mapping = linphone_upnp_port_binding_new_or_collect(lupnp->pending_bindings, protocol, port, port);
808 local_addr = upnp_igd_get_local_ipaddress(lupnp->upnp_igd_ctxt);
809 external_addr = upnp_igd_get_external_ipaddress(lupnp->upnp_igd_ctxt);
811 // Force binding update on local address change
812 if(local_addr != NULL) {
813 if(strncmp((*port_mapping)->local_addr, local_addr, sizeof((*port_mapping)->local_addr))) {
814 linphone_upnp_context_send_remove_port_binding(lupnp, *port_mapping, FALSE);
815 strncpy((*port_mapping)->local_addr, local_addr, sizeof((*port_mapping)->local_addr));
818 if(external_addr != NULL) {
819 strncpy((*port_mapping)->external_addr, external_addr, sizeof((*port_mapping)->external_addr));
822 // Add (if not already done) the binding
823 if(now - (*port_mapping)->last_update >= retry_delay) {
824 (*port_mapping)->last_update = now;
825 linphone_upnp_context_send_add_port_binding(lupnp, *port_mapping, FALSE);
828 if(*port_mapping != NULL) {
829 linphone_upnp_context_send_remove_port_binding(lupnp, *port_mapping, FALSE);
830 *port_mapping = NULL;
835 void linphone_upnp_update_config(UpnpContext* lupnp) {
838 UpnpPortBinding *port_mapping;
841 for(item = lupnp->adding_configs;item!=NULL;item=item->next) {
842 port_mapping = (UpnpPortBinding *)item->data;
843 snprintf(key, sizeof(key), "%s-%d-%d",
844 (port_mapping->protocol == UPNP_IGD_IP_PROTOCOL_TCP)? "TCP":"UDP",
845 port_mapping->external_port,
846 port_mapping->local_port);
847 lp_config_set_string(lupnp->lc->config, UPNP_SECTION_NAME, key, "uPnP");
848 linphone_upnp_port_binding_log(ORTP_DEBUG, "Configuration: Added port binding", port_mapping);
850 ms_list_for_each(lupnp->adding_configs,(void (*)(void*))linphone_upnp_port_binding_release);
851 lupnp->adding_configs = ms_list_free(lupnp->adding_configs);
854 for(item = lupnp->removing_configs;item!=NULL;item=item->next) {
855 port_mapping = (UpnpPortBinding *)item->data;
856 snprintf(key, sizeof(key), "%s-%d-%d",
857 (port_mapping->protocol == UPNP_IGD_IP_PROTOCOL_TCP)? "TCP":"UDP",
858 port_mapping->external_port,
859 port_mapping->local_port);
860 lp_config_set_string(lupnp->lc->config, UPNP_SECTION_NAME, key, NULL);
861 linphone_upnp_port_binding_log(ORTP_DEBUG, "Configuration: Removed port binding", port_mapping);
863 ms_list_for_each(lupnp->removing_configs,(void (*)(void*))linphone_upnp_port_binding_release);
864 lupnp->removing_configs = ms_list_free(lupnp->removing_configs);
867 void linphone_upnp_update_proxy(UpnpContext* lupnp, bool_t force) {
868 LinphoneUpnpState ready_state;
870 time_t now = (force)? (lupnp->last_ready_check + UPNP_CORE_READY_CHECK) : time(NULL);
872 /* Refresh registers if we are ready */
873 if(now - lupnp->last_ready_check >= UPNP_CORE_READY_CHECK) {
874 lupnp->last_ready_check = now;
875 ready_state = (_linphone_upnp_context_is_ready_for_register(lupnp))? LinphoneUpnpStateOk: LinphoneUpnpStateKo;
876 if(ready_state != lupnp->last_ready_state) {
877 for(item=linphone_core_get_proxy_config_list(lupnp->lc);item!=NULL;item=item->next) {
878 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)item->data;
879 if (linphone_proxy_config_register_enabled(cfg)) {
880 if (ready_state != LinphoneUpnpStateOk) {
881 // Only reset ithe registration if we require that upnp should be ok
882 if(lupnp->lc->sip_conf.register_only_when_upnp_is_ok) {
883 linphone_proxy_config_set_state(cfg, LinphoneRegistrationNone, "Registration impossible (uPnP not ready)");
890 lupnp->last_ready_state = ready_state;
895 bool_t linphone_core_upnp_hook(void *data) {
896 LCSipTransports transport;
897 UpnpContext *lupnp = (UpnpContext *)data;
899 ms_mutex_lock(&lupnp->mutex);
902 if(lupnp->state == LinphoneUpnpStateOk) {
903 linphone_core_get_sip_transports(lupnp->lc, &transport);
904 linphone_upnp_update_port_binding(lupnp, &lupnp->sip_udp, UPNP_IGD_IP_PROTOCOL_UDP, transport.udp_port, UPNP_CORE_RETRY_DELAY);
905 linphone_upnp_update_port_binding(lupnp, &lupnp->sip_tcp, UPNP_IGD_IP_PROTOCOL_TCP, transport.tcp_port, UPNP_CORE_RETRY_DELAY);
906 linphone_upnp_update_port_binding(lupnp, &lupnp->sip_tls, UPNP_IGD_IP_PROTOCOL_TCP, transport.tls_port, UPNP_CORE_RETRY_DELAY);
909 linphone_upnp_update_proxy(lupnp, FALSE);
910 linphone_upnp_update_config(lupnp);
912 ms_mutex_unlock(&lupnp->mutex);
916 int linphone_core_update_local_media_description_from_upnp(SalMediaDescription *desc, UpnpSession *session) {
918 SalStreamDescription *stream;
919 UpnpStream *upnpStream;
921 for (i = 0; i < desc->n_active_streams; i++) {
922 stream = &desc->streams[i];
924 if(stream->type == SalAudio) {
925 upnpStream = session->audio;
926 } else if(stream->type == SalVideo) {
927 upnpStream = session->video;
929 if(upnpStream != NULL) {
930 if(upnpStream->rtp != NULL && upnpStream->rtp->state == LinphoneUpnpStateOk) {
931 strncpy(stream->rtp_addr, upnpStream->rtp->external_addr, LINPHONE_IPADDR_SIZE);
932 stream->rtp_port = upnpStream->rtp->external_port;
934 if(upnpStream->rtcp != NULL && upnpStream->rtcp->state == LinphoneUpnpStateOk) {
935 strncpy(stream->rtcp_addr, upnpStream->rtcp->external_addr, LINPHONE_IPADDR_SIZE);
936 stream->rtcp_port = upnpStream->rtcp->external_port;
948 UpnpPortBinding *linphone_upnp_port_binding_new() {
949 UpnpPortBinding *port = NULL;
950 port = ms_new0(UpnpPortBinding,1);
951 ms_mutex_init(&port->mutex, NULL);
952 port->state = LinphoneUpnpStateIdle;
953 port->protocol = UPNP_IGD_IP_PROTOCOL_UDP;
954 port->local_addr[0] = '\0';
955 port->local_port = -1;
956 port->external_addr[0] = '\0';
957 port->external_port = -1;
958 port->to_remove = FALSE;
959 port->to_add = FALSE;
961 port->last_update = 0;
965 UpnpPortBinding *linphone_upnp_port_binding_new_with_parameters(upnp_igd_ip_protocol protocol, int local_port, int external_port) {
966 UpnpPortBinding *port_binding = linphone_upnp_port_binding_new();
967 port_binding->protocol = protocol;
968 port_binding->local_port = local_port;
969 port_binding->external_port = external_port;
973 UpnpPortBinding *linphone_upnp_port_binding_new_or_collect(MSList *list, upnp_igd_ip_protocol protocol, int local_port, int external_port) {
974 UpnpPortBinding *tmp_binding;
975 UpnpPortBinding *end_binding;
976 end_binding = linphone_upnp_port_binding_new_with_parameters(protocol, local_port, external_port);
977 tmp_binding = linphone_upnp_port_binding_equivalent_in_list(list, end_binding);
978 if(tmp_binding != NULL) {
979 linphone_upnp_port_binding_release(end_binding);
980 end_binding = tmp_binding;
985 UpnpPortBinding *linphone_upnp_port_binding_copy(const UpnpPortBinding *port) {
986 UpnpPortBinding *new_port = NULL;
987 new_port = ms_new0(UpnpPortBinding,1);
988 memcpy(new_port, port, sizeof(UpnpPortBinding));
989 ms_mutex_init(&new_port->mutex, NULL);
994 void linphone_upnp_port_binding_log(int level, const char *msg, const UpnpPortBinding *port) {
995 if(strlen(port->local_addr)) {
996 ortp_log(level, "uPnP IGD: %s %s|%d->%s:%d (retry %d)", msg,
997 (port->protocol == UPNP_IGD_IP_PROTOCOL_TCP)? "TCP":"UDP",
1003 ortp_log(level, "uPnP IGD: %s %s|%d->%d (retry %d)", msg,
1004 (port->protocol == UPNP_IGD_IP_PROTOCOL_TCP)? "TCP":"UDP",
1005 port->external_port,
1011 bool_t linphone_upnp_port_binding_equal(const UpnpPortBinding *port1, const UpnpPortBinding *port2) {
1012 return port1->protocol == port2->protocol &&
1013 port1->local_port == port2->local_port &&
1014 port1->external_port == port2->external_port;
1017 UpnpPortBinding *linphone_upnp_port_binding_equivalent_in_list(MSList *list, const UpnpPortBinding *port) {
1018 UpnpPortBinding *port_mapping;
1019 while(list != NULL) {
1020 port_mapping = (UpnpPortBinding *)list->data;
1021 if(linphone_upnp_port_binding_equal(port, port_mapping)) {
1022 return port_mapping;
1030 UpnpPortBinding *linphone_upnp_port_binding_retain(UpnpPortBinding *port) {
1031 ms_mutex_lock(&port->mutex);
1033 ms_mutex_unlock(&port->mutex);
1037 void linphone_upnp_port_binding_release(UpnpPortBinding *port) {
1038 ms_mutex_lock(&port->mutex);
1039 if(--port->ref == 0) {
1040 ms_mutex_unlock(&port->mutex);
1041 ms_mutex_destroy(&port->mutex);
1045 ms_mutex_unlock(&port->mutex);
1053 UpnpStream* linphone_upnp_stream_new() {
1054 UpnpStream *stream = ms_new0(UpnpStream,1);
1055 stream->state = LinphoneUpnpStateIdle;
1057 stream->rtcp = NULL;
1061 void linphone_upnp_stream_destroy(UpnpStream* stream) {
1062 if(stream->rtp != NULL) {
1063 linphone_upnp_port_binding_release(stream->rtp);
1066 if(stream->rtcp != NULL) {
1067 linphone_upnp_port_binding_release(stream->rtcp);
1068 stream->rtcp = NULL;
1078 UpnpSession* linphone_upnp_session_new(LinphoneCall* call) {
1079 UpnpSession *session = ms_new0(UpnpSession,1);
1080 session->call = call;
1081 session->state = LinphoneUpnpStateIdle;
1082 session->audio = linphone_upnp_stream_new();
1083 session->video = linphone_upnp_stream_new();
1087 void linphone_upnp_session_destroy(UpnpSession *session) {
1088 LinphoneCore *lc = session->call->core;
1090 if(lc->upnp != NULL) {
1091 /* Remove bindings */
1092 if(session->audio->rtp != NULL) {
1093 linphone_upnp_context_send_remove_port_binding(lc->upnp, session->audio->rtp, TRUE);
1095 if(session->audio->rtcp != NULL) {
1096 linphone_upnp_context_send_remove_port_binding(lc->upnp, session->audio->rtcp, TRUE);
1098 if(session->video->rtp != NULL) {
1099 linphone_upnp_context_send_remove_port_binding(lc->upnp, session->video->rtp, TRUE);
1101 if(session->video->rtcp != NULL) {
1102 linphone_upnp_context_send_remove_port_binding(lc->upnp, session->video->rtcp, TRUE);
1106 linphone_upnp_stream_destroy(session->audio);
1107 linphone_upnp_stream_destroy(session->video);
1111 LinphoneUpnpState linphone_upnp_session_get_state(UpnpSession *session) {
1112 return session->state;
1120 struct linphone_upnp_config_list_port_bindings_struct {
1121 struct _LpConfig *lpc;
1125 static void linphone_upnp_config_list_port_bindings_cb(const char *entry, struct linphone_upnp_config_list_port_bindings_struct *cookie) {
1126 char protocol_str[4]; // TCP or UDP
1127 upnp_igd_ip_protocol protocol;
1130 bool_t valid = TRUE;
1131 UpnpPortBinding *port;
1132 if(sscanf(entry, "%3s-%i-%i", protocol_str, &external_port, &local_port) == 3) {
1133 if(strcasecmp(protocol_str, "TCP") == 0) {
1134 protocol = UPNP_IGD_IP_PROTOCOL_TCP;
1135 } else if(strcasecmp(protocol_str, "UDP") == 0) {
1136 protocol = UPNP_IGD_IP_PROTOCOL_UDP;
1141 port = linphone_upnp_port_binding_new();
1142 port->state = LinphoneUpnpStateOk;
1143 port->protocol = protocol;
1144 port->external_port = external_port;
1145 port->local_port = local_port;
1146 cookie->retList = ms_list_append(cookie->retList, port);
1152 ms_warning("uPnP configuration invalid line: %s", entry);
1156 MSList *linphone_upnp_config_list_port_bindings(struct _LpConfig *lpc) {
1157 struct linphone_upnp_config_list_port_bindings_struct cookie = {lpc, NULL};
1158 lp_config_for_each_entry(lpc, UPNP_SECTION_NAME, (void(*)(const char *, void*))linphone_upnp_config_list_port_bindings_cb, &cookie);
1159 return cookie.retList;
1162 void linphone_upnp_config_add_port_binding(UpnpContext *lupnp, const UpnpPortBinding *port) {
1164 UpnpPortBinding *list_port;
1166 list = lupnp->removing_configs;
1167 while(list != NULL) {
1168 list_port = (UpnpPortBinding *)list->data;
1169 if(linphone_upnp_port_binding_equal(list_port, port) == TRUE) {
1170 lupnp->removing_configs = ms_list_remove(lupnp->removing_configs, list_port);
1171 linphone_upnp_port_binding_release(list_port);
1174 list = ms_list_next(list);
1177 list = lupnp->adding_configs;
1178 while(list != NULL) {
1179 list_port = (UpnpPortBinding *)list->data;
1180 if(linphone_upnp_port_binding_equal(list_port, port) == TRUE) {
1183 list = ms_list_next(list);
1186 list_port = linphone_upnp_port_binding_copy(port);
1187 lupnp->adding_configs = ms_list_append(lupnp->adding_configs, list_port);
1190 void linphone_upnp_config_remove_port_binding(UpnpContext *lupnp, const UpnpPortBinding *port) {
1192 UpnpPortBinding *list_port;
1194 list = lupnp->adding_configs;
1195 while(list != NULL) {
1196 list_port = (UpnpPortBinding *)list->data;
1197 if(linphone_upnp_port_binding_equal(list_port, port) == TRUE) {
1198 lupnp->adding_configs = ms_list_remove(lupnp->adding_configs, list_port);
1199 linphone_upnp_port_binding_release(list_port);
1202 list = ms_list_next(list);
1205 list = lupnp->removing_configs;
1206 while(list != NULL) {
1207 list_port = (UpnpPortBinding *)list->data;
1208 if(linphone_upnp_port_binding_equal(list_port, port) == TRUE) {
1211 list = ms_list_next(list);
1214 list_port = linphone_upnp_port_binding_copy(port);
1215 lupnp->removing_configs = ms_list_append(lupnp->removing_configs, list_port);