/* * Initialize. */ PJ_DEF(pj_status_t) pj_turn_sock_alloc(pj_turn_sock *turn_sock, const pj_str_t *domain, int default_port, pj_dns_resolver *resolver, const pj_stun_auth_cred *cred, const pj_turn_alloc_param *param) { pj_status_t status; PJ_ASSERT_RETURN(turn_sock && domain, PJ_EINVAL); PJ_ASSERT_RETURN(turn_sock->sess, PJ_EINVALIDOP); pj_grp_lock_acquire(turn_sock->grp_lock); /* Copy alloc param. We will call session_alloc() only after the * server address has been resolved. */ if (param) { pj_turn_alloc_param_copy(turn_sock->pool, &turn_sock->alloc_param, param); } else { pj_turn_alloc_param_default(&turn_sock->alloc_param); } /* Set credental */ if (cred) { status = pj_turn_session_set_credential(turn_sock->sess, cred); if (status != PJ_SUCCESS) { sess_fail(turn_sock, "Error setting credential", status); pj_grp_lock_release(turn_sock->grp_lock); return status; } } /* Resolve server */ status = pj_turn_session_set_server(turn_sock->sess, domain, default_port, resolver); if (status != PJ_SUCCESS) { sess_fail(turn_sock, "Error setting TURN server", status); pj_grp_lock_release(turn_sock->grp_lock); return status; } /* Done for now. The next work will be done when session state moved * to RESOLVED state. */ pj_grp_lock_release(turn_sock->grp_lock); return PJ_SUCCESS; }
static int create_test_session(pj_stun_config *stun_cfg, const struct test_session_cfg *cfg, struct test_session **p_sess) { struct test_session *sess; pj_pool_t *pool; pj_turn_sock_cb turn_sock_cb; pj_turn_alloc_param alloc_param; pj_stun_auth_cred cred; pj_status_t status; /* Create client */ pool = pj_pool_create(mem, "turnclient", 512, 512, NULL); sess = PJ_POOL_ZALLOC_T(pool, struct test_session); sess->pool = pool; sess->stun_cfg = stun_cfg; sess->destroy_on_state = cfg->client.destroy_on_state; pj_bzero(&turn_sock_cb, sizeof(turn_sock_cb)); turn_sock_cb.on_rx_data = &turn_on_rx_data; turn_sock_cb.on_state = &turn_on_state; status = pj_turn_sock_create(sess->stun_cfg, pj_AF_INET(), PJ_TURN_TP_UDP, &turn_sock_cb, 0, sess, &sess->turn_sock); if (status != PJ_SUCCESS) { destroy_session(sess); return -20; } /* Create test server */ status = create_test_server(sess->stun_cfg, cfg->srv.flags, SRV_DOMAIN, &sess->test_srv); if (status != PJ_SUCCESS) { destroy_session(sess); return -30; } sess->test_srv->turn_respond_allocate = cfg->srv.respond_allocate; sess->test_srv->turn_respond_refresh = cfg->srv.respond_refresh; /* Create client resolver */ status = pj_dns_resolver_create(mem, "resolver", 0, sess->stun_cfg->timer_heap, sess->stun_cfg->ioqueue, &sess->resolver); if (status != PJ_SUCCESS) { destroy_session(sess); return -40; } else { pj_str_t dns_srv = pj_str("127.0.0.1"); pj_uint16_t dns_srv_port = (pj_uint16_t) DNS_SERVER_PORT; status = pj_dns_resolver_set_ns(sess->resolver, 1, &dns_srv, &dns_srv_port); if (status != PJ_SUCCESS) { destroy_session(sess); return -50; } } /* Init TURN credential */ pj_bzero(&cred, sizeof(cred)); cred.type = PJ_STUN_AUTH_CRED_STATIC; cred.data.static_cred.realm = pj_str(SRV_DOMAIN); cred.data.static_cred.username = pj_str(TURN_USERNAME); cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN; cred.data.static_cred.data = pj_str(TURN_PASSWD); /* Init TURN allocate parameter */ pj_turn_alloc_param_default(&alloc_param); alloc_param.ka_interval = KA_INTERVAL; /* Start the client */ if (cfg->client.enable_dns_srv) { /* Use DNS SRV to resolve server, may fallback to DNS A */ pj_str_t domain = pj_str(SRV_DOMAIN); status = pj_turn_sock_alloc(sess->turn_sock, &domain, TURN_SERVER_PORT, sess->resolver, &cred, &alloc_param); } else { /* Explicitly specify server address */ pj_str_t host = pj_str("127.0.0.1"); status = pj_turn_sock_alloc(sess->turn_sock, &host, TURN_SERVER_PORT, NULL, &cred, &alloc_param); } if (status != PJ_SUCCESS) { if (cfg->client.destroy_on_state >= PJ_TURN_STATE_READY) { destroy_session(sess); return -70; } } *p_sess = sess; return 0; }
/* * 2013-05-08 DEAN modifedf by adding parameters. * Initialize. */ PJ_DEF(pj_status_t) pj_turn_sock_alloc2(pj_turn_sock *turn_sock, const pj_str_t *domain, int default_port, pj_dns_resolver *resolver, const pj_stun_auth_cred *cred, const pj_turn_alloc_param *param, int curr_turn, int turn_cnt, pj_turn_server turn_list[]) { pj_status_t status; PJ_ASSERT_RETURN(turn_sock && domain && turn_cnt <= MAX_TURN_SERVER_COUNT, PJ_EINVAL); PJ_ASSERT_RETURN(turn_sock->sess, PJ_EINVALIDOP); /* Copy alloc param. We will call session_alloc() only after the * server address has been resolved. */ if (param) { pj_turn_alloc_param_copy(turn_sock->pool, &turn_sock->alloc_param, param); } else { pj_turn_alloc_param_default(&turn_sock->alloc_param); } /* Set credential */ if (cred) { PJ_LOG(4, (THIS_FILE, "pj_turn_sock_alloc2() turn tp type=%d", turn_sock->conn_type)); PJ_LOG(4, (THIS_FILE, "pj_turn_sock_alloc2() turn server=%.*s:%d", domain->slen, domain->ptr, default_port)); PJ_LOG(4, (THIS_FILE, "pj_turn_sock_alloc2() turn realm=%.*s", cred->data.static_cred.realm.slen, cred->data.static_cred.realm.ptr)); PJ_LOG(4, (THIS_FILE, "pj_turn_sock_alloc2() turn username=%.*s", cred->data.static_cred.username.slen, cred->data.static_cred.username.ptr)); PJ_LOG(4, (THIS_FILE, "pj_turn_sock_alloc2() turn password=%.*s", cred->data.static_cred.data.slen, cred->data.static_cred.data.ptr)); status = pj_turn_session_set_credential(turn_sock->sess, cred); if (status != PJ_SUCCESS) { PJ_LOG(1, ("turn_sock.c", "!!! TURN DEALLOCATE !!! in pj_turn_sock_alloc2() pj_turn_session_set_credential failed=%d", status)); sess_fail(turn_sock, "Error setting credential", status); return status; } } // 2014-04-19 DEAN, for retry another turn use turn_sock->turn.domain = domain; turn_sock->turn.default_port = default_port; turn_sock->turn.resolver = resolver; turn_sock->curr_turn = curr_turn; turn_sock->turn_cnt = turn_cnt; memcpy(turn_sock->turn_list, turn_list, sizeof(pj_turn_server)*turn_cnt); /* Resolve server */ status = pj_turn_session_set_server(turn_sock->sess, domain, default_port, resolver); if (status != PJ_SUCCESS) { PJ_LOG(1, ("turn_sock.c", "!!! TURN DEALLOCATE !!! in pj_turn_sock_alloc2() pj_turn_session_set_server failed=%d", status)); sess_fail(turn_sock, "Error setting TURN server", status); return status; } /* Done for now. The next work will be done when session state moved * to RESOLVED state. */ return PJ_SUCCESS; }