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"
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"
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
)
27 #if defined (ACE_HAS_IPV6)
28 , is_ipv6_decimal_ (false)
29 #endif /* ACE_HAS_IPV6 */
31 , object_addr_set_ (false)
34 this->set (addr
, use_dotted_decimal_addresses
);
37 TAO_DIOP_Endpoint::TAO_DIOP_Endpoint (const char *host
,
39 const ACE_INET_Addr
&addr
,
40 CORBA::Short priority
)
41 : TAO_Endpoint (TAO_TAG_DIOP_PROFILE
,
45 #if defined (ACE_HAS_IPV6)
46 , is_ipv6_decimal_ (false)
47 #endif /* ACE_HAS_IPV6 */
49 , object_addr_set_ (false)
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
),
59 #if defined (ACE_HAS_IPV6)
60 is_ipv6_decimal_ (false),
61 #endif /* ACE_HAS_IPV6 */
63 object_addr_set_ (false),
68 TAO_DIOP_Endpoint::TAO_DIOP_Endpoint (const char *host
,
70 CORBA::Short priority
)
71 : TAO_Endpoint (TAO_TAG_DIOP_PROFILE
, priority
),
74 #if defined (ACE_HAS_IPV6)
75 is_ipv6_decimal_ (false),
76 #endif /* ACE_HAS_IPV6 */
78 object_addr_set_ (false),
81 this->host (host
); // With IPv6 performs check for decimal address
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, ")
102 ACE_TEXT ("cannot determine hostname")));
105 const char *tmp
= addr
.get_host_addr ();
108 if (TAO_debug_level
> 0)
109 TAOLIB_DEBUG ((LM_DEBUG
,
110 ACE_TEXT ("TAO (%P|%t) - ")
111 ACE_TEXT ("DIOP_Endpoint::set, ")
113 ACE_TEXT ("cannot determine hostname\n")));
119 #if defined (ACE_HAS_IPV6)
120 if (addr
.get_type () == PF_INET6
)
121 this->is_ipv6_decimal_
= true;
122 #endif /* ACE_HAS_IPV6 */
126 this->host_
= CORBA::string_dup (tmp_host
);
128 this->port_
= addr
.get_port_number ();
134 TAO_DIOP_Endpoint::addr_to_string (char *buffer
, size_t length
)
137 ACE_OS::strlen (this->host_
.in ()) // chars in host name
138 + sizeof (':') // delimiter
139 + ACE_OS::strlen ("65536") // max port
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
)
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_
);
155 #endif /* ACE_HAS_IPV6 */
156 ACE_OS::sprintf (buffer
, "%s:%d",
157 this->host_
.in (), this->port_
);
163 TAO_DIOP_Endpoint::host (const char *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 ();
175 TAO_DIOP_Endpoint::next ()
181 TAO_DIOP_Endpoint::duplicate ()
183 TAO_DIOP_Endpoint
*endpoint
= 0;
185 ACE_NEW_RETURN (endpoint
,
186 TAO_DIOP_Endpoint (this->host_
.in (),
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
);
204 return (this->port () == endpoint
->port ()
205 && ACE_OS::strcmp (this->host (), endpoint
->host ()) == 0);
209 TAO_DIOP_Endpoint::hash ()
211 if (this->hash_val_
!= 0)
212 return this->hash_val_
;
215 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX
,
217 this->addr_lookup_lock_
,
220 if (this->hash_val_
!= 0)
221 return 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.
239 // Double checked locking optimization.
240 if (!this->object_addr_set_
)
242 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX
,
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_
;
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_
)
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_
,
276 (this->is_ipv6_decimal_
||
277 this->object_addr_
.set (this->port_
,
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
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);
298 this->object_addr_set_
= true;
302 TAO_END_VERSIONED_NAMESPACE_DECL
304 #endif /* TAO_HAS_DIOP && TAO_HAS_DIOP != 0 */