Multihomed_INET_Addr.cpp

Go to the documentation of this file.
00001 // Multihomed_INET_Addr.cpp,v 4.17 2006/04/19 19:13:09 jwillemsen Exp
00002 
00003 // Extends ACE_INET_Addr with support for multi-homed addresses.
00004 
00005 #include "ace/Multihomed_INET_Addr.h"
00006 #include "ace/Log_Msg.h"
00007 
00008 #if !defined (__ACE_INLINE__)
00009 #  include "ace/Multihomed_INET_Addr.inl"
00010 #endif /* __ACE_INLINE__ */
00011 
00012 ACE_RCSID (ace,
00013            Multihomed_INET_Addr,
00014            "Multihomed_INET_Addr.cpp,v 4.17 2006/04/19 19:13:09 jwillemsen Exp")
00015 
00016 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00017 
00018 ACE_ALLOC_HOOK_DEFINE(ACE_Multihomed_INET_Addr)
00019 
00020 // Default constructor
00021 
00022 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr (void)
00023   : secondaries_ (0)
00024 {
00025   ACE_TRACE ("ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr");
00026 }
00027 
00028 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr (const char address[])
00029  : ACE_INET_Addr (address),
00030    secondaries_ (0)
00031 {
00032 }
00033 
00034 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr(u_short port_number,
00035                                                    const char host_name[],
00036                                                    int encode,
00037                                                    int address_family,
00038                                                    const char *(secondary_host_names[]),
00039                                                    size_t size){
00040 
00041   // Initialize the primary INET addr
00042   ACE_INET_Addr::set(port_number, host_name, encode, address_family);
00043 
00044   // check for secondary INET addrs
00045   if (secondary_host_names && size){
00046     // we have a non-zero pointer and size
00047     this->secondaries_.size(size); // size the array
00048 
00049     size_t next_empty_slot = 0;
00050     for (size_t i = 0; i < size; ++i) {
00051       int ret = this->secondaries_[next_empty_slot].set(port_number,
00052                                                        secondary_host_names[i],
00053                                                        encode,
00054                                                        address_family);
00055       if (ret) {
00056         ACE_DEBUG ((LM_DEBUG,
00057                     ACE_LIB_TEXT ("Invalid INET addr (%s:%u) will be ignored\n"),
00058                     ACE_TEXT_CHAR_TO_TCHAR (secondary_host_names[i]), port_number));
00059         this->secondaries_.size(this->secondaries_.size() - 1);
00060       }
00061       else
00062         ++next_empty_slot;
00063     }
00064   }
00065 
00066   return;
00067 }
00068 
00069 #if defined (ACE_HAS_WCHAR)
00070 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr(u_short port_number,
00071                                                    const wchar_t host_name[],
00072                                                    int encode,
00073                                                    int address_family,
00074                                                    const wchar_t *(secondary_host_names[]),
00075                                                    size_t size){
00076 
00077   // Initialize the primary INET addr
00078   ACE_INET_Addr::set(port_number, host_name, encode, address_family);
00079 
00080   // check for secondary INET addrs
00081   if (secondary_host_names && size){
00082     // we have a non-zero pointer and size
00083     this->secondaries_.size(size); // size the array
00084 
00085     size_t next_empty_slot = 0;
00086     for (size_t i = 0; i < size; ++i) {
00087       int ret = this->secondaries_[next_empty_slot].set(port_number,
00088                                                        secondary_host_names[i],
00089                                                        encode,
00090                                                        address_family);
00091       if (ret) {
00092         ACE_DEBUG ((LM_DEBUG,
00093                     ACE_LIB_TEXT ("Invalid INET addr (%s:%u) will be ignored\n"),
00094                     ACE_TEXT_WCHAR_TO_TCHAR (secondary_host_names[i]), port_number));
00095         this->secondaries_.size(this->secondaries_.size() - 1);
00096       }
00097       else
00098         ++next_empty_slot;
00099     }
00100   }
00101 
00102   return;
00103 }
00104 #endif /* ACE_HAS_WCHAR */
00105 
00106 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr(u_short port_number,
00107                                                    ACE_UINT32 primary_ip_addr,
00108                                                    int encode,
00109                                                    const ACE_UINT32 *secondary_ip_addrs,
00110                                                    size_t size){
00111 
00112   // Initialize the primary INET addr
00113   ACE_INET_Addr::set(port_number, primary_ip_addr, encode);
00114 
00115   // check for secondary INET addrs
00116   if (secondary_ip_addrs && size){
00117     // we have a non-zero pointer and size
00118     this->secondaries_.size(size); // size the array
00119 
00120     size_t next_empty_slot = 0;
00121     for (size_t i = 0; i < size; ++i) {
00122       int const ret = this->secondaries_[next_empty_slot].set(port_number,
00123                                                               secondary_ip_addrs[i],
00124                                                               encode);
00125 
00126       if (ret) {
00127         ACE_DEBUG ((LM_DEBUG,
00128                     "Invalid INET addr (%u:%u) will be ignored\n",
00129                     secondary_ip_addrs[i], port_number));
00130         this->secondaries_.size(this->secondaries_.size() - 1);
00131       }
00132       else
00133         ++next_empty_slot;
00134     }
00135   }
00136 
00137   return;
00138 }
00139 
00140 // Set implementations  (NEED BETTER COMMENT HERE)
00141 int
00142 ACE_Multihomed_INET_Addr::set (u_short port_number,
00143                                const char host_name[],
00144                                int encode,
00145                                int address_family,
00146                                const char *(secondary_host_names[]),
00147                                size_t size)
00148 {
00149   this->secondaries_.size(size);
00150 
00151   for (size_t i = 0; i < size; ++i) {
00152 
00153     int const ret = this->secondaries_[i].set(port_number,
00154                                               secondary_host_names[i],
00155                                               encode,
00156                                               address_family);
00157     if (ret) {
00158       return ret;
00159     }
00160   }
00161 
00162   return ACE_INET_Addr::set(port_number, host_name, encode, address_family);
00163 }
00164 
00165 #if defined (ACE_HAS_WCHAR)
00166 //
00167 // WCHAR version of ::set
00168 //
00169 int
00170 ACE_Multihomed_INET_Addr::set (u_short port_number,
00171                                const wchar_t host_name[],
00172                                int encode,
00173                                int address_family,
00174                                const wchar_t *(secondary_host_names[]),
00175                                size_t size)
00176 {
00177   this->secondaries_.size(size);
00178 
00179   for (size_t i = 0; i < size; ++i) {
00180 
00181     int ret = this->secondaries_[i].set(port_number,
00182                                        secondary_host_names[i],
00183                                        encode,
00184                                        address_family);
00185     if (ret) {
00186       return ret;
00187     }
00188   }
00189 
00190   return ACE_INET_Addr::set(port_number, host_name, encode, address_family);
00191 }
00192 #endif /* ACE_HAS_WCHAR */
00193 
00194 int
00195 ACE_Multihomed_INET_Addr::set (u_short port_number,
00196                                ACE_UINT32 primary_ip_addr,
00197                                int encode,
00198                                const ACE_UINT32 *secondary_ip_addrs,
00199                                size_t size)
00200 {
00201   this->secondaries_.size(size);
00202 
00203   for (size_t i = 0; i < size; ++i) {
00204 
00205     int ret = this->secondaries_[i].set(port_number,
00206                                        secondary_ip_addrs[i],
00207                                        encode);
00208 
00209     if (ret) {
00210       return ret;
00211     }
00212   }
00213 
00214   return ACE_INET_Addr::set(port_number, primary_ip_addr, encode);
00215 }
00216 
00217 void
00218 ACE_Multihomed_INET_Addr::set_port_number (u_short port_number, int encode)
00219 {
00220   size_t i = 0;
00221   while (i < secondaries_.size())
00222     secondaries_[i++].set_port_number(port_number, encode);
00223 
00224   this->ACE_INET_Addr::set_port_number(port_number, encode);
00225 }
00226 
00227 int
00228 ACE_Multihomed_INET_Addr::get_secondary_addresses(ACE_INET_Addr *secondary_addrs,
00229                                                   size_t size) const
00230 {
00231   size_t top =
00232     size < this->secondaries_.size() ?
00233     size : this->secondaries_.size();
00234 
00235   for (size_t i = 0; i < top; ++i)
00236     {
00237 
00238       int ret =
00239         secondary_addrs[i].set (this->secondaries_[i]);
00240 
00241       if (ret)
00242         return ret;
00243     }
00244 
00245   return 0;
00246 }
00247 
00248 void
00249 ACE_Multihomed_INET_Addr::get_addresses(sockaddr_in *addrs,
00250                                         size_t size) const
00251 {
00252   // Copy primary address to the first slot of the user-supplied array
00253   if (size > 0) {
00254     addrs[0] = *reinterpret_cast<sockaddr_in*> (this->get_addr ());
00255   }
00256 
00257   // Copy secondary addresses to remaining slots of the user-supplied
00258   // array.  Secondary address [i] is copied to slot [i+1]
00259 
00260   size_t top = size - 1 < this->secondaries_.size() ?
00261     size - 1 : this->secondaries_.size();
00262 
00263   for (size_t i = 0; i < top; ++i) {
00264     addrs[i+1] =
00265       *reinterpret_cast<sockaddr_in*> (this->secondaries_[i].get_addr());
00266   }
00267 }
00268 
00269 #if defined (ACE_HAS_IPV6)
00270 void
00271 ACE_Multihomed_INET_Addr::get_addresses(sockaddr_in6 *addrs,
00272                                         size_t size) const
00273 {
00274   // Copy primary address to the first slot of the user-supplied array
00275   if (size > 0)
00276     {
00277       addrs[0] = *reinterpret_cast<sockaddr_in6*> (this->get_addr ());
00278     }
00279 
00280   // Copy secondary addresses to remaining slots of the user-supplied
00281   // array.  Secondary address [i] is copied to slot [i+1]
00282   size_t top =
00283     size - 1 < this->secondaries_.size() ?
00284     size - 1 : this->secondaries_.size();
00285 
00286   for (size_t i = 0; i < top; ++i)
00287     {
00288       addrs[i+1] =
00289         *reinterpret_cast<sockaddr_in6*> (this->secondaries_[i].get_addr());
00290     }
00291 }
00292 #endif /* ACE_HAS_IPV6 */
00293 
00294 
00295 ACE_Multihomed_INET_Addr::~ACE_Multihomed_INET_Addr (void)
00296 {
00297 
00298 }
00299 
00300 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:56 2006 for ACE by doxygen 1.3.6