Revert to Current Include Style
[ACE_TAO.git] / ACE / ace / SOCK_Dgram_Mcast.h
blob5c4c835eccdf5a6781f73ceab6f969557b03f183
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file SOCK_Dgram_Mcast.h
7 * @author Irfan Pyrali <irfan@cs.wustl.edu>
8 * @author Tim Harrison <harrison@cs.wustl.edu>
9 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
10 * @author Bill Fulton <bill_a_fulton@raytheon.com>
11 * @author Don Hinton <dhinton@objectsciences.com>
13 //=============================================================================
15 #ifndef ACE_SOCK_DGRAM_MCAST_H
16 #define ACE_SOCK_DGRAM_MCAST_H
18 #include /**/ "ace/pre.h"
20 #include /**/ "ace/ACE_export.h"
22 #if !defined (ACE_LACKS_PRAGMA_ONCE)
23 # pragma once
24 #endif /* ACE_LACKS_PRAGMA_ONCE */
26 #include "ace/SOCK_Dgram.h"
27 #include "ace/INET_Addr.h"
29 #if defined (ACE_SOCK_DGRAM_MCAST_DUMPABLE)
30 # include "ace/Containers_T.h"
31 # include "ace/Synch_Traits.h"
32 # include "ace/Thread_Mutex.h"
33 # if !defined (ACE_SDM_LOCK)
34 # define ACE_SDM_LOCK ACE_SYNCH_MUTEX
35 # endif /* ACE_SDM_LOCK */
36 #endif /* ACE_SOCK_DGRAM_MCAST_DUMPABLE */
38 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
40 /**
41 * @class ACE_SOCK_Dgram_Mcast
43 * @brief Defines the ACE socket wrapper for UDP/IP multicast.
45 * Supports multiple simultaneous subscriptions, unsubscription from one or
46 * all subscriptions, and independent send/recv address and interface
47 * specifications. Constructor arguments determine per-instance optional
48 * functionality.
50 * Note that multicast semantics and implementation details are @i very
51 * environment-specific; this class is just a wrapper around the underlying
52 * implementation and does not try to normalize the concept of multicast
53 * communications.
55 * Usage Notes:
56 * - Send and receive addresses and network interfaces, but not port number,
57 * are independent. While this instance is open, one send interface (and a
58 * default send address) is in effect and 0, 1, or multiple receive
59 * addresses/interfaces are in effect.
60 * - The first open()/subscribe() invocation defines the network interface
61 * and default address used for all sends by this instance, defines the
62 * port number and optionally the multicast address bound to the underlying
63 * socket, and defines the (one) port number that is used for all subscribes
64 * (subsequent subscribes must pass the same port number or 0).
65 * - The default loopback state is not explicitly set; the environment will
66 * determine the default state. Note that some environments (e.g. some
67 * Windows versions) do not allow the default to be changed, and that the
68 * semantics of loopback control are environment dependent (e.g. sender vs.
69 * receiver control).
70 * - In general, due to multicast design and implementation quirks/bugs, it is
71 * difficult to tell which address a received message was sent to or which
72 * interface it was received on (even if only one subscription is active).
73 * However, there are filtering options that can be applied, to narrow it
74 * down considerably.
76 * Interface specification notes (for subscribe() and unsubscribe()):
77 * - If @a net_if == 0, the @c OPT_NULLIFACE_ALL and @c OPT_NULLIFACE_ONE
78 * options determine whether only the system default interface
79 * (if @c OPT_NULLIFACE_ONE is set) or all interfaces (if
80 * @c OPT_NULLIFACE_ALL is set) is affected. Specifying all interfaces
81 * functions correctly only on:
82 * + Windows
83 * + Platforms with the ACE_HAS_GETIFADDRS config setting (includes Linux)
84 * + Platforms which accept the IP address as an interface
85 * name/specification
86 * + Systems with only one non-loopback interface.
87 * Other platforms require additional supporting code.
88 * - Multiple subscriptions for the same address but different interfaces is
89 * normally supported, but re-subscription to an address/interface that is
90 * already subscribed is normally not allowed.
91 * - The @a net_if interface specification syntax is environment-specific.
92 * UNIX systems will normally use device specifications such as "le0" or
93 * "elxl1", while other systems will use the IP address of the interface.
94 * Some platforms, such as pSoS, support only cardinal numbers as network
95 * interface specifications; for these platforms, just give these numbers in
96 * string form and join() will convert them into numbers.
98 class ACE_Export ACE_SOCK_Dgram_Mcast : public ACE_SOCK_Dgram
100 public:
103 * @brief Option parameters.
105 * These control per-instance optional functionality. They are set via
106 * an optional constructor argument.
108 * @note Certain option values are not valid for all environments (see
109 * comments in source file for environment-specific restrictions). Default
110 * values are always valid values for the compilation environment.
112 enum options
114 /* Define whether a specific multicast address (in addition to the port
115 * number) is bound to the socket.
116 * @note:
117 * - Effect of doing this is stack/environment dependent, but in most
118 * environments can be used to filter out unwanted unicast, broadcast,
119 * and (other) multicast messages sent to the same port number.
120 * - Some IP stacks (e.g. some Windows) do not support binding multicast
121 * addresses. Using this option will always cause an open() error.
122 * - It's not strictly possible for user code to do this level of filtering
123 * without the bind; some environments support ways to determine which
124 * address a message was sent to, but this class interface does not
125 * support access to that information.
126 * - The address (and port number) passed to open() (or the first
127 * join(), if open() is not explicitly invoked) is the one that is bound.
130 /// Disable address bind. (Bind only port.)
131 /// @note This might seem odd, but we need a way to distinguish between
132 /// default behavior, which might or might not be to bind, and explicitly
133 /// choosing to bind or not to bind--which "is the question." ;-)
134 OPT_BINDADDR_NO = 0,
135 /// Enable address bind. (Bind port and address.)
136 OPT_BINDADDR_YES = 1,
137 /// Default value for BINDADDR option. (Environment-dependent.)
138 #if defined (ACE_LACKS_PERFECT_MULTICAST_FILTERING) \
139 && (ACE_LACKS_PERFECT_MULTICAST_FILTERING == 1)
140 /// Platforms that don't support perfect filtering. Note that perfect
141 /// filtering only really applies to multicast traffic, not unicast
142 /// or broadcast.
143 DEFOPT_BINDADDR = OPT_BINDADDR_YES,
144 # else
145 /// At least some Win32 OS's can not bind mcast addr, so disable it.
146 /// General-purpose default behavior is 'disabled', since effect is
147 /// environment-specific and side-effects might be surprising.
148 DEFOPT_BINDADDR = OPT_BINDADDR_NO,
149 #endif /* ACE_LACKS_PERFECT_MULTICAST_FILTERING = 1) */
152 * Define the interpretation of NULL as a join interface specification.
153 * If the interface part of a multicast address specification is NULL, it
154 * will be interpreted to mean either "the default interface" or "all
155 * interfaces", depending on the setting of this option.
156 * @note
157 * - The @c OPT_NULLIFACE_ALL option can be used only in the following
158 * environments:
159 * + Windows
160 * + Platforms with the ACE_HAS_GETIFADDRS config setting (includes
161 * Linux)
162 * + Platforms which accept the IP address as an interface
163 * name/specification and for which
164 * ACE_Sock_Connect::get_ip_interfaces() is fully implemented
165 * + Systems with only one non-loopback interface.
166 * Other platforms require additional supporting code.
167 * - The default behavior in most IP stacks is to use the default
168 * interface where "default" has rather ad-hoc semantics.
169 * - This applies only to receives, not sends (which always use only one
170 * interface; NULL means use the "system default" interface).
172 /// Supported values:
173 /// If (net_if==NULL), use default interface.
174 /// @note This might seem odd, but we need a way to distinguish between
175 /// default behavior, which might or might not be to bind, and explicitly
176 /// choosing to bind or not to bind--which "is the question." ;-)
177 OPT_NULLIFACE_ONE = 0,
178 /// If (net_if==NULL), use all mcast interfaces.
179 OPT_NULLIFACE_ALL = 2,
180 /// Default value for NULLIFACE option. (Environment-dependent.)
181 #ifdef ACE_WIN32
182 /// This is the (ad-hoc) legacy behavior for Win32/WinSock.
183 /// @note Older version of WinSock/MSVC may not get all multicast-capable
184 /// interfaces (e.g. PPP interfaces).
185 DEFOPT_NULLIFACE = OPT_NULLIFACE_ALL,
186 #else
187 /// General-purpose default behavior (as per legacy behavior).
188 DEFOPT_NULLIFACE = OPT_NULLIFACE_ONE,
189 #endif /* ACE_WIN32 */
190 /// All default options.
191 DEFOPTS = DEFOPT_BINDADDR | DEFOPT_NULLIFACE
194 /// Create an unitialized instance and define per-instance optional
195 /// functionality.
197 * You must invoke open() or join(), to create/bind a socket and define
198 * operational parameters, before performing any I/O with this instance.
200 ACE_SOCK_Dgram_Mcast (options opts = DEFOPTS);
202 /// Release all resources and implicitly or explicitly unsubscribe
203 /// from all currently subscribed groups.
205 * The OPT_DTORUNSUB_YES_ option defines whether an explicit unsubscribe() is
206 * done by the destructor. If not, most systems will automatically
207 * unsubscribe upon the close of the socket.
209 ~ACE_SOCK_Dgram_Mcast ();
212 * Explicitly open/bind the socket and define the network interface
213 * and default multicast address used for sending messages.
215 * This method is optional; if not explicitly invoked, it is invoked by
216 * the first join(), using the subscribed address/port number and network
217 * interface parameters.
219 * @param mcast_addr Defines the default send address/port number and,
220 * if the @c OPT_BINDADDR_YES option is used, the multicast address
221 * that is bound to this socket. The port number in @a mcast_addr
222 * may be 0, in which case a system-assigned (ephemeral) port number
223 * is used for sending and receiving.
225 * @param net_if If @a net_if is not 0, it defines the network interface
226 * used for all sends by this instance, otherwise the system default
227 * interface is used. (The @a net_if parameter is ignored if this
228 * feature is not supported by the environment.)
230 * @param reuse_addr If @a reuse_addr is not 0, the @c SO_REUSEADDR option
231 * and, if it is supported, the SO_REUSEPORT option are enabled.
233 * @retval 0 on success
234 * @retval -1 if the call fails. Failure can occur due to problems with
235 * the address, port, and/or interface parameters or during system open()
236 * or socket option processing.
238 int open (const ACE_INET_Addr &mcast_addr,
239 const ACE_TCHAR *net_if = 0,
240 int reuse_addr = 1);
242 // = Multicast group subscribe/unsubscribe routines.
244 /// Join a multicast group on a given interface (or all interfaces, if
245 /// supported).
247 * The given group is joined on the specified interface. If option
248 * OPT_NULLIFACE_ALL is used and @a net_if is = 0, the group is joined on
249 * all multicast capable interfaces (IFF supported). Multiple subscriptions
250 * to various address and interface combinations are supported and tracked.
251 * If this is the first invocation of subscribe(), and open() was not
252 * previously invoked, open() will be invoked using @a mcast_addr for binding
253 * the socket and @a net_if as the interface for send().
255 * Returns: -1 if the call fails. Failure can occur due to problems with
256 * the address, port#, and/or interface parameters or during the subscription
257 * attempt. Once bind() has been invoked (by the first open() or
258 * subscribe()), returns errno of ENXIO if the port# is not 0 and does not
259 * match the bound port#, or if OPT_BINDADDR_YES option is used
260 * and the address does not match the bound address. Returns errno of
261 * ENODEV if the addr/port#/interface parameters appeared valid, but no
262 * subscription(s) succeeded. An error is unconditionally returned if
263 * option OPT_NULLIFACE_ALL is used, @a net_if is NULL, and
264 * ACE_Sock_Connect::get_ip_interfaces() is not implemented in this
265 * environment.
267 * Note that the optional @a reuse_addr parameter does not apply to
268 * subscriptions; it is only used if open() is implicitly invoked (see above).
270 * Uses the mcast_addr to determine protocol_family, and protocol which
271 * we always pass as 0 anyway.
273 int join (const ACE_INET_Addr &mcast_addr,
274 int reuse_addr = 1, // (see above)
275 const ACE_TCHAR *net_if = 0);
278 /// Leave a multicast group on a given interface (or all interfaces, if
279 /// supported).
281 * The specified group/interface combination is unsubscribed. If option
282 * OPT_NULLIFACE_ALL is used and @a net_if is = 0, the group is unsubscribed
283 * from all interfaces (IFF supported).
285 * Returns: -1 if the unsubscribe failed. Most environments will return -1
286 * if there was no active subscription for this address/interface combination.
287 * An error is unconditionally returned if option OPT_NULLIFACE_ALL is used,
288 * @a net_if is = 0, and ACE_Sock_Connect::get_ip_interfaces() is not
289 * implemented in this environment (_even if_ the subscribe() specifies a
290 * non- NULL @a net_if).
292 * leave() replaces unsubscribe() and uses mcast_addr to determine
293 * protocol_family, and protocol which we always pass as 0 anyway.
295 int leave (const ACE_INET_Addr &mcast_addr,
296 const ACE_TCHAR *net_if = 0);
298 // = Data transfer routines.
300 /// Send @a n bytes in @a buf, using the multicast address and network interface
301 /// defined by the first open() or subscribe().
302 ssize_t send (const void *buf,
303 size_t n,
304 int flags = 0) const;
306 /// Send @a n iovecs, using the multicast address and network interface
307 /// defined by the first open() or subscribe().
308 ssize_t send (const iovec iov[],
309 int n,
310 int flags = 0) const;
312 // = Options.
314 /// Set a socket option.
316 * Set an IP option that takes a char as input, such as IP_MULTICAST_LOOP
317 * or IP_MULTICAST_TTL. This is just a more concise, nice interface to a
318 * subset of possible ACE_SOCK::set_option calls, but only works for
319 * IPPROTO_IP or IPPROTO_IPV6 level options.
321 * Returns 0 on success, -1 on failure.
323 * @deprecated This method has been deprecated since it cannot be used
324 * easily with with IPv6 options. Use ACE_SOCK::set_option instead.
326 int set_option (int option, char optval);
328 /// Dump the state of an object.
330 * Logs the setting of all options, the bound address, the send address and
331 * interface, and the list of current subscriptions.
333 void dump () const;
335 /// Declare the dynamic allocation hooks.
336 ACE_ALLOC_HOOK_DECLARE;
338 /// Override write accessor for the constructor options (@see enum options above)
339 /// This class is typically default instantiated in a connection handler templated
340 /// framework so these cannot be specified on construction.
341 void opts (int opts);
343 /// Read accessor for the constructor options (@see enum options above)
344 int opts () const;
346 private:
348 /// Subscribe to a multicast address on one or more network interface(s).
349 /// (No QoS support.)
350 int subscribe_ifs (const ACE_INET_Addr &mcast_addr,
351 const ACE_TCHAR *net_if,
352 int reuse_addr);
354 /// Do subscription processing w/out updating the subscription list.
355 /// (Layered method for subscribe() processing).
356 int subscribe_i (const ACE_INET_Addr &mcast_addr,
357 int reuse_addr = 1,
358 const ACE_TCHAR *net_if = 0);
360 /// Unsubscribe from a multicast address on one or more network interface(s).
361 int unsubscribe_ifs (const ACE_INET_Addr &mcast_addr,
362 const ACE_TCHAR *net_if = 0);
364 /// Do unsubscription processing w/out udpating subscription list.
365 /// (Layered method for <unsubscribe> processing).
366 int unsubscribe_i (const ACE_INET_Addr &mcast_addr,
367 const ACE_TCHAR *net_if = 0);
369 protected:
370 /// Contains common open functionality so that inheriting classes can
371 /// reuse it.
372 int open_i (const ACE_INET_Addr &mcast_addr, // Bound & sendto address.
373 const ACE_TCHAR *net_if = 0, // Net interface for sends.
374 int reuse_addr = 1);
376 /// Empty the dynamic subscription list.
377 int clear_subs_list ();
379 private:
380 /// Per-instance options..
381 int opts_;
383 /// Multicast address to which local send() methods send datagrams.
384 ACE_INET_Addr send_addr_;
385 /// Network interface to which all send() methods send multicast datagrams.
386 ACE_TCHAR *send_net_if_;
388 #if defined (ACE_SOCK_DGRAM_MCAST_DUMPABLE)
389 typedef ACE_DLList<ip_mreq> subscription_list_t;
390 typedef ACE_DLList_Iterator<ip_mreq> subscription_list_iter_t;
391 /// List of currently subscribed addr/iface pairs (and assc. types).
392 mutable subscription_list_t subscription_list_;
393 /// Lock used to protect subscription list.
394 mutable ACE_SDM_LOCK subscription_list_lock_;
395 // (Lock type does not need to support recursive locking.)
396 #endif /* ACE_SOCK_DGRAM_MCAST_DUMPABLE */
400 ACE_END_VERSIONED_NAMESPACE_DECL
402 #if defined (__ACE_INLINE__)
403 #include "ace/SOCK_Dgram_Mcast.inl"
404 #endif /* __ACE_INLINE__ */
406 #include /**/ "ace/post.h"
407 #endif /* ACE_SOCK_DGRAM_MCAST_H */