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

ACE_SOCK_Dgram_Bcast Class Reference

Defines the member functions for the ACE_SOCK datagram abstraction. More...

#include <SOCK_Dgram_Bcast.h>

Inheritance diagram for ACE_SOCK_Dgram_Bcast:

Inheritance graph
[legend]
Collaboration diagram for ACE_SOCK_Dgram_Bcast:

Collaboration graph
[legend]
List of all members.

Public Methods

 ACE_SOCK_Dgram_Bcast (void)
 Default constructor. More...

 ACE_SOCK_Dgram_Bcast (const ACE_Addr &local, int protocol_family=PF_INET, int protocol=0, int reuse_addr=0, const ACE_TCHAR *host_name=0)
 ~ACE_SOCK_Dgram_Bcast (void)
 Default dtor. More...

int open (const ACE_Addr &local, int protocol_family=PF_INET, int protocol=0, int reuse_addr=0, const ACE_TCHAR *host_name=0)
 Initiate a connectionless datagram broadcast endpoint. More...

int close (void)
 Close up and release dynamically allocated resources. More...

ssize_t send (const void *buf, size_t n, u_short portnum, int flags=0) const
 Broadcast the datagram to every interface. Returns the average number of bytes sent. More...

ssize_t send (const iovec iov[], int n, u_short portnum, int flags=0) const
 Broadcast the <iovec> datagrams to every interface. Returns the average number of bytes sent. More...

ssize_t send (const void *buf, size_t n, const ACE_Addr &addr, int flags=0) const
 Broadcast an N byte datagram to ADDR (note that addr must be preassigned to the broadcast address of the subnet...). More...

ssize_t send (const iovec iov[], int n, const ACE_Addr &addr, int flags=0) const
void dump (void) const
 Dump the state of an object. More...


Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks. More...


Private Methods

int mk_broadcast (const ACE_TCHAR *host_name)
 Make broadcast available for Datagram socket. More...

int get_remote_addr (ACE_Addr &) const
 Do not allow this function to percolate up to this interface... More...


Private Attributes

ACE_Bcast_Nodeif_list_
 Points to the head of the list of broadcast interfaces. More...


Detailed Description

Defines the member functions for the ACE_SOCK datagram abstraction.

Definition at line 54 of file SOCK_Dgram_Bcast.h.


Constructor & Destructor Documentation

ACE_SOCK_Dgram_Bcast::ACE_SOCK_Dgram_Bcast void   
 

Default constructor.

Definition at line 54 of file SOCK_Dgram_Bcast.cpp.

References ACE_TRACE.

00055   : if_list_ (0)
00056 {
00057   ACE_TRACE ("ACE_SOCK_Dgram_Bcast::ACE_SOCK_Dgram_Bcast");
00058 }

ACE_SOCK_Dgram_Bcast::ACE_SOCK_Dgram_Bcast const ACE_Addr   local,
int    protocol_family = PF_INET,
int    protocol = 0,
int    reuse_addr = 0,
const ACE_TCHAR   host_name = 0
 

Definition at line 63 of file SOCK_Dgram_Bcast.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TCHAR, ACE_TRACE, LM_ERROR, and mk_broadcast.

00068   : ACE_SOCK_Dgram (local, protocol_family, protocol, reuse_addr),
00069     if_list_ (0)
00070 {
00071   ACE_TRACE ("ACE_SOCK_Dgram_Bcast::ACE_SOCK_Dgram_Bcast");
00072 
00073   if (this->mk_broadcast (host_name) == -1)
00074     ACE_ERROR ((LM_ERROR,
00075                 ACE_LIB_TEXT ("%p\n"),
00076                 ACE_LIB_TEXT ("ACE_SOCK_Dgram_Bcast")));
00077 }

ASYS_INLINE ACE_SOCK_Dgram_Bcast::~ACE_SOCK_Dgram_Bcast void   
 

Default dtor.

Definition at line 12 of file SOCK_Dgram_Bcast.i.

References ACE_TRACE.

00013 {
00014   ACE_TRACE ("ACE_SOCK_Dgram_Bcast::~ACE_SOCK_Dgram_Bcast");
00015 }


Member Function Documentation

int ACE_SOCK_Dgram_Bcast::close void   
 

Close up and release dynamically allocated resources.

Reimplemented from ACE_SOCK.

