}
if (IN_SET(socket_address_family(a), AF_INET, AF_INET6)) {
- if (bind_to_device)
- if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
- return -errno;
+ if (bind_to_device) {
+ r = socket_bind_to_ifname(fd, bind_to_device);
+ if (r < 0)
+ return r;
+ }
if (reuse_port) {
r = setsockopt_int(fd, SOL_SOCKET, SO_REUSEPORT, true);
.in.sin_addr.s_addr = address,
};
_cleanup_close_ int s = -1;
- char ifname[IF_NAMESIZE] = "";
int r;
s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
return r;
if (ifindex > 0) {
- if (if_indextoname(ifindex, ifname) == 0)
- return -errno;
-
- r = setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
+ r = socket_bind_to_ifindex(s, ifindex);
if (r < 0)
- return -errno;
+ return r;
}
if (address == INADDR_ANY) {
static int icmp6_bind_router_message(const struct icmp6_filter *filter,
const struct ipv6_mreq *mreq) {
- int index = mreq->ipv6mr_interface;
+ int ifindex = mreq->ipv6mr_interface;
_cleanup_close_ int s = -1;
- char ifname[IF_NAMESIZE] = "";
int r;
s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_ICMPV6);
IPV6_PKTINFO socket option also applies for ICMPv6 multicast.
Empirical experiments indicates otherwise and therefore an
IPV6_MULTICAST_IF socket option is used here instead */
- r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, index);
+ r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, ifindex);
if (r < 0)
return r;
if (r < 0)
return r;
- if (if_indextoname(index, ifname) == 0)
- return -errno;
-
- r = setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
+ r = socket_bind_to_ifindex(s, ifindex);
if (r < 0)
- return -errno;
+ return r;
return TAKE_FD(s);
}
return r;
/* Make sure no traffic from outside the local host can leak to onto this socket */
- if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, "lo", 3) < 0)
- return -errno;
+ r = socket_bind_to_ifindex(fd, LOOPBACK_IFINDEX);
+ if (r < 0)
+ return r;
if (bind(fd, &sa.sa, sizeof(sa.in)) < 0)
return -errno;
return r;
/* Make sure no traffic from outside the local host can leak to onto this socket */
- if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, "lo", 3) < 0)
- return -errno;
+ r = socket_bind_to_ifindex(fd, LOOPBACK_IFINDEX);
+ if (r < 0)
+ return r;
if (bind(fd, &sa.sa, sizeof(sa.in)) < 0)
return -errno;