6 * Copyright (C) 2010 Jakub Adam <jakub.adam@ktknet.cz>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "sipe-backend.h"
31 #include "sipe-utils.h"
34 append_attribute(struct sdpmedia
*media
, gchar
*attr
)
36 gchar
**parts
= g_strsplit(attr
+ 2, ":", 2);
43 media
->attributes
= sipe_utils_nameval_add(media
->attributes
,
45 parts
[1] ? parts
[1] : "");
51 parse_attributes(struct sdpmsg
*smsg
, gchar
*msg
) {
52 gchar
**lines
= g_strsplit(msg
, "\r\n", 0);
55 while (*ptr
!= NULL
) {
56 if (g_str_has_prefix(*ptr
, "o=")) {
57 gchar
**parts
= g_strsplit(*ptr
+ 2, " ", 6);
58 smsg
->ip
= g_strdup(parts
[5]);
60 } else if (g_str_has_prefix(*ptr
, "m=")) {
61 gchar
**parts
= g_strsplit(*ptr
+ 2, " ", 3);
62 struct sdpmedia
*media
= g_new0(struct sdpmedia
, 1);
64 smsg
->media
= g_slist_append(smsg
->media
, media
);
66 media
->name
= g_strdup(parts
[0]);
67 media
->port
= atoi(parts
[1]);
71 while (*(++ptr
) && !g_str_has_prefix(*ptr
, "m=")) {
73 if (g_str_has_prefix(*ptr
, "a=")) {
74 if (!append_attribute(media
, *ptr
)) {
91 static struct sdpcandidate
* sdpcandidate_copy(struct sdpcandidate
*candidate
);
92 static void sdpcandidate_free(struct sdpcandidate
*candidate
);
94 static SipeComponentType
95 parse_component(const gchar
*str
)
98 case 1: return SIPE_COMPONENT_RTP
;
99 case 2: return SIPE_COMPONENT_RTCP
;
100 default: return SIPE_COMPONENT_NONE
;
105 base64_pad(const gchar
* str
)
107 size_t str_len
= strlen(str
);
108 int mod
= str_len
% 4;
111 gchar
*result
= NULL
;
113 gchar
*ptr
= result
= g_malloc(str_len
+ pad
+ 1);
115 memcpy(ptr
, str
, str_len
);
117 memset(ptr
, '=', pad
);
123 return g_strdup(str
);
127 parse_append_candidate_draft_6(gchar
**tokens
, GSList
*candidates
)
129 struct sdpcandidate
*candidate
= g_new0(struct sdpcandidate
, 1);
131 candidate
->username
= base64_pad(tokens
[0]);
132 candidate
->component
= parse_component(tokens
[1]);
133 candidate
->password
= base64_pad(tokens
[2]);
135 if (sipe_strequal(tokens
[3], "UDP"))
136 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_UDP
;
137 else if (sipe_strequal(tokens
[3], "TCP"))
138 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
;
140 sdpcandidate_free(candidate
);
144 candidate
->priority
= atoi(tokens
[4] + 2);
145 candidate
->ip
= g_strdup(tokens
[5]);
146 candidate
->port
= atoi(tokens
[6]);
148 candidates
= g_slist_append(candidates
, candidate
);
150 // draft 6 candidates are both active and passive
151 if (candidate
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
) {
152 candidate
= sdpcandidate_copy(candidate
);
153 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
;
154 candidates
= g_slist_append(candidates
, candidate
);
161 parse_append_candidate_rfc_5245(gchar
**tokens
, GSList
*candidates
)
163 struct sdpcandidate
*candidate
= g_new0(struct sdpcandidate
, 1);
165 candidate
->foundation
= g_strdup(tokens
[0]);
166 candidate
->component
= parse_component(tokens
[1]);
168 if (sipe_strequal(tokens
[2], "UDP"))
169 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_UDP
;
170 else if (sipe_strequal(tokens
[2], "TCP-ACT"))
171 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
;
172 else if (sipe_strequal(tokens
[2], "TCP-PASS"))
173 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
;
175 sdpcandidate_free(candidate
);
179 candidate
->priority
= atoi(tokens
[3]);
180 candidate
->ip
= g_strdup(tokens
[4]);
181 candidate
->port
= atoi(tokens
[5]);
183 if (sipe_strequal(tokens
[7], "host"))
184 candidate
->type
= SIPE_CANDIDATE_TYPE_HOST
;
185 else if (sipe_strequal(tokens
[7], "relay"))
186 candidate
->type
= SIPE_CANDIDATE_TYPE_RELAY
;
187 else if (sipe_strequal(tokens
[7], "srflx"))
188 candidate
->type
= SIPE_CANDIDATE_TYPE_SRFLX
;
189 else if (sipe_strequal(tokens
[7], "prflx"))
190 candidate
->type
= SIPE_CANDIDATE_TYPE_PRFLX
;
192 sdpcandidate_free(candidate
);
196 return g_slist_append(candidates
, candidate
);
200 parse_candidates(GSList
*attrs
, SipeIceVersion
*ice_version
)
202 GSList
*candidates
= NULL
;
206 while ((attr
= sipe_utils_nameval_find_instance(attrs
, "candidate", i
++))) {
207 gchar
**tokens
= g_strsplit_set(attr
, " ", 0);
209 if (sipe_strequal(tokens
[6], "typ")) {
210 candidates
= parse_append_candidate_rfc_5245(tokens
, candidates
);
212 *ice_version
= SIPE_ICE_RFC_5245
;
214 candidates
= parse_append_candidate_draft_6(tokens
, candidates
);
216 *ice_version
= SIPE_ICE_DRAFT_6
;
223 *ice_version
= SIPE_ICE_NO_ICE
;
225 if (*ice_version
== SIPE_ICE_RFC_5245
) {
226 const gchar
*username
= sipe_utils_nameval_find(attrs
, "ice-ufrag");
227 const gchar
*password
= sipe_utils_nameval_find(attrs
, "ice-pwd");
229 if (username
&& password
) {
231 for (i
= candidates
; i
; i
= i
->next
) {
232 struct sdpcandidate
*c
= i
->data
;
233 c
->username
= g_strdup(username
);
234 c
->password
= g_strdup(password
);
243 create_legacy_candidates(gchar
*ip
, guint16 port
)
245 struct sdpcandidate
*candidate
;
246 GSList
*candidates
= NULL
;
248 candidate
= g_new0(struct sdpcandidate
, 1);
249 candidate
->foundation
= g_strdup("1");
250 candidate
->component
= SIPE_COMPONENT_RTP
;
251 candidate
->type
= SIPE_CANDIDATE_TYPE_HOST
;
252 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_UDP
;
253 candidate
->ip
= g_strdup(ip
);
254 candidate
->port
= port
;
256 candidates
= g_slist_append(candidates
, candidate
);
258 candidate
= g_new0(struct sdpcandidate
, 1);
259 candidate
->foundation
= g_strdup("1");
260 candidate
->component
= SIPE_COMPONENT_RTCP
;
261 candidate
->type
= SIPE_CANDIDATE_TYPE_HOST
;
262 candidate
->protocol
= SIPE_NETWORK_PROTOCOL_UDP
;
263 candidate
->ip
= g_strdup(ip
);
264 candidate
->port
= port
+ 1;
266 candidates
= g_slist_append(candidates
, candidate
);
272 parse_codecs(GSList
*attrs
, SipeMediaType type
)
276 GSList
*codecs
= NULL
;
278 while ((attr
= sipe_utils_nameval_find_instance(attrs
, "rtpmap", i
++))) {
279 struct sdpcodec
*codec
= g_new0(struct sdpcodec
, 1);
280 gchar
**tokens
= g_strsplit_set(attr
, " /", 3);
285 codec
->id
= atoi(tokens
[0]);
286 codec
->name
= g_strdup(tokens
[1]);
287 codec
->clock_rate
= atoi(tokens
[2]);
290 // TODO: more secure and effective implementation
291 while((params
= sipe_utils_nameval_find_instance(attrs
, "fmtp", j
++))) {
292 gchar
**tokens
= g_strsplit_set(params
, " ", 0);
293 gchar
**next
= tokens
+ 1;
295 if (atoi(tokens
[0]) == codec
->id
) {
300 if (sscanf(*next
, "%[a-zA-Z0-9]=%s", name
, value
) == 2)
301 codec
->parameters
= sipe_utils_nameval_add(codec
->parameters
, name
, value
);
310 codecs
= g_slist_append(codecs
, codec
);
318 sdpmsg_parse_msg(gchar
*msg
)
320 struct sdpmsg
*smsg
= g_new0(struct sdpmsg
, 1);
323 if (!parse_attributes(smsg
, msg
)) {
328 for (i
= smsg
->media
; i
; i
= i
->next
) {
329 struct sdpmedia
*media
= i
->data
;
332 media
->candidates
= parse_candidates(media
->attributes
,
335 if (!media
->candidates
&& media
->port
!= 0) {
336 // No a=candidate in SDP message, this seems to be MSOC 2005
337 media
->candidates
= create_legacy_candidates(smsg
->ip
, media
->port
);
340 if (sipe_strequal(media
->name
, "audio"))
341 type
= SIPE_MEDIA_AUDIO
;
342 else if (sipe_strequal(media
->name
, "video"))
343 type
= SIPE_MEDIA_VIDEO
;
345 // Unknown media type
350 media
->codecs
= parse_codecs(media
->attributes
, type
);
357 codecs_to_string(GSList
*codecs
)
359 GString
*result
= g_string_new(NULL
);
361 for (; codecs
; codecs
= codecs
->next
) {
362 struct sdpcodec
*c
= codecs
->data
;
363 GSList
*params
= c
->parameters
;
365 g_string_append_printf(result
,
366 "a=rtpmap:%d %s/%d\r\n",
372 g_string_append_printf(result
, "a=fmtp:%d", c
->id
);
374 for (; params
; params
= params
->next
) {
375 struct sipnameval
* par
= params
->data
;
376 g_string_append_printf(result
, " %s=%s",
377 par
->name
, par
->value
);
380 g_string_append(result
, "\r\n");
384 return g_string_free(result
, FALSE
);
388 codec_ids_to_string(GSList
*codecs
)
390 GString
*result
= g_string_new(NULL
);
392 for (; codecs
; codecs
= codecs
->next
) {
393 struct sdpcodec
*c
= codecs
->data
;
394 g_string_append_printf(result
, " %d", c
->id
);
397 return g_string_free(result
, FALSE
);
401 base64_unpad(const gchar
*str
)
403 gchar
*result
= g_strdup(str
);
406 for (ptr
= result
+ strlen(result
); ptr
!= result
; --ptr
) {
407 if (*(ptr
- 1) != '=') {
417 candidate_sort_cb(struct sdpcandidate
*c1
, struct sdpcandidate
*c2
)
419 int cmp
= sipe_strcompare(c1
->foundation
, c2
->foundation
);
421 cmp
= sipe_strcompare(c1
->username
, c2
->username
);
423 cmp
= c1
->component
- c2
->component
;
430 candidates_to_string(GSList
*candidates
, SipeIceVersion ice_version
)
432 GString
*result
= g_string_new("");
434 GSList
*processed_tcp_candidates
= NULL
;
436 candidates
= g_slist_copy(candidates
);
437 candidates
= g_slist_sort(candidates
, (GCompareFunc
)candidate_sort_cb
);
439 for (i
= candidates
; i
; i
= i
->next
) {
440 struct sdpcandidate
*c
= i
->data
;
441 const gchar
*protocol
;
443 gchar
*related
= NULL
;
445 if (ice_version
== SIPE_ICE_RFC_5245
) {
447 switch (c
->protocol
) {
448 case SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
:
449 protocol
= "TCP-ACT";
451 case SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
:
452 protocol
= "TCP-PASS";
454 case SIPE_NETWORK_PROTOCOL_UDP
:
458 /* error unknown/unsupported type */
459 protocol
= "UNKNOWN";
464 case SIPE_CANDIDATE_TYPE_HOST
:
467 case SIPE_CANDIDATE_TYPE_RELAY
:
469 related
= g_strdup_printf("raddr %s rport %d ",
473 case SIPE_CANDIDATE_TYPE_SRFLX
:
475 related
= g_strdup_printf("raddr %s rport %d",
479 case SIPE_CANDIDATE_TYPE_PRFLX
:
483 /* error unknown/unsupported type */
488 g_string_append_printf(result
,
489 "a=candidate:%s %u %s %u %s %d typ %s %s\r\n",
497 related
? related
: "");
500 } else if (ice_version
== SIPE_ICE_DRAFT_6
) {
504 switch (c
->protocol
) {
505 case SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
:
506 case SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
: {
507 GSList
*prev_cand
= processed_tcp_candidates
;
508 for (; prev_cand
; prev_cand
= prev_cand
->next
) {
509 struct sdpcandidate
*c2
= (struct sdpcandidate
*)prev_cand
->data
;
511 if (sipe_strequal(c
->ip
, c2
->ip
) &&
512 c
->component
== c2
->component
) {
521 processed_tcp_candidates
=
522 g_slist_append(processed_tcp_candidates
, c
);
526 case SIPE_NETWORK_PROTOCOL_UDP
:
530 /* unknown/unsupported type, ignore */
539 username
= base64_unpad(c
->username
);
540 password
= base64_unpad(c
->password
);
542 g_string_append_printf(result
,
543 "a=candidate:%s %u %s %s 0.%u %s %d\r\n",
557 g_slist_free(candidates
);
558 g_slist_free(processed_tcp_candidates
);
560 return g_string_free(result
, FALSE
);
564 remote_candidates_to_string(GSList
*candidates
, SipeIceVersion ice_version
)
566 GString
*result
= g_string_new("");
568 candidates
= g_slist_copy(candidates
);
569 candidates
= g_slist_sort(candidates
, (GCompareFunc
)candidate_sort_cb
);
572 if (ice_version
== SIPE_ICE_RFC_5245
) {
574 g_string_append(result
, "a=remote-candidates:");
576 for (i
= candidates
; i
; i
= i
->next
) {
577 struct sdpcandidate
*c
= i
->data
;
578 g_string_append_printf(result
, "%u %s %u ",
579 c
->component
, c
->ip
, c
->port
);
582 g_string_append(result
, "\r\n");
583 } else if (ice_version
== SIPE_ICE_DRAFT_6
) {
584 struct sdpcandidate
*c
= candidates
->data
;
585 g_string_append_printf(result
, "a=remote-candidate:%s\r\n",
590 g_slist_free(candidates
);
592 return g_string_free(result
, FALSE
);
596 attributes_to_string(GSList
*attributes
)
598 GString
*result
= g_string_new("");
600 for (; attributes
; attributes
= attributes
->next
) {
601 struct sipnameval
*a
= attributes
->data
;
602 g_string_append_printf(result
, "a=%s", a
->name
);
603 if (!sipe_strequal(a
->value
, ""))
604 g_string_append_printf(result
, ":%s", a
->value
);
605 g_string_append(result
, "\r\n");
608 return g_string_free(result
, FALSE
);
612 media_to_string(const struct sdpmsg
*msg
, const struct sdpmedia
*media
)
616 gchar
*media_conninfo
= NULL
;
618 gchar
*codecs_str
= NULL
;
619 gchar
*codec_ids_str
= codec_ids_to_string(media
->codecs
);
621 gchar
*candidates_str
= NULL
;
622 gchar
*remote_candidates_str
= NULL
;
624 gchar
*tcp_setup_str
= NULL
;
625 gchar
*attributes_str
= NULL
;
626 gchar
*credentials
= NULL
;
628 gboolean uses_tcp_transport
= FALSE
;
630 if (media
->port
!= 0) {
631 if (!sipe_strequal(msg
->ip
, media
->ip
)) {
632 media_conninfo
= g_strdup_printf("c=IN IP4 %s\r\n", media
->ip
);
635 codecs_str
= codecs_to_string(media
->codecs
);
636 candidates_str
= candidates_to_string(media
->candidates
, msg
->ice_version
);
637 remote_candidates_str
= remote_candidates_to_string(media
->remote_candidates
,
640 if (media
->remote_candidates
) {
641 struct sdpcandidate
*c
= media
->remote_candidates
->data
;
643 c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
||
644 c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
;
645 if (uses_tcp_transport
) {
646 tcp_setup_str
= g_strdup_printf(
647 "a=connection:existing\r\n"
649 (c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
) ? "passive" : "active");
653 attributes_str
= attributes_to_string(media
->attributes
);
655 if (msg
->ice_version
== SIPE_ICE_RFC_5245
&& media
->candidates
) {
656 struct sdpcandidate
*c
= media
->candidates
->data
;
658 credentials
= g_strdup_printf("a=ice-ufrag:%s\r\n"
665 media_str
= g_strdup_printf("m=%s %d %sRTP/AVP%s\r\n"
673 media
->name
, media
->port
, uses_tcp_transport
? "TCP/" : "", codec_ids_str
,
674 media_conninfo
? media_conninfo
: "",
675 candidates_str
? candidates_str
: "",
676 remote_candidates_str
? remote_candidates_str
: "",
677 tcp_setup_str
? tcp_setup_str
: "",
678 codecs_str
? codecs_str
: "",
679 attributes_str
? attributes_str
: "",
680 credentials
? credentials
: "");
682 g_free(media_conninfo
);
684 g_free(codec_ids_str
);
685 g_free(candidates_str
);
686 g_free(remote_candidates_str
);
687 g_free(tcp_setup_str
);
688 g_free(attributes_str
);
695 sdpmsg_to_string(const struct sdpmsg
*msg
)
697 GString
*body
= g_string_new(NULL
);
700 g_string_append_printf(
703 "o=- 0 0 IN IP4 %s\r\n"
711 for (i
= msg
->media
; i
; i
= i
->next
) {
712 gchar
*media_str
= media_to_string(msg
, i
->data
);
713 g_string_append(body
, media_str
);
717 return g_string_free(body
, FALSE
);
720 static struct sdpcandidate
*
721 sdpcandidate_copy(struct sdpcandidate
*candidate
)
724 struct sdpcandidate
*copy
= g_new0(struct sdpcandidate
, 1);
726 copy
->foundation
= g_strdup(candidate
->foundation
);
727 copy
->component
= candidate
->component
;
728 copy
->type
= candidate
->type
;
729 copy
->protocol
= candidate
->protocol
;
730 copy
->priority
= candidate
->priority
;
731 copy
->ip
= g_strdup(candidate
->ip
);
732 copy
->port
= candidate
->port
;
733 copy
->base_ip
= g_strdup(candidate
->base_ip
);
734 copy
->base_port
= candidate
->base_port
;
735 copy
->username
= g_strdup(candidate
->username
);
736 copy
->password
= g_strdup(candidate
->password
);
744 sdpcandidate_free(struct sdpcandidate
*candidate
)
747 g_free(candidate
->foundation
);
748 g_free(candidate
->ip
);
749 g_free(candidate
->base_ip
);
750 g_free(candidate
->username
);
751 g_free(candidate
->password
);
757 sdpcodec_free(struct sdpcodec
*codec
)
761 sipe_utils_nameval_free(codec
->parameters
);
767 sdpmedia_free(struct sdpmedia
*media
)
775 sipe_utils_nameval_free(media
->attributes
);
777 for (item
= media
->candidates
; item
; item
= item
->next
)
778 sdpcandidate_free(item
->data
);
779 g_slist_free(media
->candidates
);
781 for (item
= media
->codecs
; item
; item
= item
->next
)
782 sdpcodec_free(item
->data
);
783 g_slist_free(media
->codecs
);
785 for (item
= media
->remote_candidates
; item
; item
= item
->next
)
786 sdpcandidate_free(item
->data
);
787 g_slist_free(media
->remote_candidates
);
794 sdpmsg_free(struct sdpmsg
*msg
)
800 for (item
= msg
->media
; item
; item
= item
->next
)
801 sdpmedia_free(item
->data
);
802 g_slist_free(msg
->media
);