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

Generated on Thu Nov 9 13:39:29 2006 for TAO_Strategies by doxygen 1.3.6