~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjmedia/src/pjmedia/transport_ice.c

  • Committer: Jackson Doak
  • Date: 2013-07-10 21:04:46 UTC
  • mfrom: (20.1.3 sid)
  • Revision ID: noskcaj@ubuntu.com-20130710210446-y8f587vza807icr9
Properly merged from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: transport_ice.c 3906 2011-12-09 07:19:25Z bennylp $ */
2
 
/* 
3
 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
 
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
19
 
 */
20
 
#include <pjmedia/transport_ice.h>
21
 
#include <pjnath/errno.h>
22
 
#include <pj/assert.h>
23
 
#include <pj/log.h>
24
 
#include <pj/pool.h>
25
 
#include <pj/rand.h>
26
 
 
27
 
#define THIS_FILE   "transport_ice.c"
28
 
#if 0
29
 
#   define TRACE__(expr)    PJ_LOG(5,expr)
30
 
#else
31
 
#   define TRACE__(expr)
32
 
#endif
33
 
 
34
 
enum oa_role
35
 
{
36
 
    ROLE_NONE,
37
 
    ROLE_OFFERER,
38
 
    ROLE_ANSWERER
39
 
};
40
 
 
41
 
struct sdp_state
42
 
{
43
 
    unsigned            match_comp_cnt; /* Matching number of components    */
44
 
    pj_bool_t           ice_mismatch;   /* Address doesn't match candidates */
45
 
    pj_bool_t           ice_restart;    /* Offer to restart ICE             */
46
 
    pj_ice_sess_role    local_role;     /* Our role                         */
47
 
};
48
 
 
49
 
struct transport_ice
50
 
{
51
 
    pjmedia_transport    base;
52
 
    pj_pool_t           *pool;
53
 
    int                  af;
54
 
    unsigned             options;       /**< Transport options.             */
55
 
 
56
 
    unsigned             comp_cnt;
57
 
    pj_ice_strans       *ice_st;
58
 
 
59
 
    pjmedia_ice_cb       cb;
60
 
    unsigned             media_option;
61
 
 
62
 
    pj_bool_t            initial_sdp;
63
 
    enum oa_role         oa_role;       /**< Last role in SDP offer/answer  */
64
 
    struct sdp_state     rem_offer_state;/**< Describes the remote offer    */
65
 
 
66
 
    void                *stream;
67
 
    pj_sockaddr          remote_rtp;
68
 
    pj_sockaddr          remote_rtcp;
69
 
    unsigned             addr_len;      /**< Length of addresses.           */
70
 
 
71
 
    pj_bool_t            use_ice;
72
 
    pj_sockaddr          rtp_src_addr;  /**< Actual source RTP address.     */
73
 
    pj_sockaddr          rtcp_src_addr; /**< Actual source RTCP address.    */
74
 
    unsigned             rtp_src_cnt;   /**< How many pkt from this addr.   */
75
 
    unsigned             rtcp_src_cnt;  /**< How many pkt from this addr.   */
76
 
 
77
 
    unsigned             tx_drop_pct;   /**< Percent of tx pkts to drop.    */
78
 
    unsigned             rx_drop_pct;   /**< Percent of rx pkts to drop.    */
79
 
 
80
 
    void               (*rtp_cb)(void*,
81
 
                                 void*,
82
 
                                 pj_ssize_t);
83
 
    void               (*rtcp_cb)(void*,
84
 
                                  void*,
85
 
                                  pj_ssize_t);
86
 
};
87
 
 
88
 
 
89
 
/*
90
 
 * These are media transport operations.
91
 
 */
92
 
static pj_status_t transport_get_info (pjmedia_transport *tp,
93
 
                                       pjmedia_transport_info *info);
94
 
static pj_status_t transport_attach   (pjmedia_transport *tp,
95
 
                                       void *user_data,
96
 
                                       const pj_sockaddr_t *rem_addr,
97
 
                                       const pj_sockaddr_t *rem_rtcp,
98
 
                                       unsigned addr_len,
99
 
                                       void (*rtp_cb)(void*,
100
 
                                                      void*,
101
 
                                                      pj_ssize_t),
102
 
                                       void (*rtcp_cb)(void*,
103
 
                                                       void*,
104
 
                                                       pj_ssize_t));
105
 
static void        transport_detach   (pjmedia_transport *tp,
106
 
                                       void *strm);
107
 
static pj_status_t transport_send_rtp( pjmedia_transport *tp,
108
 
                                       const void *pkt,
109
 
                                       pj_size_t size);
110
 
static pj_status_t transport_send_rtcp(pjmedia_transport *tp,
111
 
                                       const void *pkt,
112
 
                                       pj_size_t size);
113
 
static pj_status_t transport_send_rtcp2(pjmedia_transport *tp,
114
 
                                       const pj_sockaddr_t *addr,
115
 
                                       unsigned addr_len,
116
 
                                       const void *pkt,
117
 
                                       pj_size_t size);
118
 
static pj_status_t transport_media_create(pjmedia_transport *tp,
119
 
                                       pj_pool_t *pool,
120
 
                                       unsigned options,
121
 
                                       const pjmedia_sdp_session *rem_sdp,
122
 
                                       unsigned media_index);
123
 
static pj_status_t transport_encode_sdp(pjmedia_transport *tp,
124
 
                                        pj_pool_t *tmp_pool,
125
 
                                        pjmedia_sdp_session *sdp_local,
126
 
                                        const pjmedia_sdp_session *rem_sdp,
127
 
                                        unsigned media_index);
128
 
static pj_status_t transport_media_start(pjmedia_transport *tp,
129
 
                                       pj_pool_t *pool,
130
 
                                       const pjmedia_sdp_session *sdp_local,
131
 
                                       const pjmedia_sdp_session *rem_sdp,
132
 
                                       unsigned media_index);
133
 
static pj_status_t transport_media_stop(pjmedia_transport *tp);
134
 
static pj_status_t transport_simulate_lost(pjmedia_transport *tp,
135
 
                                       pjmedia_dir dir,
136
 
                                       unsigned pct_lost);
137
 
static pj_status_t transport_destroy  (pjmedia_transport *tp);
138
 
 
139
 
/*
140
 
 * And these are ICE callbacks.
141
 
 */
142
 
static void ice_on_rx_data(pj_ice_strans *ice_st, 
143
 
                           unsigned comp_id, 
144
 
                           void *pkt, pj_size_t size,
145
 
                           const pj_sockaddr_t *src_addr,
146
 
                           unsigned src_addr_len);
147
 
static void ice_on_ice_complete(pj_ice_strans *ice_st, 
148
 
                                pj_ice_strans_op op,
149
 
                                pj_status_t status);
150
 
 
151
 
 
152
 
static pjmedia_transport_op transport_ice_op = 
153
 
{
154
 
    &transport_get_info,
155
 
    &transport_attach,
156
 
    &transport_detach,
157
 
    &transport_send_rtp,
158
 
    &transport_send_rtcp,
159
 
    &transport_send_rtcp2,
160
 
    &transport_media_create,
161
 
    &transport_encode_sdp,
162
 
    &transport_media_start,
163
 
    &transport_media_stop,
164
 
    &transport_simulate_lost,
165
 
    &transport_destroy
166
 
};
167
 
 
168
 
static const pj_str_t STR_RTP_AVP       = { "RTP/AVP", 7 };
169
 
static const pj_str_t STR_CANDIDATE     = { "candidate", 9};
170
 
static const pj_str_t STR_REM_CAND      = { "remote-candidates", 17 };
171
 
static const pj_str_t STR_ICE_LITE      = { "ice-lite", 8};
172
 
static const pj_str_t STR_ICE_MISMATCH  = { "ice-mismatch", 12};
173
 
static const pj_str_t STR_ICE_UFRAG     = { "ice-ufrag", 9 };
174
 
static const pj_str_t STR_ICE_PWD       = { "ice-pwd", 7 };
175
 
static const pj_str_t STR_IP4           = { "IP4", 3 };
176
 
static const pj_str_t STR_IP6           = { "IP6", 3 };
177
 
static const pj_str_t STR_RTCP          = { "rtcp", 4 };
178
 
static const pj_str_t STR_BANDW_RR      = { "RR", 2 };
179
 
static const pj_str_t STR_BANDW_RS      = { "RS", 2 };
180
 
 
181
 
enum {
182
 
    COMP_RTP = 1,
183
 
    COMP_RTCP = 2
184
 
};
185
 
 
186
 
/*
187
 
 * Create ICE media transport.
188
 
 */
189
 
PJ_DEF(pj_status_t) pjmedia_ice_create(pjmedia_endpt *endpt,
190
 
                                       const char *name,
191
 
                                       unsigned comp_cnt,
192
 
                                       const pj_ice_strans_cfg *cfg,
193
 
                                       const pjmedia_ice_cb *cb,
194
 
                                       pjmedia_transport **p_tp)
195
 
{
196
 
    return pjmedia_ice_create2(endpt, name, comp_cnt, cfg, cb, 0, p_tp);
197
 
}
198
 
 
199
 
/*
200
 
 * Create ICE media transport.
201
 
 */
202
 
PJ_DEF(pj_status_t) pjmedia_ice_create2(pjmedia_endpt *endpt,
203
 
                                        const char *name,
204
 
                                        unsigned comp_cnt,
205
 
                                        const pj_ice_strans_cfg *cfg,
206
 
                                        const pjmedia_ice_cb *cb,
207
 
                                        unsigned options,
208
 
                                        pjmedia_transport **p_tp)
209
 
{
210
 
    pj_pool_t *pool;
211
 
    pj_ice_strans_cb ice_st_cb;
212
 
    struct transport_ice *tp_ice;
213
 
    pj_status_t status;
214
 
 
215
 
    PJ_ASSERT_RETURN(endpt && comp_cnt && cfg && p_tp, PJ_EINVAL);
216
 
 
217
 
    /* Create transport instance */
218
 
    pool = pjmedia_endpt_create_pool(endpt, name, 512, 512);
219
 
    tp_ice = PJ_POOL_ZALLOC_T(pool, struct transport_ice);
220
 
    tp_ice->pool = pool;
221
 
    tp_ice->af = cfg->af;
222
 
    tp_ice->options = options;
223
 
    tp_ice->comp_cnt = comp_cnt;
224
 
    pj_ansi_strcpy(tp_ice->base.name, pool->obj_name);
225
 
    tp_ice->base.op = &transport_ice_op;
226
 
    tp_ice->base.type = PJMEDIA_TRANSPORT_TYPE_ICE;
227
 
    tp_ice->initial_sdp = PJ_TRUE;
228
 
    tp_ice->oa_role = ROLE_NONE;
229
 
    tp_ice->use_ice = PJ_FALSE;
230
 
 
231
 
    if (cb)
232
 
        pj_memcpy(&tp_ice->cb, cb, sizeof(pjmedia_ice_cb));
233
 
 
234
 
    /* Assign return value first because ICE might call callback
235
 
     * in create()
236
 
     */
237
 
    *p_tp = &tp_ice->base;
238
 
 
239
 
    /* Configure ICE callbacks */
240
 
    pj_bzero(&ice_st_cb, sizeof(ice_st_cb));
241
 
    ice_st_cb.on_ice_complete = &ice_on_ice_complete;
242
 
    ice_st_cb.on_rx_data = &ice_on_rx_data;
243
 
 
244
 
    /* Create ICE */
245
 
    status = pj_ice_strans_create(name, cfg, comp_cnt, tp_ice, 
246
 
                                  &ice_st_cb, &tp_ice->ice_st);
247
 
    if (status != PJ_SUCCESS) {
248
 
        pj_pool_release(pool);
249
 
        *p_tp = NULL;
250
 
        return status;
251
 
    }
252
 
 
253
 
    /* Done */
254
 
    return PJ_SUCCESS;
255
 
}
256
 
 
257
 