Definition at line 33 of file SOCK_Dgram_Bcast.cpp.

References ACE_TRACE, ACE_SOCK::close, if_list_, and ACE_Bcast_Node::next_.

00034 {
00035   ACE_TRACE ("ACE_SOCK_Dgram_Bcast::close");
00036 
00037   ACE_Bcast_Node *temp = this->if_list_;
00038 
00039   // Release the dynamically allocated memory.
00040 
00041   while (temp != 0)
00042     {
00043       ACE_Bcast_Node *hold = temp->next_;
00044       delete temp;
00045       temp = hold;
00046     }
00047 
00048   // Shut down the descriptor.
00049   return ACE_SOCK::close ();
00050 }

void ACE_SOCK_Dgram_Bcast::dump void    const
 

Dump the state of an object.

Reimplemented from ACE_SOCK_Dgram.

Definition at line 25 of file SOCK_Dgram_Bcast.cpp.

References ACE_TRACE.

00026 {
00027   ACE_TRACE ("ACE_SOCK_Dgram_Bcast::dump");
00028 }

int ACE_SOCK_Dgram_Bcast::get_remote_addr ACE_Addr   const [private]
 

Do not allow this function to percolate up to this interface...

Reimplemented from ACE_SOCK_Dgram.

int ACE_SOCK_Dgram_Bcast::mk_broadcast const ACE_TCHAR   host_name [private]
 

Make broadcast available for Datagram socket.

Definition at line 100 of file SOCK_Dgram_Bcast.cpp.

References ACE_BIT_ENABLED, ACE_DEBUG, ACE_ERROR, ACE_ERROR_RETURN, ACE_NEW_RETURN, ACE_TCHAR, ACE_TRACE, ACE_UINT64, caddr_t, ACE::debug, ACE_IPC_SAP::get_handle, ACE_OS::gethostbyname, if_list_, ACE_OS::ioctl, LM_DEBUG, LM_ERROR, ACE_OS_String::memcpy, ACE_OS::setsockopt, and SIOCGIFBRDADDR.

Referenced by ACE_SOCK_Dgram_Bcast, and open.

