net/SocketDescriptor: add multicast methods

This commit is contained in:
Max Kellermann 2018-08-21 11:37:19 +02:00
parent 21783ff5f7
commit 5d7dd12f7a
2 changed files with 41 additions and 0 deletions

View File

@ -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

View File

@ -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;