/* Disable ICE when SDP from remote doesn't contain a=candidate line */
258
 
static void set_no_ice(struct transport_ice *tp_ice, const char *reason,
259
 
                       pj_status_t err)
260
 
{
261
 
    if (err != PJ_SUCCESS) {
262
 
        char errmsg[PJ_ERR_MSG_SIZE];
263
 
        pj_strerror(err, errmsg, sizeof(errmsg));
264
 
        PJ_LOG(4,(tp_ice->base.name, 
265
 
                  "Stopping ICE, reason=%s:%s", reason, errmsg));
266
 
    } else {
267
 
        PJ_LOG(4,(tp_ice->base.name, 
268
 
                  "Stopping ICE, reason=%s", reason));
269
 
    }
270
 
 
271
 
    pj_ice_strans_stop_ice(tp_ice->ice_st);
272
 
 
273
 
    tp_ice->use_ice = PJ_FALSE;
274
 
}
275
 
 
276
 
 
277
 
/* Create SDP candidate attribute */
278
 
static int print_sdp_cand_attr(char *buffer, int max_len,
279
 
                               const pj_ice_sess_cand *cand)
280
 
{
281
 
    char ipaddr[PJ_INET6_ADDRSTRLEN+2];
282
 
    int len, len2;
283
 
 
284
 
    len = pj_ansi_snprintf( buffer, max_len,
285
 
                            "%.*s %u UDP %u %s %u typ ",
286
 
                            (int)cand->foundation.slen,
287
 
                            cand->foundation.ptr,
288
 
                            (unsigned)cand->comp_id,
289
 
                            cand->prio,
290
 
                            pj_sockaddr_print(&cand->addr, ipaddr, 
291
 
                                              sizeof(ipaddr), 0),
292
 
                            (unsigned)pj_sockaddr_get_port(&cand->addr));
293
 
    if (len < 1 || len >= max_len)
294
 
        return -1;
295
 
 
296
 
    switch (cand->type) {
297
 
    case PJ_ICE_CAND_TYPE_HOST:
298
 
        len2 = pj_ansi_snprintf(buffer+len, max_len-len, "host");
299
 
        break;
300
 
    case PJ_ICE_CAND_TYPE_SRFLX:
301
 
    case PJ_ICE_CAND_TYPE_RELAYED:
302
 
    case PJ_ICE_CAND_TYPE_PRFLX:
303
 
        len2 = pj_ansi_snprintf(buffer+len, max_len-len,
304
 
                                "%s raddr %s rport %d",
305
 
                                pj_ice_get_cand_type_name(cand->type),
306
 
                                pj_sockaddr_print(&cand->rel_addr, ipaddr,
307
 
                                                  sizeof(ipaddr), 0),
308
 
                                (int)pj_sockaddr_get_port(&cand->rel_addr));
309
 
        break;
310
 
    default:
311
 
        pj_assert(!"Invalid candidate type");
312
 
        len2 = -1;
313
 
        break;
314
 
    }
315
 
    if (len2 < 1 || len2 >= max_len)
316
 
        return -1;
317
 
 
318
 
    return len+len2;
319
 
}
320
 
 
321
 
 
322
 
/* Get ice-ufrag and ice-pwd attribute */
323
 
static void get_ice_attr(const pjmedia_sdp_session *rem_sdp,
324
 
                         const pjmedia_sdp_media *rem_m,
325
 
                         const pjmedia_sdp_attr **p_ice_ufrag,
326
 
                         const pjmedia_sdp_attr **p_ice_pwd)
327
 
{
328
 
    pjmedia_sdp_attr *attr;
329
 
 
330
 
    /* Find ice-ufrag attribute in media descriptor */
331
 
    attr = pjmedia_sdp_attr_find(rem_m->attr_count, rem_m->attr,
332
 
                                 &STR_ICE_UFRAG, NULL);
333
 
    if (attr == NULL) {
334
 
        /* Find ice-ufrag attribute in session descriptor */
335
 
        attr = pjmedia_sdp_attr_find(rem_sdp->attr_count, rem_sdp->attr,
336
 
                                     &STR_ICE_UFRAG, NULL);
337
 
    }
338
 
    *p_ice_ufrag = attr;
339
 
 
340
 
    /* Find ice-pwd attribute in media descriptor */
341
 
    attr = pjmedia_sdp_attr_find(rem_m->attr_count, rem_m->attr,
342
 
                                 &STR_ICE_PWD, NULL);
343
 
    if (attr == NULL) {
344
 
        /* Find ice-pwd attribute in session descriptor */
345
 
        attr = pjmedia_sdp_attr_find(rem_sdp->attr_count, rem_sdp->attr,
346
 
                                     &STR_ICE_PWD, NULL);
347
 
    }
348
 
    *p_ice_pwd = attr;
349
 
}
350
 
 
351
 
 
352
 
/* Encode and add "a=ice-mismatch" attribute in the SDP */
353
 
static void encode_ice_mismatch(pj_pool_t *sdp_pool,
354
 
                                pjmedia_sdp_session *sdp_local,
355
 
                                unsigned media_index)
356
 
{
357
 
    pjmedia_sdp_attr *attr;
358
 
    pjmedia_sdp_media *m = sdp_local->media[media_index];
359
 
 
360
 
    attr = PJ_POOL_ALLOC_T(sdp_pool, pjmedia_sdp_attr);
361
 
    attr->name = STR_ICE_MISMATCH;
362
 
    attr->value.slen = 0;
363
 
    pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
364
 
}
365
 
 
366
 
 
367
 
/* Encode ICE information in SDP */
368
 
static pj_status_t encode_session_in_sdp(struct transport_ice *tp_ice,
369
 
                                         pj_pool_t *sdp_pool,
370
 
                                         pjmedia_sdp_session *sdp_local,
371
 
                                         unsigned media_index,
372
 
                                         unsigned comp_cnt,
373
 
                                         pj_bool_t restart_session)
374
 
