Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

SOCK_Dgram_Mcast_QoS.cpp

Go to the documentation of this file.
00001 // $Id: SOCK_Dgram_Mcast_QoS.cpp,v 1.1.1.2 2003/02/21 18:36:32 chad Exp $
00002 
00003 #include "SOCK_Dgram_Mcast_QoS.h"
00004 #include "ace/Log_Msg.h"
00005 
00006 #if defined (ACE_WIN32)
00007 #include "ace/Sock_Connect.h"  // needed for subscribe_ifs()
00008 #endif /* ACE_WIN32 */
00009 
00010 #if defined (ACE_LACKS_INLINE_FUNCTIONS)
00011 #include "SOCK_Dgram_Mcast_QoS.i"
00012 #endif /* ACE_LACKS_INLINE_FUNCTIONS */
00013 
00014 // This is a workaround for platforms with non-standard
00015 // definitions of the ip_mreq structure
00016 #if ! defined (IMR_MULTIADDR)
00017 #define IMR_MULTIADDR imr_multiaddr
00018 #endif /* ! defined (IMR_MULTIADDR) */
00019 
00020 ACE_RCSID(ace, SOCK_Dgram_Mcast_QoS, "$Id $")
00021 
00022 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_Dgram_Mcast_QoS)
00023 
00024 // Dummy default constructor...
00025 
00026 ACE_SOCK_Dgram_Mcast_QoS::ACE_SOCK_Dgram_Mcast_QoS (options opts)
00027   : ACE_SOCK_Dgram_Mcast (opts)
00028 {
00029   ACE_TRACE ("ACE_SOCK_Dgram_Mcast_QoS::ACE_SOCK_Dgram_Mcast_QoS");
00030 }
00031 
00032 int
00033 ACE_SOCK_Dgram_Mcast_QoS::open (const ACE_INET_Addr &addr,
00034                                 const ACE_QoS_Params &qos_params,
00035                                 int protocol_family,
00036                                 int protocol,
00037                                 ACE_Protocol_Info *protocolinfo,
00038                                 ACE_SOCK_GROUP g,
00039                                 u_long flags,
00040                                 int reuse_addr)
00041 {
00042   ACE_TRACE ("ACE_SOCK_Dgram_Mcast_QoS::open");
00043 
00044   ACE_UNUSED_ARG (qos_params);
00045 
00046   // Only perform the <open> initialization if we haven't been opened
00047   // earlier.
00048   if (this->get_handle () != ACE_INVALID_HANDLE)
00049     return 0;
00050 
00051   ACE_DEBUG ((LM_DEBUG,
00052               "Get Handle Returns Invalid Handle\n"));
00053 
00054   if (ACE_SOCK::open (SOCK_DGRAM,
00055                       protocol_family,
00056                       protocol,
00057                       protocolinfo,
00058                       g,
00059                       flags,
00060                       reuse_addr) == -1)
00061     return -1;
00062 
00063   return this->open_i (addr, 0, reuse_addr);
00064 }
00065 
00066 
00067 int
00068 ACE_SOCK_Dgram_Mcast_QoS::subscribe_ifs (const ACE_INET_Addr &mcast_addr,
00069                                          const ACE_QoS_Params &qos_params,
00070                                          const ACE_TCHAR *net_if,
00071                                          int protocol_family,
00072                                          int protocol,
00073                                          int reuse_addr,
00074                                          ACE_Protocol_Info *protocolinfo)
00075 {
00076   ACE_TRACE ("ACE_SOCK_Dgram_Mcast_QoS::subscribe_ifs");
00077 #if defined (ACE_WIN32)
00078   // Windows NT's winsock has trouble with multicast subscribes in the
00079   // presence of multiple network interfaces when the IP address is
00080   // given as INADDR_ANY.  It will pick the first interface and only
00081   // accept mcast there.  So, to work around this, cycle through all
00082   // of the interfaces known and subscribe to all the non-loopback
00083   // ones.
00084   //
00085   // Note that this only needs to be done on NT, but there's no way to
00086   // tell at this point if the code will be running on NT - only if it
00087   // is compiled for NT-only or for NT/95, and that doesn't really
00088   // help us.  It doesn't hurt to do this on Win95, it's just a little
00089   // slower than it normally would be.
00090   //
00091   // NOTE - <ACE_Sock_Connect::get_ip_interfaces> doesn't always get all
00092   // of the interfaces.  In particular, it may not get a PPP interface.  This
00093   // is a limitation of the way <ACE_Sock_Connect::get_ip_interfaces> works
00094   // with MSVC.  The reliable way of getting the interface list is
00095   // available only with MSVC 5.
00096 
00097   if (net_if == 0)
00098     {
00099       ACE_INET_Addr *if_addrs = 0;
00100       size_t if_cnt;
00101 
00102       if (ACE_Sock_Connect::get_ip_interfaces (if_cnt,
00103                                                if_addrs) != 0)
00104         return -1;
00105 
00106       size_t nr_subscribed = 0;
00107 
00108       if (if_cnt < 2)
00109         {
00110           if (this->subscribe (mcast_addr,
00111                                qos_params,
00112                                reuse_addr,
00113                                ACE_LIB_TEXT ("0.0.0.0"),
00114                                protocol_family,
00115                                protocol,
00116                                protocolinfo) == 0)
00117             ++nr_subscribed;
00118         }
00119       else
00120         // Iterate through all the interfaces, figure out which ones
00121         // offer multicast service, and subscribe to them.
00122         while (if_cnt > 0)
00123           {
00124             --if_cnt;
00125 
00126             // Convert to 0-based for indexing, next loop check.
00127             if (if_addrs[if_cnt].get_ip_address() == INADDR_LOOPBACK)
00128               continue;
00129             if (this->subscribe (mcast_addr,
00130                                  qos_params,
00131                                  reuse_addr,
00132                                  ACE_TEXT_CHAR_TO_TCHAR
00133                                    (if_addrs[if_cnt].get_host_addr()),
00134                                  protocol_family,
00135                                  protocol,
00136                                  protocolinfo) == 0)
00137               ++nr_subscribed;
00138           }
00139 
00140       delete [] if_addrs;
00141 
00142       if (nr_subscribed == 0)
00143         {
00144           errno = ENODEV;
00145           return -1;
00146         }
00147       else
00148         // 1 indicates a "short-circuit" return.  This handles the
00149         // rather bizarre semantics of checking all the interfaces on
00150         // NT.
00151         return 1;
00152     }
00153 #else
00154   ACE_UNUSED_ARG (mcast_addr);
00155   ACE_UNUSED_ARG (qos_params);
00156   ACE_UNUSED_ARG (protocol_family);
00157   ACE_UNUSED_ARG (protocol);
00158   ACE_UNUSED_ARG (reuse_addr);
00159   ACE_UNUSED_ARG (protocolinfo);
00160 #endif /* ACE_WIN32 */
00161   // Otherwise, do it like everyone else...
00162 
00163   // Create multicast request.
00164   if (this->make_multicast_ifaddr (0,
00165                                    mcast_addr,
00166                                    net_if) == -1)
00167     return -1;
00168   else
00169     return 0;
00170 }
00171 
00172 int
00173 ACE_SOCK_Dgram_Mcast_QoS::subscribe (const ACE_INET_Addr &mcast_addr,
00174                                      const ACE_QoS_Params &qos_params,
00175                                      int reuse_addr,
00176                                      const ACE_TCHAR *net_if,
00177                                      int protocol_family,
00178                                      int protocol,
00179                                      ACE_Protocol_Info *protocolinfo,
00180                                      ACE_SOCK_GROUP g,
00181                                      u_long flags,
00182                                      ACE_QoS_Session *qos_session)
00183 {
00184   ACE_TRACE ("ACE_SOCK_Dgram_Mcast_QoS::subscribe");
00185 
00186   if (this->open (mcast_addr,
00187                   qos_params,
00188                   protocol_family,
00189                   protocol,
00190                   protocolinfo,
00191                   g,
00192                   flags,
00193                   reuse_addr) == -1)
00194     return -1;
00195 
00196   // The following method call only applies to Win32 currently.
00197   int result = this->subscribe_ifs (mcast_addr,
00198                                     qos_params,
00199                                     net_if,
00200                                     protocol_family,
00201                                     protocol,
00202                                     reuse_addr,
00203                                     protocolinfo);
00204   // Check for the "short-circuit" return value of 1 (for NT).
00205   if (result != 0)
00206     return result;
00207 
00208   // Tell network device driver to read datagrams with a
00209   // <mcast_request_if_> IP interface.
00210   else
00211     {
00212       // Check if the mcast_addr passed into this method is the
00213       // same as the QoS session address.
00214       if (mcast_addr == qos_session->dest_addr ())
00215         {
00216           // Subscribe to the QoS session.
00217           if (this->qos_manager_.join_qos_session (qos_session) == -1)
00218             ACE_ERROR_RETURN ((LM_ERROR,
00219                                ACE_LIB_TEXT ("Unable to join QoS Session\n")),
00220                               -1);
00221         }
00222       else
00223         {
00224           if (this->close () != 0)
00225             ACE_ERROR ((LM_ERROR,
00226                         ACE_LIB_TEXT ("Unable to close socket\n")));
00227             ACE_ERROR_RETURN ((LM_ERROR,
00228                                ACE_LIB_TEXT ("Dest Addr in the QoS Session does")
00229                                ACE_LIB_TEXT (" not match the address passed into")
00230                                ACE_LIB_TEXT (" subscribe\n")),
00231                               -1);
00232         }
00233 
00234       ip_mreq ret_mreq;
00235       this->make_multicast_ifaddr (&ret_mreq, mcast_addr, net_if);
00236 
00237       // XX This is windows stuff only. fredk
00238       if (ACE_OS::join_leaf (this->get_handle (),
00239                              ACE_reinterpret_cast (const sockaddr *,
00240                                                    &ret_mreq.IMR_MULTIADDR.s_addr),
00241                              sizeof ret_mreq.IMR_MULTIADDR.s_addr,
00242                              qos_params) == ACE_INVALID_HANDLE
00243           && errno != ENOTSUP)
00244         return -1;
00245 
00246       else
00247         if (qos_params.socket_qos () != 0)
00248           qos_session->qos (*(qos_params.socket_qos ()));
00249 
00250       return 0;
00251     }
00252 }
00253 
00254 
00255 
00256 

Generated on Mon Jun 16 13:17:26 2003 for ACE_QoS by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002