Ejemplo n.º 1
0
static int msg_apply_changes_f(sip_msg_t *msg, char *str1, char *str2)
{
	struct dest_info dst;
	str obuf;
	sip_msg_t tmp;

	if(msg->first_line.type!=SIP_REPLY && get_route_type()!=REQUEST_ROUTE)
	{
		LM_ERR("invalid usage - not in request route\n");
		return -1;
	}

	init_dest_info(&dst);
	dst.proto = PROTO_UDP;
	if(msg->first_line.type == SIP_REPLY) {
		obuf.s = generate_res_buf_from_sip_res(msg,
				(unsigned int*)&obuf.len, BUILD_NO_VIA1_UPDATE);
	} else {
		if(msg->msg_flags & FL_RR_ADDED) {
			LM_ERR("cannot apply msg changes after adding record-route"
					" header - it breaks conditional 2nd header\n");
			return -1;
		}
		obuf.s = build_req_buf_from_sip_req(msg,
				(unsigned int*)&obuf.len, &dst,
				BUILD_NO_PATH|BUILD_NO_LOCAL_VIA|BUILD_NO_VIA1_UPDATE);
	}
	if(obuf.s == NULL)
	{
		LM_ERR("couldn't update msg buffer content\n");
		return -1;
	}
	if(obuf.len>=BUF_SIZE)
	{
		LM_ERR("new buffer overflow (%d)\n", obuf.len);
		pkg_free(obuf.s);
		return -1;
	}
	/* temporary copy */
	memcpy(&tmp, msg, sizeof(sip_msg_t));

	/* reset dst uri and path vector to avoid freeing - restored later */
	if(msg->dst_uri.s!=NULL)
	{
		msg->dst_uri.s = NULL;
		msg->dst_uri.len = 0;
	}
	if(msg->path_vec.s!=NULL)
	{
		msg->path_vec.s = NULL;
		msg->path_vec.len = 0;
	}

	/* free old msg structure */
	free_sip_msg(msg);
	memset(msg, 0, sizeof(sip_msg_t));

	/* restore msg fields */
	msg->buf                = tmp.buf;
	msg->id                 = tmp.id;
	msg->rcv                = tmp.rcv;
	msg->set_global_address = tmp.set_global_address;
	msg->set_global_port    = tmp.set_global_port;
	msg->flags              = tmp.flags;
	msg->msg_flags          = tmp.msg_flags;
	msg->hash_index         = tmp.hash_index;
	msg->force_send_socket  = tmp.force_send_socket;
	msg->fwd_send_flags     = tmp.fwd_send_flags;
	msg->rpl_send_flags     = tmp.rpl_send_flags;
	msg->dst_uri            = tmp.dst_uri;
	msg->path_vec           = tmp.path_vec;

	memcpy(msg->buf, obuf.s, obuf.len);
	msg->len = obuf.len;
	msg->buf[msg->len] = '\0';

	/* free new buffer - copied in the static buffer from old sip_msg_t */
	pkg_free(obuf.s);

	/* reparse the message */
	LM_DBG("SIP message content updated - reparsing\n");
	if (parse_msg(msg->buf, msg->len, msg)!=0){
		LM_ERR("parsing new sip message failed\n");
		return -1;
	}

	return 1;
}
Ejemplo n.º 2
0
/**
  * dump current SIP message and a diff lump list
  * part of the code taken from msg_apply_changes_f
  */
static int w_dbg_sip_msg(struct sip_msg* msg, char *level, char *facility)
{
	int ilevel = cfg_get(core, core_cfg, debug);
	int ifacility= cfg_get(core, core_cfg, log_facility);
	int flag = FLAG_MSG_LUMPS_ONLY; // copy lumps only, not the whole message
	unsigned int new_buf_offs=0, orig_offs = 0;
	char *hdr_lumps = NULL;
	char *bdy_lumps = NULL;
	const char *start_txt = "------------------------- START OF SIP message debug --------------------------\n";
	const char *hdr_txt =   "------------------------------ SIP header diffs -------------------------------\n";
	const char *bdy_txt =   "------------------------------- SIP body diffs --------------------------------\n";
	const char *end_txt =   "-------------------------- END OF SIP message debug ---------------------------\n\n";
	struct dest_info send_info;
	str obuf;

	if (msg->first_line.type != SIP_REPLY && get_route_type() != REQUEST_ROUTE) {
		LM_ERR("invalid usage - not in request route\n");
		return -1;
	}

	if (level != NULL) {
		/* substract L_OFFSET previously added */
		ilevel = (int)(long)level - L_OFFSET;
	}

	if (facility != NULL) {
		ifacility = (int)(long)facility;
	}

	/* msg_apply_changes_f code needed to get the current msg */
	init_dest_info(&send_info);
	send_info.proto = PROTO_UDP;
	if(msg->first_line.type == SIP_REPLY) {
		obuf.s = generate_res_buf_from_sip_res(msg,
				(unsigned int*)&obuf.len, BUILD_NO_VIA1_UPDATE);
	} else {
		obuf.s = build_req_buf_from_sip_req(msg,
				(unsigned int*)&obuf.len, &send_info,
				BUILD_NO_PATH|BUILD_NO_LOCAL_VIA|BUILD_NO_VIA1_UPDATE);
	}

	if(obuf.s == NULL)
	{
		LM_ERR("couldn't update msg buffer content\n");
		return -1;
	}

	if(obuf.len >= BUF_SIZE)
	{
		LM_ERR("new buffer overflow (%d)\n", obuf.len);
		pkg_free(obuf.s);
		return -1;
	}

	/* skip original uri */
	if(msg->first_line.type == SIP_REQUEST) {
		if(msg->new_uri.s) {
			orig_offs = msg->first_line.u.request.uri.s - msg->buf;
			orig_offs += msg->first_line.u.request.uri.len;
		}
	}

	/* alloc private mem and copy lumps */
	hdr_lumps = pkg_malloc(BUF_SIZE);
	bdy_lumps = pkg_malloc(BUF_SIZE);

	new_buf_offs = 0;
	process_lumps(msg, msg->add_rm, hdr_lumps, &new_buf_offs, &orig_offs, &send_info, flag);

	new_buf_offs = 0;
	process_lumps(msg, msg->body_lumps, bdy_lumps, &new_buf_offs, &orig_offs, &send_info, flag);

	/* do the print */
	if (_dbg_sip_msg_cline < 0 ) {
		LOG_FC(ifacility, ilevel, "CONFIG LINE unknown\n%s%.*s%s%s%s%s%s",
			start_txt,
			obuf.len, obuf.s,
			hdr_txt, hdr_lumps,
			bdy_txt, bdy_lumps,
			end_txt);
	} else {
		LOG_FC(ifacility, ilevel, "CONFIG LINE %d\n%s%.*s%s%s%s%s%s",
			_dbg_sip_msg_cline,
			start_txt,
			obuf.len, obuf.s,
			hdr_txt, hdr_lumps,
			bdy_txt, bdy_lumps,
			end_txt);
	}

	/* free lumps */
	if (hdr_lumps) {
		pkg_free(hdr_lumps);
	}

	if (bdy_lumps) {
		pkg_free(bdy_lumps);
	}

	return 1;
}