diff options
-rw-r--r-- | src/net/SocketDescriptor.cxx | 35 | ||||
-rw-r--r-- | src/net/SocketDescriptor.hxx | 6 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/net/SocketDescriptor.cxx b/src/net/SocketDescriptor.cxx index 25d0b0252..db15cd32d 100644 --- a/src/net/SocketDescriptor.cxx +++ b/src/net/SocketDescriptor.cxx @@ -31,6 +31,8 @@ #include "SocketDescriptor.hxx" #include "SocketAddress.hxx" #include "StaticSocketAddress.hxx" +#include "IPv4Address.hxx" +#include "IPv6Address.hxx" #ifdef _WIN32 #include <winsock2.h> @@ -335,6 +337,39 @@ SocketDescriptor::SetTcpFastOpen(int qlen) noexcept #endif +bool +SocketDescriptor::AddMembership(const IPv4Address &address) noexcept +{ + struct ip_mreq r{address.GetAddress(), IPv4Address(0).GetAddress()}; + return setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, + &r, sizeof(r)) == 0; +} + +bool +SocketDescriptor::AddMembership(const IPv6Address &address) noexcept +{ + struct ipv6_mreq r{address.GetAddress(), 0}; + r.ipv6mr_interface = address.GetScopeId(); + return setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, + &r, sizeof(r)) == 0; +} + +bool +SocketDescriptor::AddMembership(SocketAddress address) noexcept +{ + switch (address.GetFamily()) { + case AF_INET: + return AddMembership(IPv4Address(address)); + + case AF_INET6: + return AddMembership(IPv6Address(address)); + + default: + errno = EINVAL; + return false; + } +} + #endif bool diff --git a/src/net/SocketDescriptor.hxx b/src/net/SocketDescriptor.hxx index 212e10c7c..2a4833dd0 100644 --- a/src/net/SocketDescriptor.hxx +++ b/src/net/SocketDescriptor.hxx @@ -36,6 +36,8 @@ class SocketAddress; class StaticSocketAddress; +class IPv4Address; +class IPv6Address; /** * An OO wrapper for a UNIX socket descriptor. @@ -193,6 +195,10 @@ public: bool SetBindToDevice(const char *name) noexcept; bool SetTcpFastOpen(int qlen=16) noexcept; + + bool AddMembership(const IPv4Address &address) noexcept; + bool AddMembership(const IPv6Address &address) noexcept; + bool AddMembership(SocketAddress address) noexcept; #endif bool Bind(SocketAddress address) noexcept; |