3 Copyright (C) 2010 Simon MORLAT (simon.morlat@free.fr)
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "ortp/ortp_srtp.h"
24 #include <eXosip2/eXosip.h>
26 #define keywordcmp(key,b) strncmp(key,b,sizeof(key))
30 static char *make_relay_session_id(const char *username, const char *relay){
31 /*ideally this should be a hash of the parameters with a random part*/
35 long long int res=((long long int)s1)<<32 | (long long int) s2;
37 b64_encode(src, sizeof(long long int), tmp, sizeof(tmp));
38 return osip_strdup(tmp);
42 static void add_relay_info(sdp_message_t *sdp, int mline, const char *relay, const char *relay_session_id){
44 if (relay) sdp_message_a_attribute_add(sdp, mline,
45 osip_strdup ("relay-addr"),osip_strdup(relay));
46 if (relay_session_id) sdp_message_a_attribute_add(sdp, mline,
47 osip_strdup ("relay-session-id"), osip_strdup(relay_session_id));
52 static char * int_2char(int a){
53 char *p=osip_malloc(16);
54 snprintf(p,16,"%i",a);
58 /* return the value of attr "field" for payload pt at line pos (field=rtpmap,fmtp...)*/
59 static const char *sdp_message_a_attr_value_get_with_pt(sdp_message_t *sdp,int pos,int pt,const char *field)
61 int i,tmppt=0,scanned=0;
63 sdp_attribute_t *attr;
64 for (i=0;(attr=sdp_message_attribute_get(sdp,pos,i))!=NULL;i++){
65 if (keywordcmp(field,attr->a_att_field)==0 && attr->a_att_value!=NULL){
66 int nb = sscanf(attr->a_att_value,"%i %n",&tmppt,&scanned);
67 /* the return value may depend on how %n is interpreted by the libc: see manpage*/
68 if (nb == 1 || nb==2 ){
70 tmp=attr->a_att_value+scanned;
74 }else ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
81 /* return the value of attr "field" */
82 static const char *sdp_message_a_attr_value_get(sdp_message_t *sdp,int pos,const char *field)
85 sdp_attribute_t *attr;
86 for (i=0;(attr=sdp_message_attribute_get(sdp,pos,i))!=NULL;i++){
87 if (keywordcmp(field,attr->a_att_field)==0 && attr->a_att_value!=NULL){
88 return attr->a_att_value;
95 static int _sdp_message_get_a_ptime(sdp_message_t *sdp, int mline){
97 sdp_attribute_t *attr;
98 for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){
99 if (keywordcmp("ptime",attr->a_att_field)==0){
100 int nb = sscanf(attr->a_att_value,"%i",&ret);
101 /* the return value may depend on how %n is interpreted by the libc: see manpage*/
104 }else ms_warning("sdp has a strange a=ptime line (%s) ",attr->a_att_value);
110 static int _sdp_message_get_mline_dir(sdp_message_t *sdp, int mline){
112 sdp_attribute_t *attr;
113 for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){
114 if (keywordcmp("sendrecv",attr->a_att_field)==0){
115 return SalStreamSendRecv;
116 }else if (keywordcmp("sendonly",attr->a_att_field)==0){
117 return SalStreamSendOnly;
118 }else if (keywordcmp("recvonly",attr->a_att_field)==0){
119 return SalStreamRecvOnly;
120 }else if (keywordcmp("inactive",attr->a_att_field)==0){
121 return SalStreamInactive;
124 return SalStreamSendRecv;
127 static sdp_message_t *create_generic_sdp(const SalMediaDescription *desc)
129 sdp_message_t *local;
133 const char *rtp_addr = desc->addr;
135 snprintf(sessid,16,"%i",desc->session_id);
136 snprintf(sessver,16,"%i",desc->session_ver);
137 sdp_message_init (&local);
138 if (strchr(desc->addr,':')!=NULL){
141 sdp_message_v_version_set (local, osip_strdup ("0"));
142 sdp_message_o_origin_set (local, osip_strdup (desc->username),
143 osip_strdup (sessid), osip_strdup (sessver),
144 osip_strdup ("IN"), inet6 ? osip_strdup("IP6") : osip_strdup ("IP4"),
145 osip_strdup (desc->addr));
146 sdp_message_s_name_set (local, osip_strdup ("Talk"));
147 /* Do not set the c= line to 0.0.0.0 if there is an ICE session. */
148 if((desc->ice_ufrag[0] != '\0') || !sal_media_description_has_dir (desc,SalStreamSendOnly))
150 sdp_message_c_connection_add (local, -1,
151 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
152 osip_strdup (rtp_addr), NULL, NULL);
156 sdp_message_c_connection_add (local, -1,
157 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
158 inet6 ? osip_strdup ("::0") : osip_strdup ("0.0.0.0"), NULL, NULL);
160 sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
161 if (desc->bandwidth>0) sdp_message_b_bandwidth_add (local, -1, osip_strdup ("AS"),
162 int_2char(desc->bandwidth));
163 if (desc->ice_completed == TRUE) sdp_message_a_attribute_add(local, -1, osip_strdup("nortpproxy"), osip_strdup("yes"));
164 if (desc->ice_pwd[0] != '\0') sdp_message_a_attribute_add(local, -1, osip_strdup("ice-pwd"), osip_strdup(desc->ice_pwd));
165 if (desc->ice_ufrag[0] != '\0') sdp_message_a_attribute_add(local, -1, osip_strdup("ice-ufrag"), osip_strdup(desc->ice_ufrag));
171 static bool_t is_known_rtpmap(const PayloadType *pt){
172 switch(payload_type_get_number(pt)){
182 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt, bool_t strip_well_known_rtpmaps)
185 sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
187 if (!strip_well_known_rtpmaps || !is_known_rtpmap(pt)){
189 snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt),
190 pt->mime_type, pt->clock_rate,pt->channels);
192 snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt),
193 pt->mime_type, pt->clock_rate);
194 sdp_message_a_attribute_add (msg, line,
195 osip_strdup ("rtpmap"), osip_strdup(attr));
198 if (pt->recv_fmtp != NULL)
200 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
201 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
206 static void add_ice_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc)
209 const SalIceCandidate *candidate;
213 for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_ICE_CANDIDATES; i++) {
214 candidate = &desc->ice_candidates[i];
215 if ((candidate->addr[0] == '\0') || (candidate->port == 0)) break;
216 nb = snprintf(buffer, sizeof(buffer), "%s %u UDP %u %s %d typ %s",
217 candidate->foundation, candidate->componentID, candidate->priority, candidate->addr, candidate->port, candidate->type);
219 ms_error("Cannot add ICE candidate attribute!");
222 if (candidate->raddr[0] != '\0') {
223 nb = snprintf(buffer + nb, sizeof(buffer) - nb, " raddr %s rport %d", candidate->raddr, candidate->rport);
225 ms_error("Cannot add ICE candidate attribute!");
229 sdp_message_a_attribute_add(msg, lineno, osip_strdup("candidate"), osip_strdup(buffer));
233 static void add_ice_remote_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc)
237 const SalIceRemoteCandidate *candidate;
242 for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_ICE_REMOTE_CANDIDATES; i++) {
243 candidate = &desc->ice_remote_candidates[i];
244 if ((candidate->addr[0] != '\0') && (candidate->port != 0)) {
245 offset = snprintf(ptr, buffer + sizeof(buffer) - ptr, "%s%d %s %d", (i > 0) ? " " : "", i + 1, candidate->addr, candidate->port);
247 ms_error("Cannot add ICE remote-candidates attribute!");
253 if (buffer[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("remote-candidates"), osip_strdup(buffer));
256 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc){
259 const char *rtp_addr;
260 const char *rtcp_addr;
261 const char *dir="sendrecv";
264 bool_t strip_well_known_rtpmaps;
265 bool_t different_rtp_and_rtcp_addr;
267 switch (desc->type) {
278 rtp_addr=desc->rtp_addr;
279 rtcp_addr=desc->rtcp_addr;
280 rtp_port=desc->rtp_port;
281 rtcp_port=desc->rtcp_port;
282 if (desc->candidates[0].addr[0]!='\0'){
283 rtp_addr=desc->candidates[0].addr;
284 rtp_port=desc->candidates[0].port;
287 if (desc->proto == SalProtoRtpSavp) {
290 sdp_message_m_media_add (msg, osip_strdup (mt),
291 int_2char (rtp_port), NULL,
292 osip_strdup ("RTP/SAVP"));
294 /* add crypto lines */
295 for(i=0; i<SAL_CRYPTO_ALGO_MAX; i++) {
297 switch (desc->crypto[i].algo) {
298 case AES_128_SHA1_80:
299 snprintf(buffer, 1024, "%d %s inline:%s",
300 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_80", desc->crypto[i].master_key);
301 sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
302 osip_strdup(buffer));
304 case AES_128_SHA1_32:
305 snprintf(buffer, 1024, "%d %s inline:%s",
306 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_32", desc->crypto[i].master_key);
307 sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
308 osip_strdup(buffer));
310 case AES_128_NO_AUTH:
311 ms_warning("Unsupported crypto suite: AES_128_NO_AUTH");
313 case NO_CIPHER_SHA1_80:
314 ms_warning("Unsupported crypto suite: NO_CIPHER_SHA1_80");
317 i = SAL_CRYPTO_ALGO_MAX;
322 sdp_message_m_media_add (msg, osip_strdup (mt),
323 int_2char (rtp_port), NULL,
324 osip_strdup ("RTP/AVP"));
328 /*only add a c= line within the stream description if address are differents*/
329 if (rtp_addr[0]!='\0' && strcmp(rtp_addr,sdp_message_c_addr_get(msg, -1, 0))!=0){
331 if (strchr(rtp_addr,':')!=NULL){
334 sdp_message_c_connection_add (msg, lineno,
335 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
336 osip_strdup (rtp_addr), NULL, NULL);
339 if (desc->bandwidth>0) sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
340 int_2char(desc->bandwidth));
341 if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"),
342 int_2char(desc->ptime));
343 strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5;
345 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
346 add_payload(msg, lineno, (PayloadType*)elem->data,strip_well_known_rtpmaps);
349 /* to comply with SDP we cannot have an empty payload type number list */
350 /* as it happens only when mline is declined with a zero port, it does not matter to put whatever codec*/
351 sdp_message_m_payload_add (msg,lineno, int_2char (0));
354 case SalStreamSendRecv:
358 case SalStreamRecvOnly:
361 case SalStreamSendOnly:
364 case SalStreamInactive:
368 if (dir) sdp_message_a_attribute_add (msg, lineno, osip_strdup (dir),NULL);
370 different_rtp_and_rtcp_addr = (rtcp_addr[0] != '\0') && (strcmp(rtp_addr, rtcp_addr) != 0);
371 if ((rtcp_port != (rtp_port + 1)) || (different_rtp_and_rtcp_addr == TRUE)) {
372 if (different_rtp_and_rtcp_addr == TRUE) {
374 snprintf(buffer, sizeof(buffer), "%u IN IP4 %s", rtcp_port, rtcp_addr);
375 sdp_message_a_attribute_add(msg, lineno, osip_strdup("rtcp"), osip_strdup(buffer));
377 sdp_message_a_attribute_add(msg, lineno, osip_strdup("rtcp"), int_2char(rtcp_port));
381 if (desc->ice_completed == TRUE) {
382 sdp_message_a_attribute_add(msg, lineno, osip_strdup("nortpproxy"), osip_strdup("yes"));
384 if (desc->ice_mismatch == TRUE) {
385 sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-mismatch"), NULL);
387 if (desc->ice_pwd[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-pwd"), osip_strdup(desc->ice_pwd));
388 if (desc->ice_ufrag[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-ufrag"), osip_strdup(desc->ice_ufrag));
389 add_ice_candidates(msg, lineno, desc);
390 add_ice_remote_candidates(msg, lineno, desc);
395 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc){
397 sdp_message_t *msg=create_generic_sdp(desc);
398 for(i=0;i<desc->nstreams;++i){
399 add_line(msg,i,&desc->streams[i]);
404 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
406 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
408 pt->mime_type=ms_strdup(refpt->mime_type);
409 pt->clock_rate=refpt->clock_rate;
411 ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
412 payload_type_get_number(pt));
416 char *mime=ms_strdup(rtpmap);
417 char *p=strchr(mime,'/');
426 pt->channels=atoi(chans);
427 }else pt->channels=1;
428 pt->clock_rate=atoi(p);
435 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){
437 const char *mtype,*proto,*rtp_port,*rtp_addr,*number;
438 sdp_bandwidth_t *sbw=NULL;
439 sdp_attribute_t *attr;
440 int nb_ice_candidates;
442 rtp_addr=sdp_message_c_addr_get (msg, -1, 0);
444 strncpy(desc->addr,rtp_addr,sizeof(desc->addr));
445 for(j=0;(sbw=sdp_message_bandwidth_get(msg,-1,j))!=NULL;++j){
446 if (strcasecmp(sbw->b_bwtype,"AS")==0) desc->bandwidth=atoi(sbw->b_bandwidth);
449 /* Get ICE remote ufrag and remote pwd, and ice_lite flag */
450 for (i = 0; (i < SAL_MEDIA_DESCRIPTION_MAX_MESSAGE_ATTRIBUTES) && ((attr = sdp_message_attribute_get(msg, -1, i)) != NULL); i++) {
451 if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
452 strncpy(desc->ice_ufrag, attr->a_att_value, sizeof(desc->ice_ufrag));
453 } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
454 strncpy(desc->ice_pwd, attr->a_att_value, sizeof(desc->ice_pwd));
455 } else if (keywordcmp("ice-lite", attr->a_att_field) == 0) {
456 desc->ice_lite = TRUE;
460 /* for each m= line */
461 for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
463 SalStreamDescription *stream=&desc->streams[i];
464 nb_ice_candidates = 0;
466 memset(stream,0,sizeof(*stream));
467 mtype = sdp_message_m_media_get(msg, i);
468 proto = sdp_message_m_proto_get (msg, i);
469 rtp_port = sdp_message_m_port_get(msg, i);
470 stream->proto=SalProtoUnknown;
472 if (strcasecmp(proto,"RTP/AVP")==0)
473 stream->proto=SalProtoRtpAvp;
474 else if (strcasecmp(proto,"RTP/SAVP")==0){
475 stream->proto=SalProtoRtpSavp;
478 rtp_addr = sdp_message_c_addr_get (msg, i, 0);
479 if (rtp_addr != NULL)
480 strncpy(stream->rtp_addr,rtp_addr,sizeof(stream->rtp_addr));
482 stream->rtp_port=atoi(rtp_port);
484 stream->ptime=_sdp_message_get_a_ptime(msg,i);
485 if (strcasecmp("audio", mtype) == 0){
486 stream->type=SalAudio;
487 }else if (strcasecmp("video", mtype) == 0){
488 stream->type=SalVideo;
490 stream->type=SalOther;
491 strncpy(stream->typeother,mtype,sizeof(stream->typeother)-1);
493 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
494 if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
496 stream->dir=_sdp_message_get_mline_dir(msg,i);
497 /* for each payload type */
498 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
499 const char *rtpmap,*fmtp;
500 int ptn=atoi(number);
501 PayloadType *pt=payload_type_new();
502 payload_type_set_number(pt,ptn);
503 /* get the rtpmap associated to this codec, if any */
504 rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
505 if (payload_type_fill_from_rtpmap(pt,rtpmap)==0){
506 /* get the fmtp, if any */
507 fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
508 payload_type_set_send_fmtp(pt,fmtp);
509 stream->payloads=ms_list_append(stream->payloads,pt);
510 ms_message("Found payload %s/%i fmtp=%s",pt->mime_type,pt->clock_rate,
511 pt->send_fmtp ? pt->send_fmtp : "");
515 /* Get media specific RTCP attribute */
516 stream->rtcp_port = stream->rtp_port + 1;
517 snprintf(stream->rtcp_addr, sizeof(stream->rtcp_addr), "%s", stream->rtp_addr);
518 for (j = 0; ((attr = sdp_message_attribute_get(msg, i, j)) != NULL); j++) {
519 if ((keywordcmp("rtcp", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
521 int nb = sscanf(attr->a_att_value, "%d IN IP4 %s", &stream->rtcp_port, tmp);
523 /* SDP rtcp attribute only contains the port */
524 } else if (nb == 2) {
525 strncpy(stream->rtcp_addr, tmp, sizeof(stream->rtcp_addr));
527 ms_warning("sdp has a strange a= line (%s) nb=%i", attr->a_att_value, nb);
532 /* read crypto lines if any */
533 if (stream->proto == SalProtoRtpSavp) {
534 int k, valid_count = 0;
536 memset(&stream->crypto, 0, sizeof(stream->crypto));
537 for (k=0;valid_count < SAL_CRYPTO_ALGO_MAX && (attr=sdp_message_attribute_get(msg,i,k))!=NULL;k++){
538 char tmp[256], tmp2[256];
539 if (keywordcmp("crypto",attr->a_att_field)==0 && attr->a_att_value!=NULL){
540 int nb = sscanf(attr->a_att_value, "%d %256s inline:%256s",
541 &stream->crypto[valid_count].tag,
544 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'",
545 stream->crypto[valid_count].tag,
549 if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_80") == 0)
550 stream->crypto[valid_count].algo = AES_128_SHA1_80;
551 else if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_32") == 0)
552 stream->crypto[valid_count].algo = AES_128_SHA1_32;
554 ms_warning("Failed to parse crypto-algo: '%s'", tmp);
555 stream->crypto[valid_count].algo = 0;
557 if (stream->crypto[valid_count].algo) {
558 strncpy(stream->crypto[valid_count].master_key, tmp2, 41);
559 stream->crypto[valid_count].master_key[40] = '\0';
560 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'",
561 stream->crypto[valid_count].tag,
563 stream->crypto[valid_count].master_key);
567 ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
571 ms_message("Found: %d valid crypto lines", valid_count);
574 /* Get ICE candidate attributes if any */
575 for (j = 0; (attr = sdp_message_attribute_get(msg, i, j)) != NULL; j++) {
576 if ((keywordcmp("candidate", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
577 SalIceCandidate *candidate = &stream->ice_candidates[nb_ice_candidates];
578 int nb = sscanf(attr->a_att_value, "%s %u UDP %u %s %d typ %s raddr %s rport %d",
579 candidate->foundation, &candidate->componentID, &candidate->priority, candidate->addr, &candidate->port,
580 candidate->type, candidate->raddr, &candidate->rport);
581 if ((nb == 6) || (nb == 8)) nb_ice_candidates++;
582 else memset(candidate, 0, sizeof(*candidate));
583 } else if ((keywordcmp("remote-candidates", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
584 SalIceRemoteCandidate candidate;
585 unsigned int componentID;
587 char *ptr = attr->a_att_value;
588 while (3 == sscanf(ptr, "%u %s %u%n", &componentID, candidate.addr, &candidate.port, &offset)) {
589 if ((componentID > 0) && (componentID <= SAL_MEDIA_DESCRIPTION_MAX_ICE_REMOTE_CANDIDATES)) {
590 SalIceRemoteCandidate *remote_candidate = &stream->ice_remote_candidates[componentID - 1];
591 strncpy(remote_candidate->addr, candidate.addr, sizeof(remote_candidate->addr));
592 remote_candidate->port = candidate.port;
595 if (ptr[offset] == ' ') ptr += 1;
597 } else if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
598 strncpy(stream->ice_ufrag, attr->a_att_value, sizeof(stream->ice_ufrag));
599 } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
600 strncpy(stream->ice_pwd, attr->a_att_value, sizeof(stream->ice_pwd));
601 } else if (keywordcmp("ice-mismatch", attr->a_att_field) == 0) {
602 stream->ice_mismatch = TRUE;