From a6df1bd718c1cdd17084fc9e7ba362bd8b850b95 Mon Sep 17 00:00:00 2001 From: Christian Spielberger Date: Thu, 10 Jun 2021 16:09:52 +0200 Subject: [PATCH] sa,sip,http: remove sa_is_ipv6ll(); use HAVE_INET6 correctly --- include/re_sa.h | 6 ++---- src/http/client.c | 2 +- src/sa/sa.c | 36 +++++------------------------------- src/sip/reply.c | 20 +++++++++++++------- src/sip/request.c | 14 +++++++++----- src/sip/transp.c | 6 ++---- 6 files changed, 32 insertions(+), 52 deletions(-) diff --git a/include/re_sa.h b/include/re_sa.h index df6fe3eba..1a74f68c0 100644 --- a/include/re_sa.h +++ b/include/re_sa.h @@ -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); diff --git a/src/http/client.c b/src/http/client.c index 2a284030d..91e57a827 100644 --- a/src/http/client.c +++ b/src/http/client.c @@ -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; diff --git a/src/sa/sa.c b/src/sa/sa.c index 1149673e8..ab9fc8a7e 100644 --- a/src/sa/sa.c +++ b/src/sa/sa.c @@ -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); -} diff --git a/src/sip/reply.c b/src/sip/reply.c index 08426f966..1007cd0d1 100644 --- a/src/sip/reply.c +++ b/src/sip/reply.c @@ -21,12 +21,13 @@ #include +#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); @@ -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, @@ -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); diff --git a/src/sip/request.c b/src/sip/request.c index bc357dddd..0bb916997 100644 --- a/src/sip/request.c +++ b/src/sip/request.c @@ -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); @@ -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) @@ -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) { diff --git a/src/sip/transp.c b/src/sip/transp.c index a67026f40..2ffd93b93 100644 --- a/src/sip/transp.c +++ b/src/sip/transp.c @@ -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; @@ -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",