00001
00002
00003
00004
00005 #include "tao/IIOP_Acceptor.h"
00006
00007 #if defined (TAO_HAS_IIOP) && (TAO_HAS_IIOP != 0)
00008
00009 #include "tao/IIOP_Profile.h"
00010 #include "tao/MProfile.h"
00011 #include "tao/debug.h"
00012 #include "tao/Protocols_Hooks.h"
00013 #include "tao/Codeset_Manager.h"
00014 #include "tao/Transport.h"
00015 #include "tao/ORB_Core.h"
00016 #include "tao/CDR.h"
00017
00018 #if !defined(__ACE_INLINE__)
00019 #include "tao/IIOP_Acceptor.inl"
00020 #endif
00021
00022 #include "ace/Auto_Ptr.h"
00023 #include "ace/OS_NS_string.h"
00024 #include "ace/os_include/os_netdb.h"
00025
00026 ACE_RCSID (tao,
00027 IIOP_Acceptor,
00028 "$Id: IIOP_Acceptor.cpp 85630 2009-06-12 18:44:52Z johnnyw $")
00029
00030 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00031
00032 TAO_IIOP_Acceptor::TAO_IIOP_Acceptor (void)
00033 : TAO_Acceptor (IOP::TAG_INTERNET_IOP),
00034 addrs_ (0),
00035 port_span_ (1),
00036 hosts_ (0),
00037 hostname_in_ior_ (0),
00038 endpoint_count_ (0),
00039 version_ (TAO_DEF_GIOP_MAJOR, TAO_DEF_GIOP_MINOR),
00040 orb_core_ (0),
00041 reuse_addr_ (1),
00042 #if defined (ACE_HAS_IPV6) && !defined (ACE_USES_IPV4_IPV6_MIGRATION)
00043 default_address_ (static_cast<unsigned short> (0), ACE_IPV6_ANY, AF_INET6),
00044 #else
00045 default_address_ (static_cast<unsigned short> (0), static_cast<ACE_UINT32> (INADDR_ANY)),
00046 #endif
00047 base_acceptor_ (this),
00048 creation_strategy_ (0),
00049 concurrency_strategy_ (0),
00050 accept_strategy_ (0)
00051 {
00052 #if defined (ACE_HAS_IPV6) && defined (ACE_USES_IPV4_IPV6_MIGRATION)
00053 if (ACE::ipv6_enabled())
00054 default_address_.set (
00055 static_cast<unsigned short> (0),
00056 ACE_IPV6_ANY,
00057 AF_INET6);
00058 #endif
00059 }
00060
00061
00062
00063 TAO_IIOP_Acceptor::~TAO_IIOP_Acceptor (void)
00064 {
00065
00066
00067 this->close ();
00068
00069 delete this->creation_strategy_;
00070 delete this->concurrency_strategy_;
00071 delete this->accept_strategy_;
00072
00073 delete [] this->addrs_;
00074
00075 for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
00076 CORBA::string_free (this->hosts_[i]);
00077
00078 delete [] this->hosts_;
00079
00080 delete [] this->hostname_in_ior_;
00081 }
00082
00083
00084
00085
00086
00087 int
00088 TAO_IIOP_Acceptor::create_profile (const TAO::ObjectKey &object_key,
00089 TAO_MProfile &mprofile,
00090 CORBA::Short priority)
00091 {
00092
00093 if (this->endpoint_count_ == 0)
00094 return -1;
00095
00096
00097
00098 if (priority == TAO_INVALID_PRIORITY &&
00099 this->orb_core_->orb_params ()->shared_profile () == 0)
00100 return this->create_new_profile (object_key, mprofile, priority);
00101 else
00102 return this->create_shared_profile (object_key, mprofile, priority);
00103 }
00104
00105 int
00106 TAO_IIOP_Acceptor::create_new_profile (const TAO::ObjectKey &object_key,
00107 TAO_MProfile &mprofile,
00108 CORBA::Short priority)
00109 {
00110
00111 int const count = mprofile.profile_count ();
00112 if ((mprofile.size () - count) < this->endpoint_count_
00113 && mprofile.grow (count + this->endpoint_count_) == -1)
00114 return -1;
00115
00116
00117 for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
00118 {
00119
00120 if (i > 0
00121 && (this->addrs_[i].get_port_number() == this->addrs_[0].get_port_number())
00122 && ACE_OS::strcmp(this->hosts_[i], this->hosts_[0]) == 0)
00123 continue;
00124
00125 TAO_IIOP_Profile *pfile = 0;
00126 ACE_NEW_RETURN (pfile,
00127 TAO_IIOP_Profile (this->hosts_[i],
00128 this->addrs_[i].get_port_number (),
00129 object_key,
00130 this->addrs_[i],
00131 this->version_,
00132 this->orb_core_),
00133 -1);
00134 pfile->endpoint ()->priority (priority);
00135
00136 if (mprofile.give_profile (pfile) == -1)
00137 {
00138 pfile->_decr_refcnt ();
00139 pfile = 0;
00140 return -1;
00141 }
00142
00143
00144
00145
00146 if (!this->orb_core_->orb_params ()->std_profile_components ()
00147 || (this->version_.major == 1 && this->version_.minor == 0))
00148 continue;
00149
00150 pfile->tagged_components ().set_orb_type (TAO_ORB_TYPE);
00151
00152 TAO_Codeset_Manager *csm = this->orb_core_->codeset_manager();
00153 if (csm)
00154 csm->set_codeset(pfile->tagged_components());
00155 }
00156
00157 return 0;
00158 }
00159
00160 int
00161 TAO_IIOP_Acceptor::create_shared_profile (const TAO::ObjectKey &object_key,
00162 TAO_MProfile &mprofile,
00163 CORBA::Short priority)
00164 {
00165 CORBA::ULong index = 0;
00166 TAO_Profile *pfile = 0;
00167 TAO_IIOP_Profile *iiop_profile = 0;
00168
00169
00170 for (TAO_PHandle i = 0; i != mprofile.profile_count (); ++i)
00171 {
00172 pfile = mprofile.get_profile (i);
00173 if (pfile->tag () == IOP::TAG_INTERNET_IOP)
00174 {
00175 iiop_profile = dynamic_cast<TAO_IIOP_Profile *> (pfile);
00176 break;
00177 }
00178 }
00179
00180
00181
00182 if (iiop_profile == 0)
00183 {
00184 ACE_NEW_RETURN (iiop_profile,
00185 TAO_IIOP_Profile (this->hosts_[0],
00186 this->addrs_[0].get_port_number (),
00187 object_key,
00188 this->addrs_[0],
00189 this->version_,
00190 this->orb_core_),
00191 -1);
00192
00193 iiop_profile->endpoint ()->priority (priority);
00194
00195 if (mprofile.give_profile (iiop_profile) == -1)
00196 {
00197 iiop_profile->_decr_refcnt ();
00198 iiop_profile = 0;
00199 return -1;
00200 }
00201
00202
00203
00204
00205 if (this->orb_core_->orb_params ()->std_profile_components ()
00206 && (this->version_.major >= 1 && this->version_.minor >= 1))
00207 {
00208 iiop_profile->tagged_components ().set_orb_type (TAO_ORB_TYPE);
00209 TAO_Codeset_Manager *csm = this->orb_core_->codeset_manager();
00210 if (csm)
00211 csm->set_codeset(iiop_profile->tagged_components());
00212 }
00213
00214 index = 1;
00215 }
00216
00217
00218 for (;
00219 index < this->endpoint_count_;
00220 ++index)
00221 {
00222 if (index > 0 &&
00223 this->addrs_[index].get_port_number() == this->addrs_[0].get_port_number() &&
00224 ACE_OS::strcmp(this->hosts_[index], this->hosts_[0]) == 0)
00225 continue;
00226
00227 TAO_IIOP_Endpoint *endpoint = 0;
00228 ACE_NEW_RETURN (endpoint,
00229 TAO_IIOP_Endpoint (this->hosts_[index],
00230 this->addrs_[index].get_port_number (),
00231 this->addrs_[index]),
00232 -1);
00233 endpoint->priority (priority);
00234 iiop_profile->add_endpoint (endpoint);
00235 }
00236
00237 return 0;
00238 }
00239
00240 int
00241 TAO_IIOP_Acceptor::is_collocated (const TAO_Endpoint *endpoint)
00242 {
00243 const TAO_IIOP_Endpoint *endp =
00244 dynamic_cast<const TAO_IIOP_Endpoint *> (endpoint);
00245
00246
00247 if (endp == 0)
00248 return 0;
00249
00250 for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
00251 {
00252
00253
00254
00255
00256
00257
00258 if (endp->port() == this->addrs_[i].get_port_number()
00259 && ACE_OS::strcmp(endp->host(), this->hosts_[i]) == 0)
00260 return 1;
00261 }
00262
00263 return 0;
00264 }
00265
00266 int
00267 TAO_IIOP_Acceptor::close (void)
00268 {
00269 return this->base_acceptor_.close ();
00270 }
00271
00272 int
00273 TAO_IIOP_Acceptor::open (TAO_ORB_Core *orb_core,
00274 ACE_Reactor *reactor,
00275 int major,
00276 int minor,
00277 const char *address,
00278 const char *options)
00279 {
00280 if (TAO_debug_level > 2)
00281 {
00282 ACE_DEBUG ((LM_DEBUG,
00283 ACE_TEXT ("TAO (%P|%t) - ")
00284 ACE_TEXT ("IIOP_Acceptor::open, address==%C, options=%C\n"),
00285 address, options));
00286 }
00287
00288 this->orb_core_ = orb_core;
00289
00290 if (this->hosts_ != 0)
00291 {
00292
00293
00294 ACE_ERROR_RETURN ((LM_ERROR,
00295 ACE_TEXT ("TAO (%P|%t) - ")
00296 ACE_TEXT ("IIOP_Acceptor::open, ")
00297 ACE_TEXT ("hostname already set\n\n")),
00298 -1);
00299 }
00300
00301 if (address == 0)
00302 return -1;
00303
00304 if (major >=0 && minor >= 0)
00305 this->version_.set_version (static_cast<CORBA::Octet> (major),
00306 static_cast<CORBA::Octet> (minor));
00307
00308 if (this->parse_options (options) == -1)
00309 return -1;
00310
00311 ACE_CString specified_hostname;
00312 ACE_INET_Addr addr;
00313 int def_type = AF_UNSPEC;
00314
00315 if (this->parse_address (address,
00316 addr,
00317 specified_hostname,
00318 &def_type) == -1)
00319 return -1;
00320
00321 if (specified_hostname.length() == 0)
00322 {
00323
00324
00325
00326
00327
00328 if (this->probe_interfaces (orb_core, def_type) == -1)
00329 return -1;
00330
00331
00332
00333
00334 addr.set(this->default_address_);
00335
00336 return this->open_i (addr, reactor);
00337 }
00338
00339 #if defined (ACE_HAS_IPV6)
00340
00341 if (this->orb_core_->orb_params ()->connect_ipv6_only () &&
00342 (addr.get_type () != AF_INET6 ||
00343 addr.is_ipv4_mapped_ipv6 ()))
00344 {
00345 ACE_ERROR_RETURN ((LM_ERROR,
00346 ACE_TEXT ("TAO (%P|%t) - ")
00347 ACE_TEXT ("IIOP_Acceptor::open, ")
00348 ACE_TEXT ("non-IPv6 endpoints not allowed when ")
00349 ACE_TEXT ("connect_ipv6_only is set\n\n")),
00350 -1);
00351 }
00352 #endif
00353
00354 if (TAO_debug_level > 2)
00355 {
00356 ACE_DEBUG ((LM_DEBUG,
00357 ACE_TEXT ("TAO (%P|%t) - ")
00358 ACE_TEXT ("IIOP_Acceptor::open, specified host=%C:%d\n"),
00359 (specified_hostname.length() == 0 ? "<null>" : specified_hostname.c_str()),
00360 addr.get_port_number ()));
00361 }
00362
00363 this->endpoint_count_ = 1;
00364
00365 ACE_NEW_RETURN (this->addrs_,
00366 ACE_INET_Addr[this->endpoint_count_],
00367 -1);
00368
00369 ACE_NEW_RETURN (this->hosts_,
00370 char *[this->endpoint_count_],
00371 -1);
00372
00373 this->hosts_[0] = 0;
00374
00375 if (this->hostname_in_ior_ != 0)
00376 {
00377 if (TAO_debug_level > 2)
00378 {
00379 ACE_DEBUG ((LM_DEBUG,
00380 ACE_TEXT ("TAO (%P|%t) - ")
00381 ACE_TEXT ("IIOP_Acceptor::open, ")
00382 ACE_TEXT ("Overriding address in IOR with %C\n"),
00383 this->hostname_in_ior_));
00384 }
00385 specified_hostname = this->hostname_in_ior_;
00386 }
00387
00388 if (this->hostname (orb_core,
00389 addr,
00390 this->hosts_[0],
00391 specified_hostname.c_str()) != 0)
00392 return -1;
00393
00394
00395
00396 if (this->addrs_[0].set (addr) != 0)
00397 return -1;
00398
00399 return this->open_i (addr, reactor);
00400 }
00401
00402 int
00403 TAO_IIOP_Acceptor::open_default (TAO_ORB_Core *orb_core,
00404 ACE_Reactor *reactor,
00405 int major,
00406 int minor,
00407 const char *options)
00408 {
00409 this->orb_core_ = orb_core;
00410
00411 if (this->hosts_ != 0)
00412 {
00413
00414
00415 ACE_ERROR_RETURN ((LM_ERROR,
00416 ACE_TEXT ("TAO (%P|%t) - ")
00417 ACE_TEXT ("IIOP_Acceptor::open_default, ")
00418 ACE_TEXT ("hostname already set\n\n")),
00419 -1);
00420 }
00421
00422 if (major >= 0 && minor >= 0)
00423 this->version_.set_version (static_cast<CORBA::Octet> (major),
00424 static_cast<CORBA::Octet> (minor));
00425
00426
00427 if (this->parse_options (options) == -1)
00428 return -1;
00429
00430
00431 if (this->probe_interfaces (orb_core) == -1)
00432 return -1;
00433
00434
00435
00436
00437 ACE_INET_Addr addr;
00438
00439 if (addr.set (this->default_address_) != 0)
00440 return -1;
00441
00442 return this->open_i (addr, reactor);
00443 }
00444
00445 int
00446 TAO_IIOP_Acceptor::open_i (const ACE_INET_Addr& addr,
00447 ACE_Reactor *reactor)
00448 {
00449 ACE_NEW_RETURN (this->creation_strategy_,
00450 CREATION_STRATEGY (this->orb_core_),
00451 -1);
00452
00453 ACE_NEW_RETURN (this->concurrency_strategy_,
00454 CONCURRENCY_STRATEGY (this->orb_core_),
00455 -1);
00456
00457 ACE_NEW_RETURN (this->accept_strategy_,
00458 ACCEPT_STRATEGY (this->orb_core_),
00459 -1);
00460
00461 unsigned short const requested_port = addr.get_port_number ();
00462 if (requested_port == 0)
00463 {
00464
00465 if (this->base_acceptor_.open (addr,
00466 reactor,
00467 this->creation_strategy_,
00468 this->accept_strategy_,
00469 this->concurrency_strategy_,
00470 0, 0, 0, 1,
00471 this->reuse_addr_) == -1)
00472 {
00473 if (TAO_debug_level > 0)
00474 ACE_ERROR ((LM_ERROR,
00475 ACE_TEXT ("TAO (%P|%t) - IIOP_Acceptor::open_i, ")
00476 ACE_TEXT ("%p\n"),
00477 ACE_TEXT ("cannot open acceptor")));
00478 return -1;
00479 }
00480 }
00481 else
00482 {
00483 ACE_INET_Addr a(addr);
00484
00485 bool found_a_port = false;
00486 ACE_UINT32 last_port = requested_port + this->port_span_ - 1;
00487 if (last_port > ACE_MAX_DEFAULT_PORT)
00488 {
00489 last_port = ACE_MAX_DEFAULT_PORT;
00490 }
00491
00492 for (ACE_UINT32 p = requested_port; p <= last_port; p++)
00493 {
00494 if (TAO_debug_level > 5)
00495 ACE_DEBUG ((LM_DEBUG,
00496 ACE_TEXT ("TAO (%P|%t) - IIOP_Acceptor::open_i, ")
00497 ACE_TEXT ("trying to listen on port %d\n"), p));
00498
00499
00500 a.set_port_number ((u_short)p);
00501 if (this->base_acceptor_.open (a,
00502 reactor,
00503 this->creation_strategy_,
00504 this->accept_strategy_,
00505 this->concurrency_strategy_,
00506 0, 0, 0, 1,
00507 this->reuse_addr_) != -1)
00508 {
00509 found_a_port = true;
00510 break;
00511 }
00512 }
00513
00514
00515 if (! found_a_port)
00516 {
00517 if (TAO_debug_level > 0)
00518 ACE_DEBUG ((LM_DEBUG,
00519 ACE_TEXT ("TAO (%P|%t) - IIOP_Acceptor::open_i, ")
00520 ACE_TEXT ("cannot open acceptor in port range (%d,%d)")
00521 ACE_TEXT ("- %p\n"),
00522 requested_port, last_port, ACE_TEXT("")));
00523 return -1;
00524 }
00525 }
00526
00527 #if defined (ACE_HAS_IPV6) && defined (ACE_HAS_IPV6_V6ONLY)
00528
00529
00530 if (this->orb_core_->orb_params ()->connect_ipv6_only () &&
00531 addr.is_any ())
00532 {
00533 if (TAO_debug_level > 5)
00534 ACE_DEBUG ((LM_DEBUG,
00535 ACE_TEXT("TAO (%P|%t) - IIOP_Acceptor::open_i, ")
00536 ACE_TEXT("setting IPV6_V6ONLY\n")));
00537
00538
00539 int on = 1;
00540 if (this->base_acceptor_.acceptor ().set_option (IPPROTO_IPV6,
00541 IPV6_V6ONLY,
00542 (void *) &on,
00543 sizeof (on)) == -1)
00544 {
00545 ACE_ERROR ((LM_ERROR,
00546 ACE_TEXT ("TAO (%P|%t) - IIOP_Acceptor::open_i, ")
00547 ACE_TEXT ("%p\n"),
00548 ACE_TEXT ("cannot set IPV6_V6ONLY")));
00549 }
00550 }
00551 #endif
00552
00553 ACE_INET_Addr address;
00554
00555
00556
00557 if (this->base_acceptor_.acceptor ().get_local_addr (address) != 0)
00558 {
00559 if (TAO_debug_level > 0)
00560 ACE_ERROR ((LM_ERROR,
00561 ACE_TEXT ("TAO (%P|%t) - IIOP_Acceptor::open_i, ")
00562 ACE_TEXT ("%p\n"),
00563 ACE_TEXT ("cannot get local addr")));
00564 return -1;
00565 }
00566
00567
00568
00569
00570
00571 unsigned short port = address.get_port_number ();
00572 for (CORBA::ULong j = 0; j < this->endpoint_count_; ++j)
00573 this->addrs_[j].set_port_number (port, 1);
00574
00575 this->default_address_.set_port_number (port);
00576
00577 (void) this->base_acceptor_.acceptor().enable (ACE_CLOEXEC);
00578
00579
00580
00581
00582 if (TAO_debug_level > 5)
00583 {
00584 for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
00585 {
00586 ACE_DEBUG ((LM_DEBUG,
00587 ACE_TEXT ("TAO (%P|%t) - IIOP_Acceptor::open_i, ")
00588 ACE_TEXT ("listening on: <%C:%u>\n"),
00589 this->hosts_[i],
00590 this->addrs_[i].get_port_number ()));
00591 }
00592 }
00593
00594
00595
00596
00597
00598 this->set_error_retry_delay (
00599 this->orb_core_->orb_params ()->accept_error_delay());
00600
00601 return 0;
00602 }
00603
00604 int
00605 TAO_IIOP_Acceptor::hostname (TAO_ORB_Core *orb_core,
00606 const ACE_INET_Addr &addr,
00607 char *&host,
00608 const char *specified_hostname)
00609 {
00610 if (this->hostname_in_ior_ != 0)
00611 {
00612 if (TAO_debug_level >= 5)
00613 ACE_DEBUG ((LM_DEBUG,
00614 ACE_TEXT ("TAO (%P|%t) IIOP_Acceptor - ")
00615 ACE_TEXT ("Overriding the hostname with <%C>\n"),
00616 this->hostname_in_ior_));
00617
00618 host = CORBA::string_dup (this->hostname_in_ior_);
00619 }
00620 else if (orb_core->orb_params ()->use_dotted_decimal_addresses ())
00621 {
00622
00623
00624 return this->dotted_decimal_address (addr, host);
00625 }
00626 else if (specified_hostname != 0)
00627 {
00628
00629
00630 host = CORBA::string_dup (specified_hostname);
00631 }
00632 else
00633 {
00634 char tmp_host[MAXHOSTNAMELEN + 1];
00635
00636
00637 #if defined (ACE_HAS_IPV6)
00638
00639
00640
00641
00642 if (addr.is_ipv4_compat_ipv6 () ||
00643 addr.get_host_name (tmp_host, sizeof (tmp_host)) != 0)
00644 #else
00645 if (addr.get_host_name (tmp_host, sizeof (tmp_host)) != 0)
00646 #endif
00647 {
00648
00649 return this->dotted_decimal_address (addr, host);
00650 }
00651 else
00652 {
00653 host = CORBA::string_dup (tmp_host);
00654 }
00655 }
00656
00657 return 0;
00658 }
00659
00660
00661 int
00662 TAO_IIOP_Acceptor::parse_address (const char *address,
00663 ACE_INET_Addr &addr,
00664 ACE_CString &specified_hostname,
00665 int *def_type)
00666 {
00667 {
00668 ACE_INET_Addr tmp;
00669 addr.set (tmp);
00670 specified_hostname.clear();
00671 }
00672
00673 const char *port_separator_loc = ACE_OS::strchr (address, ':');
00674 char tmp_host[MAXHOSTNAMELEN + 1];
00675 tmp_host[0] = '\0';
00676 bool host_defaulted = port_separator_loc == address;
00677 bool ipv6_in_host = false;
00678 if (def_type)
00679 *def_type = AF_UNSPEC;
00680
00681 #if defined (ACE_HAS_IPV6)
00682
00683
00684 if ((this->version_.major > TAO_MIN_IPV6_IIOP_MAJOR ||
00685 this->version_.minor >= TAO_MIN_IPV6_IIOP_MINOR) &&
00686 address[0] == '[')
00687 {
00688
00689
00690 char const * const cp_pos = ACE_OS::strchr (address, ']');
00691 if (cp_pos == 0)
00692 {
00693
00694 ACE_ERROR_RETURN ((LM_ERROR,
00695 ACE_TEXT ("TAO (%P|%t) - ")
00696 ACE_TEXT ("IIOP_Acceptor::open, ")
00697 ACE_TEXT ("Invalid IPv6 decimal address specified\n\n")),
00698 -1);
00699 }
00700 else
00701 {
00702
00703 size_t const len = cp_pos - (address + 1);
00704
00705 if (len >= sizeof (tmp_host))
00706 return -1;
00707
00708 ipv6_in_host = true;
00709 host_defaulted = (cp_pos == address+1) ||
00710 (cp_pos == address+3 && address[1] == ':' && address[2] == ':');
00711 if (cp_pos[1] == ':')
00712 port_separator_loc = cp_pos + 1;
00713 else
00714 port_separator_loc = 0;
00715 if (def_type)
00716 *def_type = AF_INET6;
00717
00718 ACE_OS::memcpy (tmp_host, address + 1, len);
00719 tmp_host[len] = '\0';
00720 }
00721 }
00722 else
00723 #endif
00724 if (!host_defaulted)
00725 {
00726 if (port_separator_loc != 0)
00727 {
00728
00729 size_t const len = port_separator_loc - address;
00730
00731 if (len >= sizeof (tmp_host))
00732 return -1;
00733
00734 ACE_OS::memcpy (tmp_host, address, len);
00735 tmp_host[len] = '\0';
00736 }
00737 else
00738 ACE_OS::strcpy (tmp_host, address);
00739 }
00740
00741 if (!ipv6_in_host && !host_defaulted)
00742 {
00743 if (addr.set((unsigned short)0,tmp_host) != 0)
00744 return -1;
00745 this->default_address_.set(addr);
00746 host_defaulted = addr.is_any();
00747 if (def_type)
00748 *def_type = AF_INET;
00749 }
00750
00751 if (host_defaulted)
00752 {
00753
00754 unsigned short portno = 0;
00755 if (port_separator_loc != 0)
00756 {
00757 portno =
00758 static_cast<u_short> (ACE_OS::atoi (port_separator_loc +
00759 sizeof (':')));
00760 }
00761 this->default_address_.set_port_number (portno);
00762
00763
00764 if (addr.set (this->default_address_) != 0)
00765 return -1;
00766 }
00767 else if (port_separator_loc == 0)
00768 {
00769
00770
00771 specified_hostname = tmp_host[0] == '\0' ? address : tmp_host;
00772 if (addr.set ((unsigned short) 0,
00773 specified_hostname.c_str()) != 0)
00774 return -1;
00775 }
00776 else
00777 {
00778
00779 if (addr.set (address) != 0)
00780 return -1;
00781 if (tmp_host[0] == '\0')
00782 {
00783
00784 size_t const len = port_separator_loc - address;
00785
00786 if (len >= sizeof (tmp_host))
00787 return -1;
00788
00789 ACE_OS::memcpy (tmp_host, address, len);
00790 tmp_host[len] = '\0';
00791 }
00792 specified_hostname = tmp_host;
00793 }
00794
00795 return 1;
00796 }
00797
00798
00799 int
00800 TAO_IIOP_Acceptor::dotted_decimal_address (const ACE_INET_Addr &addr,
00801 char *&host)
00802 {
00803 int result = 0;
00804 const char *tmp = 0;
00805
00806
00807
00808
00809
00810
00811 if (addr.is_any ())
00812 {
00813 ACE_INET_Addr new_addr;
00814 #if defined (ACE_HAS_IPV6)
00815 result = new_addr.set (addr.get_port_number (),
00816 addr.get_host_name (),
00817 1,
00818 addr.get_type ());
00819 #else
00820 result = new_addr.set (addr.get_port_number (),
00821 addr.get_host_name ());
00822 #endif
00823 tmp = new_addr.get_host_addr ();
00824 }
00825 else
00826 tmp = addr.get_host_addr ();
00827
00828 if (tmp == 0 || result != 0)
00829 {
00830 if (TAO_debug_level > 0)
00831 ACE_ERROR ((LM_ERROR,
00832 ACE_TEXT ("TAO (%P|%t) - ")
00833 ACE_TEXT ("IIOP_Acceptor::dotted_decimal_address, ")
00834 ACE_TEXT ("- %p\n"),
00835 ACE_TEXT ("cannot determine hostname")));
00836 return -1;
00837 }
00838
00839 host = CORBA::string_dup (tmp);
00840 return 0;
00841 }
00842
00843 int
00844 TAO_IIOP_Acceptor::probe_interfaces (TAO_ORB_Core *orb_core, int def_type)
00845 {
00846
00847
00848
00849
00850 ACE_INET_Addr *if_addrs = 0;
00851 size_t if_cnt = 0;
00852
00853 if (ACE::get_ip_interfaces (if_cnt, if_addrs) != 0
00854 && errno != ENOTSUP)
00855 {
00856
00857
00858
00859 return -1;
00860 }
00861
00862 if (if_cnt == 0 || if_addrs == 0)
00863 {
00864 if (TAO_debug_level > 0)
00865 {
00866 ACE_DEBUG ((LM_WARNING,
00867 ACE_TEXT ("TAO (%P|%t) - Unable to probe network ")
00868 ACE_TEXT ("interfaces. Using default.\n")));
00869 }
00870
00871 if_cnt = 1;
00872 delete [] if_addrs;
00873 ACE_NEW_RETURN (if_addrs,
00874 ACE_INET_Addr[if_cnt],
00875 -1);
00876 }
00877
00878
00879
00880 size_t lo_cnt = 0;
00881 for (size_t j = 0; j < if_cnt; ++j)
00882 if (if_addrs[j].is_loopback ())
00883 ++lo_cnt;
00884
00885 #if defined (ACE_HAS_IPV6)
00886 size_t ipv4_cnt = 0;
00887 size_t ipv4_lo_cnt = 0;
00888 size_t ipv6_ll = 0;
00889 bool ipv6_non_ll = false;
00890
00891
00892 for (size_t j = 0; j < if_cnt; ++j)
00893 if (if_addrs[j].get_type () != AF_INET6 ||
00894 if_addrs[j].is_ipv4_mapped_ipv6 ())
00895 {
00896 ++ipv4_cnt;
00897 if (if_addrs[j].is_loopback ())
00898 ++ipv4_lo_cnt;
00899 }
00900 else if (!if_addrs[j].is_linklocal () &&
00901 !if_addrs[j].is_loopback())
00902 {
00903 ipv6_non_ll = true;
00904 }
00905 else if (!orb_core->orb_params ()->use_ipv6_link_local () &&
00906 if_addrs[j].is_linklocal ())
00907 {
00908 ++ipv6_ll;
00909 }
00910 #endif
00911
00912
00913
00914 ACE_Auto_Basic_Array_Ptr<ACE_INET_Addr> safe_if_addrs (if_addrs);
00915
00916 #if defined (ACE_HAS_IPV6)
00917 bool ipv4_only = def_type == AF_INET;
00918 bool ipv6_only = (def_type == AF_INET6) ||
00919 orb_core->orb_params ()->connect_ipv6_only ();
00920 #if defined (ACE_WIN32)
00921 if (this->default_address_.get_type () == AF_INET)
00922 ipv4_only = true;
00923 else
00924 ipv6_only = true;
00925 #endif
00926
00927
00928
00929 bool ignore_lo;
00930 if (ipv6_only)
00931
00932 ignore_lo = ipv6_non_ll;
00933 else if (ipv4_only)
00934 ignore_lo = ipv4_cnt != ipv4_lo_cnt;
00935 else
00936 ignore_lo = if_cnt != lo_cnt;
00937
00938
00939 size_t if_ok_cnt = if_cnt;
00940 if (ipv6_only)
00941 {
00942 if_ok_cnt -= ipv4_cnt;
00943 lo_cnt -= ipv4_lo_cnt;
00944 ipv4_lo_cnt = 0;
00945 }
00946 else if (ipv4_only)
00947 {
00948 if_ok_cnt = ipv4_cnt;
00949 lo_cnt = ipv4_lo_cnt;
00950 ipv6_ll = 0;
00951 }
00952
00953
00954
00955
00956
00957 if (!ipv4_only && !ipv6_non_ll)
00958 lo_cnt = ipv4_lo_cnt;
00959
00960 if (!ignore_lo)
00961 this->endpoint_count_ = static_cast<CORBA::ULong> (if_ok_cnt - ipv6_ll);
00962 else
00963 this->endpoint_count_ = static_cast<CORBA::ULong> (if_ok_cnt - ipv6_ll - lo_cnt);
00964 #else
00965
00966
00967
00968 bool ignore_lo;
00969 ignore_lo = if_cnt != lo_cnt;
00970 if (!ignore_lo)
00971 this->endpoint_count_ = static_cast<CORBA::ULong> (if_cnt);
00972 else
00973 this->endpoint_count_ = static_cast<CORBA::ULong> (if_cnt - lo_cnt);
00974 #endif
00975
00976 if (this->endpoint_count_ == 0)
00977 {
00978 if (TAO_debug_level > 0)
00979 ACE_DEBUG ((LM_DEBUG,
00980 ACE_TEXT("(%P|%t) TAO_IIOP_Acceptor::probe_interfaces ")
00981 ACE_TEXT("found no usable addresses, def_type = %d\n"),
00982 def_type));
00983 return -1;
00984 }
00985
00986 ACE_NEW_RETURN (this->addrs_,
00987 ACE_INET_Addr[this->endpoint_count_],
00988 -1);
00989
00990 ACE_NEW_RETURN (this->hosts_,
00991 char *[this->endpoint_count_],
00992 -1);
00993
00994 ACE_OS::memset (this->hosts_, 0, sizeof (char*) * this->endpoint_count_);
00995
00996
00997
00998
00999 size_t host_cnt = 0;
01000
01001 for (size_t i = 0; i < if_cnt; ++i)
01002 {
01003 #if defined (ACE_HAS_IPV6)
01004
01005
01006 if (ignore_lo &&
01007 if_addrs[i].is_loopback () &&
01008 (ipv4_only ||
01009 ipv6_non_ll ||
01010 if_addrs[i].get_type () != AF_INET6))
01011 continue;
01012
01013
01014 if (ipv4_only &&
01015 (if_addrs[i].get_type () != AF_INET))
01016 continue;
01017
01018
01019 if (ipv6_only &&
01020 (if_addrs[i].get_type () != AF_INET6 ||
01021 if_addrs[i].is_ipv4_mapped_ipv6 ()))
01022 continue;
01023
01024
01025 if (!orb_core->orb_params ()->use_ipv6_link_local () &&
01026 if_addrs[i].is_linklocal ())
01027 continue;
01028 #else
01029
01030
01031 if (ignore_lo &&
01032 if_addrs[i].is_loopback ())
01033 continue;
01034 #endif
01035
01036 if (this->hostname (orb_core,
01037 if_addrs[i],
01038 this->hosts_[host_cnt]) != 0)
01039 return -1;
01040
01041
01042
01043 if (this->addrs_[host_cnt].set (if_addrs[i]) != 0)
01044 return -1;
01045
01046 ++host_cnt;
01047 }
01048
01049 return 0;
01050 }
01051
01052 CORBA::ULong
01053 TAO_IIOP_Acceptor::endpoint_count (void)
01054 {
01055 return this->endpoint_count_;
01056 }
01057
01058 int
01059 TAO_IIOP_Acceptor::object_key (IOP::TaggedProfile &profile,
01060 TAO::ObjectKey &object_key)
01061 {
01062
01063 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
01064 TAO_InputCDR cdr (profile.profile_data.mb ());
01065 #else
01066 TAO_InputCDR cdr (reinterpret_cast<char*> (profile.profile_data.get_buffer ()),
01067 profile.profile_data.length ());
01068 #endif
01069
01070 CORBA::Octet major;
01071 CORBA::Octet minor = CORBA::Octet();
01072
01073
01074
01075 if (!(cdr.read_octet (major)
01076 && cdr.read_octet (minor)))
01077 {
01078 if (TAO_debug_level > 0)
01079 {
01080 ACE_DEBUG ((LM_DEBUG,
01081 ACE_TEXT ("TAO (%P|%t) - TAO_IIOP_Acceptor::object_key, v%d.%d\n"),
01082 major,
01083 minor));
01084 }
01085 return -1;
01086 }
01087
01088 CORBA::String_var host;
01089 CORBA::UShort port = 0;
01090
01091
01092 if (cdr.read_string (host.out ()) == 0
01093 || cdr.read_ushort (port) == 0)
01094 {
01095 if (TAO_debug_level > 0)
01096 {
01097 ACE_DEBUG ((LM_DEBUG,
01098 ACE_TEXT ("TAO (%P|%t) - TAO_IIOP_Acceptor::object_key, ")
01099 ACE_TEXT ("error while decoding host/port\n")));
01100 }
01101 return -1;
01102 }
01103
01104
01105 if ((cdr >> object_key) == 0)
01106 return -1;
01107
01108
01109
01110 return 1;
01111 }
01112
01113 int
01114 TAO_IIOP_Acceptor::parse_options (const char *str)
01115 {
01116 if (str == 0)
01117 return 0;
01118
01119
01120
01121
01122
01123 const ACE_CString options (str);
01124
01125 const size_t len = options.length ();
01126
01127 static const char option_delimiter = '&';
01128
01129
01130 int argc = 1;
01131
01132 for (size_t i = 0; i < len; ++i)
01133 if (options[i] == option_delimiter)
01134 argc++;
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144 ACE_CString *argv_base = 0;
01145 ACE_NEW_RETURN (argv_base, ACE_CString[argc],-1);
01146 ACE_CString **argv = 0;
01147 ACE_NEW_RETURN (argv, ACE_CString*[argc],-1);
01148
01149 ACE_CString::size_type begin = 0;
01150 ACE_CString::size_type end = 0;
01151 int result = 0;
01152 for (int j = 0; j < argc; ++j)
01153 {
01154 if (j < argc - 1)
01155 end = options.find (option_delimiter, begin);
01156 else
01157 end = len;
01158
01159 if (end == begin)
01160 {
01161 ACE_ERROR ((LM_ERROR,
01162 ACE_TEXT ("TAO (%P|%t) - Zero length IIOP option.\n")));
01163 result = -1;
01164 break;
01165 }
01166 else if (end != options.npos)
01167 {
01168 argv_base[j] = options.substring (begin, end - begin);
01169 argv[j] = &argv_base[j];
01170 begin = end + 1;
01171 }
01172 else
01173 {
01174 break;
01175 }
01176 }
01177
01178 if (result == 0)
01179 result = this->parse_options_i (argc,argv);
01180
01181 if (argc > 0)
01182 {
01183 ACE_ERROR ((LM_ERROR,
01184 ACE_TEXT ("TAO (%P|%t) - IIOP")
01185 ACE_TEXT (" endpoint has %d unknown options:\n"),
01186 argc));
01187 for (int i = 0; i < argc; i++)
01188 ACE_ERROR ((LM_ERROR,
01189 ACE_TEXT("\t%C\n"),
01190 argv[i]->c_str()));
01191 result = -1;
01192 }
01193 delete [] argv;
01194 delete [] argv_base;
01195 return result;
01196 }
01197
01198 int
01199 TAO_IIOP_Acceptor::parse_options_i (int &argc,
01200 ACE_CString **argv)
01201 {
01202 int i = 0;
01203 while (i < argc)
01204 {
01205 ACE_CString::size_type const len = argv[i]->length ();
01206 ACE_CString::size_type const slot = argv[i]->find ('=');
01207
01208 if (slot == len - 1
01209 || slot == ACE_CString::npos)
01210 ACE_ERROR_RETURN ((LM_ERROR,
01211 ACE_TEXT ("TAO (%P|%t) - IIOP option <%C> is ")
01212 ACE_TEXT ("missing a value.\n"),
01213 argv[i]->c_str ()),
01214 -1);
01215
01216 ACE_CString name = argv[i]->substring (0, slot);
01217 ACE_CString value = argv[i]->substring (slot + 1);
01218
01219 if (name.length () == 0)
01220 ACE_ERROR_RETURN ((LM_ERROR,
01221 ACE_TEXT ("TAO (%P|%t) Zero length IIOP ")
01222 ACE_TEXT ("option name.\n")),
01223 -1);
01224 if (name == "portspan")
01225 {
01226 int range = static_cast <int> (ACE_OS::atoi (value.c_str ()));
01227
01228 if (range < 1 || range > ACE_MAX_DEFAULT_PORT)
01229 ACE_ERROR_RETURN ((LM_ERROR,
01230 ACE_TEXT ("TAO (%P|%t) Invalid IIOP endpoint ")
01231 ACE_TEXT ("portspan: <%C>\n")
01232 ACE_TEXT ("Valid range 1 -- %d\n"),
01233 value.c_str (), ACE_MAX_DEFAULT_PORT),
01234 -1);
01235
01236 this->port_span_ = static_cast <u_short> (range);
01237 }
01238 else if (name == "hostname_in_ior")
01239 {
01240 this->hostname_in_ior_ = value.rep ();
01241 }
01242 else if (name == "reuse_addr")
01243 {
01244 this->reuse_addr_ = ACE_OS::atoi (value.c_str ());
01245 }
01246 else
01247 {
01248
01249 ++i;
01250 continue;
01251 }
01252
01253
01254
01255
01256 --argc;
01257 ACE_CString *temp = argv[i];
01258 for (int j = i; j <= argc-1; ++j)
01259 argv[j] = argv[j+1];
01260 argv[argc] = temp;
01261 }
01262 return 0;
01263 }
01264
01265 TAO_END_VERSIONED_NAMESPACE_DECL
01266
01267 #endif
01268
01269