Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set TCP source port in Via and Contact header #334

Merged
merged 5 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/sip/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,19 @@ static void response_handler(int err, const struct sip_msg *msg, void *arg)
static int request(struct sip_request *req, enum sip_transp tp,
const struct sa *dst)
{
struct mbuf *mb = NULL;
struct mbuf *mbs = NULL;
struct mbuf *mb = NULL;
struct mbuf *cont = NULL;
char *branch = NULL;
int err = ENOMEM;
struct sa laddr;
struct sip_conncfg *conncfg;

req->provrecv = false;

branch = mem_alloc(24, NULL);
mb = mbuf_alloc(1024);
mbs = mbuf_alloc(256);
mb = mbuf_alloc(1024);

if (!branch || !mb)
goto out;
Expand All @@ -181,11 +184,22 @@ static int request(struct sip_request *req, enum sip_transp tp,
if (err)
goto out;

err |= req->sendh ? req->sendh(tp, &laddr, dst, mbs, &cont, req->arg) :
sreimers marked this conversation as resolved.
Show resolved Hide resolved
0;
if (err)
goto out;

mbuf_set_pos(mbs, 0);
if (tp==SIP_TRANSP_TCP || tp==SIP_TRANSP_TLS) {
conncfg = sip_conncfg_find(req->sip, dst);
if (conncfg && conncfg->srcport)
sa_set_port(&laddr, conncfg->srcport);
}

err = mbuf_printf(mb, "%s %s SIP/2.0\r\n", req->met, req->uri);
err |= mbuf_printf(mb, "Via: SIP/2.0/%s %J;branch=%s;rport\r\n",
sip_transp_name(tp), &laddr, branch);
err |= req->sendh ? req->sendh(tp, &laddr, dst, mb, &cont, req->arg) :
0;
err |= mbuf_write_mem(mb, mbuf_buf(mbs), mbuf_get_left(mbs));
err |= mbuf_write_mem(mb, mbuf_buf(req->mb), mbuf_get_left(req->mb));
err |= cont ? mbuf_write_mem(mb, mbuf_buf(cont), mbuf_get_left(cont)) :
0;
Expand All @@ -206,6 +220,7 @@ static int request(struct sip_request *req, enum sip_transp tp,

out:
mem_deref(branch);
mem_deref(mbs);
mem_deref(mb);

return err;
Expand Down
20 changes: 20 additions & 0 deletions src/sip/sip.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,23 @@ void sip_set_trace_handler(struct sip *sip, sip_trace_h *traceh)

sip->traceh = traceh;
}


struct sip_conncfg *sip_conncfg_find(struct sip *sip,
const struct sa *paddr)
{
struct le *le;

le = list_head(hash_list(sip->ht_conncfg, sa_hash(paddr, SA_ALL)));
for (; le; le = le->next) {

struct sip_conncfg *cfg = le->data;

if (!sa_cmp(&cfg->paddr, paddr, SA_ALL))
continue;

return cfg;
}

return NULL;
}
4 changes: 4 additions & 0 deletions src/sip/sip.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ int sip_keepalive_tcp(struct sip_keepalive *ka, struct sip_conn *conn,
int sip_keepalive_udp(struct sip_keepalive *ka, struct sip *sip,
struct udp_sock *us, const struct sa *paddr,
uint32_t interval);

/* sip_conncfg */
struct sip_conncfg *sip_conncfg_find(struct sip *sip,
const struct sa *paddr);
24 changes: 2 additions & 22 deletions src/sip/transp.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,6 @@ static const struct sip_transport *transp_find(struct sip *sip,
}


static struct sip_conncfg *conncfg_find(struct sip *sip,
const struct sa *paddr)
{
struct le *le;

le = list_head(hash_list(sip->ht_conncfg, sa_hash(paddr, SA_ALL)));
for (; le; le = le->next) {

struct sip_conncfg *cfg = le->data;

if (!sa_cmp(&cfg->paddr, paddr, SA_ALL))
continue;

return cfg;
}

return NULL;
}


static struct sip_conn *conn_find(struct sip *sip, const struct sa *paddr,
bool secure)
{
Expand Down Expand Up @@ -787,7 +767,7 @@ static int conn_send(struct sip_connqent **qentp, struct sip *sip, bool secure,
conn->sip = sip;
conn->tp = secure ? SIP_TRANSP_TLS : SIP_TRANSP_TCP;

conncfg = conncfg_find(sip, dst);
conncfg = sip_conncfg_find(sip, dst);
if (conncfg && conncfg->srcport) {
struct sa src;
sa_init(&src, sa_af(dst));
Expand Down Expand Up @@ -1894,7 +1874,7 @@ int sip_conncfg_set(struct sip *sip, const struct sa *paddr,
if (!sip || !sa_isset(paddr, SA_ALL))
return EINVAL;

cfg = conncfg_find(sip, paddr);
cfg = sip_conncfg_find(sip, paddr);
if (cfg) {
cfg->srcport = conncfg.srcport;
return 0;
Expand Down
2 changes: 2 additions & 0 deletions src/sipreg/reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ static int send_handler(enum sip_transp tp, const struct sa *src,

reg->laddr = *src;
reg->tp = tp;
if (reg->srcport && tp != SIP_TRANSP_UDP)
sa_set_port(&reg->laddr, reg->srcport);

err = mbuf_printf(mb, "Contact: <sip:%s@%J%s>;expires=%u%s%s",
reg->cuser, &reg->laddr, sip_transp_param(reg->tp),
Expand Down