PJ_DEF(pj_status_t) pjsua_detect_nat_type()
{
    pj_status_t status;

    if (pjsua_var.nat_in_progress)
	return PJ_SUCCESS;

    /* Make sure STUN server resolution has completed */
    status = resolve_stun_server(PJ_TRUE, PJ_TRUE, 0);
    if (status != PJ_SUCCESS) {
	pjsua_var.nat_status = status;
	pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
	return status;
    }

    /* Make sure we have STUN */
    if (pjsua_var.stun_srv.addr.sa_family == 0) {
	pjsua_var.nat_status = PJNATH_ESTUNINSERVER;
	return PJNATH_ESTUNINSERVER;
    }

    status = pj_stun_detect_nat_type2(&pjsua_var.stun_srv, 
				      &pjsua_var.stun_cfg, 
				      NULL, &nat_detect_cb);

    if (status != PJ_SUCCESS) {
	pjsua_var.nat_status = status;
	pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
	return status;
    }

    pjsua_var.nat_in_progress = PJ_TRUE;

    return PJ_SUCCESS;
}
PJ_DEF(pj_status_t) pjsua_detect_nat_type()
{
    pj_status_t status;

    if (pjsua_var.nat_in_progress)
	return PJ_SUCCESS;

     
    status = resolve_stun_server(PJ_TRUE, PJ_TRUE, 0);
    if (status != PJ_SUCCESS) {
	pjsua_var.nat_status = status;
	pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
	return status;
    }

     
    if (pjsua_var.stun_srv.addr.sa_family == 0) {
	pjsua_var.nat_status = PJNATH_ESTUNINSERVER;
	return PJNATH_ESTUNINSERVER;
    }

    status = pj_stun_detect_nat_type2(&pjsua_var.stun_srv, 
				      &pjsua_var.stun_cfg, 
				      NULL, &nat_detect_cb);

    if (status != PJ_SUCCESS) {
	pjsua_var.nat_status = status;
	pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
	return status;
    }

    pjsua_var.nat_in_progress = PJ_TRUE;

    return PJ_SUCCESS;
}