{
375
 
    enum { 
376
 
        ATTR_BUF_LEN = 160,     /* Max len of a=candidate attr */
377
 
        RATTR_BUF_LEN= 160      /* Max len of a=remote-candidates attr */
378
 
    };
379
 
    pjmedia_sdp_media *m = sdp_local->media[media_index];
380
 
    pj_str_t local_ufrag, local_pwd;
381
 
    pjmedia_sdp_attr *attr;
382
 
    pj_status_t status;
383
 
 
384
 
    /* Must have a session */
385
 
    PJ_ASSERT_RETURN(pj_ice_strans_has_sess(tp_ice->ice_st), PJ_EBUG);
386
 
 
387
 
    /* Get ufrag and pwd from current session */
388
 
    pj_ice_strans_get_ufrag_pwd(tp_ice->ice_st, &local_ufrag, &local_pwd,
389
 
                                NULL, NULL);
390
 
 
391
 
    /* The listing of candidates depends on whether ICE has completed
392
 
     * or not. When ICE has completed:
393
 
     *
394
 
     * 9.1.2.2: Existing Media Streams with ICE Completed
395
 
     *   The agent MUST include a candidate attributes for candidates
396
 
     *   matching the default destination for each component of the 
397
 
     *   media stream, and MUST NOT include any other candidates.
398
 
     *
399
 
     * When ICE has not completed, we shall include all candidates.
400
 
     *
401
 
     * Except when we have detected that remote is offering to restart
402
 
     * the session, in this case we will answer with full ICE SDP and
403
 
     * new ufrag/pwd pair.
404
 
     */
405
 
    if (!restart_session && pj_ice_strans_sess_is_complete(tp_ice->ice_st) &&
406
 
        pj_ice_strans_get_state(tp_ice->ice_st) != PJ_ICE_STRANS_STATE_FAILED)
407
 
    {
408
 
        const pj_ice_sess_check *check;
409
 
        char *attr_buf;
410
 
        pjmedia_sdp_conn *conn;
411
 
        pjmedia_sdp_attr *a_rtcp;
412
 
        pj_str_t rem_cand;
413
 
        unsigned comp;
414
 
 
415
 
        /* Encode ice-ufrag attribute */
416
 
        attr = pjmedia_sdp_attr_create(sdp_pool, STR_ICE_UFRAG.ptr,
417
 
                                       &local_ufrag);
418
 
        pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
419
 
 
420
 
        /* Encode ice-pwd attribute */
421
 
        attr = pjmedia_sdp_attr_create(sdp_pool, STR_ICE_PWD.ptr, 
422
 
                                       &local_pwd);
423
 
        pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
424
 
 
425
 
        /* Prepare buffer */
426
 
        attr_buf = (char*) pj_pool_alloc(sdp_pool, ATTR_BUF_LEN);
427
 
        rem_cand.ptr = (char*) pj_pool_alloc(sdp_pool, RATTR_BUF_LEN);
428
 
        rem_cand.slen = 0;
429
 
 
430
 
        /* 9.1.2.2: Existing Media Streams with ICE Completed
431
 
         *   The default destination for media (i.e., the values of 
432
 
         *   the IP addresses and ports in the m and c line used for
433
 
         *   that media stream) MUST be the local candidate from the
434
 
         *   highest priority nominated pair in the valid list for each
435
 
         *   component.
436
 
         */
437
 
        check = pj_ice_strans_get_valid_pair(tp_ice->ice_st, 1);
438
 
        if (check == NULL) {
439
 
            pj_assert(!"Shouldn't happen");
440
 
            return PJ_EBUG;
441
 
        }
442
 
 
443
 
        /* Override connection line address and media port number */
444
 
        conn = m->conn;
445
 
        if (conn == NULL)
446
 
            conn = sdp_local->conn;
447
 
 
448
 
        conn->addr.ptr = (char*) pj_pool_alloc(sdp_pool, 
449
 
                                               PJ_INET6_ADDRSTRLEN);
450
 
        pj_sockaddr_print(&check->lcand->addr, conn->addr.ptr, 
451
 
                          PJ_INET6_ADDRSTRLEN, 0);
452
 
        conn->addr.slen = pj_ansi_strlen(conn->addr.ptr);
453
 
        m->desc.port = pj_sockaddr_get_port(&check->lcand->addr);
454
 
 
455
 
        /* Override address RTCP attribute if it's present */
456
 
        if (comp_cnt == 2 &&
457
 
            (check = pj_ice_strans_get_valid_pair(tp_ice->ice_st, 
458
 
                                                  COMP_RTCP)) != NULL &&
459
 
            (a_rtcp = pjmedia_sdp_attr_find(m->attr_count, m->attr, 
460
 
                                            &STR_RTCP, 0)) != NULL) 
461
 
        {
462
 
            pjmedia_sdp_attr_remove(&m->attr_count, m->attr, a_rtcp);
463
 
 
464
 
            a_rtcp = pjmedia_sdp_attr_create_rtcp(sdp_pool, 
465
 
                                                  &check->lcand->addr);
466
 
            if (a_rtcp)
467
 
                pjmedia_sdp_attr_add(&m->attr_count, m->attr, a_rtcp);
468
 
        }
469
 
 
470
 
        /* Encode only candidates matching the default destination 
471
 
         * for each component 
472
 
         */
473
 
        for (comp=0; comp < comp_cnt; ++comp) {
474
 
            int len;
475
 
            pj_str_t value;
476
 
 
477
 
            /* Get valid pair for this component */
478
 
            check = pj_ice_strans_get_valid_pair(tp_ice->ice_st, comp+1);
479
 
            if (check == NULL)
480
 
                continue;
481
 
 
482
 
            /* Print and add local candidate in the pair */
483
 
            value.ptr = attr_buf;
484
 
            value.slen = print_sdp_cand_attr(attr_buf, ATTR_BUF_LEN, 
485
 
                                             check->lcand);
486
 
            if (value.slen < 0) {
487
 
                pj_assert(!"Not enough attr_buf to print candidate");
488
 
                return PJ_EBUG;
489
 
            }
490
 
 
491
 
            attr = pjmedia_sdp_attr_create(sdp_pool, STR_CANDIDATE.ptr,
492
 
                                           &value);
493
 
            pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
494
 
 
495
 
            /* Append to a=remote-candidates attribute */
496
 
            if (pj_ice_strans_get_role(tp_ice->ice_st) == 
497
 
                                    PJ_ICE_SESS_ROLE_CONTROLLING) 
498
 
            {
499
 
                char rem_addr[PJ_INET6_ADDRSTRLEN];
500
 
 
501
 
                pj_sockaddr_print(&check->rcand->addr, rem_addr, 
502
 
                                  sizeof(rem_addr), 0);
503
 
                len = pj_ansi_snprintf(
504
 
                           rem_cand.ptr + rem_cand.slen,
505
 
                           RATTR_BUF_LEN - rem_cand.slen,
506
 
                           "%s%u %s %u", 
507
 
                           (rem_cand.slen==0? "" : " "),
508
 
                           comp+1, rem_addr,
509
 
                           pj_sockaddr_get_port(&check->rcand->addr)
510
 
                           );
511
 
                if (len < 1 || len >= RATTR_BUF_LEN) {
512
 
                    pj_assert(!"Not enough buffer to print "
513
 
                               "remote-candidates");
514
 
                    return PJ_EBUG;
515
 
                }
516
 
 
517
 
                rem_cand.slen += len;
518
 
            }
519
 
        }
520
 
 
521
 
        /* 9.1.2.2: Existing Media Streams with ICE Completed
522
 
         *   In addition, if the agent is controlling, it MUST include
523
 
         *   the a=remote-candidates attribute for each media stream 
524
 
         *   whose check list is in the Completed state.  The attribute
525
 
         *   contains the remote candidates from the highest priority 
526
 
         *   nominated pair in the valid list for each component of that
527
 
         *   media stream.
528
 
         */
529
 
        if (pj_ice_strans_get_role(tp_ice->ice_st) == 
530
 
                                    PJ_ICE_SESS_ROLE_CONTROLLING) 
531
 
        {
532
 
            attr = pjmedia_sdp_attr_create(sdp_pool, STR_REM_CAND.ptr, 
533
 
                                           &rem_cand);
534
 
            pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
535
 
        }
536
 
 
537
 
    } else if (pj_ice_strans_has_sess(tp_ice->ice_st) &&
538
 
               pj_ice_strans_get_state(tp_ice->ice_st) !=
539
 
                        PJ_ICE_STRANS_STATE_FAILED)
540
 
    {
541
 
        /* Encode all candidates to SDP media */
542
 
        char *attr_buf;
543
 
        unsigned comp;
544
 
 
545
 
        /* If ICE is not restarted, encode current ICE ufrag/pwd.
546
 
         * Otherwise generate new one.
547
 
         */
548
 
        if (!restart_session) {
549
 
            attr = pjmedia_sdp_attr_create(sdp_pool, STR_ICE_UFRAG.ptr,
550
 
                                           &local_ufrag);
551
 
            pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
552
 
 
553
 
            attr = pjmedia_sdp_attr_create(sdp_pool, STR_ICE_PWD.ptr, 
554
 
                                           &local_pwd);
555
 
            pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
556
 
 
557
 
        } else {
558
 
            pj_str_t str;
559
 
 
560
 
            str.slen = PJ_ICE_UFRAG_LEN;
561
 
            str.ptr = (char*) pj_pool_alloc(sdp_pool, str.slen);
562
 
            pj_create_random_string(str.ptr, str.slen);
563
 
            attr = pjmedia_sdp_attr_create(sdp_pool, STR_ICE_UFRAG.ptr, &str);
564
 
            pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
565
 
 
566
 
            str.ptr = (char*) pj_pool_alloc(sdp_pool, str.slen);
567
 
            pj_create_random_string(str.ptr, str.slen);
568
 
            attr = pjmedia_sdp_attr_create(sdp_pool, STR_ICE_PWD.ptr, &str);
569
 
            pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
570
 
        }
571
 
 
572
 
        /* Create buffer to encode candidates as SDP attribute */
573
 
        attr_buf = (char*) pj_pool_alloc(sdp_pool, ATTR_BUF_LEN);
574
 
 
575
 
        for (comp=0; comp < comp_cnt; ++comp) {
576
 
            unsigned cand_cnt;
577
 
            pj_ice_sess_cand cand[PJ_ICE_ST_MAX_CAND];
578
 
            unsigned i;
579
 
 
580
 
            cand_cnt = PJ_ARRAY_SIZE(cand);
581
 
            status = pj_ice_strans_enum_cands(tp_ice->ice_st, comp+1,
582
 
                                              &cand_cnt, cand);
583
 
            if (status != PJ_SUCCESS)
584
 
                return status;
585
 
 
586
 
            for (i=0; i<cand_cnt; ++i) {
587
 
                pj_str_t value;
588
 
 
589
 
                value.slen = print_sdp_cand_attr(attr_buf, ATTR_BUF_LEN, 
590
 
                                                 &cand[i]);
591
 
                if (value.slen < 0) {
592
 
                    pj_assert(!"Not enough attr_buf to print candidate");
593
 
                    return PJ_EBUG;
594
 
                }
595
 
 
596
 
                value.ptr = attr_buf;
597
 
                attr = pjmedia_sdp_attr_create(sdp_pool, 
598
 
                                               STR_CANDIDATE.ptr,
599
 
                                               &value);
600
 
                pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
601
 
            }
602
 
        }
603
 
    } else {
604
 
        /* ICE has failed, application should have terminated this call */
605
 
    }
606
 
 
607
 
    /* Removing a=rtcp line when there is only one component. */
608
 
    if (comp_cnt == 1) {
609
 
        attr = pjmedia_sdp_attr_find(m->attr_count, m->attr, &STR_RTCP, NULL);
610
 
        if (attr)
611
 
            pjmedia_sdp_attr_remove(&m->attr_count, m->attr, attr);
612
 
        /* If RTCP is not in use, we MUST send b=RS:0 and b=RR:0. */
613
 
        pj_assert(m->bandw_count + 2 <= PJ_ARRAY_SIZE(m->bandw));
614
 
        if (m->bandw_count + 2 <= PJ_ARRAY_SIZE(m->bandw)) {
615
 
            m->bandw[m->bandw_count] = PJ_POOL_ZALLOC_T(sdp_pool,
616
 
                                                        pjmedia_sdp_bandw);
617
 
            pj_memcpy(&m->bandw[m->bandw_count]->modifier, &STR_BANDW_RS,
618
 
                      sizeof(pj_str_t));
619
 
            m->bandw_count++;
620
 
            m->bandw[m->bandw_count] = PJ_POOL_ZALLOC_T(sdp_pool,
621
 
                                                        pjmedia_sdp_bandw);
622
 
            pj_memcpy(&m->bandw[m->bandw_count]->modifier, &STR_BANDW_RR,
623
 
                      sizeof(pj_str_t));
624
 
            m->bandw_count++;
625
 
        }
626
 
    }
627
 
    
628
 
 
629
 
    return PJ_SUCCESS;
630
 
}
631
 
 
632
 
 
633
 
/* Parse a=candidate line */
634
 
static pj_status_t parse_cand(const char *obj_name,
635
 
                              pj_pool_t *pool,
636
 
                              const pj_str_t *orig_input,
637
 
                              pj_ice_sess_cand *cand)
638
 