00101 {
00102   ACE_TRACE ("ACE_SOCK_Dgram_Bcast::mk_broadcast");
00103 
00104   int one = 1;
00105 
00106   if (ACE_OS::setsockopt (this->get_handle (),
00107                           SOL_SOCKET,
00108                           SO_BROADCAST,
00109                           (char *) &one,
00110                           sizeof one) == -1)
00111     return -1;
00112 
00113 #if !defined (ACE_WIN32)
00114   ACE_HANDLE s = this->get_handle ();
00115 
00116   char buf[BUFSIZ];
00117   struct ifconf ifc;
00118 
00119   ifc.ifc_len = sizeof buf;
00120   ifc.ifc_buf = buf;
00121 
00122   // Get interface structure and initialize the addresses using UNIX
00123   // techniques.
00124   if (ACE_OS::ioctl (s,
00125                      SIOCGIFCONF,
00126                      (char *) &ifc) == -1)
00127     ACE_ERROR_RETURN ((LM_ERROR, "%p\n",
00128                       "ACE_SOCK_Dgram_Bcast::mk_broadcast: ioctl (get interface configuration)"),
00129                       ACE_INVALID_HANDLE);
00130 
00131   struct ifreq *ifr = ifc.ifc_req;
00132 
00133   struct sockaddr_in host_addr;
00134 
00135   //Get host ip address
00136   if (host_name)
00137     {
00138       hostent *hp = ACE_OS::gethostbyname (host_name);
00139 
00140       if (hp == 0)
00141         return -1;
00142       else
00143 #if defined(_UNICOS)
00144         {
00145           ACE_UINT64 haddr;  // a place to put the address
00146           char * haddrp = (char *) &haddr;  // convert to char pointer
00147           ACE_OS::memcpy(haddrp,(char *) hp->h_addr,hp->h_length);
00148           host_addr.sin_addr.s_addr = haddr;
00149         }
00150 #else /* ! _UNICOS */
00151         ACE_OS::memcpy ((char *) &host_addr.sin_addr.s_addr,
00152                         (char *) hp->h_addr,
00153                         hp->h_length);
00154 #endif /* ! _UNICOS */
00155     }
00156 
00157   for (int n = ifc.ifc_len / sizeof (struct ifreq) ; n > 0;
00158 #if !defined(CHORUS_4) && !defined(AIX)
00159        n--, ifr++)
00160 #else
00161        n--,
00162            ((ifr->ifr_addr.sa_len <= sizeof (struct sockaddr)) ?
00163              ifr++ :
00164              ifr = (struct ifreq *)
00165              (ifr->ifr_addr.sa_len + (caddr_t) &ifr->ifr_addr)))
00166 #endif /* CHORUS_4 */
00167     {
00168       // Compare host ip address with interface ip address.
00169       if (host_name)
00170         {
00171           struct sockaddr_in if_addr;
00172 
00173           ACE_OS::memcpy (&if_addr,
00174                           &ifr->ifr_addr,
00175                           sizeof if_addr);
00176 
00177           if (host_addr.sin_addr.s_addr != if_addr.sin_addr.s_addr)
00178             continue;
00179         }
00180 
00181       if (ifr->ifr_addr.sa_family != AF_INET)
00182         {
00183           // Note that some systems seem to generate 0 (AF_UNDEF) for
00184           // the sa_family, even when there are no errors!  Thus, we
00185           // only print an error if this is not the case, or if we're
00186           // in "debugging" mode.
00187           if (ifr->ifr_addr.sa_family != 0
00188               || ACE::debug () > 0)
00189           ACE_DEBUG ((LM_DEBUG,
00190                       "warning %p: sa_family: %d\n",
00191                       "ACE_SOCK_Dgram_Bcast::mk_broadcast: Not AF_INET",
00192                       ifr->ifr_addr.sa_family));
00193           continue;
00194         }
00195 
00196       struct ifreq flags = *ifr;
00197       struct ifreq if_req = *ifr;
00198 
00199       if (ACE_OS::ioctl (s,
00200                          SIOCGIFFLAGS,
00201                          (char *) &flags) == -1)
00202         {
00203           ACE_ERROR ((LM_ERROR, "%p\n",
00204                      "ACE_SOCK_Dgram_Bcast::mk_broadcast: ioctl (get interface flags)"));
00205           continue;
00206         }
00207 
00208       if (ACE_BIT_ENABLED (flags.ifr_flags,
00209                            IFF_UP) == 0)
00210         {
00211           ACE_ERROR ((LM_ERROR, "%p\n",
00212                      "ACE_SOCK_Dgram_Bcast::mk_broadcast: Network interface is not up"));
00213           continue;
00214         }
00215 
00216       if (ACE_BIT_ENABLED (flags.ifr_flags,
00217                            IFF_LOOPBACK))
00218         continue;
00219 
00220       if (ACE_BIT_ENABLED (flags.ifr_flags,
00221                            IFF_BROADCAST))
00222         {
00223           if (ACE_OS::ioctl (s,
00224                              SIOCGIFBRDADDR,
00225                              (char *) &if_req) == -1)
00226             ACE_ERROR ((LM_ERROR, "%p\n",
00227                        "ACE_SOCK_Dgram_Bcast::mk_broadcast: ioctl (get broadaddr)"));
00228           else
00229             {
00230               ACE_INET_Addr addr (ACE_reinterpret_cast (sockaddr_in *,
00231                                                         &if_req.ifr_broadaddr),
00232                                   sizeof if_req.ifr_broadaddr);
00233               ACE_NEW_RETURN (this->if_list_,
00234                               ACE_Bcast_Node (addr,
00235                                               this->if_list_),
00236                               -1);
00237             }
00238         }
00239       else
00240         ACE_ERROR ((LM_ERROR, "%p\n",
00241                    "ACE_SOCK_Dgram_Bcast::mk_broadcast: Broadcast is not enable for this interface."));
00242     }
00243 #else
00244   ACE_UNUSED_ARG (host_name);
00245 
00246   ACE_INET_Addr addr (u_short (0),
00247                       ACE_UINT32 (INADDR_BROADCAST));
00248   ACE_NEW_RETURN (this->if_list_,
00249                   ACE_Bcast_Node (addr,
00250                                   this->if_list_),
00251                   -1);
00252 #endif /* !ACE_WIN32 */
00253   return this->if_list_ == 0 ? -1 : 0;
00254 }

