Example #1
0
int async_send_query(sip_msg_t *msg, str *query, cfg_action_t *act)
{
	async_query_t *aq;
	unsigned int tindex = 0;
	unsigned int tlabel = 0;
	short suspend = 0;
	int dsize;
	int len;
	tm_cell_t *t = 0;

	if(query==0) {
		LM_ERR("invalid parameters\n");
		return -1;
	}

	t = tmb.t_gett();
	if (t==NULL || t==T_UNDEFINED) {
		LM_DBG("no pre-existing transaction, switching to transaction-less behavior\n");
	} else if (!ah_params.suspend_transaction) {
		LM_DBG("transaction won't be suspended\n");
	} else {
		if(tmb.t_suspend==NULL) {
			LM_ERR("http async query is disabled - tm module not loaded\n");
			return -1;
		}

		if(tmb.t_suspend(msg, &tindex, &tlabel)<0) {
			LM_ERR("failed to suspend request processing\n");
			return -1;
		}

		suspend = 1;

		LM_DBG("transaction suspended [%u:%u]\n", tindex, tlabel);
	}
	dsize = sizeof(async_query_t);
	aq = (async_query_t*)shm_malloc(dsize);

	if(aq==NULL)
	{
		LM_ERR("no more shm\n");
		goto error;
	}
	memset(aq,0,dsize);

	if(shm_str_dup(&aq->query, query)<0) {
		goto error;
	}

	aq->param = act;
	aq->tindex = tindex;
	aq->tlabel = tlabel;
	
	aq->query_params.tls_verify_peer = ah_params.tls_verify_peer;
	aq->query_params.tls_verify_host = ah_params.tls_verify_host;
	aq->query_params.suspend_transaction = suspend;
	aq->query_params.timeout = ah_params.timeout;
	aq->query_params.headers = ah_params.headers;
	aq->query_params.method = ah_params.method;
	aq->query_params.authmethod = ah_params.authmethod;
	
	q_idx++;
	snprintf(q_id, MAX_ID_LEN+1, "%u-%u", (unsigned int)getpid(), q_idx);
	strncpy(aq->id, q_id, strlen(q_id));

	aq->query_params.tls_client_cert.s = NULL;
	aq->query_params.tls_client_cert.len = 0;
	if (ah_params.tls_client_cert.s && ah_params.tls_client_cert.len > 0) {
		if (shm_str_dup(&aq->query_params.tls_client_cert, &(ah_params.tls_client_cert)) < 0) {
			LM_ERR("Error allocating aq->query_params.tls_client_cert\n");
			goto error;
		}
	}

	aq->query_params.tls_client_key.s = NULL;
	aq->query_params.tls_client_key.len = 0;
	if (ah_params.tls_client_key.s && ah_params.tls_client_key.len > 0) {
		if (shm_str_dup(&aq->query_params.tls_client_key, &(ah_params.tls_client_key)) < 0) {
			LM_ERR("Error allocating aq->query_params.tls_client_key\n");
			goto error;
		}
	}

	aq->query_params.tls_ca_path.s = NULL;
	aq->query_params.tls_ca_path.len = 0;
	if (ah_params.tls_ca_path.s && ah_params.tls_ca_path.len > 0) {
		if (shm_str_dup(&aq->query_params.tls_ca_path, &(ah_params.tls_ca_path)) < 0) {
			LM_ERR("Error allocating aq->query_params.tls_ca_path\n");
			goto error;
		}
	}

	aq->query_params.body.s = NULL;
	aq->query_params.body.len = 0;
	if (ah_params.body.s && ah_params.body.len > 0) {
		if (shm_str_dup(&aq->query_params.body, &(ah_params.body)) < 0) {
			LM_ERR("Error allocating aq->query_params.body\n");
			goto error;
		}
	}

	aq->query_params.username = NULL;
	if (ah_params.username) {
		len = strlen(ah_params.username);
		aq->query_params.username = shm_malloc(len+1);
	
		if(aq->query_params.username == NULL) {
			LM_ERR("error in shm_malloc\n");
			goto error;
		}

		strncpy(aq->query_params.username, ah_params.username, len);
		aq->query_params.username[len] = '\0';
	}

	aq->query_params.password = NULL;
	if (ah_params.password) {
		len = strlen(ah_params.password);
		aq->query_params.password = shm_malloc(len+1);
	
		if(aq->query_params.password == NULL) {
			LM_ERR("error in shm_malloc\n");
			goto error;
		}

		strncpy(aq->query_params.password, ah_params.password, len);
		aq->query_params.password[len] = '\0';
	}

	set_query_params(&ah_params);

	if(async_push_query(aq)<0) {
		LM_ERR("failed to relay query: %.*s\n", query->len, query->s);
		goto error;
	}

	if (suspend)  {
		/* force exit in config */
		return 0;
	}
	
	/* continue route processing */
	return 1;

error:

	if (suspend) {
		tmb.t_cancel_suspend(tindex, tlabel);
	}
	free_async_query(aq);
	return -1;
}
Example #2
0
void init_query_params(struct query_params *p) {
	memset(&ah_params, 0, sizeof(struct query_params));
	set_query_params(p);
}
Example #3
0
static int ah_set_req(struct sip_msg* msg, pv_param_t *param,
		int op, pv_value_t *val)
{
	pv_value_t *tval;

	if(param==NULL || tmb.t_request==NULL)
		return -1;

	tval = val;
	if((tval!=NULL) && (tval->flags&PV_VAL_NULL)) {
		tval = NULL;
	}

	switch((enum http_req_name_t) param->pvn.u.isname.name.n) {
	case E_HRN_ALL:
		if (tval == NULL)
			set_query_params(&ah_params);
		break;
	case E_HRN_HDR:
		if (tval) {
			if (!(tval->flags & PV_VAL_STR)) {
				LM_ERR("invalid value type for $http_req(hdr)\n");
				return -1;
			}
			header_list_add(&ah_params.headers, &tval->rs);
		}
		break;
	case E_HRN_METHOD:
		if (tval) {
			if (!(tval->flags & PV_VAL_STR)) {
				LM_ERR("invalid value type for $http_req(method)\n");
				return -1;
			}
			query_params_set_method(&ah_params, &tval->rs);
		} else {
			ah_params.method = AH_METH_DEFAULT;
		}
		break;
	case E_HRN_TIMEOUT:
		if (tval) {
			if (!(tval->flags & PV_VAL_INT)) {
				LM_ERR("invalid value type for $http_req(timeout)\n");
				return -1;
			}
			ah_params.timeout = tval->ri;
		} else {
			ah_params.timeout = http_timeout;
		}
		break;
	case E_HRN_TLS_CA_PATH:
		if (tval) {
			if (!(tval->flags & PV_VAL_STR)) {
				LM_ERR("invalid value type for $http_req(tls_ca_path)\n");
				return -1;
			}
			set_query_param(&ah_params.tls_ca_path, tval->rs);
		} else {
			set_query_param(&ah_params.tls_ca_path, tls_ca_path);
		}
		break;
	case E_HRN_TLS_CLIENT_KEY:
		if (tval) {
			if (!(tval->flags & PV_VAL_STR)) {
				LM_ERR("invalid value type for $http_req(tls_client_key)\n");
				return -1;
			}
			set_query_param(&ah_params.tls_client_key, tval->rs);
		} else {
			set_query_param(&ah_params.tls_client_key, tls_client_key);
		}
		break;
	case E_HRN_TLS_CLIENT_CERT:
		if (tval) {
			if (!(tval->flags & PV_VAL_STR)) {
				LM_ERR("invalid value type for $http_req(tls_client_cert)\n");
				return -1;
			}
			set_query_param(&ah_params.tls_client_cert, tval->rs);
		} else {
			set_query_param(&ah_params.tls_client_cert, tls_client_cert);
		}
		break;
	case E_HRN_SUSPEND:
		if (tval) {
			if (!(tval->flags & PV_VAL_INT)) {
				LM_ERR("invalid value type for $http_req(suspend)\n");
				return -1;
			}
			ah_params.suspend_transaction = tval->ri?1:0;
		} else {
			ah_params.suspend_transaction = 1;
		}
		break;
	}

	return 1;
}