{
639
 
    pj_str_t input;
640
 
    char *token, *host;
641
 
    int af;
642
 
    pj_str_t s;
643
 
    pj_status_t status = PJNATH_EICEINCANDSDP;
644
 
 
645
 
    pj_bzero(cand, sizeof(*cand));
646
 
    pj_strdup_with_null(pool, &input, orig_input);
647
 
 
648
 
    PJ_UNUSED_ARG(obj_name);
649
 
 
650
 
    /* Foundation */
651
 
    token = strtok(input.ptr, " ");
652
 
    if (!token) {
653
 
        TRACE__((obj_name, "Expecting ICE foundation in candidate"));
654
 
        goto on_return;
655
 
    }
656
 
    pj_strdup2(pool, &cand->foundation, token);
657
 
 
658
 
    /* Component ID */
659
 
    token = strtok(NULL, " ");
660
 
    if (!token) {
661
 
        TRACE__((obj_name, "Expecting ICE component ID in candidate"));
662
 
        goto on_return;
663
 
    }
664
 
    cand->comp_id = (pj_uint8_t) atoi(token);
665
 
 
666
 
    /* Transport */
667
 
    token = strtok(NULL, " ");
668
 
    if (!token) {
669
 
        TRACE__((obj_name, "Expecting ICE transport in candidate"));
670
 
        goto on_return;
671
 
    }
672
 
    if (pj_ansi_stricmp(token, "UDP") != 0) {
673
 
        TRACE__((obj_name, 
674
 
                 "Expecting ICE UDP transport only in candidate"));
675
 
        goto on_return;
676
 
    }
677
 
 
678
 
    /* Priority */
679
 
    token = strtok(NULL, " ");
680
 
    if (!token) {
681
 
        TRACE__((obj_name, "Expecting ICE priority in candidate"));
682
 
        goto on_return;
683
 
    }
684
 
    cand->prio = atoi(token);
685
 
 
686
 
    /* Host */
687
 
    host = strtok(NULL, " ");
688
 
    if (!host) {
689
 
        TRACE__((obj_name, "Expecting ICE host in candidate"));
690
 
        goto on_return;
691
 
    }
692
 
    /* Detect address family */
693
 
    if (pj_ansi_strchr(host, ':'))
694
 
        af = pj_AF_INET6();
695
 
    else
696
 
        af = pj_AF_INET();
697
 
    /* Assign address */
698
 
    if (pj_sockaddr_init(af, &cand->addr, pj_cstr(&s, host), 0)) {
699
 
        TRACE__((obj_name, "Invalid ICE candidate address"));
700
 
        goto on_return;
701
 
    }
702
 
 
703
 
    /* Port */
704
 
    token = strtok(NULL, " ");
705
 
    if (!token) {
706
 
        TRACE__((obj_name, "Expecting ICE port number in candidate"));
707
 
        goto on_return;
708
 
    }
709
 
    pj_sockaddr_set_port(&cand->addr, (pj_uint16_t)atoi(token));
710
 
 
711
 
    /* typ */
712
 
    token = strtok(NULL, " ");
713
 
    if (!token) {
714
 
        TRACE__((obj_name, "Expecting ICE \"typ\" in candidate"));
715
 
        goto on_return;
716
 
    }
717
 
    if (pj_ansi_stricmp(token, "typ") != 0) {
718
 
        TRACE__((obj_name, "Expecting ICE \"typ\" in candidate"));
719
 
        goto on_return;
720
 
    }
721
 
 
722
 
    /* candidate type */
723
 
    token = strtok(NULL, " ");
724
 
    if (!token) {
725
 
        TRACE__((obj_name, "Expecting ICE candidate type in candidate"));
726
 
        goto on_return;
727
 
    }
728
 
 
729
 
    if (pj_ansi_stricmp(token, "host") == 0) {
730
 
        cand->type = PJ_ICE_CAND_TYPE_HOST;
731
 
 
732
 
    } else if (pj_ansi_stricmp(token, "srflx") == 0) {
733
 
        cand->type = PJ_ICE_CAND_TYPE_SRFLX;
734
 
 
735
 
    } else if (pj_ansi_stricmp(token, "relay") == 0) {
736
 
        cand->type = PJ_ICE_CAND_TYPE_RELAYED;
737
 
 
738
 
    } else if (pj_ansi_stricmp(token, "prflx") == 0) {
739
 
        cand->type = PJ_ICE_CAND_TYPE_PRFLX;
740
 
 
741
 
    } else {
742
 
        PJ_LOG(5,(obj_name, "Invalid ICE candidate type %s in candidate", 
743
 
                  token));
744
 
        goto on_return;
745
 
    }
746
 
 
747
 
    status = PJ_SUCCESS;
748
 
 
749
 
on_return:
750
 
    return status;
751
 
}
752
 
 
753
 
 
754
 
/* Create initial SDP offer */
755
 
static pj_status_t create_initial_offer(struct transport_ice *tp_ice,
756
 
                                        pj_pool_t *sdp_pool,
757
 
                                        pjmedia_sdp_session *loc_sdp,
758
 
                                        unsigned media_index)
759
 
{
760
 
    pj_status_t status;
761
 
 
762
 
    /* Encode ICE in SDP */
763
 
    status = encode_session_in_sdp(tp_ice, sdp_pool, loc_sdp, media_index, 
764
 
                                   tp_ice->comp_cnt, PJ_FALSE);
765
 
    if (status != PJ_SUCCESS) {
766
 
        set_no_ice(tp_ice, "Error encoding SDP answer", status);
767
 
        return status;
768
 
    }
769
 
 
770
 
    return PJ_SUCCESS;
771
 
}
772
 
 
773
 
 
774
 
/* Verify incoming offer */
775
 
static pj_status_t verify_ice_sdp(struct transport_ice *tp_ice,
776
 
                                  pj_pool_t *tmp_pool,
777
 
                                  const pjmedia_sdp_session *rem_sdp,
778
 
                                  unsigned media_index,
779
 
                                  pj_ice_sess_role current_ice_role,
780
 
                                  struct sdp_state *sdp_state)
781
 
{
782
 
    const pjmedia_sdp_media *rem_m;
783
 
    const pjmedia_sdp_attr *ufrag_attr, *pwd_attr;
784
 
    const pjmedia_sdp_conn *rem_conn;
785
 
    pj_bool_t comp1_found=PJ_FALSE, comp2_found=PJ_FALSE, has_rtcp=PJ_FALSE;
786
 
    pj_sockaddr rem_conn_addr, rtcp_addr;
787
 
    unsigned i;
788
 
    pj_status_t status;
789
 
 
790
 
    rem_m = rem_sdp->media[media_index];
791
 
 
792
 
    /* Get the "ice-ufrag" and "ice-pwd" attributes */
793
 
    get_ice_attr(rem_sdp, rem_m, &ufrag_attr, &pwd_attr);
794
 
 
795
 
    /* If "ice-ufrag" or "ice-pwd" are not found, disable ICE */
796
 
    if (ufrag_attr==NULL || pwd_attr==NULL) {
797
 
        sdp_state->match_comp_cnt = 0;
798
 
        return PJ_SUCCESS;
799
 
    }
800
 
 
801
 
    /* Verify that default target for each component matches one of the 
802
 
     * candidate for the component. Otherwise stop ICE with ICE ice_mismatch 
803
 
     * error.
804
 
     */
805
 
 
806
 
    /* Component 1 is the c= line */
807
 
    rem_conn = rem_m->conn;
808
 
    if (rem_conn == NULL)
809
 
        rem_conn = rem_sdp->conn;
810
 
    if (!rem_conn)
811
 
        return PJMEDIA_SDP_EMISSINGCONN;
812
 
 
813
 
    /* Verify address family matches */
814
 
    if ((tp_ice->af==pj_AF_INET() && 
815
 
         pj_strcmp(&rem_conn->addr_type, &STR_IP4)!=0) ||
816
 
        (tp_ice->af==pj_AF_INET6() && 
817
 
         pj_strcmp(&rem_conn->addr_type, &STR_IP6)!=0))
818
 
    {
819
 
        return PJMEDIA_SDP_ETPORTNOTEQUAL;
820
 
    }
821
 
 
822
 
    /* Assign remote connection address */
823
 
    status = pj_sockaddr_init(tp_ice->af, &rem_conn_addr, &rem_conn->addr,
824
 
                              (pj_uint16_t)rem_m->desc.port);
825
 
    if (status != PJ_SUCCESS)
826
 
        return status;
827
 
 
828
 
    if (tp_ice->comp_cnt > 1) {
829
 
        const pjmedia_sdp_attr *attr;
830
 
 
831
 
        /* Get default RTCP candidate from a=rtcp line, if present, otherwise
832
 
         * calculate default RTCP candidate from default RTP target.
833
 
         */
834
 
        attr = pjmedia_sdp_attr_find(rem_m->attr_count, rem_m->attr, 
835
 
                                     &STR_RTCP, NULL);
836
 
        has_rtcp = (attr != NULL);
837
 
 
838
 
        if (attr) {
839
 
            pjmedia_sdp_rtcp_attr rtcp_attr;
840
 
 
841
 
            status = pjmedia_sdp_attr_get_rtcp(attr, &rtcp_attr);
842
 
            if (status != PJ_SUCCESS) {
843
 
                /* Error parsing a=rtcp attribute */
844
 
                return status;
845
 
            }
846
 
        
847
 
            if (rtcp_attr.addr.slen) {
848
 
                /* Verify address family matches */
849
 
                if ((tp_ice->af==pj_AF_INET() && 
850
 
                     pj_strcmp(&rtcp_attr.addr_type, &STR_IP4)!=0) ||
851
 
                    (tp_ice->af==pj_AF_INET6() && 
852
 
                     pj_strcmp(&rtcp_attr.addr_type, &STR_IP6)!=0))
853
 
                {
854
 
                    return PJMEDIA_SDP_ETPORTNOTEQUAL;
855
 
                }
856
 
 
857
 
                /* Assign RTCP address */
858
 
                status = pj_sockaddr_init(tp_ice->af, &rtcp_addr,
859
 
                                          &rtcp_attr.addr,
860
 
                                          (pj_uint16_t)rtcp_attr.port);
861
 
                if (status != PJ_SUCCESS) {
862
 
                    return PJMEDIA_SDP_EINRTCP;
863
 
                }
864
 
            } else {
865
 
                /* Assign RTCP address */
866
 
                status = pj_sockaddr_init(tp_ice->af, &rtcp_addr, 
867
 
                                          NULL, 
868
 
                                          (pj_uint16_t)rtcp_attr.port);
869
 
                if (status != PJ_SUCCESS) {
870
 
                    return PJMEDIA_SDP_EINRTCP;
871
 
                }
872
 
                pj_sockaddr_copy_addr(&rtcp_addr, &rem_conn_addr);
873
 
            }
874
 
        } else {
875
 
            unsigned rtcp_port;
876
 
        
877
 
            rtcp_port = pj_sockaddr_get_port(&rem_conn_addr) + 1;
878
 
            pj_sockaddr_cp(&rtcp_addr, &rem_conn_addr);
879
 
            pj_sockaddr_set_port(&rtcp_addr, (pj_uint16_t)rtcp_port);
880
 
        }
881
 
    }
882
 
 
883
 
    /* Find the default addresses in a=candidate attributes. 
884
 
     */
885
 
    for (i=0; i<rem_m->attr_count; ++i) {
886
 
        pj_ice_sess_cand cand;
887
 
 
888
 
        if (pj_strcmp(&rem_m->attr[i]->name, &STR_CANDIDATE)!=0)
889
 
            continue;
890
 
 
891
 
        status = parse_cand(tp_ice->base.name, tmp_pool, 
892
 
                            &rem_m->attr[i]->value, &cand);
893
 
        if (status != PJ_SUCCESS) {
894
 
            PJ_LOG(4,(tp_ice->base.name, 
895
 
                      "Error in parsing SDP candidate attribute '%.*s', "
896
 
                      "candidate is ignored",
897
 
                      (int)rem_m->attr[i]->value.slen, 
898
 
                      rem_m->attr[i]->value.ptr));
899
 
            continue;
900
 
        }
901
 
 
902
 
        if (!comp1_found && cand.comp_id==COMP_RTP &&
903
 
            pj_sockaddr_cmp(&rem_conn_addr, &cand.addr)==0) 
904
 
        {
905
 
            comp1_found = PJ_TRUE;
906
 
        } else if (!comp2_found && cand.comp_id==COMP_RTCP &&
907
 
                    pj_sockaddr_cmp(&rtcp_addr, &cand.addr)==0) 
908
 
        {
909
 
            comp2_found = PJ_TRUE;
910
 
        }
911
 
 
912
 
        if (cand.comp_id == COMP_RTCP)
913
 
            has_rtcp = PJ_TRUE;
914
 
 
915
 
        if (comp1_found && (comp2_found || tp_ice->comp_cnt==1))
916
 
            break;
917
 
    }
918
 
 
919
 
    /* Check matched component count and ice_mismatch */
920
 
    if (comp1_found && (tp_ice->comp_cnt==1 || !has_rtcp)) {
921
 
        sdp_state->match_comp_cnt = 1;
922
 
        sdp_state->ice_mismatch = PJ_FALSE;
923
 
    } else if (comp1_found && comp2_found) {
924
 
        sdp_state->match_comp_cnt = 2;
925
 
        sdp_state->ice_mismatch = PJ_FALSE;
926
 
    } else {
927
 
        sdp_state->match_comp_cnt = (tp_ice->comp_cnt > 1 && has_rtcp)? 2 : 1;
928
 
        sdp_state->ice_mismatch = PJ_TRUE;
929
 
    }
930
 
 
931
 
 
932
 
    /* Detect remote restarting session */
933
 
    if (pj_ice_strans_has_sess(tp_ice->ice_st) &&
934
 
        (pj_ice_strans_sess_is_running(tp_ice->ice_st) ||
935
 
         pj_ice_strans_sess_is_complete(tp_ice->ice_st))) 
936
 
    {
937
 
        pj_str_t rem_run_ufrag, rem_run_pwd;
938
 
        pj_ice_strans_get_ufrag_pwd(tp_ice->ice_st, NULL, NULL,
939
 
                                    &rem_run_ufrag, &rem_run_pwd);
940
 
        if (pj_strcmp(&ufrag_attr->value, &rem_run_ufrag) ||
941
 
            pj_strcmp(&pwd_attr->value, &rem_run_pwd))
942
 
        {
943
 
            /* Remote offers to restart ICE */
944
 
            sdp_state->ice_restart = PJ_TRUE;
945
 
        } else {
946
 
            sdp_state->ice_restart = PJ_FALSE;
947
 
        }
948
 
    } else {
949
 
        sdp_state->ice_restart = PJ_FALSE;
950
 
    }
951
 
 
952
 
    /* Detect our role */
953
 
    if (current_ice_role==PJ_ICE_SESS_ROLE_CONTROLLING) {
954
 
        sdp_state->local_role = PJ_ICE_SESS_ROLE_CONTROLLING;
955
 
    } else {
956
 
        if (pjmedia_sdp_attr_find(rem_sdp->attr_count, rem_sdp->attr,
957
 
                                  &STR_ICE_LITE, NULL) != NULL)
958
 
        {
959
 
            /* Remote is ICE Lite */
960
 
            sdp_state->local_role = PJ_ICE_SESS_ROLE_CONTROLLING;
961
 
        } else {
962
 
            sdp_state->local_role = PJ_ICE_SESS_ROLE_CONTROLLED;
963
 
        }
964
 
    }
965
 
 
966
 
    PJ_LOG(4,(tp_ice->base.name, 
967
 
              "Processing SDP: support ICE=%u, common comp_cnt=%u, "
968
 
              "ice_mismatch=%u, ice_restart=%u, local_role=%s",
969
 
              (sdp_state->match_comp_cnt != 0), 
970
 
              sdp_state->match_comp_cnt, 
971
 
              sdp_state->ice_mismatch, 
972
 
              sdp_state->ice_restart,
973
 
              pj_ice_sess_role_name(sdp_state->local_role)));
974
 
 
975
 
    return PJ_SUCCESS;
976
 
 
977
 
}
978
 
 
979
 
 
980
 
/* Verify incoming offer and create initial answer */
981
 
static pj_status_t create_initial_answer(struct transport_ice *tp_ice,
982
 
                                         pj_pool_t *sdp_pool,
983
 
                                         pjmedia_sdp_session *loc_sdp,
984
 
                                         const pjmedia_sdp_session *rem_sdp,
985
 
                                         unsigned media_index)
986
 
{
987
 
    const pjmedia_sdp_media *rem_m = rem_sdp->media[media_index];
988
 
    pj_status_t status;
989
 
 
990
 
    /* Check if media is removed (just in case) */
991
 
    if (rem_m->desc.port == 0) {
992
 
        return PJ_SUCCESS;
993
 
    }
994
 
 
995
 
    /* Verify the offer */
996
 
    status = verify_ice_sdp(tp_ice, sdp_pool, rem_sdp, media_index, 
997
 
                            PJ_ICE_SESS_ROLE_CONTROLLED, 
998
 
                            &tp_ice->rem_offer_state);
999
 
    if (status != PJ_SUCCESS) {
1000
 
        set_no_ice(tp_ice, "Invalid SDP offer", status);
1001
 
        return status;
1002
 
    }
1003
 
 
1004
 
    /* Does remote support ICE? */
1005
 
    if (tp_ice->rem_offer_state.match_comp_cnt==0) {
1006
 
        set_no_ice(tp_ice, "No ICE found in SDP offer", PJ_SUCCESS);
1007
 
        return PJ_SUCCESS;
1008
 
    }
1009
 
 
1010
 
    /* ICE ice_mismatch? */
1011
 
    if (tp_ice->rem_offer_state.ice_mismatch) {
1012
 
        set_no_ice(tp_ice, "ICE ice_mismatch in remote offer", PJ_SUCCESS);
1013
 
        encode_ice_mismatch(sdp_pool, loc_sdp, media_index);
1014
 
        return PJ_SUCCESS;
1015
 
    }
1016
 
 
1017
 
    /* Encode ICE in SDP */
1018
 
    status = encode_session_in_sdp(tp_ice, sdp_pool, loc_sdp, media_index, 
1019
 
                                   tp_ice->rem_offer_state.match_comp_cnt,
1020
 
                                   PJ_FALSE);
1021
 
    if (status != PJ_SUCCESS) {
1022
 
        set_no_ice(tp_ice, "Error encoding SDP answer", status);
1023
 
        return status;
1024
 
    }
1025
 
 
1026
 
    return PJ_SUCCESS;
1027
 
}
1028
 
 
1029
 
 
1030
 
/* Create subsequent SDP offer */
1031
 
static pj_status_t create_subsequent_offer(struct transport_ice *tp_ice,
1032
 
                                           pj_pool_t *sdp_pool,
1033
 
                                           pjmedia_sdp_session *loc_sdp,
1034
 
                                           unsigned media_index)
1035
 
{
1036
 
    unsigned comp_cnt;
1037
 
 
1038
 
    if (pj_ice_strans_has_sess(tp_ice->ice_st) == PJ_FALSE) {
1039
 
        /* We don't have ICE */
1040
 
        return PJ_SUCCESS;
1041
 
    }
1042
 
 
1043
 
    comp_cnt = pj_ice_strans_get_running_comp_cnt(tp_ice->ice_st);
1044
 
    return encode_session_in_sdp(tp_ice, sdp_pool, loc_sdp, media_index,
1045
 
                                 comp_cnt, PJ_FALSE);
1046
 
}
1047
 
 
1048
 
 
1049
 
/* Create subsequent SDP answer */
1050
 
static pj_status_t create_subsequent_answer(struct transport_ice *tp_ice,
1051
 
                                            pj_pool_t *sdp_pool,
1052
 
                                            pjmedia_sdp_session *loc_sdp,
1053
 
                                            const pjmedia_sdp_session *rem_sdp,
1054
 
                                            unsigned media_index)
1055
 
{
1056
 
    pj_status_t status;
1057
 
 
1058
 
    /* We have a session */
1059
 
    status = verify_ice_sdp(tp_ice, sdp_pool, rem_sdp, media_index, 
1060
 
                            PJ_ICE_SESS_ROLE_CONTROLLED, 
1061
 
                            &tp_ice->rem_offer_state);
1062
 
    if (status != PJ_SUCCESS) {
1063
 
        /* Something wrong with the offer */
1064
 
        return status;
1065
 
    }
1066
 
 
1067
 
    if (pj_ice_strans_has_sess(tp_ice->ice_st)) {
1068
 
        /*
1069
 
         * Received subsequent offer while we have ICE active.
1070
 
         */
1071
 
 
1072
 
        if (tp_ice->rem_offer_state.match_comp_cnt == 0) {
1073
 
            /* Remote no longer offers ICE */
1074
 
            return PJ_SUCCESS;
1075
 
        }
1076
 
 
1077
 
        if (tp_ice->rem_offer_state.ice_mismatch) {
1078
 
            encode_ice_mismatch(sdp_pool, loc_sdp, media_index);
1079
 
            return PJ_SUCCESS;
1080
 
        }
1081
 
 
1082
 
        status = encode_session_in_sdp(tp_ice, sdp_pool, loc_sdp, media_index,
1083
 
                                       tp_ice->rem_offer_state.match_comp_cnt,
1084
 
                                       tp_ice->rem_offer_state.ice_restart);
1085
 
        if (status != PJ_SUCCESS)
1086
 
            return status;
1087
 
 
1088
 
        /* Done */
1089
 
 
1090
 
    } else {
1091
 
        /*
1092
 
         * Received subsequent offer while we DON'T have ICE active.
1093
 
         */
1094
 
 
1095
 
        if (tp_ice->rem_offer_state.match_comp_cnt == 0) {
1096
 
            /* Remote does not support ICE */
1097
 
            return PJ_SUCCESS;
1098
 
        }
1099
 
 
1100
 
        if (tp_ice->rem_offer_state.ice_mismatch) {
1101
 
            encode_ice_mismatch(sdp_pool, loc_sdp, media_index);
1102
 
            return PJ_SUCCESS;
1103
 
        }
1104
 
 
1105
 
        /* Looks like now remote is offering ICE, so we need to create
1106
 
         * ICE session now.
1107
 
         */
1108
 
        status = pj_ice_strans_init_ice(tp_ice->ice_st, 
1109
 
                                        PJ_ICE_SESS_ROLE_CONTROLLED,
1110
 
                                        NULL, NULL);
1111
 
        if (status != PJ_SUCCESS) {
1112
 
            /* Fail to create new ICE session */
1113
 
            return status;
1114
 
        }
1115
 
 
1116
 
        status = encode_session_in_sdp(tp_ice, sdp_pool, loc_sdp, media_index,
1117
 
                                       tp_ice->rem_offer_state.match_comp_cnt,
1118
 
                                       tp_ice->rem_offer_state.ice_restart);
1119
 
        if (status != PJ_SUCCESS)
1120
 
            return status;
1121
 
 
1122
 
        /* Done */
1123
 
    }
1124
 
 
1125
 
    return PJ_SUCCESS;
1126
 
}
1127
 
 
1128
 
 
1129
 
/*
1130
 
 * For both UAC and UAS, pass in the SDP before sending it to remote.
1131
 
 * This will add ICE attributes to the SDP.
1132
 
 */
1133
 
static pj_status_t transport_media_create(pjmedia_transport *tp,
1134
 
                                          pj_pool_t *sdp_pool,
1135
 
                                          unsigned options,
1136
 
                                          const pjmedia_sdp_session *rem_sdp,
1137
 
                                          unsigned media_index)
1138
 
{
1139
 
    struct transport_ice *tp_ice = (struct transport_ice*)tp;
1140
 
    pj_ice_sess_role ice_role;
1141
 
    pj_status_t status;
1142
 
 
1143
 
    PJ_UNUSED_ARG(media_index);
1144
 
    PJ_UNUSED_ARG(sdp_pool);
1145
 
 
1146
 
    tp_ice->media_option = options;
1147
 
    tp_ice->oa_role = ROLE_NONE;
1148
 
    tp_ice->initial_sdp = PJ_TRUE;
1149
 
 
1150
 
    /* Init ICE, the initial role is set now based on availability of
1151
 
     * rem_sdp, but it will be checked again later.
1152
 
     */
1153
 
    ice_role = (rem_sdp==NULL ? PJ_ICE_SESS_ROLE_CONTROLLING : 
1154
 
                                PJ_ICE_SESS_ROLE_CONTROLLED);
1155
 
    status = pj_ice_strans_init_ice(tp_ice->ice_st, ice_role, NULL, NULL);
1156
 
 
1157
 
    /* Done */
1158
 
    return status;
1159
 
}
1160
 
 
1161
 
 
1162
 
static pj_status_t transport_encode_sdp(pjmedia_transport *tp,
1163
 
                                        pj_pool_t *sdp_pool,
1164
 
                                        pjmedia_sdp_session *sdp_local,
1165
 
                                        const pjmedia_sdp_session *rem_sdp,
1166
 
                                        unsigned media_index)
1167
 
{
1168
 
    struct transport_ice *tp_ice = (struct transport_ice*)tp;
1169
 
    pj_status_t status;
1170
 
 
1171
 
    /* Validate media transport */
1172
 
    /* This transport only support RTP/AVP transport, unless if
1173
 
     * transport checking is disabled
1174
 
     */
1175
 
    if ((tp_ice->media_option & PJMEDIA_TPMED_NO_TRANSPORT_CHECKING) == 0) {
1176
 
        pjmedia_sdp_media *loc_m, *rem_m;
1177
 
 
1178
 
        rem_m = rem_sdp? rem_sdp->media[media_index] : NULL;
1179
 
        loc_m = sdp_local->media[media_index];
1180
 
 
1181
 
        if (pj_stricmp(&loc_m->desc.transport, &STR_RTP_AVP) ||
1182
 
           (rem_m && pj_stricmp(&rem_m->desc.transport, &STR_RTP_AVP)))
1183
 
        {
1184
 
            pjmedia_sdp_media_deactivate(sdp_pool, loc_m);
1185
 
            return PJMEDIA_SDP_EINPROTO;
1186
 
        }
1187
 
    }
1188
 
 
1189
 
    if (tp_ice->initial_sdp) {
1190
 
        if (rem_sdp) {
1191
 
            status = create_initial_answer(tp_ice, sdp_pool, sdp_local, 
1192
 
                                           rem_sdp, media_index);
1193
 
        } else {
1194
 
            status = create_initial_offer(tp_ice, sdp_pool, sdp_local,
1195
 
                                          media_index);
1196
 
        }
1197
 
    } else {
1198
 
        if (rem_sdp) {
1199
 
            status = create_subsequent_answer(tp_ice, sdp_pool, sdp_local,
1200
 
                                              rem_sdp, media_index);
1201
 
        } else {
1202
 
            status = create_subsequent_offer(tp_ice, sdp_pool, sdp_local,
1203
 
                                             media_index);
1204
 
        }
1205
 
    }
1206
 
 
1207
 
    if (status==PJ_SUCCESS) {
1208
 
        if (rem_sdp)
1209
 
            tp_ice->oa_role = ROLE_ANSWERER;
1210
 
        else
1211
 
            tp_ice->oa_role = ROLE_OFFERER;
1212
 
    }
1213
 
 
1214
 
    return status;
1215
 
}
1216
 
 
1217
 
 
1218
 
/* Start ICE session with the specified remote SDP */
1219
 
static pj_status_t start_ice(struct transport_ice *tp_ice,
1220
 
                             pj_pool_t *tmp_pool,
1221
 
                             const pjmedia_sdp_session *rem_sdp,
1222
 
                             unsigned media_index)
1223
 
{
1224
 
    pjmedia_sdp_media *rem_m = rem_sdp->media[media_index];
1225
 
    const pjmedia_sdp_attr *ufrag_attr, *pwd_attr;
1226
 
    pj_ice_sess_cand *cand;
1227
 
    unsigned i, cand_cnt;
1228
 
    pj_status_t status;
1229
 
 
1230
 
    get_ice_attr(rem_sdp, rem_m, &ufrag_attr, &pwd_attr);
1231
 
 
1232
 
    /* Allocate candidate array */
1233
 
    cand = (pj_ice_sess_cand*)
1234
 
           pj_pool_calloc(tmp_pool, PJ_ICE_MAX_CAND, 
1235
 
                          sizeof(pj_ice_sess_cand));
1236
 
 
1237
 
    /* Get all candidates in the media */
1238
 
    cand_cnt = 0;
1239
 
    for (i=0; i<rem_m->attr_count && cand_cnt < PJ_ICE_MAX_CAND; ++i) {
1240
 
        pjmedia_sdp_attr *attr;
1241
 
 
1242
 
        attr = rem_m->attr[i];
1243
 
 
1244
 
        if (pj_strcmp(&attr->name, &STR_CANDIDATE)!=0)
1245
 
            continue;
1246
 
 
1247
 
        /* Parse candidate */
1248
 
        status = parse_cand(tp_ice->base.name, tmp_pool, &attr->value, 
1249
 
                            &cand[cand_cnt]);
1250
 
        if (status != PJ_SUCCESS) {
1251
 
            PJ_LOG(4,(tp_ice->base.name, 
1252
 
                      "Error in parsing SDP candidate attribute '%.*s', "
1253
 
                      "candidate is ignored",
1254
 
                      (int)attr->value.slen, attr->value.ptr));
1255
 
            continue;
1256
 
        }
1257
 
 
1258
 
        cand_cnt++;
1259
 
    }
1260
 
 
1261
 
    /* Start ICE */
1262
 
    return pj_ice_strans_start_ice(tp_ice->ice_st, &ufrag_attr->value, 
1263
 
                                   &pwd_attr->value, cand_cnt, cand);
1264
 
}
1265
 
 
1266
 
 
1267
 
/*
1268
 
 * Start ICE checks when both offer and answer have been negotiated
1269
 
 * by SDP negotiator.
1270
 
 */
1271
 
static pj_status_t transport_media_start(pjmedia_transport *tp,
1272
 
                                         pj_pool_t *tmp_pool,
1273
 
                                         const pjmedia_sdp_session *sdp_local,
1274
 
                                         const pjmedia_sdp_session *rem_sdp,
1275
 
                                         unsigned media_index)
1276
 
{
1277
 
    struct transport_ice *tp_ice = (struct transport_ice*)tp;
1278
 
    pjmedia_sdp_media *rem_m;
1279
 
    enum oa_role current_oa_role;
1280
 
    pj_bool_t initial_oa;
1281
 
    pj_status_t status;
1282
 
 
1283
 
    PJ_ASSERT_RETURN(tp && tmp_pool && rem_sdp, PJ_EINVAL);
1284
 
    PJ_ASSERT_RETURN(media_index < rem_sdp->media_count, PJ_EINVAL);
1285
 
 
1286
 
    rem_m = rem_sdp->media[media_index];
1287
 
 
1288
 
    initial_oa = tp_ice->initial_sdp;
1289
 
    current_oa_role = tp_ice->oa_role;
1290
 
 
1291
 
    /* SDP has been negotiated */
1292
 
    tp_ice->initial_sdp = PJ_FALSE;
1293
 
    tp_ice->oa_role = ROLE_NONE;
1294
 
 
1295
 
    /* Nothing to do if we don't have ICE session */
1296
 
    if (pj_ice_strans_has_sess(tp_ice->ice_st) == PJ_FALSE) {
1297
 
        return PJ_SUCCESS;
1298
 
    }
1299
 
 
1300
 
    /* Special case for Session Timer. The re-INVITE for session refresh
1301
 
     * doesn't call transport_encode_sdp(), causing current_oa_role to
1302
 
     * be set to ROLE_NONE. This is a workaround.
1303
 
     */
1304
 
    if (current_oa_role == ROLE_NONE) {
1305
 
        current_oa_role = ROLE_OFFERER;
1306
 
    }
1307
 
 
1308
 
    /* Processing depends on the offer/answer role */
1309
 
    if (current_oa_role == ROLE_OFFERER) {
1310
 
        /*
1311
 
         * We are offerer. So this will be the first time we see the
1312
 
         * remote's SDP.
1313
 
         */
1314
 
        struct sdp_state answer_state;
1315
 
 
1316
 
        /* Verify the answer */
1317
 
        status = verify_ice_sdp(tp_ice, tmp_pool, rem_sdp, media_index, 
1318
 
                                PJ_ICE_SESS_ROLE_CONTROLLING, &answer_state);
1319
 
        if (status != PJ_SUCCESS) {
1320
 
            /* Something wrong in the SDP answer */
1321
 
            set_no_ice(tp_ice, "Invalid remote SDP answer", status);
1322
 
            return status;
1323
 
        }
1324
 
 
1325
 
        /* Does it have ICE? */
1326
 
        if (answer_state.match_comp_cnt == 0) {
1327
 
            /* Remote doesn't support ICE */
1328
 
            set_no_ice(tp_ice, "Remote answer doesn't support ICE", 
1329
 
                       PJ_SUCCESS);
1330
 
            return PJ_SUCCESS;
1331
 
        }
1332
 
 
1333
 
        /* Check if remote has reported ice-mismatch */
1334
 
        if (pjmedia_sdp_attr_find(rem_m->attr_count, rem_m->attr, 
1335
 
                                  &STR_ICE_MISMATCH, NULL) != NULL)
1336
 
        {
1337
 
            /* Remote has reported ice-mismatch */
1338
 
            set_no_ice(tp_ice, 
1339
 
                       "Remote answer contains 'ice-mismatch' attribute", 
1340
 
                       PJ_SUCCESS);
1341
 
            return PJ_SUCCESS;
1342
 
        }
1343
 
 
1344
 
        /* Check if remote has indicated a restart */
1345
 
        if (answer_state.ice_restart) {
1346
 
            PJ_LOG(2,(tp_ice->base.name, 
1347
 
                      "Warning: remote has signalled ICE restart in SDP "
1348
 
                      "answer which is disallowed. Remote ICE negotiation"
1349
 
                      " may fail."));
1350
 
        }
1351
 
 
1352
 
        /* Check if the answer itself is mismatched */
1353
 
        if (answer_state.ice_mismatch) {
1354
 
            /* This happens either when a B2BUA modified remote answer but
1355
 
             * strangely didn't modify our offer, or remote is not capable
1356
 
             * of detecting mismatch in our offer (it didn't put 
1357
 
             * 'ice-mismatch' attribute in the answer).
1358
 
             */
1359
 
            PJ_LOG(2,(tp_ice->base.name, 
1360
 
                      "Warning: remote answer mismatch, but it does not "
1361
 
                      "reject our offer with 'ice-mismatch'. ICE negotiation "
1362
 
                      "may fail"));
1363
 
        }
1364
 
 
1365
 
        /* Do nothing if ICE is complete or running */
1366
 
        if (pj_ice_strans_sess_is_running(tp_ice->ice_st)) {
1367
 
            PJ_LOG(4,(tp_ice->base.name,
1368
 
                      "Ignored offer/answer because ICE is running"));
1369
 
            return PJ_SUCCESS;
1370
 
        }
1371
 
 
1372
 
        if (pj_ice_strans_sess_is_complete(tp_ice->ice_st)) {
1373
 
            PJ_LOG(4,(tp_ice->base.name, "ICE session unchanged"));
1374
 
            return PJ_SUCCESS;
1375
 
        }
1376
 
 
1377
 
        /* Start ICE */
1378
 
 
1379
 
    } else {
1380
 
        /*
1381
 
         * We are answerer. We've seen and negotiated remote's SDP
1382
 
         * before, and the result is in "rem_offer_state".
1383
 
         */
1384
 
        const pjmedia_sdp_attr *ufrag_attr, *pwd_attr;
1385
 
 
1386
 
        /* Check for ICE in remote offer */
1387
 
        if (tp_ice->rem_offer_state.match_comp_cnt == 0) {
1388
 
            /* No ICE attribute present */
1389
 
            set_no_ice(tp_ice, "Remote no longer offers ICE",
1390
 
                       PJ_SUCCESS);
1391
 
            return PJ_SUCCESS;
1392
 
        }
1393
 
 
1394
 
        /* Check for ICE ice_mismatch condition in the offer */
1395
 
        if (tp_ice->rem_offer_state.ice_mismatch) {
1396
 
            set_no_ice(tp_ice, "Remote offer mismatch: ", 
1397
 
                       PJNATH_EICEMISMATCH);
1398
 
            return PJ_SUCCESS;
1399
 
        }
1400
 
 
1401
 
        /* If ICE is complete and remote doesn't request restart,
1402
 
         * then leave the session as is.
1403
 
         */
1404
 
        if (!initial_oa && tp_ice->rem_offer_state.ice_restart == PJ_FALSE) {
1405
 
            /* Remote has not requested ICE restart, so session is
1406
 
             * unchanged.
1407
 
             */
1408
 
            PJ_LOG(4,(tp_ice->base.name, "ICE session unchanged"));
1409
 
            return PJ_SUCCESS;
1410
 
        }
1411
 
 
1412
 
        /* Either remote has requested ICE restart or this is our
1413
 
         * first answer. 
1414
 
         */
1415
 
 
1416
 
        /* Stop ICE */
1417
 
        if (!initial_oa) {
1418
 
            set_no_ice(tp_ice, "restarting by remote request..", PJ_SUCCESS);
1419
 
 
1420
 
            /* We have put new ICE ufrag and pwd in the answer. Now
1421
 
             * create a new ICE session with that ufrag/pwd pair.
1422
 
             */
1423
 
            get_ice_attr(sdp_local, sdp_local->media[media_index], 
1424
 
                         &ufrag_attr, &pwd_attr);
1425
 
            status = pj_ice_strans_init_ice(tp_ice->ice_st, 
1426
 
                                            tp_ice->rem_offer_state.local_role,
1427
 
                                            &ufrag_attr->value, 
1428
 
                                            &pwd_attr->value);
1429
 
            if (status != PJ_SUCCESS) {
1430
 
                PJ_LOG(1,(tp_ice->base.name, 
1431
 
                          "ICE re-initialization failed (status=%d)!",
1432
 
                          status));
1433
 
                return status;
1434
 
            }
1435
 
        }
1436
 
 
1437
 
        /* Ticket #977: Update role if turns out we're supposed to be the 
1438
 
         * Controlling agent (e.g. when talking to ice-lite peer). 
1439
 
         */
1440
 
        if (tp_ice->rem_offer_state.local_role==PJ_ICE_SESS_ROLE_CONTROLLING &&
1441
 
            pj_ice_strans_has_sess(tp_ice->ice_st)) 
1442
 
        {
1443
 
            pj_ice_strans_change_role(tp_ice->ice_st, 
1444
 
                                      PJ_ICE_SESS_ROLE_CONTROLLING);
1445
 
        }
1446
 
 
1447
 
 
1448
 
        /* start ICE */
1449
 
    }
1450
 
 
1451
 
    /* Now start ICE */
1452
 
    status = start_ice(tp_ice, tmp_pool, rem_sdp, media_index);
1453
 
    if (status != PJ_SUCCESS) {
1454
 
        PJ_LOG(1,(tp_ice->base.name, 
1455
 
                  "ICE restart failed (status=%d)!",
1456
 
                  status));
1457
 
        return status;
1458
 
    }
1459
 
 
1460
 
    /* Done */
1461
 
    tp_ice->use_ice = PJ_TRUE;
1462
 
 
1463
 
    return PJ_SUCCESS;
1464
 
}
1465
 
 
1466
 
 
1467
 
