2007-04-05 20:26:28 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2008-03-02 12:53:15 +00:00
|
|
|
* Copyright (C) 2007 Denys Vlasenko
|
2007-04-05 20:26:28 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2007-04-05 20:26:28 +00:00
|
|
|
*/
|
|
|
|
#include "libbb.h"
|
2007-04-03 01:13:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This asks kernel to let us know dst addr/port of incoming packets
|
|
|
|
* We don't check for errors here. Not supported == won't be used
|
|
|
|
*/
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC
|
2011-08-28 03:31:49 +00:00
|
|
|
socket_want_pktinfo(int fd UNUSED_PARAM)
|
2007-04-03 01:13:04 +00:00
|
|
|
{
|
|
|
|
#ifdef IP_PKTINFO
|
|
|
|
setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &const_int_1, sizeof(int));
|
|
|
|
#endif
|
2007-04-04 10:16:15 +00:00
|
|
|
#if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
|
2007-04-03 01:13:04 +00:00
|
|
|
setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &const_int_1, sizeof(int));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
ssize_t FAST_FUNC
|
2007-04-03 01:13:04 +00:00
|
|
|
send_to_from(int fd, void *buf, size_t len, int flags,
|
2008-03-17 09:02:21 +00:00
|
|
|
const struct sockaddr *to,
|
|
|
|
const struct sockaddr *from,
|
2007-04-03 01:13:04 +00:00
|
|
|
socklen_t tolen)
|
|
|
|
{
|
|
|
|
#ifndef IP_PKTINFO
|
2009-11-26 15:52:25 +00:00
|
|
|
(void)from; /* suppress "unused from" warning */
|
2007-04-03 01:13:04 +00:00
|
|
|
return sendto(fd, buf, len, flags, to, tolen);
|
|
|
|
#else
|
|
|
|
struct iovec iov[1];
|
|
|
|
struct msghdr msg;
|
2008-05-22 17:41:01 +00:00
|
|
|
union {
|
|
|
|
char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
|
2009-11-26 15:52:25 +00:00
|
|
|
# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
|
2008-05-22 17:41:01 +00:00
|
|
|
char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
|
2009-11-26 15:52:25 +00:00
|
|
|
# endif
|
2008-05-22 17:41:01 +00:00
|
|
|
} u;
|
2007-04-03 01:13:04 +00:00
|
|
|
struct cmsghdr* cmsgptr;
|
|
|
|
|
|
|
|
if (from->sa_family != AF_INET
|
2009-11-26 15:52:25 +00:00
|
|
|
# if ENABLE_FEATURE_IPV6
|
2007-04-03 01:13:04 +00:00
|
|
|
&& from->sa_family != AF_INET6
|
2009-11-26 15:52:25 +00:00
|
|
|
# endif
|
2007-04-03 01:13:04 +00:00
|
|
|
) {
|
|
|
|
/* ANY local address */
|
|
|
|
return sendto(fd, buf, len, flags, to, tolen);
|
|
|
|
}
|
2007-05-30 00:29:55 +00:00
|
|
|
|
2007-04-03 01:13:04 +00:00
|
|
|
/* man recvmsg and man cmsg is needed to make sense of code below */
|
|
|
|
|
|
|
|
iov[0].iov_base = buf;
|
|
|
|
iov[0].iov_len = len;
|
|
|
|
|
2008-05-22 17:41:01 +00:00
|
|
|
memset(&u, 0, sizeof(u));
|
2007-04-03 01:13:04 +00:00
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.msg_name = (void *)(struct sockaddr *)to; /* or compiler will annoy us */
|
|
|
|
msg.msg_namelen = tolen;
|
|
|
|
msg.msg_iov = iov;
|
|
|
|
msg.msg_iovlen = 1;
|
2008-05-22 17:41:01 +00:00
|
|
|
msg.msg_control = &u;
|
|
|
|
msg.msg_controllen = sizeof(u);
|
2007-04-03 01:13:04 +00:00
|
|
|
msg.msg_flags = flags;
|
|
|
|
|
|
|
|
cmsgptr = CMSG_FIRSTHDR(&msg);
|
|
|
|
if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
|
|
|
|
struct in_pktinfo *pktptr;
|
|
|
|
cmsgptr->cmsg_level = IPPROTO_IP;
|
|
|
|
cmsgptr->cmsg_type = IP_PKTINFO;
|
|
|
|
cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
|
|
|
|
pktptr = (struct in_pktinfo *)(CMSG_DATA(cmsgptr));
|
2009-11-26 15:52:25 +00:00
|
|
|
/*pktptr->ipi_ifindex = 0; -- already done by memset(u...) */
|
|
|
|
/* In general, CMSG_DATA() can be unaligned, but in this case
|
|
|
|
* we know for sure it is sufficiently aligned:
|
|
|
|
* CMSG_FIRSTHDR simply returns &u above,
|
|
|
|
* and CMSG_DATA returns &u + size_t + int + int.
|
|
|
|
* Thus direct assignment is ok:
|
|
|
|
*/
|
2007-04-03 01:13:04 +00:00
|
|
|
pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr;
|
|
|
|
}
|
2009-11-26 15:52:25 +00:00
|
|
|
# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
|
2007-04-03 01:13:04 +00:00
|
|
|
else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
|
|
|
|
struct in6_pktinfo *pktptr;
|
|
|
|
cmsgptr->cmsg_level = IPPROTO_IPV6;
|
2007-04-04 10:16:15 +00:00
|
|
|
cmsgptr->cmsg_type = IPV6_PKTINFO;
|
2007-04-03 01:13:04 +00:00
|
|
|
cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
|
|
|
|
pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr));
|
2009-11-26 15:52:25 +00:00
|
|
|
/* pktptr->ipi6_ifindex = 0; -- already done by memset(u...) */
|
2007-04-03 01:13:04 +00:00
|
|
|
pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr;
|
|
|
|
}
|
2009-11-26 15:52:25 +00:00
|
|
|
# endif
|
2008-05-22 17:41:01 +00:00
|
|
|
msg.msg_controllen = cmsgptr->cmsg_len;
|
|
|
|
|
2007-04-03 01:13:04 +00:00
|
|
|
return sendmsg(fd, &msg, flags);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-04-04 10:16:15 +00:00
|
|
|
/* NB: this will never set port# in 'to'!
|
|
|
|
* _Only_ IP/IPv6 address part of 'to' is _maybe_ modified.
|
2007-04-04 11:02:55 +00:00
|
|
|
* Typical usage is to preinit 'to' with "default" value
|
2007-04-04 10:16:15 +00:00
|
|
|
* before calling recv_from_to(). */
|
2008-06-27 02:52:20 +00:00
|
|
|
ssize_t FAST_FUNC
|
2007-04-03 01:13:04 +00:00
|
|
|
recv_from_to(int fd, void *buf, size_t len, int flags,
|
|
|
|
struct sockaddr *from, struct sockaddr *to,
|
|
|
|
socklen_t sa_size)
|
|
|
|
{
|
|
|
|
#ifndef IP_PKTINFO
|
2009-11-26 15:52:25 +00:00
|
|
|
(void)to; /* suppress "unused to" warning */
|
2007-04-03 01:13:04 +00:00
|
|
|
return recvfrom(fd, buf, len, flags, from, &sa_size);
|
|
|
|
#else
|
|
|
|
/* man recvmsg and man cmsg is needed to make sense of code below */
|
|
|
|
struct iovec iov[1];
|
|
|
|
union {
|
|
|
|
char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
|
2009-11-26 15:52:25 +00:00
|
|
|
# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
|
2007-04-03 01:13:04 +00:00
|
|
|
char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
|
2009-11-26 15:52:25 +00:00
|
|
|
# endif
|
2007-04-03 01:13:04 +00:00
|
|
|
} u;
|
|
|
|
struct cmsghdr *cmsgptr;
|
|
|
|
struct msghdr msg;
|
2008-05-15 21:30:45 +00:00
|
|
|
ssize_t recv_length;
|
2007-04-03 01:13:04 +00:00
|
|
|
|
|
|
|
iov[0].iov_base = buf;
|
|
|
|
iov[0].iov_len = len;
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.msg_name = (struct sockaddr *)from;
|
|
|
|
msg.msg_namelen = sa_size;
|
|
|
|
msg.msg_iov = iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = &u;
|
|
|
|
msg.msg_controllen = sizeof(u);
|
|
|
|
|
|
|
|
recv_length = recvmsg(fd, &msg, flags);
|
|
|
|
if (recv_length < 0)
|
|
|
|
return recv_length;
|
|
|
|
|
2009-11-26 15:52:25 +00:00
|
|
|
# define to4 ((struct sockaddr_in*)to)
|
|
|
|
# define to6 ((struct sockaddr_in6*)to)
|
2007-04-03 01:13:04 +00:00
|
|
|
/* Here we try to retrieve destination IP and memorize it */
|
|
|
|
for (cmsgptr = CMSG_FIRSTHDR(&msg);
|
|
|
|
cmsgptr != NULL;
|
|
|
|
cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)
|
|
|
|
) {
|
|
|
|
if (cmsgptr->cmsg_level == IPPROTO_IP
|
|
|
|
&& cmsgptr->cmsg_type == IP_PKTINFO
|
|
|
|
) {
|
2010-02-04 17:41:18 +00:00
|
|
|
const int IPI_ADDR_OFF = offsetof(struct in_pktinfo, ipi_addr);
|
2007-04-03 01:13:04 +00:00
|
|
|
to->sa_family = AF_INET;
|
2010-02-04 17:41:18 +00:00
|
|
|
/*# define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) )*/
|
2009-11-26 15:52:25 +00:00
|
|
|
/*to4->sin_addr = pktinfo(cmsgptr)->ipi_addr; - may be unaligned */
|
2010-02-04 17:41:18 +00:00
|
|
|
memcpy(&to4->sin_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI_ADDR_OFF, sizeof(to4->sin_addr));
|
2009-11-26 15:52:25 +00:00
|
|
|
/*to4->sin_port = 123; - this data is not supplied by kernel */
|
2007-04-03 01:13:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-11-26 15:52:25 +00:00
|
|
|
# if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
|
2007-04-03 01:13:04 +00:00
|
|
|
if (cmsgptr->cmsg_level == IPPROTO_IPV6
|
2007-04-04 10:16:15 +00:00
|
|
|
&& cmsgptr->cmsg_type == IPV6_PKTINFO
|
2007-04-03 01:13:04 +00:00
|
|
|
) {
|
2010-02-04 17:41:18 +00:00
|
|
|
const int IPI6_ADDR_OFF = offsetof(struct in6_pktinfo, ipi6_addr);
|
2007-04-03 01:13:04 +00:00
|
|
|
to->sa_family = AF_INET6;
|
2010-02-04 17:41:18 +00:00
|
|
|
/*# define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) )*/
|
2009-11-26 15:52:25 +00:00
|
|
|
/*to6->sin6_addr = pktinfo(cmsgptr)->ipi6_addr; - may be unaligned */
|
2010-02-04 17:41:18 +00:00
|
|
|
memcpy(&to6->sin6_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI6_ADDR_OFF, sizeof(to6->sin6_addr));
|
2009-11-26 15:52:25 +00:00
|
|
|
/*to6->sin6_port = 123; */
|
2007-04-03 01:13:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-11-26 15:52:25 +00:00
|
|
|
# endif
|
2007-04-03 01:13:04 +00:00
|
|
|
}
|
|
|
|
return recv_length;
|
|
|
|
#endif
|
|
|
|
}
|