SOCK_Dgram_Mcast_QoS.cpp

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

Generated on Sun Jan 27 13:03:37 2008 for ACE_QoS by doxygen 1.3.6