ACE+TAO-7_0_8
[ACE_TAO.git] / ACE / tests / Max_Default_Port_Test_IPV6.cpp
blob22ae15ea6eeda0a01d2a5e5e6fe44ce1c52cf620
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)
47 if (this->peer_acceptor_.open (addr, 1) == -1)
49 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
50 ACE_TEXT ("My_Accept_Handler open")));
51 ACE_OS::exit (1);
54 return 0;
58 ACE_HANDLE
59 My_Accept_Handler::get_handle () const
61 return this->peer_acceptor_.get_handle ();
64 int
65 My_Accept_Handler::handle_input (ACE_HANDLE)
68 if (this->peer_acceptor_.accept(this->stream_, 0) == -1) {
69 ACE_ERROR((LM_ERROR, ACE_TEXT ("%p\n"),
70 ACE_TEXT ("peer_acceptor.accept")));
71 ACE_OS::exit(1);
74 ACE_DEBUG ((LM_DEBUG,
75 ACE_TEXT ("My_Accept_Handler::handle_input\n")));
77 // Close the opened stream, else it'll leak a handle. Don't close
78 // the acceptor here, though, because get_handle() needs it to
79 // correctly allow removal from the reactor later. It gets closed
80 // in the destructor.
81 this->stream_.close ();
83 return 0;
86 u_short
87 My_Accept_Handler::port ()
89 return this->addr_.get_port_number();
92 long max_connected_port = 0;
94 #if defined (ACE_HAS_IPV6)
95 static void *
96 client (void *arg)
98 ACE_INET_Addr *remote_addr = reinterpret_cast<ACE_INET_Addr *> (arg);
100 ACE_INET_Addr server_addr (remote_addr->get_port_number (),
101 "::1");
103 ACE_SOCK_Stream cli_stream;
105 ACE_SOCK_Connector con;
106 ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
108 ACE_DEBUG ((LM_DEBUG,
109 ACE_TEXT ("(%P|%t) Connecting to port %d\n"),
110 server_addr.get_port_number()));
112 // Initiate connection with server; don't wait forever
113 if (con.connect (cli_stream,
114 server_addr,
115 &timeout) == -1)
117 ACE_ERROR ((LM_ERROR,
118 ACE_TEXT ("(%P|%t) %p\n"),
119 ACE_TEXT ("connection failed")));
121 return 0;
124 // if connect succesful, what is the max port number we connected
125 // up to now.
126 int connected_port = server_addr.get_port_number ();
128 if (connected_port > max_connected_port)
129 max_connected_port = connected_port;
131 cli_stream.close ();
133 return 0;
135 #endif /*ACE_HAS_IPV6*/
138 run_main (int argc, ACE_TCHAR *argv[])
140 ACE_START_TEST (ACE_TEXT ("Max_Default_Port_Test_IPV6"));
142 ACE_UNUSED_ARG (argc);
143 ACE_UNUSED_ARG (argv);
145 #if defined (ACE_HAS_IPV6)
146 u_short max_listened_port = 0;
148 //Ports beyond 65279 were said to bad on NT sp 3.
149 for (u_short idx = USHRT_MAX; idx != USHRT_MAX - 300; --idx)
151 ACE_INET_Addr addr (idx, "::");
153 My_Accept_Handler *eh = new My_Accept_Handler (addr);
156 if ( ACE_Reactor::instance()->register_handler (
158 ACE_Event_Handler::ACCEPT_MASK) == -1)
160 ACE_ERROR_RETURN ((LM_ERROR,
161 ACE_TEXT ("%p\n"),
162 ACE_TEXT ("Failed to register event handler")),
166 ACE_DEBUG ((LM_DEBUG, "Registered event handler at %d\n", idx));
168 ACE_Time_Value tv (1);
170 #if defined (ACE_HAS_THREADS)
172 if (ACE_Thread_Manager::instance ()->spawn_n
174 ACE_THR_FUNC (client),
175 reinterpret_cast<void *> (&addr),
176 THR_NEW_LWP | THR_DETACHED) == -1)
178 ACE_ERROR_RETURN ((LM_ERROR,
179 ACE_TEXT ("(%P|%t) %p\n"),
180 ACE_TEXT ("thread create failed")),
183 ACE_Thread_Manager::instance ()->wait ();
185 #else
186 ACE_UNUSED_ARG (client);
187 ACE_ERROR ((LM_ERROR,
188 ACE_TEXT ("(%P|%t) only one thread may be run in a process on this platform\n%a"),
189 1));
190 #endif //ACE_HAS_THREADS
192 if (ACE_Reactor::instance()->handle_events (tv) == -1 )
194 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
195 ACE_TEXT ("Reactor::handle_events")),
199 // see if I can register a reactor at this port.
200 if (eh->port () == idx)
202 if (idx > max_listened_port)
203 max_listened_port = idx;
205 else
207 ACE_ERROR_RETURN ((LM_ERROR,
208 ACE_TEXT ("Test Fail, listening port %d\n"),
209 eh->port()),
213 ACE_Reactor::instance()->remove_handler (
215 ACE_Event_Handler::ACCEPT_MASK |
216 ACE_Event_Handler::DONT_CALL);
217 delete eh;
220 ACE_DEBUG ((LM_DEBUG,
221 "Value of ACE_MAX_DEFAULT_PORT %d\n",
222 ACE_MAX_DEFAULT_PORT));
224 ACE_DEBUG ((LM_DEBUG,
225 "Highest port value I can listen at %d\n",
226 max_listened_port));
228 ACE_DEBUG ((LM_DEBUG,
229 "Highest port value I can connect to %d\n",
230 max_connected_port));
232 if ((max_listened_port == ACE_MAX_DEFAULT_PORT) &&
233 (max_connected_port == ACE_MAX_DEFAULT_PORT))
235 ACE_DEBUG ((LM_DEBUG,
236 ACE_TEXT ("Valid ACE_MAX_DEFAULT_PORT value: %d\n"),
237 max_listened_port));
239 else
241 ACE_ERROR ((LM_ERROR,
242 ACE_TEXT ("Invalid ACE_MAX_DEFAULT_PORT ")
243 ACE_TEXT ("or %d port may be busy; got to %d\n"),
244 ACE_MAX_DEFAULT_PORT, max_listened_port));
248 #endif /* ACE_HAS_IPV6 */
250 ACE_END_TEST;
251 return 0;