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 *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 if (desc->streams[0].ice_check_list != NULL) {
148 const IceCandidate *candidate = ice_check_list_default_local_candidate(desc->streams[0].ice_check_list);
149 if (candidate != NULL) {
150 addr=candidate->taddr.ip;
153 if(!sal_media_description_has_dir (desc,SalStreamSendOnly))
155 sdp_message_c_connection_add (local, -1,
156 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
157 osip_strdup (addr), NULL, NULL);
161 sdp_message_c_connection_add (local, -1,
162 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
163 inet6 ? osip_strdup ("::0") : osip_strdup ("0.0.0.0"), NULL, NULL);
165 sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
166 if (desc->bandwidth>0) sdp_message_b_bandwidth_add (local, -1, osip_strdup ("AS"),
167 int_2char(desc->bandwidth));
168 if (desc->streams[0].ice_check_list != NULL) {
170 snprintf(buffer ,sizeof(buffer), "%s", ice_session_local_pwd(desc->streams[0].ice_check_list->session));
171 sdp_message_a_attribute_add(local, -1, osip_strdup("ice-pwd"), osip_strdup(buffer));
172 snprintf(buffer ,sizeof(buffer), "%s", ice_session_local_ufrag(desc->streams[0].ice_check_list->session));
173 sdp_message_a_attribute_add(local, -1, osip_strdup("ice-ufrag"), osip_strdup(buffer));
180 static bool_t is_known_rtpmap(const PayloadType *pt){
181 switch(payload_type_get_number(pt)){
191 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt, bool_t strip_well_known_rtpmaps)
194 sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
196 if (!strip_well_known_rtpmaps || !is_known_rtpmap(pt)){
198 snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt),
199 pt->mime_type, pt->clock_rate,pt->channels);
201 snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt),
202 pt->mime_type, pt->clock_rate);
203 sdp_message_a_attribute_add (msg, line,
204 osip_strdup ("rtpmap"), osip_strdup(attr));
207 if (pt->recv_fmtp != NULL)
209 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
210 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
215 static void add_ice_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc)
218 const IceCandidate *candidate;
221 if (desc->ice_check_list != NULL) {
222 for (i = 0; i < ms_list_size(desc->ice_check_list->local_candidates); i++) {
223 candidate = ms_list_nth_data(desc->ice_check_list->local_candidates, i);
224 snprintf(buffer, sizeof(buffer), "%s %d UDP %d %s %d typ %s",
225 candidate->foundation, candidate->componentID, candidate->priority, candidate->taddr.ip, candidate->taddr.port, ice_candidate_type(candidate));
226 sdp_message_a_attribute_add(msg, lineno, osip_strdup("candidate"), osip_strdup(buffer));
232 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc){
236 const char *dir="sendrecv";
238 bool_t strip_well_known_rtpmaps;
240 switch (desc->type) {
253 if (desc->ice_check_list != NULL) {
254 const IceCandidate *candidate = ice_check_list_default_local_candidate(desc->ice_check_list);
255 if (candidate != NULL) {
256 addr=candidate->taddr.ip;
257 port=candidate->taddr.port;
259 } else if (desc->candidates[0].addr[0]!='\0'){
260 addr=desc->candidates[0].addr;
261 port=desc->candidates[0].port;
264 /*only add a c= line within the stream description if address are differents*/
265 if (strcmp(addr,sdp_message_c_addr_get(msg, -1, 0))!=0){
267 if (strchr(addr,':')!=NULL){
270 sdp_message_c_connection_add (msg, lineno,
271 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
272 osip_strdup (addr), NULL, NULL);
275 if (desc->proto == SalProtoRtpSavp) {
278 sdp_message_m_media_add (msg, osip_strdup (mt),
279 int_2char (port), NULL,
280 osip_strdup ("RTP/SAVP"));
282 /* add crypto lines */
283 for(i=0; i<SAL_CRYPTO_ALGO_MAX; i++) {
285 switch (desc->crypto[i].algo) {
286 case AES_128_SHA1_80:
287 snprintf(buffer, 1024, "%d %s inline:%s",
288 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_80", desc->crypto[i].master_key);
289 sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
290 osip_strdup(buffer));
292 case AES_128_SHA1_32:
293 snprintf(buffer, 1024, "%d %s inline:%s",
294 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_32", desc->crypto[i].master_key);
295 sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
296 osip_strdup(buffer));
298 case AES_128_NO_AUTH:
299 ms_warning("Unsupported crypto suite: AES_128_NO_AUTH");
301 case NO_CIPHER_SHA1_80:
302 ms_warning("Unsupported crypto suite: NO_CIPHER_SHA1_80");
305 i = SAL_CRYPTO_ALGO_MAX;
310 sdp_message_m_media_add (msg, osip_strdup (mt),
311 int_2char (port), NULL,
312 osip_strdup ("RTP/AVP"));
315 if (desc->bandwidth>0) sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
316 int_2char(desc->bandwidth));
317 if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"),
318 int_2char(desc->ptime));
319 strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5;
321 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
322 add_payload(msg, lineno, (PayloadType*)elem->data,strip_well_known_rtpmaps);
325 /* to comply with SDP we cannot have an empty payload type number list */
326 /* as it happens only when mline is declined with a zero port, it does not matter to put whatever codec*/
327 sdp_message_m_payload_add (msg,lineno, int_2char (0));
330 case SalStreamSendRecv:
334 case SalStreamRecvOnly:
337 case SalStreamSendOnly:
340 case SalStreamInactive:
344 if (dir) sdp_message_a_attribute_add (msg, lineno, osip_strdup (dir),NULL);
345 add_ice_candidates(msg, lineno, desc);
349 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc){
351 sdp_message_t *msg=create_generic_sdp(desc);
352 for(i=0;i<desc->nstreams;++i){
353 add_line(msg,i,&desc->streams[i]);
358 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
360 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
362 pt->mime_type=ms_strdup(refpt->mime_type);
363 pt->clock_rate=refpt->clock_rate;
365 ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
366 payload_type_get_number(pt));
370 char *mime=ms_strdup(rtpmap);
371 char *p=strchr(mime,'/');
380 pt->channels=atoi(chans);
381 }else pt->channels=1;
382 pt->clock_rate=atoi(p);
389 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){
391 const char *mtype,*proto,*port,*addr,*number;
392 const char *ice_ufrag, *ice_pwd;
393 IceSession *ice_session = NULL;
394 sdp_bandwidth_t *sbw=NULL;
395 sdp_attribute_t *attr;
396 int media_attribute_nb;
397 bool_t ice_lite = FALSE;
399 addr=sdp_message_c_addr_get (msg, -1, 0);
401 strncpy(desc->addr,addr,sizeof(desc->addr));
402 for(j=0;(sbw=sdp_message_bandwidth_get(msg,-1,j))!=NULL;++j){
403 if (strcasecmp(sbw->b_bwtype,"AS")==0) desc->bandwidth=atoi(sbw->b_bandwidth);
406 /* for each m= line */
407 for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
409 SalStreamDescription *stream=&desc->streams[i];
411 memset(stream,0,sizeof(*stream));
412 mtype = sdp_message_m_media_get(msg, i);
413 proto = sdp_message_m_proto_get (msg, i);
414 port = sdp_message_m_port_get(msg, i);
415 stream->proto=SalProtoUnknown;
417 if (strcasecmp(proto,"RTP/AVP")==0)
418 stream->proto=SalProtoRtpAvp;
419 else if (strcasecmp(proto,"RTP/SAVP")==0){
420 stream->proto=SalProtoRtpSavp;
423 addr = sdp_message_c_addr_get (msg, i, 0);
425 strncpy(stream->addr,addr,sizeof(stream->addr));
427 stream->port=atoi(port);
429 stream->ptime=_sdp_message_get_a_ptime(msg,i);
430 if (strcasecmp("audio", mtype) == 0){
431 stream->type=SalAudio;
432 }else if (strcasecmp("video", mtype) == 0){
433 stream->type=SalVideo;
435 stream->type=SalOther;
436 strncpy(stream->typeother,mtype,sizeof(stream->typeother)-1);
438 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
439 if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
441 stream->dir=_sdp_message_get_mline_dir(msg,i);
442 media_attribute_nb = 0;
443 /* for each payload type */
444 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
445 const char *rtpmap,*fmtp;
446 int ptn=atoi(number);
447 PayloadType *pt=payload_type_new();
448 payload_type_set_number(pt,ptn);
449 /* get the rtpmap associated to this codec, if any */
450 rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
451 if (rtpmap != NULL) media_attribute_nb++;
452 if (payload_type_fill_from_rtpmap(pt,rtpmap)==0){
453 /* get the fmtp, if any */
454 fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
455 if (fmtp != NULL) media_attribute_nb++;
456 payload_type_set_send_fmtp(pt,fmtp);
457 stream->payloads=ms_list_append(stream->payloads,pt);
458 ms_message("Found payload %s/%i fmtp=%s",pt->mime_type,pt->clock_rate,
459 pt->send_fmtp ? pt->send_fmtp : "");
463 /* read crypto lines if any */
464 if (stream->proto == SalProtoRtpSavp) {
465 int k, valid_count = 0;
467 memset(&stream->crypto, 0, sizeof(stream->crypto));
468 for (k=0;valid_count < SAL_CRYPTO_ALGO_MAX && (attr=sdp_message_attribute_get(msg,i,k))!=NULL;k++){
469 char tmp[256], tmp2[256];
470 if (keywordcmp("crypto",attr->a_att_field)==0 && attr->a_att_value!=NULL){
471 int nb = sscanf(attr->a_att_value, "%d %256s inline:%256s",
472 &stream->crypto[valid_count].tag,
475 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'",
476 stream->crypto[valid_count].tag,
480 if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_80") == 0)
481 stream->crypto[valid_count].algo = AES_128_SHA1_80;
482 else if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_32") == 0)
483 stream->crypto[valid_count].algo = AES_128_SHA1_32;
485 ms_warning("Failed to parse crypto-algo: '%s'", tmp);
486 stream->crypto[valid_count].algo = 0;
488 if (stream->crypto[valid_count].algo) {
489 strncpy(stream->crypto[valid_count].master_key, tmp2, 41);
490 stream->crypto[valid_count].master_key[40] = '\0';
491 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'",
492 stream->crypto[valid_count].tag,
494 stream->crypto[valid_count].master_key);
498 ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
502 ms_message("Found: %d valid crypto lines", valid_count);
505 /* Get ICE candidate attributes if any */
506 ice_ufrag = ice_pwd = NULL;
507 for (j = 0; (j < SAL_MEDIA_DESCRIPTION_MAX_ICE_CANDIDATES) && ((attr = sdp_message_attribute_get(msg, i, media_attribute_nb + j)) != NULL); j++) {
508 if ((keywordcmp("candidate", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
512 unsigned int priority;
513 unsigned int componentID;
517 /* Allocate the ICE session if it has not been done yet. */
518 if (ice_session == NULL) ice_session = ice_session_new();
519 /* Allocate the ICE check list if it has not been done yet. */
520 if (desc->streams[i].ice_check_list == NULL) {
521 desc->streams[i].ice_check_list = ice_check_list_new();
522 ice_session_add_check_list(ice_session, desc->streams[i].ice_check_list);
524 nb = sscanf(attr->a_att_value, "%s %u UDP %u %s %u typ %s",
525 foundation, &componentID, &priority, ip, &port, type);
527 ice_add_remote_candidate(desc->streams[i].ice_check_list, type, ip, port, componentID, priority, foundation);
529 } else if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
530 ice_ufrag = attr->a_att_value;
531 } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
532 ice_pwd = attr->a_att_value;
535 if ((ice_ufrag != NULL) && (ice_pwd != NULL)) {
536 ice_check_list_set_remote_credentials(desc->streams[i].ice_check_list, ice_ufrag, ice_pwd);
538 if (desc->streams[i].ice_check_list) ice_dump_candidates(desc->streams[i].ice_check_list);
540 /* Get ICE remote ufrag and remote pwd */
541 ice_ufrag = ice_pwd = NULL;
542 for (i = 0; (i < SAL_MEDIA_DESCRIPTION_MAX_MESSAGE_ATTRIBUTES) && ((attr = sdp_message_attribute_get(msg, -1, i)) != NULL); i++) {
543 if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
544 ice_ufrag = attr->a_att_value;
545 } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
546 ice_pwd = attr->a_att_value;
547 } else if (keywordcmp("ice-lite", attr->a_att_field) == 0) {
551 if (ice_session != NULL) {
552 if (ice_lite == TRUE) {
553 ice_session_set_role(ice_session, IR_Controlling);
555 ice_session_set_role(ice_session, IR_Controlled);
557 if ((ice_ufrag != NULL) && (ice_pwd != NULL)) {
558 ice_session_set_remote_credentials(ice_session, ice_ufrag, ice_pwd);
559 ice_dump_session(ice_session);