Skip to content

Commit

Permalink
[core] adds back the ability to reply to VIA (and not to network src)
Browse files Browse the repository at this point in the history
This is a rework of #3327 (credits go to @carstenbock).
Even more, we merged:
* force_rport()
* this new reply_to_via PR
* add_local_rport()
* force_tcp_alias()
as a single VIA related function:
  set_via_handling("force-rport|add-local-rport|reply-to-via|force-tcp-alias")

Closes #3327
  • Loading branch information
bogdan-iancu committed Apr 11, 2024
1 parent 9089106 commit 1ab78f8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
41 changes: 41 additions & 0 deletions core_cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "status_report.h"
#include "cachedb/cachedb.h"
#include "msg_translator.h"
#include "mod_fix.h"
/* needed by tcpconn_add_alias() */
#include "net/tcp_conn_defs.h"

Expand All @@ -64,6 +65,7 @@ static int fixup_format_string(void** param);
static int fixup_nt_string(void** param);
static int fixup_nt_str(void** param);
static int fixup_nt_str_free(void** param);
static int fixup_via_hdl(void** param);

static int w_forward(struct sip_msg *msg, struct proxy_l *dest);
static int w_send(struct sip_msg *msg, struct proxy_l *dest, str *headers);
Expand Down Expand Up @@ -127,6 +129,7 @@ static int w_script_trace(struct sip_msg *msg, int *log_level,
pv_elem_t *fmt_string, void *info_str);
static int w_is_myself(struct sip_msg *msg, str *host, int *port);
static int w_print_avps(struct sip_msg* msg, char* foo, char *bar);
static int w_set_via_handling(struct sip_msg* msg, int* flags);

#ifndef FUZZ_BUILD
static
Expand Down Expand Up @@ -362,6 +365,9 @@ const cmd_export_t core_cmds[]={
ALL_ROUTES},
{"avp_print", (cmd_function)w_print_avps, {{0, 0, 0}},
ALL_ROUTES},
{"set_via_handling", (cmd_function)w_set_via_handling, {
{CMD_PARAM_STR, fixup_via_hdl, 0}, {0,0,0}},
ALL_ROUTES},
{0,0,{{0,0,0}},0}
};

Expand Down Expand Up @@ -1463,3 +1469,38 @@ static int w_print_avps(struct sip_msg* msg, char* foo, char *bar)
}


static str via_hdl_flag_names[] = {
str_init("force-rport"),
str_init("add-local-rport"),
str_init("reply-to-via"),
str_init("force-tcp-alias"),
STR_NULL
};
enum via_hdl_flags {
VIA_HDL_FORCE_RPORT,
VIA_HDL_ADD_LOCAL_RPORT,
VIA_HDL_REPLY_TO_VIA,
VIA_HDL_FORCE_TCP_ALIAS,
};
static int fixup_via_hdl(void** param)
{
return fixup_named_flags(param, via_hdl_flag_names, NULL, NULL);
}

static int w_set_via_handling(struct sip_msg* msg, int* flags)
{
if (*flags & VIA_HDL_FORCE_RPORT)
msg->msg_flags |= FL_FORCE_RPORT;

if (*flags & VIA_HDL_ADD_LOCAL_RPORT)
msg->msg_flags|=FL_FORCE_LOCAL_RPORT;

if (*flags & VIA_HDL_REPLY_TO_VIA)
msg->msg_flags |= FL_REPLY_TO_VIA;

if (*flags & VIA_HDL_FORCE_TCP_ALIAS)
w_force_tcp_alias( msg, 0);

return 1;
}

10 changes: 9 additions & 1 deletion modules/sl/sl_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,15 @@ int sl_send_reply_helper(struct sip_msg *msg ,int code, const str *text)
if ( msg->REQ_METHOD==METHOD_ACK)
return 1;

update_sock_struct_from_ip( &to, msg );
if (msg->msg_flags&FL_REPLY_TO_VIA) {
if (update_sock_struct_from_via( &to, msg, msg->via1 )==-1) {
LM_ERR("cannot lookup reply dst: %.*s\n",
msg->via1->host.len, msg->via1->host.s);
goto error;
}
} else {
update_sock_struct_from_ip( &to, msg );
}

/* if that is a redirection message, dump current message set to it */
if (code>=300 && code<400) {
Expand Down
12 changes: 11 additions & 1 deletion modules/tm/t_lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,18 @@ int init_rb( struct retr_buf *rb, struct sip_msg *msg)
{
int proto;

update_sock_struct_from_ip( &rb->dst.to, msg );
proto=msg->rcv.proto;

if (msg->msg_flags&FL_REPLY_TO_VIA) {
if (update_sock_struct_from_via( &(rb->dst.to), msg, msg->via1 )==-1) {
LM_ERR("cannot lookup reply dst: %.*s\n",
msg->via1->host.len, msg->via1->host.s );
ser_error=E_BAD_VIA;
return 0;
}
} else {
update_sock_struct_from_ip( &rb->dst.to, msg );
}
rb->dst.proto=proto;
rb->dst.proto_reserved1=msg->rcv.proto_reserved1;
/* use for sending replies the incoming interface of the request -bogdan */
Expand Down
3 changes: 2 additions & 1 deletion parser/msg_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ enum request_method {
};

#define FL_FORCE_RPORT (1<<0) /* force rport (top via) */
#define FL_FORCE_ACTIVE (1<<1) /* force active SDP */
#define FL_REPLY_TO_VIA (1<<1) /* force replying to VIA ip:port */
#define FL_FORCE_ACTIVE (1<<1) /* force active SDP (deprecated) */
#define FL_FORCE_LOCAL_RPORT (1<<2) /* force local rport (local via) */
#define FL_SDP_IP_AFS (1<<3) /* SDP IP rewritten */
#define FL_SDP_PORT_AFS (1<<4) /* SDP port rewritten */
Expand Down

0 comments on commit 1ab78f8

Please sign in to comment.