1 //=============================================================================
3 * @file Max_Default_Port_Test.cpp
5 * This is a test for ACE_MAX_DEFAULT_PORT value. The test tests the
6 * highest value of the port number at which an event handler can be
7 * registered and a Connector can be connected to.
9 * Some weird behaviour has been reported on Windows NT (sp 3) when
10 * the port number exceeds 65279 resulting ACE_MAX_DEFAULT_PORT to set
11 * to zero on that platform.
13 * In this test, the event handler is started at the port value
14 * USHRT_MAX and decremented for 'ports_to_test' port values and tested
15 * if the highest port number used agrees with ACE_MAX_DEFAULT_PORT value.
17 * @author Chanaka Liyanaarachchi <chanaka@ociweb.com>
19 //=============================================================================
22 #include "test_config.h"
23 #include "ace/Reactor.h"
24 #include "ace/SOCK_Connector.h"
25 #include "ace/Thread_Manager.h"
26 #include "ace/OS_NS_unistd.h"
27 #include "ace/Time_Value.h"
29 #include "Max_Default_Port_Test.h"
31 // implement a retry and recuperation mechanism for VxWorks because test will otherwise fail
32 // on a non-optimized kernel with several ETIME errors on the client connects.
33 #if defined (ACE_VXWORKS)
37 My_Accept_Handler::My_Accept_Handler (ACE_INET_Addr
&addr
)
40 if (addr
.get_port_number() != 0)
45 My_Accept_Handler::~My_Accept_Handler ()
47 this->peer_acceptor_
.close (); // Prevent handle leaks
52 My_Accept_Handler::open (ACE_INET_Addr
&addr
)
54 if (this->peer_acceptor_
.open (addr
, 1) == -1)
56 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"),
57 ACE_TEXT ("My_Accept_Handler open")));
65 My_Accept_Handler::get_handle () const
67 return this->peer_acceptor_
.get_handle ();
71 My_Accept_Handler::handle_input (ACE_HANDLE
)
73 if (this->peer_acceptor_
.accept(this->stream_
, 0) == -1) {
74 ACE_ERROR((LM_ERROR
, ACE_TEXT ("%p\n"),
75 ACE_TEXT ("peer_acceptor.accept")));
80 ACE_TEXT ("My_Accept_Handler::handle_input\n")));
82 // Close the opened stream, else it'll leak a handle. Don't close
83 // the acceptor here, though, because get_handle() needs it to
84 // correctly allow removal from the reactor later. It gets closed
86 this->stream_
.close ();
92 My_Accept_Handler::port ()
94 return this->addr_
.get_port_number();
97 long max_connected_port
= 0;
102 ACE_INET_Addr
*remote_addr
= reinterpret_cast<ACE_INET_Addr
*> (arg
);
104 ACE_INET_Addr
server_addr (remote_addr
->get_port_number (),
108 ACE_SOCK_Stream cli_stream
;
109 ACE_SOCK_Connector con
;
110 ACE_Time_Value
timeout (ACE_DEFAULT_TIMEOUT
);
112 ACE_DEBUG ((LM_DEBUG
,
113 ACE_TEXT ("(%P|%t) Connecting to port %d\n"),
114 server_addr
.get_port_number()));
116 // Initiate connection with server; don't wait forever
117 if (con
.connect (cli_stream
,
121 #if defined (ACE_VXWORKS)
124 if ( ++retry_port_
<6 )
126 ACE_DEBUG ((LM_DEBUG
,
127 ACE_TEXT ("(%P|%t) Going to retry port %d\n"),
128 server_addr
.get_port_number()));
135 ACE_ERROR ((LM_ERROR
,
136 ACE_TEXT ("(%P|%t) %p\n"),
137 ACE_TEXT ("connection failed")));
139 #if defined (ACE_VXWORKS)
145 #if defined (ACE_VXWORKS)
149 // if connect succesful, what is the max port number we connected
151 int connected_port
= server_addr
.get_port_number ();
153 if (connected_port
> max_connected_port
)
154 max_connected_port
= connected_port
;
162 run_main (int argc
, ACE_TCHAR
*argv
[])
164 ACE_START_TEST (ACE_TEXT ("Max_Default_Port_Test"));
166 ACE_UNUSED_ARG (argc
);
167 ACE_UNUSED_ARG (argv
);
169 #ifndef ACE_LACKS_ACCEPT
171 u_short max_listened_port
= 0;
173 #if defined (__Lynx__)
174 // LynxOS can handle only 80 test iterations.
175 // This needs to be investigated further -- olli 12.11.2007
176 const u_short ports_to_test
= 80;
178 const u_short ports_to_test
= 300;
181 //Ports beyond 65279 were said to bad on NT sp 3.
182 for (u_short idx
= USHRT_MAX
; idx
!= USHRT_MAX
- ports_to_test
; --idx
)
184 #if defined (ACE_VXWORKS)
188 ACE_OS::sleep (ACE_Time_Value (2*ACE_DEFAULT_TIMEOUT
));
192 ACE_INET_Addr
addr (idx
);
194 My_Accept_Handler
*eh
= new My_Accept_Handler (addr
);
197 if ( ACE_Reactor::instance()->register_handler (
199 ACE_Event_Handler::ACCEPT_MASK
) == -1)
201 ACE_ERROR_RETURN ((LM_ERROR
,
203 ACE_TEXT ("Failed to register event handler")),
207 ACE_DEBUG ((LM_DEBUG
, "Registered event handler at %d\n", idx
));
209 ACE_Time_Value
tv (1);
211 #if defined (ACE_HAS_THREADS)
213 if (ACE_Thread_Manager::instance ()->spawn_n
215 ACE_THR_FUNC (client
),
216 reinterpret_cast <void *> (&addr
),
217 THR_NEW_LWP
| THR_DETACHED
) == -1)
219 ACE_ERROR_RETURN ((LM_ERROR
,
220 ACE_TEXT ("(%P|%t) %p\n"),
221 ACE_TEXT ("thread create failed")),
224 ACE_Thread_Manager::instance ()->wait ();
227 ACE_UNUSED_ARG (client
);
228 ACE_ERROR ((LM_ERROR
,
229 ACE_TEXT ("(%P|%t) only one thread may be run in a process on this platform\n%a"),
231 #endif //ACE_HAS_THREADS
233 if (ACE_Reactor::instance()->handle_events (tv
) == -1 )
235 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
236 ACE_TEXT ("Reactor::handle_events")),
240 // see if I can register a reactor at this port.
241 if (eh
->port () == idx
)
243 if (idx
> max_listened_port
)
244 max_listened_port
= idx
;
248 ACE_ERROR_RETURN ((LM_ERROR
,
249 ACE_TEXT ("Test Fail, listening port %d\n"),
254 ACE_Reactor::instance()->remove_handler (
256 ACE_Event_Handler::ACCEPT_MASK
|
257 ACE_Event_Handler::DONT_CALL
);
261 ACE_DEBUG ((LM_DEBUG
,
262 "Value of ACE_MAX_DEFAULT_PORT %d\n",
263 ACE_MAX_DEFAULT_PORT
));
265 ACE_DEBUG ((LM_DEBUG
,
266 "Highest port value I can listen at %d\n",
269 ACE_DEBUG ((LM_DEBUG
,
270 "Highest port value I can connect to %d\n",
271 max_connected_port
));
273 if ((max_listened_port
== ACE_MAX_DEFAULT_PORT
) &&
274 (max_connected_port
== ACE_MAX_DEFAULT_PORT
))
276 ACE_DEBUG ((LM_DEBUG
,
277 ACE_TEXT ("Valid ACE_MAX_DEFAULT_PORT value: %d\n"),
282 ACE_ERROR ((LM_ERROR
,
283 ACE_TEXT ("Invalid ACE_MAX_DEFAULT_PORT ")
284 ACE_TEXT ("or %d port may be busy; got to %d\n"),
285 ACE_MAX_DEFAULT_PORT
, max_listened_port
));
288 #endif // ACE_LACKS_ACCEPT