/* * 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; }
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; }
PJStunTurn::PJStunTurn() { if (PJ_SUCCESS != pjnath_init()) { g_warning("cannot init pjnath"); return; } pj_ice_strans_cfg_default(&ice_cfg_); pj_timer_heap_create(PJSIP::this_->pool_, 100, &ice_cfg_.stun_cfg.timer_heap); pj_ioqueue_create(PJSIP::this_->pool_, 512, &ice_cfg_.stun_cfg.ioqueue); ice_cfg_.stun_cfg.pf = PJSIP::this_->pool_->factory; if (PJ_SUCCESS != pj_thread_create(PJSIP::this_->pool_, "switcherSIP", &worker_thread, this, 0, 0, &thread_)) { g_warning("STUN TURN thread creating failed"); return; } ice_cfg_.af = pj_AF_INET(); ice_cfg_.stun.cfg.max_pkt_size = 8192; ice_cfg_.turn.cfg.max_pkt_size = 8192; ice_cfg_.opt.aggressive = PJ_FALSE; ice_cfg_.stun_cfg.rto_msec = 500; // set stun/turn config SIPPlugin::this_->install_method( "Set STUN/TURN parameters", // long name "set_stun_turn", // name "Set STUN/TURN configuration", // description "the server(s) are reachable", // return description Method::make_arg_description("STUN server address", // long name "stun", // name "string", // description "TURN server address", "turn", "string", "TURN user name", "turn_user", "string", "TURN user password", "turn_pass", "string", nullptr), (Method::method_ptr)&set_stun_turn, G_TYPE_BOOLEAN, Method::make_arg_type_description( G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, nullptr), this); }
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; }
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; }
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; }
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 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; }