]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2_sdp.c
Add ICE attributes in the SDP content.
[linphone] / coreapi / sal_eXosip2_sdp.c
1 /*
2 linphone
3 Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)
4
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.
9
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.
14
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.
18 */
19
20
21 #include "ortp/b64.h"
22 #include "ortp/ortp_srtp.h"
23 #include "sal.h"
24 #include <eXosip2/eXosip.h>
25
26 #define keywordcmp(key,b) strncmp(key,b,sizeof(key))
27
28 #ifdef FOR_LATER
29
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*/
32         char tmp[128];
33         int s1=(int)random();
34         int s2=(int)random();
35         long long int res=((long long int)s1)<<32 | (long long int) s2;
36         void *src=&res;
37         b64_encode(src, sizeof(long long int), tmp, sizeof(tmp));
38         return osip_strdup(tmp);
39 }
40
41
42 static void add_relay_info(sdp_message_t *sdp, int mline, const char *relay, const char *relay_session_id){
43
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));
48 }
49
50 #endif
51
52 static char * int_2char(int a){
53         char *p=osip_malloc(16);
54         snprintf(p,16,"%i",a);
55         return p;
56 }
57
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)
60 {
61         int i,tmppt=0,scanned=0;
62         char *tmp;
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 ){
69                                 if (pt==tmppt){
70                                         tmp=attr->a_att_value+scanned;
71                                         if (strlen(tmp)>0)
72                                                 return tmp;
73                                 }
74                         }else ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
75                 }
76         }
77         return NULL;
78 }
79
80 #ifdef FOR_LATER
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)
83 {
84         int i;
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;
89                 }
90         }
91         return NULL;
92 }
93 #endif
94
95 static int _sdp_message_get_a_ptime(sdp_message_t *sdp, int mline){
96         int i,ret;
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*/
102                         if (nb == 1){
103                                 return ret;
104                         }else ms_warning("sdp has a strange a=ptime line (%s) ",attr->a_att_value);
105                 }
106         }
107         return 0;
108 }
109
110 static int _sdp_message_get_mline_dir(sdp_message_t *sdp, int mline){
111         int i;
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;
122                 }
123         }
124         return SalStreamSendRecv;
125 }
126
127 static sdp_message_t *create_generic_sdp(const SalMediaDescription *desc)
128 {
129         sdp_message_t *local;
130         int inet6;
131         char sessid[16];
132         char sessver[16];
133         
134         snprintf(sessid,16,"%i",desc->session_id);
135         snprintf(sessver,16,"%i",desc->session_ver);
136         sdp_message_init (&local);
137         if (strchr(desc->addr,':')!=NULL){
138                 inet6=1;
139         }else inet6=0;
140         sdp_message_v_version_set (local, osip_strdup ("0"));
141         sdp_message_o_origin_set (local, osip_strdup (desc->username),
142                           osip_strdup (sessid), osip_strdup (sessver),
143                           osip_strdup ("IN"), inet6 ? osip_strdup("IP6") : osip_strdup ("IP4"),
144                           osip_strdup (desc->addr));
145         sdp_message_s_name_set (local, osip_strdup ("Talk"));
146         if(!sal_media_description_has_dir (desc,SalStreamSendOnly))
147         {
148                 sdp_message_c_connection_add (local, -1,
149                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
150                                                 osip_strdup (desc->addr), NULL, NULL);
151         }
152         else
153         {
154                 sdp_message_c_connection_add (local, -1,
155                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
156                                                 inet6 ? osip_strdup ("::0") : osip_strdup ("0.0.0.0"), NULL, NULL);
157         }               
158         sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
159         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (local, -1, osip_strdup ("AS"),
160                         int_2char(desc->bandwidth));
161         if (desc->streams[0].ice_check_list != NULL) {
162                 char buffer[512];
163                 snprintf(buffer ,sizeof(buffer), "%s", ice_session_local_pwd(desc->streams[0].ice_check_list->session));
164                 sdp_message_a_attribute_add(local, -1, osip_strdup("ice-pwd"), osip_strdup(buffer));
165                 snprintf(buffer ,sizeof(buffer), "%s", ice_session_local_ufrag(desc->streams[0].ice_check_list->session));
166                 sdp_message_a_attribute_add(local, -1, osip_strdup("ice-ufrag"), osip_strdup(buffer));
167         }
168
169         return local;
170 }
171
172
173 static bool_t is_known_rtpmap(const PayloadType *pt){
174         switch(payload_type_get_number(pt)){
175                 case 0:
176                 case 8:
177                 case 3:
178                 case 34:
179                         return TRUE;
180         }
181         return FALSE;
182 }
183
184 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt, bool_t strip_well_known_rtpmaps)
185 {
186         char attr[256];
187         sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
188
189         if (!strip_well_known_rtpmaps || !is_known_rtpmap(pt)){
190                 if (pt->channels>1)
191                         snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt), 
192                                         pt->mime_type, pt->clock_rate,pt->channels);
193                 else
194                         snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt), 
195                                         pt->mime_type, pt->clock_rate);
196                 sdp_message_a_attribute_add (msg, line,
197                                                  osip_strdup ("rtpmap"), osip_strdup(attr));
198         }
199
200         if (pt->recv_fmtp != NULL)
201         {
202                 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
203                 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
204                                      osip_strdup(attr));
205         }
206 }
207
208 static void add_ice_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc)
209 {
210         char buffer[1024];
211         IceCandidate *candidate;
212         int i;
213
214         if (desc->ice_check_list != NULL) {
215                 for (i = 0; i < ms_list_size(desc->ice_check_list->local_candidates); i++) {
216                         candidate = ms_list_nth_data(desc->ice_check_list->local_candidates, i);
217                         snprintf(buffer, sizeof(buffer), "%s %d UDP %d %s %d typ %s",
218                                 candidate->foundation, candidate->componentID, candidate->priority, candidate->taddr.ip, candidate->taddr.port, ice_candidate_type(candidate));
219                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("candidate"), osip_strdup(buffer));
220                 }
221         }
222 }
223
224
225 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc){
226         const char *mt=NULL;
227         const MSList *elem;
228         const char *addr;
229         const char *dir="sendrecv";
230         int port;
231         bool_t strip_well_known_rtpmaps;
232         
233         switch (desc->type) {
234         case SalAudio:
235                 mt="audio";
236                 break;
237         case SalVideo:
238                 mt="video";
239                 break;
240         case SalOther:
241                 mt=desc->typeother;
242                 break;
243         }
244         if (desc->candidates[0].addr[0]!='\0'){
245                 addr=desc->candidates[0].addr;
246                 port=desc->candidates[0].port;
247         }else{
248                 addr=desc->addr;
249                 port=desc->port;
250         }
251         /*only add a c= line within the stream description if address are differents*/
252         if (strcmp(addr,sdp_message_c_addr_get(msg, -1, 0))!=0){
253                 bool_t inet6;
254                 if (strchr(addr,':')!=NULL){
255                         inet6=TRUE;
256                 }else inet6=FALSE;
257                 sdp_message_c_connection_add (msg, lineno,
258                               osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
259                               osip_strdup (addr), NULL, NULL);
260         }
261         
262         if (desc->proto == SalProtoRtpSavp) {
263                 int i;
264                 
265                 sdp_message_m_media_add (msg, osip_strdup (mt),
266                                          int_2char (port), NULL,
267                                          osip_strdup ("RTP/SAVP"));
268        
269                 /* add crypto lines */
270                 for(i=0; i<SAL_CRYPTO_ALGO_MAX; i++) {
271                         char buffer[1024];
272                         switch (desc->crypto[i].algo) {
273                                 case AES_128_SHA1_80:
274                                         snprintf(buffer, 1024, "%d %s inline:%s",
275                                                 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_80", desc->crypto[i].master_key);
276                                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
277                                                 osip_strdup(buffer));
278                                         break;
279                                 case AES_128_SHA1_32:
280                                         snprintf(buffer, 1024, "%d %s inline:%s",
281                                                 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_32", desc->crypto[i].master_key);
282                                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
283                                                 osip_strdup(buffer));
284                                         break;
285                                 case AES_128_NO_AUTH:
286                                         ms_warning("Unsupported crypto suite: AES_128_NO_AUTH");
287                                         break;
288                                 case NO_CIPHER_SHA1_80:
289                                         ms_warning("Unsupported crypto suite: NO_CIPHER_SHA1_80");
290                                         break; 
291                                 default:
292                                         i = SAL_CRYPTO_ALGO_MAX;
293                         }
294                 }
295                 
296         } else {
297                 sdp_message_m_media_add (msg, osip_strdup (mt),
298                                          int_2char (port), NULL,
299                                          osip_strdup ("RTP/AVP"));
300                 
301         }
302         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
303                                      int_2char(desc->bandwidth));
304         if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"),
305                                 int_2char(desc->ptime));
306         strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5;
307         if (desc->payloads){
308                 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
309                         add_payload(msg, lineno, (PayloadType*)elem->data,strip_well_known_rtpmaps);
310                 }
311         }else{
312                 /* to comply with SDP we cannot have an empty payload type number list */
313                 /* as it happens only when mline is declined with a zero port, it does not matter to put whatever codec*/
314                 sdp_message_m_payload_add (msg,lineno, int_2char (0));
315         }
316         switch(desc->dir){
317                 case SalStreamSendRecv:
318                         /*dir="sendrecv";*/
319                         dir=NULL;
320                 break;
321                 case SalStreamRecvOnly:
322                         dir="recvonly";
323                         break;
324                 case SalStreamSendOnly:
325                         dir="sendonly";
326                         break;
327                 case SalStreamInactive:
328                         dir="inactive";
329                         break;
330         }
331         if (dir) sdp_message_a_attribute_add (msg, lineno, osip_strdup (dir),NULL);
332         add_ice_candidates(msg, lineno, desc);
333 }
334
335
336 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc){
337         int i;
338         sdp_message_t *msg=create_generic_sdp(desc);
339         for(i=0;i<desc->nstreams;++i){
340                 add_line(msg,i,&desc->streams[i]);
341         }
342         return msg;
343 }
344
345 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
346         if (rtpmap==NULL){
347                 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
348                 if (refpt){
349                         pt->mime_type=ms_strdup(refpt->mime_type);
350                         pt->clock_rate=refpt->clock_rate;
351                 }else{
352                         ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
353                             payload_type_get_number(pt));
354                         return -1;
355                 }
356         }else{
357                 char *mime=ms_strdup(rtpmap);
358                 char *p=strchr(mime,'/');
359                 if (p){
360                         char *chans;
361                         *p='\0';
362                         p++;
363                         chans=strchr(p,'/');
364                         if (chans){
365                                 *chans='\0';
366                                 chans++;
367                                 pt->channels=atoi(chans);
368                         }else pt->channels=1;
369                         pt->clock_rate=atoi(p);
370                 }
371                 pt->mime_type=mime;
372         }
373         return 0;
374 }
375
376 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){
377         int i,j;
378         const char *mtype,*proto,*port,*addr,*number;
379         sdp_bandwidth_t *sbw=NULL;
380         
381         addr=sdp_message_c_addr_get (msg, -1, 0);
382         if (addr)
383                 strncpy(desc->addr,addr,sizeof(desc->addr));
384         for(j=0;(sbw=sdp_message_bandwidth_get(msg,-1,j))!=NULL;++j){
385                 if (strcasecmp(sbw->b_bwtype,"AS")==0) desc->bandwidth=atoi(sbw->b_bandwidth);
386         }
387         
388         /* for each m= line */
389         for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
390         {
391                 SalStreamDescription *stream=&desc->streams[i];
392                 
393                 memset(stream,0,sizeof(*stream));
394                 mtype = sdp_message_m_media_get(msg, i);
395                 proto = sdp_message_m_proto_get (msg, i);
396                 port = sdp_message_m_port_get(msg, i);
397                 stream->proto=SalProtoUnknown;
398                 if (proto){
399                         if (strcasecmp(proto,"RTP/AVP")==0)
400                                 stream->proto=SalProtoRtpAvp;
401                         else if (strcasecmp(proto,"RTP/SAVP")==0){
402                                 stream->proto=SalProtoRtpSavp;
403                         }
404                 }
405                 addr = sdp_message_c_addr_get (msg, i, 0);
406                 if (addr != NULL)
407                         strncpy(stream->addr,addr,sizeof(stream->addr));
408                 if (port)
409                         stream->port=atoi(port);
410                 
411                 stream->ptime=_sdp_message_get_a_ptime(msg,i);
412                 if (strcasecmp("audio", mtype) == 0){
413                         stream->type=SalAudio;
414                 }else if (strcasecmp("video", mtype) == 0){
415                         stream->type=SalVideo;
416                 }else {
417                         stream->type=SalOther;
418                         strncpy(stream->typeother,mtype,sizeof(stream->typeother)-1);
419                 }
420                 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
421                         if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
422                 }
423                 stream->dir=_sdp_message_get_mline_dir(msg,i);          
424                 /* for each payload type */
425                 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
426                         const char *rtpmap,*fmtp;
427                         int ptn=atoi(number);
428                         PayloadType *pt=payload_type_new();
429                         payload_type_set_number(pt,ptn);
430                         /* get the rtpmap associated to this codec, if any */
431                         rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
432                         if (payload_type_fill_from_rtpmap(pt,rtpmap)==0){
433                                 /* get the fmtp, if any */
434                                 fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
435                                 payload_type_set_send_fmtp(pt,fmtp);
436                                 stream->payloads=ms_list_append(stream->payloads,pt);
437                                 ms_message("Found payload %s/%i fmtp=%s",pt->mime_type,pt->clock_rate,
438                                         pt->send_fmtp ? pt->send_fmtp : "");
439                         }
440                 }
441                 
442                 /* read crypto lines if any */
443                 if (stream->proto == SalProtoRtpSavp) {
444                         int k, valid_count = 0;
445                         sdp_attribute_t *attr;
446                                 
447                         memset(&stream->crypto, 0, sizeof(stream->crypto));
448                         for (k=0;valid_count < SAL_CRYPTO_ALGO_MAX && (attr=sdp_message_attribute_get(msg,i,k))!=NULL;k++){
449                                 char tmp[256], tmp2[256];
450                                 if (keywordcmp("crypto",attr->a_att_field)==0 && attr->a_att_value!=NULL){
451                                         int nb = sscanf(attr->a_att_value, "%d %256s inline:%256s",
452                                                 &stream->crypto[valid_count].tag,
453                                                 tmp,
454                                                 tmp2);
455                                                 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'", 
456                                                                 stream->crypto[valid_count].tag, 
457                                                                 tmp, 
458                                                                 tmp2);
459                                         if (nb == 3) {
460                                                 if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_80") == 0)
461                                                         stream->crypto[valid_count].algo = AES_128_SHA1_80;
462                                                 else if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_32") == 0)
463                                                         stream->crypto[valid_count].algo = AES_128_SHA1_32;
464                                                 else {
465                                                         ms_warning("Failed to parse crypto-algo: '%s'", tmp);
466                                                         stream->crypto[valid_count].algo = 0;
467                                                 }
468                                                 if (stream->crypto[valid_count].algo) {
469                                                         strncpy(stream->crypto[valid_count].master_key, tmp2, 41);
470                                                         stream->crypto[valid_count].master_key[40] = '\0';
471                                                         ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'", 
472                                                                 stream->crypto[valid_count].tag, 
473                                                                 tmp, 
474                                                                 stream->crypto[valid_count].master_key);
475                                                         valid_count++;
476                                                 }
477                                         } else {
478                                                 ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
479                                         }
480                                 }
481                         }
482                         ms_message("Found: %d valid crypto lines", valid_count);
483                 }
484         }
485         desc->nstreams=i;
486         return 0;
487 }