Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
McastSock.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_NET_MCASTSOCK_HPP
18#define TOOLBOX_NET_MCASTSOCK_HPP
19
23
24namespace toolbox {
25inline namespace net {
26
28 explicit IpMcastGroup(const IpAddr& addr, unsigned ifindex = 0) noexcept;
29
30 IpMcastGroup(const IpAddr& addr, const char* ifname, std::error_code& ec) noexcept
31 : IpMcastGroup(addr, os::if_nametoindex(ifname, ec))
32 {
33 }
34 IpMcastGroup(const IpAddr& addr, const char* ifname)
35 : IpMcastGroup(addr, os::if_nametoindex(ifname))
36 {
37 }
38
39 int family;
40 union {
41 ip_mreqn ipv4;
42 ipv6_mreq ipv6;
43 };
44};
45
47inline void join_group(int sockfd, const IpMcastGroup& group, std::error_code& ec) noexcept
48{
49 if (group.family == AF_INET6) {
51 } else {
52 assert(group.family == AF_INET);
54 }
55}
56
58inline void join_group(int sockfd, const IpMcastGroup& group)
59{
60 if (group.family == AF_INET6) {
62 } else {
63 assert(group.family == AF_INET);
65 }
66}
67
69inline void join_group(int sockfd, const IpAddr& addr, unsigned ifindex,
70 std::error_code& ec) noexcept
71{
73}
74
76inline void join_group(int sockfd, const IpAddr& addr, unsigned ifindex)
77{
79}
80
82inline void join_group(int sockfd, const IpAddr& addr, const char* ifname,
83 std::error_code& ec) noexcept
84{
86}
87
89inline void join_group(int sockfd, const IpAddr& addr, const char* ifname)
90{
92}
93
95inline void leave_group(int sockfd, const IpMcastGroup& group, std::error_code& ec) noexcept
96{
97 if (group.family == AF_INET6) {
99 } else {
100 assert(group.family == AF_INET);
102 }
103}
104
106inline void leave_group(int sockfd, const IpMcastGroup& group)
107{
108 if (group.family == AF_INET6) {
110 } else {
111 assert(group.family == AF_INET);
113 }
114}
115
117inline void leave_group(int sockfd, const IpAddr& addr, unsigned ifindex,
118 std::error_code& ec) noexcept
119{
121}
122
124inline void leave_group(int sockfd, const IpAddr& addr, unsigned ifindex)
125{
127}
128
130inline void leave_group(int sockfd, const IpAddr& addr, const char* ifname,
131 std::error_code& ec) noexcept
132{
134}
135
137inline void leave_group(int sockfd, const IpAddr& addr, const char* ifname)
138{
140}
141
142inline void set_ip_mcast_if(int sockfd, int family, unsigned ifindex, std::error_code& ec) noexcept
143{
144 if (family == AF_INET6) {
146 } else {
147 assert(family == AF_INET);
148 ip_mreqn mreqn{};
149 mreqn.imr_ifindex = ifindex;
151 }
152}
153
154inline void set_ip_mcast_if(int sockfd, int family, unsigned ifindex)
155{
156 if (family == AF_INET6) {
158 } else {
159 assert(family == AF_INET);
160 ip_mreqn mreqn{};
161 mreqn.imr_ifindex = ifindex;
163 }
164}
165
166inline void set_ip_mcast_if(int sockfd, int family, const char* ifname,
167 std::error_code& ec) noexcept
168{
169 const auto ifindex = os::if_nametoindex(ifname, ec);
170 if (!ec) {
171 set_ip_mcast_if(sockfd, family, ifindex, ec);
172 }
173}
174
175inline void set_ip_mcast_if(int sockfd, int family, const char* ifname)
176{
178}
179
181inline void set_ip_mcast_loop(int sockfd, int family, bool enabled, std::error_code& ec) noexcept
182{
183 if (family == AF_INET6) {
184 const unsigned optval{enabled ? 1U : 0U};
186 } else {
187 assert(family == AF_INET);
188 const unsigned char optval = enabled ? 1 : 0;
190 }
191}
192
194inline void set_ip_mcast_loop(int sockfd, int family, bool enabled)
195{
196 if (family == AF_INET6) {
197 const unsigned optval{enabled ? 1U : 0U};
199 } else {
200 assert(family == AF_INET);
201 const unsigned char optval = enabled ? 1 : 0;
203 }
204}
205
207inline void set_ip_mcast_ttl(int sockfd, int family, int ttl, std::error_code& ec) noexcept
208{
209 if (family == AF_INET6) {
210 const int optval{ttl};
212 } else {
213 assert(family == AF_INET);
214 const unsigned char optval = ttl;
216 }
217}
218
220inline void set_ip_mcast_ttl(int sockfd, int family, int ttl)
221{
222 if (family == AF_INET6) {
223 const int optval{ttl};
225 } else {
226 assert(family == AF_INET);
227 const unsigned char optval = ttl;
229 }
230}
231
236
237 using IoSock::IoSock;
238
239 McastSock(Protocol protocol, std::error_code& ec) noexcept
240 : IoSock{os::socket(protocol, ec), protocol.family()}
241 {
242 }
243 explicit McastSock(Protocol protocol)
244 : IoSock{os::socket(protocol), protocol.family()}
245 {
246 }
248
249 // Logically const.
250 void get_sock_name(Endpoint& ep, std::error_code& ec) noexcept
251 {
253 }
255 void bind(const Endpoint& ep, std::error_code& ec) noexcept { os::bind(get(), ep, ec); }
256 void bind(const Endpoint& ep) { os::bind(get(), ep); }
257 void connect(const Endpoint& ep, std::error_code& ec) noexcept
258 {
259 return os::connect(get(), ep, ec);
260 }
261 void connect(const Endpoint& ep) { return os::connect(get(), ep); }
262
263 ssize_t recvfrom(void* buf, std::size_t len, int flags, Endpoint& ep,
264 std::error_code& ec) noexcept
265 {
266 return os::recvfrom(get(), buf, len, flags, ep, ec);
267 }
268 std::size_t recvfrom(void* buf, std::size_t len, int flags, Endpoint& ep)
269 {
270 return os::recvfrom(get(), buf, len, flags, ep);
271 }
272
273 ssize_t recvfrom(MutableBuffer buf, int flags, Endpoint& ep, std::error_code& ec) noexcept
274 {
275 return os::recvfrom(get(), buf, flags, ep, ec);
276 }
278 {
279 return os::recvfrom(get(), buf, flags, ep);
280 }
281
282 ssize_t sendto(const void* buf, std::size_t len, int flags, const Endpoint& ep,
283 std::error_code& ec) noexcept
284 {
285 return os::sendto(get(), buf, len, flags, ep, ec);
286 }
287 std::size_t sendto(const void* buf, std::size_t len, int flags, const Endpoint& ep)
288 {
289 return os::sendto(get(), buf, len, flags, ep);
290 }
291
292 ssize_t sendto(ConstBuffer buf, int flags, const Endpoint& ep, std::error_code& ec) noexcept
293 {
294 return os::sendto(get(), buf, flags, ep, ec);
295 }
296 std::size_t sendto(ConstBuffer buf, int flags, const Endpoint& ep)
297 {
298 return os::sendto(get(), buf, flags, ep);
299 }
300
301 void join_group(const IpAddr& addr, unsigned ifindex, std::error_code& ec) noexcept
302 {
304 }
305 void join_group(const IpAddr& addr, unsigned ifindex)
306 {
308 }
309
310 void join_group(const IpAddr& addr, const char* ifname, std::error_code& ec) noexcept
311 {
312 return toolbox::join_group(get(), addr, ifname, ec);
313 }
314 void join_group(const IpAddr& addr, const char* ifname)
315 {
317 }
318
319 void leave_group(const IpAddr& addr, unsigned ifindex, std::error_code& ec) noexcept
320 {
322 }
323 void leave_group(const IpAddr& addr, unsigned ifindex)
324 {
326 }
327
328 void leave_group(const IpAddr& addr, const char* ifname, std::error_code& ec) noexcept
329 {
331 }
332 void leave_group(const IpAddr& addr, const char* ifname)
333 {
335 }
336
337 void set_ip_mcast_if(const char* ifname, std::error_code& ec) noexcept
338 {
340 }
341 void set_ip_mcast_if(const char* ifname)
342 {
344 }
345
346 void set_ip_mcast_loop(bool enabled, std::error_code& ec) noexcept
347 {
349 }
351 {
353 }
354
355 void set_ip_mcast_ttl(int ttl, std::error_code& ec) noexcept
356 {
358 }
360};
361
362} // namespace net
363} // namespace toolbox
364
365#endif // TOOLBOX_NET_MCASTSOCK_HPP
#define TOOLBOX_API
Definition Config.h:39
STL namespace.
boost::asio::const_buffer ConstBuffer
Definition Buffer.hpp:28
boost::asio::mutable_buffer MutableBuffer
Definition Buffer.hpp:29
void set_ip_mcast_if(int sockfd, int family, unsigned ifindex, std::error_code &ec) noexcept
void set_ip_mcast_ttl(int sockfd, int family, int ttl, std::error_code &ec) noexcept
Set or read the time-to-live value of outgoing multicast packets for this socket.
void leave_group(int sockfd, const IpMcastGroup &group, std::error_code &ec) noexcept
Leave a multicast group.
Definition McastSock.hpp:95
void set_ip_mcast_loop(int sockfd, int family, bool enabled, std::error_code &ec) noexcept
Determines whether sent multicast packets should be looped back to the local sockets.
boost::asio::ip::address IpAddr
Definition IpAddr.hpp:24
void join_group(int sockfd, const IpMcastGroup &group, std::error_code &ec) noexcept
Join a multicast group.
Definition McastSock.hpp:47
IpEndpoint< UdpProtocol > UdpEndpoint
Definition Endpoint.hpp:40
void setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen, std::error_code &ec) noexcept
Set socket option.
Definition Socket.hpp:596
void bind(int sockfd, const sockaddr &addr, socklen_t addrlen, std::error_code &ec) noexcept
Bind a name to a socket.
Definition Socket.hpp:261
ssize_t recvfrom(int sockfd, void *buf, std::size_t len, int flags, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Receive a message from a socket.
Definition Socket.hpp:386
void getsockname(int sockfd, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Get the socket name.
Definition Socket.hpp:538
unsigned if_nametoindex(const char *ifname, std::error_code &ec) noexcept
Returns the index of the network interface corresponding to the name ifname.
Definition Socket.hpp:100
FileHandle socket(int family, int type, int protocol, std::error_code &ec) noexcept
Create an endpoint for communication.
Definition Socket.hpp:124
void connect(int sockfd, const sockaddr &addr, socklen_t addrlen, std::error_code &ec) noexcept
Initiate a connection on a socket.
Definition Socket.hpp:291
ssize_t sendto(int sockfd, const void *buf, std::size_t len, int flags, const sockaddr &addr, socklen_t addrlen, std::error_code &ec) noexcept
Send a message on a socket.
Definition Socket.hpp:483
constexpr const auto & get(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s, TagT tag={})
Definition Struct.hpp:110
constexpr auto bind() noexcept
Definition Slot.hpp:92
IoSock() noexcept=default
IpMcastGroup(const IpAddr &addr, const char *ifname)
Definition McastSock.hpp:34
IpMcastGroup(const IpAddr &addr, const char *ifname, std::error_code &ec) noexcept
Definition McastSock.hpp:30
Connectionless Datagram Socket. All state is in base class, so object can be sliced.
McastSock() noexcept=default
void bind(const Endpoint &ep, std::error_code &ec) noexcept
void leave_group(const IpAddr &addr, const char *ifname)
void get_sock_name(Endpoint &ep)
void join_group(const IpAddr &addr, unsigned ifindex)
ssize_t sendto(const void *buf, std::size_t len, int flags, const Endpoint &ep, std::error_code &ec) noexcept
void set_ip_mcast_ttl(int ttl, std::error_code &ec) noexcept
void join_group(const IpAddr &addr, const char *ifname)
void connect(const Endpoint &ep, std::error_code &ec) noexcept
void set_ip_mcast_loop(bool enabled)
ssize_t recvfrom(MutableBuffer buf, int flags, Endpoint &ep, std::error_code &ec) noexcept
void leave_group(const IpAddr &addr, unsigned ifindex)
std::size_t recvfrom(void *buf, std::size_t len, int flags, Endpoint &ep)
std::size_t sendto(const void *buf, std::size_t len, int flags, const Endpoint &ep)
void join_group(const IpAddr &addr, unsigned ifindex, std::error_code &ec) noexcept
McastSock(Protocol protocol)
ssize_t sendto(ConstBuffer buf, int flags, const Endpoint &ep, std::error_code &ec) noexcept
void leave_group(const IpAddr &addr, const char *ifname, std::error_code &ec) noexcept
void set_ip_mcast_ttl(int ttl)
void bind(const Endpoint &ep)
void join_group(const IpAddr &addr, const char *ifname, std::error_code &ec) noexcept
McastSock(Protocol protocol, std::error_code &ec) noexcept
void set_ip_mcast_loop(bool enabled, std::error_code &ec) noexcept
void set_ip_mcast_if(const char *ifname)
void leave_group(const IpAddr &addr, unsigned ifindex, std::error_code &ec) noexcept
std::size_t recvfrom(MutableBuffer buf, int flags, Endpoint &ep)
void set_ip_mcast_if(const char *ifname, std::error_code &ec) noexcept
std::size_t sendto(ConstBuffer buf, int flags, const Endpoint &ep)
void connect(const Endpoint &ep)
void get_sock_name(Endpoint &ep, std::error_code &ec) noexcept
ssize_t recvfrom(void *buf, std::size_t len, int flags, Endpoint &ep, std::error_code &ec) noexcept
int family() const noexcept
Definition Socket.hpp:788