Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tao / Strategies / DIOP_Endpoint.cpp
blob335af696afb4a576783f2fa19f17bfacf5805cec
1 #include "tao/Strategies/DIOP_Endpoint.h"
3 #if defined (TAO_HAS_DIOP) && (TAO_HAS_DIOP != 0)
5 #include "tao/Strategies/DIOP_Connection_Handler.h"
6 #include "tao/debug.h"
7 #include "tao/ORB_Constants.h"
9 #include "ace/OS_NS_stdio.h"
10 #include "ace/OS_NS_string.h"
12 #if !defined (__ACE_INLINE__)
13 # include "tao/Strategies/DIOP_Endpoint.inl"
14 #endif /* __ACE_INLINE__ */
16 #include "ace/os_include/os_netdb.h"
17 #include <cstring>
19 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
21 TAO_DIOP_Endpoint::TAO_DIOP_Endpoint (const ACE_INET_Addr &addr,
22 int use_dotted_decimal_addresses)
24 : TAO_Endpoint (TAO_TAG_DIOP_PROFILE)
25 , host_ ()
26 , port_ (0)
27 #if defined (ACE_HAS_IPV6)
28 , is_ipv6_decimal_ (false)
29 #endif /* ACE_HAS_IPV6 */
30 , object_addr_ (addr)
31 , object_addr_set_ (false)
32 , next_ (0)
34 this->set (addr, use_dotted_decimal_addresses);
37 TAO_DIOP_Endpoint::TAO_DIOP_Endpoint (const char *host,
38 CORBA::UShort port,
39 const ACE_INET_Addr &addr,
40 CORBA::Short priority)
41 : TAO_Endpoint (TAO_TAG_DIOP_PROFILE,
42 priority)
43 , host_ ()
44 , port_ (port)
45 #if defined (ACE_HAS_IPV6)
46 , is_ipv6_decimal_ (false)
47 #endif /* ACE_HAS_IPV6 */
48 , object_addr_ (addr)
49 , object_addr_set_ (false)
50 , next_ (0)
52 this->host (host); // With IPv6 performs check for decimal address
55 TAO_DIOP_Endpoint::TAO_DIOP_Endpoint ()
56 : TAO_Endpoint (TAO_TAG_DIOP_PROFILE),
57 host_ (),
58 port_ (0),
59 #if defined (ACE_HAS_IPV6)
60 is_ipv6_decimal_ (false),
61 #endif /* ACE_HAS_IPV6 */
62 object_addr_ (),
63 object_addr_set_ (false),
64 next_ (0)
68 TAO_DIOP_Endpoint::TAO_DIOP_Endpoint (const char *host,
69 CORBA::UShort port,
70 CORBA::Short priority)
71 : TAO_Endpoint (TAO_TAG_DIOP_PROFILE, priority),
72 host_ (),
73 port_ (port),
74 #if defined (ACE_HAS_IPV6)
75 is_ipv6_decimal_ (false),
76 #endif /* ACE_HAS_IPV6 */
77 object_addr_ (),
78 object_addr_set_ (false),
79 next_ (0)
81 this->host (host); // With IPv6 performs check for decimal address
84 int
85 TAO_DIOP_Endpoint::set (const ACE_INET_Addr &addr,
86 int use_dotted_decimal_addresses)
88 char tmp_host[MAXHOSTNAMELEN + 1];
90 #if defined (ACE_HAS_IPV6)
91 this->is_ipv6_decimal_ = false; // Reset
92 #endif /* ACE_HAS_IPV6 */
94 if (use_dotted_decimal_addresses
95 || addr.get_host_name (tmp_host, sizeof (tmp_host)) != 0)
97 if (use_dotted_decimal_addresses == 0 && TAO_debug_level > 5)
99 TAOLIB_DEBUG ((LM_DEBUG,
100 ACE_TEXT ("TAO (%P|%t) - DIOP_Endpoint::set, ")
101 ACE_TEXT ("%p\n"),
102 ACE_TEXT ("cannot determine hostname")));
105 const char *tmp = addr.get_host_addr ();
106 if (tmp == 0)
108 if (TAO_debug_level > 0)
109 TAOLIB_DEBUG ((LM_DEBUG,
110 ACE_TEXT ("TAO (%P|%t) - ")
111 ACE_TEXT ("DIOP_Endpoint::set, ")
112 ACE_TEXT ("%p\n"),
113 ACE_TEXT ("cannot determine hostname\n")));
114 return -1;
116 else
118 this->host_ = tmp;
119 #if defined (ACE_HAS_IPV6)
120 if (addr.get_type () == PF_INET6)
121 this->is_ipv6_decimal_ = true;
122 #endif /* ACE_HAS_IPV6 */
125 else
126 this->host_ = CORBA::string_dup (tmp_host);
128 this->port_ = addr.get_port_number ();
130 return 0;
134 TAO_DIOP_Endpoint::addr_to_string (char *buffer, size_t length)
136 size_t actual_len =
137 ACE_OS::strlen (this->host_.in ()) // chars in host name
138 + sizeof (':') // delimiter
139 + ACE_OS::strlen ("65536") // max port
140 + sizeof ('\0');
142 #if defined (ACE_HAS_IPV6)
143 if (this->is_ipv6_decimal_)
144 actual_len += 2; // '[' + ']'
145 #endif /* ACE_HAS_IPV6 */
147 if (length < actual_len)
148 return -1;
150 #if defined (ACE_HAS_IPV6)
151 if (this->is_ipv6_decimal_)
152 ACE_OS::sprintf (buffer, "[%s]:%d",
153 this->host_.in (), this->port_);
154 else
155 #endif /* ACE_HAS_IPV6 */
156 ACE_OS::sprintf (buffer, "%s:%d",
157 this->host_.in (), this->port_);
159 return 0;
162 const char *
163 TAO_DIOP_Endpoint::host (const char *h)
165 this->host_ = h;
166 #if defined (ACE_HAS_IPV6)
167 if (std::strchr (h, ':') != 0)
168 this->is_ipv6_decimal_ = true;
169 #endif /* ACE_HAS_IPV6 */
171 return this->host_.in ();
174 TAO_Endpoint *
175 TAO_DIOP_Endpoint::next ()
177 return this->next_;
180 TAO_Endpoint *
181 TAO_DIOP_Endpoint::duplicate ()
183 TAO_DIOP_Endpoint *endpoint = 0;
185 ACE_NEW_RETURN (endpoint,
186 TAO_DIOP_Endpoint (this->host_.in (),
187 this->port_,
188 this->object_addr_,
189 this->priority ()),
192 return endpoint;
195 CORBA::Boolean
196 TAO_DIOP_Endpoint::is_equivalent (const TAO_Endpoint *other_endpoint)
198 TAO_Endpoint *endpt = const_cast<TAO_Endpoint *> (other_endpoint);
200 TAO_DIOP_Endpoint *endpoint = dynamic_cast<TAO_DIOP_Endpoint *> (endpt);
201 if (endpoint == 0)
202 return 0;
204 return (this->port () == endpoint->port ()
205 && ACE_OS::strcmp (this->host (), endpoint->host ()) == 0);
208 CORBA::ULong
209 TAO_DIOP_Endpoint::hash ()
211 if (this->hash_val_ != 0)
212 return this->hash_val_;
215 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
216 guard,
217 this->addr_lookup_lock_,
218 this->hash_val_);
219 // .. DCL
220 if (this->hash_val_ != 0)
221 return this->hash_val_;
223 this->hash_val_ =
224 ACE::hash_pjw (this->host ()) + this->port ();
227 return this->hash_val_;
230 const ACE_INET_Addr &
231 TAO_DIOP_Endpoint::object_addr () const
233 // The object_addr_ is initialized here, rather than at IOR decode
234 // time for several reasons:
235 // 1. A request on the object may never be invoked.
236 // 2. The DNS setup may have changed dynamically.
237 // ...etc..
239 // Double checked locking optimization.
240 if (!this->object_addr_set_)
242 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
243 guard,
244 this->addr_lookup_lock_,
245 this->object_addr_ );
247 if (!this->object_addr_set_)
249 (void) this->object_addr_i ();
252 return this->object_addr_;
255 void
256 TAO_DIOP_Endpoint::object_addr_i () const
258 // We should have already held the lock
260 #if defined (ACE_HAS_IPV6)
261 bool is_ipv4_decimal_ = false;
262 if (!this->is_ipv6_decimal_)
263 is_ipv4_decimal_ =
264 ACE_OS::strspn (this->host_.in (), ".0123456789") ==
265 ACE_OS::strlen (this->host_.in ());
267 // If this is *not* an IPv4 decimal address at first try to
268 // resolve the address as an IPv6 address; if that fails
269 // (or it's an IPv4 address) and the address is *not* an IPv6
270 // decimal address try to resolve it as an IPv4 address.
271 if ((is_ipv4_decimal_ ||
272 this->object_addr_.set (this->port_,
273 this->host_.in (),
275 AF_INET6) == -1) &&
276 (this->is_ipv6_decimal_ ||
277 this->object_addr_.set (this->port_,
278 this->host_.in (),
280 AF_INET) == -1))
281 #else
282 if (this->object_addr_.set (this->port_,
283 this->host_.in ()) == -1)
284 #endif /* ACE_HAS_IPV6 */
286 // If this call fails, it most likely due a hostname
287 // lookup failure caused by a DNS misconfiguration. If
288 // a request is made to the object at the given host and
289 // port, then a CORBA::TRANSIENT() exception should be
290 // thrown.
292 // Invalidate the ACE_INET_Addr. This is used as a flag
293 // to denote that ACE_INET_Addr initialization failed.
294 this->object_addr_.set_type (-1);
296 else
298 this->object_addr_set_ = true;
302 TAO_END_VERSIONED_NAMESPACE_DECL
304 #endif /* TAO_HAS_DIOP && TAO_HAS_DIOP != 0 */