int ACE_SOCK_Dgram_Bcast::open const ACE_Addr   local,
int    protocol_family = PF_INET,
int    protocol = 0,
int    reuse_addr = 0,
const ACE_TCHAR   host_name = 0
 

Initiate a connectionless datagram broadcast endpoint.

Definition at line 82 of file SOCK_Dgram_Bcast.cpp.

References ACE_TCHAR, ACE_TRACE, mk_broadcast, and ACE_SOCK_Dgram::open.

00087 {
00088   ACE_TRACE ("ACE_SOCK_Dgram_Bcast::open");
00089 
00090   if (this->ACE_SOCK_Dgram::open (local, protocol_family,
00091                                   protocol, reuse_addr) == -1)
00092     return -1;
00093 
00094   return this->mk_broadcast (host_name);
00095 }

ssize_t ACE_SOCK_Dgram_Bcast::send const iovec    iov[],
int    n,
const ACE_Addr   addr,
int    flags = 0
const
 

Broadcast an <iovec> of size <n> to <addr> as a datagram (note that addr must be preassigned to the broadcast address of the subnet...)

Reimplemented from ACE_SOCK_Dgram.

ASYS_INLINE ssize_t ACE_SOCK_Dgram_Bcast::send const void *    buf,
size_t    n,
const ACE_Addr   addr,
int    flags = 0
const
 

Broadcast an N byte datagram to ADDR (note that addr must be preassigned to the broadcast address of the subnet...).

Reimplemented from ACE_SOCK_Dgram.

Definition at line 21 of file SOCK_Dgram_Bcast.i.

References ACE_TRACE, ACE_Addr::get_addr, ACE_Addr::get_size, and ACE_OS::sendto.

00025 {
00026   ACE_TRACE ("ACE_SOCK_Dgram_Bcast::send");
00027 
00028   sockaddr *saddr = (sockaddr *) addr.get_addr ();
00029   int len = addr.get_size ();
00030   return ACE_OS::sendto (this->get_handle (), (const char *) buf, n, flags, 
00031                          (struct sockaddr *) saddr, len);
00032 }

ssize_t ACE_SOCK_Dgram_Bcast::send const iovec    iov[],
int    n,
u_short    portnum,
int    flags = 0
const
 

Broadcast the <iovec> datagrams to every interface. Returns the average number of bytes sent.

ssize_t ACE_SOCK_Dgram_Bcast::send const void *    buf,
size_t    n,
u_short    portnum,
int    flags = 0
const
 

Broadcast the datagram to every interface. Returns the average number of bytes sent.

Definition at line 260 of file SOCK_Dgram_Bcast.cpp.

References ACE_TRACE, ACE_Bcast_Node::bcast_addr_, if_list_, ACE_Bcast_Node::next_, ACE_SOCK_Dgram::send, ACE_INET_Addr::set_port_number, and ssize_t.

00264 {
00265   ACE_TRACE ("ACE_SOCK_Dgram_Bcast::send");
00266   size_t iterations = 0;
00267   ssize_t total_bytes = 0;
00268 
00269   if (this->if_list_ == 0)
00270     return -1;
00271 
00272   for (ACE_Bcast_Node *temp = this->if_list_;
00273        temp != 0;
00274        temp = temp->next_)
00275     {
00276       temp->bcast_addr_.set_port_number (port_number);
00277 
00278       ssize_t bytes_sent = ACE_SOCK_Dgram::send (buf,
00279                                                  n,
00280                                                  temp->bcast_addr_,
00281                                                  flags);
00282 
00283       if (bytes_sent == -1)
00284         return -1;
00285       else
00286         total_bytes += bytes_sent;
00287 
00288       iterations++;
00289     }
00290 
00291   return iterations == 0 ? 0 : total_bytes / iterations;
00292 }


Member Data Documentation

ACE_SOCK_Dgram_Bcast::ACE_ALLOC_HOOK_DECLARE
 

Declare the dynamic allocation hooks.

Reimplemented from ACE_SOCK_Dgram.

Definition at line 117 of file SOCK_Dgram_Bcast.h.

ACE_Bcast_Node* ACE_SOCK_Dgram_Bcast::if_list_ [private]
 

Points to the head of the list of broadcast interfaces.

Definition at line 124 of file SOCK_Dgram_Bcast.h.

Referenced by close, mk_broadcast, and send.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 12:56:22 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002