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 78931 2007-07-18 09:59:36Z johnnyw $")
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_ (),
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   return 0;
00617 }
00618 
00619 int
00620 TAO_SCIOP_Acceptor::hostname (TAO_ORB_Core *orb_core,
00621                              ACE_INET_Addr &addr,
00622                              char *&host,
00623                              const char *specified_hostname)
00624 {
00625   if (orb_core->orb_params ()->use_dotted_decimal_addresses ())
00626     {
00627       // If dotted decimal addresses are enabled,
00628       // just return ours.
00629       return this->dotted_decimal_address (addr, host);
00630     }
00631   else if (specified_hostname != 0)
00632     {
00633       // If the user specified a hostname, pass it back
00634       // blindly as it overrides our choice of hostname.
00635       host = CORBA::string_dup (specified_hostname);
00636     }
00637   else
00638     {
00639       char tmp_host[MAXHOSTNAMELEN + 1];
00640 
00641       // Get the hostname associated with our address
00642       if (addr.get_host_name (tmp_host, sizeof (tmp_host)) != 0)
00643         {
00644           // On failure, just return the decimal address.
00645           return this->dotted_decimal_address (addr, host);
00646         }
00647       else
00648         {
00649           host = CORBA::string_dup (tmp_host);
00650         }
00651     }
00652 
00653   return 0;
00654 }
00655 
00656 int
00657 TAO_SCIOP_Acceptor::dotted_decimal_address (ACE_INET_Addr &addr,
00658                                            char *&host)
00659 {
00660   int result = 0;
00661   const char *tmp = 0;
00662 
00663   // If the IP address in the INET_Addr is the INADDR_ANY address,
00664   // then force the actual IP address to be used by initializing a new
00665   // INET_Addr with the hostname from the original one.  If that fails
00666   // then something is seriously wrong with the systems networking
00667   // setup.
00668   if (addr.get_ip_address () == INADDR_ANY)
00669     {
00670       ACE_INET_Addr new_addr;
00671       result = new_addr.set (addr.get_port_number (),
00672                              addr.get_host_name ());
00673       tmp = new_addr.get_host_addr ();
00674     }
00675   else
00676     tmp = addr.get_host_addr ();
00677 
00678   if (tmp == 0 || result != 0)
00679     {
00680       if (TAO_debug_level > 0)
00681         ACE_DEBUG ((LM_DEBUG,
00682                     ACE_TEXT ("\n\nTAO (%P|%t) ")
00683                     ACE_TEXT ("SCIOP_Acceptor::dotted_decimal_address ")
00684                     ACE_TEXT ("- %p\n\n"),
00685                     ACE_TEXT ("cannot determine hostname")));
00686       return -1;
00687     }
00688 
00689   host = CORBA::string_dup (tmp);
00690   return 0;
00691 }
00692 
00693 int
00694 TAO_SCIOP_Acceptor::probe_interfaces (TAO_ORB_Core *orb_core)
00695 {
00696   // Extract the hostname for each network interface, and then cache
00697   // it.  The hostnames will then be used when creating a
00698   // TAO_SCIOP_Profile for each endpoint setup on the probed
00699   // network interfaces.
00700   ACE_INET_Addr *if_addrs = 0;
00701   size_t if_cnt = 0;
00702 
00703   if (ACE::get_ip_interfaces (if_cnt,
00704                               if_addrs) != 0
00705       && errno != ENOTSUP)
00706     {
00707       // In the case where errno == ENOTSUP, if_cnt and if_addrs will
00708       // not be modified, and will each remain equal to zero.  This
00709       // causes the default interface to be used.
00710       return -1;
00711     }
00712 
00713   if (if_cnt == 0 || if_addrs == 0)
00714     {
00715       if (TAO_debug_level > 0)
00716         {
00717           ACE_DEBUG ((LM_WARNING,
00718                       ACE_TEXT ("TAO (%P|%t) Unable to probe network ")
00719                       ACE_TEXT ("interfaces.  Using default.\n")));
00720         }
00721 
00722       if_cnt = 1; // Force the network interface count to be one.
00723       delete [] if_addrs;
00724       ACE_NEW_RETURN (if_addrs,
00725                       ACE_INET_Addr[if_cnt],
00726                       -1);
00727     }
00728 
00729   // Scan for the loopback interface since it shouldn't be included in
00730   // the list of cached hostnames unless it is the only interface.
00731   size_t lo_cnt = 0;  // Loopback interface count
00732   for (size_t j = 0; j < if_cnt; ++j)
00733     if (if_addrs[j].get_ip_address () == INADDR_LOOPBACK)
00734       ++lo_cnt;
00735 
00736   // The instantiation for this template is in
00737   // tao/SCIOP_Connector.cpp.
00738   ACE_Auto_Basic_Array_Ptr<ACE_INET_Addr> safe_if_addrs (if_addrs);
00739 
00740   // If the loopback interface is the only interface then include it
00741   // in the list of interfaces to query for a hostname, otherwise
00742   // exclude it from the list.
00743   if (if_cnt == lo_cnt)
00744     this->endpoint_count_ = static_cast<CORBA::ULong> (if_cnt);
00745   else
00746     this->endpoint_count_ = static_cast<CORBA::ULong> (if_cnt - lo_cnt);
00747 
00748   ACE_NEW_RETURN (this->addrs_,
00749                   ACE_INET_Addr[this->endpoint_count_],
00750                   -1);
00751 
00752   ACE_NEW_RETURN (this->hosts_,
00753                   char *[this->endpoint_count_],
00754                   -1);
00755 
00756   ACE_OS::memset (this->hosts_, 0, sizeof (char*) * this->endpoint_count_);
00757 
00758   // The number of hosts/interfaces we want to cache may not be the
00759   // same as the number of detected interfaces so keep a separate
00760   // count.
00761   size_t host_cnt = 0;
00762 
00763   for (size_t i = 0; i < if_cnt; ++i)
00764     {
00765       // Ignore any loopback interface if there are other
00766       // non-loopback interfaces.
00767       if (if_cnt != lo_cnt &&
00768           if_addrs[i].get_ip_address() == INADDR_LOOPBACK)
00769         continue;
00770 
00771       if (this->hostname_in_ior_ != 0)
00772         {
00773           if (TAO_debug_level > 2)
00774             {
00775               ACE_DEBUG ((LM_DEBUG,
00776                           ACE_TEXT ("Overriding address in IOR with %s\n"),
00777                           this->hostname_in_ior_));
00778             }
00779           if (this->hostname (orb_core,
00780                               if_addrs[i],
00781                               this->hosts_[host_cnt],
00782                               this->hostname_in_ior_) != 0)
00783             return -1;
00784         }
00785       else
00786         {
00787           if (this->hostname (orb_core,
00788                               if_addrs[i],
00789                               this->hosts_[host_cnt]) != 0)
00790             return -1;
00791         }
00792 
00793       // Copy the addr.  The port is (re)set in
00794       // TAO_SCIOP_Acceptor::open_i().
00795       if (this->addrs_[host_cnt].set (if_addrs[i]) != 0)
00796         return -1;
00797 
00798       ++host_cnt;
00799     }
00800 
00801   return 0;
00802 }
00803 
00804 int
00805 TAO_SCIOP_Acceptor::parse_multiple_hostnames (const char *hostnames,
00806                                               ACE_Array<ACE_CString> &hostnames_out)
00807 {
00808 
00809   // Make a copy of hostnames string
00810   int const hostnames_string_length = ACE_OS::strlen(hostnames) + 1;
00811   char* hostnames_copy = 0;
00812   ACE_NEW_RETURN (hostnames_copy,
00813                   char[hostnames_string_length],
00814                   -1);
00815   ACE_Auto_Basic_Array_Ptr<char> hostnames_copy_auto(hostnames_copy);
00816   ACE_OS::strncpy(hostnames_copy, hostnames, hostnames_string_length);
00817 
00818   // Count the number of hostnames separated by "+"
00819   size_t num_hostnames = 0;
00820   char *last = 0;
00821   const char* hostname = ACE_OS::strtok_r (hostnames_copy, "+", &last);
00822 
00823   while (hostname != 0) {
00824     ++num_hostnames;
00825     hostname = ACE_OS::strtok_r (0, "+", &last);
00826   }
00827 
00828   // Set the size of the array to the number of hostnames
00829   if (hostnames_out.size(num_hostnames) == -1) {
00830 
00831     ACE_ERROR_RETURN ((LM_ERROR,
00832                        ACE_TEXT ("TAO (%P|%t) Could not allocate storage ")
00833                        ACE_TEXT ("for %d hostnames in SCIOP endpoint\n"),
00834                        num_hostnames),
00835                       -1);
00836   }
00837 
00838   // Refresh copy of hostnames string
00839   ACE_NEW_RETURN (hostnames_copy,
00840                   char[hostnames_string_length],
00841                   -1);
00842   hostnames_copy_auto.reset(hostnames_copy);
00843   ACE_OS::strncpy(hostnames_copy, hostnames, hostnames_string_length);
00844 
00845   // Iterate over the hostnames and stuff them into the array
00846   size_t index = 0;
00847   last = 0;
00848   hostname = ACE_OS::strtok_r (hostnames_copy, "+", &last);
00849 
00850   while (index < num_hostnames) {
00851     ACE_CString hostname_object(hostname);
00852     hostnames_out.set(hostname_object, index++);
00853 
00854     hostname = ACE_OS::strtok_r (0, "+", &last);
00855   }
00856 
00857   return 0;
00858 }
00859 
00860 
00861 CORBA::ULong
00862 TAO_SCIOP_Acceptor::endpoint_count (void)
00863 {
00864   return this->endpoint_count_;
00865 }
00866 
00867 int
00868 TAO_SCIOP_Acceptor::object_key (IOP::TaggedProfile &profile,
00869                                TAO::ObjectKey &object_key)
00870 {
00871   // Create the decoding association from the encapsulation in the buffer,
00872 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
00873   TAO_InputCDR cdr (profile.profile_data.mb ());
00874 #else
00875   TAO_InputCDR cdr (reinterpret_cast<char*> (profile.profile_data.get_buffer ()),
00876                     profile.profile_data.length ());
00877 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
00878 
00879   CORBA::Octet major, minor;
00880 
00881   // Read the version. We just read it here. We don't*do any*
00882   // processing.
00883   if (!(cdr.read_octet (major)
00884         && cdr.read_octet (minor)))
00885     {
00886       if (TAO_debug_level > 0)
00887         {
00888           ACE_DEBUG ((LM_DEBUG,
00889                       ACE_TEXT ("TAO (%P|%t) SCIOP_Profile::decode - v%d.%d\n"),
00890                       major,
00891                       minor));
00892         }
00893       return -1;
00894     }
00895 
00896   CORBA::String_var host;
00897   CORBA::UShort port = 0;
00898 
00899   // Get host and port. No processing here too..
00900   if (cdr.read_string (host.out ()) == 0
00901       || cdr.read_ushort (port) == 0)
00902     {
00903       if (TAO_debug_level > 0)
00904         {
00905           ACE_DEBUG ((LM_DEBUG,
00906                       ACE_TEXT ("TAO (%P|%t) TAO_SCIOP_Acceptor::object_key - ")
00907                       ACE_TEXT ("error while decoding host/port")));
00908         }
00909       return -1;
00910     }
00911 
00912   // ... and object key.
00913   if ((cdr >> object_key) == 0)
00914     return -1;
00915 
00916   // We are NOT bothered about the rest.
00917 
00918   return 1;
00919 }
00920 
00921 
00922 int
00923 TAO_SCIOP_Acceptor::parse_options (const char *str)
00924 {
00925   if (str == 0)
00926     return 0;  // No options to parse.  Not a problem.
00927 
00928   // Use an option format similar to the one used for CGI scripts in
00929   // HTTP URLs.
00930   // e.g.:  option1=foo&option2=bar
00931 
00932   ACE_CString options (str);
00933 
00934   size_t len = options.length ();
00935 
00936   const char option_delimiter = '&';
00937 
00938   // Count the number of options.
00939 
00940   CORBA::ULong option_count = 1;
00941   // Number of endpoints in the string  (initialized to 1).
00942 
00943   // Only check for endpoints after the protocol specification and
00944   // before the object key.
00945   for (size_t i = 0; i < len; ++i)
00946     if (options[i] == option_delimiter)
00947       ++option_count;
00948 
00949   // The idea behind the following loop is to split the options into
00950   // (option, name) pairs.
00951   // For example,
00952   //    `option1=foo&option2=bar'
00953   // will be parsed into:
00954   //    `option1=foo'
00955   //    `option2=bar'
00956 
00957   ACE_CString::size_type begin = 0;
00958   ACE_CString::size_type end = 0;
00959 
00960   for (CORBA::ULong j = 0; j < option_count; ++j)
00961     {
00962       if (j < option_count - 1)
00963         end = options.find (option_delimiter, begin);
00964       else
00965         end = len;
00966 
00967       if (end == begin)
00968         ACE_ERROR_RETURN ((LM_ERROR,
00969                            ACE_TEXT ("TAO (%P|%t) Zero length SCIOP option.\n")),
00970                           -1);
00971       else if (end != ACE_CString::npos)
00972         {
00973           ACE_CString opt = options.substring (begin, end - begin);
00974 
00975           ACE_CString::size_type slot = opt.find ("=");
00976 
00977           if (slot == len - 1
00978               || slot == ACE_CString::npos)
00979             ACE_ERROR_RETURN ((LM_ERROR,
00980                                ACE_TEXT ("TAO (%P|%t) SCIOP option <%s> is ")
00981                                ACE_TEXT ("missing a value.\n"),
00982                                opt.c_str ()),
00983                               -1);
00984 
00985           ACE_CString name = opt.substring (0, slot);
00986           ACE_CString value = opt.substring (slot + 1);
00987 
00988           if (name.length () == 0)
00989             ACE_ERROR_RETURN ((LM_ERROR,
00990                                ACE_TEXT ("TAO (%P|%t) Zero length SCIOP ")
00991                                ACE_TEXT ("option name.\n")),
00992                               -1);
00993 
00994           if (name == "priority")
00995             {
00996               ACE_ERROR_RETURN ((LM_ERROR,
00997                                  ACE_TEXT ("TAO (%P|%t) Invalid SCIOP endpoint format: ")
00998                                  ACE_TEXT ("endpoint priorities no longer supported. \n")),
00999                                 -1);
01000             }
01001           else if (name == "portspan")
01002             {
01003               int range = static_cast<int> (ACE_OS::atoi (value.c_str ()));
01004               // @@ What's the lower bound on the range?  zero, or one?
01005               if (range < 1 || range > ACE_MAX_DEFAULT_PORT)
01006                 ACE_ERROR_RETURN ((LM_ERROR,
01007                                    ACE_TEXT ("TAO (%P|%t) Invalid SCIOP endpoint ")
01008                                    ACE_TEXT ("portspan: <%s>\n")
01009                                    ACE_TEXT ("Valid range 1 -- %d\n"),
01010                                    value.c_str (), ACE_MAX_DEFAULT_PORT),
01011                                   -1);
01012 
01013               this->port_span_ = static_cast<u_short> (range);
01014             }
01015           else if (name == "hostname_in_ior")
01016             {
01017               this->hostname_in_ior_ = value.rep ();
01018             }
01019           else
01020             ACE_ERROR_RETURN ((LM_ERROR,
01021                                ACE_TEXT ("TAO (%P|%t) Invalid SCIOP option: <%s>\n"),
01022                                name.c_str ()),
01023                               -1);
01024 
01025           begin = end + 1;
01026         }
01027       else
01028         {
01029           break; // No other options.
01030         }
01031     }
01032 
01033   return 0;
01034 }
01035 
01036 TAO_END_VERSIONED_NAMESPACE_DECL
01037 
01038 #endif /* TAO_HAS_SCIOP == 1 */

Generated on Sun Jan 27 15:59:48 2008 for TAO_Strategies by doxygen 1.3.6