media: send 'encryption' SDP attribute based on policy
[siplcs.git] / src / core / sipe-media.c
blob42b744439ecdb0fd10c782877b7c82248c067115
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 sdpmedia *sdpmedia = g_new0(struct sdpmedia, 1);
348 GList *codecs = sipe_backend_get_local_codecs(SIPE_MEDIA_CALL,
349 SIPE_MEDIA_STREAM);
350 SipeEncryptionPolicy encryption_policy =
351 get_encryption_policy(call_private->sipe_private);
352 guint rtcp_port = 0;
353 SipeMediaType type;
354 GSList *attributes = NULL;
355 GList *candidates;
356 GList *i;
358 sdpmedia->name = g_strdup(SIPE_MEDIA_STREAM->id);
360 if (sipe_strequal(sdpmedia->name, "audio"))
361 type = SIPE_MEDIA_AUDIO;
362 else if (sipe_strequal(sdpmedia->name, "video"))
363 type = SIPE_MEDIA_VIDEO;
364 else {
365 // TODO: incompatible media, should not happen here
366 g_free(sdpmedia->name);
367 g_free(sdpmedia);
368 sipe_media_codec_list_free(codecs);
369 return(NULL);
372 // Process codecs
373 for (i = codecs; i; i = i->next) {
374 struct sipe_backend_codec *codec = i->data;
375 struct sdpcodec *c = g_new0(struct sdpcodec, 1);
376 GList *params;
378 c->id = sipe_backend_codec_get_id(codec);
379 c->name = sipe_backend_codec_get_name(codec);
380 c->clock_rate = sipe_backend_codec_get_clock_rate(codec);
381 c->type = type;
383 params = sipe_backend_codec_get_optional_parameters(codec);
384 for (; params; params = params->next) {
385 struct sipnameval *param = params->data;
386 struct sipnameval *copy = g_new0(struct sipnameval, 1);
388 copy->name = g_strdup(param->name);
389 copy->value = g_strdup(param->value);
391 c->parameters = g_slist_append(c->parameters, copy);
394 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
395 * type) that must not appear in SDP messages we send. Thus,
396 * let's ignore any codec having the same id as one we already
397 * have in the converted list. */
398 sdpmedia->codecs = sipe_utils_slist_insert_unique_sorted(
399 sdpmedia->codecs, c, sdpcodec_compare,
400 (GDestroyNotify)sdpcodec_free);
403 sipe_media_codec_list_free(codecs);
405 // Process local candidates
406 // If we have established candidate pairs, send them in SDP response.
407 // Otherwise send all available local candidates.
408 candidates = sipe_backend_media_get_active_local_candidates(SIPE_MEDIA_CALL,
409 SIPE_MEDIA_STREAM);
410 if (!candidates) {
411 candidates = sipe_backend_get_local_candidates(SIPE_MEDIA_CALL,
412 SIPE_MEDIA_STREAM);
413 candidates = remove_wrong_farstream_0_1_tcp_candidates(candidates);
416 sdpmedia->candidates = backend_candidates_to_sdpcandidate(candidates);
417 fill_zero_tcp_act_ports_from_tcp_pass(sdpmedia->candidates);
419 sipe_media_candidate_list_free(candidates);
421 get_stream_ip_and_ports(sdpmedia->candidates, &sdpmedia->ip, &sdpmedia->port,
422 &rtcp_port, SIPE_CANDIDATE_TYPE_HOST);
423 // No usable HOST candidates, use any candidate
424 if (sdpmedia->ip == NULL && sdpmedia->candidates) {
425 get_stream_ip_and_ports(sdpmedia->candidates, &sdpmedia->ip, &sdpmedia->port,
426 &rtcp_port, SIPE_CANDIDATE_TYPE_ANY);
429 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM))
430 attributes = sipe_utils_nameval_add(attributes, "inactive", "");
432 if (rtcp_port) {
433 gchar *tmp = g_strdup_printf("%u", rtcp_port);
434 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
435 g_free(tmp);
438 if (encryption_policy != call_private->sipe_private->server_av_encryption_policy) {
439 const gchar *encryption = NULL;
440 switch (encryption_policy) {
441 case SIPE_ENCRYPTION_POLICY_REJECTED:
442 encryption = "rejected";
443 break;
444 case SIPE_ENCRYPTION_POLICY_OPTIONAL:
445 encryption = "optional";
446 break;
447 case SIPE_ENCRYPTION_POLICY_REQUIRED:
448 default:
449 encryption = "required";
450 break;
453 attributes = sipe_utils_nameval_add(attributes, "encryption", encryption);
456 sdpmedia->attributes = attributes;
458 // Process remote candidates
459 candidates = sipe_backend_media_get_active_remote_candidates(SIPE_MEDIA_CALL,
460 SIPE_MEDIA_STREAM);
461 sdpmedia->remote_candidates = backend_candidates_to_sdpcandidate(candidates);
462 sipe_media_candidate_list_free(candidates);
464 // Set our key if encryption is enabled.
465 if (stream_private->encryption_key &&
466 encryption_policy != SIPE_ENCRYPTION_POLICY_REJECTED) {
467 sdpmedia->encryption_key = g_memdup(stream_private->encryption_key,
468 SIPE_SRTP_KEY_LEN);
471 return sdpmedia;
474 static struct sdpmsg *
475 sipe_media_to_sdpmsg(struct sipe_media_call_private *call_private)
477 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
478 GSList *streams = call_private->streams;
480 for (; streams; streams = streams->next) {
481 struct sdpmedia *media = media_stream_to_sdpmedia(call_private,
482 streams->data);
483 if (media) {
484 msg->media = g_slist_append(msg->media, media);
486 if (msg->ip == NULL)
487 msg->ip = g_strdup(media->ip);
491 msg->media = g_slist_concat(msg->media, call_private->failed_media);
492 call_private->failed_media = NULL;
494 msg->ice_version = call_private->ice_version;
496 return msg;
499 static void
500 sipe_invite_call(struct sipe_core_private *sipe_private, TransCallback tc)
502 gchar *hdr;
503 gchar *contact;
504 gchar *p_preferred_identity = NULL;
505 gchar *body;
506 struct sipe_media_call_private *call_private = sipe_private->media_call;
507 struct sip_session *session;
508 struct sip_dialog *dialog;
509 struct sdpmsg *msg;
510 gboolean add_2007_fallback = FALSE;
512 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
513 dialog = session->dialogs->data;
514 add_2007_fallback = dialog->cseq == 0 &&
515 call_private->ice_version == SIPE_ICE_RFC_5245 &&
516 !sipe_strequal(SIPE_MEDIA_CALL->with, sipe_private->test_call_bot_uri);
518 contact = get_contact(sipe_private);
520 if (sipe_private->uc_line_uri) {
521 gchar *self = sip_uri_self(sipe_private);
522 p_preferred_identity = g_strdup_printf(
523 "P-Preferred-Identity: <%s>, <%s>\r\n",
524 self, sipe_private->uc_line_uri);
525 g_free(self);
528 hdr = g_strdup_printf(
529 "ms-keep-alive: UAC;hop-hop=yes\r\n"
530 "Contact: %s\r\n"
531 "%s"
532 "Content-Type: %s\r\n",
533 contact,
534 p_preferred_identity ? p_preferred_identity : "",
535 add_2007_fallback ?
536 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
537 : "application/sdp");
538 g_free(contact);
539 g_free(p_preferred_identity);
541 msg = sipe_media_to_sdpmsg(call_private);
542 body = sdpmsg_to_string(msg);
544 if (add_2007_fallback) {
545 gchar *tmp;
546 tmp = g_strdup_printf(
547 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
548 "Content-Type: application/sdp\r\n"
549 "Content-Transfer-Encoding: 7bit\r\n"
550 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
551 "\r\n"
552 "o=- 0 0 IN IP4 %s\r\n"
553 "s=session\r\n"
554 "c=IN IP4 %s\r\n"
555 "m=audio 0 RTP/AVP\r\n"
556 "\r\n"
557 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
558 "Content-Type: application/sdp\r\n"
559 "Content-Transfer-Encoding: 7bit\r\n"
560 "Content-Disposition: session; handling=optional\r\n"
561 "\r\n"
562 "%s"
563 "\r\n"
564 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
565 msg->ip, msg->ip, body);
566 g_free(body);
567 body = tmp;
570 sdpmsg_free(msg);
572 dialog->outgoing_invite = sip_transport_invite(sipe_private,
573 hdr,
574 body,
575 dialog,
576 tc);
578 g_free(body);
579 g_free(hdr);
582 static struct sip_dialog *
583 sipe_media_dialog_init(struct sip_session* session, struct sipmsg *msg)
585 gchar *newTag = gentag();
586 const gchar *oldHeader;
587 gchar *newHeader;
588 struct sip_dialog *dialog;
590 oldHeader = sipmsg_find_header(msg, "To");
591 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
592 sipmsg_remove_header_now(msg, "To");
593 sipmsg_add_header_now(msg, "To", newHeader);
594 g_free(newHeader);
596 dialog = sipe_dialog_add(session);
597 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
598 dialog->with = parse_from(sipmsg_find_header(msg, "From"));
599 sipe_dialog_parse(dialog, msg, FALSE);
601 return dialog;
604 static void
605 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
607 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private);
608 gchar *body = sdpmsg_to_string(msg);
609 sdpmsg_free(msg);
610 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
611 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
612 g_free(body);
615 static gboolean
616 encryption_levels_compatible(struct sdpmsg *msg)
618 GSList *i;
620 for (i = msg->media; i; i = i->next) {
621 const gchar *enc_level;
622 struct sdpmedia *m = i->data;
624 enc_level = sipe_utils_nameval_find(m->attributes, "encryption");
626 // Decline call if peer requires encryption as we don't support it yet.
627 if (sipe_strequal(enc_level, "required"))
628 return FALSE;
631 return TRUE;
634 static gboolean
635 process_invite_call_response(struct sipe_core_private *sipe_private,
636 struct sipmsg *msg,
637 struct transaction *trans);
639 struct sipe_media_stream *
640 sipe_core_media_get_stream_by_id(struct sipe_media_call *call, const gchar *id)
642 GSList *i;
643 for (i = SIPE_MEDIA_CALL_PRIVATE->streams; i; i = i->next) {
644 struct sipe_media_stream *stream = i->data;
645 if (sipe_strequal(stream->id, id))
646 return stream;
648 return NULL;
651 static gboolean
652 update_call_from_remote_sdp(struct sipe_media_call_private* call_private,
653 struct sdpmedia *media)
655 struct sipe_media_stream *stream;
656 GList *backend_candidates = NULL;
657 GList *backend_codecs = NULL;
658 GSList *i;
659 gboolean result = TRUE;
661 stream = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, media->name);
662 if (media->port == 0) {
663 if (stream) {
664 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
666 return TRUE;
669 if (!stream)
670 return FALSE;
672 for (i = media->codecs; i; i = i->next) {
673 struct sdpcodec *c = i->data;
674 struct sipe_backend_codec *codec;
675 GSList *j;
677 codec = sipe_backend_codec_new(c->id,
678 c->name,
679 c->type,
680 c->clock_rate);
682 for (j = c->parameters; j; j = j->next) {
683 struct sipnameval *attr = j->data;
685 sipe_backend_codec_add_optional_parameter(codec,
686 attr->name,
687 attr->value);
690 backend_codecs = g_list_append(backend_codecs, codec);
693 result = sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL, stream,
694 backend_codecs);
695 sipe_media_codec_list_free(backend_codecs);
697 if (result == FALSE) {
698 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
699 return FALSE;
702 for (i = media->candidates; i; i = i->next) {
703 struct sdpcandidate *c = i->data;
704 struct sipe_backend_candidate *candidate;
705 candidate = sipe_backend_candidate_new(c->foundation,
706 c->component,
707 c->type,
708 c->protocol,
709 c->ip,
710 c->port,
711 c->username,
712 c->password);
713 sipe_backend_candidate_set_priority(candidate, c->priority);
715 backend_candidates = g_list_append(backend_candidates, candidate);
718 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL, stream,
719 backend_candidates);
720 sipe_media_candidate_list_free(backend_candidates);
722 if (sipe_utils_nameval_find(media->attributes, "inactive")) {
723 sipe_backend_stream_hold(SIPE_MEDIA_CALL, stream, FALSE);
724 } else if (sipe_backend_stream_is_held(stream)) {
725 sipe_backend_stream_unhold(SIPE_MEDIA_CALL, stream, FALSE);
728 return TRUE;
731 static void
732 apply_remote_message(struct sipe_media_call_private* call_private,
733 struct sdpmsg* msg)
735 GSList *i;
737 sipe_utils_slist_free_full(call_private->failed_media, (GDestroyNotify)sdpmedia_free);
738 call_private->failed_media = NULL;
740 for (i = msg->media; i; i = i->next) {
741 struct sdpmedia *media = i->data;
742 if (!update_call_from_remote_sdp(call_private, media)) {
743 media->port = 0;
744 call_private->failed_media =
745 g_slist_append(call_private->failed_media, media);
749 /* We need to keep failed medias until response is sent, remove them
750 * from sdpmsg that is to be freed. */
751 for (i = call_private->failed_media; i; i = i->next) {
752 msg->media = g_slist_remove(msg->media, i->data);
755 call_private->encryption_compatible = encryption_levels_compatible(msg);
758 static gboolean
759 call_initialized(struct sipe_media_call *call)
761 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
762 for (; streams; streams = streams->next) {
763 if (!sipe_backend_stream_initialized(call, streams->data)) {
764 return FALSE;
768 return TRUE;
771 // Sends an invite response when the call is accepted and local candidates were
772 // prepared, otherwise does nothing. If error response is sent, call_private is
773 // disposed before function returns. Returns true when response was sent.
774 static gboolean
775 send_invite_response_if_ready(struct sipe_media_call_private *call_private)
777 struct sipe_backend_media *backend_media;
779 backend_media = call_private->public.backend_private;
781 if (!sipe_backend_media_accepted(backend_media) ||
782 !call_initialized(&call_private->public))
783 return FALSE;
785 if (!call_private->encryption_compatible) {
786 struct sipe_core_private *sipe_private = call_private->sipe_private;
788 sipmsg_add_header(call_private->invitation, "Warning",
789 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
790 sip_transport_response(sipe_private,
791 call_private->invitation,
792 488, "Encryption Levels not compatible",
793 NULL);
794 sipe_backend_media_reject(backend_media, FALSE);
795 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
796 _("Unable to establish a call"),
797 _("Encryption settings of peer are incompatible with ours."));
798 } else {
799 send_response_with_session_description(call_private, 200, "OK");
802 return TRUE;
805 static void
806 stream_initialized_cb(struct sipe_media_call *call,
807 struct sipe_media_stream *stream)
809 if (call_initialized(call)) {
810 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
812 if (sipe_backend_media_is_initiator(call, stream)) {
813 sipe_invite_call(call_private->sipe_private,
814 process_invite_call_response);
815 } else if (call_private->smsg) {
816 struct sdpmsg *smsg = call_private->smsg;
817 call_private->smsg = NULL;
819 apply_remote_message(call_private, smsg);
820 send_invite_response_if_ready(call_private);
821 sdpmsg_free(smsg);
826 static void phone_state_publish(struct sipe_core_private *sipe_private)
828 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
829 sipe_ocs2007_phone_state_publish(sipe_private);
830 } else {
831 // TODO: OCS 2005 support. Is anyone still using it at all?
835 static void
836 stream_end_cb(struct sipe_media_call* call, struct sipe_media_stream* stream)
838 remove_stream(call, SIPE_MEDIA_STREAM_PRIVATE);
841 static void
842 media_end_cb(struct sipe_media_call *call)
844 g_return_if_fail(call);
846 SIPE_MEDIA_CALL_PRIVATE->sipe_private->media_call = NULL;
847 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
848 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
851 static void
852 call_accept_cb(struct sipe_media_call *call, gboolean local)
854 if (local) {
855 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE);
857 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
860 static void
861 call_reject_cb(struct sipe_media_call *call, gboolean local)
863 if (local) {
864 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
865 sip_transport_response(call_private->sipe_private,
866 call_private->invitation,
867 603, "Decline", NULL);
871 static gboolean
872 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
873 struct transaction *trans);
875 static void call_hold_cb(struct sipe_media_call *call,
876 gboolean local,
877 SIPE_UNUSED_PARAMETER gboolean state)
879 if (local)
880 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
881 sipe_media_send_ack);
884 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
886 if (local) {
887 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
888 struct sip_session *session;
889 session = sipe_session_find_call(call_private->sipe_private,
890 call->with);
892 if (session) {
893 sipe_session_close(call_private->sipe_private, session);
898 static void
899 error_cb(struct sipe_media_call *call, gchar *message)
901 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
902 struct sipe_core_private *sipe_private = call_private->sipe_private;
903 gboolean initiator = sipe_backend_media_is_initiator(call, NULL);
904 gboolean accepted = sipe_backend_media_accepted(call->backend_private);
906 gchar *title = g_strdup_printf("Call with %s failed", call->with);
907 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, message);
908 g_free(title);
910 if (!initiator && !accepted) {
911 sip_transport_response(sipe_private,
912 call_private->invitation,
913 488, "Not Acceptable Here", NULL);
916 sipe_backend_media_hangup(call->backend_private, initiator || accepted);
919 static struct sipe_media_call_private *
920 sipe_media_call_new(struct sipe_core_private *sipe_private,
921 const gchar* with, gboolean initiator, SipeIceVersion ice_version)
923 struct sipe_media_call_private *call_private = g_new0(struct sipe_media_call_private, 1);
924 gchar *cname;
926 call_private->sipe_private = sipe_private;
928 cname = g_strdup(sipe_private->contact + 1);
929 cname[strlen(cname) - 1] = '\0';
931 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
932 SIPE_MEDIA_CALL,
933 with,
934 initiator);
935 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
937 call_private->ice_version = ice_version;
938 call_private->encryption_compatible = TRUE;
940 call_private->public.stream_initialized_cb = stream_initialized_cb;
941 call_private->public.stream_end_cb = stream_end_cb;
942 call_private->public.media_end_cb = media_end_cb;
943 call_private->public.call_accept_cb = call_accept_cb;
944 call_private->public.call_reject_cb = call_reject_cb;
945 call_private->public.call_hold_cb = call_hold_cb;
946 call_private->public.call_hangup_cb = call_hangup_cb;
947 call_private->public.error_cb = error_cb;
949 g_free(cname);
951 return call_private;
954 void sipe_media_hangup(struct sipe_media_call_private *call_private)
956 if (call_private) {
957 sipe_backend_media_hangup(call_private->public.backend_private,
958 FALSE);
962 static gboolean
963 sipe_media_stream_add(struct sipe_core_private *sipe_private, const gchar *id,
964 const gchar *with, SipeMediaType type,
965 SipeIceVersion ice_version, gboolean initiator)
967 struct sipe_media_call_private *call_private = sipe_private->media_call;
968 struct sipe_media_stream_private *stream_private;
969 struct sipe_backend_media_stream *backend_stream;
970 struct sipe_backend_media_relays *backend_media_relays;
972 backend_media_relays = sipe_backend_media_relays_convert(
973 sipe_private->media_relays,
974 sipe_private->media_relay_username,
975 sipe_private->media_relay_password);
977 backend_stream = sipe_backend_media_add_stream(SIPE_MEDIA_CALL,
978 id, with, type, ice_version,
979 initiator, backend_media_relays);
981 sipe_backend_media_relays_free(backend_media_relays);
983 if (!backend_stream) {
984 return FALSE;
987 stream_private = g_new0(struct sipe_media_stream_private, 1);
988 SIPE_MEDIA_STREAM->id = g_strdup(id);
989 SIPE_MEDIA_STREAM->backend_private = backend_stream;
991 #ifdef HAVE_SRTP
993 int i;
994 stream_private->encryption_key = g_new0(guchar, SIPE_SRTP_KEY_LEN);
995 for (i = 0; i != SIPE_SRTP_KEY_LEN; ++i) {
996 stream_private->encryption_key[i] = rand() & 0xff;
999 #endif
1001 sipe_private->media_call->streams =
1002 g_slist_append(sipe_private->media_call->streams,
1003 stream_private);
1005 return TRUE;
1008 static void
1009 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
1010 const char *with, SipeIceVersion ice_version,
1011 gboolean with_video)
1013 struct sip_session *session;
1014 struct sip_dialog *dialog;
1016 if (sipe_private->media_call)
1017 return;
1019 sipe_private->media_call = sipe_media_call_new(sipe_private, with, TRUE,
1020 ice_version);
1022 session = sipe_session_add_call(sipe_private, with);
1023 dialog = sipe_dialog_add(session);
1024 dialog->callid = gencallid();
1025 dialog->with = g_strdup(session->with);
1026 dialog->ourtag = gentag();
1028 sipe_private->media_call->public.with = g_strdup(session->with);
1030 if (!sipe_media_stream_add(sipe_private, "audio", with, SIPE_MEDIA_AUDIO,
1031 sipe_private->media_call->ice_version,
1032 TRUE)) {
1033 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1034 _("Error occured"),
1035 _("Error creating audio stream"));
1036 sipe_media_hangup(sipe_private->media_call);
1037 return;
1040 if (with_video &&
1041 !sipe_media_stream_add(sipe_private, "video", with, SIPE_MEDIA_VIDEO,
1042 sipe_private->media_call->ice_version,
1043 TRUE)) {
1044 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1045 _("Error occured"),
1046 _("Error creating video stream"));
1047 sipe_media_hangup(sipe_private->media_call);
1048 return;
1051 // Processing continues in stream_initialized_cb
1054 void
1055 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
1056 const char *with,
1057 gboolean with_video)
1059 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
1060 SIPE_ICE_RFC_5245, with_video);
1063 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
1064 struct sipe_chat_session *chat_session)
1066 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1067 struct sip_session *session;
1068 struct sip_dialog *dialog;
1069 SipeIceVersion ice_version;
1070 gchar **parts;
1071 gchar *av_uri;
1073 if (!sipe_conf_supports_mcu_type(sipe_private, "audio-video")) {
1074 sipe_backend_notify_error(sipe_public, _("Join conference call"),
1075 _("Conference calls are not supported on this server."));
1076 return;
1079 session = sipe_session_find_chat(sipe_private, chat_session);
1081 if (sipe_private->media_call || !session)
1082 return;
1084 session->is_call = TRUE;
1086 parts = g_strsplit(chat_session->id, "app:conf:focus:", 2);
1087 av_uri = g_strjoinv("app:conf:audio-video:", parts);
1088 g_strfreev(parts);
1090 ice_version = SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) ? SIPE_ICE_RFC_5245 :
1091 SIPE_ICE_DRAFT_6;
1093 sipe_private->media_call = sipe_media_call_new(sipe_private, av_uri,
1094 TRUE, ice_version);
1096 session = sipe_session_add_call(sipe_private, av_uri);
1097 dialog = sipe_dialog_add(session);
1098 dialog->callid = gencallid();
1099 dialog->with = g_strdup(session->with);
1100 dialog->ourtag = gentag();
1102 g_free(av_uri);
1104 sipe_private->media_call->public.with = g_strdup(session->with);
1106 if (!sipe_media_stream_add(sipe_private, "audio", dialog->with,
1107 SIPE_MEDIA_AUDIO,
1108 sipe_private->media_call->ice_version,
1109 TRUE)) {
1110 sipe_backend_notify_error(sipe_public,
1111 _("Error occured"),
1112 _("Error creating audio stream"));
1113 sipe_media_hangup(sipe_private->media_call);
1114 sipe_private->media_call = NULL;
1117 // Processing continues in stream_initialized_cb
1120 gboolean sipe_core_media_in_call(struct sipe_core_public *sipe_public)
1122 if (sipe_public) {
1123 return SIPE_CORE_PRIVATE->media_call != NULL;
1125 return FALSE;
1128 static gboolean phone_number_is_valid(const gchar *phone_number)
1130 if (!phone_number || sipe_strequal(phone_number, "")) {
1131 return FALSE;
1134 if (*phone_number == '+') {
1135 ++phone_number;
1138 while (*phone_number != '\0') {
1139 if (!g_ascii_isdigit(*phone_number)) {
1140 return FALSE;
1142 ++phone_number;
1145 return TRUE;
1148 void sipe_core_media_phone_call(struct sipe_core_public *sipe_public,
1149 const gchar *phone_number)
1151 g_return_if_fail(sipe_public);
1153 if (phone_number_is_valid(phone_number)) {
1154 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
1155 phone_number, sipe_public->sip_domain);
1157 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
1159 g_free(phone_uri);
1160 } else {
1161 sipe_backend_notify_error(sipe_public,
1162 _("Unable to establish a call"),
1163 _("Invalid phone number"));
1167 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
1169 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1170 if (!sipe_private->test_call_bot_uri) {
1171 sipe_backend_notify_error(sipe_public,
1172 _("Unable to establish a call"),
1173 _("Audio Test Service is not available."));
1174 return;
1177 sipe_core_media_initiate_call(sipe_public,
1178 sipe_private->test_call_bot_uri, FALSE);
1181 void
1182 process_incoming_invite_call(struct sipe_core_private *sipe_private,
1183 struct sipmsg *msg)
1185 struct sipe_media_call_private *call_private = sipe_private->media_call;
1186 struct sdpmsg *smsg;
1187 gboolean has_new_media = FALSE;
1188 GSList *i;
1190 if (call_private) {
1191 char *self;
1193 if (!is_media_session_msg(call_private, msg)) {
1194 sip_transport_response(sipe_private, msg, 486, "Busy Here", NULL);
1195 return;
1198 self = sip_uri_self(sipe_private);
1199 if (sipe_strequal(SIPE_MEDIA_CALL->with, self)) {
1200 g_free(self);
1201 sip_transport_response(sipe_private, msg, 488, "Not Acceptable Here", NULL);
1202 return;
1204 g_free(self);
1207 smsg = sdpmsg_parse_msg(msg->body);
1208 if (!smsg) {
1209 sip_transport_response(sipe_private, msg,
1210 488, "Not Acceptable Here", NULL);
1211 sipe_media_hangup(call_private);
1212 return;
1215 if (!call_private) {
1216 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1217 struct sip_session *session;
1219 call_private = sipe_media_call_new(sipe_private, with, FALSE, smsg->ice_version);
1220 session = sipe_session_add_call(sipe_private, with);
1221 sipe_media_dialog_init(session, msg);
1223 SIPE_MEDIA_CALL->with = g_strdup(session->with);
1224 sipe_private->media_call = call_private;
1225 g_free(with);
1228 if (call_private->invitation)
1229 sipmsg_free(call_private->invitation);
1230 call_private->invitation = sipmsg_copy(msg);
1232 // Create any new media streams
1233 for (i = smsg->media; i; i = i->next) {
1234 struct sdpmedia *media = i->data;
1235 gchar *id = media->name;
1236 SipeMediaType type;
1238 if ( media->port != 0
1239 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, id)) {
1240 gchar *with;
1242 if (sipe_strequal(id, "audio"))
1243 type = SIPE_MEDIA_AUDIO;
1244 else if (sipe_strequal(id, "video"))
1245 type = SIPE_MEDIA_VIDEO;
1246 else
1247 continue;
1249 with = parse_from(sipmsg_find_header(msg, "From"));
1250 sipe_media_stream_add(sipe_private, id, with, type,
1251 smsg->ice_version, FALSE);
1252 has_new_media = TRUE;
1253 g_free(with);
1257 if (has_new_media) {
1258 sdpmsg_free(call_private->smsg);
1259 call_private->smsg = smsg;
1260 sip_transport_response(sipe_private, call_private->invitation,
1261 180, "Ringing", NULL);
1262 // Processing continues in stream_initialized_cb
1263 } else {
1264 apply_remote_message(call_private, smsg);
1265 send_response_with_session_description(call_private, 200, "OK");
1267 sdpmsg_free(smsg);
1271 void process_incoming_cancel_call(struct sipe_core_private *sipe_private,
1272 struct sipmsg *msg)
1274 struct sipe_media_call_private *call_private = sipe_private->media_call;
1276 // We respond to the CANCEL request with 200 OK response and
1277 // with 487 Request Terminated to the remote INVITE in progress.
1278 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1280 if (call_private->invitation) {
1281 sip_transport_response(sipe_private, call_private->invitation,
1282 487, "Request Terminated", NULL);
1285 sipe_media_hangup(call_private);
1288 static gboolean
1289 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1290 struct sipmsg *msg,
1291 struct transaction *trans)
1293 struct sipe_media_call_private *call_private = sipe_private->media_call;
1294 struct sip_session *session;
1295 struct sip_dialog *dialog;
1296 int tmp_cseq;
1298 if (!is_media_session_msg(call_private, msg))
1299 return FALSE;
1301 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
1302 dialog = session->dialogs->data;
1303 if (!dialog)
1304 return FALSE;
1306 tmp_cseq = dialog->cseq;
1308 dialog->cseq = sip_transaction_cseq(trans) - 1;
1309 sip_transport_ack(sipe_private, dialog);
1310 dialog->cseq = tmp_cseq;
1312 dialog->outgoing_invite = NULL;
1314 return TRUE;
1317 static gboolean
1318 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1319 struct sipmsg *msg,
1320 struct transaction *trans)
1322 if (!sipe_media_send_ack(sipe_private, msg, trans))
1323 return FALSE;
1325 sipe_backend_media_accept(sipe_private->media_call->public.backend_private,
1326 FALSE);
1328 return TRUE;
1331 static void
1332 reinvite_on_candidate_pair_cb(struct sipe_core_public *sipe_public)
1334 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1335 struct sipe_media_call_private *media_call = sipe_private->media_call;
1336 GSList *streams;
1338 if (!media_call)
1339 return;
1341 streams = media_call->streams;
1343 for (; streams; streams = streams->next) {
1344 struct sipe_media_stream *s = streams->data;
1345 GList *remote_candidates = sipe_backend_media_get_active_remote_candidates(&media_call->public, s);
1346 guint components = g_list_length(remote_candidates);
1348 sipe_media_candidate_list_free(remote_candidates);
1350 // We must have candidates for both (RTP + RTCP) components ready
1351 if (components < 2) {
1352 sipe_schedule_mseconds(sipe_private,
1353 "<+media-reinvite-on-candidate-pair>",
1354 NULL,
1355 500,
1356 (sipe_schedule_action) reinvite_on_candidate_pair_cb,
1357 NULL);
1358 return;
1362 sipe_invite_call(sipe_private, sipe_media_send_final_ack);
1365 static gboolean
1366 maybe_retry_call_with_ice_version(struct sipe_core_private *sipe_private,
1367 SipeIceVersion ice_version,
1368 struct transaction *trans)
1370 struct sipe_media_call_private *call_private = sipe_private->media_call;
1372 if (call_private->ice_version != ice_version &&
1373 sip_transaction_cseq(trans) == 1) {
1374 gchar *with = g_strdup(SIPE_MEDIA_CALL->with);
1375 gboolean with_video = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, "video") != NULL;
1377 sipe_media_hangup(call_private);
1378 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1379 ice_version == SIPE_ICE_DRAFT_6 ? 6 : 19);
1380 sipe_media_initiate_call(sipe_private, with, ice_version,
1381 with_video);
1383 g_free(with);
1384 return TRUE;
1387 return FALSE;
1390 static gboolean
1391 process_invite_call_response(struct sipe_core_private *sipe_private,
1392 struct sipmsg *msg,
1393 struct transaction *trans)
1395 const gchar *with;
1396 struct sipe_media_call_private *call_private = sipe_private->media_call;
1397 struct sip_session *session;
1398 struct sip_dialog *dialog;
1399 struct sdpmsg *smsg;
1401 if (!is_media_session_msg(call_private, msg))
1402 return FALSE;
1404 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
1405 dialog = session->dialogs->data;
1407 with = dialog->with;
1409 dialog->outgoing_invite = NULL;
1411 if (msg->response >= 400) {
1412 // Call rejected by remote peer or an error occurred
1413 const gchar *title;
1414 GString *desc = g_string_new("");
1415 gboolean append_responsestr = FALSE;
1417 switch (msg->response) {
1418 case 480: {
1419 title = _("User unavailable");
1421 if (sipmsg_parse_warning(msg, NULL) == 391) {
1422 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1423 } else
1424 g_string_append_printf(desc, _("User %s is not available"), with);
1425 break;
1427 case 603:
1428 case 605:
1429 title = _("Call rejected");
1430 g_string_append_printf(desc, _("User %s rejected call"), with);
1431 break;
1432 case 415:
1433 // OCS/Lync really sends response string with 'Mutipart' typo.
1434 if (sipe_strequal(msg->responsestr, "Mutipart mime in content type not supported by Archiving CDR service") &&
1435 maybe_retry_call_with_ice_version(sipe_private, SIPE_ICE_DRAFT_6, trans)) {
1436 return TRUE;
1438 title = _("Unsupported media type");
1439 break;
1440 case 488: {
1441 /* Check for incompatible encryption levels error.
1443 * MS Lync 2010:
1444 * 488 Not Acceptable Here
1445 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1447 * older clients (and SIPE itself):
1448 * 488 Encryption Levels not compatible
1450 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
1451 SipeIceVersion retry_ice_version = SIPE_ICE_DRAFT_6;
1453 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
1454 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
1455 title = _("Unable to establish a call");
1456 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
1457 break;
1460 /* Check if this is failed conference using
1461 * ICEv6 with reason "Error parsing SDP" and
1462 * retry using ICEv19. */
1463 ms_diag = sipmsg_find_header(msg, "ms-diagnostics");
1464 if (ms_diag && g_str_has_prefix(ms_diag, "7008;")) {
1465 retry_ice_version = SIPE_ICE_RFC_5245;
1468 if (maybe_retry_call_with_ice_version(sipe_private, retry_ice_version, trans)) {
1469 return TRUE;
1471 // Break intentionally omitted
1473 default:
1474 title = _("Error occured");
1475 g_string_append(desc, _("Unable to establish a call"));
1476 append_responsestr = TRUE;
1477 break;
1480 if (append_responsestr) {
1481 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
1483 g_string_append_printf(desc, "\n%d %s",
1484 msg->response, msg->responsestr);
1485 if (reason) {
1486 g_string_append_printf(desc, "\n\n%s", reason);
1487 g_free(reason);
1491 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
1492 g_string_free(desc, TRUE);
1494 sipe_media_send_ack(sipe_private, msg, trans);
1495 sipe_media_hangup(call_private);
1497 return TRUE;
1500 sipe_dialog_parse(dialog, msg, TRUE);
1501 smsg = sdpmsg_parse_msg(msg->body);
1502 if (!smsg) {
1503 sip_transport_response(sipe_private, msg,
1504 488, "Not Acceptable Here", NULL);
1505 sipe_media_hangup(call_private);
1506 return FALSE;
1509 apply_remote_message(call_private, smsg);
1510 sdpmsg_free(smsg);
1512 sipe_media_send_ack(sipe_private, msg, trans);
1513 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC);
1515 return TRUE;
1518 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1519 struct sipmsg *msg)
1521 if (call_private) {
1522 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1523 struct sip_session *session;
1525 session = sipe_session_find_call(call_private->sipe_private,
1526 SIPE_MEDIA_CALL->with);
1527 if (session) {
1528 struct sip_dialog *dialog = session->dialogs->data;
1529 return sipe_strequal(dialog->callid, callid);
1532 return FALSE;
1535 void sipe_media_handle_going_offline(struct sipe_media_call_private *call_private)
1537 struct sipe_backend_media *backend_private;
1539 backend_private = call_private->public.backend_private;
1541 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL, NULL) &&
1542 !sipe_backend_media_accepted(backend_private)) {
1543 sip_transport_response(call_private->sipe_private,
1544 call_private->invitation,
1545 480, "Temporarily Unavailable", NULL);
1546 } else {
1547 struct sip_session *session;
1549 session = sipe_session_find_call(call_private->sipe_private,
1550 SIPE_MEDIA_CALL->with);
1551 if (session)
1552 sipe_session_close(call_private->sipe_private, session);
1555 sipe_media_hangup(call_private);
1558 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
1560 return g_strstr_len(SIPE_MEDIA_CALL->with, -1, "app:conf:audio-video:") != NULL;
1563 static void
1564 sipe_media_relay_free(struct sipe_media_relay *relay)
1566 g_free(relay->hostname);
1567 if (relay->dns_query)
1568 sipe_backend_dns_query_cancel(relay->dns_query);
1569 g_free(relay);
1572 void
1573 sipe_media_relay_list_free(GSList *list)
1575 for (; list; list = g_slist_delete_link(list, list))
1576 sipe_media_relay_free(list->data);
1579 static void
1580 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1581 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1583 gchar *hostname = relay->hostname;
1584 relay->dns_query = NULL;
1586 if (ip && port) {
1587 relay->hostname = g_strdup(ip);
1588 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1589 } else {
1590 relay->hostname = NULL;
1591 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1594 g_free(hostname);
1597 static gboolean
1598 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1599 struct sipmsg *msg,
1600 SIPE_UNUSED_PARAMETER struct transaction *trans)
1602 g_free(sipe_private->media_relay_username);
1603 g_free(sipe_private->media_relay_password);
1604 sipe_media_relay_list_free(sipe_private->media_relays);
1605 sipe_private->media_relay_username = NULL;
1606 sipe_private->media_relay_password = NULL;
1607 sipe_private->media_relays = NULL;
1609 if (msg->response >= 400) {
1610 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1611 "Failed to obtain A/V Edge credentials.");
1612 return FALSE;
1615 if (msg->response == 200) {
1616 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1618 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1619 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1620 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1621 const sipe_xml *item;
1622 GSList *relays = NULL;
1624 item = sipe_xml_child(xn_credentials, "username");
1625 sipe_private->media_relay_username = sipe_xml_data(item);
1626 item = sipe_xml_child(xn_credentials, "password");
1627 sipe_private->media_relay_password = sipe_xml_data(item);
1629 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1630 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1631 const sipe_xml *node;
1632 gchar *tmp;
1634 node = sipe_xml_child(item, "hostName");
1635 relay->hostname = sipe_xml_data(node);
1637 node = sipe_xml_child(item, "udpPort");
1638 if (node) {
1639 relay->udp_port = atoi(tmp = sipe_xml_data(node));
1640 g_free(tmp);
1643 node = sipe_xml_child(item, "tcpPort");
1644 if (node) {
1645 relay->tcp_port = atoi(tmp = sipe_xml_data(node));
1646 g_free(tmp);
1649 relays = g_slist_append(relays, relay);
1651 relay->dns_query = sipe_backend_dns_query_a(
1652 SIPE_CORE_PUBLIC,
1653 relay->hostname,
1654 relay->udp_port,
1655 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1656 relay);
1658 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1659 relay->hostname,
1660 relay->tcp_port, relay->udp_port);
1663 sipe_private->media_relays = relays;
1666 sipe_xml_free(xn_response);
1669 return TRUE;
1672 void
1673 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1675 // TODO: re-request credentials after duration expires?
1676 static const char CRED_REQUEST_XML[] =
1677 "<request requestID=\"%d\" "
1678 "from=\"%s\" "
1679 "version=\"1.0\" "
1680 "to=\"%s\" "
1681 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1682 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1683 "<credentialsRequest credentialsRequestID=\"%d\">"
1684 "<identity>%s</identity>"
1685 "<location>%s</location>"
1686 "<duration>480</duration>"
1687 "</credentialsRequest>"
1688 "</request>";
1690 int request_id = rand();
1691 gchar *self;
1692 gchar *body;
1694 if (!sipe_private->mras_uri)
1695 return;
1697 self = sip_uri_self(sipe_private);
1699 body = g_strdup_printf(
1700 CRED_REQUEST_XML,
1701 request_id,
1702 self,
1703 sipe_private->mras_uri,
1704 request_id,
1705 self,
1706 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1707 g_free(self);
1709 sip_transport_service(sipe_private,
1710 sipe_private->mras_uri,
1711 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1712 body,
1713 process_get_av_edge_credentials_response);
1715 g_free(body);
1719 Local Variables:
1720 mode: c
1721 c-file-style: "bsd"
1722 indent-tabs-mode: t
1723 tab-width: 8
1724 End: