1
/* $Id: transport_ice.c 3906 2011-12-09 07:19:25Z bennylp $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#include <pjmedia/transport_ice.h>
21
#include <pjnath/errno.h>
22
#include <pj/assert.h>
27
#define THIS_FILE "transport_ice.c"
29
# define TRACE__(expr) PJ_LOG(5,expr)
31
# define TRACE__(expr)
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 */
51
pjmedia_transport base;
54
unsigned options; /**< Transport options. */
57
pj_ice_strans *ice_st;
60
unsigned media_option;
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 */
67
pj_sockaddr remote_rtp;
68
pj_sockaddr remote_rtcp;
69
unsigned addr_len; /**< Length of addresses. */
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. */
77
unsigned tx_drop_pct; /**< Percent of tx pkts to drop. */
78
unsigned rx_drop_pct; /**< Percent of rx pkts to drop. */
83
void (*rtcp_cb)(void*,
90
* These are media transport operations.
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,
96
const pj_sockaddr_t *rem_addr,
97
const pj_sockaddr_t *rem_rtcp,
102
void (*rtcp_cb)(void*,
105
static void transport_detach (pjmedia_transport *tp,
107
static pj_status_t transport_send_rtp( pjmedia_transport *tp,
110
static pj_status_t transport_send_rtcp(pjmedia_transport *tp,
113
static pj_status_t transport_send_rtcp2(pjmedia_transport *tp,
114
const pj_sockaddr_t *addr,
118
static pj_status_t transport_media_create(pjmedia_transport *tp,
121
const pjmedia_sdp_session *rem_sdp,
122
unsigned media_index);
123
static pj_status_t transport_encode_sdp(pjmedia_transport *tp,
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,
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,
137
static pj_status_t transport_destroy (pjmedia_transport *tp);
140
* And these are ICE callbacks.
142
static void ice_on_rx_data(pj_ice_strans *ice_st,
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,
152
static pjmedia_transport_op transport_ice_op =
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,
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 };
187
* Create ICE media transport.
189
PJ_DEF(pj_status_t) pjmedia_ice_create(pjmedia_endpt *endpt,
192
const pj_ice_strans_cfg *cfg,
193
const pjmedia_ice_cb *cb,
194
pjmedia_transport **p_tp)
196
return pjmedia_ice_create2(endpt, name, comp_cnt, cfg, cb, 0, p_tp);
200
* Create ICE media transport.
202
PJ_DEF(pj_status_t) pjmedia_ice_create2(pjmedia_endpt *endpt,
205
const pj_ice_strans_cfg *cfg,
206
const pjmedia_ice_cb *cb,
208
pjmedia_transport **p_tp)
211
pj_ice_strans_cb ice_st_cb;
212
struct transport_ice *tp_ice;
215
PJ_ASSERT_RETURN(endpt && comp_cnt && cfg && p_tp, PJ_EINVAL);
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);
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;
232
pj_memcpy(&tp_ice->cb, cb, sizeof(pjmedia_ice_cb));
234
/* Assign return value first because ICE might call callback
237
*p_tp = &tp_ice->base;
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;
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);
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,
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));
267
PJ_LOG(4,(tp_ice->base.name,
268
"Stopping ICE, reason=%s", reason));
271
pj_ice_strans_stop_ice(tp_ice->ice_st);
273
tp_ice->use_ice = PJ_FALSE;
277
/* Create SDP candidate attribute */
278
static int print_sdp_cand_attr(char *buffer, int max_len,
279
const pj_ice_sess_cand *cand)
281
char ipaddr[PJ_INET6_ADDRSTRLEN+2];
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,
290
pj_sockaddr_print(&cand->addr, ipaddr,
292
(unsigned)pj_sockaddr_get_port(&cand->addr));
293
if (len < 1 || len >= max_len)
296
switch (cand->type) {
297
case PJ_ICE_CAND_TYPE_HOST:
298
len2 = pj_ansi_snprintf(buffer+len, max_len-len, "host");
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,
308
(int)pj_sockaddr_get_port(&cand->rel_addr));
311
pj_assert(!"Invalid candidate type");
315
if (len2 < 1 || len2 >= max_len)
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)
328
pjmedia_sdp_attr *attr;
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);
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);
340
/* Find ice-pwd attribute in media descriptor */
341
attr = pjmedia_sdp_attr_find(rem_m->attr_count, rem_m->attr,
344
/* Find ice-pwd attribute in session descriptor */
345
attr = pjmedia_sdp_attr_find(rem_sdp->attr_count, rem_sdp->attr,
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)
357
pjmedia_sdp_attr *attr;
358
pjmedia_sdp_media *m = sdp_local->media[media_index];
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);
367
/* Encode ICE information in SDP */
368
static pj_status_t encode_session_in_sdp(struct transport_ice *tp_ice,
370
pjmedia_sdp_session *sdp_local,
371
unsigned media_index,
373
pj_bool_t restart_session)
376
ATTR_BUF_LEN = 160, /* Max len of a=candidate attr */
377
RATTR_BUF_LEN= 160 /* Max len of a=remote-candidates attr */
379
pjmedia_sdp_media *m = sdp_local->media[media_index];
380
pj_str_t local_ufrag, local_pwd;
381
pjmedia_sdp_attr *attr;
384
/* Must have a session */
385
PJ_ASSERT_RETURN(pj_ice_strans_has_sess(tp_ice->ice_st), PJ_EBUG);
387
/* Get ufrag and pwd from current session */
388
pj_ice_strans_get_ufrag_pwd(tp_ice->ice_st, &local_ufrag, &local_pwd,
391
/* The listing of candidates depends on whether ICE has completed
392
* or not. When ICE has completed:
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.
399
* When ICE has not completed, we shall include all candidates.
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.
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)
408
const pj_ice_sess_check *check;
410
pjmedia_sdp_conn *conn;
411
pjmedia_sdp_attr *a_rtcp;
415
/* Encode ice-ufrag attribute */
416
attr = pjmedia_sdp_attr_create(sdp_pool, STR_ICE_UFRAG.ptr,
418
pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
420
/* Encode ice-pwd attribute */
421
attr = pjmedia_sdp_attr_create(sdp_pool, STR_ICE_PWD.ptr,
423
pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
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);
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
437
check = pj_ice_strans_get_valid_pair(tp_ice->ice_st, 1);
439
pj_assert(!"Shouldn't happen");
443
/* Override connection line address and media port number */
446
conn = sdp_local->conn;
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);
455
/* Override address RTCP attribute if it's present */
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)
462
pjmedia_sdp_attr_remove(&m->attr_count, m->attr, a_rtcp);
464
a_rtcp = pjmedia_sdp_attr_create_rtcp(sdp_pool,
465
&check->lcand->addr);
467
pjmedia_sdp_attr_add(&m->attr_count, m->attr, a_rtcp);
470
/* Encode only candidates matching the default destination
473
for (comp=0; comp < comp_cnt; ++comp) {
477
/* Get valid pair for this component */
478
check = pj_ice_strans_get_valid_pair(tp_ice->ice_st, comp+1);
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,
486
if (value.slen < 0) {
487
pj_assert(!"Not enough attr_buf to print candidate");
491
attr = pjmedia_sdp_attr_create(sdp_pool, STR_CANDIDATE.ptr,
493
pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
495
/* Append to a=remote-candidates attribute */
496
if (pj_ice_strans_get_role(tp_ice->ice_st) ==
497
PJ_ICE_SESS_ROLE_CONTROLLING)
499
char rem_addr[PJ_INET6_ADDRSTRLEN];
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,
507
(rem_cand.slen==0? "" : " "),
509
pj_sockaddr_get_port(&check->rcand->addr)
511
if (len < 1 || len >= RATTR_BUF_LEN) {
512
pj_assert(!"Not enough buffer to print "
513
"remote-candidates");
517
rem_cand.slen += len;
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
529
if (pj_ice_strans_get_role(tp_ice->ice_st) ==
530
PJ_ICE_SESS_ROLE_CONTROLLING)
532
attr = pjmedia_sdp_attr_create(sdp_pool, STR_REM_CAND.ptr,
534
pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
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)
541
/* Encode all candidates to SDP media */
545
/* If ICE is not restarted, encode current ICE ufrag/pwd.
546
* Otherwise generate new one.
548
if (!restart_session) {
549
attr = pjmedia_sdp_attr_create(sdp_pool, STR_ICE_UFRAG.ptr,
551
pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
553
attr = pjmedia_sdp_attr_create(sdp_pool, STR_ICE_PWD.ptr,
555
pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
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);
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);
572
/* Create buffer to encode candidates as SDP attribute */
573
attr_buf = (char*) pj_pool_alloc(sdp_pool, ATTR_BUF_LEN);
575
for (comp=0; comp < comp_cnt; ++comp) {
577
pj_ice_sess_cand cand[PJ_ICE_ST_MAX_CAND];
580
cand_cnt = PJ_ARRAY_SIZE(cand);
581
status = pj_ice_strans_enum_cands(tp_ice->ice_st, comp+1,
583
if (status != PJ_SUCCESS)
586
for (i=0; i<cand_cnt; ++i) {
589
value.slen = print_sdp_cand_attr(attr_buf, ATTR_BUF_LEN,
591
if (value.slen < 0) {
592
pj_assert(!"Not enough attr_buf to print candidate");
596
value.ptr = attr_buf;
597
attr = pjmedia_sdp_attr_create(sdp_pool,
600
pjmedia_sdp_attr_add(&m->attr_count, m->attr, attr);
604
/* ICE has failed, application should have terminated this call */
607
/* Removing a=rtcp line when there is only one component. */
609
attr = pjmedia_sdp_attr_find(m->attr_count, m->attr, &STR_RTCP, NULL);
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,
617
pj_memcpy(&m->bandw[m->bandw_count]->modifier, &STR_BANDW_RS,
620
m->bandw[m->bandw_count] = PJ_POOL_ZALLOC_T(sdp_pool,
622
pj_memcpy(&m->bandw[m->bandw_count]->modifier, &STR_BANDW_RR,
633
/* Parse a=candidate line */
634
static pj_status_t parse_cand(const char *obj_name,
636
const pj_str_t *orig_input,
637
pj_ice_sess_cand *cand)
643
pj_status_t status = PJNATH_EICEINCANDSDP;
645
pj_bzero(cand, sizeof(*cand));
646
pj_strdup_with_null(pool, &input, orig_input);
648
PJ_UNUSED_ARG(obj_name);
651
token = strtok(input.ptr, " ");
653
TRACE__((obj_name, "Expecting ICE foundation in candidate"));
656
pj_strdup2(pool, &cand->foundation, token);
659
token = strtok(NULL, " ");
661
TRACE__((obj_name, "Expecting ICE component ID in candidate"));
664
cand->comp_id = (pj_uint8_t) atoi(token);
667
token = strtok(NULL, " ");
669
TRACE__((obj_name, "Expecting ICE transport in candidate"));
672
if (pj_ansi_stricmp(token, "UDP") != 0) {
674
"Expecting ICE UDP transport only in candidate"));
679
token = strtok(NULL, " ");
681
TRACE__((obj_name, "Expecting ICE priority in candidate"));
684
cand->prio = atoi(token);
687
host = strtok(NULL, " ");
689
TRACE__((obj_name, "Expecting ICE host in candidate"));
692
/* Detect address family */
693
if (pj_ansi_strchr(host, ':'))
698
if (pj_sockaddr_init(af, &cand->addr, pj_cstr(&s, host), 0)) {
699
TRACE__((obj_name, "Invalid ICE candidate address"));
704
token = strtok(NULL, " ");
706
TRACE__((obj_name, "Expecting ICE port number in candidate"));
709
pj_sockaddr_set_port(&cand->addr, (pj_uint16_t)atoi(token));
712
token = strtok(NULL, " ");
714
TRACE__((obj_name, "Expecting ICE \"typ\" in candidate"));
717
if (pj_ansi_stricmp(token, "typ") != 0) {
718
TRACE__((obj_name, "Expecting ICE \"typ\" in candidate"));
723
token = strtok(NULL, " ");
725
TRACE__((obj_name, "Expecting ICE candidate type in candidate"));
729
if (pj_ansi_stricmp(token, "host") == 0) {
730
cand->type = PJ_ICE_CAND_TYPE_HOST;
732
} else if (pj_ansi_stricmp(token, "srflx") == 0) {
733
cand->type = PJ_ICE_CAND_TYPE_SRFLX;
735
} else if (pj_ansi_stricmp(token, "relay") == 0) {
736
cand->type = PJ_ICE_CAND_TYPE_RELAYED;
738
} else if (pj_ansi_stricmp(token, "prflx") == 0) {
739
cand->type = PJ_ICE_CAND_TYPE_PRFLX;
742
PJ_LOG(5,(obj_name, "Invalid ICE candidate type %s in candidate",
754
/* Create initial SDP offer */
755
static pj_status_t create_initial_offer(struct transport_ice *tp_ice,
757
pjmedia_sdp_session *loc_sdp,
758
unsigned media_index)
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);
774
/* Verify incoming offer */
775
static pj_status_t verify_ice_sdp(struct transport_ice *tp_ice,
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)
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;
790
rem_m = rem_sdp->media[media_index];
792
/* Get the "ice-ufrag" and "ice-pwd" attributes */
793
get_ice_attr(rem_sdp, rem_m, &ufrag_attr, &pwd_attr);
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;
801
/* Verify that default target for each component matches one of the
802
* candidate for the component. Otherwise stop ICE with ICE ice_mismatch
806
/* Component 1 is the c= line */
807
rem_conn = rem_m->conn;
808
if (rem_conn == NULL)
809
rem_conn = rem_sdp->conn;
811
return PJMEDIA_SDP_EMISSINGCONN;
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))
819
return PJMEDIA_SDP_ETPORTNOTEQUAL;
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)
828
if (tp_ice->comp_cnt > 1) {
829
const pjmedia_sdp_attr *attr;
831
/* Get default RTCP candidate from a=rtcp line, if present, otherwise
832
* calculate default RTCP candidate from default RTP target.
834
attr = pjmedia_sdp_attr_find(rem_m->attr_count, rem_m->attr,
836
has_rtcp = (attr != NULL);
839
pjmedia_sdp_rtcp_attr rtcp_attr;
841
status = pjmedia_sdp_attr_get_rtcp(attr, &rtcp_attr);
842
if (status != PJ_SUCCESS) {
843
/* Error parsing a=rtcp attribute */
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))
854
return PJMEDIA_SDP_ETPORTNOTEQUAL;
857
/* Assign RTCP address */
858
status = pj_sockaddr_init(tp_ice->af, &rtcp_addr,
860
(pj_uint16_t)rtcp_attr.port);
861
if (status != PJ_SUCCESS) {
862
return PJMEDIA_SDP_EINRTCP;
865
/* Assign RTCP address */
866
status = pj_sockaddr_init(tp_ice->af, &rtcp_addr,
868
(pj_uint16_t)rtcp_attr.port);
869
if (status != PJ_SUCCESS) {
870
return PJMEDIA_SDP_EINRTCP;
872
pj_sockaddr_copy_addr(&rtcp_addr, &rem_conn_addr);
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);
883
/* Find the default addresses in a=candidate attributes.
885
for (i=0; i<rem_m->attr_count; ++i) {
886
pj_ice_sess_cand cand;
888
if (pj_strcmp(&rem_m->attr[i]->name, &STR_CANDIDATE)!=0)
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));
902
if (!comp1_found && cand.comp_id==COMP_RTP &&
903
pj_sockaddr_cmp(&rem_conn_addr, &cand.addr)==0)
905
comp1_found = PJ_TRUE;
906
} else if (!comp2_found && cand.comp_id==COMP_RTCP &&
907
pj_sockaddr_cmp(&rtcp_addr, &cand.addr)==0)
909
comp2_found = PJ_TRUE;
912
if (cand.comp_id == COMP_RTCP)
915
if (comp1_found && (comp2_found || tp_ice->comp_cnt==1))
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;
927
sdp_state->match_comp_cnt = (tp_ice->comp_cnt > 1 && has_rtcp)? 2 : 1;
928
sdp_state->ice_mismatch = PJ_TRUE;
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)))
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))
943
/* Remote offers to restart ICE */
944
sdp_state->ice_restart = PJ_TRUE;
946
sdp_state->ice_restart = PJ_FALSE;
949
sdp_state->ice_restart = PJ_FALSE;
952
/* Detect our role */
953
if (current_ice_role==PJ_ICE_SESS_ROLE_CONTROLLING) {
954
sdp_state->local_role = PJ_ICE_SESS_ROLE_CONTROLLING;
956
if (pjmedia_sdp_attr_find(rem_sdp->attr_count, rem_sdp->attr,
957
&STR_ICE_LITE, NULL) != NULL)
959
/* Remote is ICE Lite */
960
sdp_state->local_role = PJ_ICE_SESS_ROLE_CONTROLLING;
962
sdp_state->local_role = PJ_ICE_SESS_ROLE_CONTROLLED;
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)));
980
/* Verify incoming offer and create initial answer */
981
static pj_status_t create_initial_answer(struct transport_ice *tp_ice,
983
pjmedia_sdp_session *loc_sdp,
984
const pjmedia_sdp_session *rem_sdp,
985
unsigned media_index)
987
const pjmedia_sdp_media *rem_m = rem_sdp->media[media_index];
990
/* Check if media is removed (just in case) */
991
if (rem_m->desc.port == 0) {
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);
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);
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);
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,
1021
if (status != PJ_SUCCESS) {
1022
set_no_ice(tp_ice, "Error encoding SDP answer", status);
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)
1038
if (pj_ice_strans_has_sess(tp_ice->ice_st) == PJ_FALSE) {
1039
/* We don't have ICE */
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);
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)
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 */
1067
if (pj_ice_strans_has_sess(tp_ice->ice_st)) {
1069
* Received subsequent offer while we have ICE active.
1072
if (tp_ice->rem_offer_state.match_comp_cnt == 0) {
1073
/* Remote no longer offers ICE */
1077
if (tp_ice->rem_offer_state.ice_mismatch) {
1078
encode_ice_mismatch(sdp_pool, loc_sdp, media_index);
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)
1092
* Received subsequent offer while we DON'T have ICE active.
1095
if (tp_ice->rem_offer_state.match_comp_cnt == 0) {
1096
/* Remote does not support ICE */
1100
if (tp_ice->rem_offer_state.ice_mismatch) {
1101
encode_ice_mismatch(sdp_pool, loc_sdp, media_index);
1105
/* Looks like now remote is offering ICE, so we need to create
1108
status = pj_ice_strans_init_ice(tp_ice->ice_st,
1109
PJ_ICE_SESS_ROLE_CONTROLLED,
1111
if (status != PJ_SUCCESS) {
1112
/* Fail to create new ICE session */
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)
1130
* For both UAC and UAS, pass in the SDP before sending it to remote.
1131
* This will add ICE attributes to the SDP.
1133
static pj_status_t transport_media_create(pjmedia_transport *tp,
1134
pj_pool_t *sdp_pool,
1136
const pjmedia_sdp_session *rem_sdp,
1137
unsigned media_index)
1139
struct transport_ice *tp_ice = (struct transport_ice*)tp;
1140
pj_ice_sess_role ice_role;
1143
PJ_UNUSED_ARG(media_index);
1144
PJ_UNUSED_ARG(sdp_pool);
1146
tp_ice->media_option = options;
1147
tp_ice->oa_role = ROLE_NONE;
1148
tp_ice->initial_sdp = PJ_TRUE;
1150
/* Init ICE, the initial role is set now based on availability of
1151
* rem_sdp, but it will be checked again later.
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);
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)
1168
struct transport_ice *tp_ice = (struct transport_ice*)tp;
1171
/* Validate media transport */
1172
/* This transport only support RTP/AVP transport, unless if
1173
* transport checking is disabled
1175
if ((tp_ice->media_option & PJMEDIA_TPMED_NO_TRANSPORT_CHECKING) == 0) {
1176
pjmedia_sdp_media *loc_m, *rem_m;
1178
rem_m = rem_sdp? rem_sdp->media[media_index] : NULL;
1179
loc_m = sdp_local->media[media_index];
1181
if (pj_stricmp(&loc_m->desc.transport, &STR_RTP_AVP) ||
1182
(rem_m && pj_stricmp(&rem_m->desc.transport, &STR_RTP_AVP)))
1184
pjmedia_sdp_media_deactivate(sdp_pool, loc_m);
1185
return PJMEDIA_SDP_EINPROTO;
1189
if (tp_ice->initial_sdp) {
1191
status = create_initial_answer(tp_ice, sdp_pool, sdp_local,
1192
rem_sdp, media_index);
1194
status = create_initial_offer(tp_ice, sdp_pool, sdp_local,
1199
status = create_subsequent_answer(tp_ice, sdp_pool, sdp_local,
1200
rem_sdp, media_index);
1202
status = create_subsequent_offer(tp_ice, sdp_pool, sdp_local,
1207
if (status==PJ_SUCCESS) {
1209
tp_ice->oa_role = ROLE_ANSWERER;
1211
tp_ice->oa_role = ROLE_OFFERER;
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)
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;
1230
get_ice_attr(rem_sdp, rem_m, &ufrag_attr, &pwd_attr);
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));
1237
/* Get all candidates in the media */
1239
for (i=0; i<rem_m->attr_count && cand_cnt < PJ_ICE_MAX_CAND; ++i) {
1240
pjmedia_sdp_attr *attr;
1242
attr = rem_m->attr[i];
1244
if (pj_strcmp(&attr->name, &STR_CANDIDATE)!=0)
1247
/* Parse candidate */
1248
status = parse_cand(tp_ice->base.name, tmp_pool, &attr->value,
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));
1262
return pj_ice_strans_start_ice(tp_ice->ice_st, &ufrag_attr->value,
1263
&pwd_attr->value, cand_cnt, cand);
1268
* Start ICE checks when both offer and answer have been negotiated
1269
* by SDP negotiator.
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)
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;
1283
PJ_ASSERT_RETURN(tp && tmp_pool && rem_sdp, PJ_EINVAL);
1284
PJ_ASSERT_RETURN(media_index < rem_sdp->media_count, PJ_EINVAL);
1286
rem_m = rem_sdp->media[media_index];
1288
initial_oa = tp_ice->initial_sdp;
1289
current_oa_role = tp_ice->oa_role;
1291
/* SDP has been negotiated */
1292
tp_ice->initial_sdp = PJ_FALSE;
1293
tp_ice->oa_role = ROLE_NONE;
1295
/* Nothing to do if we don't have ICE session */
1296
if (pj_ice_strans_has_sess(tp_ice->ice_st) == PJ_FALSE) {
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.
1304
if (current_oa_role == ROLE_NONE) {
1305
current_oa_role = ROLE_OFFERER;
1308
/* Processing depends on the offer/answer role */
1309
if (current_oa_role == ROLE_OFFERER) {
1311
* We are offerer. So this will be the first time we see the
1314
struct sdp_state answer_state;
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);
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",
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)
1337
/* Remote has reported ice-mismatch */
1339
"Remote answer contains 'ice-mismatch' attribute",
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"
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).
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 "
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"));
1372
if (pj_ice_strans_sess_is_complete(tp_ice->ice_st)) {
1373
PJ_LOG(4,(tp_ice->base.name, "ICE session unchanged"));
1381
* We are answerer. We've seen and negotiated remote's SDP
1382
* before, and the result is in "rem_offer_state".
1384
const pjmedia_sdp_attr *ufrag_attr, *pwd_attr;
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",
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);
1401
/* If ICE is complete and remote doesn't request restart,
1402
* then leave the session as is.
1404
if (!initial_oa && tp_ice->rem_offer_state.ice_restart == PJ_FALSE) {
1405
/* Remote has not requested ICE restart, so session is
1408
PJ_LOG(4,(tp_ice->base.name, "ICE session unchanged"));
1412
/* Either remote has requested ICE restart or this is our
1418
set_no_ice(tp_ice, "restarting by remote request..", PJ_SUCCESS);
1420
/* We have put new ICE ufrag and pwd in the answer. Now
1421
* create a new ICE session with that ufrag/pwd pair.
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,
1429
if (status != PJ_SUCCESS) {
1430
PJ_LOG(1,(tp_ice->base.name,
1431
"ICE re-initialization failed (status=%d)!",
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).
1440
if (tp_ice->rem_offer_state.local_role==PJ_ICE_SESS_ROLE_CONTROLLING &&
1441
pj_ice_strans_has_sess(tp_ice->ice_st))
1443
pj_ice_strans_change_role(tp_ice->ice_st,
1444
PJ_ICE_SESS_ROLE_CONTROLLING);
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)!",
1461
tp_ice->use_ice = PJ_TRUE;
1467
static pj_status_t transport_media_stop(pjmedia_transport *tp)
1469
struct transport_ice *tp_ice = (struct transport_ice*)tp;
1471
set_no_ice(tp_ice, "media stop requested", PJ_SUCCESS);
1477
static pj_status_t transport_get_info(pjmedia_transport *tp,
1478
pjmedia_transport_info *info)
1480
struct transport_ice *tp_ice = (struct transport_ice*)tp;
1481
pj_ice_sess_cand cand;
1484
pj_bzero(&info->sock_info, sizeof(info->sock_info));
1485
info->sock_info.rtp_sock = info->sock_info.rtcp_sock = PJ_INVALID_SOCKET;
1487
/* Get RTP default address */
1488
status = pj_ice_strans_get_def_cand(tp_ice->ice_st, 1, &cand);
1489
if (status != PJ_SUCCESS)
1492
pj_sockaddr_cp(&info->sock_info.rtp_addr_name, &cand.addr);
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)
1500
pj_sockaddr_cp(&info->sock_info.rtcp_addr_name, &cand.addr);
1503
/* Set remote address originating RTP & RTCP if this transport has
1504
* ICE activated or received any packets.
1506
if (tp_ice->use_ice || tp_ice->rtp_src_cnt) {
1507
info->src_rtp_name = tp_ice->rtp_src_addr;
1509
if (tp_ice->use_ice || tp_ice->rtcp_src_cnt) {
1510
info->src_rtcp_name = tp_ice->rtcp_src_addr;
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;
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);
1524
ii = (pjmedia_ice_transport_info*) tsi->buffer;
1525
pj_bzero(ii, sizeof(*ii));
1527
if (pj_ice_strans_has_sess(tp_ice->ice_st))
1528
ii->role = pj_ice_strans_get_role(tp_ice->ice_st);
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);
1534
for (i=1; i<=ii->comp_cnt && i<=PJ_ARRAY_SIZE(ii->comp); ++i) {
1535
const pj_ice_sess_check *chk;
1537
chk = pj_ice_strans_get_valid_pair(tp_ice->ice_st, i);
1539
ii->comp[i-1].lcand_type = chk->lcand->type;
1540
ii->comp[i-1].rcand_type = chk->rcand->type;
1549
static pj_status_t transport_attach (pjmedia_transport *tp,
1551
const pj_sockaddr_t *rem_addr,
1552
const pj_sockaddr_t *rem_rtcp,
1554
void (*rtp_cb)(void*,
1557
void (*rtcp_cb)(void*,
1561
struct transport_ice *tp_ice = (struct transport_ice*)tp;
1563
tp_ice->stream = stream;
1564
tp_ice->rtp_cb = rtp_cb;
1565
tp_ice->rtcp_cb = rtcp_cb;
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;
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;
1581
static void transport_detach(pjmedia_transport *tp,
1584
struct transport_ice *tp_ice = (struct transport_ice*)tp;
1586
/* TODO: need to solve ticket #460 here */
1588
tp_ice->rtp_cb = NULL;
1589
tp_ice->rtcp_cb = NULL;
1590
tp_ice->stream = NULL;
1592
PJ_UNUSED_ARG(strm);
1596
static pj_status_t transport_send_rtp(pjmedia_transport *tp,
1600
struct transport_ice *tp_ice = (struct transport_ice*)tp;
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 "
1612
return pj_ice_strans_sendto(tp_ice->ice_st, 1,
1613
pkt, size, &tp_ice->remote_rtp,
1618
static pj_status_t transport_send_rtcp(pjmedia_transport *tp,
1622
return transport_send_rtcp2(tp, NULL, 0, pkt, size);
1625
static pj_status_t transport_send_rtcp2(pjmedia_transport *tp,
1626
const pj_sockaddr_t *addr,
1631
struct transport_ice *tp_ice = (struct transport_ice*)tp;
1632
if (tp_ice->comp_cnt > 1) {
1634
addr = &tp_ice->remote_rtcp;
1635
addr_len = pj_sockaddr_get_len(addr);
1637
return pj_ice_strans_sendto(tp_ice->ice_st, 2, pkt, size,
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)
1650
struct transport_ice *tp_ice;
1651
pj_bool_t discard = PJ_FALSE;
1653
tp_ice = (struct transport_ice*) pj_ice_strans_get_user_data(ice_st);
1655
if (comp_id==1 && tp_ice->rtp_cb) {
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 "
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.
1672
if (!tp_ice->use_ice) {
1673
pj_bool_t enable_switch =
1674
((tp_ice->options & PJMEDIA_ICE_NO_SRC_ADDR_CHECKING)==0);
1676
if (!enable_switch ||
1677
pj_sockaddr_cmp(&tp_ice->remote_rtp, src_addr) == 0)
1679
/* Don't switch while we're receiving from remote_rtp */
1680
tp_ice->rtp_src_cnt = 0;
1683
++tp_ice->rtp_src_cnt;
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);
1690
tp_ice->rtp_src_cnt = 0;
1694
if (tp_ice->rtp_src_cnt < PJMEDIA_RTP_NAT_PROBATION_CNT) {
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);
1704
tp_ice->rtp_src_cnt = 0;
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)));
1711
/* Also update remote RTCP address if actual RTCP source
1712
* address is not heard yet.
1714
if (!pj_sockaddr_has_addr(&tp_ice->rtcp_src_addr)) {
1717
pj_sockaddr_cp(&tp_ice->remote_rtcp,
1718
&tp_ice->remote_rtp);
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);
1724
PJ_LOG(4,(tp_ice->base.name,
1725
"Remote RTCP address switched to predicted "
1727
pj_sockaddr_print(&tp_ice->remote_rtcp,
1729
sizeof(addr_text), 3)));
1736
(*tp_ice->rtp_cb)(tp_ice->stream, pkt, size);
1738
} else if (comp_id==2 && tp_ice->rtcp_cb) {
1740
/* Check if RTCP source address is the same as the configured
1741
* remote address, and switch the address when they are
1744
if (!tp_ice->use_ice &&
1745
(tp_ice->options & PJMEDIA_ICE_NO_SRC_ADDR_CHECKING)==0)
1747
if (pj_sockaddr_cmp(&tp_ice->remote_rtcp, src_addr) == 0) {
1748
tp_ice->rtcp_src_cnt = 0;
1752
++tp_ice->rtcp_src_cnt;
1753
if (tp_ice->rtcp_src_cnt < PJMEDIA_RTCP_NAT_PROBATION_CNT) {
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);
1760
pj_assert(tp_ice->addr_len==pj_sockaddr_get_len(src_addr));
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),
1772
(*tp_ice->rtcp_cb)(tp_ice->stream, pkt, size);
1775
PJ_UNUSED_ARG(src_addr_len);
1779
static void ice_on_ice_complete(pj_ice_strans *ice_st,
1780
pj_ice_strans_op op,
1783
struct transport_ice *tp_ice;
1785
tp_ice = (struct transport_ice*) pj_ice_strans_get_user_data(ice_st);
1787
/* Notify application */
1788
if (tp_ice->cb.on_ice_complete)
1789
(*tp_ice->cb.on_ice_complete)(&tp_ice->base, op, result);
1794
static pj_status_t transport_simulate_lost(pjmedia_transport *tp,
1798
struct transport_ice *ice = (struct transport_ice*) tp;
1800
PJ_ASSERT_RETURN(tp && pct_lost <= 100, PJ_EINVAL);
1802
if (dir & PJMEDIA_DIR_ENCODING)
1803
ice->tx_drop_pct = pct_lost;
1805
if (dir & PJMEDIA_DIR_DECODING)
1806
ice->rx_drop_pct = pct_lost;
1813
* Destroy ICE media transport.
1815
static pj_status_t transport_destroy(pjmedia_transport *tp)
1817
struct transport_ice *tp_ice = (struct transport_ice*)tp;
1819
if (tp_ice->ice_st) {
1820
pj_ice_strans_destroy(tp_ice->ice_st);
1821
tp_ice->ice_st = NULL;
1825
pj_pool_t *pool = tp_ice->pool;
1826
tp_ice->pool = NULL;
1827
pj_pool_release(pool);