Merge pull request #2218 from jwillemsen/jwi-pthreadsigmask
[ACE_TAO.git] / TAO / tao / Strategies / SCIOP_Connection_Handler.cpp
blob5d6de7fbb5adcfba0dc9d7e630e907521358a5e6
1 #include "tao/Strategies/SCIOP_Connection_Handler.h"
3 #if TAO_HAS_SCIOP == 1
5 // jcohen@atl.lmco.com: The purpose of this is to allow RH9 to build
6 // SCIOR Profile support even without a functioning SCTP implementation
8 #ifndef IPPROTO_SCTP
9 # include "netinet/sctp.h"
10 #else // !IPPROTO_SCTP
11 # ifndef SCTP_NODELAY
12 # define SCTP_NODELAY 1
13 # endif // !SCTP_NODELAY
14 #endif
16 #include "tao/Timeprobe.h"
17 #include "tao/debug.h"
18 #include "tao/ORB_Core.h"
19 #include "tao/ORB.h"
20 #include "tao/CDR.h"
21 #include "tao/Server_Strategy_Factory.h"
22 #include "tao/Strategies/SCIOP_Transport.h"
23 #include "tao/Strategies/SCIOP_Endpoint.h"
24 #include "tao/Transport_Cache_Manager.h"
25 #include "tao/Thread_Lane_Resources.h"
26 #include "tao/Base_Transport_Property.h"
27 #include "tao/Resume_Handle.h"
28 #include "tao/Protocols_Hooks.h"
29 #include "tao/Wait_Strategy.h"
30 #include "ace/os_include/os_netdb.h"
32 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
34 TAO_SCIOP_Connection_Handler::TAO_SCIOP_Connection_Handler (ACE_Thread_Manager *t)
35 : TAO_SCIOP_SVC_HANDLER (t, 0 , 0),
36 TAO_Connection_Handler (0),
37 dscp_codepoint_ (IPDSFIELD_DSCP_DEFAULT << 2)
39 // This constructor should *never* get called, it is just here to
40 // make the compiler happy: the default implementation of the
41 // Creation_Strategy requires a constructor with that signature, we
42 // don't use that implementation, but some (most?) compilers
43 // instantiate it anyway.
44 ACE_ASSERT (0);
48 TAO_SCIOP_Connection_Handler::TAO_SCIOP_Connection_Handler (TAO_ORB_Core *orb_core)
49 : TAO_SCIOP_SVC_HANDLER (orb_core->thr_mgr (), 0, 0),
50 TAO_Connection_Handler (orb_core),
51 dscp_codepoint_ (IPDSFIELD_DSCP_DEFAULT << 2)
53 TAO_SCIOP_Transport* specific_transport = 0;
54 ACE_NEW (specific_transport,
55 TAO_SCIOP_Transport (this, orb_core));
57 // store this pointer (indirectly increment ref count)
58 this->transport (specific_transport);
61 TAO_SCIOP_Connection_Handler::~TAO_SCIOP_Connection_Handler ()
63 delete this->transport ();
64 int const result =
65 this->release_os_resources ();
67 if (result == -1 && TAO_debug_level)
69 TAOLIB_ERROR ((LM_ERROR,
70 ACE_TEXT("TAO (%P|%t) - SCIOP_Connection_Handler::")
71 ACE_TEXT("~scIOP_Connection_Handler, ")
72 ACE_TEXT("release_os_resources() failed %m\n")));
76 int
77 TAO_SCIOP_Connection_Handler::open_handler (void *v)
79 return this->open (v);
82 int
83 TAO_SCIOP_Connection_Handler::open (void*)
85 if (this->shared_open() == -1)
86 return -1;
88 TAO_SCIOP_Protocol_Properties protocol_properties;
90 // Initialize values from ORB params.
91 protocol_properties.send_buffer_size_ =
92 this->orb_core ()->orb_params ()->sock_sndbuf_size ();
93 protocol_properties.recv_buffer_size_ =
94 this->orb_core ()->orb_params ()->sock_rcvbuf_size ();
95 protocol_properties.no_delay_ =
96 this->orb_core ()->orb_params ()->nodelay ();
97 protocol_properties.hop_limit_ =
98 this->orb_core ()->orb_params ()->ip_hoplimit ();
100 TAO_Protocols_Hooks *tph = this->orb_core ()->get_protocols_hooks ();
102 if (tph != 0)
106 if (this->transport ()->opened_as () == TAO::TAO_CLIENT_ROLE)
108 tph->client_protocol_properties_at_orb_level (protocol_properties);
110 else
112 tph->server_protocol_properties_at_orb_level (protocol_properties);
115 catch (const ::CORBA::Exception& ex)
117 return -1;
121 if (this->set_socket_option (this->peer (),
122 protocol_properties.send_buffer_size_,
123 protocol_properties.recv_buffer_size_) == -1)
124 return -1;
126 #if !defined (ACE_LACKS_TCP_NODELAY)
127 if (this->peer ().set_option (IPPROTO_SCTP,
128 SCTP_NODELAY,
129 (void *) &protocol_properties.no_delay_,
130 sizeof (protocol_properties.no_delay_)) == -1)
131 return -1;
132 #endif /* ! ACE_LACKS_TCP_NODELAY */
134 if (protocol_properties.hop_limit_ >= 0)
136 int result = 0;
137 #if defined (ACE_HAS_IPV6)
138 ACE_INET_Addr local_addr;
139 if (this->peer ().get_local_addr (local_addr) == -1)
141 result = -1;
143 else if (local_addr.get_type () == AF_INET6)
145 #if defined (ACE_WIN32)
146 DWORD hop_limit =
147 static_cast<DWORD> (protocol_properties.hop_limit_);
148 #else
149 int hop_limit =
150 static_cast<int> (protocol_properties.hop_limit_);
151 #endif
152 result = this->peer ().set_option (
153 IPPROTO_IPV6,
154 IPV6_UNICAST_HOPS,
155 (void *) &hop_limit,
156 sizeof (hop_limit));
158 else
159 #endif /* ACE_HAS_IPV6 */
161 #if defined (ACE_WIN32)
162 DWORD hop_limit =
163 static_cast<DWORD> (protocol_properties.hop_limit_);
164 #else
165 int hop_limit =
166 static_cast<int> (protocol_properties.hop_limit_);
167 #endif
168 result = this->peer ().set_option (
169 IPPROTO_IP,
170 IP_TTL,
171 (void *) &hop_limit,
172 sizeof (hop_limit));
175 if (result != 0)
177 if (TAO_debug_level)
179 TAOLIB_ERROR ((LM_ERROR,
180 ACE_TEXT("TAO (%P|%t) - SCIOP_Connection_Handler::open, ")
181 ACE_TEXT("couldn't set hop limit\n\n")));
183 return -1;
187 if (this->transport ()->wait_strategy ()->non_blocking ())
189 if (this->peer ().enable (ACE_NONBLOCK) == -1)
190 return -1;
193 // Called by the <Strategy_Acceptor> when the handler is
194 // completely connected.
196 ACE_INET_Addr remote_addr;
197 if (this->peer ().get_remote_addr (remote_addr) == -1)
198 return -1;
200 ACE_INET_Addr local_addr;
201 if (this->peer ().get_local_addr (local_addr) == -1)
202 return -1;
204 if (TAO_debug_level > 2)
205 TAOLIB_DEBUG ((LM_DEBUG,
206 ACE_TEXT("TAO(%P|%t) - SCIOP_Connection_Handler::open, ")
207 ACE_TEXT("The local addr is (%C)\n"),
208 local_addr.get_host_addr ()));
210 if (local_addr.get_ip_address () == remote_addr.get_ip_address ()
211 && local_addr.get_port_number () == remote_addr.get_port_number ())
213 if (TAO_debug_level > 0)
215 ACE_TCHAR remote_as_string[MAXHOSTNAMELEN + 16];
216 ACE_TCHAR local_as_string[MAXHOSTNAMELEN + 16];
218 (void) remote_addr.addr_to_string (remote_as_string,
219 sizeof(remote_as_string));
220 (void) local_addr.addr_to_string (local_as_string,
221 sizeof(local_as_string));
222 TAOLIB_ERROR ((LM_ERROR,
223 ACE_TEXT("TAO(%P|%t) - TAO_SCIOP_Connection_Handler::open, ")
224 ACE_TEXT("Holy Cow! The remote addr and ")
225 ACE_TEXT("local addr are identical (%s == %s)\n"),
226 remote_as_string, local_as_string));
228 return -1;
231 if (TAO_debug_level > 0)
233 ACE_TCHAR client[MAXHOSTNAMELEN + 16];
235 // Verify that we can resolve the peer hostname.
236 if (remote_addr.addr_to_string (client, sizeof (client)) == -1)
237 return -1;
239 TAOLIB_DEBUG ((LM_DEBUG,
240 ACE_TEXT ("TAO (%P|%t) - SCIOP_Connection_Handler::open, SCIOP ")
241 ACE_TEXT ("connection to peer <%s> on [%d]\n"),
242 client, this->peer ().get_handle ()));
245 // Set that the transport is now connected, if fails we return -1
246 // Use C-style cast b/c otherwise we get warnings on lots of
247 // compilers
248 if (!this->transport ()->post_open ((size_t) this->get_handle ()))
249 return -1;
251 this->state_changed (TAO_LF_Event::LFS_SUCCESS,
252 this->orb_core ()->leader_follower ());
254 return 0;
258 TAO_SCIOP_Connection_Handler::resume_handler ()
260 return ACE_Event_Handler::ACE_APPLICATION_RESUMES_HANDLER;
264 TAO_SCIOP_Connection_Handler::close_connection ()
266 return this->close_connection_eh (this);
270 TAO_SCIOP_Connection_Handler::handle_input (ACE_HANDLE h)
272 return this->handle_input_eh (h, this);
276 TAO_SCIOP_Connection_Handler::handle_output (ACE_HANDLE handle)
278 int const result =
279 this->handle_output_eh (handle, this);
281 if (result == -1)
283 this->close_connection ();
284 return 0;
287 return result;
291 TAO_SCIOP_Connection_Handler::handle_timeout (const ACE_Time_Value &,
292 const void *)
294 // Using this to ensure this instance will be deleted (if necessary)
295 // only after reset_state(). Without this, when this refcount==1 -
296 // the call to close() will cause a call to remove_reference() which
297 // will delete this. At that point this->reset_state() is in no
298 // man's territory and that causes SEGV on some platforms (Windows!)
300 TAO_Auto_Reference<TAO_SCIOP_Connection_Handler> safeguard (*this);
302 // NOTE: Perhaps not the best solution, as it feels like the upper
303 // layers should be responsible for this?
305 // We don't use this upcall for I/O. This is only used by the
306 // Connector to indicate that the connection timedout. Therefore,
307 // we should call close().
308 int ret = this->close ();
309 this->reset_state (TAO_LF_Event::LFS_TIMEOUT);
310 return ret;
314 TAO_SCIOP_Connection_Handler::handle_close (ACE_HANDLE,
315 ACE_Reactor_Mask)
317 ACE_ASSERT (0);
318 return 0;
322 TAO_SCIOP_Connection_Handler::close (u_long flags)
324 return this->close_handler (flags);
328 TAO_SCIOP_Connection_Handler::release_os_resources ()
330 return this->peer().close ();
334 TAO_SCIOP_Connection_Handler::add_transport_to_cache ()
336 ACE_INET_Addr addr;
338 // Get the peername.
339 if (this->peer ().get_remote_addr (addr) == -1)
340 return -1;
342 // Construct an SCIOP_Endpoint object
343 TAO_SCIOP_Endpoint endpoint (
344 addr,
345 this->orb_core()->orb_params()->use_dotted_decimal_addresses ());
347 // Construct a property object
348 TAO_Base_Transport_Property prop (&endpoint);
350 TAO::Transport_Cache_Manager &cache =
351 this->orb_core ()->lane_resources ().transport_cache ();
353 // Idle the transport..
354 return cache.cache_transport (&prop, this->transport ());
358 TAO_SCIOP_Connection_Handler::process_listen_point_list (
359 IIOP::ListenPointList &listen_list)
361 // Get the size of the list
362 CORBA::ULong const len = listen_list.length ();
364 for (CORBA::ULong i = 0; i < len; ++ i)
366 IIOP::ListenPoint listen_point = listen_list[i];
367 ACE_INET_Addr addr (listen_point.port,
368 listen_point.host.in ());
370 if (TAO_debug_level > 0)
372 TAOLIB_DEBUG ((LM_DEBUG,
373 ACE_TEXT("(%P|%t) Listening port [%d] on [%C]\n"),
374 listen_point.port,
375 listen_point.host.in ()));
378 // Construct an SCIOP_Endpoint object
379 TAO_SCIOP_Endpoint endpoint (addr,
380 this->orb_core()->orb_params()->use_dotted_decimal_addresses ());
382 // Construct a property object
383 TAO_Base_Transport_Property prop (&endpoint);
385 // Mark the connection as bidirectional
386 prop.set_bidir_flag (1);
388 // The property for this handler has changed. Recache the
389 // handler with this property
390 int const retval = this->transport ()->recache_transport (&prop);
391 if (retval == -1)
392 return retval;
394 // Make the handler idle and ready for use
395 this->transport ()->make_idle ();
398 return 0;
402 TAO_SCIOP_Connection_Handler::set_tos (int tos)
404 if (tos != this->dscp_codepoint_)
406 int const result = this->peer ().set_option (IPPROTO_IP,
407 IP_TOS,
408 (int *) &tos ,
409 (int) sizeof (tos));
411 if (TAO_debug_level)
413 TAOLIB_DEBUG ((LM_DEBUG,
414 ACE_TEXT("TAO (%P|%t) - SCIOP_Connection_Handler::")
415 ACE_TEXT("set_dscp_codepoint -> dscp: %x; result: %d; %C\n"),
416 tos,
417 result,
418 result == -1 ? "try running as superuser" : ""));
421 // On successful setting of TOS field.
422 if (result == 0)
423 this->dscp_codepoint_ = tos;
426 return 0;
430 TAO_SCIOP_Connection_Handler::set_dscp_codepoint (CORBA::Long dscp)
432 int tos = IPDSFIELD_DSCP_DEFAULT << 2;
433 tos = (int)(dscp) << 2;
434 this->set_tos (tos);
435 return 0;
439 TAO_SCIOP_Connection_Handler::set_dscp_codepoint (CORBA::Boolean set_network_priority)
441 int tos = IPDSFIELD_DSCP_DEFAULT << 2;
443 if (set_network_priority)
445 TAO_Protocols_Hooks *tph = this->orb_core ()->get_protocols_hooks ();
447 if (tph != 0)
449 CORBA::Long codepoint = tph->get_dscp_codepoint ();
450 tos = (int)(codepoint) << 2;
451 this->set_tos (tos);
455 return 0;
459 TAO_SCIOP_Connection_Handler::handle_write_ready (const ACE_Time_Value *t)
461 return ACE::handle_write_ready (this->peer ().get_handle (), t);
464 TAO_END_VERSIONED_NAMESPACE_DECL
466 #endif /* TAO_HAS_SCIOP == 1 */