| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <pjlib.h> |
| 21 |
#include <pjlib-util.h> |
| 22 |
#include <pjnath.h> |
| 23 |
|
| 24 |
|
| 25 |
#define THIS_FILE "icedemo.c" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#define KA_INTERVAL 300 |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
static struct app_t |
| 35 |
{ |
| 36 |
|
| 37 |
struct options |
| 38 |
{ |
| 39 |
unsigned comp_cnt; |
| 40 |
pj_str_t ns; |
| 41 |
int max_host; |
| 42 |
pj_bool_t regular; |
| 43 |
pj_str_t stun_srv; |
| 44 |
pj_str_t turn_srv; |
| 45 |
pj_bool_t turn_tcp; |
| 46 |
pj_str_t turn_username; |
| 47 |
pj_str_t turn_password; |
| 48 |
pj_bool_t turn_fingerprint; |
| 49 |
const char *log_file; |
| 50 |
} opt; |
| 51 |
|
| 52 |
|
| 53 |
pj_caching_pool cp; |
| 54 |
pj_pool_t *pool; |
| 55 |
pj_thread_t *thread; |
| 56 |
pj_bool_t thread_quit_flag; |
| 57 |
pj_ice_strans_cfg ice_cfg; |
| 58 |
pj_ice_strans *icest; |
| 59 |
FILE *log_fhnd; |
| 60 |
|
| 61 |
|
| 62 |
struct rem_info |
| 63 |
{ |
| 64 |
char ufrag[80]; |
| 65 |
char pwd[80]; |
| 66 |
unsigned comp_cnt; |
| 67 |
pj_sockaddr def_addr[PJ_ICE_MAX_COMP]; |
| 68 |
unsigned cand_cnt; |
| 69 |
pj_ice_sess_cand cand[PJ_ICE_ST_MAX_CAND]; |
| 70 |
} rem; |
| 71 |
|
| 72 |
} icedemo; |
| 73 |
|
| 74 |
|
| 75 |
static void icedemo_perror(const char *title, pj_status_t status) |
| 76 |
{ |
| 77 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 78 |
|
| 79 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 80 |
PJ_LOG(1,(THIS_FILE, "%s: %s", title, errmsg)); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static void err_exit(const char *title, pj_status_t status) |
| 87 |
{ |
| 88 |
if (status != PJ_SUCCESS) { |
| 89 |
icedemo_perror(title, status); |
| 90 |
} |
| 91 |
PJ_LOG(3,(THIS_FILE, "Shutting down..")); |
| 92 |
|
| 93 |
if (icedemo.icest) |
| 94 |
pj_ice_strans_destroy(icedemo.icest); |
| 95 |
|
| 96 |
pj_thread_sleep(500); |
| 97 |
|
| 98 |
icedemo.thread_quit_flag = PJ_TRUE; |
| 99 |
if (icedemo.thread) { |
| 100 |
pj_thread_join(icedemo.thread); |
| 101 |
pj_thread_destroy(icedemo.thread); |
| 102 |
} |
| 103 |
|
| 104 |
if (icedemo.ice_cfg.stun_cfg.ioqueue) |
| 105 |
pj_ioqueue_destroy(icedemo.ice_cfg.stun_cfg.ioqueue); |
| 106 |
|
| 107 |
if (icedemo.ice_cfg.stun_cfg.timer_heap) |
| 108 |
pj_timer_heap_destroy(icedemo.ice_cfg.stun_cfg.timer_heap); |
| 109 |
|
| 110 |
pj_caching_pool_destroy(&icedemo.cp); |
| 111 |
|
| 112 |
pj_shutdown(); |
| 113 |
|
| 114 |
if (icedemo.log_fhnd) { |
| 115 |
fclose(icedemo.log_fhnd); |
| 116 |
icedemo.log_fhnd = NULL; |
| 117 |
} |
| 118 |
|
| 119 |
exit(status != PJ_SUCCESS); |
| 120 |
} |
| 121 |
|
| 122 |
#define CHECK(expr) status=expr; \ |
| 123 |
if (status!=PJ_SUCCESS) { \ |
| 124 |
err_exit(#expr, status); \ |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static pj_status_t handle_events(unsigned max_msec, unsigned *p_count) |
| 132 |
{ |
| 133 |
enum { MAX_NET_EVENTS = 1 }; |
| 134 |
pj_time_val max_timeout = {0, 0}; |
| 135 |
pj_time_val timeout = { 0, 0}; |
| 136 |
unsigned count = 0, net_event_count = 0; |
| 137 |
int c; |
| 138 |
|
| 139 |
max_timeout.msec = max_msec; |
| 140 |
|
| 141 |
|
| 142 |
timeout.sec = timeout.msec = 0; |
| 143 |
c = pj_timer_heap_poll( icedemo.ice_cfg.stun_cfg.timer_heap, &timeout ); |
| 144 |
if (c > 0) |
| 145 |
count += c; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
pj_assert(timeout.sec >= 0 && timeout.msec >= 0); |
| 151 |
if (timeout.msec >= 1000) timeout.msec = 999; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if (PJ_TIME_VAL_GT(timeout, max_timeout)) |
| 157 |
timeout = max_timeout; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
do { |
| 171 |
c = pj_ioqueue_poll( icedemo.ice_cfg.stun_cfg.ioqueue, &timeout); |
| 172 |
if (c < 0) { |
| 173 |
pj_status_t err = pj_get_netos_error(); |
| 174 |
pj_thread_sleep(PJ_TIME_VAL_MSEC(timeout)); |
| 175 |
if (p_count) |
| 176 |
*p_count = count; |
| 177 |
return err; |
| 178 |
} else if (c == 0) { |
| 179 |
break; |
| 180 |
} else { |
| 181 |
net_event_count += c; |
| 182 |
timeout.sec = timeout.msec = 0; |
| 183 |
} |
| 184 |
} while (c > 0 && net_event_count < MAX_NET_EVENTS); |
| 185 |
|
| 186 |
count += net_event_count; |
| 187 |
if (p_count) |
| 188 |
*p_count = count; |
| 189 |
|
| 190 |
return PJ_SUCCESS; |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
static int icedemo_worker_thread(void *unused) |
| 198 |
{ |
| 199 |
PJ_UNUSED_ARG(unused); |
| 200 |
|
| 201 |
while (!icedemo.thread_quit_flag) { |
| 202 |
handle_events(500, NULL); |
| 203 |
} |
| 204 |
|
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
static void cb_on_rx_data(pj_ice_strans *ice_st, |
| 215 |
unsigned comp_id, |
| 216 |
void *pkt, pj_size_t size, |
| 217 |
const pj_sockaddr_t *src_addr, |
| 218 |
unsigned src_addr_len) |
| 219 |
{ |
| 220 |
char ipstr[PJ_INET6_ADDRSTRLEN+10]; |
| 221 |
|
| 222 |
PJ_UNUSED_ARG(ice_st); |
| 223 |
PJ_UNUSED_ARG(src_addr_len); |
| 224 |
PJ_UNUSED_ARG(pkt); |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
PJ_LOG(3,(THIS_FILE, "Component %d: received %ld bytes data from %s: \"%.*s\"", |
| 230 |
comp_id, size, |
| 231 |
pj_sockaddr_print(src_addr, ipstr, sizeof(ipstr), 3), |
| 232 |
(unsigned)size, |
| 233 |
(char*)pkt)); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
static void cb_on_ice_complete(pj_ice_strans *ice_st, |
| 241 |
pj_ice_strans_op op, |
| 242 |
pj_status_t status) |
| 243 |
{ |
| 244 |
const char *opname = |
| 245 |
(op==PJ_ICE_STRANS_OP_INIT? "initialization" : |
| 246 |
(op==PJ_ICE_STRANS_OP_NEGOTIATION ? "negotiation" : "unknown_op")); |
| 247 |
|
| 248 |
if (status == PJ_SUCCESS) { |
| 249 |
PJ_LOG(3,(THIS_FILE, "ICE %s successful", opname)); |
| 250 |
} else { |
| 251 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 252 |
|
| 253 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 254 |
PJ_LOG(1,(THIS_FILE, "ICE %s failed: %s", opname, errmsg)); |
| 255 |
pj_ice_strans_destroy(ice_st); |
| 256 |
icedemo.icest = NULL; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
static void log_func(int level, const char *data, int len) |
| 262 |
{ |
| 263 |
pj_log_write(level, data, len); |
| 264 |
if (icedemo.log_fhnd) { |
| 265 |
if (fwrite(data, len, 1, icedemo.log_fhnd) != 1) |
| 266 |
return; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
static pj_status_t icedemo_init(void) |
| 276 |
{ |
| 277 |
pj_status_t status; |
| 278 |
|
| 279 |
if (icedemo.opt.log_file) { |
| 280 |
icedemo.log_fhnd = fopen(icedemo.opt.log_file, "a"); |
| 281 |
pj_log_set_log_func(&log_func); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
CHECK( pj_init() ); |
| 286 |
CHECK( pjlib_util_init() ); |
| 287 |
CHECK( pjnath_init() ); |
| 288 |
|
| 289 |
|
| 290 |
pj_caching_pool_init(&icedemo.cp, NULL, 0); |
| 291 |
|
| 292 |
|
| 293 |
pj_ice_strans_cfg_default(&icedemo.ice_cfg); |
| 294 |
|
| 295 |
icedemo.ice_cfg.stun_cfg.pf = &icedemo.cp.factory; |
| 296 |
|
| 297 |
|
| 298 |
icedemo.pool = pj_pool_create(&icedemo.cp.factory, "icedemo", |
| 299 |
512, 512, NULL); |
| 300 |
|
| 301 |
|
| 302 |
CHECK( pj_timer_heap_create(icedemo.pool, 100, |
| 303 |
&icedemo.ice_cfg.stun_cfg.timer_heap) ); |
| 304 |
|
| 305 |
|
| 306 |
CHECK( pj_ioqueue_create(icedemo.pool, 16, |
| 307 |
&icedemo.ice_cfg.stun_cfg.ioqueue) ); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
CHECK( pj_thread_create(icedemo.pool, "icedemo", &icedemo_worker_thread, |
| 314 |
NULL, 0, 0, &icedemo.thread) ); |
| 315 |
|
| 316 |
icedemo.ice_cfg.af = pj_AF_INET(); |
| 317 |
|
| 318 |
|
| 319 |
if (icedemo.opt.ns.slen) { |
| 320 |
CHECK( pj_dns_resolver_create(&icedemo.cp.factory, |
| 321 |
"resolver", |
| 322 |
0, |
| 323 |
icedemo.ice_cfg.stun_cfg.timer_heap, |
| 324 |
icedemo.ice_cfg.stun_cfg.ioqueue, |
| 325 |
&icedemo.ice_cfg.resolver) ); |
| 326 |
|
| 327 |
CHECK( pj_dns_resolver_set_ns(icedemo.ice_cfg.resolver, 1, |
| 328 |
&icedemo.opt.ns, NULL) ); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
if (icedemo.opt.max_host != -1) |
| 335 |
icedemo.ice_cfg.stun.max_host_cands = icedemo.opt.max_host; |
| 336 |
|
| 337 |
|
| 338 |
if (icedemo.opt.regular) |
| 339 |
icedemo.ice_cfg.opt.aggressive = PJ_FALSE; |
| 340 |
else |
| 341 |
icedemo.ice_cfg.opt.aggressive = PJ_TRUE; |
| 342 |
|
| 343 |
|
| 344 |
if (icedemo.opt.stun_srv.slen) { |
| 345 |
char *pos; |
| 346 |
|
| 347 |
|
| 348 |
if ((pos=pj_strchr(&icedemo.opt.stun_srv, ':')) != NULL) { |
| 349 |
icedemo.ice_cfg.stun.server.ptr = icedemo.opt.stun_srv.ptr; |
| 350 |
icedemo.ice_cfg.stun.server.slen = (pos - icedemo.opt.stun_srv.ptr); |
| 351 |
|
| 352 |
icedemo.ice_cfg.stun.port = (pj_uint16_t)atoi(pos+1); |
| 353 |
} else { |
| 354 |
icedemo.ice_cfg.stun.server = icedemo.opt.stun_srv; |
| 355 |
icedemo.ice_cfg.stun.port = PJ_STUN_PORT; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
icedemo.ice_cfg.stun.cfg.ka_interval = KA_INTERVAL; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
if (icedemo.opt.turn_srv.slen) { |
| 366 |
char *pos; |
| 367 |
|
| 368 |
|
| 369 |
if ((pos=pj_strchr(&icedemo.opt.turn_srv, ':')) != NULL) { |
| 370 |
icedemo.ice_cfg.turn.server.ptr = icedemo.opt.turn_srv.ptr; |
| 371 |
icedemo.ice_cfg.turn.server.slen = (pos - icedemo.opt.turn_srv.ptr); |
| 372 |
|
| 373 |
icedemo.ice_cfg.turn.port = (pj_uint16_t)atoi(pos+1); |
| 374 |
} else { |
| 375 |
icedemo.ice_cfg.turn.server = icedemo.opt.turn_srv; |
| 376 |
icedemo.ice_cfg.turn.port = PJ_STUN_PORT; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
icedemo.ice_cfg.turn.auth_cred.type = PJ_STUN_AUTH_CRED_STATIC; |
| 381 |
icedemo.ice_cfg.turn.auth_cred.data.static_cred.username = icedemo.opt.turn_username; |
| 382 |
icedemo.ice_cfg.turn.auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN; |
| 383 |
icedemo.ice_cfg.turn.auth_cred.data.static_cred.data = icedemo.opt.turn_password; |
| 384 |
|
| 385 |
|
| 386 |
if (icedemo.opt.turn_tcp) |
| 387 |
icedemo.ice_cfg.turn.conn_type = PJ_TURN_TP_TCP; |
| 388 |
else |
| 389 |
icedemo.ice_cfg.turn.conn_type = PJ_TURN_TP_UDP; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
icedemo.ice_cfg.turn.alloc_param.ka_interval = KA_INTERVAL; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
return PJ_SUCCESS; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
static void icedemo_create_instance(void) |
| 406 |
{ |
| 407 |
pj_ice_strans_cb icecb; |
| 408 |
pj_status_t status; |
| 409 |
|
| 410 |
if (icedemo.icest != NULL) { |
| 411 |
puts("ICE instance already created, destroy it first"); |
| 412 |
return; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
pj_bzero(&icecb, sizeof(icecb)); |
| 417 |
icecb.on_rx_data = cb_on_rx_data; |
| 418 |
icecb.on_ice_complete = cb_on_ice_complete; |
| 419 |
|
| 420 |
|
| 421 |
status = pj_ice_strans_create("icedemo", |
| 422 |
&icedemo.ice_cfg, |
| 423 |
icedemo.opt.comp_cnt, |
| 424 |
NULL, |
| 425 |
&icecb, |
| 426 |
&icedemo.icest) |
| 427 |
; |
| 428 |
if (status != PJ_SUCCESS) |
| 429 |
icedemo_perror("error creating ice", status); |
| 430 |
else |
| 431 |
PJ_LOG(3,(THIS_FILE, "ICE instance successfully created")); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
static void reset_rem_info(void) |
| 436 |
{ |
| 437 |
pj_bzero(&icedemo.rem, sizeof(icedemo.rem)); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
static void icedemo_destroy_instance(void) |
| 445 |
{ |
| 446 |
if (icedemo.icest == NULL) { |
| 447 |
PJ_LOG(1,(THIS_FILE, "Error: No ICE instance, create it first")); |
| 448 |
return; |
| 449 |
} |
| 450 |
|
| 451 |
pj_ice_strans_destroy(icedemo.icest); |
| 452 |
icedemo.icest = NULL; |
| 453 |
|
| 454 |
reset_rem_info(); |
| 455 |
|
| 456 |
PJ_LOG(3,(THIS_FILE, "ICE instance destroyed")); |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
static void icedemo_init_session(unsigned rolechar) |
| 464 |
{ |
| 465 |
pj_ice_sess_role role = (pj_tolower((pj_uint8_t)rolechar)=='o' ? |
| 466 |
PJ_ICE_SESS_ROLE_CONTROLLING : |
| 467 |
PJ_ICE_SESS_ROLE_CONTROLLED); |
| 468 |
pj_status_t status; |
| 469 |
|
| 470 |
if (icedemo.icest == NULL) { |
| 471 |
PJ_LOG(1,(THIS_FILE, "Error: No ICE instance, create it first")); |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
if (pj_ice_strans_has_sess(icedemo.icest)) { |
| 476 |
PJ_LOG(1,(THIS_FILE, "Error: Session already created")); |
| 477 |
return; |
| 478 |
} |
| 479 |
|
| 480 |
status = pj_ice_strans_init_ice(icedemo.icest, role, NULL, NULL); |
| 481 |
if (status != PJ_SUCCESS) |
| 482 |
icedemo_perror("error creating session", status); |
| 483 |
else |
| 484 |
PJ_LOG(3,(THIS_FILE, "ICE session created")); |
| 485 |
|
| 486 |
reset_rem_info(); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
static void icedemo_stop_session(void) |
| 494 |
{ |
| 495 |
pj_status_t status; |
| 496 |
|
| 497 |
if (icedemo.icest == NULL) { |
| 498 |
PJ_LOG(1,(THIS_FILE, "Error: No ICE instance, create it first")); |
| 499 |
return; |
| 500 |
} |
| 501 |
|
| 502 |
if (!pj_ice_strans_has_sess(icedemo.icest)) { |
| 503 |
PJ_LOG(1,(THIS_FILE, "Error: No ICE session, initialize first")); |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
status = pj_ice_strans_stop_ice(icedemo.icest); |
| 508 |
if (status != PJ_SUCCESS) |
| 509 |
icedemo_perror("error stopping session", status); |
| 510 |
else |
| 511 |
PJ_LOG(3,(THIS_FILE, "ICE session stopped")); |
| 512 |
|
| 513 |
reset_rem_info(); |
| 514 |
} |
| 515 |
|
| 516 |
#define PRINT(...) \ |
| 517 |
printed = pj_ansi_snprintf(p, maxlen - (p-buffer), \ |
| 518 |
__VA_ARGS__); \ |
| 519 |
if (printed <= 0 || printed >= (int)(maxlen - (p-buffer))) \ |
| 520 |
return -PJ_ETOOSMALL; \ |
| 521 |
p += printed |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
static int print_cand(char buffer[], unsigned maxlen, |
| 526 |
const pj_ice_sess_cand *cand) |
| 527 |
{ |
| 528 |
char ipaddr[PJ_INET6_ADDRSTRLEN]; |
| 529 |
char *p = buffer; |
| 530 |
int printed; |
| 531 |
|
| 532 |
PRINT("a=candidate:%.*s %u UDP %u %s %u typ ", |
| 533 |
(int)cand->foundation.slen, |
| 534 |
cand->foundation.ptr, |
| 535 |
(unsigned)cand->comp_id, |
| 536 |
cand->prio, |
| 537 |
pj_sockaddr_print(&cand->addr, ipaddr, |
| 538 |
sizeof(ipaddr), 0), |
| 539 |
(unsigned)pj_sockaddr_get_port(&cand->addr)); |
| 540 |
|
| 541 |
PRINT("%s\n", |
| 542 |
pj_ice_get_cand_type_name(cand->type)); |
| 543 |
|
| 544 |
if (p == buffer+maxlen) |
| 545 |
return -PJ_ETOOSMALL; |
| 546 |
|
| 547 |
*p = '\0'; |
| 548 |
|
| 549 |
return (int)(p-buffer); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
static int encode_session(char buffer[], unsigned maxlen) |
| 556 |
{ |
| 557 |
char *p = buffer; |
| 558 |
unsigned comp; |
| 559 |
int printed; |
| 560 |
pj_str_t local_ufrag, local_pwd; |
| 561 |
pj_status_t status; |
| 562 |
|
| 563 |
|
| 564 |
PRINT("v=0\no=- 3414953978 3414953978 IN IP4 localhost\ns=ice\nt=0 0\n"); |
| 565 |
|
| 566 |
|
| 567 |
pj_ice_strans_get_ufrag_pwd(icedemo.icest, &local_ufrag, &local_pwd, |
| 568 |
NULL, NULL); |
| 569 |
|
| 570 |
|
| 571 |
PRINT("a=ice-ufrag:%.*s\na=ice-pwd:%.*s\n", |
| 572 |
(int)local_ufrag.slen, |
| 573 |
local_ufrag.ptr, |
| 574 |
(int)local_pwd.slen, |
| 575 |
local_pwd.ptr); |
| 576 |
|
| 577 |
|
| 578 |
for (comp=0; comp<icedemo.opt.comp_cnt; ++comp) { |
| 579 |
unsigned j, cand_cnt; |
| 580 |
pj_ice_sess_cand cand[PJ_ICE_ST_MAX_CAND]; |
| 581 |
char ipaddr[PJ_INET6_ADDRSTRLEN]; |
| 582 |
|
| 583 |
|
| 584 |
status = pj_ice_strans_get_def_cand(icedemo.icest, comp+1, &cand[0]); |
| 585 |
if (status != PJ_SUCCESS) |
| 586 |
return -status; |
| 587 |
|
| 588 |
|
| 589 |
if (comp==0) { |
| 590 |
|
| 591 |
PRINT("m=audio %d RTP/AVP 0\n" |
| 592 |
"c=IN IP4 %s\n", |
| 593 |
(int)pj_sockaddr_get_port(&cand[0].addr), |
| 594 |
pj_sockaddr_print(&cand[0].addr, ipaddr, |
| 595 |
sizeof(ipaddr), 0)); |
| 596 |
} else if (comp==1) { |
| 597 |
|
| 598 |
PRINT("a=rtcp:%d IN IP4 %s\n", |
| 599 |
(int)pj_sockaddr_get_port(&cand[0].addr), |
| 600 |
pj_sockaddr_print(&cand[0].addr, ipaddr, |
| 601 |
sizeof(ipaddr), 0)); |
| 602 |
} else { |
| 603 |
|
| 604 |
PRINT("a=Xice-defcand:%d IN IP4 %s\n", |
| 605 |
(int)pj_sockaddr_get_port(&cand[0].addr), |
| 606 |
pj_sockaddr_print(&cand[0].addr, ipaddr, |
| 607 |
sizeof(ipaddr), 0)); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
cand_cnt = PJ_ARRAY_SIZE(cand); |
| 612 |
status = pj_ice_strans_enum_cands(icedemo.icest, comp+1, |
| 613 |
&cand_cnt, cand); |
| 614 |
if (status != PJ_SUCCESS) |
| 615 |
return -status; |
| 616 |
|
| 617 |
|
| 618 |
for (j=0; j<cand_cnt; ++j) { |
| 619 |
printed = print_cand(p, maxlen - (unsigned)(p-buffer), &cand[j]); |
| 620 |
if (printed < 0) |
| 621 |
return -PJ_ETOOSMALL; |
| 622 |
p += printed; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
if (p == buffer+maxlen) |
| 627 |
return -PJ_ETOOSMALL; |
| 628 |
|
| 629 |
*p = '\0'; |
| 630 |
return (int)(p - buffer); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
static void icedemo_show_ice(void) |
| 639 |
{ |
| 640 |
static char buffer[1000]; |
| 641 |
int len; |
| 642 |
|
| 643 |
if (icedemo.icest == NULL) { |
| 644 |
PJ_LOG(1,(THIS_FILE, "Error: No ICE instance, create it first")); |
| 645 |
return; |
| 646 |
} |
| 647 |
|
| 648 |
puts("General info"); |
| 649 |
puts("---------------"); |
| 650 |
printf("Component count : %d\n", icedemo.opt.comp_cnt); |
| 651 |
printf("Status : "); |
| 652 |
if (pj_ice_strans_sess_is_complete(icedemo.icest)) |
| 653 |
puts("negotiation complete"); |
| 654 |
else if (pj_ice_strans_sess_is_running(icedemo.icest)) |
| 655 |
puts("negotiation is in progress"); |
| 656 |
else if (pj_ice_strans_has_sess(icedemo.icest)) |
| 657 |
puts("session ready"); |
| 658 |
else |
| 659 |
puts("session not created"); |
| 660 |
|
| 661 |
if (!pj_ice_strans_has_sess(icedemo.icest)) { |
| 662 |
puts("Create the session first to see more info"); |
| 663 |
return; |
| 664 |
} |
| 665 |
|
| 666 |
printf("Negotiated comp_cnt: %d\n", |
| 667 |
pj_ice_strans_get_running_comp_cnt(icedemo.icest)); |
| 668 |
printf("Role : %s\n", |
| 669 |
pj_ice_strans_get_role(icedemo.icest)==PJ_ICE_SESS_ROLE_CONTROLLED ? |
| 670 |
"controlled" : "controlling"); |
| 671 |
|
| 672 |
len = encode_session(buffer, sizeof(buffer)); |
| 673 |
if (len < 0) |
| 674 |
err_exit("not enough buffer to show ICE status", -len); |
| 675 |
|
| 676 |
puts(""); |
| 677 |
printf("Local SDP (paste this to remote host):\n" |
| 678 |
"--------------------------------------\n" |
| 679 |
"%s\n", buffer); |
| 680 |
|
| 681 |
|
| 682 |
puts(""); |
| 683 |
puts("Remote info:\n" |
| 684 |
"----------------------"); |
| 685 |
if (icedemo.rem.cand_cnt==0) { |
| 686 |
puts("No remote info yet"); |
| 687 |
} else { |
| 688 |
unsigned i; |
| 689 |
|
| 690 |
printf("Remote ufrag : %s\n", icedemo.rem.ufrag); |
| 691 |
printf("Remote password : %s\n", icedemo.rem.pwd); |
| 692 |
printf("Remote cand. cnt. : %d\n", icedemo.rem.cand_cnt); |
| 693 |
|
| 694 |
for (i=0; i<icedemo.rem.cand_cnt; ++i) { |
| 695 |
len = print_cand(buffer, sizeof(buffer), &icedemo.rem.cand[i]); |
| 696 |
if (len < 0) |
| 697 |
err_exit("not enough buffer to show ICE status", -len); |
| 698 |
|
| 699 |
printf(" %s", buffer); |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
static void icedemo_input_remote(void) |
| 710 |
{ |
| 711 |
char linebuf[80]; |
| 712 |
unsigned media_cnt = 0; |
| 713 |
unsigned comp0_port = 0; |
| 714 |
char comp0_addr[80]; |
| 715 |
pj_bool_t done = PJ_FALSE; |
| 716 |
|
| 717 |
puts("Paste SDP from remote host, end with empty line"); |
| 718 |
|
| 719 |
reset_rem_info(); |
| 720 |
|
| 721 |
comp0_addr[0] = '\0'; |
| 722 |
|
| 723 |
while (!done) { |
| 724 |
pj_size_t len; |
| 725 |
char *line; |
| 726 |
|
| 727 |
printf(">"); |
| 728 |
if (stdout) fflush(stdout); |
| 729 |
|
| 730 |
if (fgets(linebuf, sizeof(linebuf), stdin)==NULL) |
| 731 |
break; |
| 732 |
|
| 733 |
len = strlen(linebuf); |
| 734 |
while (len && (linebuf[len-1] == '\r' || linebuf[len-1] == '\n')) |
| 735 |
linebuf[--len] = '\0'; |
| 736 |
|
| 737 |
line = linebuf; |
| 738 |
while (len && pj_isspace(*line)) |
| 739 |
++line, --len; |
| 740 |
|
| 741 |
if (len==0) |
| 742 |
break; |
| 743 |
|
| 744 |
|
| 745 |
if (media_cnt > 1) |
| 746 |
continue; |
| 747 |
|
| 748 |
switch (line[0]) { |
| 749 |
case 'm': |
| 750 |
{ |
| 751 |
int cnt; |
| 752 |
char media[32], portstr[32]; |
| 753 |
|
| 754 |
++media_cnt; |
| 755 |
if (media_cnt > 1) { |
| 756 |
puts("Media line ignored"); |
| 757 |
break; |
| 758 |
} |
| 759 |
|
| 760 |
cnt = sscanf(line+2, "%s %s RTP/", media, portstr); |
| 761 |
if (cnt != 2) { |
| 762 |
PJ_LOG(1,(THIS_FILE, "Error parsing media line")); |
| 763 |
goto on_error; |
| 764 |
} |
| 765 |
|
| 766 |
comp0_port = atoi(portstr); |
| 767 |
|
| 768 |
} |
| 769 |
break; |
| 770 |
case 'c': |
| 771 |
{ |
| 772 |
int cnt; |
| 773 |
char c[32], net[32], ip[80]; |
| 774 |
|
| 775 |
cnt = sscanf(line+2, "%s %s %s", c, net, ip); |
| 776 |
if (cnt != 3) { |
| 777 |
PJ_LOG(1,(THIS_FILE, "Error parsing connection line")); |
| 778 |
goto on_error; |
| 779 |
} |
| 780 |
|
| 781 |
pj_ansi_strxcpy(comp0_addr, ip, sizeof(comp0_addr)); |
| 782 |
} |
| 783 |
break; |
| 784 |
case 'a': |
| 785 |
{ |
| 786 |
char *attr = strtok(line+2, ": \t\r\n"); |
| 787 |
if (strcmp(attr, "ice-ufrag")==0) { |
| 788 |
pj_ansi_strxcpy(icedemo.rem.ufrag, attr+strlen(attr)+1, |
| 789 |
sizeof(icedemo.rem.ufrag)); |
| 790 |
} else if (strcmp(attr, "ice-pwd")==0) { |
| 791 |
pj_ansi_strxcpy(icedemo.rem.pwd, attr+strlen(attr)+1, |
| 792 |
sizeof(icedemo.rem.pwd)); |
| 793 |
} else if (strcmp(attr, "rtcp")==0) { |
| 794 |
char *val = attr+strlen(attr)+1; |
| 795 |
int af, cnt; |
| 796 |
int port; |
| 797 |
char net[32], ip[64]; |
| 798 |
pj_str_t tmp_addr; |
| 799 |
pj_status_t status; |
| 800 |
|
| 801 |
cnt = sscanf(val, "%d IN %s %s", &port, net, ip); |
| 802 |
if (cnt != 3) { |
| 803 |
PJ_LOG(1,(THIS_FILE, "Error parsing rtcp attribute")); |
| 804 |
goto on_error; |
| 805 |
} |
| 806 |
|
| 807 |
if (strchr(ip, ':')) |
| 808 |
af = pj_AF_INET6(); |
| 809 |
else |
| 810 |
af = pj_AF_INET(); |
| 811 |
|
| 812 |
pj_sockaddr_init(af, &icedemo.rem.def_addr[1], NULL, 0); |
| 813 |
tmp_addr = pj_str(ip); |
| 814 |
status = pj_sockaddr_set_str_addr(af, &icedemo.rem.def_addr[1], |
| 815 |
&tmp_addr); |
| 816 |
if (status != PJ_SUCCESS) { |
| 817 |
PJ_LOG(1,(THIS_FILE, "Invalid IP address")); |
| 818 |
goto on_error; |
| 819 |
} |
| 820 |
pj_sockaddr_set_port(&icedemo.rem.def_addr[1], (pj_uint16_t)port); |
| 821 |
|
| 822 |
} else if (strcmp(attr, "candidate")==0) { |
| 823 |
char *sdpcand = attr+strlen(attr)+1; |
| 824 |
int af, cnt; |
| 825 |
char foundation[32], transport[12], ipaddr[80], type[32]; |
| 826 |
pj_str_t tmpaddr; |
| 827 |
int comp_id, prio, port; |
| 828 |
pj_ice_sess_cand *cand; |
| 829 |
pj_status_t status; |
| 830 |
|
| 831 |
cnt = sscanf(sdpcand, "%s %d %s %d %s %d typ %s", |
| 832 |
foundation, |
| 833 |
&comp_id, |
| 834 |
transport, |
| 835 |
&prio, |
| 836 |
ipaddr, |
| 837 |
&port, |
| 838 |
type); |
| 839 |
if (cnt != 7) { |
| 840 |
PJ_LOG(1, (THIS_FILE, "error: Invalid ICE candidate line")); |
| 841 |
goto on_error; |
| 842 |
} |
| 843 |
|
| 844 |
cand = &icedemo.rem.cand[icedemo.rem.cand_cnt]; |
| 845 |
pj_bzero(cand, sizeof(*cand)); |
| 846 |
|
| 847 |
if (strcmp(type, "host")==0) |
| 848 |
cand->type = PJ_ICE_CAND_TYPE_HOST; |
| 849 |
else if (strcmp(type, "srflx")==0) |
| 850 |
cand->type = PJ_ICE_CAND_TYPE_SRFLX; |
| 851 |
else if (strcmp(type, "relay")==0) |
| 852 |
cand->type = PJ_ICE_CAND_TYPE_RELAYED; |
| 853 |
else { |
| 854 |
PJ_LOG(1, (THIS_FILE, "Error: invalid candidate type '%s'", |
| 855 |
type)); |
| 856 |
goto on_error; |
| 857 |
} |
| 858 |
|
| 859 |
cand->comp_id = (pj_uint8_t)comp_id; |
| 860 |
pj_strdup2(icedemo.pool, &cand->foundation, foundation); |
| 861 |
cand->prio = prio; |
| 862 |
|
| 863 |
if (strchr(ipaddr, ':')) |
| 864 |
af = pj_AF_INET6(); |
| 865 |
else |
| 866 |
af = pj_AF_INET(); |
| 867 |
|
| 868 |
tmpaddr = pj_str(ipaddr); |
| 869 |
pj_sockaddr_init(af, &cand->addr, NULL, 0); |
| 870 |
status = pj_sockaddr_set_str_addr(af, &cand->addr, &tmpaddr); |
| 871 |
if (status != PJ_SUCCESS) { |
| 872 |
PJ_LOG(1,(THIS_FILE, "Error: invalid IP address '%s'", |
| 873 |
ipaddr)); |
| 874 |
goto on_error; |
| 875 |
} |
| 876 |
|
| 877 |
pj_sockaddr_set_port(&cand->addr, (pj_uint16_t)port); |
| 878 |
|
| 879 |
++icedemo.rem.cand_cnt; |
| 880 |
|
| 881 |
if (cand->comp_id > icedemo.rem.comp_cnt) |
| 882 |
icedemo.rem.comp_cnt = cand->comp_id; |
| 883 |
} |
| 884 |
} |
| 885 |
break; |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
if (icedemo.rem.cand_cnt==0 || |
| 890 |
icedemo.rem.ufrag[0]==0 || |
| 891 |
icedemo.rem.pwd[0]==0 || |
| 892 |
icedemo.rem.comp_cnt == 0) |
| 893 |
{ |
| 894 |
PJ_LOG(1, (THIS_FILE, "Error: not enough info")); |
| 895 |
goto on_error; |
| 896 |
} |
| 897 |
|
| 898 |
if (comp0_port==0 || comp0_addr[0]=='\0') { |
| 899 |
PJ_LOG(1, (THIS_FILE, "Error: default address for component 0 not found")); |
| 900 |
goto on_error; |
| 901 |
} else { |
| 902 |
int af; |
| 903 |
pj_str_t tmp_addr; |
| 904 |
pj_status_t status; |
| 905 |
|
| 906 |
if (strchr(comp0_addr, ':')) |
| 907 |
af = pj_AF_INET6(); |
| 908 |
else |
| 909 |
af = pj_AF_INET(); |
| 910 |
|
| 911 |
pj_sockaddr_init(af, &icedemo.rem.def_addr[0], NULL, 0); |
| 912 |
tmp_addr = pj_str(comp0_addr); |
| 913 |
status = pj_sockaddr_set_str_addr(af, &icedemo.rem.def_addr[0], |
| 914 |
&tmp_addr); |
| 915 |
if (status != PJ_SUCCESS) { |
| 916 |
PJ_LOG(1,(THIS_FILE, "Invalid IP address in c= line")); |
| 917 |
goto on_error; |
| 918 |
} |
| 919 |
pj_sockaddr_set_port(&icedemo.rem.def_addr[0], (pj_uint16_t)comp0_port); |
| 920 |
} |
| 921 |
|
| 922 |
PJ_LOG(3, (THIS_FILE, "Done, %d remote candidate(s) added", |
| 923 |
icedemo.rem.cand_cnt)); |
| 924 |
return; |
| 925 |
|
| 926 |
on_error: |
| 927 |
reset_rem_info(); |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
static void icedemo_start_nego(void) |
| 935 |
{ |
| 936 |
pj_str_t rufrag, rpwd; |
| 937 |
pj_status_t status; |
| 938 |
|
| 939 |
if (icedemo.icest == NULL) { |
| 940 |
PJ_LOG(1,(THIS_FILE, "Error: No ICE instance, create it first")); |
| 941 |
return; |
| 942 |
} |
| 943 |
|
| 944 |
if (!pj_ice_strans_has_sess(icedemo.icest)) { |
| 945 |
PJ_LOG(1,(THIS_FILE, "Error: No ICE session, initialize first")); |
| 946 |
return; |
| 947 |
} |
| 948 |
|
| 949 |
if (icedemo.rem.cand_cnt == 0) { |
| 950 |
PJ_LOG(1,(THIS_FILE, "Error: No remote info, input remote info first")); |
| 951 |
return; |
| 952 |
} |
| 953 |
|
| 954 |
PJ_LOG(3,(THIS_FILE, "Starting ICE negotiation..")); |
| 955 |
|
| 956 |
status = pj_ice_strans_start_ice(icedemo.icest, |
| 957 |
pj_cstr(&rufrag, icedemo.rem.ufrag), |
| 958 |
pj_cstr(&rpwd, icedemo.rem.pwd), |
| 959 |
icedemo.rem.cand_cnt, |
| 960 |
icedemo.rem.cand); |
| 961 |
if (status != PJ_SUCCESS) |
| 962 |
icedemo_perror("Error starting ICE", status); |
| 963 |
else |
| 964 |
PJ_LOG(3,(THIS_FILE, "ICE negotiation started")); |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
static void icedemo_send_data(unsigned comp_id, const char *data) |
| 972 |
{ |
| 973 |
pj_status_t status; |
| 974 |
|
| 975 |
if (icedemo.icest == NULL) { |
| 976 |
PJ_LOG(1,(THIS_FILE, "Error: No ICE instance, create it first")); |
| 977 |
return; |
| 978 |
} |
| 979 |
|
| 980 |
if (!pj_ice_strans_has_sess(icedemo.icest)) { |
| 981 |
PJ_LOG(1,(THIS_FILE, "Error: No ICE session, initialize first")); |
| 982 |
return; |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
if (comp_id<1||comp_id>pj_ice_strans_get_running_comp_cnt(icedemo.icest)) { |
| 993 |
PJ_LOG(1,(THIS_FILE, "Error: invalid component ID")); |
| 994 |
return; |
| 995 |
} |
| 996 |
|
| 997 |
status = pj_ice_strans_sendto2(icedemo.icest, comp_id, data, strlen(data), |
| 998 |
&icedemo.rem.def_addr[comp_id-1], |
| 999 |
pj_sockaddr_get_len(&icedemo.rem.def_addr[comp_id-1])); |
| 1000 |
if (status != PJ_SUCCESS && status != PJ_EPENDING) |
| 1001 |
icedemo_perror("Error sending data", status); |
| 1002 |
else |
| 1003 |
PJ_LOG(3,(THIS_FILE, "Data sent/will be sent")); |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
static void icedemo_help_menu(void) |
| 1011 |
{ |
| 1012 |
puts(""); |
| 1013 |
puts("-= Help on using ICE and this icedemo program =-"); |
| 1014 |
puts(""); |
| 1015 |
puts("This application demonstrates how to use ICE in pjnath without having\n" |
| 1016 |
"to use the SIP protocol. To use this application, you will need to run\n" |
| 1017 |
"two instances of this application, to simulate two ICE agents.\n"); |
| 1018 |
|
| 1019 |
puts("Basic ICE flow:\n" |
| 1020 |
" create instance [menu \"c\"]\n" |
| 1021 |
" repeat these steps as wanted:\n" |
| 1022 |
" - init session as offerer or answerer [menu \"i\"]\n" |
| 1023 |
" - display our SDP [menu \"s\"]\n" |
| 1024 |
" - \"send\" our SDP from the \"show\" output above to remote, by\n" |
| 1025 |
" copy-pasting the SDP to the other icedemo application\n" |
| 1026 |
" - parse remote SDP, by pasting SDP generated by the other icedemo\n" |
| 1027 |
" instance [menu \"r\"]\n" |
| 1028 |
" - begin ICE negotiation in our end [menu \"b\"], and \n" |
| 1029 |
" - immediately begin ICE negotiation in the other icedemo instance\n" |
| 1030 |
" - ICE negotiation will run, and result will be printed to screen\n" |
| 1031 |
" - send application data to remote [menu \"x\"]\n" |
| 1032 |
" - end/stop ICE session [menu \"e\"]\n" |
| 1033 |
" destroy instance [menu \"d\"]\n" |
| 1034 |
""); |
| 1035 |
|
| 1036 |
puts(""); |
| 1037 |
puts("This concludes the help screen."); |
| 1038 |
puts(""); |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
static void icedemo_print_menu(void) |
| 1046 |
{ |
| 1047 |
puts(""); |
| 1048 |
puts("+----------------------------------------------------------------------+"); |
| 1049 |
puts("| M E N U |"); |
| 1050 |
puts("+---+------------------------------------------------------------------+"); |
| 1051 |
puts("| c | create Create the instance |"); |
| 1052 |
puts("| d | destroy Destroy the instance |"); |
| 1053 |
puts("| i | init o|a Initialize ICE session as offerer or answerer |"); |
| 1054 |
puts("| e | stop End/stop ICE session |"); |
| 1055 |
puts("| s | show Display local ICE info |"); |
| 1056 |
puts("| r | remote Input remote ICE info |"); |
| 1057 |
puts("| b | start Begin ICE negotiation |"); |
| 1058 |
puts("| x | send <compid> .. Send data to remote |"); |
| 1059 |
puts("+---+------------------------------------------------------------------+"); |
| 1060 |
puts("| h | help * Help! * |"); |
| 1061 |
puts("| q | quit Quit |"); |
| 1062 |
puts("+----------------------------------------------------------------------+"); |
| 1063 |
} |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
static void icedemo_console(void) |
| 1070 |
{ |
| 1071 |
pj_bool_t app_quit = PJ_FALSE; |
| 1072 |
|
| 1073 |
while (!app_quit) { |
| 1074 |
char input[80], *cmd; |
| 1075 |
const char *SEP = " \t\r\n"; |
| 1076 |
pj_size_t len; |
| 1077 |
|
| 1078 |
icedemo_print_menu(); |
| 1079 |
|
| 1080 |
printf("Input: "); |
| 1081 |
if (stdout) fflush(stdout); |
| 1082 |
|
| 1083 |
pj_bzero(input, sizeof(input)); |
| 1084 |
if (fgets(input, sizeof(input), stdin) == NULL) |
| 1085 |
break; |
| 1086 |
|
| 1087 |
len = strlen(input); |
| 1088 |
while (len && (input[len-1]=='\r' || input[len-1]=='\n')) |
| 1089 |
input[--len] = '\0'; |
| 1090 |
|
| 1091 |
cmd = strtok(input, SEP); |
| 1092 |
if (!cmd) |
| 1093 |
continue; |
| 1094 |
|
| 1095 |
if (strcmp(cmd, "create")==0 || strcmp(cmd, "c")==0) { |
| 1096 |
|
| 1097 |
icedemo_create_instance(); |
| 1098 |
|
| 1099 |
} else if (strcmp(cmd, "destroy")==0 || strcmp(cmd, "d")==0) { |
| 1100 |
|
| 1101 |
icedemo_destroy_instance(); |
| 1102 |
|
| 1103 |
} else if (strcmp(cmd, "init")==0 || strcmp(cmd, "i")==0) { |
| 1104 |
|
| 1105 |
char *role = strtok(NULL, SEP); |
| 1106 |
if (role) |
| 1107 |
icedemo_init_session(*role); |
| 1108 |
else |
| 1109 |
puts("error: Role required"); |
| 1110 |
|
| 1111 |
} else if (strcmp(cmd, "stop")==0 || strcmp(cmd, "e")==0) { |
| 1112 |
|
| 1113 |
icedemo_stop_session(); |
| 1114 |
|
| 1115 |
} else if (strcmp(cmd, "show")==0 || strcmp(cmd, "s")==0) { |
| 1116 |
|
| 1117 |
icedemo_show_ice(); |
| 1118 |
|
| 1119 |
} else if (strcmp(cmd, "remote")==0 || strcmp(cmd, "r")==0) { |
| 1120 |
|
| 1121 |
icedemo_input_remote(); |
| 1122 |
|
| 1123 |
} else if (strcmp(cmd, "start")==0 || strcmp(cmd, "b")==0) { |
| 1124 |
|
| 1125 |
icedemo_start_nego(); |
| 1126 |
|
| 1127 |
} else if (strcmp(cmd, "send")==0 || strcmp(cmd, "x")==0) { |
| 1128 |
|
| 1129 |
char *comp = strtok(NULL, SEP); |
| 1130 |
|
| 1131 |
if (!comp) { |
| 1132 |
PJ_LOG(1,(THIS_FILE, "Error: component ID required")); |
| 1133 |
} else { |
| 1134 |
char *data = comp + strlen(comp) + 1; |
| 1135 |
if (!data) |
| 1136 |
data = ""; |
| 1137 |
icedemo_send_data(atoi(comp), data); |
| 1138 |
} |
| 1139 |
|
| 1140 |
} else if (strcmp(cmd, "help")==0 || strcmp(cmd, "h")==0) { |
| 1141 |
|
| 1142 |
icedemo_help_menu(); |
| 1143 |
|
| 1144 |
} else if (strcmp(cmd, "quit")==0 || strcmp(cmd, "q")==0) { |
| 1145 |
|
| 1146 |
app_quit = PJ_TRUE; |
| 1147 |
|
| 1148 |
} else { |
| 1149 |
|
| 1150 |
printf("Invalid command '%s'\n", cmd); |
| 1151 |
|
| 1152 |
} |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
static void icedemo_usage() |
| 1161 |
{ |
| 1162 |
puts("Usage: icedemo [optons]"); |
| 1163 |
printf("icedemo v%s by pjsip.org\n", pj_get_version()); |
| 1164 |
puts(""); |
| 1165 |
puts("General options:"); |
| 1166 |
puts(" --comp-cnt, -c N Component count (default=1)"); |
| 1167 |
puts(" --nameserver, -n IP Configure nameserver to activate DNS SRV"); |
| 1168 |
puts(" resolution"); |
| 1169 |
puts(" --max-host, -H N Set max number of host candidates to N"); |
| 1170 |
puts(" --regular, -R Use regular nomination (default aggressive)"); |
| 1171 |
puts(" --log-file, -L FILE Save output to log FILE"); |
| 1172 |
puts(" --help, -h Display this screen."); |
| 1173 |
puts(""); |
| 1174 |
puts("STUN related options:"); |
| 1175 |
puts(" --stun-srv, -s HOSTDOM Enable srflx candidate by resolving to STUN server."); |
| 1176 |
puts(" HOSTDOM may be a \"host_or_ip[:port]\" or a domain"); |
| 1177 |
puts(" name if DNS SRV resolution is used."); |
| 1178 |
puts(""); |
| 1179 |
puts("TURN related options:"); |
| 1180 |
puts(" --turn-srv, -t HOSTDOM Enable relayed candidate by using this TURN server."); |
| 1181 |
puts(" HOSTDOM may be a \"host_or_ip[:port]\" or a domain"); |
| 1182 |
puts(" name if DNS SRV resolution is used."); |
| 1183 |
puts(" --turn-tcp, -T Use TCP to connect to TURN server"); |
| 1184 |
puts(" --turn-username, -u UID Set TURN username of the credential to UID"); |
| 1185 |
puts(" --turn-password, -p PWD Set password of the credential to WPWD"); |
| 1186 |
puts(" --turn-fingerprint, -F Use fingerprint for outgoing TURN requests"); |
| 1187 |
puts(""); |
| 1188 |
} |
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
int main(int argc, char *argv[]) |
| 1195 |
{ |
| 1196 |
struct pj_getopt_option long_options[] = { |
| 1197 |
{ "comp-cnt", 1, 0, 'c'}, |
| 1198 |
{ "nameserver", 1, 0, 'n'}, |
| 1199 |
{ "max-host", 1, 0, 'H'}, |
| 1200 |
{ "help", 0, 0, 'h'}, |
| 1201 |
{ "stun-srv", 1, 0, 's'}, |
| 1202 |
{ "turn-srv", 1, 0, 't'}, |
| 1203 |
{ "turn-tcp", 0, 0, 'T'}, |
| 1204 |
{ "turn-username", 1, 0, 'u'}, |
| 1205 |
{ "turn-password", 1, 0, 'p'}, |
| 1206 |
{ "turn-fingerprint", 0, 0, 'F'}, |
| 1207 |
{ "regular", 0, 0, 'R'}, |
| 1208 |
{ "log-file", 1, 0, 'L'}, |
| 1209 |
}; |
| 1210 |
int c, opt_id; |
| 1211 |
pj_status_t status; |
| 1212 |
|
| 1213 |
icedemo.opt.comp_cnt = 1; |
| 1214 |
icedemo.opt.max_host = -1; |
| 1215 |
|
| 1216 |
while((c=pj_getopt_long(argc,argv, "c:n:s:t:u:p:H:L:hTFR", long_options, &opt_id))!=-1) { |
| 1217 |
switch (c) { |
| 1218 |
case 'c': |
| 1219 |
icedemo.opt.comp_cnt = atoi(pj_optarg); |
| 1220 |
if (icedemo.opt.comp_cnt < 1 || icedemo.opt.comp_cnt > PJ_ICE_MAX_COMP) { |
| 1221 |
puts("Invalid component count value"); |
| 1222 |
return 1; |
| 1223 |
} |
| 1224 |
break; |
| 1225 |
case 'n': |
| 1226 |
icedemo.opt.ns = pj_str(pj_optarg); |
| 1227 |
break; |
| 1228 |
case 'H': |
| 1229 |
icedemo.opt.max_host = atoi(pj_optarg); |
| 1230 |
break; |
| 1231 |
case 'h': |
| 1232 |
icedemo_usage(); |
| 1233 |
return 0; |
| 1234 |
case 's': |
| 1235 |
icedemo.opt.stun_srv = pj_str(pj_optarg); |
| 1236 |
break; |
| 1237 |
case 't': |
| 1238 |
icedemo.opt.turn_srv = pj_str(pj_optarg); |
| 1239 |
break; |
| 1240 |
case 'T': |
| 1241 |
icedemo.opt.turn_tcp = PJ_TRUE; |
| 1242 |
break; |
| 1243 |
case 'u': |
| 1244 |
icedemo.opt.turn_username = pj_str(pj_optarg); |
| 1245 |
break; |
| 1246 |
case 'p': |
| 1247 |
icedemo.opt.turn_password = pj_str(pj_optarg); |
| 1248 |
break; |
| 1249 |
case 'F': |
| 1250 |
icedemo.opt.turn_fingerprint = PJ_TRUE; |
| 1251 |
break; |
| 1252 |
case 'R': |
| 1253 |
icedemo.opt.regular = PJ_TRUE; |
| 1254 |
break; |
| 1255 |
case 'L': |
| 1256 |
icedemo.opt.log_file = pj_optarg; |
| 1257 |
break; |
| 1258 |
default: |
| 1259 |
printf("Argument \"%s\" is not valid. Use -h to see help", |
| 1260 |
argv[pj_optind]); |
| 1261 |
return 1; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
status = icedemo_init(); |
| 1266 |
if (status != PJ_SUCCESS) |
| 1267 |
return 1; |
| 1268 |
|
| 1269 |
icedemo_console(); |
| 1270 |
|
| 1271 |
err_exit("Quitting..", PJ_SUCCESS); |
| 1272 |
return 0; |
| 1273 |
} |