Skip to content

Commit

Permalink
Remove last cppcheck complaint
Browse files Browse the repository at this point in the history
The old code was a mess of ifdefs.  The new code is a bit
better, but still pretty damned bizarre
  • Loading branch information
alandekok committed Feb 13, 2015
1 parent 10a550b commit fec535f
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions src/lib/udpfromto.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,17 @@ int sendfromto(int s, void *buf, size_t len, int flags,
struct sockaddr *to, socklen_t tolen)
{
struct msghdr msgh;
struct cmsghdr *cmsg;
struct iovec iov;
char cbuf[256];

/*
* Unknown address family, die.
*/
if (from && (from->sa_family != AF_INET) && (from->sa_family != AF_INET6)) {
errno = EINVAL;
return -1;
}

#ifdef __FreeBSD__
/*
* FreeBSD is extra pedantic about the use of IP_SENDSRCADDR,
Expand Down Expand Up @@ -345,19 +352,28 @@ int sendfromto(int s, void *buf, size_t len, int flags,
break;
}
#else
# if !defined(IP_PKTINFO) && !defined(IP_SENDSRCADDR) && !defined(IPV6_PKTINFO)

/*
* If the sendmsg() flags aren't defined, fall back to
* using sendto().
*/
from = NULL;
# if !defined(IP_PKTINFO) && !defined(IP_SENDSRCADDR)
if (from && from->sa_family == AF_INET) {
from = NULL;
}
# endif
#endif

# if !defined(IPV6_PKTINFO)
if (from && from->sa_family == AF_INET6) {
from = NULL;
}
# endif
#endif /* !__FreeBSD__ */

/*
* Catch the case where the caller passes invalid arguments.
* No "from", just use regular sendto.
*/
if (!from || (fromlen == 0) || (from->sa_family == AF_UNSPEC)) {
if (!from || (fromlen == 0)) {
return sendto(s, buf, len, flags, to, tolen);
}

Expand All @@ -372,13 +388,12 @@ int sendfromto(int s, void *buf, size_t len, int flags,
msgh.msg_name = to;
msgh.msg_namelen = tolen;

# if defined(IP_PKTINFO) || defined(IP_SENDSRCADDR)
if (from->sa_family == AF_INET) {
#if !defined(IP_PKTINFO) && !defined(IP_SENDSRCADDR)
return sendto(s, buf, len, flags, to, tolen);
#else
struct sockaddr_in *s4 = (struct sockaddr_in *) from;

# ifdef IP_PKTINFO
struct cmsghdr *cmsg;
struct in_pktinfo *pkt;

msgh.msg_control = cbuf;
Expand All @@ -395,6 +410,7 @@ int sendfromto(int s, void *buf, size_t len, int flags,
# endif

# ifdef IP_SENDSRCADDR
struct cmsghdr *cmsg;
struct in_addr *in;

msgh.msg_control = cbuf;
Expand All @@ -408,16 +424,14 @@ int sendfromto(int s, void *buf, size_t len, int flags,
in = (struct in_addr *) CMSG_DATA(cmsg);
*in = s4->sin_addr;
# endif
#endif /* IP_PKTINFO or IP_SENDSRCADDR */
}
#endif

#ifdef AF_INET6
else if (from->sa_family == AF_INET6) {
# if !defined(IPV6_PKTINFO)
return sendto(s, buf, len, flags, to, tolen);
# else
# if defined(IPV6_PKTINFO)
if (from->sa_family == AF_INET6) {
struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) from;

struct cmsghdr *cmsg;
struct in6_pktinfo *pkt;

msgh.msg_control = cbuf;
Expand All @@ -431,17 +445,8 @@ int sendfromto(int s, void *buf, size_t len, int flags,
pkt = (struct in6_pktinfo *) CMSG_DATA(cmsg);
memset(pkt, 0, sizeof(*pkt));
pkt->ipi6_addr = s6->sin6_addr;
# endif /* IPV6_PKTINFO */
}
#endif

/*
* Unknown address family.
*/
else {
errno = EINVAL;
return -1;
}
# endif /* IPV6_PKTINFO */

return sendmsg(s, &msgh, flags);
}
Expand Down

0 comments on commit fec535f

Please sign in to comment.