21 #include <sys/socket.h> 22 #include <netinet/in.h> 37 if (::setsockopt(
fd(), SOL_SOCKET, SO_BROADCAST, &ivalue,
sizeof(ivalue)) < 0)
45 ::socklen_t len (
sizeof(value));
46 if (::getsockopt(
fd(), SOL_SOCKET, SO_BROADCAST, &value, &len) < 0)
55 socklen_t len (
sizeof(value));
56 if (::getsockopt(
fd(),SOL_IP,IP_MULTICAST_LOOP,&value,&len) < 0)
65 if (::setsockopt(
fd(),SOL_IP,IP_MULTICAST_LOOP,&ivalue,
sizeof(ivalue)) < 0)
72 struct ip_mreqn mreqn;
73 ::memset(&mreqn, 0,
sizeof(mreqn));
75 mreqn.imr_ifindex = if_nametoindex(iface.c_str());
76 if (mreqn.imr_ifindex == 0)
79 if (::setsockopt(
fd(),SOL_IP,IP_MULTICAST_IF,&mreqn,
sizeof(mreqn)) < 0)
87 socklen_t len (
sizeof(value));
88 if (::getsockopt(
fd(),SOL_IP,IP_MULTICAST_TTL,&value,&len) < 0)
96 if (::setsockopt(
fd(),SOL_IP,IP_MULTICAST_TTL,&value,
sizeof(value)) < 0)
106 struct ip_mreqn mreqn;
107 mreqn.imr_multiaddr.s_addr = mcAddr.
inaddr();
108 mreqn.imr_address.s_addr = htons(INADDR_ANY);
109 mreqn.imr_ifindex = 0;
110 if (::setsockopt(
fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
118 struct ip_mreqn mreqn;
119 mreqn.imr_multiaddr.s_addr = mcAddr.
inaddr();
120 mreqn.imr_address.s_addr = localAddr.
inaddr();
121 mreqn.imr_ifindex = 0;
122 if (::setsockopt(
fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
127 std::string
const & iface)
130 struct ip_mreqn mreqn;
131 mreqn.imr_multiaddr.s_addr = mcAddr.
inaddr();
132 mreqn.imr_address.s_addr = htons(INADDR_ANY);
133 mreqn.imr_ifindex = if_nametoindex(iface.c_str());
134 if (mreqn.imr_ifindex == 0)
136 if (::setsockopt(
fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
143 struct ip_mreqn mreqn;
144 mreqn.imr_multiaddr.s_addr = mcAddr.
inaddr();
145 mreqn.imr_address.s_addr = htons(INADDR_ANY);
146 mreqn.imr_ifindex = 0;
147 if (::setsockopt(
fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
155 struct ip_mreqn mreqn;
156 mreqn.imr_multiaddr.s_addr = mcAddr.
inaddr();
157 mreqn.imr_address.s_addr = localAddr.
inaddr();
158 mreqn.imr_ifindex = 0;
159 if (::setsockopt(
fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
164 std::string
const & iface)
167 struct ip_mreqn mreqn;
168 mreqn.imr_multiaddr.s_addr = mcAddr.
inaddr();
169 mreqn.imr_address.s_addr = htons(INADDR_ANY);
170 mreqn.imr_ifindex = if_nametoindex(iface.c_str());
171 if (mreqn.imr_ifindex == 0)
173 if (::setsockopt(
fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
181 struct group_source_req req;
182 ::memset(&req, 0,
sizeof(req));
183 req.gsr_interface = if_nametoindex(iface.c_str());
184 if (req.gsr_interface == 0)
186 req.gsr_group.ss_family = AF_INET;
187 reinterpret_cast<struct sockaddr_in&
>(req.gsr_group).sin_addr.s_addr = group.
inaddr();
188 req.gsr_source.ss_family = AF_INET;
189 reinterpret_cast<struct sockaddr_in&
>(req.gsr_source).sin_addr.s_addr = source.
inaddr();
190 if (::setsockopt(fd, SOL_IP, MCAST_JOIN_SOURCE_GROUP, &req,
sizeof(req)) < 0)
197 std::string
const & iface)
200 mc4SSMSourceRequest(MCAST_JOIN_SOURCE_GROUP,
fd(), group, source, iface);
205 std::string
const & iface)
208 mc4SSMSourceRequest(MCAST_LEAVE_SOURCE_GROUP,
fd(), group, source, iface);
218 struct ip_mreqn mreqn;
220 mreqn.imr_address.s_addr = htons(INADDR_ANY);
221 mreqn.imr_ifindex = 0;
222 if (::setsockopt(
fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
226 struct ipv6_mreq mreqn;
227 std::copy(mcAddr.begin(), mcAddr.end(), mreqn.ipv6mr_multiaddr.s6_addr);
228 mreqn.ipv6mr_interface = 0;
229 if (::setsockopt(
fd(),SOL_IPV6,IPV6_ADD_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
235 std::string
const & iface)
239 struct ip_mreqn mreqn;
241 mreqn.imr_address.s_addr = htons(INADDR_ANY);
242 mreqn.imr_ifindex = if_nametoindex(iface.c_str());
243 if (mreqn.imr_ifindex == 0)
245 if (::setsockopt(
fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
249 struct ipv6_mreq mreqn;
250 std::copy(mcAddr.begin(), mcAddr.end(), mreqn.ipv6mr_multiaddr.s6_addr);
251 mreqn.ipv6mr_interface = if_nametoindex(iface.c_str());
252 if (mreqn.ipv6mr_interface == 0)
254 if (::setsockopt(
fd(),SOL_IPV6,IPV6_ADD_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
263 struct ip_mreqn mreqn;
265 mreqn.imr_address.s_addr = htons(INADDR_ANY);
266 mreqn.imr_ifindex = 0;
267 if (::setsockopt(
fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
271 struct ipv6_mreq mreqn;
272 std::copy(mcAddr.begin(), mcAddr.end(), mreqn.ipv6mr_multiaddr.s6_addr);
273 mreqn.ipv6mr_interface = 0;
274 if (::setsockopt(
fd(),SOL_IPV6,IPV6_DROP_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
281 std::string
const & iface)
285 struct ip_mreqn mreqn;
287 mreqn.imr_address.s_addr = htons(INADDR_ANY);
288 mreqn.imr_ifindex = if_nametoindex(iface.c_str());
289 if (mreqn.imr_ifindex == 0)
291 if (::setsockopt(
fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
295 struct ipv6_mreq mreqn;
296 std::copy(mcAddr.begin(), mcAddr.end(), mreqn.ipv6mr_multiaddr.s6_addr);
297 mreqn.ipv6mr_interface = if_nametoindex(iface.c_str());
298 if (mreqn.ipv6mr_interface == 0)
300 if (::setsockopt(
fd(),SOL_IPV6,IPV6_DROP_MEMBERSHIP,&mreqn,
sizeof(mreqn)) < 0)
310 struct group_source_req req;
311 ::memset(&req, 0,
sizeof(req));
312 req.gsr_interface = ifacei;
313 req.gsr_group.ss_family = AF_INET6;
314 std::copy(group.begin(), group.end(),
315 reinterpret_cast<struct sockaddr_in6&
>(req.gsr_group).sin6_addr.s6_addr);
316 req.gsr_source.ss_family = AF_INET6;
317 std::copy(source.begin(), source.end(),
318 reinterpret_cast<struct sockaddr_in6&
>(req.gsr_source).sin6_addr.s6_addr);
319 if (::setsockopt(fd, SOL_IPV6, MCAST_JOIN_SOURCE_GROUP, &req,
sizeof(req)) < 0)
327 if (! iface.empty()) {
328 ifacei = if_nametoindex(iface.c_str());
332 mc6SSMSourceRequest(operation, fd, group, source, ifacei);
339 std::string
const & iface)
342 mc6SSMSourceRequest(MCAST_JOIN_SOURCE_GROUP,
fd(), group, source, iface);
350 mc6SSMSourceRequest(MCAST_JOIN_SOURCE_GROUP,
fd(), group, source, ifacei);
355 std::string
const & iface)
358 mc6SSMSourceRequest(MCAST_LEAVE_SOURCE_GROUP,
fd(), group, source, iface);
void mcAddMembership(INet6Address const &mcAddr) const
Join multicast group on default interface.
INet4Address inet4address() const
Return embedded INet4 address.
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
void mcDropMembership(INet4Address const &mcAddr) const
Leave multicast group.
void mcAddMembership(INet4Address const &mcAddr) const
Join multicast group on default interface.
void mcIface(std::string const &iface=std::string()) const
Set multicast send interface of the socket.
int fd() const
Get file descriptor.
void mcJoinSSMSource(INet6Address const &group, INet6Address const &source, std::string const &iface) const
join SSM multicast group
MulticastSocketProtocol public header.
void mcDropMembership(INet6Address const &mcAddr) const
Leave multicast group.
void mcLeaveSSMSource(INet4Address const &group, INet4Address const &source, std::string const &iface) const
leave SSM multicast group
bool mcLoop() const
Return current multicast loopback state.
bool inet4Mapped() const
true, if address is INet4 mapped
void mcJoinSSMSource(INet4Address const &group, INet4Address const &source, std::string const &iface) const
join SSM multicast group
void mcLeaveSSMSource(INet6Address const &group, INet6Address const &source, std::string const &iface) const
leave SSM multicast group
#define SENF_EXC_DEBUGINFO
inaddr_type inaddr() const
Return the raw network byte order address.
unsigned mcTTL() const
Return current multicast TTL.
bool broadcastEnabled() const
Get broadcast send/receive state.