SCIOP_Acceptor.cpp

Go to the documentation of this file.
00001 #include "tao/Strategies/SCIOP_Acceptor.h"
00002 #include "tao/Strategies/SCIOP_Profile.h"
00003 
00004 #if TAO_HAS_SCIOP == 1
00005 
00006 #include "tao/MProfile.h"
00007 #include "tao/debug.h"
00008 #include "tao/Protocols_Hooks.h"
00009 #include "tao/Codeset_Manager.h"
00010 #include "tao/Transport.h"
00011 #include "tao/CDR.h"
00012 
00013 #if !defined(__ACE_INLINE__)
00014 #include "tao/Strategies/SCIOP_Acceptor.inl"
00015 #endif /* __ACE_INLINE__ */
00016 
00017 ACE_RCSID(tao,
00018           SCIOP_Acceptor,
00019           "$Id: SCIOP_Acceptor.cpp 81470 2008-04-28 12:40:23Z elliott_c $")
00020 
00021 
00022 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00023 
00024 TAO_SCIOP_Acceptor::TAO_SCIOP_Acceptor (void)
00025   : TAO_Acceptor (TAO_TAG_SCIOP_PROFILE),
00026     addrs_ (0),
00027     port_span_ (1),
00028     hosts_ (0),
00029     endpoint_count_ (0),
00030     hostname_in_ior_ (0),
00031     version_ (TAO_DEF_SCIOP_MAJOR, TAO_DEF_SCIOP_MINOR),
00032     orb_core_ (0),
00033     base_acceptor_ (this),
00034     creation_strategy_ (0),
00035     concurrency_strategy_ (0),
00036     accept_strategy_ (0)
00037 {
00038 }
00039 
00040 TAO_SCIOP_Acceptor::~TAO_SCIOP_Acceptor (void)
00041 {
00042   // Make sure we are closed before we start destroying the
00043   // strategies.
00044   this->close ();
00045 
00046   delete this->creation_strategy_;
00047   delete this->concurrency_strategy_;
00048   delete this->accept_strategy_;
00049 
00050   delete [] this->addrs_;
00051 
00052   for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
00053     CORBA::string_free (this->hosts_[i]);
00054 
00055   delete [] this->hosts_;
00056 }
00057 
00058 // TODO =
00059 //    2) For V1.[1,2] there are tagged components
00060 int
00061 TAO_SCIOP_Acceptor::create_profile (const TAO::ObjectKey &object_key,
00062                                    TAO_MProfile &mprofile,
00063                                    CORBA::Short priority)
00064 {
00065   // Sanity check.
00066   if (this->endpoint_count_ == 0)
00067     return -1;
00068 
00069   // Check if multiple endpoints should be put in one profile or
00070   // if they should be spread across multiple profiles.
00071   /*
00072   if (priority == TAO_INVALID_PRIORITY)
00073     return this->create_new_profile (object_key,
00074                                      mprofile,
00075                                      priority);
00076   else
00077   */
00078     return this->create_shared_profile (object_key,
00079                                         mprofile,
00080                                         priority);
00081 }
00082 
00083 int
00084 TAO_SCIOP_Acceptor::create_new_profile (const TAO::ObjectKey &object_key,
00085                                         TAO_MProfile &mprofile,
00086                                         CORBA::Short priority)
00087 {
00088   // Adding this->endpoint_count_ to the TAO_MProfile.
00089   int const count = mprofile.profile_count ();
00090   if ((mprofile.size () - count) < this->endpoint_count_
00091       && mprofile.grow (count + this->endpoint_count_) == -1)
00092     return -1;
00093 
00094   // Create a profile for each acceptor endpoint.
00095   for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
00096     {
00097       TAO_SCIOP_Profile *pfile = 0;
00098       ACE_NEW_RETURN (pfile,
00099                       TAO_SCIOP_Profile (this->hosts_[i],
00100                                         this->addrs_[i].get_port_number (),
00101                                         object_key,
00102                                         this->addrs_[i],
00103                                         this->version_,
00104                                         this->orb_core_),
00105                       -1);
00106       pfile->endpoint ()->priority (priority);
00107 
00108       if (mprofile.give_profile (pfile) == -1)
00109         {
00110           pfile->_decr_refcnt ();
00111           pfile = 0;
00112           return -1;
00113         }
00114 
00115       pfile->tagged_components ().set_orb_type (TAO_ORB_TYPE);
00116 
00117       TAO_Codeset_Manager *csm = this->orb_core_->codeset_manager();
00118       if (csm)
00119         csm->set_codeset(pfile->tagged_components());
00120     }
00121 
00122   return 0;
00123 }
00124 
00125 int
00126 TAO_SCIOP_Acceptor::create_shared_profile (const TAO::ObjectKey &object_key,
00127                                            TAO_MProfile &mprofile,
00128                                            CORBA::Short priority)
00129 {
00130   CORBA::ULong index = 0;
00131   TAO_SCIOP_Profile *sciop_profile = 0;
00132 
00133   // Do not check <mprofile> for the presence of an existing
00134   // SCIOP_Profile.  With SCIOP, there is a one-to-one relationship
00135   // between Acceptors and Profiles.
00136     {
00137       ACE_NEW_RETURN (sciop_profile,
00138                       TAO_SCIOP_Profile (this->hosts_[0],
00139                                         this->addrs_[0].get_port_number (),
00140                                         object_key,
00141                                         this->addrs_[0],
00142                                         this->version_,
00143                                         this->orb_core_),
00144                       -1);
00145       sciop_profile->endpoint ()->priority (priority);
00146 
00147       if (mprofile.give_profile (sciop_profile) == -1)
00148         {
00149           sciop_profile->_decr_refcnt ();
00150           sciop_profile = 0;
00151           return -1;
00152         }
00153 
00154       sciop_profile->tagged_components ().set_orb_type (TAO_ORB_TYPE);
00155 
00156       TAO_Codeset_Manager *csm = this->orb_core_->codeset_manager();
00157       if (csm)
00158         csm->set_codeset(sciop_profile->tagged_components());
00159 
00160       index = 1;
00161     }
00162 
00163   // Add any remaining acceptor endpoints to the SCIOP_Profile.
00164   for (;
00165        index < this->endpoint_count_;
00166        ++index)
00167     {
00168       TAO_SCIOP_Endpoint *endpoint = 0;
00169       ACE_NEW_RETURN (endpoint,
00170                       TAO_SCIOP_Endpoint (this->hosts_[index],
00171                                          this->addrs_[index].get_port_number (),
00172                                          this->addrs_[index]),
00173                       -1);
00174       endpoint->priority (priority);
00175       sciop_profile->add_endpoint (endpoint);
00176     }
00177 
00178   return 0;
00179 }
00180 
00181 int
00182 TAO_SCIOP_Acceptor::is_collocated (const TAO_Endpoint *endpoint)
00183 {
00184   const TAO_SCIOP_Endpoint *endp =
00185     dynamic_cast<const TAO_SCIOP_Endpoint *> (endpoint);
00186 
00187   // Make sure the dynamically cast pointer is valid.
00188   if (endp == 0)
00189     return 0;
00190 
00191   for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
00192     {
00193       // compare the port and host name.  Please do *NOT* optimize
00194       // this code by comparing the IP address instead.  That would
00195       // trigger the following bug:
00196       //
00197       // http://deuce.doc.wustl.edu/bugzilla/show_bug.cgi?id=1220
00198       //
00199       if (endp->port() == this->addrs_[i].get_port_number()
00200           && ACE_OS::strcmp(endp->host(), this->hosts_[i]) == 0)
00201         return 1;
00202     }
00203 
00204   return 0;
00205 }
00206 
00207 int
00208 TAO_SCIOP_Acceptor::close (void)
00209 {
00210   return this->base_acceptor_.close ();
00211 }
00212 
00213 int
00214 TAO_SCIOP_Acceptor::open (TAO_ORB_Core *orb_core,
00215                          ACE_Reactor *reactor,
00216                          int major,
00217                          int minor,
00218                          const char *address,
00219                          const char *options)
00220 {
00221 
00222   this->orb_core_ = orb_core;
00223 
00224   if (this->hosts_ != 0)
00225     {
00226       // The hostname cache has already been set!
00227       // This is bad mojo, i.e. an internal TAO error.
00228       ACE_ERROR_RETURN ((LM_ERROR,
00229                          ACE_TEXT ("TAO (%P|%t) - ")
00230                          ACE_TEXT ("SCIOP_Acceptor::open, ")
00231                          ACE_TEXT ("hostname already set\n\n")),
00232                         -1);
00233     }
00234 
00235   if (address == 0)
00236     return -1;
00237 
00238   ACE_UNUSED_ARG (major);
00239   ACE_UNUSED_ARG (minor);
00240 
00241   // Parse options
00242   if (this->parse_options (options) == -1)
00243     return -1;
00244 
00245   ACE_Multihomed_INET_Addr addr;
00246 
00247   const char *port_separator_loc = ACE_OS::strchr (address, ':');
00248   ACE_Auto_Basic_Array_Ptr<char> tmp_host_auto;
00249 
00250   if (port_separator_loc == address)
00251     {
00252       // The address is a port number or port name.  No hostname was
00253       // specified.  The hostname for each network interface and the
00254       // fully qualified domain name must be obtained.
00255 
00256       // Check for multiple network interfaces.
00257       if (this->probe_interfaces (orb_core) == -1)
00258         return -1;
00259 
00260       // First convert the port into a usable form.
00261       ACE_INET_Addr temp_addr;
00262       if (temp_addr.set (address + sizeof (':')) != 0)
00263         return -1;
00264 
00265       // Now reset the port and set the host.
00266       if (addr.set (temp_addr.get_port_number (),
00267                     static_cast<ACE_UINT32> (INADDR_ANY),
00268                     1) != 0)
00269         return -1;
00270       else
00271         return this->open_i (addr,
00272                              reactor);
00273     }
00274 
00275   // If we've reached this point, then the address consists of one or
00276   // more hostnames, followed perhaps by a port.
00277 
00278   u_short port_number = 0;
00279   char *tmp_host = 0;
00280   size_t hostname_length = 0;
00281 
00282   if (port_separator_loc != 0) {
00283 
00284     // Port separator was found.  Check that the next character is
00285     // not the terminator.
00286     const char *port_loc = port_separator_loc;
00287     ++port_loc;
00288     if (port_loc == 0) {
00289       ACE_ERROR_RETURN ((LM_ERROR,
00290                          ACE_TEXT ("TAO (%P|%t) ")
00291                          ACE_TEXT ("SCIOP_Acceptor::open - ")
00292                          ACE_TEXT ("no port number after the ")
00293                          ACE_TEXT ("colon in \"%s\"\n"),
00294                          address),
00295                         -1);
00296     }
00297 
00298     // Read the port number
00299     ACE_INET_Addr temp_addr;
00300     if (temp_addr.string_to_addr(port_loc) != 0)
00301       return -1;
00302     port_number = temp_addr.get_port_number();
00303 
00304     // Set the length of the hostname
00305     hostname_length = port_separator_loc - address;
00306 
00307   } else {
00308 
00309     // Port separator was not found.  We allow port_number to retain
00310     // the value of 0, which will cause the port to be chosen for us
00311     // in open_i.
00312 
00313     // Set the length of the hostname
00314     hostname_length = ACE_OS::strlen(address);
00315   }
00316 
00317   ACE_NEW_RETURN(tmp_host, char[hostname_length + 1], -1);
00318   tmp_host_auto.reset(tmp_host);
00319   ACE_OS::memcpy (tmp_host, address, hostname_length);
00320   tmp_host[hostname_length] = '\0';
00321 
00322   // There may be multiple hostnames.  Parse them.
00323   ACE_Array<ACE_CString> hostnames;
00324   if (parse_multiple_hostnames(tmp_host, hostnames) != 0)
00325     return -1;
00326 
00327   // Check that at least one hostname was obtained.
00328   if (hostnames.size() < 1) {
00329     ACE_ERROR_RETURN ((LM_ERROR,
00330                        ACE_TEXT ("TAO (%P|%t) ")
00331                        ACE_TEXT ("SCIOP_Acceptor::open - ")
00332                        ACE_TEXT ("no hostnames in string \"%s\"\n"),
00333                        tmp_host),
00334                       -1);
00335   }
00336 
00337   // Obtain the primary ip address.
00338   ACE_UINT32 primary_ip_addr = 0;
00339   {
00340     // Obtain a char* for the primary hostname.
00341     ACE_CString & primary_hostname_obj = hostnames[0];
00342     ACE_Auto_Basic_Array_Ptr<char> primary_hostname_auto(primary_hostname_obj.rep());
00343     const char* primary_hostname = primary_hostname_auto.get();
00344 
00345     // Convert the primary hostname to ACE_UINT32
00346     ACE_INET_Addr temp_addr;
00347     if (temp_addr.set((u_short) 0, primary_hostname) != 0)
00348       return -1;
00349 
00350     primary_ip_addr = temp_addr.get_ip_address();
00351   }
00352 
00353   // Allocate an array of secondary ip addresses.
00354   ACE_UINT32 *secondary_ip_addrs = 0;
00355   ACE_Auto_Basic_Array_Ptr<ACE_UINT32> secondary_ip_addrs_auto;
00356   size_t num_secondary_ip_addrs = hostnames.size() - 1;
00357   if (num_secondary_ip_addrs > 0) {
00358     ACE_NEW_RETURN(secondary_ip_addrs,
00359                    ACE_UINT32[num_secondary_ip_addrs],
00360                    -1);
00361     secondary_ip_addrs_auto.reset(secondary_ip_addrs);
00362   }
00363 
00364   // Populate the array of secondary ip addresses
00365   size_t i = 0;
00366   ACE_INET_Addr temp_addr;
00367   while (i < num_secondary_ip_addrs) {
00368 
00369     // Obtain a char* for a single secondary hostname.
00370     ACE_CString & hostname_obj = hostnames[i + 1];
00371     ACE_Auto_Basic_Array_Ptr<char> hostname_auto(hostname_obj.rep());
00372     const char* hostname = hostname_auto.get();
00373 
00374     // Obtain the ip address for this secondary hostname.
00375     if (temp_addr.set((u_short) 0, hostname) != 0)
00376       return -1;
00377 
00378     // Put secondary ip address into the array
00379     secondary_ip_addrs[i++] = temp_addr.get_ip_address();
00380   }
00381 
00382   // Populate our ACE_Multihomed_INET_Addr with all the right
00383   // stuff.
00384   if (addr.set(port_number,
00385                primary_ip_addr,
00386                1,
00387                secondary_ip_addrs,
00388                num_secondary_ip_addrs));
00389 
00390   // Number of endpoints equals the size of the hostname array.
00391   this->endpoint_count_ = hostnames.size();
00392 
00393   ACE_NEW_RETURN (this->addrs_,
00394                   ACE_INET_Addr[this->endpoint_count_],
00395                   -1);
00396 
00397   ACE_NEW_RETURN (this->hosts_,
00398                   char *[this->endpoint_count_],
00399                   -1);
00400 
00401   // Copy the primary address to the first slot of the
00402   // addrs_ array.
00403   this->addrs_[0].set (addr);
00404 
00405   // Copy secondary addresses to the remaining slots of the
00406   // addrs_ array.
00407   ACE_INET_Addr *secondary_addrs = this->addrs_;
00408   ++secondary_addrs;
00409   addr.get_secondary_addresses(secondary_addrs, num_secondary_ip_addrs);
00410 
00411   // Set cached hostnames.
00412   i = 0;
00413   while (i < hostnames.size()) {
00414 
00415     // The hostname_in_ior_ field may override the FIRST hostname only.
00416     if (this->hostname_in_ior_ != 0 && i == 0)
00417     {
00418       if (TAO_debug_level > 2)
00419       {
00420         ACE_DEBUG ((LM_DEBUG,
00421                     ACE_TEXT ("Overriding address in IOR with %s\n"),
00422                     this->hostname_in_ior_));
00423       }
00424       if (this->hostname (orb_core,
00425                           this->addrs_[i],
00426                           this->hosts_[i],
00427                           this->hostname_in_ior_) != 0)
00428         return -1;
00429     }
00430     else
00431     {
00432       // Obtain a char* for the hostname.
00433       ACE_CString & hostname_obj = hostnames[i];
00434       ACE_Auto_Basic_Array_Ptr<char> hostname_auto(hostname_obj.rep());
00435       const char* hostname = hostname_auto.get();
00436 
00437       if (this->hostname (orb_core,
00438                           this->addrs_[i],
00439                           this->hosts_[i],
00440                           hostname) != 0)
00441         return -1;
00442     }
00443 
00444     ++i;
00445   }
00446 
00447   // Invoke open_i.
00448   return this->open_i (addr,
00449                        reactor);
00450 }
00451 
00452 int
00453 TAO_SCIOP_Acceptor::open_default (TAO_ORB_Core *orb_core,
00454                                  ACE_Reactor *reactor,
00455                                  int major,
00456                                  int minor,
00457                                  const char *options)
00458 {
00459   this->orb_core_ = orb_core;
00460 
00461   if (this->hosts_ != 0)
00462     {
00463       // The hostname cache has already been set!
00464       // This is bad mojo, i.e. an internal TAO error.
00465       ACE_ERROR_RETURN ((LM_ERROR,
00466                          ACE_TEXT ("TAO (%P|%t) ")
00467                          ACE_TEXT ("SCIOP_Acceptor::open_default - ")
00468                          ACE_TEXT ("hostname already set\n\n")),
00469                         -1);
00470     }
00471 
00472 
00473   ACE_UNUSED_ARG (major);
00474   ACE_UNUSED_ARG (minor);
00475 
00476   // Parse options
00477   if (this->parse_options (options) == -1)
00478     return -1;
00479 
00480   // Check for multiple network interfaces.
00481   if (this->probe_interfaces (orb_core) == -1)
00482     return -1;
00483 
00484   // Now that each network interface's hostname has been cached, open
00485   // an endpoint on each network interface using the INADDR_ANY
00486   // address.
00487   ACE_Multihomed_INET_Addr addr;
00488 
00489   if (addr.set (static_cast<u_short> (0),
00490                 static_cast<ACE_UINT32> (INADDR_ANY),
00491                 1) != 0)
00492     return -1;
00493 
00494   return this->open_i (addr,
00495                        reactor);
00496 }
00497 
00498 int
00499 TAO_SCIOP_Acceptor::open_i (const ACE_Multihomed_INET_Addr& addr,
00500                            ACE_Reactor *reactor)
00501 {
00502   ACE_NEW_RETURN (this->creation_strategy_,
00503                   TAO_SCIOP_CREATION_STRATEGY (this->orb_core_),
00504                   -1);
00505 
00506   ACE_NEW_RETURN (this->concurrency_strategy_,
00507                   TAO_SCIOP_CONCURRENCY_STRATEGY (this->orb_core_),
00508                   -1);
00509 
00510   ACE_NEW_RETURN (this->accept_strategy_,
00511                   TAO_SCIOP_ACCEPT_STRATEGY (this->orb_core_),
00512                   -1);
00513 
00514   u_short requested_port = addr.get_port_number ();
00515   if (requested_port == 0)
00516     {
00517       // don't care, i.e., let the OS choose an ephemeral port
00518       if (this->base_acceptor_.open (addr,
00519                                      reactor,
00520                                      this->creation_strategy_,
00521                                      this->accept_strategy_,
00522                                      this->concurrency_strategy_) == -1)
00523         {
00524           if (TAO_debug_level > 0)
00525             ACE_DEBUG ((LM_DEBUG,
00526                         ACE_TEXT ("\n\nTAO (%P|%t) SCIOP_Acceptor::open_i ")
00527                         ACE_TEXT ("- %p\n\n"),
00528                         ACE_TEXT ("cannot open acceptor")));
00529           return -1;
00530         }
00531     }
00532   else
00533     {
00534       ACE_Multihomed_INET_Addr a(addr);
00535 
00536       int found_a_port = 0;
00537       ACE_UINT32 last_port = requested_port + this->port_span_ - 1;
00538       if (last_port > ACE_MAX_DEFAULT_PORT)
00539         {
00540           last_port = ACE_MAX_DEFAULT_PORT;
00541         }
00542 
00543       for (ACE_UINT32 p = requested_port; p <= last_port; p++)
00544         {
00545           if (TAO_debug_level > 5)
00546             ACE_DEBUG ((LM_DEBUG,
00547                         ACE_TEXT ("TAO (%P|%t) SCIOP_Acceptor::open_i() ")
00548                         ACE_TEXT ("trying to listen on port %d\n"), p));
00549 
00550           // Now try to actually open on that port
00551           a.set_port_number ((u_short)p);
00552           if (this->base_acceptor_.open (a,
00553                                          reactor,
00554                                          this->creation_strategy_,
00555                                          this->accept_strategy_,
00556                                          this->concurrency_strategy_) != -1)
00557             {
00558               found_a_port = 1;
00559               break;
00560             }
00561         }
00562 
00563       // Now, if we couldn't locate a port, we punt
00564       if (! found_a_port)
00565         {
00566           if (TAO_debug_level > 0)
00567             ACE_DEBUG ((LM_DEBUG,
00568                         ACE_TEXT ("\n\nTAO (%P|%t) SCIOP_Acceptor::open_i ")
00569                         ACE_TEXT ("cannot open acceptor in port range (%d,%d)")
00570                         ACE_TEXT ("- %p\n\n"),
00571                         requested_port, last_port, ACE_TEXT("")));
00572           return -1;
00573         }
00574     }
00575 
00576   ACE_INET_Addr address;
00577 
00578   // We do this make sure the port number the endpoint is listening on
00579   // gets set in the addr.
00580   if (this->base_acceptor_.acceptor ().get_local_addr (address) != 0)
00581     {
00582       // @@ Should this be a catastrophic error???
00583       if (TAO_debug_level > 0)
00584         ACE_DEBUG ((LM_DEBUG,
00585                     ACE_TEXT ("\n\nTAO (%P|%t) SCIOP_Acceptor::open_i ")
00586                     ACE_TEXT ("- %p\n\n"),
00587                     ACE_TEXT ("cannot get local addr")));
00588       return -1;
00589     }
00590 
00591   // Set the port for each addr.  If there is more than one network
00592   // interface then the endpoint created on each interface will be on
00593   // the same port.  This is how a wildcard socket bind() is supposed
00594   // to work.
00595   u_short port = address.get_port_number ();
00596   for (CORBA::ULong j = 0; j < this->endpoint_count_; ++j)
00597     this->addrs_[j].set_port_number (port, 1);
00598 
00599   (void) this->base_acceptor_.acceptor().enable (ACE_CLOEXEC);
00600   // This avoids having child processes acquire the listen socket thereby
00601   // denying the server the opportunity to restart on a well-known endpoint.
00602   // This does not affect the aberrent behavior on Win32 platforms.
00603 
00604   if (TAO_debug_level > 5)
00605     {
00606       for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
00607         {
00608           ACE_DEBUG ((LM_DEBUG,
00609                       ACE_TEXT ("\nTAO (%P|%t) SCIOP_Acceptor::open_i - ")
00610                       ACE_TEXT ("listening on: <%s:%u>\n"),
00611                       ACE_TEXT_CHAR_TO_TCHAR(this->hosts_[i]),
00612                       this->addrs_[i].get_port_number ()));
00613         }
00614     }
00615 
00616   // In the event that an accept() fails, we can examine the reason.  If
00617   // the reason warrants it, we can try accepting again at a later time.
00618   // The amount of time we wait to accept again is governed by this orb
00619   // parameter.
00620   this->set_error_retry_delay (
00621     this->orb_core_->orb_params ()->accept_error_delay());
00622 
00623   return 0;
00624 }
00625 
00626 int
00627 TAO_SCIOP_Acceptor::hostname (TAO_ORB_Core *orb_core,
00628                              ACE_INET_Addr &addr,
00629                              char *&host,
00630                              const char *specified_hostname)
00631 {
00632   if (orb_core->orb_params ()->use_dotted_decimal_addresses ())
00633     {
00634       // If dotted decimal addresses are enabled,
00635       // just return ours.
00636       return this->dotted_decimal_address (addr, host);
00637     }
00638   else if (specified_hostname != 0)
00639     {
00640       // If the user specified a hostname, pass it back
00641       // blindly as it overrides our choice of hostname.
00642       host = CORBA::string_dup (specified_hostname);
00643     }
00644   else
00645     {
00646       char tmp_host[MAXHOSTNAMELEN + 1];
00647 
00648       // Get the hostname associated with our address
00649       if (addr.get_host_name (tmp_host, sizeof (tmp_host)) != 0)
00650         {
00651           // On failure, just return the decimal address.
00652           return this->dotted_decimal_address (addr, host);
00653         }
00654       else
00655         {
00656           host = CORBA::string_dup (tmp_host);
00657         }
00658     }
00659 
00660   return 0;
00661 }
00662 
00663 int
00664 TAO_SCIOP_Acceptor::dotted_decimal_address (ACE_INET_Addr &addr,
00665                                            char *&host)
00666 {
00667   int result = 0;
00668   const char *tmp = 0;
00669 
00670   // If the IP address in the INET_Addr is the INADDR_ANY address,
00671   // then force the actual IP address to be used by initializing a new
00672   // INET_Addr with the hostname from the original one.  If that fails
00673   // then something is seriously wrong with the systems networking
00674   // setup.
00675   if (addr.get_ip_address () == INADDR_ANY)
00676     {
00677       ACE_INET_Addr new_addr;
00678       result = new_addr.set (addr.get_port_number (),
00679                              addr.get_host_name ());
00680       tmp = new_addr.get_host_addr ();
00681     }
00682   else
00683     tmp = addr.get_host_addr ();
00684 
00685   if (tmp == 0 || result != 0)
00686     {
00687       if (TAO_debug_level > 0)
00688         ACE_DEBUG ((LM_DEBUG,
00689                     ACE_TEXT ("\n\nTAO (%P|%t) ")
00690                     ACE_TEXT ("SCIOP_Acceptor::dotted_decimal_address ")
00691                     ACE_TEXT ("- %p\n\n"),
00692                     ACE_TEXT ("cannot determine hostname")));
00693       return -1;
00694     }
00695 
00696   host = CORBA::string_dup (tmp);
00697   return 0;
00698 }
00699 
00700 int
00701 TAO_SCIOP_Acceptor::probe_interfaces (TAO_ORB_Core *orb_core)
00702 {
00703   // Extract the hostname for each network interface, and then cache
00704   // it.  The hostnames will then be used when creating a
00705   // TAO_SCIOP_Profile for each endpoint setup on the probed
00706   // network interfaces.
00707   ACE_INET_Addr *if_addrs = 0;
00708   size_t if_cnt = 0;
00709 
00710   if (ACE::get_ip_interfaces (if_cnt,
00711                               if_addrs) != 0
00712       && errno != ENOTSUP)
00713     {
00714       // In the case where errno == ENOTSUP, if_cnt and if_addrs will
00715       // not be modified, and will each remain equal to zero.  This
00716       // causes the default interface to be used.
00717       return -1;
00718     }
00719 
00720   if (if_cnt == 0 || if_addrs == 0)
00721     {
00722       if (TAO_debug_level > 0)
00723         {
00724           ACE_DEBUG ((LM_WARNING,
00725                       ACE_TEXT ("TAO (%P|%t) Unable to probe network ")
00726                       ACE_TEXT ("interfaces.  Using default.\n")));
00727         }
00728 
00729       if_cnt = 1; // Force the network interface count to be one.
00730       delete [] if_addrs;
00731       ACE_NEW_RETURN (if_addrs,
00732                       ACE_INET_Addr[if_cnt],
00733                       -1);
00734     }
00735 
00736   // Scan for the loopback interface since it shouldn't be included in
00737   // the list of cached hostnames unless it is the only interface.
00738   size_t lo_cnt = 0;  // Loopback interface count
00739   for (size_t j = 0; j < if_cnt; ++j)
00740     if (if_addrs[j].get_ip_address () == INADDR_LOOPBACK)
00741       ++lo_cnt;
00742 
00743   // The instantiation for this template is in
00744   // tao/SCIOP_Connector.cpp.
00745   ACE_Auto_Basic_Array_Ptr<ACE_INET_Addr> safe_if_addrs (if_addrs);
00746 
00747   // If the loopback interface is the only interface then include it
00748   // in the list of interfaces to query for a hostname, otherwise
00749   // exclude it from the list.
00750   if (if_cnt == lo_cnt)
00751     this->endpoint_count_ = static_cast<CORBA::ULong> (if_cnt);
00752   else
00753     this->endpoint_count_ = static_cast<CORBA::ULong> (if_cnt - lo_cnt);
00754 
00755   ACE_NEW_RETURN (this->addrs_,
00756                   ACE_INET_Addr[this->endpoint_count_],
00757                   -1);
00758 
00759   ACE_NEW_RETURN (this->hosts_,
00760                   char *[this->endpoint_count_],
00761                   -1);
00762 
00763   ACE_OS::memset (this->hosts_, 0, sizeof (char*) * this->endpoint_count_);
00764 
00765   // The number of hosts/interfaces we want to cache may not be the
00766   // same as the number of detected interfaces so keep a separate
00767   // count.
00768   size_t host_cnt = 0;
00769 
00770   for (size_t i = 0; i < if_cnt; ++i)
00771     {
00772       // Ignore any loopback interface if there are other
00773       // non-loopback interfaces.
00774       if (if_cnt != lo_cnt &&
00775           if_addrs[i].get_ip_address() == INADDR_LOOPBACK)
00776         continue;
00777 
00778       if (this->hostname_in_ior_ != 0)
00779         {
00780           if (TAO_debug_level > 2)
00781             {
00782               ACE_DEBUG ((LM_DEBUG,
00783                           ACE_TEXT ("Overriding address in IOR with %s\n"),
00784                           this->hostname_in_ior_));
00785             }
00786           if (this->hostname (orb_core,
00787                               if_addrs[i],
00788                               this->hosts_[host_cnt],
00789                               this->hostname_in_ior_) != 0)
00790             return -1;
00791         }
00792       else
00793         {
00794           if (this->hostname (orb_core,
00795                               if_addrs[i],
00796                               this->hosts_[host_cnt]) != 0)
00797             return -1;
00798         }
00799 
00800       // Copy the addr.  The port is (re)set in
00801       // TAO_SCIOP_Acceptor::open_i().
00802       if (this->addrs_[host_cnt].set (if_addrs[i]) != 0)
00803         return -1;
00804 
00805       ++host_cnt;
00806     }
00807 
00808   return 0;
00809 }
00810 
00811 int
00812 TAO_SCIOP_Acceptor::parse_multiple_hostnames (const char *hostnames,
00813                                               ACE_Array<ACE_CString> &hostnames_out)
00814 {
00815 
00816   // Make a copy of hostnames string
00817   int const hostnames_string_length = ACE_OS::strlen(hostnames) + 1;
00818   char* hostnames_copy = 0;
00819   ACE_NEW_RETURN (hostnames_copy,
00820                   char[hostnames_string_length],
00821                   -1);
00822   ACE_Auto_Basic_Array_Ptr<char> hostnames_copy_auto(hostnames_copy);
00823   ACE_OS::strncpy(hostnames_copy, hostnames, hostnames_string_length);
00824 
00825   // Count the number of hostnames separated by "+"
00826   size_t num_hostnames = 0;
00827   char *last = 0;
00828   const char* hostname = ACE_OS::strtok_r (hostnames_copy, "+", &last);
00829 
00830   while (hostname != 0) {
00831     ++num_hostnames;
00832     hostname = ACE_OS::strtok_r (0, "+", &last);
00833   }
00834 
00835   // Set the size of the array to the number of hostnames
00836   if (hostnames_out.size(num_hostnames) == -1) {
00837 
00838     ACE_ERROR_RETURN ((LM_ERROR,
00839                        ACE_TEXT ("TAO (%P|%t) Could not allocate storage ")
00840                        ACE_TEXT ("for %d hostnames in SCIOP endpoint\n"),
00841                        num_hostnames),
00842                       -1);
00843   }
00844 
00845   // Refresh copy of hostnames string
00846   ACE_NEW_RETURN (hostnames_copy,
00847                   char[hostnames_string_length],
00848                   -1);
00849   hostnames_copy_auto.reset(hostnames_copy);
00850   ACE_OS::strncpy(hostnames_copy, hostnames, hostnames_string_length);
00851 
00852   // Iterate over the hostnames and stuff them into the array
00853   size_t index = 0;
00854   last = 0;
00855   hostname = ACE_OS::strtok_r (hostnames_copy, "+", &last);
00856 
00857   while (index < num_hostnames) {
00858     ACE_CString hostname_object(hostname);
00859     hostnames_out.set(hostname_object, index++);
00860 
00861     hostname = ACE_OS::strtok_r (0, "+", &last);
00862   }
00863 
00864   return 0;
00865 }
00866 
00867 
00868 CORBA::ULong
00869 TAO_SCIOP_Acceptor::endpoint_count (void)
00870 {
00871   return this->endpoint_count_;
00872 }
00873 
00874 int
00875 TAO_SCIOP_Acceptor::object_key (IOP::TaggedProfile &profile,
00876                                TAO::ObjectKey &object_key)
00877 {
00878   // Create the decoding association from the encapsulation in the buffer,
00879 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
00880   TAO_InputCDR cdr (profile.profile_data.mb ());
00881 #else
00882   TAO_InputCDR cdr (reinterpret_cast<char*> (profile.profile_data.get_buffer ()),
00883                     profile.profile_data.length ());
00884 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
00885 
00886   CORBA::Octet major, minor;
00887 
00888   // Read the version. We just read it here. We don't*do any*
00889   // processing.
00890   if (!(cdr.read_octet (major)
00891         && cdr.read_octet (minor)))
00892     {
00893       if (TAO_debug_level > 0)
00894         {
00895           ACE_DEBUG ((LM_DEBUG,
00896                       ACE_TEXT ("TAO (%P|%t) SCIOP_Profile::decode - v%d.%d\n"),
00897                       major,
00898                       minor));
00899         }
00900       return -1;
00901     }
00902 
00903   CORBA::String_var host;
00904   CORBA::UShort port = 0;
00905 
00906   // Get host and port. No processing here too..
00907   if (cdr.read_string (host.out ()) == 0
00908       || cdr.read_ushort (port) == 0)
00909     {
00910       if (TAO_debug_level > 0)
00911         {
00912           ACE_DEBUG ((LM_DEBUG,
00913                       ACE_TEXT ("TAO (%P|%t) TAO_SCIOP_Acceptor::object_key - ")
00914                       ACE_TEXT ("error while decoding host/port")));
00915         }
00916       return -1;
00917     }
00918 
00919   // ... and object key.
00920   if ((cdr >> object_key) == 0)
00921     return -1;
00922 
00923   // We are NOT bothered about the rest.
00924 
00925   return 1;
00926 }
00927 
00928 
00929 int
00930 TAO_SCIOP_Acceptor::parse_options (const char *str)
00931 {
00932   if (str == 0)
00933     return 0;  // No options to parse.  Not a problem.
00934 
00935   // Use an option format similar to the one used for CGI scripts in
00936   // HTTP URLs.
00937   // e.g.:  option1=foo&option2=bar
00938 
00939   ACE_CString options (str);
00940 
00941   size_t len = options.length ();
00942 
00943   const char option_delimiter = '&';
00944 
00945   // Count the number of options.
00946 
00947   CORBA::ULong option_count = 1;
00948   // Number of endpoints in the string  (initialized to 1).
00949 
00950   // Only check for endpoints after the protocol specification and
00951   // before the object key.
00952   for (size_t i = 0; i < len; ++i)
00953     if (options[i] == option_delimiter)
00954       ++option_count;
00955 
00956   // The idea behind the following loop is to split the options into
00957   // (option, name) pairs.
00958   // For example,
00959   //    `option1=foo&option2=bar'
00960   // will be parsed into:
00961   //    `option1=foo'
00962   //    `option2=bar'
00963 
00964   ACE_CString::size_type begin = 0;
00965   ACE_CString::size_type end = 0;
00966 
00967   for (CORBA::ULong j = 0; j < option_count; ++j)
00968     {
00969       if (j < option_count - 1)
00970         end = options.find (option_delimiter, begin);
00971       else
00972         end = len;
00973 
00974       if (end == begin)
00975         ACE_ERROR_RETURN ((LM_ERROR,
00976                            ACE_TEXT ("TAO (%P|%t) Zero length SCIOP option.\n")),
00977                           -1);
00978       else if (end != ACE_CString::npos)
00979         {
00980           ACE_CString opt = options.substring (begin, end - begin);
00981 
00982           ACE_CString::size_type slot = opt.find ("=");
00983 
00984           if (slot == len - 1
00985               || slot == ACE_CString::npos)
00986             ACE_ERROR_RETURN ((LM_ERROR,
00987                                ACE_TEXT ("TAO (%P|%t) SCIOP option <%s> is ")
00988                                ACE_TEXT ("missing a value.\n"),
00989                                opt.c_str ()),
00990                               -1);
00991 
00992           ACE_CString name = opt.substring (0, slot);
00993           ACE_CString value = opt.substring (slot + 1);
00994 
00995           if (name.length () == 0)
00996             ACE_ERROR_RETURN ((LM_ERROR,
00997                                ACE_TEXT ("TAO (%P|%t) Zero length SCIOP ")
00998                                ACE_TEXT ("option name.\n")),
00999                               -1);
01000 
01001           if (name == "priority")
01002             {
01003               ACE_ERROR_RETURN ((LM_ERROR,
01004                                  ACE_TEXT ("TAO (%P|%t) Invalid SCIOP endpoint format: ")
01005                                  ACE_TEXT ("endpoint priorities no longer supported. \n")),
01006                                 -1);
01007             }
01008           else if (name == "portspan")
01009             {
01010               int range = static_cast<int> (ACE_OS::atoi (value.c_str ()));
01011               // @@ What's the lower bound on the range?  zero, or one?
01012               if (range < 1 || range > ACE_MAX_DEFAULT_PORT)
01013                 ACE_ERROR_RETURN ((LM_ERROR,
01014                                    ACE_TEXT ("TAO (%P|%t) Invalid SCIOP endpoint ")
01015                                    ACE_TEXT ("portspan: <%s>\n")
01016                                    ACE_TEXT ("Valid range 1 -- %d\n"),
01017                                    value.c_str (), ACE_MAX_DEFAULT_PORT),
01018                                   -1);
01019 
01020               this->port_span_ = static_cast<u_short> (range);
01021             }
01022           else if (name == "hostname_in_ior")
01023             {
01024               this->hostname_in_ior_ = value.rep ();
01025             }
01026           else
01027             ACE_ERROR_RETURN ((LM_ERROR,
01028                                ACE_TEXT ("TAO (%P|%t) Invalid SCIOP option: <%s>\n"),
01029                                name.c_str ()),
01030                               -1);
01031 
01032           begin = end + 1;
01033         }
01034       else
01035         {
01036           break; // No other options.
01037         }
01038     }
01039 
01040   return 0;
01041 }
01042 
01043 TAO_END_VERSIONED_NAMESPACE_DECL
01044 
01045 #endif /* TAO_HAS_SCIOP == 1 */

Generated on Tue Feb 2 17:47:18 2010 for TAO_Strategies by  doxygen 1.4.7