summaryrefslogtreecommitdiff
path: root/src/net/SocketDescriptor.cxx
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2018-08-21 11:37:19 +0200
committerMax Kellermann <max@musicpd.org>2018-08-21 11:37:19 +0200
commit5d7dd12f7ac33a3f62b32d38117537a69ef48fdc (patch)
tree1c3e0c56da2062572cf6174c6d94c3e449a05ed7 /src/net/SocketDescriptor.cxx
parent21783ff5f7ad84f4e7e1f64bbe5f83d0ae4ab057 (diff)
net/SocketDescriptor: add multicast methods
Diffstat (limited to 'src/net/SocketDescriptor.cxx')
-rw-r--r--src/net/SocketDescriptor.cxx35
1 files changed, 35 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