srtp: negotiate encryption and apply keys to backend
[siplcs.git] / src / core / sipe-media.c
bloba35b8b442b8fb3fe9affca6da285e2e2fd503a06
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
6 * Copyright (C) 2011-2015 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010 Jakub Adam <jakub.adam@ktknet.cz>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <glib.h>
34 #include "sipe-common.h"
35 #include "sipmsg.h"
36 #include "sip-transport.h"
37 #include "sipe-backend.h"
38 #include "sdpmsg.h"
39 #include "sipe-chat.h"
40 #include "sipe-conf.h"
41 #include "sipe-core.h"
42 #include "sipe-core-private.h"
43 #include "sipe-dialog.h"
44 #include "sipe-media.h"
45 #include "sipe-ocs2007.h"
46 #include "sipe-session.h"
47 #include "sipe-utils.h"
48 #include "sipe-nls.h"
49 #include "sipe-schedule.h"
50 #include "sipe-xml.h"
52 struct sipe_media_call_private {
53 struct sipe_media_call public;
55 /* private part starts here */
56 struct sipe_core_private *sipe_private;
58 GSList *streams;
60 struct sipmsg *invitation;
61 SipeIceVersion ice_version;
62 gboolean encryption_compatible;
64 struct sdpmsg *smsg;
65 GSList *failed_media;
67 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
68 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
70 struct sipe_media_stream_private {
71 struct sipe_media_stream public;
73 guchar *encryption_key;
75 #define SIPE_MEDIA_STREAM ((struct sipe_media_stream *) stream_private)
76 #define SIPE_MEDIA_STREAM_PRIVATE ((struct sipe_media_stream_private *) stream)
78 static void sipe_media_codec_list_free(GList *codecs)
80 for (; codecs; codecs = g_list_delete_link(codecs, codecs))
81 sipe_backend_codec_free(codecs->data);
84 static void sipe_media_candidate_list_free(GList *candidates)
86 for (; candidates; candidates = g_list_delete_link(candidates, candidates))
87 sipe_backend_candidate_free(candidates->data);
90 static void
91 remove_stream(struct sipe_media_call* call,
92 struct sipe_media_stream_private *stream_private)
94 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
95 call_private->streams =
96 g_slist_remove(call_private->streams, stream_private);
97 sipe_backend_media_stream_free(SIPE_MEDIA_STREAM->backend_private);
98 g_free(SIPE_MEDIA_STREAM->id);
99 g_free(stream_private->encryption_key);
100 g_free(stream_private);
103 static void
104 sipe_media_call_free(struct sipe_media_call_private *call_private)
106 if (call_private) {
107 struct sip_session *session;
109 while (call_private->streams) {
110 remove_stream(SIPE_MEDIA_CALL,
111 call_private->streams->data);
114 sipe_backend_media_free(call_private->public.backend_private);
116 session = sipe_session_find_call(call_private->sipe_private,
117 SIPE_MEDIA_CALL->with);
118 if (session)
119 sipe_session_remove(call_private->sipe_private, session);
121 if (call_private->invitation)
122 sipmsg_free(call_private->invitation);
124 sdpmsg_free(call_private->smsg);
125 sipe_utils_slist_free_full(call_private->failed_media,
126 (GDestroyNotify)sdpmedia_free);
127 g_free(SIPE_MEDIA_CALL->with);
128 g_free(call_private);
132 static gint
133 candidate_sort_cb(struct sdpcandidate *c1, struct sdpcandidate *c2)
135 int cmp = sipe_strcompare(c1->foundation, c2->foundation);
136 if (cmp == 0) {
137 cmp = sipe_strcompare(c1->username, c2->username);
138 if (cmp == 0)
139 cmp = c1->component - c2->component;
142 return cmp;
145 static GSList *
146 backend_candidates_to_sdpcandidate(GList *candidates)
148 GSList *result = NULL;
149 GList *i;
151 for (i = candidates; i; i = i->next) {
152 struct sipe_backend_candidate *candidate = i->data;
153 struct sdpcandidate *c;
155 gchar *ip = sipe_backend_candidate_get_ip(candidate);
156 gchar *base_ip = sipe_backend_candidate_get_base_ip(candidate);
157 if (is_empty(ip) || strchr(ip, ':') ||
158 (base_ip && strchr(base_ip, ':'))) {
159 /* Ignore IPv6 candidates. */
160 g_free(ip);
161 g_free(base_ip);
162 continue;
165 c = g_new(struct sdpcandidate, 1);
166 c->foundation = sipe_backend_candidate_get_foundation(candidate);
167 c->component = sipe_backend_candidate_get_component_type(candidate);
168 c->type = sipe_backend_candidate_get_type(candidate);
169 c->protocol = sipe_backend_candidate_get_protocol(candidate);
170 c->ip = ip;
171 c->port = sipe_backend_candidate_get_port(candidate);
172 c->base_ip = base_ip;
173 c->base_port = sipe_backend_candidate_get_base_port(candidate);
174 c->priority = sipe_backend_candidate_get_priority(candidate);
175 c->username = sipe_backend_candidate_get_username(candidate);
176 c->password = sipe_backend_candidate_get_password(candidate);
178 result = g_slist_insert_sorted(result, c,
179 (GCompareFunc)candidate_sort_cb);
182 return result;
185 static void
186 get_stream_ip_and_ports(GSList *candidates,
187 gchar **ip, guint *rtp_port, guint *rtcp_port,
188 SipeCandidateType type)
190 *ip = 0;
191 *rtp_port = 0;
192 *rtcp_port = 0;
194 for (; candidates; candidates = candidates->next) {
195 struct sdpcandidate *candidate = candidates->data;
197 if (type == SIPE_CANDIDATE_TYPE_ANY || candidate->type == type) {
198 if (!*ip) {
199 *ip = g_strdup(candidate->ip);
200 } else if (!sipe_strequal(*ip, candidate->ip)) {
201 continue;
204 if (candidate->component == SIPE_COMPONENT_RTP) {
205 *rtp_port = candidate->port;
206 } else if (candidate->component == SIPE_COMPONENT_RTCP)
207 *rtcp_port = candidate->port;
210 if (*rtp_port != 0 && *rtcp_port != 0)
211 return;
215 static gint
216 sdpcodec_compare(gconstpointer a, gconstpointer b)
218 return ((const struct sdpcodec *)a)->id -
219 ((const struct sdpcodec *)b)->id;
222 static GList *
223 remove_wrong_farstream_0_1_tcp_candidates(GList *candidates)
225 GList *i = candidates;
226 GHashTable *foundation_to_candidate = g_hash_table_new_full(g_str_hash,
227 g_str_equal,
228 g_free,
229 NULL);
231 while (i) {
232 GList *next = i->next;
233 struct sipe_backend_candidate *c1 = i->data;
235 if (sipe_backend_candidate_get_protocol(c1) == SIPE_NETWORK_PROTOCOL_UDP) {
236 gchar *foundation = sipe_backend_candidate_get_foundation(c1);
237 struct sipe_backend_candidate *c2 = g_hash_table_lookup(foundation_to_candidate,
238 foundation);
240 if (c2) {
241 g_free(foundation);
243 if (sipe_backend_candidate_get_port(c1) ==
244 sipe_backend_candidate_get_port(c2) ||
245 (sipe_backend_candidate_get_type(c1) !=
246 SIPE_CANDIDATE_TYPE_HOST &&
247 sipe_backend_candidate_get_base_port(c1) ==
248 sipe_backend_candidate_get_base_port(c2))) {
250 * We assume that RTP+RTCP UDP pairs
251 * that share the same port are
252 * actually mistagged TCP candidates.
254 candidates = g_list_remove(candidates, c2);
255 candidates = g_list_delete_link(candidates, i);
256 sipe_backend_candidate_free(c1);
257 sipe_backend_candidate_free(c2);
259 } else
260 /* hash table takes ownership of "foundation" */
261 g_hash_table_insert(foundation_to_candidate, foundation, c1);
264 i = next;
267 g_hash_table_destroy(foundation_to_candidate);
269 return candidates;
272 static void
273 fill_zero_tcp_act_ports_from_tcp_pass(GSList *candidates)
275 GSList *i;
276 GHashTable *ip_to_port = g_hash_table_new(g_str_hash, g_str_equal);
278 for (i = candidates; i; i = i->next) {
279 struct sdpcandidate *c = i->data;
280 GSList *j;
282 if (c->protocol == SIPE_NETWORK_PROTOCOL_TCP_PASSIVE &&
283 c->type == SIPE_CANDIDATE_TYPE_HOST) {
284 g_hash_table_insert(ip_to_port, c->ip,
285 &c->port);
288 if (c->protocol != SIPE_NETWORK_PROTOCOL_TCP_ACTIVE) {
289 continue;
292 for (j = candidates; j; j = j->next) {
293 struct sdpcandidate *passive = j->data;
294 if (passive->protocol != SIPE_NETWORK_PROTOCOL_TCP_PASSIVE ||
295 c->type != passive->type) {
296 continue;
299 if (sipe_strequal(c->ip, passive->ip) &&
300 sipe_strequal(c->base_ip, passive->base_ip)) {
301 if (c->port == 0) {
302 c->port = passive->port;
305 if (c->base_port == 0) {
306 c->base_port = passive->base_port;
308 break;
313 /* Fill base ports of all TCP relay candidates using what we have
314 * collected from host candidates. */
315 for (i = candidates; i; i = i->next) {
316 struct sdpcandidate *c = i->data;
317 if (c->type == SIPE_CANDIDATE_TYPE_RELAY && c->base_port == 0) {
318 guint *base_port = (guint*)g_hash_table_lookup(ip_to_port, c->base_ip);
319 if (base_port) {
320 c->base_port = *base_port;
321 } else {
322 SIPE_DEBUG_WARNING("Couldn't determine base port for candidate "
323 "with foundation %s", c->foundation);
328 g_hash_table_destroy(ip_to_port);
331 static SipeEncryptionPolicy
332 get_encryption_policy(struct sipe_core_private *sipe_private)
334 SipeEncryptionPolicy result =
335 sipe_backend_media_get_encryption_policy(SIPE_CORE_PUBLIC);
336 if (result == SIPE_ENCRYPTION_POLICY_OBEY_SERVER) {
337 result = sipe_private->server_av_encryption_policy;
340 return result;
343 static struct sdpmedia *
344 media_stream_to_sdpmedia(struct sipe_media_call_private *call_private,
345 struct sipe_media_stream_private *stream_private)
347 struct sip_session *session =
348 sipe_session_find_call(call_private->sipe_private,
349 SIPE_MEDIA_CALL->with);
350 struct sip_dialog *dialog = session->dialogs->data;
351 struct sdpmedia *sdpmedia = g_new0(struct sdpmedia, 1);
352 GList *codecs = sipe_backend_get_local_codecs(SIPE_MEDIA_CALL,
353 SIPE_MEDIA_STREAM);
354 SipeEncryptionPolicy encryption_policy =
355 get_encryption_policy(call_private->sipe_private);
356 guint rtcp_port = 0;
357 SipeMediaType type;
358 GSList *attributes = NULL;
359 GList *candidates;
360 GList *i;
362 sdpmedia->name = g_strdup(SIPE_MEDIA_STREAM->id);
364 if (sipe_strequal(sdpmedia->name, "audio"))
365 type = SIPE_MEDIA_AUDIO;
366 else if (sipe_strequal(sdpmedia->name, "video"))
367 type = SIPE_MEDIA_VIDEO;
368 else {
369 // TODO: incompatible media, should not happen here
370 g_free(sdpmedia->name);
371 g_free(sdpmedia);
372 sipe_media_codec_list_free(codecs);
373 return(NULL);
376 // Process codecs
377 for (i = codecs; i; i = i->next) {
378 struct sipe_backend_codec *codec = i->data;
379 struct sdpcodec *c = g_new0(struct sdpcodec, 1);
380 GList *params;
382 c->id = sipe_backend_codec_get_id(codec);
383 c->name = sipe_backend_codec_get_name(codec);
384 c->clock_rate = sipe_backend_codec_get_clock_rate(codec);
385 c->type = type;
387 params = sipe_backend_codec_get_optional_parameters(codec);
388 for (; params; params = params->next) {
389 struct sipnameval *param = params->data;
390 struct sipnameval *copy = g_new0(struct sipnameval, 1);
392 copy->name = g_strdup(param->name);
393 copy->value = g_strdup(param->value);
395 c->parameters = g_slist_append(c->parameters, copy);
398 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
399 * type) that must not appear in SDP messages we send. Thus,
400 * let's ignore any codec having the same id as one we already
401 * have in the converted list. */
402 sdpmedia->codecs = sipe_utils_slist_insert_unique_sorted(
403 sdpmedia->codecs, c, sdpcodec_compare,
404 (GDestroyNotify)sdpcodec_free);
407 sipe_media_codec_list_free(codecs);
409 // Process local candidates
410 // If we have established candidate pairs, send them in SDP response.
411 // Otherwise send all available local candidates.
412 candidates = sipe_backend_media_get_active_local_candidates(SIPE_MEDIA_CALL,
413 SIPE_MEDIA_STREAM);
414 if (!candidates) {
415 candidates = sipe_backend_get_local_candidates(SIPE_MEDIA_CALL,
416 SIPE_MEDIA_STREAM);
417 candidates = remove_wrong_farstream_0_1_tcp_candidates(candidates);
420 sdpmedia->candidates = backend_candidates_to_sdpcandidate(candidates);
421 fill_zero_tcp_act_ports_from_tcp_pass(sdpmedia->candidates);
423 sipe_media_candidate_list_free(candidates);
425 get_stream_ip_and_ports(sdpmedia->candidates, &sdpmedia->ip, &sdpmedia->port,
426 &rtcp_port, SIPE_CANDIDATE_TYPE_HOST);
427 // No usable HOST candidates, use any candidate
428 if (sdpmedia->ip == NULL && sdpmedia->candidates) {
429 get_stream_ip_and_ports(sdpmedia->candidates, &sdpmedia->ip, &sdpmedia->port,
430 &rtcp_port, SIPE_CANDIDATE_TYPE_ANY);
433 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM))
434 attributes = sipe_utils_nameval_add(attributes, "inactive", "");
436 if (rtcp_port) {
437 gchar *tmp = g_strdup_printf("%u", rtcp_port);
438 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
439 g_free(tmp);
442 if (encryption_policy != call_private->sipe_private->server_av_encryption_policy) {
443 const gchar *encryption = NULL;
444 switch (encryption_policy) {
445 case SIPE_ENCRYPTION_POLICY_REJECTED:
446 encryption = "rejected";
447 break;
448 case SIPE_ENCRYPTION_POLICY_OPTIONAL:
449 encryption = "optional";
450 break;
451 case SIPE_ENCRYPTION_POLICY_REQUIRED:
452 default:
453 encryption = "required";
454 break;
457 attributes = sipe_utils_nameval_add(attributes, "encryption", encryption);
460 sdpmedia->attributes = attributes;
462 // Process remote candidates
463 candidates = sipe_backend_media_get_active_remote_candidates(SIPE_MEDIA_CALL,
464 SIPE_MEDIA_STREAM);
465 sdpmedia->remote_candidates = backend_candidates_to_sdpcandidate(candidates);
466 sipe_media_candidate_list_free(candidates);
468 sdpmedia->encryption_active = stream_private->encryption_key &&
469 call_private->encryption_compatible &&
470 (dialog->cseq != 0 || call_private->invitation);
472 // Set our key if encryption is enabled.
473 if (stream_private->encryption_key &&
474 encryption_policy != SIPE_ENCRYPTION_POLICY_REJECTED) {
475 sdpmedia->encryption_key = g_memdup(stream_private->encryption_key,
476 SIPE_SRTP_KEY_LEN);
479 return sdpmedia;
482 static struct sdpmsg *
483 sipe_media_to_sdpmsg(struct sipe_media_call_private *call_private)
485 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
486 GSList *streams = call_private->streams;
488 for (; streams; streams = streams->next) {
489 struct sdpmedia *media = media_stream_to_sdpmedia(call_private,
490 streams->data);
491 if (media) {
492 msg->media = g_slist_append(msg->media, media);
494 if (msg->ip == NULL)
495 msg->ip = g_strdup(media->ip);
499 msg->media = g_slist_concat(msg->media, call_private->failed_media);
500 call_private->failed_media = NULL;
502 msg->ice_version = call_private->ice_version;
504 return msg;
507 static void
508 sipe_invite_call(struct sipe_core_private *sipe_private, TransCallback tc)
510 gchar *hdr;
511 gchar *contact;
512 gchar *p_preferred_identity = NULL;
513 gchar *body;
514 struct sipe_media_call_private *call_private = sipe_private->media_call;
515 struct sip_session *session;
516 struct sip_dialog *dialog;
517 struct sdpmsg *msg;
518 gboolean add_2007_fallback = FALSE;
520 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
521 dialog = session->dialogs->data;
522 add_2007_fallback = dialog->cseq == 0 &&
523 call_private->ice_version == SIPE_ICE_RFC_5245 &&
524 !sipe_strequal(SIPE_MEDIA_CALL->with, sipe_private->test_call_bot_uri);
526 contact = get_contact(sipe_private);
528 if (sipe_private->uc_line_uri) {
529 gchar *self = sip_uri_self(sipe_private);
530 p_preferred_identity = g_strdup_printf(
531 "P-Preferred-Identity: <%s>, <%s>\r\n",
532 self, sipe_private->uc_line_uri);
533 g_free(self);
536 hdr = g_strdup_printf(
537 "ms-keep-alive: UAC;hop-hop=yes\r\n"
538 "Contact: %s\r\n"
539 "%s"
540 "Content-Type: %s\r\n",
541 contact,
542 p_preferred_identity ? p_preferred_identity : "",
543 add_2007_fallback ?
544 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
545 : "application/sdp");
546 g_free(contact);
547 g_free(p_preferred_identity);
549 msg = sipe_media_to_sdpmsg(call_private);
550 body = sdpmsg_to_string(msg);
552 if (add_2007_fallback) {
553 gchar *tmp;
554 tmp = g_strdup_printf(
555 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
556 "Content-Type: application/sdp\r\n"
557 "Content-Transfer-Encoding: 7bit\r\n"
558 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
559 "\r\n"
560 "o=- 0 0 IN IP4 %s\r\n"
561 "s=session\r\n"
562 "c=IN IP4 %s\r\n"
563 "m=audio 0 RTP/AVP\r\n"
564 "\r\n"
565 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
566 "Content-Type: application/sdp\r\n"
567 "Content-Transfer-Encoding: 7bit\r\n"
568 "Content-Disposition: session; handling=optional\r\n"
569 "\r\n"
570 "%s"
571 "\r\n"
572 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
573 msg->ip, msg->ip, body);
574 g_free(body);
575 body = tmp;
578 sdpmsg_free(msg);
580 dialog->outgoing_invite = sip_transport_invite(sipe_private,
581 hdr,
582 body,
583 dialog,
584 tc);
586 g_free(body);
587 g_free(hdr);
590 static struct sip_dialog *
591 sipe_media_dialog_init(struct sip_session* session, struct sipmsg *msg)
593 gchar *newTag = gentag();
594 const gchar *oldHeader;
595 gchar *newHeader;
596 struct sip_dialog *dialog;
598 oldHeader = sipmsg_find_header(msg, "To");
599 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
600 sipmsg_remove_header_now(msg, "To");
601 sipmsg_add_header_now(msg, "To", newHeader);
602 g_free(newHeader);
604 dialog = sipe_dialog_add(session);
605 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
606 dialog->with = parse_from(sipmsg_find_header(msg, "From"));
607 sipe_dialog_parse(dialog, msg, FALSE);
609 return dialog;
612 static void
613 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
615 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private);
616 gchar *body = sdpmsg_to_string(msg);
617 sdpmsg_free(msg);
618 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
619 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
620 g_free(body);
623 static gboolean
624 process_invite_call_response(struct sipe_core_private *sipe_private,
625 struct sipmsg *msg,
626 struct transaction *trans);
628 struct sipe_media_stream *
629 sipe_core_media_get_stream_by_id(struct sipe_media_call *call, const gchar *id)
631 GSList *i;
632 for (i = SIPE_MEDIA_CALL_PRIVATE->streams; i; i = i->next) {
633 struct sipe_media_stream *stream = i->data;
634 if (sipe_strequal(stream->id, id))
635 return stream;
637 return NULL;
640 static gboolean
641 update_call_from_remote_sdp(struct sipe_media_call_private* call_private,
642 struct sdpmedia *media)
644 struct sipe_media_stream *stream;
645 GList *backend_candidates = NULL;
646 GList *backend_codecs = NULL;
647 GSList *i;
648 gboolean result = TRUE;
650 stream = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, media->name);
651 if (media->port == 0) {
652 if (stream) {
653 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
655 return TRUE;
658 if (!stream)
659 return FALSE;
661 for (i = media->codecs; i; i = i->next) {
662 struct sdpcodec *c = i->data;
663 struct sipe_backend_codec *codec;
664 GSList *j;
666 codec = sipe_backend_codec_new(c->id,
667 c->name,
668 c->type,
669 c->clock_rate);
671 for (j = c->parameters; j; j = j->next) {
672 struct sipnameval *attr = j->data;
674 sipe_backend_codec_add_optional_parameter(codec,
675 attr->name,
676 attr->value);
679 backend_codecs = g_list_append(backend_codecs, codec);
682 if (media->encryption_key && SIPE_MEDIA_STREAM_PRIVATE->encryption_key) {
683 sipe_backend_media_set_encryption_keys(SIPE_MEDIA_CALL, stream,
684 SIPE_MEDIA_STREAM_PRIVATE->encryption_key,
685 media->encryption_key);
688 result = sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL, stream,
689 backend_codecs);
690 sipe_media_codec_list_free(backend_codecs);
692 if (result == FALSE) {
693 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
694 return FALSE;
697 for (i = media->candidates; i; i = i->next) {
698 struct sdpcandidate *c = i->data;
699 struct sipe_backend_candidate *candidate;
700 candidate = sipe_backend_candidate_new(c->foundation,
701 c->component,
702 c->type,
703 c->protocol,
704 c->ip,
705 c->port,
706 c->username,
707 c->password);
708 sipe_backend_candidate_set_priority(candidate, c->priority);
710 backend_candidates = g_list_append(backend_candidates, candidate);
713 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL, stream,
714 backend_candidates);
715 sipe_media_candidate_list_free(backend_candidates);
717 if (sipe_utils_nameval_find(media->attributes, "inactive")) {
718 sipe_backend_stream_hold(SIPE_MEDIA_CALL, stream, FALSE);
719 } else if (sipe_backend_stream_is_held(stream)) {
720 sipe_backend_stream_unhold(SIPE_MEDIA_CALL, stream, FALSE);
723 return TRUE;
726 static void
727 apply_remote_message(struct sipe_media_call_private* call_private,
728 struct sdpmsg* msg)
730 GSList *i;
732 sipe_utils_slist_free_full(call_private->failed_media, (GDestroyNotify)sdpmedia_free);
733 call_private->failed_media = NULL;
734 call_private->encryption_compatible = TRUE;
736 for (i = msg->media; i; i = i->next) {
737 struct sdpmedia *media = i->data;
738 const gchar *enc_level =
739 sipe_utils_nameval_find(media->attributes, "encryption");
740 if (sipe_strequal(enc_level, "rejected") &&
741 get_encryption_policy(call_private->sipe_private) == SIPE_ENCRYPTION_POLICY_REQUIRED) {
742 call_private->encryption_compatible = FALSE;
745 if (!update_call_from_remote_sdp(call_private, media)) {
746 media->port = 0;
747 call_private->failed_media =
748 g_slist_append(call_private->failed_media, media);
752 /* We need to keep failed medias until response is sent, remove them
753 * from sdpmsg that is to be freed. */
754 for (i = call_private->failed_media; i; i = i->next) {
755 msg->media = g_slist_remove(msg->media, i->data);
759 static gboolean
760 call_initialized(struct sipe_media_call *call)
762 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
763 for (; streams; streams = streams->next) {
764 if (!sipe_backend_stream_initialized(call, streams->data)) {
765 return FALSE;
769 return TRUE;
772 // Sends an invite response when the call is accepted and local candidates were
773 // prepared, otherwise does nothing. If error response is sent, call_private is
774 // disposed before function returns. Returns true when response was sent.
775 static gboolean
776 send_invite_response_if_ready(struct sipe_media_call_private *call_private)
778 struct sipe_backend_media *backend_media;
780 backend_media = call_private->public.backend_private;
782 if (!sipe_backend_media_accepted(backend_media) ||
783 !call_initialized(&call_private->public))
784 return FALSE;
786 if (!call_private->encryption_compatible) {
787 struct sipe_core_private *sipe_private = call_private->sipe_private;
789 sipmsg_add_header(call_private->invitation, "Warning",
790 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
791 sip_transport_response(sipe_private,
792 call_private->invitation,
793 488, "Encryption Levels not compatible",
794 NULL);
795 sipe_backend_media_reject(backend_media, FALSE);
796 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
797 _("Unable to establish a call"),
798 _("Encryption settings of peer are incompatible with ours."));
799 } else {
800 send_response_with_session_description(call_private, 200, "OK");
803 return TRUE;
806 static void
807 stream_initialized_cb(struct sipe_media_call *call,
808 struct sipe_media_stream *stream)
810 if (call_initialized(call)) {
811 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
813 if (sipe_backend_media_is_initiator(call, stream)) {
814 sipe_invite_call(call_private->sipe_private,
815 process_invite_call_response);
816 } else if (call_private->smsg) {
817 struct sdpmsg *smsg = call_private->smsg;
818 call_private->smsg = NULL;
820 apply_remote_message(call_private, smsg);
821 send_invite_response_if_ready(call_private);
822 sdpmsg_free(smsg);
827 static void phone_state_publish(struct sipe_core_private *sipe_private)
829 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
830 sipe_ocs2007_phone_state_publish(sipe_private);
831 } else {
832 // TODO: OCS 2005 support. Is anyone still using it at all?
836 static void
837 stream_end_cb(struct sipe_media_call* call, struct sipe_media_stream* stream)
839 remove_stream(call, SIPE_MEDIA_STREAM_PRIVATE);
842 static void
843 media_end_cb(struct sipe_media_call *call)
845 g_return_if_fail(call);
847 SIPE_MEDIA_CALL_PRIVATE->sipe_private->media_call = NULL;
848 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
849 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
852 static void
853 call_accept_cb(struct sipe_media_call *call, gboolean local)
855 if (local) {
856 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE);
858 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
861 static void
862 call_reject_cb(struct sipe_media_call *call, gboolean local)
864 if (local) {
865 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
866 sip_transport_response(call_private->sipe_private,
867 call_private->invitation,
868 603, "Decline", NULL);
872 static gboolean
873 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
874 struct transaction *trans);
876 static void call_hold_cb(struct sipe_media_call *call,
877 gboolean local,
878 SIPE_UNUSED_PARAMETER gboolean state)
880 if (local)
881 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
882 sipe_media_send_ack);
885 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
887 if (local) {
888 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
889 struct sip_session *session;
890 session = sipe_session_find_call(call_private->sipe_private,
891 call->with);
893 if (session) {
894 sipe_session_close(call_private->sipe_private, session);
899 static void
900 error_cb(struct sipe_media_call *call, gchar *message)
902 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
903 struct sipe_core_private *sipe_private = call_private->sipe_private;
904 gboolean initiator = sipe_backend_media_is_initiator(call, NULL);
905 gboolean accepted = sipe_backend_media_accepted(call->backend_private);
907 gchar *title = g_strdup_printf("Call with %s failed", call->with);
908 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, message);
909 g_free(title);
911 if (!initiator && !accepted) {
912 sip_transport_response(sipe_private,
913 call_private->invitation,
914 488, "Not Acceptable Here", NULL);
917 sipe_backend_media_hangup(call->backend_private, initiator || accepted);
920 static struct sipe_media_call_private *
921 sipe_media_call_new(struct sipe_core_private *sipe_private,
922 const gchar* with, gboolean initiator, SipeIceVersion ice_version)
924 struct sipe_media_call_private *call_private = g_new0(struct sipe_media_call_private, 1);
925 gchar *cname;
927 call_private->sipe_private = sipe_private;
929 cname = g_strdup(sipe_private->contact + 1);
930 cname[strlen(cname) - 1] = '\0';
932 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
933 SIPE_MEDIA_CALL,
934 with,
935 initiator);
936 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
938 call_private->ice_version = ice_version;
939 call_private->encryption_compatible = TRUE;
941 call_private->public.stream_initialized_cb = stream_initialized_cb;
942 call_private->public.stream_end_cb = stream_end_cb;
943 call_private->public.media_end_cb = media_end_cb;
944 call_private->public.call_accept_cb = call_accept_cb;
945 call_private->public.call_reject_cb = call_reject_cb;
946 call_private->public.call_hold_cb = call_hold_cb;
947 call_private->public.call_hangup_cb = call_hangup_cb;
948 call_private->public.error_cb = error_cb;
950 g_free(cname);
952 return call_private;
955 void sipe_media_hangup(struct sipe_media_call_private *call_private)
957 if (call_private) {
958 sipe_backend_media_hangup(call_private->public.backend_private,
959 FALSE);
963 static gboolean
964 sipe_media_stream_add(struct sipe_core_private *sipe_private, const gchar *id,
965 const gchar *with, SipeMediaType type,
966 SipeIceVersion ice_version, gboolean initiator)
968 struct sipe_media_call_private *call_private = sipe_private->media_call;
969 struct sipe_media_stream_private *stream_private;
970 struct sipe_backend_media_stream *backend_stream;
971 struct sipe_backend_media_relays *backend_media_relays;
973 backend_media_relays = sipe_backend_media_relays_convert(
974 sipe_private->media_relays,
975 sipe_private->media_relay_username,
976 sipe_private->media_relay_password);
978 backend_stream = sipe_backend_media_add_stream(SIPE_MEDIA_CALL,
979 id, with, type, ice_version,
980 initiator, backend_media_relays);
982 sipe_backend_media_relays_free(backend_media_relays);
984 if (!backend_stream) {
985 return FALSE;
988 stream_private = g_new0(struct sipe_media_stream_private, 1);
989 SIPE_MEDIA_STREAM->id = g_strdup(id);
990 SIPE_MEDIA_STREAM->backend_private = backend_stream;
992 #ifdef HAVE_SRTP
994 int i;
995 stream_private->encryption_key = g_new0(guchar, SIPE_SRTP_KEY_LEN);
996 for (i = 0; i != SIPE_SRTP_KEY_LEN; ++i) {
997 stream_private->encryption_key[i] = rand() & 0xff;
1000 #endif
1002 sipe_private->media_call->streams =
1003 g_slist_append(sipe_private->media_call->streams,
1004 stream_private);
1006 return TRUE;
1009 static void
1010 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
1011 const char *with, SipeIceVersion ice_version,
1012 gboolean with_video)
1014 struct sip_session *session;
1015 struct sip_dialog *dialog;
1017 if (sipe_private->media_call)
1018 return;
1020 sipe_private->media_call = sipe_media_call_new(sipe_private, with, TRUE,
1021 ice_version);
1023 session = sipe_session_add_call(sipe_private, with);
1024 dialog = sipe_dialog_add(session);
1025 dialog->callid = gencallid();
1026 dialog->with = g_strdup(session->with);
1027 dialog->ourtag = gentag();
1029 sipe_private->media_call->public.with = g_strdup(session->with);
1031 if (!sipe_media_stream_add(sipe_private, "audio", with, SIPE_MEDIA_AUDIO,
1032 sipe_private->media_call->ice_version,
1033 TRUE)) {
1034 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1035 _("Error occured"),
1036 _("Error creating audio stream"));
1037 sipe_media_hangup(sipe_private->media_call);
1038 return;
1041 if (with_video &&
1042 !sipe_media_stream_add(sipe_private, "video", with, SIPE_MEDIA_VIDEO,
1043 sipe_private->media_call->ice_version,
1044 TRUE)) {
1045 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1046 _("Error occured"),
1047 _("Error creating video stream"));
1048 sipe_media_hangup(sipe_private->media_call);
1049 return;
1052 // Processing continues in stream_initialized_cb
1055 void
1056 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
1057 const char *with,
1058 gboolean with_video)
1060 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
1061 SIPE_ICE_RFC_5245, with_video);
1064 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
1065 struct sipe_chat_session *chat_session)
1067 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1068 struct sip_session *session;
1069 struct sip_dialog *dialog;
1070 SipeIceVersion ice_version;
1071 gchar **parts;
1072 gchar *av_uri;
1074 if (!sipe_conf_supports_mcu_type(sipe_private, "audio-video")) {
1075 sipe_backend_notify_error(sipe_public, _("Join conference call"),
1076 _("Conference calls are not supported on this server."));
1077 return;
1080 session = sipe_session_find_chat(sipe_private, chat_session);
1082 if (sipe_private->media_call || !session)
1083 return;
1085 session->is_call = TRUE;
1087 parts = g_strsplit(chat_session->id, "app:conf:focus:", 2);
1088 av_uri = g_strjoinv("app:conf:audio-video:", parts);
1089 g_strfreev(parts);
1091 ice_version = SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) ? SIPE_ICE_RFC_5245 :
1092 SIPE_ICE_DRAFT_6;
1094 sipe_private->media_call = sipe_media_call_new(sipe_private, av_uri,
1095 TRUE, ice_version);
1097 session = sipe_session_add_call(sipe_private, av_uri);
1098 dialog = sipe_dialog_add(session);
1099 dialog->callid = gencallid();
1100 dialog->with = g_strdup(session->with);
1101 dialog->ourtag = gentag();
1103 g_free(av_uri);
1105 sipe_private->media_call->public.with = g_strdup(session->with);
1107 if (!sipe_media_stream_add(sipe_private, "audio", dialog->with,
1108 SIPE_MEDIA_AUDIO,
1109 sipe_private->media_call->ice_version,
1110 TRUE)) {
1111 sipe_backend_notify_error(sipe_public,
1112 _("Error occured"),
1113 _("Error creating audio stream"));
1114 sipe_media_hangup(sipe_private->media_call);
1115 sipe_private->media_call = NULL;
1118 // Processing continues in stream_initialized_cb
1121 gboolean sipe_core_media_in_call(struct sipe_core_public *sipe_public)
1123 if (sipe_public) {
1124 return SIPE_CORE_PRIVATE->media_call != NULL;
1126 return FALSE;
1129 static gboolean phone_number_is_valid(const gchar *phone_number)
1131 if (!phone_number || sipe_strequal(phone_number, "")) {
1132 return FALSE;
1135 if (*phone_number == '+') {
1136 ++phone_number;
1139 while (*phone_number != '\0') {
1140 if (!g_ascii_isdigit(*phone_number)) {
1141 return FALSE;
1143 ++phone_number;
1146 return TRUE;
1149 void sipe_core_media_phone_call(struct sipe_core_public *sipe_public,
1150 const gchar *phone_number)
1152 g_return_if_fail(sipe_public);
1154 if (phone_number_is_valid(phone_number)) {
1155 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
1156 phone_number, sipe_public->sip_domain);
1158 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
1160 g_free(phone_uri);
1161 } else {
1162 sipe_backend_notify_error(sipe_public,
1163 _("Unable to establish a call"),
1164 _("Invalid phone number"));
1168 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
1170 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1171 if (!sipe_private->test_call_bot_uri) {
1172 sipe_backend_notify_error(sipe_public,
1173 _("Unable to establish a call"),
1174 _("Audio Test Service is not available."));
1175 return;
1178 sipe_core_media_initiate_call(sipe_public,
1179 sipe_private->test_call_bot_uri, FALSE);
1182 void
1183 process_incoming_invite_call(struct sipe_core_private *sipe_private,
1184 struct sipmsg *msg)
1186 struct sipe_media_call_private *call_private = sipe_private->media_call;
1187 struct sdpmsg *smsg;
1188 gboolean has_new_media = FALSE;
1189 GSList *i;
1191 if (call_private) {
1192 char *self;
1194 if (!is_media_session_msg(call_private, msg)) {
1195 sip_transport_response(sipe_private, msg, 486, "Busy Here", NULL);
1196 return;
1199 self = sip_uri_self(sipe_private);
1200 if (sipe_strequal(SIPE_MEDIA_CALL->with, self)) {
1201 g_free(self);
1202 sip_transport_response(sipe_private, msg, 488, "Not Acceptable Here", NULL);
1203 return;
1205 g_free(self);
1208 smsg = sdpmsg_parse_msg(msg->body);
1209 if (!smsg) {
1210 sip_transport_response(sipe_private, msg,
1211 488, "Not Acceptable Here", NULL);
1212 sipe_media_hangup(call_private);
1213 return;
1216 if (!call_private) {
1217 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1218 struct sip_session *session;
1220 call_private = sipe_media_call_new(sipe_private, with, FALSE, smsg->ice_version);
1221 session = sipe_session_add_call(sipe_private, with);
1222 sipe_media_dialog_init(session, msg);
1224 SIPE_MEDIA_CALL->with = g_strdup(session->with);
1225 sipe_private->media_call = call_private;
1226 g_free(with);
1229 if (call_private->invitation)
1230 sipmsg_free(call_private->invitation);
1231 call_private->invitation = sipmsg_copy(msg);
1233 // Create any new media streams
1234 for (i = smsg->media; i; i = i->next) {
1235 struct sdpmedia *media = i->data;
1236 gchar *id = media->name;
1237 SipeMediaType type;
1239 if ( media->port != 0
1240 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, id)) {
1241 gchar *with;
1243 if (sipe_strequal(id, "audio"))
1244 type = SIPE_MEDIA_AUDIO;
1245 else if (sipe_strequal(id, "video"))
1246 type = SIPE_MEDIA_VIDEO;
1247 else
1248 continue;
1250 with = parse_from(sipmsg_find_header(msg, "From"));
1251 sipe_media_stream_add(sipe_private, id, with, type,
1252 smsg->ice_version, FALSE);
1253 has_new_media = TRUE;
1254 g_free(with);
1258 if (has_new_media) {
1259 sdpmsg_free(call_private->smsg);
1260 call_private->smsg = smsg;
1261 sip_transport_response(sipe_private, call_private->invitation,
1262 180, "Ringing", NULL);
1263 // Processing continues in stream_initialized_cb
1264 } else {
1265 apply_remote_message(call_private, smsg);
1266 send_response_with_session_description(call_private, 200, "OK");
1268 sdpmsg_free(smsg);
1272 void process_incoming_cancel_call(struct sipe_core_private *sipe_private,
1273 struct sipmsg *msg)
1275 struct sipe_media_call_private *call_private = sipe_private->media_call;
1277 // We respond to the CANCEL request with 200 OK response and
1278 // with 487 Request Terminated to the remote INVITE in progress.
1279 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1281 if (call_private->invitation) {
1282 sip_transport_response(sipe_private, call_private->invitation,
1283 487, "Request Terminated", NULL);
1286 sipe_media_hangup(call_private);
1289 static gboolean
1290 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1291 struct sipmsg *msg,
1292 struct transaction *trans)
1294 struct sipe_media_call_private *call_private = sipe_private->media_call;
1295 struct sip_session *session;
1296 struct sip_dialog *dialog;
1297 int tmp_cseq;
1299 if (!is_media_session_msg(call_private, msg))
1300 return FALSE;
1302 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
1303 dialog = session->dialogs->data;
1304 if (!dialog)
1305 return FALSE;
1307 tmp_cseq = dialog->cseq;
1309 dialog->cseq = sip_transaction_cseq(trans) - 1;
1310 sip_transport_ack(sipe_private, dialog);
1311 dialog->cseq = tmp_cseq;
1313 dialog->outgoing_invite = NULL;
1315 return TRUE;
1318 static gboolean
1319 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1320 struct sipmsg *msg,
1321 struct transaction *trans)
1323 if (!sipe_media_send_ack(sipe_private, msg, trans))
1324 return FALSE;
1326 sipe_backend_media_accept(sipe_private->media_call->public.backend_private,
1327 FALSE);
1329 return TRUE;
1332 static void
1333 reinvite_on_candidate_pair_cb(struct sipe_core_public *sipe_public)
1335 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1336 struct sipe_media_call_private *media_call = sipe_private->media_call;
1337 GSList *streams;
1339 if (!media_call)
1340 return;
1342 streams = media_call->streams;
1344 for (; streams; streams = streams->next) {
1345 struct sipe_media_stream *s = streams->data;
1346 GList *remote_candidates = sipe_backend_media_get_active_remote_candidates(&media_call->public, s);
1347 guint components = g_list_length(remote_candidates);
1349 sipe_media_candidate_list_free(remote_candidates);
1351 // We must have candidates for both (RTP + RTCP) components ready
1352 if (components < 2) {
1353 sipe_schedule_mseconds(sipe_private,
1354 "<+media-reinvite-on-candidate-pair>",
1355 NULL,
1356 500,
1357 (sipe_schedule_action) reinvite_on_candidate_pair_cb,
1358 NULL);
1359 return;
1363 sipe_invite_call(sipe_private, sipe_media_send_final_ack);
1366 static gboolean
1367 maybe_retry_call_with_ice_version(struct sipe_core_private *sipe_private,
1368 SipeIceVersion ice_version,
1369 struct transaction *trans)
1371 struct sipe_media_call_private *call_private = sipe_private->media_call;
1373 if (call_private->ice_version != ice_version &&
1374 sip_transaction_cseq(trans) == 1) {
1375 gchar *with = g_strdup(SIPE_MEDIA_CALL->with);
1376 gboolean with_video = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, "video") != NULL;
1378 sipe_media_hangup(call_private);
1379 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1380 ice_version == SIPE_ICE_DRAFT_6 ? 6 : 19);
1381 sipe_media_initiate_call(sipe_private, with, ice_version,
1382 with_video);
1384 g_free(with);
1385 return TRUE;
1388 return FALSE;
1391 static gboolean
1392 process_invite_call_response(struct sipe_core_private *sipe_private,
1393 struct sipmsg *msg,
1394 struct transaction *trans)
1396 const gchar *with;
1397 struct sipe_media_call_private *call_private = sipe_private->media_call;
1398 struct sip_session *session;
1399 struct sip_dialog *dialog;
1400 struct sdpmsg *smsg;
1402 if (!is_media_session_msg(call_private, msg))
1403 return FALSE;
1405 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
1406 dialog = session->dialogs->data;
1408 with = dialog->with;
1410 dialog->outgoing_invite = NULL;
1412 if (msg->response >= 400) {
1413 // Call rejected by remote peer or an error occurred
1414 const gchar *title;
1415 GString *desc = g_string_new("");
1416 gboolean append_responsestr = FALSE;
1418 switch (msg->response) {
1419 case 480: {
1420 title = _("User unavailable");
1422 if (sipmsg_parse_warning(msg, NULL) == 391) {
1423 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1424 } else
1425 g_string_append_printf(desc, _("User %s is not available"), with);
1426 break;
1428 case 603:
1429 case 605:
1430 title = _("Call rejected");
1431 g_string_append_printf(desc, _("User %s rejected call"), with);
1432 break;
1433 case 415:
1434 // OCS/Lync really sends response string with 'Mutipart' typo.
1435 if (sipe_strequal(msg->responsestr, "Mutipart mime in content type not supported by Archiving CDR service") &&
1436 maybe_retry_call_with_ice_version(sipe_private, SIPE_ICE_DRAFT_6, trans)) {
1437 return TRUE;
1439 title = _("Unsupported media type");
1440 break;
1441 case 488: {
1442 /* Check for incompatible encryption levels error.
1444 * MS Lync 2010:
1445 * 488 Not Acceptable Here
1446 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1448 * older clients (and SIPE itself):
1449 * 488 Encryption Levels not compatible
1451 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
1452 SipeIceVersion retry_ice_version = SIPE_ICE_DRAFT_6;
1454 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
1455 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
1456 title = _("Unable to establish a call");
1457 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
1458 break;
1461 /* Check if this is failed conference using
1462 * ICEv6 with reason "Error parsing SDP" and
1463 * retry using ICEv19. */
1464 ms_diag = sipmsg_find_header(msg, "ms-diagnostics");
1465 if (ms_diag && g_str_has_prefix(ms_diag, "7008;")) {
1466 retry_ice_version = SIPE_ICE_RFC_5245;
1469 if (maybe_retry_call_with_ice_version(sipe_private, retry_ice_version, trans)) {
1470 return TRUE;
1472 // Break intentionally omitted
1474 default:
1475 title = _("Error occured");
1476 g_string_append(desc, _("Unable to establish a call"));
1477 append_responsestr = TRUE;
1478 break;
1481 if (append_responsestr) {
1482 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
1484 g_string_append_printf(desc, "\n%d %s",
1485 msg->response, msg->responsestr);
1486 if (reason) {
1487 g_string_append_printf(desc, "\n\n%s", reason);
1488 g_free(reason);
1492 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
1493 g_string_free(desc, TRUE);
1495 sipe_media_send_ack(sipe_private, msg, trans);
1496 sipe_media_hangup(call_private);
1498 return TRUE;
1501 sipe_dialog_parse(dialog, msg, TRUE);
1502 smsg = sdpmsg_parse_msg(msg->body);
1503 if (!smsg) {
1504 sip_transport_response(sipe_private, msg,
1505 488, "Not Acceptable Here", NULL);
1506 sipe_media_hangup(call_private);
1507 return FALSE;
1510 apply_remote_message(call_private, smsg);
1511 sdpmsg_free(smsg);
1513 sipe_media_send_ack(sipe_private, msg, trans);
1514 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC);
1516 return TRUE;
1519 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1520 struct sipmsg *msg)
1522 if (call_private) {
1523 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1524 struct sip_session *session;
1526 session = sipe_session_find_call(call_private->sipe_private,
1527 SIPE_MEDIA_CALL->with);
1528 if (session) {
1529 struct sip_dialog *dialog = session->dialogs->data;
1530 return sipe_strequal(dialog->callid, callid);
1533 return FALSE;
1536 void sipe_media_handle_going_offline(struct sipe_media_call_private *call_private)
1538 struct sipe_backend_media *backend_private;
1540 backend_private = call_private->public.backend_private;
1542 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL, NULL) &&
1543 !sipe_backend_media_accepted(backend_private)) {
1544 sip_transport_response(call_private->sipe_private,
1545 call_private->invitation,
1546 480, "Temporarily Unavailable", NULL);
1547 } else {
1548 struct sip_session *session;
1550 session = sipe_session_find_call(call_private->sipe_private,
1551 SIPE_MEDIA_CALL->with);
1552 if (session)
1553 sipe_session_close(call_private->sipe_private, session);
1556 sipe_media_hangup(call_private);
1559 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
1561 return g_strstr_len(SIPE_MEDIA_CALL->with, -1, "app:conf:audio-video:") != NULL;
1564 static void
1565 sipe_media_relay_free(struct sipe_media_relay *relay)
1567 g_free(relay->hostname);
1568 if (relay->dns_query)
1569 sipe_backend_dns_query_cancel(relay->dns_query);
1570 g_free(relay);
1573 void
1574 sipe_media_relay_list_free(GSList *list)
1576 for (; list; list = g_slist_delete_link(list, list))
1577 sipe_media_relay_free(list->data);
1580 static void
1581 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1582 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1584 gchar *hostname = relay->hostname;
1585 relay->dns_query = NULL;
1587 if (ip && port) {
1588 relay->hostname = g_strdup(ip);
1589 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1590 } else {
1591 relay->hostname = NULL;
1592 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1595 g_free(hostname);
1598 static gboolean
1599 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1600 struct sipmsg *msg,
1601 SIPE_UNUSED_PARAMETER struct transaction *trans)
1603 g_free(sipe_private->media_relay_username);
1604 g_free(sipe_private->media_relay_password);
1605 sipe_media_relay_list_free(sipe_private->media_relays);
1606 sipe_private->media_relay_username = NULL;
1607 sipe_private->media_relay_password = NULL;
1608 sipe_private->media_relays = NULL;
1610 if (msg->response >= 400) {
1611 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1612 "Failed to obtain A/V Edge credentials.");
1613 return FALSE;
1616 if (msg->response == 200) {
1617 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1619 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1620 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1621 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1622 const sipe_xml *item;
1623 GSList *relays = NULL;
1625 item = sipe_xml_child(xn_credentials, "username");
1626 sipe_private->media_relay_username = sipe_xml_data(item);
1627 item = sipe_xml_child(xn_credentials, "password");
1628 sipe_private->media_relay_password = sipe_xml_data(item);
1630 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1631 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1632 const sipe_xml *node;
1633 gchar *tmp;
1635 node = sipe_xml_child(item, "hostName");
1636 relay->hostname = sipe_xml_data(node);
1638 node = sipe_xml_child(item, "udpPort");
1639 if (node) {
1640 relay->udp_port = atoi(tmp = sipe_xml_data(node));
1641 g_free(tmp);
1644 node = sipe_xml_child(item, "tcpPort");
1645 if (node) {
1646 relay->tcp_port = atoi(tmp = sipe_xml_data(node));
1647 g_free(tmp);
1650 relays = g_slist_append(relays, relay);
1652 relay->dns_query = sipe_backend_dns_query_a(
1653 SIPE_CORE_PUBLIC,
1654 relay->hostname,
1655 relay->udp_port,
1656 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1657 relay);
1659 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1660 relay->hostname,
1661 relay->tcp_port, relay->udp_port);
1664 sipe_private->media_relays = relays;
1667 sipe_xml_free(xn_response);
1670 return TRUE;
1673 void
1674 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1676 // TODO: re-request credentials after duration expires?
1677 static const char CRED_REQUEST_XML[] =
1678 "<request requestID=\"%d\" "
1679 "from=\"%s\" "
1680 "version=\"1.0\" "
1681 "to=\"%s\" "
1682 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1683 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1684 "<credentialsRequest credentialsRequestID=\"%d\">"
1685 "<identity>%s</identity>"
1686 "<location>%s</location>"
1687 "<duration>480</duration>"
1688 "</credentialsRequest>"
1689 "</request>";
1691 int request_id = rand();
1692 gchar *self;
1693 gchar *body;
1695 if (!sipe_private->mras_uri)
1696 return;
1698 self = sip_uri_self(sipe_private);
1700 body = g_strdup_printf(
1701 CRED_REQUEST_XML,
1702 request_id,
1703 self,
1704 sipe_private->mras_uri,
1705 request_id,
1706 self,
1707 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1708 g_free(self);
1710 sip_transport_service(sipe_private,
1711 sipe_private->mras_uri,
1712 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1713 body,
1714 process_get_av_edge_credentials_response);
1716 g_free(body);
1720 Local Variables:
1721 mode: c
1722 c-file-style: "bsd"
1723 indent-tabs-mode: t
1724 tab-width: 8
1725 End: