]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2_sdp.c
Re-Invite when ICE processing is finished successfully.
[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, const IceSession *ice_session)
128 {
129         sdp_message_t *local;
130         int inet6;
131         char sessid[16];
132         char sessver[16];
133         const char *addr = desc->addr;
134         
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){
139                 inet6=1;
140         }else inet6=0;
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 ((ice_session != NULL) && (ice_session_check_list(ice_session, 0) != NULL)) {
148                 const IceCandidate *candidate = NULL;
149                 if (ice_session_state(ice_session) == IS_Completed) {
150                         candidate = ice_check_list_nominated_valid_local_candidate(ice_session_check_list(ice_session, 0));
151                 }
152                 else {
153                         candidate = ice_check_list_default_local_candidate(ice_session_check_list(ice_session, 0));
154                 }
155                 if (candidate != NULL) {
156                         addr=candidate->taddr.ip;
157                 }
158         }
159         if(!sal_media_description_has_dir (desc,SalStreamSendOnly))
160         {
161                 sdp_message_c_connection_add (local, -1,
162                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
163                                                 osip_strdup (addr), NULL, NULL);
164         }
165         else
166         {
167                 sdp_message_c_connection_add (local, -1,
168                                 osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
169                                                 inet6 ? osip_strdup ("::0") : osip_strdup ("0.0.0.0"), NULL, NULL);
170         }               
171         sdp_message_t_time_descr_add (local, osip_strdup ("0"), osip_strdup ("0"));
172         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (local, -1, osip_strdup ("AS"),
173                         int_2char(desc->bandwidth));
174         if ((ice_session != NULL) && (ice_session_check_list(ice_session, 0) != NULL)) {
175                 char buffer[512];
176                 switch (ice_session_state(ice_session)) {
177                         case IS_Running:
178                         case IS_Stopped:
179                                 snprintf(buffer, sizeof(buffer), "%s", ice_session_local_pwd(ice_session));
180                                 sdp_message_a_attribute_add(local, -1, osip_strdup("ice-pwd"), osip_strdup(buffer));
181                                 snprintf(buffer, sizeof(buffer), "%s", ice_session_local_ufrag(ice_session));
182                                 sdp_message_a_attribute_add(local, -1, osip_strdup("ice-ufrag"), osip_strdup(buffer));
183                                 break;
184                         case IS_Completed:
185                                 sdp_message_a_attribute_add(local, -1, osip_strdup("nortpproxy"), osip_strdup("yes"));
186                                 break;
187                         default:
188                                 break;
189                 }
190         }
191
192         return local;
193 }
194
195
196 static bool_t is_known_rtpmap(const PayloadType *pt){
197         switch(payload_type_get_number(pt)){
198                 case 0:
199                 case 8:
200                 case 3:
201                 case 34:
202                         return TRUE;
203         }
204         return FALSE;
205 }
206
207 static void add_payload(sdp_message_t *msg, int line, const PayloadType *pt, bool_t strip_well_known_rtpmaps)
208 {
209         char attr[256];
210         sdp_message_m_payload_add (msg,line, int_2char (payload_type_get_number(pt)));
211
212         if (!strip_well_known_rtpmaps || !is_known_rtpmap(pt)){
213                 if (pt->channels>1)
214                         snprintf (attr,sizeof(attr),"%i %s/%i/%i", payload_type_get_number(pt), 
215                                         pt->mime_type, pt->clock_rate,pt->channels);
216                 else
217                         snprintf (attr,sizeof(attr),"%i %s/%i", payload_type_get_number(pt), 
218                                         pt->mime_type, pt->clock_rate);
219                 sdp_message_a_attribute_add (msg, line,
220                                                  osip_strdup ("rtpmap"), osip_strdup(attr));
221         }
222
223         if (pt->recv_fmtp != NULL)
224         {
225                 snprintf (attr,sizeof(attr),"%i %s", payload_type_get_number(pt),pt->recv_fmtp);
226                 sdp_message_a_attribute_add (msg, line, osip_strdup ("fmtp"),
227                                      osip_strdup(attr));
228         }
229 }
230
231 static void add_ice_candidates(sdp_message_t *msg, int lineno, const SalStreamDescription *desc, const IceCheckList *ice_cl)
232 {
233         char buffer[1024];
234         const IceCandidate *candidate;
235         int i;
236
237         if (ice_cl != NULL) {
238                 for (i = 0; i < ms_list_size(ice_cl->local_candidates); i++) {
239                         candidate = ms_list_nth_data(ice_cl->local_candidates, i);
240                         snprintf(buffer, sizeof(buffer), "%s %d UDP %d %s %d typ %s",
241                                 candidate->foundation, candidate->componentID, candidate->priority, candidate->taddr.ip, candidate->taddr.port, ice_candidate_type(candidate));
242                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("candidate"), osip_strdup(buffer));
243                 }
244         }
245 }
246
247
248 static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription *desc, const IceCheckList *ice_cl){
249         const char *mt=NULL;
250         const MSList *elem;
251         const char *addr;
252         const char *dir="sendrecv";
253         int port;
254         bool_t strip_well_known_rtpmaps;
255         
256         switch (desc->type) {
257         case SalAudio:
258                 mt="audio";
259                 break;
260         case SalVideo:
261                 mt="video";
262                 break;
263         case SalOther:
264                 mt=desc->typeother;
265                 break;
266         }
267         addr=desc->addr;
268         port=desc->port;
269         if (ice_cl != NULL) {
270                 const IceCandidate *candidate = NULL;
271                 if (ice_check_list_state(ice_cl) == ICL_Completed) {
272                         candidate = ice_check_list_nominated_valid_local_candidate(ice_cl);
273                 } else {
274                         candidate = ice_check_list_default_local_candidate(ice_cl);
275                 }
276                 if (candidate != NULL) {
277                         addr=candidate->taddr.ip;
278                         port=candidate->taddr.port;
279                 }
280         } else if (desc->candidates[0].addr[0]!='\0'){
281                 addr=desc->candidates[0].addr;
282                 port=desc->candidates[0].port;
283         }
284
285         if (desc->proto == SalProtoRtpSavp) {
286                 int i;
287                 
288                 sdp_message_m_media_add (msg, osip_strdup (mt),
289                                          int_2char (port), NULL,
290                                          osip_strdup ("RTP/SAVP"));
291        
292                 /* add crypto lines */
293                 for(i=0; i<SAL_CRYPTO_ALGO_MAX; i++) {
294                         char buffer[1024];
295                         switch (desc->crypto[i].algo) {
296                                 case AES_128_SHA1_80:
297                                         snprintf(buffer, 1024, "%d %s inline:%s",
298                                                 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_80", desc->crypto[i].master_key);
299                                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
300                                                 osip_strdup(buffer));
301                                         break;
302                                 case AES_128_SHA1_32:
303                                         snprintf(buffer, 1024, "%d %s inline:%s",
304                                                 desc->crypto[i].tag, "AES_CM_128_HMAC_SHA1_32", desc->crypto[i].master_key);
305                                         sdp_message_a_attribute_add(msg, lineno, osip_strdup("crypto"),
306                                                 osip_strdup(buffer));
307                                         break;
308                                 case AES_128_NO_AUTH:
309                                         ms_warning("Unsupported crypto suite: AES_128_NO_AUTH");
310                                         break;
311                                 case NO_CIPHER_SHA1_80:
312                                         ms_warning("Unsupported crypto suite: NO_CIPHER_SHA1_80");
313                                         break; 
314                                 default:
315                                         i = SAL_CRYPTO_ALGO_MAX;
316                         }
317                 }
318                 
319         } else {
320                 sdp_message_m_media_add (msg, osip_strdup (mt),
321                                          int_2char (port), NULL,
322                                          osip_strdup ("RTP/AVP"));
323                 
324         }
325
326         /*only add a c= line within the stream description if address are differents*/
327         if (strcmp(addr,sdp_message_c_addr_get(msg, -1, 0))!=0){
328                 bool_t inet6;
329                 if (strchr(addr,':')!=NULL){
330                         inet6=TRUE;
331                 }else inet6=FALSE;
332                 sdp_message_c_connection_add (msg, lineno,
333                               osip_strdup ("IN"), inet6 ? osip_strdup ("IP6") : osip_strdup ("IP4"),
334                               osip_strdup (addr), NULL, NULL);
335         }
336
337         if (desc->bandwidth>0) sdp_message_b_bandwidth_add (msg, lineno, osip_strdup ("AS"),
338                                      int_2char(desc->bandwidth));
339         if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"),
340                                 int_2char(desc->ptime));
341         strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5;
342         if (desc->payloads){
343                 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
344                         add_payload(msg, lineno, (PayloadType*)elem->data,strip_well_known_rtpmaps);
345                 }
346         }else{
347                 /* to comply with SDP we cannot have an empty payload type number list */
348                 /* as it happens only when mline is declined with a zero port, it does not matter to put whatever codec*/
349                 sdp_message_m_payload_add (msg,lineno, int_2char (0));
350         }
351         switch(desc->dir){
352                 case SalStreamSendRecv:
353                         /*dir="sendrecv";*/
354                         dir=NULL;
355                 break;
356                 case SalStreamRecvOnly:
357                         dir="recvonly";
358                         break;
359                 case SalStreamSendOnly:
360                         dir="sendonly";
361                         break;
362                 case SalStreamInactive:
363                         dir="inactive";
364                         break;
365         }
366         if (dir) sdp_message_a_attribute_add (msg, lineno, osip_strdup (dir),NULL);
367         if (ice_check_list_state(ice_cl) == ICL_Running) {
368                 add_ice_candidates(msg, lineno, desc, ice_cl);
369         }
370 }
371
372
373 sdp_message_t *media_description_to_sdp(const SalMediaDescription *desc, const IceSession *ice_session){
374         IceCheckList *ice_cl = NULL;
375         int i;
376         sdp_message_t *msg=create_generic_sdp(desc, ice_session);
377         for(i=0;i<desc->nstreams;++i){
378                 if (ice_session != NULL) ice_cl = ice_session_check_list(ice_session, i);
379                 else ice_cl = NULL;
380                 add_line(msg,i,&desc->streams[i], ice_cl);
381         }
382         return msg;
383 }
384
385 static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){
386         if (rtpmap==NULL){
387                 PayloadType *refpt=rtp_profile_get_payload(&av_profile,payload_type_get_number(pt));
388                 if (refpt){
389                         pt->mime_type=ms_strdup(refpt->mime_type);
390                         pt->clock_rate=refpt->clock_rate;
391                 }else{
392                         ms_error("payload number %i has no rtpmap and is unknown in AV Profile, ignored.",
393                             payload_type_get_number(pt));
394                         return -1;
395                 }
396         }else{
397                 char *mime=ms_strdup(rtpmap);
398                 char *p=strchr(mime,'/');
399                 if (p){
400                         char *chans;
401                         *p='\0';
402                         p++;
403                         chans=strchr(p,'/');
404                         if (chans){
405                                 *chans='\0';
406                                 chans++;
407                                 pt->channels=atoi(chans);
408                         }else pt->channels=1;
409                         pt->clock_rate=atoi(p);
410                 }
411                 pt->mime_type=mime;
412         }
413         return 0;
414 }
415
416 int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc, IceSession **ice_session){
417         int i,j;
418         const char *mtype,*proto,*port,*addr,*number;
419         const char *ice_ufrag, *ice_pwd;
420         sdp_bandwidth_t *sbw=NULL;
421         sdp_attribute_t *attr;
422         int media_attribute_nb;
423         bool_t ice_session_just_created = FALSE;
424         bool_t ice_lite = FALSE;
425         
426         addr=sdp_message_c_addr_get (msg, -1, 0);
427         if (addr)
428                 strncpy(desc->addr,addr,sizeof(desc->addr));
429         for(j=0;(sbw=sdp_message_bandwidth_get(msg,-1,j))!=NULL;++j){
430                 if (strcasecmp(sbw->b_bwtype,"AS")==0) desc->bandwidth=atoi(sbw->b_bandwidth);
431         }
432         
433         /* for each m= line */
434         for (i=0; !sdp_message_endof_media (msg, i) && i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS; i++)
435         {
436                 SalStreamDescription *stream=&desc->streams[i];
437                 
438                 memset(stream,0,sizeof(*stream));
439                 mtype = sdp_message_m_media_get(msg, i);
440                 proto = sdp_message_m_proto_get (msg, i);
441                 port = sdp_message_m_port_get(msg, i);
442                 stream->proto=SalProtoUnknown;
443                 if (proto){
444                         if (strcasecmp(proto,"RTP/AVP")==0)
445                                 stream->proto=SalProtoRtpAvp;
446                         else if (strcasecmp(proto,"RTP/SAVP")==0){
447                                 stream->proto=SalProtoRtpSavp;
448                         }
449                 }
450                 addr = sdp_message_c_addr_get (msg, i, 0);
451                 if (addr != NULL)
452                         strncpy(stream->addr,addr,sizeof(stream->addr));
453                 if (port)
454                         stream->port=atoi(port);
455                 
456                 stream->ptime=_sdp_message_get_a_ptime(msg,i);
457                 if (strcasecmp("audio", mtype) == 0){
458                         stream->type=SalAudio;
459                 }else if (strcasecmp("video", mtype) == 0){
460                         stream->type=SalVideo;
461                 }else {
462                         stream->type=SalOther;
463                         strncpy(stream->typeother,mtype,sizeof(stream->typeother)-1);
464                 }
465                 for(j=0;(sbw=sdp_message_bandwidth_get(msg,i,j))!=NULL;++j){
466                         if (strcasecmp(sbw->b_bwtype,"AS")==0) stream->bandwidth=atoi(sbw->b_bandwidth);
467                 }
468                 stream->dir=_sdp_message_get_mline_dir(msg,i);
469                 media_attribute_nb = 0;
470                 /* for each payload type */
471                 for (j=0;((number=sdp_message_m_payload_get (msg, i,j)) != NULL); j++){
472                         const char *rtpmap,*fmtp;
473                         int ptn=atoi(number);
474                         PayloadType *pt=payload_type_new();
475                         payload_type_set_number(pt,ptn);
476                         /* get the rtpmap associated to this codec, if any */
477                         rtpmap=sdp_message_a_attr_value_get_with_pt(msg, i,ptn,"rtpmap");
478                         if (rtpmap != NULL) media_attribute_nb++;
479                         if (payload_type_fill_from_rtpmap(pt,rtpmap)==0){
480                                 /* get the fmtp, if any */
481                                 fmtp=sdp_message_a_attr_value_get_with_pt(msg, i, ptn,"fmtp");
482                                 if (fmtp != NULL) media_attribute_nb++;
483                                 payload_type_set_send_fmtp(pt,fmtp);
484                                 stream->payloads=ms_list_append(stream->payloads,pt);
485                                 ms_message("Found payload %s/%i fmtp=%s",pt->mime_type,pt->clock_rate,
486                                         pt->send_fmtp ? pt->send_fmtp : "");
487                         }
488                 }
489                 
490                 /* read crypto lines if any */
491                 if (stream->proto == SalProtoRtpSavp) {
492                         int k, valid_count = 0;
493                                 
494                         memset(&stream->crypto, 0, sizeof(stream->crypto));
495                         for (k=0;valid_count < SAL_CRYPTO_ALGO_MAX && (attr=sdp_message_attribute_get(msg,i,k))!=NULL;k++){
496                                 char tmp[256], tmp2[256];
497                                 if (keywordcmp("crypto",attr->a_att_field)==0 && attr->a_att_value!=NULL){
498                                         int nb = sscanf(attr->a_att_value, "%d %256s inline:%256s",
499                                                 &stream->crypto[valid_count].tag,
500                                                 tmp,
501                                                 tmp2);
502                                                 ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'", 
503                                                                 stream->crypto[valid_count].tag, 
504                                                                 tmp, 
505                                                                 tmp2);
506                                         if (nb == 3) {
507                                                 if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_80") == 0)
508                                                         stream->crypto[valid_count].algo = AES_128_SHA1_80;
509                                                 else if (strcmp(tmp, "AES_CM_128_HMAC_SHA1_32") == 0)
510                                                         stream->crypto[valid_count].algo = AES_128_SHA1_32;
511                                                 else {
512                                                         ms_warning("Failed to parse crypto-algo: '%s'", tmp);
513                                                         stream->crypto[valid_count].algo = 0;
514                                                 }
515                                                 if (stream->crypto[valid_count].algo) {
516                                                         strncpy(stream->crypto[valid_count].master_key, tmp2, 41);
517                                                         stream->crypto[valid_count].master_key[40] = '\0';
518                                                         ms_message("Found valid crypto line (tag:%d algo:'%s' key:'%s'", 
519                                                                 stream->crypto[valid_count].tag, 
520                                                                 tmp, 
521                                                                 stream->crypto[valid_count].master_key);
522                                                         valid_count++;
523                                                 }
524                                         } else {
525                                                 ms_warning("sdp has a strange a= line (%s) nb=%i",attr->a_att_value,nb);
526                                         }
527                                 }
528                         }
529                         ms_message("Found: %d valid crypto lines", valid_count);
530                 }
531
532                 /* Get ICE candidate attributes if any */
533                 ice_ufrag = ice_pwd = NULL;
534                 for (j = 0; (j < SAL_MEDIA_DESCRIPTION_MAX_ICE_CANDIDATES) && ((attr = sdp_message_attribute_get(msg, i, media_attribute_nb + j)) != NULL); j++) {
535                         if ((keywordcmp("candidate", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
536                                 char ip[64];
537                                 char foundation[32];
538                                 char type[6];
539                                 unsigned int priority;
540                                 unsigned int componentID;
541                                 unsigned int port;
542                                 int nb;
543
544                                 /* Allocate the ICE session if it has not been done yet. */
545                                 if (*ice_session == NULL) {
546                                         *ice_session = ice_session_new();
547                                         ice_session_just_created = TRUE;
548                                 }
549                                 /* Allocate the ICE check list if it has not been done yet. */
550                                 if (ice_session_check_list(*ice_session, i) == NULL) {
551                                         ice_session_add_check_list(*ice_session, ice_check_list_new());
552                                 }
553                                 nb = sscanf(attr->a_att_value, "%s %u UDP %u %s %u typ %s",
554                                         foundation, &componentID, &priority, ip, &port, type);
555                                 if (nb == 6) {
556                                         ice_add_remote_candidate(ice_session_check_list(*ice_session, i), type, ip, port, componentID, priority, foundation);
557                                 }
558                         } else if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
559                                 ice_ufrag = attr->a_att_value;
560                         } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
561                                 ice_pwd = attr->a_att_value;
562                         }
563                 }
564                 if ((*ice_session != NULL) && ice_session_check_list(*ice_session, i)) {
565                         if ((ice_ufrag != NULL) && (ice_pwd != NULL)) {
566                                 ice_check_list_set_remote_credentials(ice_session_check_list(*ice_session, i), ice_ufrag, ice_pwd);
567                         }
568                         ice_dump_candidates(ice_session_check_list(*ice_session, i));
569                 }
570         }
571         desc->nstreams=i;
572
573         /* Get ICE remote ufrag and remote pwd */
574         ice_ufrag = ice_pwd = NULL;
575         for (i = 0; (i < SAL_MEDIA_DESCRIPTION_MAX_MESSAGE_ATTRIBUTES) && ((attr = sdp_message_attribute_get(msg, -1, i)) != NULL); i++) {
576                 if ((keywordcmp("ice-ufrag", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
577                         ice_ufrag = attr->a_att_value;
578                 } else if ((keywordcmp("ice-pwd", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) {
579                         ice_pwd = attr->a_att_value;
580                 } else if (keywordcmp("ice-lite", attr->a_att_field) == 0) {
581                         ice_lite = TRUE;
582                 }
583         }
584         if (*ice_session != NULL) {
585                 if (ice_session_just_created == TRUE) {
586                         if (ice_lite == TRUE) {
587                                 ice_session_set_role(*ice_session, IR_Controlling);
588                         } else {
589                                 ice_session_set_role(*ice_session, IR_Controlled);
590                         }
591                 }
592                 if ((ice_ufrag != NULL) && (ice_pwd != NULL)) {
593                         ice_session_set_remote_credentials(*ice_session, ice_ufrag, ice_pwd);
594                         ice_dump_session(*ice_session);
595                 }
596         }
597         return 0;
598 }