media: mux RTP and RTCP when using TCP connection
[siplcs.git] / src / core / sdpmsg.c
blobb2c70aa0c8c50831f9eb6e763315f14c84806d3f
1 /**
2 * @file sdpmsg.c
4 * pidgin-sipe
6 * Copyright (C) 2013 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 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include <glib.h>
30 #include "sipe-backend.h"
31 #include "sdpmsg.h"
32 #include "sipe-utils.h"
34 static gboolean
35 append_attribute(struct sdpmedia *media, gchar *attr)
37 gchar **parts = g_strsplit(attr + 2, ":", 2);
39 if(!parts[0]) {
40 g_strfreev(parts);
41 return FALSE;
44 media->attributes = sipe_utils_nameval_add(media->attributes,
45 parts[0],
46 parts[1] ? parts[1] : "");
47 g_strfreev(parts);
48 return TRUE;
51 static gboolean
52 parse_attributes(struct sdpmsg *smsg, gchar *msg) {
53 gchar **lines = g_strsplit(msg, "\r\n", 0);
54 gchar **ptr = lines;
56 while (*ptr != NULL) {
57 if (g_str_has_prefix(*ptr, "o=")) {
58 gchar **parts = g_strsplit(*ptr + 2, " ", 6);
59 smsg->ip = g_strdup(parts[5]);
60 g_strfreev(parts);
61 } else if (g_str_has_prefix(*ptr, "m=")) {
62 gchar **parts = g_strsplit(*ptr + 2, " ", 3);
63 struct sdpmedia *media = g_new0(struct sdpmedia, 1);
65 smsg->media = g_slist_append(smsg->media, media);
67 media->name = g_strdup(parts[0]);
68 media->port = atoi(parts[1]);
70 g_strfreev(parts);
72 while (*(++ptr) && !g_str_has_prefix(*ptr, "m=")) {
74 if (g_str_has_prefix(*ptr, "a=")) {
75 if (!append_attribute(media, *ptr)) {
76 g_strfreev(lines);
77 return FALSE;
81 continue;
84 ++ptr;
87 g_strfreev(lines);
89 return TRUE;
92 static struct sdpcandidate * sdpcandidate_copy(struct sdpcandidate *candidate);
93 static void sdpcandidate_free(struct sdpcandidate *candidate);
95 static SipeComponentType
96 parse_component(const gchar *str)
98 switch (atoi(str)) {
99 case 1: return SIPE_COMPONENT_RTP;
100 case 2: return SIPE_COMPONENT_RTCP;
101 default: return SIPE_COMPONENT_NONE;
105 static gchar *
106 base64_pad(const gchar* str)
108 size_t str_len = strlen(str);
109 int mod = str_len % 4;
111 if (mod > 0) {
112 gchar *result = NULL;
113 int pad = 4 - mod;
114 gchar *ptr = result = g_malloc(str_len + pad + 1);
116 memcpy(ptr, str, str_len);
117 ptr += str_len;
118 memset(ptr, '=', pad);
119 ptr += pad;
120 *ptr = '\0';
122 return result;
123 } else
124 return g_strdup(str);
127 static GSList *
128 parse_append_candidate_draft_6(gchar **tokens, GSList *candidates)
130 struct sdpcandidate *candidate = g_new0(struct sdpcandidate, 1);
132 candidate->username = base64_pad(tokens[0]);
133 candidate->component = parse_component(tokens[1]);
134 candidate->password = base64_pad(tokens[2]);
136 if (sipe_strequal(tokens[3], "UDP"))
137 candidate->protocol = SIPE_NETWORK_PROTOCOL_UDP;
138 else if (sipe_strequal(tokens[3], "TCP"))
139 candidate->protocol = SIPE_NETWORK_PROTOCOL_TCP_ACTIVE;
140 else {
141 sdpcandidate_free(candidate);
142 return candidates;
145 candidate->priority = atoi(tokens[4] + 2);
146 candidate->ip = g_strdup(tokens[5]);
147 candidate->port = atoi(tokens[6]);
149 candidates = g_slist_append(candidates, candidate);
151 // draft 6 candidates are both active and passive
152 if (candidate->protocol == SIPE_NETWORK_PROTOCOL_TCP_ACTIVE) {
153 candidate = sdpcandidate_copy(candidate);
154 candidate->protocol = SIPE_NETWORK_PROTOCOL_TCP_PASSIVE;
155 candidates = g_slist_append(candidates, candidate);
158 return candidates;
161 static GSList *
162 parse_append_candidate_rfc_5245(gchar **tokens, GSList *candidates)
164 struct sdpcandidate *candidate = g_new0(struct sdpcandidate, 1);
166 candidate->foundation = g_strdup(tokens[0]);
167 candidate->component = parse_component(tokens[1]);
169 if (sipe_strequal(tokens[2], "UDP"))
170 candidate->protocol = SIPE_NETWORK_PROTOCOL_UDP;
171 else if (sipe_strequal(tokens[2], "TCP-ACT"))
172 candidate->protocol = SIPE_NETWORK_PROTOCOL_TCP_ACTIVE;
173 else if (sipe_strequal(tokens[2], "TCP-PASS"))
174 candidate->protocol = SIPE_NETWORK_PROTOCOL_TCP_PASSIVE;
175 else {
176 sdpcandidate_free(candidate);
177 return candidates;
180 candidate->priority = atoi(tokens[3]);
181 candidate->ip = g_strdup(tokens[4]);
182 candidate->port = atoi(tokens[5]);
184 if (sipe_strequal(tokens[7], "host"))
185 candidate->type = SIPE_CANDIDATE_TYPE_HOST;
186 else if (sipe_strequal(tokens[7], "relay"))
187 candidate->type = SIPE_CANDIDATE_TYPE_RELAY;
188 else if (sipe_strequal(tokens[7], "srflx"))
189 candidate->type = SIPE_CANDIDATE_TYPE_SRFLX;
190 else if (sipe_strequal(tokens[7], "prflx"))
191 candidate->type = SIPE_CANDIDATE_TYPE_PRFLX;
192 else {
193 sdpcandidate_free(candidate);
194 return candidates;
197 candidates = g_slist_append(candidates, candidate);
199 // TCP-ACT candidates are both active and passive
200 if (candidate->protocol == SIPE_NETWORK_PROTOCOL_TCP_ACTIVE) {
201 candidate = sdpcandidate_copy(candidate);
202 candidate->protocol = SIPE_NETWORK_PROTOCOL_TCP_PASSIVE;
203 candidates = g_slist_append(candidates, candidate);
205 return candidates;
208 static GSList *
209 parse_candidates(GSList *attrs, SipeIceVersion *ice_version)
211 GSList *candidates = NULL;
212 const gchar *attr;
213 int i = 0;
215 while ((attr = sipe_utils_nameval_find_instance(attrs, "candidate", i++))) {
216 gchar **tokens = g_strsplit_set(attr, " ", 0);
218 if (sipe_strequal(tokens[6], "typ")) {
219 candidates = parse_append_candidate_rfc_5245(tokens, candidates);
220 if (candidates)
221 *ice_version = SIPE_ICE_RFC_5245;
222 } else {
223 candidates = parse_append_candidate_draft_6(tokens, candidates);
224 if (candidates)
225 *ice_version = SIPE_ICE_DRAFT_6;
228 g_strfreev(tokens);
231 if (!candidates)
232 *ice_version = SIPE_ICE_NO_ICE;
234 if (*ice_version == SIPE_ICE_RFC_5245) {
235 const gchar *username = sipe_utils_nameval_find(attrs, "ice-ufrag");
236 const gchar *password = sipe_utils_nameval_find(attrs, "ice-pwd");
238 if (username && password) {
239 GSList *i;
240 for (i = candidates; i; i = i->next) {
241 struct sdpcandidate *c = i->data;
242 c->username = g_strdup(username);
243 c->password = g_strdup(password);
248 return candidates;
251 static GSList *
252 create_legacy_candidates(gchar *ip, guint16 port)
254 struct sdpcandidate *candidate;
255 GSList *candidates = NULL;
257 candidate = g_new0(struct sdpcandidate, 1);
258 candidate->foundation = g_strdup("1");
259 candidate->component = SIPE_COMPONENT_RTP;
260 candidate->type = SIPE_CANDIDATE_TYPE_HOST;
261 candidate->protocol = SIPE_NETWORK_PROTOCOL_UDP;
262 candidate->ip = g_strdup(ip);
263 candidate->port = port;
265 candidates = g_slist_append(candidates, candidate);
267 candidate = g_new0(struct sdpcandidate, 1);
268 candidate->foundation = g_strdup("1");
269 candidate->component = SIPE_COMPONENT_RTCP;
270 candidate->type = SIPE_CANDIDATE_TYPE_HOST;
271 candidate->protocol = SIPE_NETWORK_PROTOCOL_UDP;
272 candidate->ip = g_strdup(ip);
273 candidate->port = port + 1;
275 candidates = g_slist_append(candidates, candidate);
277 return candidates;
280 static GSList *
281 parse_codecs(GSList *attrs, SipeMediaType type)
283 int i = 0;
284 const gchar *attr;
285 GSList *codecs = NULL;
287 while ((attr = sipe_utils_nameval_find_instance(attrs, "rtpmap", i++))) {
288 struct sdpcodec *codec = g_new0(struct sdpcodec, 1);
289 gchar **tokens = g_strsplit_set(attr, " /", 3);
291 int j = 0;
292 const gchar* params;
294 codec->id = atoi(tokens[0]);
295 codec->name = g_strdup(tokens[1]);
296 codec->clock_rate = atoi(tokens[2]);
297 codec->type = type;
299 // TODO: more secure and effective implementation
300 while((params = sipe_utils_nameval_find_instance(attrs, "fmtp", j++))) {
301 gchar **tokens = g_strsplit_set(params, " ", 0);
302 gchar **next = tokens + 1;
304 if (atoi(tokens[0]) == codec->id) {
305 while (*next) {
306 gchar name[50];
307 gchar value[50];
309 if (sscanf(*next, "%[a-zA-Z0-9]=%s", name, value) == 2)
310 codec->parameters = sipe_utils_nameval_add(codec->parameters, name, value);
312 ++next;
316 g_strfreev(tokens);
319 codecs = g_slist_append(codecs, codec);
320 g_strfreev(tokens);
323 return codecs;
326 struct sdpmsg *
327 sdpmsg_parse_msg(gchar *msg)
329 struct sdpmsg *smsg = g_new0(struct sdpmsg, 1);
330 GSList *i;
332 if (!parse_attributes(smsg, msg)) {
333 sdpmsg_free(smsg);
334 return NULL;
337 for (i = smsg->media; i; i = i->next) {
338 struct sdpmedia *media = i->data;
339 SipeMediaType type;
341 media->candidates = parse_candidates(media->attributes,
342 &smsg->ice_version);
344 if (!media->candidates && media->port != 0) {
345 // No a=candidate in SDP message, this seems to be MSOC 2005
346 media->candidates = create_legacy_candidates(smsg->ip, media->port);
349 if (sipe_strequal(media->name, "audio"))
350 type = SIPE_MEDIA_AUDIO;
351 else if (sipe_strequal(media->name, "video"))
352 type = SIPE_MEDIA_VIDEO;
353 else {
354 // Unknown media type
355 sdpmsg_free(smsg);
356 return NULL;
359 media->codecs = parse_codecs(media->attributes, type);
362 return smsg;
365 static gchar *
366 codecs_to_string(GSList *codecs)
368 GString *result = g_string_new(NULL);
370 for (; codecs; codecs = codecs->next) {
371 struct sdpcodec *c = codecs->data;
372 GSList *params = c->parameters;
374 g_string_append_printf(result,
375 "a=rtpmap:%d %s/%d\r\n",
376 c->id,
377 c->name,
378 c->clock_rate);
380 if (params) {
381 GString *param_str = g_string_new(NULL);
382 int written_params = 0;
384 g_string_append_printf(param_str, "a=fmtp:%d", c->id);
386 for (; params; params = params->next) {
387 struct sipnameval* par = params->data;
388 if (sipe_strequal(par->name, "farsight-send-profile")) {
389 // Lync AVMCU doesn't like this property.
390 continue;
393 g_string_append_printf(param_str, " %s=%s",
394 par->name, par->value);
395 ++written_params;
398 g_string_append(param_str, "\r\n");
400 if (written_params > 0) {
401 g_string_append(result, param_str->str);
404 g_string_free(param_str, TRUE);
408 return g_string_free(result, FALSE);
411 static gchar *
412 codec_ids_to_string(GSList *codecs)
414 GString *result = g_string_new(NULL);
416 for (; codecs; codecs = codecs->next) {
417 struct sdpcodec *c = codecs->data;
418 g_string_append_printf(result, " %d", c->id);
421 return g_string_free(result, FALSE);
424 static gchar *
425 base64_unpad(const gchar *str)
427 gchar *result = g_strdup(str);
428 gchar *ptr;
430 for (ptr = result + strlen(result); ptr != result; --ptr) {
431 if (*(ptr - 1) != '=') {
432 *ptr = '\0';
433 break;
437 return result;
440 static gchar *
441 candidates_to_string(GSList *candidates, SipeIceVersion ice_version)
443 GString *result = g_string_new("");
444 GSList *i;
445 GSList *processed_tcp_candidates = NULL;
447 for (i = candidates; i; i = i->next) {
448 struct sdpcandidate *c = i->data;
449 const gchar *protocol;
450 const gchar *type;
451 gchar *related = NULL;
453 if (ice_version == SIPE_ICE_RFC_5245) {
455 switch (c->protocol) {
456 case SIPE_NETWORK_PROTOCOL_TCP_ACTIVE:
457 protocol = "TCP-ACT";
458 break;
459 case SIPE_NETWORK_PROTOCOL_TCP_PASSIVE:
460 protocol = "TCP-PASS";
461 break;
462 case SIPE_NETWORK_PROTOCOL_UDP:
463 protocol = "UDP";
464 break;
465 default:
466 /* error unknown/unsupported type */
467 protocol = "UNKNOWN";
468 break;
471 switch (c->type) {
472 case SIPE_CANDIDATE_TYPE_HOST:
473 type = "host";
474 break;
475 case SIPE_CANDIDATE_TYPE_RELAY:
476 type = "relay";
477 related = g_strdup_printf("raddr %s rport %d ",
478 c->base_ip,
479 c->base_port);
480 break;
481 case SIPE_CANDIDATE_TYPE_SRFLX:
482 type = "srflx";
483 related = g_strdup_printf("raddr %s rport %d",
484 c->base_ip,
485 c->base_port);
486 break;
487 case SIPE_CANDIDATE_TYPE_PRFLX:
488 type = "prflx";
489 break;
490 default:
491 /* error unknown/unsupported type */
492 type = "unknown";
493 break;
496 g_string_append_printf(result,
497 "a=candidate:%s %u %s %u %s %d typ %s %s\r\n",
498 c->foundation,
499 c->component,
500 protocol,
501 c->priority,
502 c->ip,
503 c->port,
504 type,
505 related ? related : "");
506 g_free(related);
508 } else if (ice_version == SIPE_ICE_DRAFT_6) {
509 gchar *username;
510 gchar *password;
512 switch (c->protocol) {
513 case SIPE_NETWORK_PROTOCOL_TCP_ACTIVE:
514 case SIPE_NETWORK_PROTOCOL_TCP_PASSIVE: {
515 GSList *prev_cand = processed_tcp_candidates;
516 for (; prev_cand; prev_cand = prev_cand->next) {
517 struct sdpcandidate *c2 = (struct sdpcandidate *)prev_cand->data;
519 if (sipe_strequal(c->ip, c2->ip) &&
520 c->component == c2->component) {
521 break;
525 if (prev_cand) {
526 protocol = NULL;
527 } else {
528 protocol = "TCP";
529 processed_tcp_candidates =
530 g_slist_append(processed_tcp_candidates, c);
532 break;
534 case SIPE_NETWORK_PROTOCOL_UDP:
535 protocol = "UDP";
536 break;
537 default:
538 /* unknown/unsupported type, ignore */
539 protocol = NULL;
540 break;
543 if (!protocol) {
544 continue;
547 username = base64_unpad(c->username);
548 password = base64_unpad(c->password);
550 g_string_append_printf(result,
551 "a=candidate:%s %u %s %s 0.%u %s %d\r\n",
552 username,
553 c->component,
554 password,
555 protocol,
556 c->priority,
557 c->ip,
558 c->port);
560 g_free(username);
561 g_free(password);
565 g_slist_free(processed_tcp_candidates);
567 return g_string_free(result, FALSE);
570 static gchar *
571 remote_candidates_to_string(GSList *candidates, SipeIceVersion ice_version)
573 GString *result = g_string_new("");
575 if (candidates) {
576 if (ice_version == SIPE_ICE_RFC_5245) {
577 GSList *i;
578 g_string_append(result, "a=remote-candidates:");
580 for (i = candidates; i; i = i->next) {
581 struct sdpcandidate *c = i->data;
582 g_string_append_printf(result, "%u %s %u ",
583 c->component, c->ip, c->port);
586 g_string_append(result, "\r\n");
587 } else if (ice_version == SIPE_ICE_DRAFT_6) {
588 struct sdpcandidate *c = candidates->data;
589 g_string_append_printf(result, "a=remote-candidate:%s\r\n",
590 c->username);
594 return g_string_free(result, FALSE);
597 static gchar *
598 attributes_to_string(GSList *attributes)
600 GString *result = g_string_new("");
602 for (; attributes; attributes = attributes->next) {
603 struct sipnameval *a = attributes->data;
604 g_string_append_printf(result, "a=%s", a->name);
605 if (!sipe_strequal(a->value, ""))
606 g_string_append_printf(result, ":%s", a->value);
607 g_string_append(result, "\r\n");
610 return g_string_free(result, FALSE);
613 static gchar *
614 media_to_string(const struct sdpmsg *msg, const struct sdpmedia *media)
616 gchar *media_str;
618 gchar *media_conninfo = NULL;
620 gchar *codecs_str = NULL;
621 gchar *codec_ids_str = codec_ids_to_string(media->codecs);
623 gchar *candidates_str = NULL;
624 gchar *remote_candidates_str = NULL;
626 gchar *attributes_str = NULL;
627 gchar *credentials = NULL;
629 gboolean uses_tcp_transport = FALSE;
631 if (media->port != 0) {
632 if (!sipe_strequal(msg->ip, media->ip)) {
633 media_conninfo = g_strdup_printf("c=IN IP4 %s\r\n", media->ip);
636 codecs_str = codecs_to_string(media->codecs);
637 candidates_str = candidates_to_string(media->candidates, msg->ice_version);
638 remote_candidates_str = remote_candidates_to_string(media->remote_candidates,
639 msg->ice_version);
641 if (media->remote_candidates) {
642 struct sdpcandidate *c = media->remote_candidates->data;
643 uses_tcp_transport =
644 c->protocol == SIPE_NETWORK_PROTOCOL_TCP_ACTIVE ||
645 c->protocol == SIPE_NETWORK_PROTOCOL_TCP_PASSIVE ||
646 c->protocol == SIPE_NETWORK_PROTOCOL_TCP_SO;
649 attributes_str = attributes_to_string(media->attributes);
651 if (msg->ice_version == SIPE_ICE_RFC_5245 && media->candidates) {
652 struct sdpcandidate *c = media->candidates->data;
654 credentials = g_strdup_printf("a=ice-ufrag:%s\r\n"
655 "a=ice-pwd:%s\r\n",
656 c->username,
657 c->password);
661 media_str = g_strdup_printf("m=%s %d %sRTP/AVP%s\r\n"
662 "%s"
663 "%s"
664 "%s"
665 "%s"
666 "%s"
667 "%s",
668 media->name, media->port, uses_tcp_transport ? "TCP/" : "", codec_ids_str,
669 media_conninfo ? media_conninfo : "",
670 candidates_str ? candidates_str : "",
671 remote_candidates_str ? remote_candidates_str : "",
672 codecs_str ? codecs_str : "",
673 attributes_str ? attributes_str : "",
674 credentials ? credentials : "");
676 g_free(media_conninfo);
677 g_free(codecs_str);
678 g_free(codec_ids_str);
679 g_free(candidates_str);
680 g_free(remote_candidates_str);
681 g_free(attributes_str);
682 g_free(credentials);
684 return media_str;
687 gchar *
688 sdpmsg_to_string(const struct sdpmsg *msg)
690 GString *body = g_string_new(NULL);
691 GSList *i;
693 g_string_append_printf(
694 body,
695 "v=0\r\n"
696 "o=- 0 0 IN IP4 %s\r\n"
697 "s=session\r\n"
698 "c=IN IP4 %s\r\n"
699 "b=CT:99980\r\n"
700 "t=0 0\r\n",
701 msg->ip, msg->ip);
704 for (i = msg->media; i; i = i->next) {
705 gchar *media_str = media_to_string(msg, i->data);
706 g_string_append(body, media_str);
707 g_free(media_str);
710 return g_string_free(body, FALSE);
713 static struct sdpcandidate *
714 sdpcandidate_copy(struct sdpcandidate *candidate)
716 if (candidate) {
717 struct sdpcandidate *copy = g_new0(struct sdpcandidate, 1);
719 copy->foundation = g_strdup(candidate->foundation);
720 copy->component = candidate->component;
721 copy->type = candidate->type;
722 copy->protocol = candidate->protocol;
723 copy->priority = candidate->priority;
724 copy->ip = g_strdup(candidate->ip);
725 copy->port = candidate->port;
726 copy->base_ip = g_strdup(candidate->base_ip);
727 copy->base_port = candidate->base_port;
728 copy->username = g_strdup(candidate->username);
729 copy->password = g_strdup(candidate->password);
731 return copy;
732 } else
733 return NULL;
736 static void
737 sdpcandidate_free(struct sdpcandidate *candidate)
739 if (candidate) {
740 g_free(candidate->foundation);
741 g_free(candidate->ip);
742 g_free(candidate->base_ip);
743 g_free(candidate->username);
744 g_free(candidate->password);
745 g_free(candidate);
749 void
750 sdpcodec_free(struct sdpcodec *codec)
752 if (codec) {
753 g_free(codec->name);
754 sipe_utils_nameval_free(codec->parameters);
755 g_free(codec);
759 void
760 sdpmedia_free(struct sdpmedia *media)
762 if (media) {
763 g_free(media->name);
764 g_free(media->ip);
766 sipe_utils_nameval_free(media->attributes);
768 sipe_utils_slist_free_full(media->candidates,
769 (GDestroyNotify) sdpcandidate_free);
770 sipe_utils_slist_free_full(media->codecs,
771 (GDestroyNotify) sdpcodec_free);
772 sipe_utils_slist_free_full(media->remote_candidates,
773 (GDestroyNotify) sdpcandidate_free);
775 g_free(media);
779 void
780 sdpmsg_free(struct sdpmsg *msg)
782 if (msg) {
783 g_free(msg->ip);
784 sipe_utils_slist_free_full(msg->media,
785 (GDestroyNotify) sdpmedia_free);
786 g_free(msg);
791 Local Variables:
792 mode: c
793 c-file-style: "bsd"
794 indent-tabs-mode: t
795 tab-width: 8
796 End: