コード例 #1
0
ファイル: turn_sock.c プロジェクト: 0x0B501E7E/pjproject
/*
 * 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;
}
コード例 #2
0
/*
 * Notification when outgoing TCP socket has been connected.
 */
static pj_bool_t on_connect_complete(pj_activesock_t *asock,
				     pj_status_t status)
{
    pj_turn_sock *turn_sock;

    turn_sock = (pj_turn_sock*) pj_activesock_get_user_data(asock);
    if (!turn_sock)
        return PJ_FALSE;

    /* TURN session may have already been destroyed here.
     * See ticket #1557 (http://trac.pjsip.org/repos/ticket/1557).
     */
	if (!turn_sock->sess) {
		PJ_LOG(1, ("turn_sock.c", "!!! TURN DEALLOCATE !!! in on_connect_complete() turn_sock->sess is NULL status=%d", status));
		sess_fail(turn_sock, "TURN session already destroyed", status);
		return PJ_FALSE;
    }

    if (status != PJ_SUCCESS) {

		// DEAN assigned next turn server 
		if (turn_sock->turn_cnt <= ++turn_sock->curr_turn) {
			PJ_LOG(1, ("turn_sock.c", "!!! TURN DEALLOCATE !!! in on_connect_complete() turn_sock->turn_cnt <= ++turn_sock->curr_turn (%d,%d)", 
				turn_sock->turn_cnt, turn_sock->curr_turn));
			sess_fail(turn_sock, "TCP connect() error", status);
			turn_sock->curr_turn = 0;
			PJ_LOG(3, (__FILE__, "Failed(%d) connect to all turn servers.", status));
		} else {

			set_state(turn_sock->sess, PJ_TURN_STATE_NULL);

			PJ_LOG(3, (__FILE__, "Failed(%d) connect to turn server [%.*s:%d].",
				status,
				turn_sock->turn.domain->slen, 
				turn_sock->turn.domain->ptr, 
				turn_sock->turn.default_port));
			turn_sock->turn.domain = &turn_sock->turn_list[turn_sock->curr_turn].server;
			turn_sock->turn.default_port = turn_sock->turn_list[turn_sock->curr_turn].port;
			PJ_LOG(3, (__FILE__, "Try another turn server [%.*s:%d].",
				turn_sock->turn.domain->slen, 
				turn_sock->turn.domain->ptr, 
				turn_sock->turn.default_port));
			
			/* Resolve server */
			status = pj_turn_session_set_server(turn_sock->sess, turn_sock->turn.domain, 
				turn_sock->turn.default_port, turn_sock->turn.resolver);
			if (status != PJ_SUCCESS) {
				PJ_LOG(1, ("turn_sock.c", "!!! TURN DEALLOCATE !!! in on_connect_complete() pj_turn_session_set_server failed status=%d", 
					status));
				sess_fail(turn_sock, "Error setting TURN server", status);
				return status;
			}
		}
		return PJ_FALSE;
    }

    if (turn_sock->conn_type != PJ_TURN_TP_UDP) {
	PJ_LOG(5,(turn_sock->obj_name, "TCP connected"));
    }

    /* Kick start pending read operation */
    status = pj_activesock_start_read(asock, turn_sock->pool, 
				      PJ_TURN_MAX_PKT_LEN, 0);

    /* Init send_key */
    pj_ioqueue_op_key_init(&turn_sock->send_key, sizeof(turn_sock->send_key));

   /* Send Allocate request */
    status = pj_turn_session_alloc(turn_sock->sess, &turn_sock->alloc_param);
	if (status != PJ_SUCCESS) {
		PJ_LOG(1, ("turn_sock.c", "!!! TURN DEALLOCATE !!! in on_connect_complete() pj_turn_session_alloc failed status=%d", 
			status));
		sess_fail(turn_sock, "Error sending ALLOCATE", status);
		return PJ_FALSE;
    }

    return PJ_TRUE;
}
コード例 #3
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;
}