sdpmsg: don't send farsight-send-profile codec property
[siplcs.git] / src / core / sdpmsg.c
blob767497defe225a267042e4008244aab5dcc11fd6
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 return g_slist_append(candidates, candidate);
200 static GSList *
201 parse_candidates(GSList *attrs, SipeIceVersion *ice_version)
203 GSList *candidates = NULL;
204 const gchar *attr;
205 int i = 0;
207 while ((attr = sipe_utils_nameval_find_instance(attrs, "candidate", i++))) {
208 gchar **tokens = g_strsplit_set(attr, " ", 0);
210 if (sipe_strequal(tokens[6], "typ")) {
211 candidates = parse_append_candidate_rfc_5245(tokens, candidates);
212 if (candidates)
213 *ice_version = SIPE_ICE_RFC_5245;
214 } else {
215 candidates = parse_append_candidate_draft_6(tokens, candidates);
216 if (candidates)
217 *ice_version = SIPE_ICE_DRAFT_6;
220 g_strfreev(tokens);
223 if (!candidates)
224 *ice_version = SIPE_ICE_NO_ICE;
226 if (*ice_version == SIPE_ICE_RFC_5245) {
227 const gchar *username = sipe_utils_nameval_find(attrs, "ice-ufrag");
228 const gchar *password = sipe_utils_nameval_find(attrs, "ice-pwd");
230 if (username && password) {
231 GSList *i;
232 for (i = candidates; i; i = i->next) {
233 struct sdpcandidate *c = i->data;
234 c->username = g_strdup(username);
235 c->password = g_strdup(password);
240 return candidates;
243 static GSList *
244 create_legacy_candidates(gchar *ip, guint16 port)
246 struct sdpcandidate *candidate;
247 GSList *candidates = NULL;
249 candidate = g_new0(struct sdpcandidate, 1);
250 candidate->foundation = g_strdup("1");
251 candidate->component = SIPE_COMPONENT_RTP;
252 candidate->type = SIPE_CANDIDATE_TYPE_HOST;
253 candidate->protocol = SIPE_NETWORK_PROTOCOL_UDP;
254 candidate->ip = g_strdup(ip);
255 candidate->port = port;
257 candidates = g_slist_append(candidates, candidate);
259 candidate = g_new0(struct sdpcandidate, 1);
260 candidate->foundation = g_strdup("1");
261 candidate->component = SIPE_COMPONENT_RTCP;
262 candidate->type = SIPE_CANDIDATE_TYPE_HOST;
263 candidate->protocol = SIPE_NETWORK_PROTOCOL_UDP;
264 candidate->ip = g_strdup(ip);
265 candidate->port = port + 1;
267 candidates = g_slist_append(candidates, candidate);
269 return candidates;
272 static GSList *
273 parse_codecs(GSList *attrs, SipeMediaType type)
275 int i = 0;
276 const gchar *attr;
277 GSList *codecs = NULL;
279 while ((attr = sipe_utils_nameval_find_instance(attrs, "rtpmap", i++))) {
280 struct sdpcodec *codec = g_new0(struct sdpcodec, 1);
281 gchar **tokens = g_strsplit_set(attr, " /", 3);
283 int j = 0;
284 const gchar* params;
286 codec->id = atoi(tokens[0]);
287 codec->name = g_strdup(tokens[1]);
288 codec->clock_rate = atoi(tokens[2]);
289 codec->type = type;
291 // TODO: more secure and effective implementation
292 while((params = sipe_utils_nameval_find_instance(attrs, "fmtp", j++))) {
293 gchar **tokens = g_strsplit_set(params, " ", 0);
294 gchar **next = tokens + 1;
296 if (atoi(tokens[0]) == codec->id) {
297 while (*next) {
298 gchar name[50];
299 gchar value[50];
301 if (sscanf(*next, "%[a-zA-Z0-9]=%s", name, value) == 2)
302 codec->parameters = sipe_utils_nameval_add(codec->parameters, name, value);
304 ++next;
308 g_strfreev(tokens);
311 codecs = g_slist_append(codecs, codec);
312 g_strfreev(tokens);
315 return codecs;
318 struct sdpmsg *
319 sdpmsg_parse_msg(gchar *msg)
321 struct sdpmsg *smsg = g_new0(struct sdpmsg, 1);
322 GSList *i;
324 if (!parse_attributes(smsg, msg)) {
325 sdpmsg_free(smsg);
326 return NULL;
329 for (i = smsg->media; i; i = i->next) {
330 struct sdpmedia *media = i->data;
331 SipeMediaType type;
333 media->candidates = parse_candidates(media->attributes,
334 &smsg->ice_version);
336 if (!media->candidates && media->port != 0) {
337 // No a=candidate in SDP message, this seems to be MSOC 2005
338 media->candidates = create_legacy_candidates(smsg->ip, media->port);
341 if (sipe_strequal(media->name, "audio"))
342 type = SIPE_MEDIA_AUDIO;
343 else if (sipe_strequal(media->name, "video"))
344 type = SIPE_MEDIA_VIDEO;
345 else {
346 // Unknown media type
347 sdpmsg_free(smsg);
348 return NULL;
351 media->codecs = parse_codecs(media->attributes, type);
354 return smsg;
357 static gchar *
358 codecs_to_string(GSList *codecs)
360 GString *result = g_string_new(NULL);
362 for (; codecs; codecs = codecs->next) {
363 struct sdpcodec *c = codecs->data;
364 GSList *params = c->parameters;
366 g_string_append_printf(result,
367 "a=rtpmap:%d %s/%d\r\n",
368 c->id,
369 c->name,
370 c->clock_rate);
372 if (params) {
373 GString *param_str = g_string_new(NULL);
374 int written_params = 0;
376 g_string_append_printf(param_str, "a=fmtp:%d", c->id);
378 for (; params; params = params->next) {
379 struct sipnameval* par = params->data;
380 if (sipe_strequal(par->name, "farsight-send-profile")) {
381 // Lync AVMCU doesn't like this property.
382 continue;
385 g_string_append_printf(param_str, " %s=%s",
386 par->name, par->value);
387 ++written_params;
390 g_string_append(param_str, "\r\n");
392 if (written_params > 0) {
393 g_string_append(result, param_str->str);
396 g_string_free(param_str, TRUE);
400 return g_string_free(result, FALSE);
403 static gchar *
404 codec_ids_to_string(GSList *codecs)
406 GString *result = g_string_new(NULL);
408 for (; codecs; codecs = codecs->next) {
409 struct sdpcodec *c = codecs->data;
410 g_string_append_printf(result, " %d", c->id);
413 return g_string_free(result, FALSE);
416 static gchar *
417 base64_unpad(const gchar *str)
419 gchar *result = g_strdup(str);
420 gchar *ptr;
422 for (ptr = result + strlen(result); ptr != result; --ptr) {
423 if (*(ptr - 1) != '=') {
424 *ptr = '\0';
425 break;
429 return result;
432 static gchar *
433 candidates_to_string(GSList *candidates, SipeIceVersion ice_version)
435 GString *result = g_string_new("");
436 GSList *i;
437 GSList *processed_tcp_candidates = NULL;
439 for (i = candidates; i; i = i->next) {
440 struct sdpcandidate *c = i->data;
441 const gchar *protocol;
442 const gchar *type;
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";
450 break;
451 case SIPE_NETWORK_PROTOCOL_TCP_PASSIVE:
452 protocol = "TCP-PASS";
453 break;
454 case SIPE_NETWORK_PROTOCOL_UDP:
455 protocol = "UDP";
456 break;
457 default:
458 /* error unknown/unsupported type */
459 protocol = "UNKNOWN";
460 break;
463 switch (c->type) {
464 case SIPE_CANDIDATE_TYPE_HOST:
465 type = "host";
466 break;
467 case SIPE_CANDIDATE_TYPE_RELAY:
468 type = "relay";
469 related = g_strdup_printf("raddr %s rport %d ",
470 c->base_ip,
471 c->base_port);
472 break;
473 case SIPE_CANDIDATE_TYPE_SRFLX:
474 type = "srflx";
475 related = g_strdup_printf("raddr %s rport %d",
476 c->base_ip,
477 c->base_port);
478 break;
479 case SIPE_CANDIDATE_TYPE_PRFLX:
480 type = "prflx";
481 break;
482 default:
483 /* error unknown/unsupported type */
484 type = "unknown";
485 break;
488 g_string_append_printf(result,
489 "a=candidate:%s %u %s %u %s %d typ %s %s\r\n",
490 c->foundation,
491 c->component,
492 protocol,
493 c->priority,
494 c->ip,
495 c->port,
496 type,
497 related ? related : "");
498 g_free(related);
500 } else if (ice_version == SIPE_ICE_DRAFT_6) {
501 gchar *username;
502 gchar *password;
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) {
513 break;
517 if (prev_cand) {
518 protocol = NULL;
519 } else {
520 protocol = "TCP";
521 processed_tcp_candidates =
522 g_slist_append(processed_tcp_candidates, c);
524 break;
526 case SIPE_NETWORK_PROTOCOL_UDP:
527 protocol = "UDP";
528 break;
529 default:
530 /* unknown/unsupported type, ignore */
531 protocol = NULL;
532 break;
535 if (!protocol) {
536 continue;
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",
544 username,
545 c->component,
546 password,
547 protocol,
548 c->priority,
549 c->ip,
550 c->port);
552 g_free(username);
553 g_free(password);
557 g_slist_free(processed_tcp_candidates);
559 return g_string_free(result, FALSE);
562 static gchar *
563 remote_candidates_to_string(GSList *candidates, SipeIceVersion ice_version)
565 GString *result = g_string_new("");
567 if (candidates) {
568 if (ice_version == SIPE_ICE_RFC_5245) {
569 GSList *i;
570 g_string_append(result, "a=remote-candidates:");
572 for (i = candidates; i; i = i->next) {
573 struct sdpcandidate *c = i->data;
574 g_string_append_printf(result, "%u %s %u ",
575 c->component, c->ip, c->port);
578 g_string_append(result, "\r\n");
579 } else if (ice_version == SIPE_ICE_DRAFT_6) {
580 struct sdpcandidate *c = candidates->data;
581 g_string_append_printf(result, "a=remote-candidate:%s\r\n",
582 c->username);
586 return g_string_free(result, FALSE);
589 static gchar *
590 attributes_to_string(GSList *attributes)
592 GString *result = g_string_new("");
594 for (; attributes; attributes = attributes->next) {
595 struct sipnameval *a = attributes->data;
596 g_string_append_printf(result, "a=%s", a->name);
597 if (!sipe_strequal(a->value, ""))
598 g_string_append_printf(result, ":%s", a->value);
599 g_string_append(result, "\r\n");
602 return g_string_free(result, FALSE);
605 static gchar *
606 media_to_string(const struct sdpmsg *msg, const struct sdpmedia *media)
608 gchar *media_str;
610 gchar *media_conninfo = NULL;
612 gchar *codecs_str = NULL;
613 gchar *codec_ids_str = codec_ids_to_string(media->codecs);
615 gchar *candidates_str = NULL;
616 gchar *remote_candidates_str = NULL;
618 gchar *tcp_setup_str = NULL;
619 gchar *attributes_str = NULL;
620 gchar *credentials = NULL;
622 gboolean uses_tcp_transport = FALSE;
624 if (media->port != 0) {
625 if (!sipe_strequal(msg->ip, media->ip)) {
626 media_conninfo = g_strdup_printf("c=IN IP4 %s\r\n", media->ip);
629 codecs_str = codecs_to_string(media->codecs);
630 candidates_str = candidates_to_string(media->candidates, msg->ice_version);
631 remote_candidates_str = remote_candidates_to_string(media->remote_candidates,
632 msg->ice_version);
634 if (media->remote_candidates) {
635 struct sdpcandidate *c = media->remote_candidates->data;
636 uses_tcp_transport =
637 c->protocol == SIPE_NETWORK_PROTOCOL_TCP_ACTIVE ||
638 c->protocol == SIPE_NETWORK_PROTOCOL_TCP_PASSIVE;
639 if (uses_tcp_transport) {
640 tcp_setup_str = g_strdup_printf(
641 "a=connection:existing\r\n"
642 "a=setup:%s\r\n",
643 (c->protocol == SIPE_NETWORK_PROTOCOL_TCP_ACTIVE) ? "passive" : "active");
647 attributes_str = attributes_to_string(media->attributes);
649 if (msg->ice_version == SIPE_ICE_RFC_5245 && media->candidates) {
650 struct sdpcandidate *c = media->candidates->data;
652 credentials = g_strdup_printf("a=ice-ufrag:%s\r\n"
653 "a=ice-pwd:%s\r\n",
654 c->username,
655 c->password);
659 media_str = g_strdup_printf("m=%s %d %sRTP/AVP%s\r\n"
660 "%s"
661 "%s"
662 "%s"
663 "%s"
664 "%s"
665 "%s"
666 "%s",
667 media->name, media->port, uses_tcp_transport ? "TCP/" : "", codec_ids_str,
668 media_conninfo ? media_conninfo : "",
669 candidates_str ? candidates_str : "",
670 remote_candidates_str ? remote_candidates_str : "",
671 tcp_setup_str ? tcp_setup_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(tcp_setup_str);
682 g_free(attributes_str);
683 g_free(credentials);
685 return media_str;
688 gchar *
689 sdpmsg_to_string(const struct sdpmsg *msg)
691 GString *body = g_string_new(NULL);
692 GSList *i;
694 g_string_append_printf(
695 body,
696 "v=0\r\n"
697 "o=- 0 0 IN IP4 %s\r\n"
698 "s=session\r\n"
699 "c=IN IP4 %s\r\n"
700 "b=CT:99980\r\n"
701 "t=0 0\r\n",
702 msg->ip, msg->ip);
705 for (i = msg->media; i; i = i->next) {
706 gchar *media_str = media_to_string(msg, i->data);
707 g_string_append(body, media_str);
708 g_free(media_str);
711 return g_string_free(body, FALSE);
714 static struct sdpcandidate *
715 sdpcandidate_copy(struct sdpcandidate *candidate)
717 if (candidate) {
718 struct sdpcandidate *copy = g_new0(struct sdpcandidate, 1);
720 copy->foundation = g_strdup(candidate->foundation);
721 copy->component = candidate->component;
722 copy->type = candidate->type;
723 copy->protocol = candidate->protocol;
724 copy->priority = candidate->priority;
725 copy->ip = g_strdup(candidate->ip);
726 copy->port = candidate->port;
727 copy->base_ip = g_strdup(candidate->base_ip);
728 copy->base_port = candidate->base_port;
729 copy->username = g_strdup(candidate->username);
730 copy->password = g_strdup(candidate->password);
732 return copy;
733 } else
734 return NULL;
737 static void
738 sdpcandidate_free(struct sdpcandidate *candidate)
740 if (candidate) {
741 g_free(candidate->foundation);
742 g_free(candidate->ip);
743 g_free(candidate->base_ip);
744 g_free(candidate->username);
745 g_free(candidate->password);
746 g_free(candidate);
750 void
751 sdpcodec_free(struct sdpcodec *codec)
753 if (codec) {
754 g_free(codec->name);
755 sipe_utils_nameval_free(codec->parameters);
756 g_free(codec);
760 void
761 sdpmedia_free(struct sdpmedia *media)
763 if (media) {
764 g_free(media->name);
765 g_free(media->ip);
767 sipe_utils_nameval_free(media->attributes);
769 sipe_utils_slist_free_full(media->candidates,
770 (GDestroyNotify) sdpcandidate_free);
771 sipe_utils_slist_free_full(media->codecs,
772 (GDestroyNotify) sdpcodec_free);
773 sipe_utils_slist_free_full(media->remote_candidates,
774 (GDestroyNotify) sdpcandidate_free);
776 g_free(media);
780 void
781 sdpmsg_free(struct sdpmsg *msg)
783 if (msg) {
784 g_free(msg->ip);
785 sipe_utils_slist_free_full(msg->media,
786 (GDestroyNotify) sdpmedia_free);
787 g_free(msg);
792 Local Variables:
793 mode: c
794 c-file-style: "bsd"
795 indent-tabs-mode: t
796 tab-width: 8
797 End: