Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / tests / Max_Default_Port_Test_IPV6.cpp
blobfe0df33bcf707f4ff8f6b6c21397a7413c563ca5
1 // ============================================================================
2 /**
3 * @file Max_Default_Port_Test_IPV6.cpp
5 * @brief This is a test for ACE_MAX_DEFAULT_PORT value.
7 * The test tests the highest value of the port number at which an
8 * event handler can be registered and a Connector can be connected
9 * to.
11 * Some weird behaviour has been reported on Windows NT (sp 3) when
12 * the port number exceeds 65279 resulting ACE_MAX_DEFAULT_PORT to set
13 * to zero on that platform.
15 * In this test, the event handler is started at the port value
16 * USHRT_MAX and decremented for 300 port values and tested if the
17 * highest port number used agrees with ACE_MAX_DEFAULT_PORT value.
19 * @author Chanaka Liyanaarachchi <chanaka@ociweb.com>
20 * Brian Buesker <bbuesker@qualcomm.com>
22 // ============================================================================
24 #include "test_config.h"
25 #include "ace/Reactor.h"
26 #include "ace/SOCK_Connector.h"
27 #include "ace/Thread_Manager.h"
29 #include "Max_Default_Port_Test.h"
31 My_Accept_Handler::My_Accept_Handler (ACE_INET_Addr &addr)
32 : addr_ (addr)
34 if (addr.get_port_number() != 0)
35 this->open (addr);
38 My_Accept_Handler::~My_Accept_Handler ()
40 this->peer_acceptor_.close (); // Prevent handle leaks
43 int
44 My_Accept_Handler::open (ACE_INET_Addr &addr)
46 if (this->peer_acceptor_.open (addr, 1) == -1)
48 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
49 ACE_TEXT ("My_Accept_Handler open")));
50 ACE_OS::exit (1);
53 return 0;
56 ACE_HANDLE
57 My_Accept_Handler::get_handle () const
59 return this->peer_acceptor_.get_handle ();
62 int
63 My_Accept_Handler::handle_input (ACE_HANDLE)
65 if (this->peer_acceptor_.accept(this->stream_, 0) == -1) {
66 ACE_ERROR((LM_ERROR, ACE_TEXT ("%p\n"),
67 ACE_TEXT ("peer_acceptor.accept")));
68 ACE_OS::exit(1);
71 ACE_DEBUG ((LM_DEBUG,
72 ACE_TEXT ("My_Accept_Handler::handle_input\n")));
74 // Close the opened stream, else it'll leak a handle. Don't close
75 // the acceptor here, though, because get_handle() needs it to
76 // correctly allow removal from the reactor later. It gets closed
77 // in the destructor.
78 this->stream_.close ();
80 return 0;
83 u_short
84 My_Accept_Handler::port ()
86 return this->addr_.get_port_number();
89 long max_connected_port = 0;
91 #if defined (ACE_HAS_IPV6)
92 static void *
93 client (void *arg)
95 ACE_INET_Addr *remote_addr = reinterpret_cast<ACE_INET_Addr *> (arg);
97 ACE_INET_Addr server_addr (remote_addr->get_port_number (),
98 "::1");
100 ACE_SOCK_Stream cli_stream;
102 ACE_SOCK_Connector con;
103 ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
105 ACE_DEBUG ((LM_DEBUG,
106 ACE_TEXT ("(%P|%t) Connecting to port %d\n"),
107 server_addr.get_port_number()));
109 // Initiate connection with server; don't wait forever
110 if (con.connect (cli_stream,
111 server_addr,
112 &timeout) == -1)
114 ACE_ERROR ((LM_ERROR,
115 ACE_TEXT ("(%P|%t) %p\n"),
116 ACE_TEXT ("connection failed")));
118 return 0;
121 // if connect succesful, what is the max port number we connected
122 // up to now.
123 int connected_port = server_addr.get_port_number ();
125 if (connected_port > max_connected_port)
126 max_connected_port = connected_port;
128 cli_stream.close ();
130 return 0;
132 #endif /*ACE_HAS_IPV6*/
135 run_main (int argc, ACE_TCHAR *argv[])
137 ACE_START_TEST (ACE_TEXT ("Max_Default_Port_Test_IPV6"));
139 ACE_UNUSED_ARG (argc);
140 ACE_UNUSED_ARG (argv);
142 #if defined (ACE_HAS_IPV6)
143 u_short max_listened_port = 0;
145 //Ports beyond 65279 were said to bad on NT sp 3.
146 for (u_short idx = USHRT_MAX; idx != USHRT_MAX - 300; --idx)
148 ACE_INET_Addr addr (idx, "::");
150 My_Accept_Handler *eh = new My_Accept_Handler (addr);
153 if ( ACE_Reactor::instance()->register_handler (
155 ACE_Event_Handler::ACCEPT_MASK) == -1)
157 ACE_ERROR_RETURN ((LM_ERROR,
158 ACE_TEXT ("%p\n"),
159 ACE_TEXT ("Failed to register event handler")),
163 ACE_DEBUG ((LM_DEBUG, "Registered event handler at %d\n", idx));
165 ACE_Time_Value tv (1);
167 #if defined (ACE_HAS_THREADS)
169 if (ACE_Thread_Manager::instance ()->spawn_n
171 ACE_THR_FUNC (client),
172 reinterpret_cast<void *> (&addr),
173 THR_NEW_LWP | THR_DETACHED) == -1)
175 ACE_ERROR_RETURN ((LM_ERROR,
176 ACE_TEXT ("(%P|%t) %p\n"),
177 ACE_TEXT ("thread create failed")),
180 ACE_Thread_Manager::instance ()->wait ();
182 #else
183 ACE_UNUSED_ARG (client);
184 ACE_ERROR ((LM_ERROR,
185 ACE_TEXT ("(%P|%t) only one thread may be run in a process on this platform\n%a"),
186 1));
187 #endif //ACE_HAS_THREADS
189 if (ACE_Reactor::instance()->handle_events (tv) == -1 )
191 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
192 ACE_TEXT ("Reactor::handle_events")),
196 // see if I can register a reactor at this port.
197 if (eh->port () == idx)
199 if (idx > max_listened_port)
200 max_listened_port = idx;
202 else
204 ACE_ERROR_RETURN ((LM_ERROR,
205 ACE_TEXT ("Test Fail, listening port %d\n"),
206 eh->port()),
210 ACE_Reactor::instance()->remove_handler (
212 ACE_Event_Handler::ACCEPT_MASK |
213 ACE_Event_Handler::DONT_CALL);
214 delete eh;
217 ACE_DEBUG ((LM_DEBUG,
218 "Value of ACE_MAX_DEFAULT_PORT %d\n",
219 ACE_MAX_DEFAULT_PORT));
221 ACE_DEBUG ((LM_DEBUG,
222 "Highest port value I can listen at %d\n",
223 max_listened_port));
225 ACE_DEBUG ((LM_DEBUG,
226 "Highest port value I can connect to %d\n",
227 max_connected_port));
229 if ((max_listened_port == ACE_MAX_DEFAULT_PORT) &&
230 (max_connected_port == ACE_MAX_DEFAULT_PORT))
232 ACE_DEBUG ((LM_DEBUG,
233 ACE_TEXT ("Valid ACE_MAX_DEFAULT_PORT value: %d\n"),
234 max_listened_port));
236 else
238 ACE_ERROR ((LM_ERROR,
239 ACE_TEXT ("Invalid ACE_MAX_DEFAULT_PORT ")
240 ACE_TEXT ("or %d port may be busy; got to %d\n"),
241 ACE_MAX_DEFAULT_PORT, max_listened_port));
244 #endif /* ACE_HAS_IPV6 */
246 ACE_END_TEST;
247 return 0;