static pj_status_t transport_media_stop(pjmedia_transport *tp)
1468
 
{
1469
 
    struct transport_ice *tp_ice = (struct transport_ice*)tp;
1470
 
    
1471
 
    set_no_ice(tp_ice, "media stop requested", PJ_SUCCESS);
1472
 
 
1473
 
    return PJ_SUCCESS;
1474
 
}
1475
 
 
1476
 
 
1477
 
static pj_status_t transport_get_info(pjmedia_transport *tp,
1478
 
                                      pjmedia_transport_info *info)
1479
 
{
1480
 
    struct transport_ice *tp_ice = (struct transport_ice*)tp;
1481
 
    pj_ice_sess_cand cand;
1482
 
    pj_status_t status;
1483
 
 
1484
 
    pj_bzero(&info->sock_info, sizeof(info->sock_info));
1485
 
    info->sock_info.rtp_sock = info->sock_info.rtcp_sock = PJ_INVALID_SOCKET;
1486
 
 
1487
 
    /* Get RTP default address */
1488
 
    status = pj_ice_strans_get_def_cand(tp_ice->ice_st, 1, &cand);
1489
 
    if (status != PJ_SUCCESS)
1490
 
        return status;
1491
 
 
1492
 
    pj_sockaddr_cp(&info->sock_info.rtp_addr_name, &cand.addr);
1493
 
 
1494
 
    /* Get RTCP default address */
1495
 
    if (tp_ice->comp_cnt > 1) {
1496
 
        status = pj_ice_strans_get_def_cand(tp_ice->ice_st, 2, &cand);
1497
 
        if (status != PJ_SUCCESS)
1498
 
            return status;
1499
 
 
1500
 
        pj_sockaddr_cp(&info->sock_info.rtcp_addr_name, &cand.addr);
1501
 
    }
1502
 
 
1503
 
    /* Set remote address originating RTP & RTCP if this transport has 
1504
 
     * ICE activated or received any packets.
1505
 
     */
1506
 
    if (tp_ice->use_ice || tp_ice->rtp_src_cnt) {
1507
 
        info->src_rtp_name  = tp_ice->rtp_src_addr;
1508
 
    }
1509
 
    if (tp_ice->use_ice || tp_ice->rtcp_src_cnt) {
1510
 
        info->src_rtcp_name = tp_ice->rtcp_src_addr;
1511
 
    }
1512
 
 
1513
 
    /* Fill up transport specific info */
1514
 
    if (info->specific_info_cnt < PJ_ARRAY_SIZE(info->spc_info)) {
1515
 
        pjmedia_transport_specific_info *tsi;
1516
 
        pjmedia_ice_transport_info *ii;
1517
 
        unsigned i;
1518
 
 
1519
 
        pj_assert(sizeof(*ii) <= sizeof(tsi->buffer));
1520
 
        tsi = &info->spc_info[info->specific_info_cnt++];
1521
 
        tsi->type = PJMEDIA_TRANSPORT_TYPE_ICE;
1522
 
        tsi->cbsize = sizeof(*ii);
1523
 
 
1524
 
        ii = (pjmedia_ice_transport_info*) tsi->buffer;
1525
 
        pj_bzero(ii, sizeof(*ii));
1526
 
 
1527
 
        if (pj_ice_strans_has_sess(tp_ice->ice_st))
1528
 
            ii->role = pj_ice_strans_get_role(tp_ice->ice_st);
1529
 
        else
1530
 
            ii->role = PJ_ICE_SESS_ROLE_UNKNOWN;
1531
 
        ii->sess_state = pj_ice_strans_get_state(tp_ice->ice_st);
1532
 
        ii->comp_cnt = pj_ice_strans_get_running_comp_cnt(tp_ice->ice_st);
1533
 
        
1534
 
        for (i=1; i<=ii->comp_cnt && i<=PJ_ARRAY_SIZE(ii->comp); ++i) {
1535
 
            const pj_ice_sess_check *chk;
1536
 
 
1537
 
            chk = pj_ice_strans_get_valid_pair(tp_ice->ice_st, i);
1538
 
            if (chk) {
1539
 
                ii->comp[i-1].lcand_type = chk->lcand->type;
1540
 
                ii->comp[i-1].rcand_type = chk->rcand->type;
1541
 
            }
1542
 
        }
1543
 
    }
1544
 
 
1545
 
    return PJ_SUCCESS;
1546
 
}
1547
 
 
1548
 
 
1549
 
static pj_status_t transport_attach  (pjmedia_transport *tp,
1550
 
                                      void *stream,
1551
 
                                      const pj_sockaddr_t *rem_addr,
1552
 
                                      const pj_sockaddr_t *rem_rtcp,
1553
 
                                      unsigned addr_len,
1554
 
                                      void (*rtp_cb)(void*,
1555
 
                                                     void*,
1556
 
                                                     pj_ssize_t),
1557
 
                                      void (*rtcp_cb)(void*,
1558
 
                                                      void*,
1559
 
                                                      pj_ssize_t))
1560
 
{
1561
 
    struct transport_ice *tp_ice = (struct transport_ice*)tp;
1562
 
 
1563
 
    tp_ice->stream = stream;
1564
 
    tp_ice->rtp_cb = rtp_cb;
1565
 
    tp_ice->rtcp_cb = rtcp_cb;
1566
 
 
1567
 
    pj_memcpy(&tp_ice->remote_rtp, rem_addr, addr_len);
1568
 
    pj_memcpy(&tp_ice->remote_rtcp, rem_rtcp, addr_len);
1569
 
    tp_ice->addr_len = addr_len;
1570
 
 
1571
 
    /* Init source RTP & RTCP addresses and counter */
1572
 
    tp_ice->rtp_src_addr = tp_ice->remote_rtp;
1573
 
    tp_ice->rtcp_src_addr = tp_ice->remote_rtcp;
1574
 
    tp_ice->rtp_src_cnt = 0;
1575
 
    tp_ice->rtcp_src_cnt = 0;
1576
 
 
1577
 
    return PJ_SUCCESS;
1578
 
}
1579
 
 
1580
 
 
1581
 
static void transport_detach(pjmedia_transport *tp,
1582
 
                             void *strm)
1583
 
{
1584
 
    struct transport_ice *tp_ice = (struct transport_ice*)tp;
1585
 
 
1586
 
    /* TODO: need to solve ticket #460 here */
1587
 
 
1588
 
    tp_ice->rtp_cb = NULL;
1589
 
    tp_ice->rtcp_cb = NULL;
1590
 
    tp_ice->stream = NULL;
1591
 
 
1592
 
    PJ_UNUSED_ARG(strm);
1593
 
}
1594
 
 
1595
 
 
1596
 
static pj_status_t transport_send_rtp(pjmedia_transport *tp,
1597
 
                                      const void *pkt,
1598
 
                                      pj_size_t size)
1599
 
