diff options
author | Max Kellermann <max@musicpd.org> | 2018-08-21 11:37:19 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2018-08-21 11:37:19 +0200 |
commit | 5d7dd12f7ac33a3f62b32d38117537a69ef48fdc (patch) | |
tree | 1c3e0c56da2062572cf6174c6d94c3e449a05ed7 /src/net | |
parent | 21783ff5f7ad84f4e7e1f64bbe5f83d0ae4ab057 (diff) |
net/SocketDescriptor: add multicast methods
Diffstat (limited to 'src/net')
-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; |