Skip to content

Commit

Permalink
sa,sip,http: remove sa_is_ipv6ll(); use HAVE_INET6 correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 committed Jun 10, 2021
1 parent b729821 commit 4626625
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 52 deletions.
6 changes: 2 additions & 4 deletions include/re_sa.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ bool sa_is_linklocal(const struct sa *sa);
bool sa_is_loopback(const struct sa *sa);
bool sa_is_any(const struct sa *sa);

void sa_set_scopeid(struct sa *sa, int scopeid);
int sa_scopeid(const struct sa *sa);
int sa_cpy_scopeid(struct sa *sa_dst, const struct sa *sa_src);
bool sa_is_ipv6ll(const struct sa *sa);
void sa_set_scopeid(struct sa *sa, uint32_t scopeid);
uint32_t sa_scopeid(const struct sa *sa);

struct re_printf;
int sa_print_addr(struct re_printf *pf, const struct sa *sa);
2 changes: 1 addition & 1 deletion src/http/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ int http_request(struct http_req **reqp, struct http_cli *cli, const char *met,

req->srvc = 1;

sa_cpy_scopeid(&req->srvv[0], &cli->laddr6);
sa_set_scopeid(&req->srvv[0], sa_scopeid(&cli->laddr6));
err = req_connect(req);
if (err)
goto out;
Expand Down
36 changes: 5 additions & 31 deletions src/sa/sa.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,56 +593,30 @@ bool sa_is_any(const struct sa *sa)
}


void sa_set_scopeid(struct sa *sa, int scopeid)
void sa_set_scopeid(struct sa *sa, uint32_t scopeid)
{
#ifndef HAVE_INET6
return;
#endif
if (!sa)
return;

#ifdef HAVE_INET6
if (sa_af(sa) != AF_INET6)
return;

sa->u.in6.sin6_scope_id = scopeid;
#endif
}


int sa_scopeid(const struct sa *sa)
uint32_t sa_scopeid(const struct sa *sa)
{
#ifndef HAVE_INET6
return;
#endif
if (!sa)
return 0;

#ifdef HAVE_INET6
if (sa_af(sa) != AF_INET6)
return 0;

return sa->u.in6.sin6_scope_id;
}


int sa_cpy_scopeid(struct sa *sa_dst, const struct sa *sa_src)
{
#ifndef HAVE_INET6
return;
#endif
if (!sa_dst || !sa_src)
return EINVAL;

if (!sa_is_ipv6ll(sa_dst) || !sa_is_ipv6ll(sa_src))
return EINVAL;

sa_dst->u.in6.sin6_scope_id = sa_src->u.in6.sin6_scope_id;
return 0;
}


bool sa_is_ipv6ll(const struct sa *sa)
{
#ifndef HAVE_INET6
return;
#endif
return sa_af(sa) == AF_INET6 && sa_is_linklocal(sa);
}
20 changes: 13 additions & 7 deletions src/sip/reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
#include <re_dbg.h>


#if HAVE_INET6
static int dst_set_scopeid(struct sa *dst, struct sip *sip, enum sip_transp tp)
{
struct sa laddr;
int err;

if (!sa_is_ipv6ll(dst))
if (sa_af(dst) != AF_INET6 || !sa_is_linklocal(dst))
return 0;

err = sip_transp_laddr(sip, &laddr, tp, dst);
Expand All @@ -35,14 +36,15 @@ static int dst_set_scopeid(struct sa *dst, struct sip *sip, enum sip_transp tp)
return err;
}

err = sa_cpy_scopeid(dst, &laddr);
if (err) {
DEBUG_WARNING("could not copy scope id from %j to %j\n",
&laddr, dst);
if (sa_af(&laddr) != AF_INET6 || !sa_is_linklocal(&laddr)) {
DEBUG_WARNING("laddr %j is not IPv6 link local\n", &laddr);
return EINVAL;
}

return err;
sa_set_scopeid(dst, sa_scopeid(&laddr));
return 0;
}
#endif


static int vreplyf(struct sip_strans **stp, struct mbuf **mbp, bool trans,
Expand Down Expand Up @@ -143,7 +145,11 @@ static int vreplyf(struct sip_strans **stp, struct mbuf **mbp, bool trans,
mb->pos = 0;

sip_reply_addr(&dst, msg, rport);
dst_set_scopeid(&dst, sip, msg->tp);
#if HAVE_INET6
err = dst_set_scopeid(&dst, sip, msg->tp);
if (err)
goto out;
#endif

if (trans) {
err = sip_strans_reply(stp, sip, msg, &dst, scode, mb);
Expand Down
14 changes: 9 additions & 5 deletions src/sip/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,13 @@ static int request(struct sip_request *req, enum sip_transp tp,
}


#if HAVE_INET6
static int dst_set_scopeid(struct sa *dst, struct sip_request *req)
{
struct sa laddr;
int err;

if (!sa_is_ipv6ll(dst))
if (sa_af(dst) != AF_INET6 || !sa_is_linklocal(dst))
return 0;

err = sip_transp_laddr(req->sip, &laddr, req->tp, dst);
Expand All @@ -225,14 +226,15 @@ static int dst_set_scopeid(struct sa *dst, struct sip_request *req)
return err;
}

err = sa_cpy_scopeid(dst, &laddr);
if (err) {
DEBUG_WARNING("could not copy scope id from %j to %j\n",
&laddr, dst);
if (sa_af(&laddr) != AF_INET6 || !sa_is_linklocal(&laddr)) {
DEBUG_WARNING("laddr %j is not IPv6 link local\n", &laddr);
return EINVAL;
}

sa_set_scopeid(dst, sa_scopeid(&laddr));
return err;
}
#endif


static int request_next(struct sip_request *req)
Expand Down Expand Up @@ -699,9 +701,11 @@ int sip_request(struct sip_request **reqp, struct sip *sip, bool stateful,
if (!sa_set_str(&dst, req->host,
sip_transp_port(req->tp, route->port))) {

#if HAVE_INET6
err = dst_set_scopeid(&dst, req);
if (err)
goto out;
#endif

err = request(req, req->tp, &dst);
if (!req->stateful) {
Expand Down
6 changes: 2 additions & 4 deletions src/sip/transp.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ static const struct sip_transport *transp_find(struct sip *sip,
continue;

sa_cpy(&dsttmp, dst);
if (sa_is_ipv6ll(dst))
sa_cpy_scopeid(&dsttmp, src);
sa_set_scopeid(&dsttmp, sa_scopeid(src));

if (net_dst_is_source_addr(&dsttmp, src))
continue;
Expand Down Expand Up @@ -391,8 +390,7 @@ static void udp_recv_handler(const struct sa *src, struct mbuf *mb, void *arg)
msg->src = *src;
msg->dst = transp->laddr;
msg->tp = SIP_TRANSP_UDP;
if (sa_is_ipv6ll(&msg->src))
err = sa_cpy_scopeid(&msg->src, &transp->laddr);
sa_set_scopeid(&msg->src, sa_scopeid(&transp->laddr));

if (err) {
DEBUG_WARNING("could not copy IPv6 scope id from %j to %j\n",
Expand Down

0 comments on commit 4626625

Please sign in to comment.