]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2_sdp.c
Add common name certificate verification function in sal
[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/port.h"
22 #include "ortp/b64.h"
23 #include "ortp/ortp_srtp.h"
24 #include "sal.h"
25 #include <eXosip2/eXosip.h>
26
27 #define keywordcmp(key,b) strncmp(key,b,sizeof(key))
28
29 #ifdef FOR_LATER
30
31 static char *make_relay_session_id(const char *username, const char *relay){
32         /*ideally this should be a hash of the parameters with a random part*/
33         char tmp[128];
34         int s1=(int)random();
35         int s2=(int)random();
36         long long int res=((long long int)s1)<<32 | (long long int) s2;
37         void *src=&res;
38         b64_encode(src, sizeof(long long int), tmp, sizeof(tmp));
39         return osip_strdup(tmp);
40 }
41
42
43 static void add_relay_info(sdp_message_t *sdp, int mline, const char *relay, const char *relay_session_id){
44
45         if (relay) sdp_message_a_attribute_add(sdp, mline,
46                                      osip_strdup ("relay-addr"),osip_strdup(relay));
47         if (relay_session_id) sdp_message_a_attribute_add(sdp, mline,
48                                      osip_strdup ("relay-session-id"), osip_strdup(relay_session_id));
49 }
50
51 #endif
52
53 static char * int_2char(int a){
54         char *p=osip_malloc(16);
55         snprintf(p,16,"%i",a);
56         return p;
57 }
58
59 /* return the value of attr "field" for payload pt at line pos (field=rtpmap,fmtp...)*/
60 static const char *sdp_message_a_attr_value_get_with_pt(sdp_message_t *sdp,int pos,int pt,const char *field)
61 {
62         int i,tmppt=0,scanned=0;
63         char *tmp;
64         sdp_attribute_t *attr;
65         for (i=0;(attr=sdp_message_attribute_get(sdp,pos,i))!=NULL;i++){
66                 if (keywordcmp(field,attr->a_att_field)==0 && attr->a_att_value!=NULL){
67                         int nb = sscanf(attr->a_att_value,"%i %n",&tmppt,&scanned);
68                         /* the return value may depend on how %n is interpreted by the libc: see manpage*/
69                         if (nb == 1 || nb==2 ){
70                                 if (pt==tmppt){
71                                         tmp=attr->a_att_value+scanned;
72                                         if (strlen(tmp)>0)
73                                                 return tmp;
74                                 }
75                         }else ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
76                 }
77         }
78         return NULL;
79 }
80
81 #ifdef FOR_LATER
82 /* return the value of attr "field" */
83 static const char *sdp_message_a_attr_value_get(sdp_message_t *sdp,int pos,const char *field)
84 {
85         int i;
86         sdp_attribute_t *attr;
87         for (i=0;(attr=sdp_message_attribute_get(sdp,pos,i))!=NULL;i++){
88                 if (keywordcmp(field,attr->a_att_field)==0 && attr->a_att_value!=NULL){
89                         return attr->a_att_value;
90                 }
91         }
92         return NULL;
93 }
94 #endif
95
96 static int _sdp_message_get_a_ptime(sdp_message_t *sdp, int mline){
97         int i,ret;
98         sdp_attribute_t *attr;
99         for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){
100                 if (keywordcmp("ptime",attr->a_att_field)==0){
101                         int nb = sscanf(attr->a_att_value,"%i",&ret);
102                         /* the return value may depend on how %n is interpreted by the libc: see manpage*/
103                         if (nb == 1){
104                                 return ret;
105                         }else ms_warning("sdp has a strange a=ptime line (%s) ",attr->a_att_value);
106                 }
107         }
108         return 0;
109 }
110
111 static int _sdp_message_get_mline_dir(sdp_message_t *sdp, int mline){
112         int i;
113         sdp_attribute_t *attr;
114         for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){
115                 if (keywordcmp("sendrecv",attr->a_att_field)==0){
116                         return SalStreamSendRecv;
117                 }else if (keywordcmp("sendonly",attr->a_att_field)==0){
118                         return SalStreamSendOnly;
119                 }else if (keywordcmp("recvonly",attr->a_att_field)==0){
120                         return SalStreamRecvOnly;
121                 }else if (keywordcmp("inactive",attr->a_att_field)==0){
122                         return SalStreamInactive;
123                 }
124         }
125         return SalStreamSendRecv;
126 }
127
128 static sdp_message_t *create_generic_sdp(const SalMediaDescription *desc)
129 {
130         sdp_message_t *local;
131         int inet6;
132         char sessid[16];
133         char sessver[16];
134         const char *rtp_addr = desc->addr;
135         
136         snprintf(sessid,16,"%i",desc->session_id);
137         snprintf(sessver,16,"%i",desc->session_ver);
138         sdp_message_init (&local);
139         if (strchr(desc->addr,':')!=NULL){
140                 inet6=1;
141         }else inet6=0;
142         sdp_message_v_version_set (local, osip_strdup ("0"));
143         sdp_message_o_origin_set (local, osip_strdup (desc->username),
144                           osip_strdup (sessid), osip_strdup (sessver),
145                           osip_strdup ("IN"), inet6 ? osip_strdup("IP6") : osip_strdup ("IP4"),
146                           osip_strdup (desc->addr));
147         sdp_message_s_name_set (local, osip_strdup ("Talk"));
148         /* Do not set the c= line to 0.0.0.0 if there is an ICE session. */
149         if((desc->ice_ufrag[0] != '\0') || !sal_media_description_has_dir (desc,SalStreamSendOnly))
150         {
151                 sdp_message_c_connection_add (local, -1,
152                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
153                                                 osip_strdup (rtp_addr), NULL, NULL);
154         }
155         else
156         {
157                 sdp_message_c_connection_add (local, -1,
158                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
159                                                 inet6 ? osip_strdup ("::0") : osip_strdup ("0.0.0.0"), NULL, NULL);
160         }               
161         sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
162         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (local, -1, osip_strdup ("AS"),
163                         int_2char(desc->bandwidth));
164         if (desc->ice_completed == TRUE) sdp_message_a_attribute_add(local, -1, osip_strdup("nortpproxy"), osip_strdup("yes"));
165         if (desc->ice_pwd[0] != '\0') sdp_message_a_attribute_add(local, -1, osip_strdup("ice-pwd"), osip_strdup(desc->ice_pwd));
166         if (desc->ice_ufrag[0] != '\0') sdp_message_a_attribute_add(local, -1, osip_strdup("ice-ufrag"), osip_strdup(desc->ice_ufrag));
167
168         return local;
169 }
170
171
172 static bool_t is_known_rtpmap(const PayloadType *pt){
173         switch(payload_type_get_number(pt)){
174                 case 0:
175                 case 8:
176                 case 3:
177                 case 34:
178                         return TRUE;
179         }
180         return FALSE;
181 }
182
183 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt, bool_t strip_well_known_rtpmaps)
184 {
185         char attr[256];
186         sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
187
188         if (!strip_well_known_rtpmaps || !is_known_rtpmap(pt)){
189                 if (pt->channels>1)
190                         snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt), 
191                                         pt->mime_type, pt->clock_rate,pt->channels);
192                 else
193                         snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt), 
194                                         pt->mime_type, pt->clock_rate);
195                 sdp_message_a_attribute_add (msg, line,
196                                                  osip_strdup ("rtpmap"), osip_strdup(attr));
197         }
198
199         if (pt->recv_fmtp != NULL)
200         {
201                 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
202                 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
203                                      osip_strdup(attr));
204         }
205 }
206
207 static void add_ice_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc)
208 {
209         char buffer[1024];
210         const SalIceCandidate *candidate;
211         int nb;
212         int i;
213
214         for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_ICE_CANDIDATES; i++) {
215                 candidate = &desc->ice_candidates[i];
216                 if ((candidate->addr[0] == '\0') || (candidate->port == 0)) break;
217                 nb = snprintf(buffer, sizeof(buffer), "%s %u UDP %u %s %d typ %s",
218                         candidate->foundation, candidate->componentID, candidate->priority, candidate->addr, candidate->port, candidate->type);
219                 if (nb < 0) {
220                         ms_error("Cannot add ICE candidate attribute!");
221                         return;
222                 }
223                 if (candidate->raddr[0] != '\0') {
224                         nb = snprintf(buffer + nb, sizeof(buffer) - nb, " raddr %s rport %d", candidate->raddr, candidate->rport);
225                         if (nb < 0) {
226                                 ms_error("Cannot add ICE candidate attribute!");
227                                 return;
228                         }
229                 }
230                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("candidate"), osip_strdup(buffer));
231         }
232 }
233
234 static void add_ice_remote_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc)
235 {
236         char buffer[1024];
237         char *ptr = buffer;
238         const SalIceRemoteCandidate *candidate;
239         int offset = 0;
240         int i;
241
242         buffer[0] = '\0';
243         for (i = 0; i < SAL_MEDIA_DESCRIPTION_MAX_ICE_REMOTE_CANDIDATES; i++) {
244                 candidate = &desc->ice_remote_candidates[i];
245                 if ((candidate->addr[0] != '\0') && (candidate->port != 0)) {
246                         offset = snprintf(ptr, buffer + sizeof(buffer) - ptr, "%s%d %s %d", (i > 0) ? " " : "", i + 1, candidate->addr, candidate->port);
247                         if (offset < 0) {
248                                 ms_error("Cannot add ICE remote-candidates attribute!");
249                                 return;
250                         }
251                         ptr += offset;
252                 }
253         }
254         if (buffer[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("remote-candidates"), osip_strdup(buffer));
255 }
256
257 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc){
258         const char *mt=NULL;
259         const MSList *elem;
260         const char *rtp_addr;
261         const char *rtcp_addr;
262         const char *dir="sendrecv";
263         int rtp_port;
264         int rtcp_port;
265         bool_t strip_well_known_rtpmaps;
266         bool_t different_rtp_and_rtcp_addr;
267         
268         switch (desc->type) {
269         case SalAudio:
270                 mt="audio";
271                 break;
272         case SalVideo:
273                 mt="video";
274                 break;
275         case SalOther:
276                 mt=desc->typeother;
277                 break;
278         }
279         rtp_addr=desc->rtp_addr;
280         rtcp_addr=desc->rtcp_addr;
281         rtp_port=desc->rtp_port;
282         rtcp_port=desc->rtcp_port;
283
284         if (desc->proto == SalProtoRtpSavp) {
285                 int i;
286                 
287                 sdp_message_m_media_add (msg, osip_strdup (mt),
288                                          int_2char (rtp_port), NULL,
289                                          osip_strdup ("RTP/SAVP"));
290        
291                 /* add crypto lines */
292                 for(i=0; i<SAL_CRYPTO_ALGO_MAX; i++) {
293                         char buffer[1024];
294                         switch (desc->crypto[i].algo) {
295                                 case AES_128_SHA1_80:
296                                         snprintf(buffer, 1024, "%d %s inline:%s",
297                                                 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_80", desc->crypto[i].master_key);
298                                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
299                                                 osip_strdup(buffer));
300                                         break;
301                                 case AES_128_SHA1_32:
302                                         snprintf(buffer, 1024, "%d %s inline:%s",
303                                                 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_32", desc->crypto[i].master_key);
304                                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
305                                                 osip_strdup(buffer));
306                                         break;
307                                 case AES_128_NO_AUTH:
308                                         ms_warning("Unsupported crypto suite: AES_128_NO_AUTH");
309                                         break;
310                                 case NO_CIPHER_SHA1_80:
311                                         ms_warning("Unsupported crypto suite: NO_CIPHER_SHA1_80");
312                                         break; 
313                                 default:
314                                         i = SAL_CRYPTO_ALGO_MAX;
315                         }
316                 }
317                 
318         } else {
319                 sdp_message_m_media_add (msg, osip_strdup (mt),
320                                          int_2char (rtp_port), NULL,
321                                          osip_strdup ("RTP/AVP"));
322                 
323         }
324
325         /*only add a c= line within the stream description if address are differents*/
326         if (rtp_addr[0]!='\0' && strcmp(rtp_addr,sdp_message_c_addr_get(msg, -1, 0))!=0){
327                 bool_t inet6;
328                 if (strchr(rtp_addr,':')!=NULL){
329                         inet6=TRUE;
330                 }else inet6=FALSE;
331                 sdp_message_c_connection_add (msg, lineno,
332                               osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
333                               osip_strdup (rtp_addr), NULL, NULL);
334         }
335
336         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
337                                      int_2char(desc->bandwidth));
338         if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"),
339                                 int_2char(desc->ptime));
340         strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5;
341         if (desc->payloads){
342                 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
343                         add_payload(msg, lineno, (PayloadType*)elem->data,strip_well_known_rtpmaps);
344                 }
345         }else{
346                 /* to comply with SDP we cannot have an empty payload type number list */
347                 /* as it happens only when mline is declined with a zero port, it does not matter to put whatever codec*/
348                 sdp_message_m_payload_add (msg,lineno, int_2char (0));
349         }
350         switch(desc->dir){
351                 case SalStreamSendRecv:
352                         /*dir="sendrecv";*/
353                         dir=NULL;
354                 break;
355                 case SalStreamRecvOnly:
356                         dir="recvonly";
357                         break;
358                 case SalStreamSendOnly:
359                         dir="sendonly";
360                         break;
361                 case SalStreamInactive:
362                         dir="inactive";
363                         break;
364         }
365         if (dir) sdp_message_a_attribute_add (msg, lineno, osip_strdup (dir),NULL);
366         if (rtp_port != 0) {
367                 different_rtp_and_rtcp_addr = (rtcp_addr[0] != '\0') && (strcmp(rtp_addr, rtcp_addr) != 0);
368                 if ((rtcp_port != (rtp_port + 1)) || (different_rtp_and_rtcp_addr == TRUE)) {
369                         if (different_rtp_and_rtcp_addr == TRUE) {
370                                 char buffer[1024];
371                                 snprintf(buffer, sizeof(buffer), "%u IN IP4 %s", rtcp_port, rtcp_addr);
372                                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("rtcp"), osip_strdup(buffer));
373                         } else {
374                                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("rtcp"), int_2char(rtcp_port));
375                         }
376                 }
377         }
378         if (desc->ice_completed == TRUE) {
379                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("nortpproxy"), osip_strdup("yes"));
380         }
381         if (desc->ice_mismatch == TRUE) {
382                 sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-mismatch"), NULL);
383         } else {
384                 if (desc->rtp_port != 0) {
385                         if (desc->ice_pwd[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-pwd"), osip_strdup(desc->ice_pwd));
386                         if (desc->ice_ufrag[0] != '\0') sdp_message_a_attribute_add(msg, lineno, osip_strdup("ice-ufrag"), osip_strdup(desc->ice_ufrag));
387                         add_ice_candidates(msg, lineno, desc);
388                         add_ice_remote_candidates(msg, lineno, desc);
389                 }
390         }
391 }
392
393
394 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc){
395         int i;
396         sdp_message_t *msg=create_generic_sdp(desc);
397         for(i=0;i<desc->nstreams;++i){
398                 add_line(msg,i,&desc->streams[i]);
399         }
400         return msg;
401 }
402
403 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
404         if (rtpmap==NULL){
405                 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
406                 if (refpt){
407                         pt->mime_type=ms_strdup(refpt->mime_type);
408                         pt->clock_rate=refpt->clock_rate;
409                 }else{
410                         ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
411                             payload_type_get_number(pt));
412                         return -1;
413                 }
414         }else{
415                 char *mime=ms_strdup(rtpmap);
416                 char *p=strchr(mime,'/');
417                 if (p){
418                         char *chans;
419                         *p='\0';
420                         p++;
421                         chans=strchr(p,'/');
422                         if (chans){
423                                 *chans='\0';
424                                 chans++;
425                                 pt->channels=atoi(chans);
426                         }else pt->channels=1;
427                         pt->clock_rate=atoi(p);
428                 }
429                 pt->mime_type=mime;
430         }
431         return 0;
432 }
433
434 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){
435         int i,j;
436         const char *mtype,*proto,*rtp_port,*rtp_addr,*number;
437         sdp_bandwidth_t *sbw=NULL;
438         sdp_attribute_t *attr;
439         int nb_ice_candidates;
440
441         rtp_addr=sdp_message_c_addr_get (msg, -1, 0);
442         if (rtp_addr)
443                 strncpy(desc->addr,rtp_addr,sizeof(desc->addr));
444         for(j=0;(sbw=sdp_message_bandwidth_get(msg,-1,j))!=NULL;++j){
445                 if (strcasecmp(sbw->b_bwtype,"AS")==0) desc->bandwidth=atoi(sbw->b_bandwidth);
446         }
447
448         /* Get ICE remote ufrag and remote pwd, and ice_lite flag */
449         for (i = 0; (i < SAL_MEDIA_DESCRIPTION_MAX_MESSAGE_ATTRIBUTES) && ((attr = sdp_message_attribute_get(msg, -1, i)) != NULL); i++) {
450                 if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
451                         strncpy(desc->ice_ufrag, attr->a_att_value, sizeof(desc->ice_ufrag));
452                 } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
453                         strncpy(desc->ice_pwd, attr->a_att_value, sizeof(desc->ice_pwd));
454                 } else if (keywordcmp("ice-lite", attr->a_att_field) == 0) {
455                         desc->ice_lite = TRUE;
456                 }
457         }
458
459         /* for each m= line */
460         for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
461         {
462                 SalStreamDescription *stream=&desc->streams[i];
463                 nb_ice_candidates = 0;
464                 
465                 memset(stream,0,sizeof(*stream));
466                 mtype = sdp_message_m_media_get(msg, i);
467                 proto = sdp_message_m_proto_get (msg, i);
468                 rtp_port = sdp_message_m_port_get(msg, i);
469                 stream->proto=SalProtoUnknown;
470                 if (proto){
471                         if (strcasecmp(proto,"RTP/AVP")==0)
472                                 stream->proto=SalProtoRtpAvp;
473                         else if (strcasecmp(proto,"RTP/SAVP")==0){
474                                 stream->proto=SalProtoRtpSavp;
475                         }
476                 }
477                 rtp_addr = sdp_message_c_addr_get (msg, i, 0);
478                 if (rtp_addr != NULL)
479                         strncpy(stream->rtp_addr,rtp_addr,sizeof(stream->rtp_addr));
480                 if (rtp_port)
481                         stream->rtp_port=atoi(rtp_port);
482                 
483                 stream->ptime=_sdp_message_get_a_ptime(msg,i);
484                 if (strcasecmp("audio", mtype) == 0){
485                         stream->type=SalAudio;
486                 }else if (strcasecmp("video", mtype) == 0){
487                         stream->type=SalVideo;
488                 }else {
489                         stream->type=SalOther;
490                         strncpy(stream->typeother,mtype,sizeof(stream->typeother)-1);
491                 }
492                 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
493                         if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
494                 }
495                 stream->dir=_sdp_message_get_mline_dir(msg,i);
496                 /* for each payload type */
497                 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
498                         const char *rtpmap,*fmtp;
499                         int ptn=atoi(number);
500                         PayloadType *pt=payload_type_new();
501                         payload_type_set_number(pt,ptn);
502                         /* get the rtpmap associated to this codec, if any */
503                         rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
504                         if (payload_type_fill_from_rtpmap(pt,rtpmap)==0){
505                                 /* get the fmtp, if any */
506                                 fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
507                                 payload_type_set_send_fmtp(pt,fmtp);
508                                 stream->payloads=ms_list_append(stream->payloads,pt);
509                                 ms_message("Found payload %s/%i fmtp=%s",pt->mime_type,pt->clock_rate,
510                                         pt->send_fmtp ? pt->send_fmtp : "");
511                         }
512                 }
513
514                 /* Get media specific RTCP attribute */
515                 stream->rtcp_port = stream->rtp_port + 1;
516                 snprintf(stream->rtcp_addr, sizeof(stream->rtcp_addr), "%s", stream->rtp_addr);
517                 for (j = 0; ((attr = sdp_message_attribute_get(msg, i, j)) != NULL); j++) {
518                         if ((keywordcmp("rtcp", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
519                                 char tmp[256];
520                                 int nb = sscanf(attr->a_att_value, "%d IN IP4 %s", &stream->rtcp_port, tmp);
521                                 if (nb == 1) {
522                                         /* SDP rtcp attribute only contains the port */
523                                 } else if (nb == 2) {
524                                         strncpy(stream->rtcp_addr, tmp, sizeof(stream->rtcp_addr));
525                                 } else {
526                                         ms_warning("sdp has a strange a= line (%s) nb=%i", attr->a_att_value, nb);
527                                 }
528                         }
529                 }
530
531                 /* read crypto lines if any */
532                 if (stream->proto == SalProtoRtpSavp) {
533                         int k, valid_count = 0;
534                                 
535                         memset(&stream->crypto, 0, sizeof(stream->crypto));
536                         for (k=0;valid_count < SAL_CRYPTO_ALGO_MAX && (attr=sdp_message_attribute_get(msg,i,k))!=NULL;k++){
537                                 char tmp[256], tmp2[256];
538                                 if (keywordcmp("crypto",attr->a_att_field)==0 && attr->a_att_value!=NULL){
539                                         int nb = sscanf(attr->a_att_value, "%d %256s inline:%256s",
540                                                 &stream->crypto[valid_count].tag,
541                                                 tmp,
542                                                 tmp2);
543                                                 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'", 
544                                                                 stream->crypto[valid_count].tag, 
545                                                                 tmp, 
546                                                                 tmp2);
547                                         if (nb == 3) {
548                                                 if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_80") == 0)
549                                                         stream->crypto[valid_count].algo = AES_128_SHA1_80;
550                                                 else if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_32") == 0)
551                                                         stream->crypto[valid_count].algo = AES_128_SHA1_32;
552                                                 else {
553                                                         ms_warning("Failed to parse crypto-algo: '%s'", tmp);
554                                                         stream->crypto[valid_count].algo = 0;
555                                                 }
556                                                 if (stream->crypto[valid_count].algo) {
557                                                         strncpy(stream->crypto[valid_count].master_key, tmp2, 41);
558                                                         stream->crypto[valid_count].master_key[40] = '\0';
559                                                         ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'", 
560                                                                 stream->crypto[valid_count].tag, 
561                                                                 tmp, 
562                                                                 stream->crypto[valid_count].master_key);
563                                                         valid_count++;
564                                                 }
565                                         } else {
566                                                 ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
567                                         }
568                                 }
569                         }
570                         ms_message("Found: %d valid crypto lines", valid_count);
571                 }
572
573                 /* Get ICE candidate attributes if any */
574                 for (j = 0; (attr = sdp_message_attribute_get(msg, i, j)) != NULL; j++) {
575                         if ((keywordcmp("candidate", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
576                                 SalIceCandidate *candidate = &stream->ice_candidates[nb_ice_candidates];
577                                 int nb = sscanf(attr->a_att_value, "%s %u UDP %u %s %d typ %s raddr %s rport %d",
578                                         candidate->foundation, &candidate->componentID, &candidate->priority, candidate->addr, &candidate->port,
579                                         candidate->type, candidate->raddr, &candidate->rport);
580                                 if ((nb == 6) || (nb == 8)) nb_ice_candidates++;
581                                 else memset(candidate, 0, sizeof(*candidate));
582                         } else if ((keywordcmp("remote-candidates", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
583                                 SalIceRemoteCandidate candidate;
584                                 unsigned int componentID;
585                                 int offset;
586                                 char *ptr = attr->a_att_value;
587                                 while (3 == sscanf(ptr, "%u %s %u%n", &componentID, candidate.addr, &candidate.port, &offset)) {
588                                         if ((componentID > 0) && (componentID <= SAL_MEDIA_DESCRIPTION_MAX_ICE_REMOTE_CANDIDATES)) {
589                                                 SalIceRemoteCandidate *remote_candidate = &stream->ice_remote_candidates[componentID - 1];
590                                                 strncpy(remote_candidate->addr, candidate.addr, sizeof(remote_candidate->addr));
591                                                 remote_candidate->port = candidate.port;
592                                         }
593                                         ptr += offset;
594                                         if (ptr[offset] == ' ') ptr += 1;
595                                 }
596                         } else if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
597                                 strncpy(stream->ice_ufrag, attr->a_att_value, sizeof(stream->ice_ufrag));
598                         } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
599                                 strncpy(stream->ice_pwd, attr->a_att_value, sizeof(stream->ice_pwd));
600                         } else if (keywordcmp("ice-mismatch", attr->a_att_field) == 0) {
601                                 stream->ice_mismatch = TRUE;
602                         }
603                 }
604         }
605         desc->nstreams=i;
606         return 0;
607 }