diff options
author | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2014-04-18 10:48:31 +0000 |
---|---|---|
committer | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2014-04-18 10:48:31 +0000 |
commit | ead8f16d7f9c050a5bff8d2edc980b290f54e7f9 (patch) | |
tree | e553cd5fec4045a19aee8bcf03d02210ec98cfa7 /sys/netinet6/in6_src.c | |
parent | c8eda18cb26aab069ab65363196d11a8a337b4ae (diff) |
Invert the signature logic of in{,6}_selectsrc, make them return the
error code and pass the resulting source address back to the caller
through a pointer, as suggested by chrisz. This gives us more readable
code, and eases the deletion of useless checks in the callers' error path.
Add a bunch of "0 -> NULL" conversions, while here.
ok chrisz@ mpi@
Diffstat (limited to 'sys/netinet6/in6_src.c')
-rw-r--r-- | sys/netinet6/in6_src.c | 100 |
1 files changed, 49 insertions, 51 deletions
diff --git a/sys/netinet6/in6_src.c b/sys/netinet6/in6_src.c index 7ca882e4ab2..513aef2a245 100644 --- a/sys/netinet6/in6_src.c +++ b/sys/netinet6/in6_src.c @@ -1,4 +1,4 @@ -/* $OpenBSD: in6_src.c,v 1.41 2014/04/07 10:04:17 mpi Exp $ */ +/* $OpenBSD: in6_src.c,v 1.42 2014/04/18 10:48:30 jca Exp $ */ /* $KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 itojun Exp $ */ /* @@ -96,18 +96,18 @@ int selectroute(struct sockaddr_in6 *, struct ip6_pktopts *, * If necessary, this function lookups the routing table and returns * an entry to the caller for later use. */ -struct in6_addr * -in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, - struct ip6_moptions *mopts, struct route_in6 *ro, struct in6_addr *laddr, - int *errorp, u_int rtableid) +int +in6_selectsrc(struct in6_addr **in6src, struct sockaddr_in6 *dstsock, + struct ip6_pktopts *opts, struct ip6_moptions *mopts, + struct route_in6 *ro, struct in6_addr *laddr, u_int rtableid) { struct ifnet *ifp = NULL; struct in6_addr *dst; struct in6_ifaddr *ia6 = NULL; struct in6_pktinfo *pi = NULL; + int error; dst = &dstsock->sin6_addr; - *errorp = 0; /* * If the source address is explicitly specified by the caller, @@ -120,9 +120,9 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, struct sockaddr_in6 sa6; /* get the outgoing interface */ - if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, - &ifp, rtableid)) != 0) - return (NULL); + error = in6_selectif(dstsock, opts, mopts, ro, &ifp, rtableid); + if (error) + return (error); bzero(&sa6, sizeof(sa6)); sa6.sin6_family = AF_INET6; @@ -134,22 +134,23 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, ia6 = ifatoia6(ifa_ifwithaddr(sin6tosa(&sa6), rtableid)); if (ia6 == NULL || - (ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) { - *errorp = EADDRNOTAVAIL; - return (NULL); - } + (ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) + return (EADDRNOTAVAIL); pi->ipi6_addr = sa6.sin6_addr; /* XXX: this overrides pi */ - return (&pi->ipi6_addr); + *in6src = &pi->ipi6_addr; + return (0); } /* * If the source address is not specified but the socket(if any) * is already bound, use the bound address. */ - if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) - return (laddr); + if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) { + *in6src = laddr; + return (0); + } /* * If the caller doesn't specify the source address but @@ -158,16 +159,15 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, */ if (pi && pi->ipi6_ifindex) { ifp = if_get(pi->ipi6_ifindex); - if (ifp == NULL) { - *errorp = ENXIO; /* XXX: better error? */ - return (0); - } + if (ifp == NULL) + return (ENXIO); /* XXX: better error? */ + ia6 = in6_ifawithscope(ifp, dst, rtableid); - if (ia6 == 0) { - *errorp = EADDRNOTAVAIL; - return (0); - } - return (&ia6->ia_addr.sin6_addr); + if (ia6 == NULL) + return (EADDRNOTAVAIL); + + *in6src = &ia6->ia_addr.sin6_addr; + return (0); } /* @@ -182,16 +182,15 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst) || IN6_IS_ADDR_MC_INTFACELOCAL(dst)) && dstsock->sin6_scope_id) { ifp = if_get(dstsock->sin6_scope_id); - if (ifp == NULL) { - *errorp = ENXIO; /* XXX: better error? */ - return (0); - } + if (ifp == NULL) + return (ENXIO); /* XXX: better error? */ + ia6 = in6_ifawithscope(ifp, dst, rtableid); - if (ia6 == 0) { - *errorp = EADDRNOTAVAIL; - return (0); - } - return (&ia6->ia_addr.sin6_addr); + if (ia6 == NULL) + return (EADDRNOTAVAIL); + + *in6src = &ia6->ia_addr.sin6_addr; + return (0); } /* @@ -209,11 +208,11 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, if (ifp) { ia6 = in6_ifawithscope(ifp, dst, rtableid); - if (ia6 == 0) { - *errorp = EADDRNOTAVAIL; - return (0); - } - return (&ia6->ia_addr.sin6_addr); + if (ia6 == NULL) + return (EADDRNOTAVAIL); + + *in6src = &ia6->ia_addr.sin6_addr; + return (0); } } @@ -236,11 +235,11 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, if (ia6 == 0) ia6 = ifatoia6(rt->rt_ifa); } - if (ia6 == 0) { - *errorp = EADDRNOTAVAIL; - return (0); - } - return (&ia6->ia_addr.sin6_addr); + if (ia6 == NULL) + return (EADDRNOTAVAIL); + + *in6src = &ia6->ia_addr.sin6_addr; + return (0); } } @@ -286,15 +285,14 @@ in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, if (ia6 == 0) /* xxx scope error ?*/ ia6 = ifatoia6(ro->ro_rt->rt_ifa); } - if (ia6 == 0) { - *errorp = EHOSTUNREACH; /* no route */ - return (0); - } - return (&ia6->ia_addr.sin6_addr); + if (ia6 == NULL) + return (EHOSTUNREACH); /* no route */ + + *in6src = &ia6->ia_addr.sin6_addr; + return (0); } - *errorp = EADDRNOTAVAIL; - return (0); + return (EADDRNOTAVAIL); } int |