/* * Create app */ static pj_status_t create_app(void) { pj_status_t status; status = pj_init(); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Error initializing pjlib", status); return status; } /* init PJLIB-UTIL: */ status = pjlib_util_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&app.cp, &pj_pool_factory_default_policy, CACHING_POOL_SIZE); /* Create application pool for misc. */ app.pool = pj_pool_create(&app.cp.factory, "app", 1000, 1000, NULL); /* Create the endpoint: */ status = pjsip_endpt_create(&app.cp.factory, pj_gethostname()->ptr, &app.sip_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); return status; }
static pj_status_t init_stack() { pj_sockaddr addr; pjsip_inv_callback inv_cb; pj_status_t status; pj_log_set_level(5); status = pj_init(); CHECK_STATUS(); pj_log_set_level(3); status = pjlib_util_init(); CHECK_STATUS(); pj_caching_pool_init(&app.cp, NULL, 0); app.pool = pj_pool_create( &app.cp.factory, "sipecho", 512, 512, 0); status = pjsip_endpt_create(&app.cp.factory, NULL, &app.sip_endpt); CHECK_STATUS(); pj_log_set_level(4); pj_sockaddr_init(AF, &addr, NULL, (pj_uint16_t)SIP_PORT); if (AF == pj_AF_INET()) { status = pjsip_udp_transport_start( app.sip_endpt, &addr.ipv4, NULL, 1, NULL); } else if (AF == pj_AF_INET6()) { status = pjsip_udp_transport_start6(app.sip_endpt, &addr.ipv6, NULL, 1, NULL); } else { status = PJ_EAFNOTSUP; } pj_log_set_level(3); CHECK_STATUS(); status = pjsip_tsx_layer_init_module(app.sip_endpt) || pjsip_ua_init_module( app.sip_endpt, NULL ); CHECK_STATUS(); pj_bzero(&inv_cb, sizeof(inv_cb)); inv_cb.on_state_changed = &call_on_state_changed; inv_cb.on_new_session = &call_on_forked; inv_cb.on_media_update = &call_on_media_update; inv_cb.on_rx_offer = &call_on_rx_offer; status = pjsip_inv_usage_init(app.sip_endpt, &inv_cb) || pjsip_100rel_init_module(app.sip_endpt) || pjsip_endpt_register_module( app.sip_endpt, &mod_sipecho) || pjsip_endpt_register_module( app.sip_endpt, &msg_logger) || //pjmedia_endpt_create(&app.cp.factory, // pjsip_endpt_get_ioqueue(app.sip_endpt), // 0, &app.med_endpt) || pj_thread_create(app.pool, "sipecho", &worker_proc, NULL, 0, 0, &app.worker_thread); CHECK_STATUS(); return PJ_SUCCESS; }
/* * main() */ int main(int argc, char *argv[]) { pj_caching_pool cp; pj_status_t status; if (argc < 2 || argc > 3) { puts("Usage: httpdemo URL [output-filename]"); return 1; } pj_log_set_level(5); pj_init(); pj_caching_pool_init(&cp, NULL, 0); mem = &cp.factory; pjlib_util_init(); if (argc > 2) f = fopen(argv[2], "wb"); else f = stdout; status = getURL(argv[1]); if (status != PJ_SUCCESS) { PJ_PERROR(1, (THIS_FILE, status, "Error")); } if (f != stdout) fclose(f); pj_caching_pool_destroy(&cp); pj_shutdown(); return 0; }
/* * main() */ int main(int argc, char *argv[]) { pj_caching_pool cp; pjmedia_endpt *med_ept; pjsip_endpoint *sip_ept; char errmsg[PJ_ERR_MSG_SIZE]; pj_status_t code; if (argc != 2) { puts("Usage: strerror ERRNUM"); return 1; } pj_log_set_level(3); pj_init(); pj_caching_pool_init(&cp, NULL, 0); pjlib_util_init(); pjnath_init(); pjmedia_endpt_create(&cp.factory, NULL, 0, &med_ept); pjsip_endpt_create(&cp.factory, "localhost", &sip_ept); pjsip_evsub_init_module(sip_ept); code = atoi(argv[1]); pj_strerror(code, errmsg, sizeof(errmsg)); printf("Status %d: %s\n", code, errmsg); pj_shutdown(); return 0; }
static int test_inner(void) { pj_caching_pool caching_pool; int rc = 0; mem = &caching_pool.factory; #if 1 pj_log_set_level(3); pj_log_set_decor(param_log_decor); PJ_UNUSED_ARG(test_log_func); #elif 1 log_file = fopen("pjnath-test.log", "wt"); pj_log_set_level(5); orig_log_func = pj_log_get_log_func(); pj_log_set_log_func(&test_log_func); #endif rc = pj_init(); if (rc != 0) { app_perror("pj_init() error!!", rc); return rc; } pj_dump_config(); pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0 ); pjlib_util_init(); pjnath_init(); #if INCLUDE_STUN_TEST DO_TEST(stun_test()); DO_TEST(sess_auth_test()); #endif #if INCLUDE_ICE_TEST DO_TEST(ice_test()); #endif #if INCLUDE_STUN_SOCK_TEST DO_TEST(stun_sock_test()); #endif #if INCLUDE_TURN_SOCK_TEST DO_TEST(turn_sock_test()); #endif #if INCLUDE_CONCUR_TEST DO_TEST(concur_test()); #endif on_return: if (log_file) fclose(log_file); return rc; }
static int test_inner(void) { pj_caching_pool caching_pool; int rc = 0; mem = &caching_pool.factory; pj_log_set_level(3); pj_log_set_decor(param_log_decor); rc = pj_init(); if (rc != 0) { app_perror("pj_init() error!!", rc); return rc; } rc = pjlib_util_init(); pj_assert(rc == 0); pj_dump_config(); pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0 ); #if INCLUDE_XML_TEST DO_TEST(xml_test()); #endif #if INCLUDE_JSON_TEST DO_TEST(json_test()); #endif #if INCLUDE_ENCRYPTION_TEST DO_TEST(encryption_test()); DO_TEST(encryption_benchmark()); #endif #if INCLUDE_STUN_TEST DO_TEST(stun_test()); #endif #if INCLUDE_RESOLVER_TEST DO_TEST(resolver_test()); #endif #if INCLUDE_HTTP_CLIENT_TEST DO_TEST(http_client_test()); #endif on_return: return rc; }
static pj_status_t init_stack(void) { pj_status_t status; /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Then init PJLIB-UTIL: */ status = pjlib_util_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&global.cp, &pj_pool_factory_default_policy, 0); /* Create the endpoint: */ status = pjsip_endpt_create(&global.cp.factory, NULL, &global.endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* Init transaction layer for stateful proxy only */ #if STATEFUL status = pjsip_tsx_layer_init_module(global.endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); #endif /* Create listening transport */ { pj_sockaddr_in addr; addr.sin_family = pj_AF_INET(); addr.sin_addr.s_addr = 0; addr.sin_port = pj_htons((pj_uint16_t)global.port); status = pjsip_udp_transport_start( global.endpt, &addr, NULL, 1, NULL); if (status != PJ_SUCCESS) return status; } /* Create pool for the application */ global.pool = pj_pool_create(&global.cp.factory, "proxyapp", 4000, 4000, NULL); /* Register the logger module */ pjsip_endpt_register_module(global.endpt, &mod_msg_logger); return PJ_SUCCESS; }
int main() { pj_turn_srv *srv; pj_turn_listener *listener; pj_status_t status; status = pj_init(); if (status != PJ_SUCCESS) return err("pj_init() error", status); pjlib_util_init(); pjnath_init(); pj_caching_pool_init(&g_cp, NULL, 0); pj_turn_auth_init(REALM); status = pj_turn_srv_create(&g_cp.factory, &srv); if (status != PJ_SUCCESS) return err("Error creating server", status); status = pj_turn_listener_create_udp(srv, pj_AF_INET(), NULL, TURN_PORT, 1, 0, &listener); if (status != PJ_SUCCESS) return err("Error creating UDP listener", status); #if PJ_HAS_TCP status = pj_turn_listener_create_tcp(srv, pj_AF_INET(), NULL, TURN_PORT, 1, 0, &listener); if (status != PJ_SUCCESS) return err("Error creating listener", status); #endif status = pj_turn_srv_add_listener(srv, listener); if (status != PJ_SUCCESS) return err("Error adding listener", status); puts("Server is running"); pj_log_set_level(LOG_LEVEL); console_main(srv); pj_turn_srv_destroy(srv); pj_caching_pool_destroy(&g_cp); pj_shutdown(); return 0; }
static int test_inner(void) { pj_caching_pool caching_pool; int rc = 0; mem = &caching_pool.factory; #if 1 pj_log_set_level(5); pj_log_set_decor(param_log_decor); #endif rc = pj_init(); if (rc != 0) { app_perror("pj_init() error!!", rc); return rc; } pj_dump_config(); pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0 ); pjlib_util_init(); pjnath_init(); #if INCLUDE_STUN_TEST DO_TEST(stun_test()); DO_TEST(sess_auth_test()); #endif #if INCLUDE_ICE_TEST DO_TEST(ice_test()); #endif #if INCLUDE_STUN_SOCK_TEST DO_TEST(stun_sock_test()); #endif #if INCLUDE_TURN_SOCK_TEST DO_TEST(turn_sock_test()); #endif on_return: return rc; }
pj_status_t init_pjsip() { pj_status_t status; // Must init PJLIB first: status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); // Dump PJLIB config to log file. pj_dump_config(); // Then init PJLIB-UTIL: status = pjlib_util_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); // Must create a pool factory before we can allocate any memory. pj_caching_pool_init(&stack_data.cp, &pj_pool_factory_default_policy, 0); // Create the endpoint. status = pjsip_endpt_create(&stack_data.cp.factory, NULL, &stack_data.endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); // Init transaction layer. status = pjsip_tsx_layer_init_module(stack_data.endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); // Create pool for the application stack_data.pool = pj_pool_create(&stack_data.cp.factory, "sprout-bono", 4000, 4000, NULL); status = register_custom_headers(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); // Enable deadlock detection on the message queue. rx_msg_q.set_deadlock_threshold(MSG_Q_DEADLOCK_TIME); return PJ_SUCCESS; }
static int init() { int i; pj_status_t status; CHECK( pj_init() ); CHECK( pjlib_util_init() ); CHECK( pjnath_init() ); /* Check that server is specified */ if (!o.srv_addr) { printf("Error: server must be specified\n"); return PJ_EINVAL; } pj_caching_pool_init(&g.cp, &pj_pool_factory_default_policy, 0); g.pool = pj_pool_create(&g.cp.factory, "main", 1000, 1000, NULL); /* Init global STUN config */ pj_stun_config_init(&g.stun_config, &g.cp.factory, 0, NULL, NULL); /* Create global timer heap */ CHECK( pj_timer_heap_create(g.pool, 1000, &g.stun_config.timer_heap) ); /* Create global ioqueue */ CHECK( pj_ioqueue_create(g.pool, 16, &g.stun_config.ioqueue) ); /* * Create peers */ for (i=0; i<(int)PJ_ARRAY_SIZE(g.peer); ++i) { pj_stun_sock_cb stun_sock_cb; char name[] = "peer0"; pj_uint16_t port; pj_stun_sock_cfg ss_cfg; pj_str_t server; pj_bzero(&stun_sock_cb, sizeof(stun_sock_cb)); stun_sock_cb.on_rx_data = &stun_sock_on_rx_data; stun_sock_cb.on_status = &stun_sock_on_status; g.peer[i].mapped_addr.addr.sa_family = pj_AF_INET(); pj_stun_sock_cfg_default(&ss_cfg); #if 1 /* make reading the log easier */ ss_cfg.ka_interval = 300; #endif name[strlen(name)-1] = '0'+i; status = pj_stun_sock_create(&g.stun_config, name, pj_AF_INET(), &stun_sock_cb, &ss_cfg, &g.peer[i], &g.peer[i].stun_sock); if (status != PJ_SUCCESS) { my_perror("pj_stun_sock_create()", status); return status; } if (o.stun_server) { server = pj_str(o.stun_server); port = PJ_STUN_PORT; } else { server = pj_str(o.srv_addr); port = (pj_uint16_t)(o.srv_port?atoi(o.srv_port):PJ_STUN_PORT); } status = pj_stun_sock_start(g.peer[i].stun_sock, &server, port, NULL); if (status != PJ_SUCCESS) { my_perror("pj_stun_sock_start()", status); return status; } } /* Start the worker thread */ CHECK( pj_thread_create(g.pool, "stun", &worker_thread, NULL, 0, 0, &g.thread) ); return PJ_SUCCESS; }
int main() { pj_caching_pool cp; pj_cli_cfg cli_cfg; pj_cli_telnet_cfg tcfg; pj_str_t xml; pj_status_t status; int i; pj_init(); pj_caching_pool_init(&cp, NULL, 0); pjlib_util_init(); /* * Create CLI app. */ pj_cli_cfg_default(&cli_cfg); cli_cfg.pf = &cp.factory; cli_cfg.name = pj_str("mycliapp"); cli_cfg.title = pj_str("My CLI Application"); status = pj_cli_create(&cli_cfg, &cli); if (status != PJ_SUCCESS) goto on_return; /* * Register some commands. */ for (i = 0; i < sizeof(cmd_xmls)/sizeof(cmd_xmls[0]); i++) { xml = pj_str(cmd_xmls[i].xml); status = pj_cli_add_cmd_from_xml(cli, NULL, &xml, cmd_xmls[i].handler, NULL, get_codec_list); if (status != PJ_SUCCESS) goto on_return; } /* * Start telnet daemon */ pj_cli_telnet_cfg_default(&tcfg); // tcfg.passwd = pj_str("pjsip"); #if USE_RANDOM_PORT tcfg.port = 0; #else tcfg.port = 2233; #endif tcfg.prompt_str = pj_str("CoolWater% "); status = pj_cli_telnet_create(cli, &tcfg, NULL); if (status != PJ_SUCCESS) goto on_return; /* * Run the system specific main loop. */ status = app_main(cli); on_return: /* * Destroy */ pj_cli_destroy(cli); cli = NULL; pj_caching_pool_destroy(&cp); pj_shutdown(); return (status != PJ_SUCCESS ? 1 : 0); }
int main() { pj_caching_pool cp; pj_cli_cfg cli_cfg; pj_cli_telnet_cfg tcfg; pj_str_t xml; pj_status_t status; pj_status_t cli_status; int i; pj_log_set_level(LOG_LEVEL); print_msg(("", "INITING .... \n")); status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); if (status != PJ_SUCCESS) { print_msg(("", "PJ INIT FAILED \n")); } pj_caching_pool_init(&cp, NULL, 0); pjlib_util_init(); /* * Create CLI app. */ pj_cli_cfg_default(&cli_cfg); print_msg(("", "INITING CLI CFG.... \n")); cli_cfg.pf = &cp.factory; cli_cfg.name = pj_str("tinysipcli"); cli_cfg.title = pj_str("TINYSIP"); cli_status = pj_cli_create(&cli_cfg, &cli); print_msg(("", "AFTER INITING CLI CFG.... \n")); if (cli_status != PJ_SUCCESS) { print_msg(("", "CLI FAILED \n")); goto on_return; } /* * Register some commands. */ for (i = 0; i < sizeof(cmd_xmls) / sizeof(cmd_xmls[0]); i++) { xml = pj_str(cmd_xmls[i].xml); // print_msg(("",cmd_xmls[i].xml)); status = pj_cli_add_cmd_from_xml(cli, NULL, &xml, cmd_xmls[i].handler, NULL, get_cmd_list); if (status != PJ_SUCCESS) continue; // goto on_return; } /* Create pjsua first! */ status = pjsua_create(); if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status); /* Init pjsua */ { pjsua_logging_config log_cfg; pjsua_logging_config_default(&log_cfg); log_cfg.console_level = LOG_LEVEL; pjsua_config cfg; pjsua_config_default(&cfg); cfg.cb.on_incoming_call = &on_incoming_call; cfg.cb.on_call_media_state = &on_call_media_state; cfg.cb.on_call_state = &on_call_state; status = pjsua_init(&cfg, &log_cfg, NULL); if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status); } /* Add UDP transport. */ { pjsua_transport_config cfg; pjsua_transport_config_default(&cfg); cfg.port = 5060; status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL); if (status != PJ_SUCCESS) error_exit("Error creating transport", status); } /* * Start telnet daemon */ pj_cli_telnet_cfg_default(&tcfg); // tcfg.passwd = pj_str("pjsip"); #if USE_RANDOM_PORT tcfg.port = 0; #else tcfg.port = 2233; #endif tcfg.prompt_str = pj_str("CMD % "); status = pj_cli_telnet_create(cli, &tcfg, NULL); if (status != PJ_SUCCESS) goto on_return; /* * Run the system specific main loop. */ status = app_main(cli); on_return: print_msg(("", "There're some issues, application is going down now!\n")); /* * Destroy */ pj_cli_destroy(cli); cli = NULL; pj_caching_pool_destroy(&cp); pj_shutdown(); return (status != PJ_SUCCESS ? 1 : 0); }
int main(int argc, char *argv[]) { pj_str_t input, output, srtp_crypto, srtp_key, codec; pjmedia_aud_dev_index dev_id = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV; pj_pcap_filter filter; pj_status_t status; enum { OPT_SRC_IP = 1, OPT_DST_IP, OPT_SRC_PORT, OPT_DST_PORT, OPT_CODEC, OPT_PLAY_DEV_ID }; struct pj_getopt_option long_options[] = { { "srtp-crypto", 1, 0, 'c' }, { "srtp-key", 1, 0, 'k' }, { "src-ip", 1, 0, OPT_SRC_IP }, { "dst-ip", 1, 0, OPT_DST_IP }, { "src-port", 1, 0, OPT_SRC_PORT }, { "dst-port", 1, 0, OPT_DST_PORT }, { "codec", 1, 0, OPT_CODEC }, { "play-dev-id", 1, 0, OPT_PLAY_DEV_ID }, { NULL, 0, 0, 0} }; int c; int option_index; char key_bin[32]; srtp_crypto.slen = srtp_key.slen = 0; codec.slen = 0; pj_pcap_filter_default(&filter); filter.link = PJ_PCAP_LINK_TYPE_ETH; filter.proto = PJ_PCAP_PROTO_TYPE_UDP; /* Parse arguments */ pj_optind = 0; while((c=pj_getopt_long(argc,argv, "c:k:", long_options, &option_index))!=-1) { switch (c) { case 'c': srtp_crypto = pj_str(pj_optarg); break; case 'k': { int key_len = sizeof(key_bin); srtp_key = pj_str(pj_optarg); if (pj_base64_decode(&srtp_key, (pj_uint8_t*)key_bin, &key_len)) { puts("Error: invalid key"); return 1; } srtp_key.ptr = key_bin; srtp_key.slen = key_len; } break; case OPT_SRC_IP: { pj_str_t t = pj_str(pj_optarg); pj_in_addr a = pj_inet_addr(&t); filter.ip_src = a.s_addr; } break; case OPT_DST_IP: { pj_str_t t = pj_str(pj_optarg); pj_in_addr a = pj_inet_addr(&t); filter.ip_dst = a.s_addr; } break; case OPT_SRC_PORT: filter.src_port = pj_htons((pj_uint16_t)atoi(pj_optarg)); break; case OPT_DST_PORT: filter.dst_port = pj_htons((pj_uint16_t)atoi(pj_optarg)); break; case OPT_CODEC: codec = pj_str(pj_optarg); break; case OPT_PLAY_DEV_ID: dev_id = atoi(pj_optarg); break; default: puts("Error: invalid option"); return 1; } } if (pj_optind != argc - 2) { puts(USAGE); return 1; } if (!(srtp_crypto.slen) != !(srtp_key.slen)) { puts("Error: both SRTP crypto and key must be specified"); puts(USAGE); return 1; } input = pj_str(argv[pj_optind]); output = pj_str(argv[pj_optind+1]); T( pj_init() ); pj_caching_pool_init(&app.cp, NULL, 0); app.pool = pj_pool_create(&app.cp.factory, "pcaputil", 1000, 1000, NULL); T( pjlib_util_init() ); T( pjmedia_endpt_create(&app.cp.factory, NULL, 0, &app.mept) ); T( pj_pcap_open(app.pool, input.ptr, &app.pcap) ); T( pj_pcap_set_filter(app.pcap, &filter) ); pcap2wav(&codec, &output, dev_id, &srtp_crypto, &srtp_key); cleanup(); return 0; }
int krx_ice_init(krx_ice* k) { pj_status_t r; if(!k) { return -1; } /* initialize pj */ r = pj_init(); CHECK_PJ_STATUS(r, "Error: cannot initialize pj.\n", -2); r = pjlib_util_init(); CHECK_PJ_STATUS(r, "Error: cannot initialize pj-util.\n", -3); r = pjnath_init(); CHECK_PJ_STATUS(r, "Error: cannot initialize pjnath.\n", -4); /* create memory pool */ pj_caching_pool_init(&k->caching_pool, NULL, 0); /* initialize the ice settings */ pj_ice_strans_cfg_default(&k->ice_cfg); /* create the pool */ k->pool = pj_pool_create(&k->caching_pool.factory, "krx_ice_pjnath", 512, 512, NULL); /* 512 = initial size, 512 = incremental size */ if(!k->pool) { printf("Error: cannot create pool.\n"); return -5; } k->ice_cfg.stun_cfg.pf = &k->caching_pool.factory; /* create heap for timers */ r = pj_timer_heap_create(k->pool, 100, &k->ice_cfg.stun_cfg.timer_heap); CHECK_PJ_STATUS(r, "Error: cannot create timer heap.\n", -6); /* create ioqueue for network I/O */ r = pj_ioqueue_create(k->pool, 16, &k->ice_cfg.stun_cfg.ioqueue); CHECK_PJ_STATUS(r, "Error: cannot create ioqueue.\n", -7); /* create managing thread */ r = pj_thread_create(k->pool, "krx_ice_pjnath", &krx_ice_worker_thread, k, 0, 0, &k->thread); CHECK_PJ_STATUS(r, "Error: cannot create managing thread.", -8); k->ice_cfg.af = pj_AF_INET(); /* @todo(roxlu): we could add a nameserver */ k->ice_cfg.opt.aggressive = PJ_FALSE; /* @todo(roxlu): read up the aggressive flag in ice_cfg. */ /* default configs */ k->max_hosts = 4; k->ncomp = 4; /* initialize the callbacks */ pj_bzero(&k->ice_cb, sizeof(k->ice_cb)); k->ice_cb.on_rx_data = krx_ice_on_rx_data; k->ice_cb.on_ice_complete = krx_ice_on_complete; /* sdp info */ k->ice_ufrag = NULL; k->ice_pwd = NULL; return 0; }
int test_main(void) { pj_status_t rc; pj_caching_pool caching_pool; const char *filename; unsigned tsx_test_cnt=0; struct tsx_test_param tsx_test[10]; pj_status_t status; #if INCLUDE_TSX_TEST unsigned i; pjsip_transport *tp; #if PJ_HAS_TCP pjsip_tpfactory *tpfactory; #endif /* PJ_HAS_TCP */ #endif /* INCLUDE_TSX_TEST */ int line; pj_log_set_level(log_level); pj_log_set_decor(param_log_decor); if ((rc=pj_init()) != PJ_SUCCESS) { app_perror("pj_init", rc); return rc; } if ((rc=pjlib_util_init()) != PJ_SUCCESS) { app_perror("pj_init", rc); return rc; } status = init_report(); if (status != PJ_SUCCESS) return status; pj_dump_config(); pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, PJSIP_TEST_MEM_SIZE ); rc = pjsip_endpt_create(&caching_pool.factory, "endpt", &endpt); if (rc != PJ_SUCCESS) { app_perror("pjsip_endpt_create", rc); pj_caching_pool_destroy(&caching_pool); return rc; } PJ_LOG(3,(THIS_FILE,"")); /* Init logger module. */ init_msg_logger(); msg_logger_set_enabled(1); /* Start transaction layer module. */ rc = pjsip_tsx_layer_init_module(endpt); if (rc != PJ_SUCCESS) { app_perror(" Error initializing transaction module", rc); goto on_return; } /* Create loop transport. */ rc = pjsip_loop_start(endpt, NULL); if (rc != PJ_SUCCESS) { app_perror(" error: unable to create datagram loop transport", rc); goto on_return; } tsx_test[tsx_test_cnt].port = 5060; tsx_test[tsx_test_cnt].tp_type = "loop-dgram"; tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_LOOP_DGRAM; ++tsx_test_cnt; #if INCLUDE_URI_TEST DO_TEST(uri_test()); #endif #if INCLUDE_MSG_TEST DO_TEST(msg_test()); DO_TEST(msg_err_test()); #endif #if INCLUDE_MULTIPART_TEST DO_TEST(multipart_test()); #endif #if INCLUDE_TXDATA_TEST DO_TEST(txdata_test()); #endif #if INCLUDE_TSX_BENCH DO_TEST(tsx_bench()); #endif #if INCLUDE_UDP_TEST DO_TEST(transport_udp_test()); #endif #if INCLUDE_LOOP_TEST DO_TEST(transport_loop_test()); #endif #if INCLUDE_TCP_TEST DO_TEST(transport_tcp_test()); #endif #if INCLUDE_RESOLVE_TEST DO_TEST(resolve_test()); #endif #if INCLUDE_TSX_TEST status = pjsip_udp_transport_start(endpt, NULL, NULL, 1, &tp); if (status == PJ_SUCCESS) { tsx_test[tsx_test_cnt].port = tp->local_name.port; tsx_test[tsx_test_cnt].tp_type = "udp"; tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_UDP; ++tsx_test_cnt; } #if PJ_HAS_TCP status = pjsip_tcp_transport_start(endpt, NULL, 1, &tpfactory); if (status == PJ_SUCCESS) { tsx_test[tsx_test_cnt].port = tpfactory->addr_name.port; tsx_test[tsx_test_cnt].tp_type = "tcp"; tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_TCP; ++tsx_test_cnt; } else { app_perror("Unable to create TCP", status); rc = -4; goto on_return; } #endif for (i=0; i<tsx_test_cnt; ++i) { DO_TSX_TEST(tsx_basic_test, &tsx_test[i]); DO_TSX_TEST(tsx_uac_test, &tsx_test[i]); DO_TSX_TEST(tsx_uas_test, &tsx_test[i]); } #endif #if INCLUDE_INV_OA_TEST DO_TEST(inv_offer_answer_test()); #endif #if INCLUDE_REGC_TEST DO_TEST(regc_test()); #endif on_return: flush_events(500); /* Dumping memory pool usage */ PJ_LOG(3,(THIS_FILE, "Peak memory size=%u MB", caching_pool.peak_used_size / 1000000)); pjsip_endpt_destroy(endpt); pj_caching_pool_destroy(&caching_pool); PJ_LOG(3,(THIS_FILE, "")); pj_thread_get_stack_info(pj_thread_this(), &filename, &line); PJ_LOG(3,(THIS_FILE, "Stack max usage: %u, deepest: %s:%u", pj_thread_get_stack_max_usage(pj_thread_this()), filename, line)); if (rc == 0) PJ_LOG(3,(THIS_FILE, "Looks like everything is okay!..")); else PJ_LOG(3,(THIS_FILE, "Test completed with error(s)")); report_ival("test-status", rc, "", "Overall test status/result (0==success)"); close_report(); return rc; }
/* * main() * */ int main(int argc, char *argv[]) { pj_caching_pool cp; pj_thread_t *thread; pj_pool_t *pool; pj_status_t status; if (argc != 2) { puts("Error: destination URL needed"); return 0; } /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Then init PJLIB-UTIL: */ status = pjlib_util_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); /* Create the endpoint: */ status = pjsip_endpt_create(&cp.factory, "sipstateless", &sip_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Add UDP transport, with hard-coded port */ { pj_sockaddr_in addr; addr.sin_family = pj_AF_INET(); addr.sin_addr.s_addr = 0; addr.sin_port = pj_htons(PORT); status = pjsip_udp_transport_start( sip_endpt, &addr, NULL, 1, NULL); if (status != PJ_SUCCESS) { PJ_LOG(3,(THIS_FILE, "Error starting UDP transport (port in use?)")); return 1; } } status = pjsip_tsx_layer_init_module(sip_endpt); pj_assert(status == PJ_SUCCESS); status = pjsip_ua_init_module(sip_endpt, NULL); pj_assert(status == PJ_SUCCESS); /* * Register our module to receive incoming requests. */ status = pjsip_endpt_register_module( sip_endpt, &mod_app); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); pool = pjsip_endpt_create_pool(sip_endpt, "", 1000, 1000); status = pj_thread_create(pool, "", &worker_thread, NULL, 0, 0, &thread); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); printf("Destination URL: %s\n", argv[1]); for (;;) { char line[10]; fgets(line, sizeof(line), stdin); switch (line[0]) { case 'm': make_call(argv[1], PJ_FALSE); break; case 'M': make_call(argv[1], PJ_TRUE); break; case 'r': reinvite(PJ_FALSE); break; case 'R': reinvite(PJ_TRUE); break; case 'h': hangup(); break; case 'q': goto on_quit; } } on_quit: quit_flag = 1; pj_thread_join(thread); pjsip_endpt_destroy(sip_endpt); pj_caching_pool_destroy(&cp); pj_shutdown(); return 0; }
/* Esta función es invocada desde MAIN=>Plugin, al igual que en TCP Es invocada desde un thread. Básicamente lo que hace es bootear PJSIP y luego se queda loopeando eventos SIP (con un pseudo polling que tiene la LIB) Adicionalmente lanza el thread para controlar los eventos TUN (el tráfico es bidireccional) */ int sip_start(int argc,char **argv) { pj_status_t status; /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); pj_log_set_level(5); /* Then init PJLIB-UTIL: */ status = pjlib_util_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); /* Create global endpoint: */ { const pj_str_t *hostname; const char *endpt_name; /* Endpoint MUST be assigned a globally unique name. * The name will be used as the hostname in Warning header. */ /* For this implementation, we'll use hostname for simplicity */ hostname = pj_gethostname(); endpt_name = hostname->ptr; /* Create the endpoint: */ status = pjsip_endpt_create(&cp.factory, endpt_name, &g_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); } /* * Add UDP transport, with hard-coded port * Alternatively, application can use pjsip_udp_transport_attach() to * start UDP transport, if it already has an UDP socket (e.g. after it * resolves the address with STUN). */ { pj_sockaddr addr; pj_sockaddr_init(AF, &addr, NULL, (pj_uint16_t)SIP_PORT); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to start UDP transport", status); return 1; } } /* * Init transaction layer. * This will create/initialize transaction hash tables etc. */ status = pjsip_tsx_layer_init_module(g_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Initialize UA layer module. * This will create/initialize dialog hash tables etc. */ status = pjsip_ua_init_module( g_endpt, NULL ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Esto seguramente no haga falta nada de esto de INVITE's ... */ /* * Init invite session module. * The invite session module initialization takes additional argument, * i.e. a structure containing callbacks to be called on specific * occurence of events. * * The on_state_changed and on_new_session callbacks are mandatory. * Application must supply the callback function. * * We use on_media_update() callback in this application to start * media transmission. */ { pjsip_inv_callback inv_cb; /* Init the callback for INVITE session: */ pj_bzero(&inv_cb, sizeof(inv_cb)); inv_cb.on_state_changed = &call_on_state_changed; inv_cb.on_new_session = &call_on_forked; // inv_cb.on_media_update = &call_on_media_update; /* Initialize invite session module: */ status = pjsip_inv_usage_init(g_endpt, &inv_cb); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); } /* Initialize 100rel support */ status = pjsip_100rel_init_module(g_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* * Register our module to receive incoming requests. */ status = pjsip_endpt_register_module( g_endpt, &mod_simpleua); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Register message logger module. */ status = pjsip_endpt_register_module( g_endpt, &msg_logger); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); debug(1,"SIP Plugin launching thread for looping events"); pthread_create( &sip_plugin_event_thread, NULL, sip_loop_sip_events, NULL); /* Aqui es donde estamos esperando a recibir datos del PIPE (que viene del TUN DRIVER) hacemos I/O BLOCK con select */ sip_loop_tun_events(); // Aqui llegamos cuando ha muerto todo ;) return 0; }
/* * main() * * If called with argument, treat argument as SIP URL to be called. * Otherwise wait for incoming calls. */ int main(int argc, char *argv[]) { pj_pool_t *pool = NULL; pj_status_t status; unsigned i; /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); pj_log_set_level(5); /* Then init PJLIB-UTIL: */ status = pjlib_util_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); /* Create global endpoint: */ { const pj_str_t *hostname; const char *endpt_name; /* Endpoint MUST be assigned a globally unique name. * The name will be used as the hostname in Warning header. */ /* For this implementation, we'll use hostname for simplicity */ hostname = pj_gethostname(); endpt_name = hostname->ptr; /* Create the endpoint: */ status = pjsip_endpt_create(&cp.factory, endpt_name, &g_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); } /* * Add UDP transport, with hard-coded port * Alternatively, application can use pjsip_udp_transport_attach() to * start UDP transport, if it already has an UDP socket (e.g. after it * resolves the address with STUN). */ { pj_sockaddr addr; pj_sockaddr_init(AF, &addr, NULL, (pj_uint16_t)SIP_PORT); if (AF == pj_AF_INET()) { status = pjsip_udp_transport_start( g_endpt, &addr.ipv4, NULL, 1, NULL); } else if (AF == pj_AF_INET6()) { status = pjsip_udp_transport_start6(g_endpt, &addr.ipv6, NULL, 1, NULL); } else { status = PJ_EAFNOTSUP; } if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to start UDP transport", status); return 1; } } /* * Init transaction layer. * This will create/initialize transaction hash tables etc. */ status = pjsip_tsx_layer_init_module(g_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Initialize UA layer module. * This will create/initialize dialog hash tables etc. */ status = pjsip_ua_init_module( g_endpt, NULL ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Init invite session module. * The invite session module initialization takes additional argument, * i.e. a structure containing callbacks to be called on specific * occurence of events. * * The on_state_changed and on_new_session callbacks are mandatory. * Application must supply the callback function. * * We use on_media_update() callback in this application to start * media transmission. */ { pjsip_inv_callback inv_cb; /* Init the callback for INVITE session: */ pj_bzero(&inv_cb, sizeof(inv_cb)); inv_cb.on_state_changed = &call_on_state_changed; inv_cb.on_new_session = &call_on_forked; inv_cb.on_media_update = &call_on_media_update; /* Initialize invite session module: */ status = pjsip_inv_usage_init(g_endpt, &inv_cb); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); } /* Initialize 100rel support */ status = pjsip_100rel_init_module(g_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* * Register our module to receive incoming requests. */ status = pjsip_endpt_register_module( g_endpt, &mod_simpleua); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Register message logger module. */ status = pjsip_endpt_register_module( g_endpt, &msg_logger); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Initialize media endpoint. * This will implicitly initialize PJMEDIA too. */ #if PJ_HAS_THREADS status = pjmedia_endpt_create(&cp.factory, NULL, 1, &g_med_endpt); #else status = pjmedia_endpt_create(&cp.factory, pjsip_endpt_get_ioqueue(g_endpt), 0, &g_med_endpt); #endif PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Add PCMA/PCMU codec to the media endpoint. */ #if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0 status = pjmedia_codec_g711_init(g_med_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); #endif #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) /* Init video subsystem */ pool = pjmedia_endpt_create_pool(g_med_endpt, "Video subsystem", 512, 512); status = pjmedia_video_format_mgr_create(pool, 64, 0, NULL); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); status = pjmedia_converter_mgr_create(pool, NULL); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); status = pjmedia_vid_codec_mgr_create(pool, NULL); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); status = pjmedia_vid_dev_subsys_init(&cp.factory); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); # if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC!=0 /* Init ffmpeg video codecs */ status = pjmedia_codec_ffmpeg_vid_init(NULL, &cp.factory); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); # endif /* PJMEDIA_HAS_FFMPEG_VID_CODEC */ #endif /* PJMEDIA_HAS_VIDEO */ /* * Create media transport used to send/receive RTP/RTCP socket. * One media transport is needed for each call. Application may * opt to re-use the same media transport for subsequent calls. */ for (i = 0; i < PJ_ARRAY_SIZE(g_med_transport); ++i) { status = pjmedia_transport_udp_create3(g_med_endpt, AF, NULL, NULL, RTP_PORT + i*2, 0, &g_med_transport[i]); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create media transport", status); return 1; } /* * Get socket info (address, port) of the media transport. We will * need this info to create SDP (i.e. the address and port info in * the SDP). */ pjmedia_transport_info_init(&g_med_tpinfo[i]); pjmedia_transport_get_info(g_med_transport[i], &g_med_tpinfo[i]); pj_memcpy(&g_sock_info[i], &g_med_tpinfo[i].sock_info, sizeof(pjmedia_sock_info)); } /* * If URL is specified, then make call immediately. */ if (argc > 1) { pj_sockaddr hostaddr; char hostip[PJ_INET6_ADDRSTRLEN+2]; char temp[80]; pj_str_t dst_uri = pj_str(argv[1]); pj_str_t local_uri; pjsip_dialog *dlg; pjmedia_sdp_session *local_sdp; pjsip_tx_data *tdata; if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to retrieve local host IP", status); return 1; } pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2); pj_ansi_sprintf(temp, "<sip:simpleuac@%s:%d>", hostip, SIP_PORT); local_uri = pj_str(temp); /* Create UAC dialog */ status = pjsip_dlg_create_uac( pjsip_ua_instance(), &local_uri, /* local URI */ &local_uri, /* local Contact */ &dst_uri, /* remote URI */ &dst_uri, /* remote target */ &dlg); /* dialog */ if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create UAC dialog", status); return 1; } /* If we expect the outgoing INVITE to be challenged, then we should * put the credentials in the dialog here, with something like this: * { pjsip_cred_info cred[1]; cred[0].realm = pj_str("sip.server.realm"); cred[0].scheme = pj_str("digest"); cred[0].username = pj_str("theuser"); cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; cred[0].data = pj_str("thepassword"); pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred); } * */ /* Get the SDP body to be put in the outgoing INVITE, by asking * media endpoint to create one for us. */ status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */ dlg->pool, /* pool. */ MAX_MEDIA_CNT, /* # of streams */ g_sock_info, /* RTP sock info */ &local_sdp); /* the SDP result */ PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Create the INVITE session, and pass the SDP returned earlier * as the session's initial capability. */ status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* If we want the initial INVITE to travel to specific SIP proxies, * then we should put the initial dialog's route set here. The final * route set will be updated once a dialog has been established. * To set the dialog's initial route set, we do it with something * like this: * { pjsip_route_hdr route_set; pjsip_route_hdr *route; const pj_str_t hname = { "Route", 5 }; char *uri = "sip:proxy.server;lr"; pj_list_init(&route_set); route = pjsip_parse_hdr( dlg->pool, &hname, uri, strlen(uri), NULL); PJ_ASSERT_RETURN(route != NULL, 1); pj_list_push_back(&route_set, route); pjsip_dlg_set_route_set(dlg, &route_set); } * * Note that Route URI SHOULD have an ";lr" parameter! */ /* Create initial INVITE request. * This INVITE request will contain a perfectly good request and * an SDP body as well. */ status = pjsip_inv_invite(g_inv, &tdata); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Send initial INVITE request. * From now on, the invite session's state will be reported to us * via the invite session callbacks. */ status = pjsip_inv_send_msg(g_inv, tdata); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); } else { /* No URL to make call to */ PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls...")); } /* Loop until one call is completed */ for (;!g_complete;) { pj_time_val timeout = {0, 10}; pjsip_endpt_handle_events(g_endpt, &timeout); } /* On exit, dump current memory usage: */ dump_pool_usage(THIS_FILE, &cp); /* Destroy audio ports. Destroy the audio port first * before the stream since the audio port has threads * that get/put frames to the stream. */ if (g_snd_port) pjmedia_snd_port_destroy(g_snd_port); #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) /* Destroy video ports */ if (g_vid_capturer) pjmedia_vid_port_destroy(g_vid_capturer); if (g_vid_renderer) pjmedia_vid_port_destroy(g_vid_renderer); #endif /* Destroy streams */ if (g_med_stream) pjmedia_stream_destroy(g_med_stream); #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) if (g_med_vstream) pjmedia_vid_stream_destroy(g_med_vstream); /* Deinit ffmpeg codec */ # if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC!=0 pjmedia_codec_ffmpeg_vid_deinit(); # endif #endif /* Destroy media transports */ for (i = 0; i < MAX_MEDIA_CNT; ++i) { if (g_med_transport[i]) pjmedia_transport_close(g_med_transport[i]); } /* Deinit pjmedia endpoint */ if (g_med_endpt) pjmedia_endpt_destroy(g_med_endpt); /* Deinit pjsip endpoint */ if (g_endpt) pjsip_endpt_destroy(g_endpt); /* Release pool */ if (pool) pj_pool_release(pool); return 0; }
pj_status_t natclient_init(ice_trans_t *icetrans, ice_option_t opt) { pj_status_t status; /* Initialize the libraries before anything else */ CHECK( pj_init(), icetrans ); CHECK( pjlib_util_init(), icetrans ); CHECK( pjnath_init(), icetrans ); #if 0 //FIXME: consider if we need to log if (natclient.opt.log_file) { icetrans->log_fhnd = fopen(natclient.opt.log_file, "a"); pj_log_set_log_func(&log_func); } #endif pj_caching_pool_init(&icetrans->cp, NULL, 0); /* Init our ICE settings with null values */ pj_ice_strans_cfg_default(&icetrans->ice_cfg); icetrans->ice_cfg.stun_cfg.pf = &icetrans->cp.factory; /* Create application memory pool */ icetrans->pool = pj_pool_create(&icetrans->cp.factory, "natclient", 512, 512, NULL); /* Create timer heap for timer stuff */ CHECK( pj_timer_heap_create(icetrans->pool, 100, &icetrans->ice_cfg.stun_cfg.timer_heap), icetrans ); /* and create ioqueue for network I/O stuff */ CHECK( pj_ioqueue_create(icetrans->pool, 16, &icetrans->ice_cfg.stun_cfg.ioqueue), icetrans ); /* something must poll the timer heap and ioqueue, * unless we're on Symbian where the timer heap and ioqueue run * on themselves. */ CHECK( pj_thread_create(icetrans->pool, "natclient", &natclient_worker_thread, icetrans, 0, 0, &icetrans->thread), icetrans ); icetrans->ice_cfg.af = pj_AF_INET(); /* Create DNS resolver if nameserver is set */ if (opt.ns.slen) { CHECK( pj_dns_resolver_create(&icetrans->cp.factory, "resolver", 0, icetrans->ice_cfg.stun_cfg.timer_heap, icetrans->ice_cfg.stun_cfg.ioqueue, &icetrans->ice_cfg.resolver), icetrans ); CHECK( pj_dns_resolver_set_ns(icetrans->ice_cfg.resolver, 1, &opt.ns, NULL) , icetrans); } /* -= Start initializing ICE stream transport config =- */ /* Maximum number of host candidates */ if (opt.max_host != -1) icetrans->ice_cfg.stun.max_host_cands = opt.max_host; /* Nomination strategy */ if (opt.regular) icetrans->ice_cfg.opt.aggressive = PJ_FALSE; else icetrans->ice_cfg.opt.aggressive = PJ_TRUE; /* Configure STUN/srflx candidate resolution */ if (opt.stun_srv.slen) { char *pos; /* Command line option may contain port number */ if ((pos=pj_strchr(&opt.stun_srv, ':')) != NULL) { icetrans->ice_cfg.stun.server.ptr = opt.stun_srv.ptr; icetrans->ice_cfg.stun.server.slen = (pos - opt.stun_srv.ptr); icetrans->ice_cfg.stun.port = (pj_uint16_t)atoi(pos+1); } else { icetrans->ice_cfg.stun.server = opt.stun_srv; icetrans->ice_cfg.stun.port = PJ_STUN_PORT; } /* For this demo app, configure longer STUN keep-alive time * so that it does't clutter the screen output. */ icetrans->ice_cfg.stun.cfg.ka_interval = KA_INTERVAL; } /* Configure TURN candidate */ if (opt.turn_srv.slen) { char *pos; /* Command line option may contain port number */ if ((pos=pj_strchr(&opt.turn_srv, ':')) != NULL) { icetrans->ice_cfg.turn.server.ptr = opt.turn_srv.ptr; icetrans->ice_cfg.turn.server.slen = (pos - opt.turn_srv.ptr); icetrans->ice_cfg.turn.port = (pj_uint16_t)atoi(pos+1); } else { icetrans->ice_cfg.turn.server = opt.turn_srv; icetrans->ice_cfg.turn.port = PJ_STUN_PORT; } /* TURN credential */ icetrans->ice_cfg.turn.auth_cred.type = PJ_STUN_AUTH_CRED_STATIC; icetrans->ice_cfg.turn.auth_cred.data.static_cred.username = opt.turn_username; icetrans->ice_cfg.turn.auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN; icetrans->ice_cfg.turn.auth_cred.data.static_cred.data = opt.turn_password; /* Connection type to TURN server */ if (opt.turn_tcp) icetrans->ice_cfg.turn.conn_type = PJ_TURN_TP_TCP; else icetrans->ice_cfg.turn.conn_type = PJ_TURN_TP_UDP; /* For this demo app, configure longer keep-alive time * so that it does't clutter the screen output. */ icetrans->ice_cfg.turn.alloc_param.ka_interval = KA_INTERVAL; } /* -= That's it for now, initialization is complete =- */ return PJ_SUCCESS; }
/* * main() * */ int main(int argc, char *argv[]) { pj_caching_pool cp; pj_pool_t *pool = NULL; pjsip_module mod_app = { NULL, NULL, /* prev, next. */ { "mod-app", 7 }, /* Name. */ -1, /* Id */ PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ NULL, /* load() */ NULL, /* start() */ NULL, /* stop() */ NULL, /* unload() */ &on_rx_request, /* on_rx_request() */ NULL, /* on_rx_response() */ NULL, /* on_tx_request. */ NULL, /* on_tx_response() */ NULL, /* on_tsx_state() */ }; int c; pj_status_t status; /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Then init PJLIB-UTIL: */ status = pjlib_util_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); /* Create global endpoint: */ { /* Endpoint MUST be assigned a globally unique name. * Ideally we should put hostname or public IP address, but * we'll just use an arbitrary name here. */ /* Create the endpoint: */ status = pjsip_endpt_create(&cp.factory, "sipstateless", &sip_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); } /* Parse arguments */ pj_optind = 0; pj_list_init(&hdr_list); while ((c=pj_getopt(argc, argv , "H:")) != -1) { switch (c) { case 'H': if (pool == NULL) { pool = pj_pool_create(&cp.factory, "sipstateless", 1000, 1000, NULL); } if (pool) { char *name; name = strtok(pj_optarg, ":"); if (name == NULL) { puts("Error: invalid header format"); return 1; } else { char *val = strtok(NULL, "\r\n"); pjsip_generic_string_hdr *h; pj_str_t hname, hvalue; hname = pj_str(name); hvalue = pj_str(val); h = pjsip_generic_string_hdr_create(pool, &hname, &hvalue); pj_list_push_back(&hdr_list, h); PJ_LOG(4,(THIS_FILE, "Header %s: %s added", name, val)); } } break; default: puts("Error: invalid argument"); usage(); return 1; } } if (pj_optind != argc) { code = atoi(argv[pj_optind]); if (code < 200 || code > 699) { puts("Error: invalid status code"); usage(); return 1; } } PJ_LOG(4,(THIS_FILE, "Returning %d to incoming requests", code)); /* * Add UDP transport, with hard-coded port */ #ifdef HAS_UDP_TRANSPORT { pj_sockaddr_in addr; addr.sin_family = pj_AF_INET(); addr.sin_addr.s_addr = 0; addr.sin_port = pj_htons(5060); status = pjsip_udp_transport_start( sip_endpt, &addr, NULL, 1, NULL); if (status != PJ_SUCCESS) { PJ_LOG(3,(THIS_FILE, "Error starting UDP transport (port in use?)")); return 1; } } #endif #if HAS_TCP_TRANSPORT /* * Add UDP transport, with hard-coded port */ { pj_sockaddr_in addr; addr.sin_family = pj_AF_INET(); addr.sin_addr.s_addr = 0; addr.sin_port = pj_htons(5060); status = pjsip_tcp_transport_start(sip_endpt, &addr, 1, NULL); if (status != PJ_SUCCESS) { PJ_LOG(3,(THIS_FILE, "Error starting TCP transport (port in use?)")); return 1; } } #endif /* * Register our module to receive incoming requests. */ status = pjsip_endpt_register_module( sip_endpt, &mod_app); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Done. Loop forever to handle incoming events. */ PJ_LOG(3,(THIS_FILE, "Press Ctrl-C to quit..")); for (;;) { pjsip_endpt_handle_events(sip_endpt, NULL); } }
int main(int argc, char *argv[]) { struct pj_getopt_option long_options[] = { { "realm", 1, 0, 'r'}, { "username", 1, 0, 'u'}, { "password", 1, 0, 'p'}, { "nonce", 1, 0, 'N'}, { "fingerprint",0, 0, 'F'}, { "help", 0, 0, 'h'} }; int c, opt_id; pj_caching_pool cp; pj_stun_server *srv; pj_stun_usage *turn; pj_status_t status; while((c=pj_getopt_long(argc,argv, "r:u:p:N:hF", long_options, &opt_id))!=-1) { switch (c) { case 'r': o.realm = pj_optarg; break; case 'u': o.user_name = pj_optarg; break; case 'p': o.password = pj_optarg; break; case 'N': o.nonce = pj_optarg; break; case 'h': usage(); return 0; case 'F': o.use_fingerprint = PJ_TRUE; break; default: printf("Argument \"%s\" is not valid. Use -h to see help", argv[pj_optind]); return 1; } } if (pj_optind != argc) { puts("Error: invalid arguments"); return 1; } pj_init(); pjlib_util_init(); pjnath_init(); pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); status = pj_stun_server_create(&cp.factory, 1, &srv); if (status != PJ_SUCCESS) { pj_stun_perror(THIS_FILE, "Unable to create server", status); return 1; } /* status = pj_stun_bind_usage_create(srv, NULL, 3478, NULL); if (status != PJ_SUCCESS) { pj_stun_perror(THIS_FILE, "Unable to create bind usage", status); return 1; } */ status = pj_stun_turn_usage_create(srv, pj_SOCK_DGRAM(), NULL, 3478, o.use_fingerprint, &turn); if (status != PJ_SUCCESS) { pj_stun_perror(THIS_FILE, "Unable to create bind usage", status); return 1; } if (o.user_name && o.password) { pj_stun_auth_cred cred; pj_bzero(&cred, sizeof(cred)); cred.type = PJ_STUN_AUTH_CRED_STATIC; cred.data.static_cred.realm = pj_str(o.realm); cred.data.static_cred.username = pj_str(o.user_name); cred.data.static_cred.data_type = 0; cred.data.static_cred.data = pj_str(o.password); cred.data.static_cred.nonce = pj_str(o.nonce); pj_stun_turn_usage_set_credential(turn, &cred); } server_main(srv); pj_stun_server_destroy(srv); pj_pool_factory_dump(&cp.factory, PJ_TRUE); pj_shutdown(); return 0; }