{
1600
 
    struct transport_ice *tp_ice = (struct transport_ice*)tp;
1601
 
 
1602
 
    /* Simulate packet lost on TX direction */
1603
 
    if (tp_ice->tx_drop_pct) {
1604
 
        if ((pj_rand() % 100) <= (int)tp_ice->tx_drop_pct) {
1605
 
            PJ_LOG(5,(tp_ice->base.name, 
1606
 
                      "TX RTP packet dropped because of pkt lost "
1607
 
                      "simulation"));
1608
 
            return PJ_SUCCESS;
1609
 
        }
1610
 
    }
1611
 
 
1612
 
    return pj_ice_strans_sendto(tp_ice->ice_st, 1, 
1613
 
                                pkt, size, &tp_ice->remote_rtp,
1614
 
                                tp_ice->addr_len);
1615
 
}
1616
 
 
1617
 
 
1618
 
static pj_status_t transport_send_rtcp(pjmedia_transport *tp,
1619
 
                                       const void *pkt,
1620
 
                                       pj_size_t size)
1621
 
{
1622
 
    return transport_send_rtcp2(tp, NULL, 0, pkt, size);
1623
 
}
1624
 
 
1625
 
static pj_status_t transport_send_rtcp2(pjmedia_transport *tp,
1626
 
                                        const pj_sockaddr_t *addr,
1627
 
                                        unsigned addr_len,
1628
 
                                        const void *pkt,
1629
 
                                        pj_size_t size)
1630
 
{
1631
 
    struct transport_ice *tp_ice = (struct transport_ice*)tp;
1632
 
    if (tp_ice->comp_cnt > 1) {
1633
 
        if (addr == NULL) {
1634
 
            addr = &tp_ice->remote_rtcp;
1635
 
            addr_len = pj_sockaddr_get_len(addr);
1636
 
        }
1637
 
        return pj_ice_strans_sendto(tp_ice->ice_st, 2, pkt, size, 
1638
 
                                    addr, addr_len);
1639
 
    } else {
1640
 
        return PJ_SUCCESS;
1641
 
    }
1642
 
}
1643
 
 
1644
 
 
1645
 
static void ice_on_rx_data(pj_ice_strans *ice_st, unsigned comp_id, 
1646
 
                           void *pkt, pj_size_t size,
1647
 
                           const pj_sockaddr_t *src_addr,
1648
 
                           unsigned src_addr_len)
1649
 
{
1650
 
    struct transport_ice *tp_ice;
1651
 
    pj_bool_t discard = PJ_FALSE;
1652
 
 
1653
 
    tp_ice = (struct transport_ice*) pj_ice_strans_get_user_data(ice_st);
1654
 
 
1655
 
    if (comp_id==1 && tp_ice->rtp_cb) {
1656
 
 
1657
 
        /* Simulate packet lost on RX direction */
1658
 
        if (tp_ice->rx_drop_pct) {
1659
 
            if ((pj_rand() % 100) <= (int)tp_ice->rx_drop_pct) {
1660
 
                PJ_LOG(5,(tp_ice->base.name, 
1661
 
                          "RX RTP packet dropped because of pkt lost "
1662
 
                          "simulation"));
1663
 
                return;
1664
 
            }
1665
 
        }
1666
 
 
1667
 
        /* See if source address of RTP packet is different than the 
1668
 
         * configured address, and switch RTP remote address to 
1669
 
         * source packet address after several consecutive packets
1670
 
         * have been received.
1671
 
         */
1672
 
        if (!tp_ice->use_ice) {
1673
 
            pj_bool_t enable_switch =
1674
 
                    ((tp_ice->options & PJMEDIA_ICE_NO_SRC_ADDR_CHECKING)==0);
1675
 
 
1676
 
            if (!enable_switch ||
1677
 
                pj_sockaddr_cmp(&tp_ice->remote_rtp, src_addr) == 0)
1678
 
            {
1679
 
                /* Don't switch while we're receiving from remote_rtp */
1680
 
                tp_ice->rtp_src_cnt = 0;
1681
 
            } else {
1682
 
 
1683
 
                ++tp_ice->rtp_src_cnt;
1684
 
 
1685
 
                /* Check if the source address is recognized. */
1686
 
                if (pj_sockaddr_cmp(src_addr, &tp_ice->rtp_src_addr) != 0) {
1687
 
                    /* Remember the new source address. */
1688
 
                    pj_sockaddr_cp(&tp_ice->rtp_src_addr, src_addr);
1689
 
                    /* Reset counter */
1690
 
                    tp_ice->rtp_src_cnt = 0;
1691
 
                    discard = PJ_TRUE;
1692
 
                }
1693
 
 
1694
 
                if (tp_ice->rtp_src_cnt < PJMEDIA_RTP_NAT_PROBATION_CNT) {
1695
 
                    discard = PJ_TRUE;
1696
 
                } else {
1697
 
                    char addr_text[80];
1698
 
 
1699
 
                    /* Set remote RTP address to source address */
1700
 
                    pj_sockaddr_cp(&tp_ice->remote_rtp, &tp_ice->rtp_src_addr);
1701
 
                    tp_ice->addr_len = pj_sockaddr_get_len(&tp_ice->remote_rtp);
1702
 
 
1703
 
                    /* Reset counter */
1704
 
                    tp_ice->rtp_src_cnt = 0;
1705
 
 
1706
 
                    PJ_LOG(4,(tp_ice->base.name,
1707
 
                              "Remote RTP address switched to %s",
1708
 
                              pj_sockaddr_print(&tp_ice->remote_rtp, addr_text,
1709
 
                                                sizeof(addr_text), 3)));
1710
 
 
1711
 
                    /* Also update remote RTCP address if actual RTCP source
1712
 
                     * address is not heard yet.
1713
 
                     */
1714
 
                    if (!pj_sockaddr_has_addr(&tp_ice->rtcp_src_addr)) {
1715
 
                        pj_uint16_t port;
1716
 
 
1717
 
                        pj_sockaddr_cp(&tp_ice->remote_rtcp, 
1718
 
                                       &tp_ice->remote_rtp);
1719
 
 
1720
 
                        port = (pj_uint16_t)
1721
 
                               (pj_sockaddr_get_port(&tp_ice->remote_rtp)+1);
1722
 
                        pj_sockaddr_set_port(&tp_ice->remote_rtcp, port);
1723
 
 
1724
 
                        PJ_LOG(4,(tp_ice->base.name,
1725
 
                                  "Remote RTCP address switched to predicted "
1726
 
                                  "address %s",
1727
 
                                  pj_sockaddr_print(&tp_ice->remote_rtcp, 
1728
 
                                                    addr_text,
1729
 
                                                    sizeof(addr_text), 3)));
1730
 
                    }
1731
 
                }
1732
 
            }
1733
 
        }
1734
 
 
1735
 
        if (!discard)
1736
 
            (*tp_ice->rtp_cb)(tp_ice->stream, pkt, size);
1737
 
 
1738
 
    } else if (comp_id==2 && tp_ice->rtcp_cb) {
1739
 
 
1740
 
        /* Check if RTCP source address is the same as the configured
1741
 
         * remote address, and switch the address when they are
1742
 
         * different.
1743
 
         */
1744
 
        if (!tp_ice->use_ice &&
1745
 
            (tp_ice->options & PJMEDIA_ICE_NO_SRC_ADDR_CHECKING)==0)
1746
 
        {
1747
 
            if (pj_sockaddr_cmp(&tp_ice->remote_rtcp, src_addr) == 0) {
1748
 
                tp_ice->rtcp_src_cnt = 0;
1749
 
            } else {
1750
 
                char addr_text[80];
1751
 
 
1752
 
                ++tp_ice->rtcp_src_cnt;
1753
 
                if (tp_ice->rtcp_src_cnt < PJMEDIA_RTCP_NAT_PROBATION_CNT) {
1754
 
                    discard = PJ_TRUE;
1755
 
                } else {
1756
 
                    tp_ice->rtcp_src_cnt = 0;
1757
 
                    pj_sockaddr_cp(&tp_ice->rtcp_src_addr, src_addr);
1758
 
                    pj_sockaddr_cp(&tp_ice->remote_rtcp, src_addr);
1759
 
 
1760
 
                    pj_assert(tp_ice->addr_len==pj_sockaddr_get_len(src_addr));
1761
 
 
1762
 
                    PJ_LOG(4,(tp_ice->base.name,
1763
 
                              "Remote RTCP address switched to %s",
1764
 
                              pj_sockaddr_print(&tp_ice->remote_rtcp,
1765
 
                                                addr_text, sizeof(addr_text),
1766
 
                                                3)));
1767
 
                }
1768
 
            }
1769
 
        }
1770
 
 
1771
 
        if (!discard)
1772
 
            (*tp_ice->rtcp_cb)(tp_ice->stream, pkt, size);
1773
 
    }
1774
 
 
1775
 
    PJ_UNUSED_ARG(src_addr_len);
1776
 
}
1777
 
 
1778
 
 
1779
 
static void ice_on_ice_complete(pj_ice_strans *ice_st, 
1780
 
                                pj_ice_strans_op op,
1781
 
                                pj_status_t result)
1782
 
{
1783
 
    struct transport_ice *tp_ice;
1784
 
 
1785
 
    tp_ice = (struct transport_ice*) pj_ice_strans_get_user_data(ice_st);
1786
 
 
1787
 
    /* Notify application */
1788
 
    if (tp_ice->cb.on_ice_complete)
1789
 
        (*tp_ice->cb.on_ice_complete)(&tp_ice->base, op, result);
1790
 
}
1791
 
 
1792
 
 
1793
 
/* Simulate lost */
1794
 
static pj_status_t transport_simulate_lost(pjmedia_transport *tp,
1795
 
                                           pjmedia_dir dir,
1796
 
                                           unsigned pct_lost)
1797
 
{
1798
 
    struct transport_ice *ice = (struct transport_ice*) tp;
1799
 
 
1800
 
    PJ_ASSERT_RETURN(tp && pct_lost <= 100, PJ_EINVAL);
1801
 
 
1802
 
    if (dir & PJMEDIA_DIR_ENCODING)
1803
 
        ice->tx_drop_pct = pct_lost;
1804
 
 
1805
 
    if (dir & PJMEDIA_DIR_DECODING)
1806
 
        ice->rx_drop_pct = pct_lost;
1807
 
 
1808
 
    return PJ_SUCCESS;
1809
 
}
1810
 
 
1811
 
 
1812
 
/*
1813
 
 * Destroy ICE media transport.
1814
 
 */
1815
 
static pj_status_t transport_destroy(pjmedia_transport *tp)
1816
 
{
1817
 
    struct transport_ice *tp_ice = (struct transport_ice*)tp;
1818
 
 
1819
 
    if (tp_ice->ice_st) {
1820
 
        pj_ice_strans_destroy(tp_ice->ice_st);
1821
 
        tp_ice->ice_st = NULL;
1822
 
    }
1823
 
 
1824
 
    if (tp_ice->pool) {
1825
 
        pj_pool_t *pool = tp_ice->pool;
1826
 
        tp_ice->pool = NULL;
1827
 
        pj_pool_release(pool);
1828
 
    }
1829
 
 
1830
 
    return PJ_SUCCESS;
1831
 
}
1832