6 * Copyright (C) 2013-2017 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
30 #include "sipe-backend.h"
31 #include "sipe-core.h"
33 #include "sipe-utils.h"
36 append_attribute(struct sdpmedia
*media
, gchar
*attr
)
38 gchar
**parts
= g_strsplit(attr
+ 2, ":", 2);
45 media
->attributes
= sipe_utils_nameval_add(media
->attributes
,
47 parts
[1] ? parts
[1] : "");
53 parse_attributes(struct sdpmsg
*smsg
, const gchar
*msg
) {
54 gchar
**lines
= g_strsplit(msg
, "\r\n", 0);
57 while (*ptr
!= NULL
) {
58 if (g_str_has_prefix(*ptr
, "o=")) {
59 gchar
**parts
= g_strsplit(*ptr
+ 2, " ", 6);
60 if (g_strv_length(parts
) != 6) {
66 smsg
->ip
= g_strdup(parts
[5]);
68 } else if (g_str_has_prefix(*ptr
, "m=")) {
70 struct sdpmedia
*media
;
72 parts
= g_strsplit(*ptr
+ 2, " ", 3);
73 if (g_strv_length(parts
) < 3) {
79 media
= g_new0(struct sdpmedia
, 1);
81 smsg
->media
= g_slist_append(smsg
->media
, media
);
83 media
->name
= g_strdup(parts
[0]);
84 media
->port
= atoi(parts
[1]);
85 media
->encryption_active
=
86 g_strstr_len(parts
[2], -1, "/SAVP") != NULL
;
90 while (*(++ptr
) && !g_str_has_prefix(*ptr
, "m=")) {
92 if (g_str_has_prefix(*ptr
, "a=")) {
93 if (!append_attribute(media
, *ptr
)) {
110 static struct sdpcandidate
* sdpcandidate_copy(struct sdpcandidate
*candidate
);
112 static SipeComponentType
113 parse_component(const gchar
*str
)
116 case 1: return SIPE_COMPONENT_RTP
;
117 case 2: return SIPE_COMPONENT_RTCP
;
118 default: return SIPE_COMPONENT_NONE
;
123 base64_pad(const gchar
* str
)
125 size_t str_len
= strlen(str
);
126 int mod
= str_len
% 4;
129 gchar
*result
= NULL
;
131 gchar
*ptr
= result
= g_malloc(str_len
+ pad
+ 1);
133 memcpy(ptr
, str
, str_len
);
135 memset(ptr
, '=', pad
);
141 return g_strdup(str
);
145 parse_append_candidate_draft_6(gchar
**tokens
, GSList
**candidates
)
147 struct sdpcandidate
*candidate
;
149 if (g_strv_length(tokens
) < 7 || strlen(tokens
[4]) < 3) {
153 candidate
= g_new0(struct sdpcandidate
, 1);
155 candidate
->username
= base64_pad(tokens
[0]);
156 candidate
->component
= parse_component(tokens
[1]);
157 candidate
->password
= base64_pad(tokens
[2]);
159 if (sipe_strequal(tokens
[3], "UDP"))
160 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_UDP
;
161 else if (sipe_strequal(tokens
[3], "TCP"))
162 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
;
164 sdpcandidate_free(candidate
);
168 candidate
->priority
= atoi(tokens
[4] + 2);
169 candidate
->ip
= g_strdup(tokens
[5]);
170 candidate
->port
= atoi(tokens
[6]);
172 *candidates
= g_slist_append(*candidates
, candidate
);
174 // draft 6 candidates are both active and passive
175 if (candidate
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
) {
176 candidate
= sdpcandidate_copy(candidate
);
177 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
;
178 *candidates
= g_slist_append(*candidates
, candidate
);
185 parse_append_candidate_rfc_5245(gchar
**tokens
, GSList
**candidates
)
187 struct sdpcandidate
*candidate
;
189 if (g_strv_length(tokens
) < 8) {
193 candidate
= g_new0(struct sdpcandidate
, 1);
194 candidate
->foundation
= g_strdup(tokens
[0]);
195 candidate
->component
= parse_component(tokens
[1]);
197 if (sipe_strcase_equal(tokens
[2], "UDP"))
198 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_UDP
;
199 else if (sipe_strcase_equal(tokens
[2], "TCP-ACT"))
200 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
;
201 else if (sipe_strcase_equal(tokens
[2], "TCP-PASS"))
202 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
;
204 sdpcandidate_free(candidate
);
208 candidate
->priority
= atoi(tokens
[3]);
209 candidate
->ip
= g_strdup(tokens
[4]);
210 candidate
->port
= atoi(tokens
[5]);
212 if (sipe_strcase_equal(tokens
[7], "host"))
213 candidate
->type
= SIPE_CANDIDATE_TYPE_HOST
;
214 else if (sipe_strcase_equal(tokens
[7], "relay"))
215 candidate
->type
= SIPE_CANDIDATE_TYPE_RELAY
;
216 else if (sipe_strcase_equal(tokens
[7], "srflx"))
217 candidate
->type
= SIPE_CANDIDATE_TYPE_SRFLX
;
218 else if (sipe_strcase_equal(tokens
[7], "prflx"))
219 candidate
->type
= SIPE_CANDIDATE_TYPE_PRFLX
;
221 sdpcandidate_free(candidate
);
225 *candidates
= g_slist_append(*candidates
, candidate
);
231 parse_candidates(GSList
*attrs
, SipeIceVersion
*ice_version
, GSList
**candidates
)
236 g_return_val_if_fail(*candidates
== NULL
, FALSE
);
238 while ((attr
= sipe_utils_nameval_find_instance(attrs
, "candidate", i
++))) {
239 gchar
**tokens
= g_strsplit_set(attr
, " ", 0);
242 if (g_strv_length(tokens
) < 7) {
247 if (sipe_strequal(tokens
[6], "typ")) {
248 parsed_ok
= parse_append_candidate_rfc_5245(tokens
,
251 *ice_version
= SIPE_ICE_RFC_5245
;
253 parsed_ok
= parse_append_candidate_draft_6(tokens
,
256 *ice_version
= SIPE_ICE_DRAFT_6
;
267 *ice_version
= SIPE_ICE_NO_ICE
;
269 if (*ice_version
== SIPE_ICE_RFC_5245
) {
270 const gchar
*username
= sipe_utils_nameval_find(attrs
, "ice-ufrag");
271 const gchar
*password
= sipe_utils_nameval_find(attrs
, "ice-pwd");
273 if (username
&& password
) {
275 for (i
= *candidates
; i
; i
= i
->next
) {
276 struct sdpcandidate
*c
= i
->data
;
277 c
->username
= g_strdup(username
);
278 c
->password
= g_strdup(password
);
287 create_legacy_candidates(gchar
*ip
, guint16 port
)
289 struct sdpcandidate
*candidate
;
290 GSList
*candidates
= NULL
;
292 candidate
= g_new0(struct sdpcandidate
, 1);
293 candidate
->foundation
= g_strdup("1");
294 candidate
->component
= SIPE_COMPONENT_RTP
;
295 candidate
->type
= SIPE_CANDIDATE_TYPE_HOST
;
296 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_UDP
;
297 candidate
->ip
= g_strdup(ip
);
298 candidate
->port
= port
;
300 candidates
= g_slist_append(candidates
, candidate
);
302 candidate
= g_new0(struct sdpcandidate
, 1);
303 candidate
->foundation
= g_strdup("1");
304 candidate
->component
= SIPE_COMPONENT_RTCP
;
305 candidate
->type
= SIPE_CANDIDATE_TYPE_HOST
;
306 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_UDP
;
307 candidate
->ip
= g_strdup(ip
);
308 candidate
->port
= port
+ 1;
310 candidates
= g_slist_append(candidates
, candidate
);
316 parse_codec_parameters(GSList
*attrs
, struct sdpcodec
*codec
)
321 while((params
= sipe_utils_nameval_find_instance(attrs
, "fmtp", i
++))) {
325 tokens
= g_strsplit(params
, " ", 0);
326 if (g_strv_length(tokens
) < 1) {
331 if (atoi(tokens
[0]) != codec
->id
) {
336 for (param
= tokens
+ 1; *param
; ++param
) {
337 gchar
**nameval
= g_strsplit(*param
, "=", 2);
339 if (g_strv_length(nameval
) != 2) {
345 sipe_utils_nameval_add(codec
->parameters
,
360 parse_codecs(GSList
*attrs
, SipeMediaType type
, GSList
**codecs
)
365 while ((attr
= sipe_utils_nameval_find_instance(attrs
, "rtpmap", i
++))) {
366 struct sdpcodec
*codec
;
369 tokens
= g_strsplit_set(attr
, " /", 4);
370 if (g_strv_length(tokens
) < 3) {
375 codec
= g_new0(struct sdpcodec
, 1);
376 codec
->id
= atoi(tokens
[0]);
377 codec
->name
= g_strdup(tokens
[1]);
378 codec
->clock_rate
= atoi(tokens
[2]);
381 if (type
== SIPE_MEDIA_AUDIO
) {
382 codec
->channels
= tokens
[3] ? atoi(tokens
[3]) : 1;
387 if (!parse_codec_parameters(attrs
, codec
)) {
388 sdpcodec_free(codec
);
392 *codecs
= g_slist_append(*codecs
, codec
);
399 parse_encryption_key(GSList
*attrs
, guchar
**key
, int *key_id
)
404 while ((attr
= sipe_utils_nameval_find_instance(attrs
, "crypto", i
++))) {
405 gchar
**tokens
= g_strsplit_set(attr
, " :|", 6);
407 if (tokens
[0] && tokens
[1] && tokens
[2] && tokens
[3] && tokens
[4] &&
408 sipe_strcase_equal(tokens
[1], "AES_CM_128_HMAC_SHA1_80") &&
409 sipe_strequal(tokens
[2], "inline") &&
412 *key
= g_base64_decode(tokens
[3], &key_len
);
413 if (key_len
!= SIPE_SRTP_KEY_LEN
) {
417 *key_id
= atoi(tokens
[0]);
429 sdpmsg_parse_msg(const gchar
*msg
)
431 struct sdpmsg
*smsg
= g_new0(struct sdpmsg
, 1);
434 if (!parse_attributes(smsg
, msg
)) {
439 for (i
= smsg
->media
; i
; i
= i
->next
) {
440 struct sdpmedia
*media
= i
->data
;
443 if (!parse_candidates(media
->attributes
, &smsg
->ice_version
,
444 &media
->candidates
)) {
449 if (!media
->candidates
&& media
->port
!= 0) {
450 // No a=candidate in SDP message, this seems to be MSOC 2005
451 media
->candidates
= create_legacy_candidates(smsg
->ip
, media
->port
);
454 if (sipe_strequal(media
->name
, "audio"))
455 type
= SIPE_MEDIA_AUDIO
;
456 else if (sipe_strequal(media
->name
, "video"))
457 type
= SIPE_MEDIA_VIDEO
;
458 else if (sipe_strequal(media
->name
, "data"))
459 type
= SIPE_MEDIA_APPLICATION
;
460 else if (sipe_strequal(media
->name
, "applicationsharing"))
461 type
= SIPE_MEDIA_APPLICATION
;
463 // Unknown media type
468 if (!parse_codecs(media
->attributes
, type
, &media
->codecs
)) {
473 parse_encryption_key(media
->attributes
, &media
->encryption_key
,
474 &media
->encryption_key_id
);
481 codecs_to_string(GSList
*codecs
)
483 GString
*result
= g_string_new(NULL
);
485 for (; codecs
; codecs
= codecs
->next
) {
486 struct sdpcodec
*c
= codecs
->data
;
487 GSList
*params
= c
->parameters
;
489 g_string_append_printf(result
,
490 "a=rtpmap:%d %s/%d\r\n",
496 GString
*param_str
= g_string_new(NULL
);
497 int written_params
= 0;
499 g_string_append_printf(param_str
, "a=fmtp:%d", c
->id
);
501 for (; params
; params
= params
->next
) {
502 struct sipnameval
* par
= params
->data
;
503 if (sipe_strequal(par
->name
, "farsight-send-profile")) {
504 // Lync AVMCU doesn't like this property.
508 g_string_append_printf(param_str
, " %s=%s",
509 par
->name
, par
->value
);
513 g_string_append(param_str
, "\r\n");
515 if (written_params
> 0) {
516 g_string_append(result
, param_str
->str
);
519 g_string_free(param_str
, TRUE
);
523 return g_string_free(result
, FALSE
);
527 codec_ids_to_string(GSList
*codecs
)
529 GString
*result
= g_string_new(NULL
);
531 for (; codecs
; codecs
= codecs
->next
) {
532 struct sdpcodec
*c
= codecs
->data
;
533 g_string_append_printf(result
, " %d", c
->id
);
536 return g_string_free(result
, FALSE
);
540 base64_unpad(const gchar
*str
)
542 gchar
*result
= g_strdup(str
);
545 for (ptr
= result
+ strlen(result
); ptr
!= result
; --ptr
) {
546 if (*(ptr
- 1) != '=') {
556 candidates_to_string(GSList
*candidates
, SipeIceVersion ice_version
)
558 GString
*result
= g_string_new("");
560 GSList
*processed_tcp_candidates
= NULL
;
562 for (i
= candidates
; i
; i
= i
->next
) {
563 struct sdpcandidate
*c
= i
->data
;
564 const gchar
*protocol
;
566 gchar
*related
= NULL
;
568 if (ice_version
== SIPE_ICE_RFC_5245
) {
570 switch (c
->protocol
) {
571 case SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
:
572 protocol
= "TCP-ACT";
574 case SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
:
575 protocol
= "TCP-PASS";
577 case SIPE_NETWORK_PROTOCOL_UDP
:
581 /* error unknown/unsupported type */
582 protocol
= "UNKNOWN";
587 case SIPE_CANDIDATE_TYPE_HOST
:
590 case SIPE_CANDIDATE_TYPE_RELAY
:
593 case SIPE_CANDIDATE_TYPE_SRFLX
:
596 case SIPE_CANDIDATE_TYPE_PRFLX
:
600 /* error unknown/unsupported type */
606 case SIPE_CANDIDATE_TYPE_RELAY
:
607 case SIPE_CANDIDATE_TYPE_SRFLX
:
608 case SIPE_CANDIDATE_TYPE_PRFLX
:
609 related
= g_strdup_printf("raddr %s rport %d",
617 g_string_append_printf(result
,
618 "a=candidate:%s %u %s %u %s %d typ %s %s\r\n",
626 related
? related
: "");
629 } else if (ice_version
== SIPE_ICE_DRAFT_6
) {
633 switch (c
->protocol
) {
634 case SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
:
635 case SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
: {
636 GSList
*prev_cand
= processed_tcp_candidates
;
637 for (; prev_cand
; prev_cand
= prev_cand
->next
) {
638 struct sdpcandidate
*c2
= (struct sdpcandidate
*)prev_cand
->data
;
640 if (sipe_strequal(c
->ip
, c2
->ip
) &&
641 c
->component
== c2
->component
) {
650 processed_tcp_candidates
=
651 g_slist_append(processed_tcp_candidates
, c
);
655 case SIPE_NETWORK_PROTOCOL_UDP
:
659 /* unknown/unsupported type, ignore */
668 username
= base64_unpad(c
->username
);
669 password
= base64_unpad(c
->password
);
671 g_string_append_printf(result
,
672 "a=candidate:%s %u %s %s 0.%u %s %d\r\n",
686 g_slist_free(processed_tcp_candidates
);
688 return g_string_free(result
, FALSE
);
692 remote_candidates_to_string(GSList
*candidates
, SipeIceVersion ice_version
)
694 GString
*result
= g_string_new("");
697 if (ice_version
== SIPE_ICE_RFC_5245
) {
699 g_string_append(result
, "a=remote-candidates:");
701 for (i
= candidates
; i
; i
= i
->next
) {
702 struct sdpcandidate
*c
= i
->data
;
703 g_string_append_printf(result
, "%u %s %u ",
704 c
->component
, c
->ip
, c
->port
);
707 g_string_append(result
, "\r\n");
708 } else if (ice_version
== SIPE_ICE_DRAFT_6
) {
709 struct sdpcandidate
*c
= candidates
->data
;
710 g_string_append_printf(result
, "a=remote-candidate:%s\r\n",
715 return g_string_free(result
, FALSE
);
719 attributes_to_string(GSList
*attributes
)
721 GString
*result
= g_string_new("");
723 for (; attributes
; attributes
= attributes
->next
) {
724 struct sipnameval
*a
= attributes
->data
;
725 g_string_append_printf(result
, "a=%s", a
->name
);
726 if (!sipe_strequal(a
->value
, ""))
727 g_string_append_printf(result
, ":%s", a
->value
);
728 g_string_append(result
, "\r\n");
731 return g_string_free(result
, FALSE
);
735 media_to_string(const struct sdpmsg
*msg
, const struct sdpmedia
*media
)
739 gchar
*transport_profile
= NULL
;
741 gchar
*media_conninfo
= NULL
;
743 gchar
*codecs_str
= NULL
;
744 gchar
*codec_ids_str
= codec_ids_to_string(media
->codecs
);
746 gchar
*candidates_str
= NULL
;
747 gchar
*remote_candidates_str
= NULL
;
749 gchar
*attributes_str
= NULL
;
750 gchar
*credentials
= NULL
;
752 gchar
*crypto
= NULL
;
754 gboolean uses_tcp_transport
= TRUE
;
756 if (media
->port
!= 0) {
757 if (!sipe_strequal(msg
->ip
, media
->ip
)) {
758 media_conninfo
= g_strdup_printf("c=IN IP4 %s\r\n", media
->ip
);
761 codecs_str
= codecs_to_string(media
->codecs
);
762 candidates_str
= candidates_to_string(media
->candidates
, msg
->ice_version
);
763 remote_candidates_str
= remote_candidates_to_string(media
->remote_candidates
,
766 if (media
->remote_candidates
) {
767 struct sdpcandidate
*c
= media
->remote_candidates
->data
;
769 c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
||
770 c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
||
771 c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_SO
;
773 GSList
*candidates
= media
->candidates
;
774 for (; candidates
; candidates
= candidates
->next
) {
775 struct sdpcandidate
*c
= candidates
->data
;
776 if (c
->protocol
== SIPE_NETWORK_PROTOCOL_UDP
) {
777 uses_tcp_transport
= FALSE
;
783 attributes_str
= attributes_to_string(media
->attributes
);
785 if (msg
->ice_version
== SIPE_ICE_RFC_5245
&& media
->candidates
) {
786 struct sdpcandidate
*c
= media
->candidates
->data
;
788 credentials
= g_strdup_printf("a=ice-ufrag:%s\r\n"
794 if (media
->encryption_key
) {
795 gchar
*key_encoded
= g_base64_encode(media
->encryption_key
, SIPE_SRTP_KEY_LEN
);
796 crypto
= g_strdup_printf("a=crypto:%d AES_CM_128_HMAC_SHA1_80 inline:%s|2^31\r\n",
797 media
->encryption_key_id
, key_encoded
);
802 transport_profile
= g_strdup_printf("%sRTP/%sAVP",
803 uses_tcp_transport
? "TCP/" : "",
804 media
->encryption_active
? "S" : "");
806 media_str
= g_strdup_printf("m=%s %d %s%s\r\n"
814 media
->name
, media
->port
, transport_profile
, codec_ids_str
,
815 media_conninfo
? media_conninfo
: "",
816 candidates_str
? candidates_str
: "",
817 crypto
? crypto
: "",
818 remote_candidates_str
? remote_candidates_str
: "",
819 codecs_str
? codecs_str
: "",
820 attributes_str
? attributes_str
: "",
821 credentials
? credentials
: "");
823 g_free(transport_profile
);
824 g_free(media_conninfo
);
826 g_free(codec_ids_str
);
827 g_free(candidates_str
);
828 g_free(remote_candidates_str
);
829 g_free(attributes_str
);
837 sdpmsg_to_string(const struct sdpmsg
*msg
)
839 GString
*body
= g_string_new(NULL
);
842 g_string_append_printf(
845 "o=- 0 0 IN IP4 %s\r\n"
853 for (i
= msg
->media
; i
; i
= i
->next
) {
854 gchar
*media_str
= media_to_string(msg
, i
->data
);
855 g_string_append(body
, media_str
);
859 return g_string_free(body
, FALSE
);
862 static struct sdpcandidate
*
863 sdpcandidate_copy(struct sdpcandidate
*candidate
)
866 struct sdpcandidate
*copy
= g_new0(struct sdpcandidate
, 1);
868 copy
->foundation
= g_strdup(candidate
->foundation
);
869 copy
->component
= candidate
->component
;
870 copy
->type
= candidate
->type
;
871 copy
->protocol
= candidate
->protocol
;
872 copy
->priority
= candidate
->priority
;
873 copy
->ip
= g_strdup(candidate
->ip
);
874 copy
->port
= candidate
->port
;
875 copy
->base_ip
= g_strdup(candidate
->base_ip
);
876 copy
->base_port
= candidate
->base_port
;
877 copy
->username
= g_strdup(candidate
->username
);
878 copy
->password
= g_strdup(candidate
->password
);
886 sdpcandidate_free(struct sdpcandidate
*candidate
)
889 g_free(candidate
->foundation
);
890 g_free(candidate
->ip
);
891 g_free(candidate
->base_ip
);
892 g_free(candidate
->username
);
893 g_free(candidate
->password
);
899 sdpcodec_free(struct sdpcodec
*codec
)
903 sipe_utils_nameval_free(codec
->parameters
);
909 sdpmedia_free(struct sdpmedia
*media
)
915 sipe_utils_nameval_free(media
->attributes
);
917 sipe_utils_slist_free_full(media
->candidates
,
918 (GDestroyNotify
) sdpcandidate_free
);
919 sipe_utils_slist_free_full(media
->codecs
,
920 (GDestroyNotify
) sdpcodec_free
);
921 sipe_utils_slist_free_full(media
->remote_candidates
,
922 (GDestroyNotify
) sdpcandidate_free
);
924 g_free(media
->encryption_key
);
931 sdpmsg_free(struct sdpmsg
*msg
)
935 sipe_utils_slist_free_full(msg
->media
,
936 (GDestroyNotify
) sdpmedia_free
);