Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Max_Default_Port_Test_IPV6.cpp
blob1be7e0e8ab996a32bf56ebdb6cc269c7b27c36b8
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);
39 My_Accept_Handler::~My_Accept_Handler ()
41 this->peer_acceptor_.close (); // Prevent handle leaks
45 int
46 My_Accept_Handler::open (ACE_INET_Addr &addr)
49 if (this->peer_acceptor_.open (addr, 1) == -1)
51 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
52 ACE_TEXT ("My_Accept_Handler open")));
53 ACE_OS::exit (1);
56 return 0;
60 ACE_HANDLE
61 My_Accept_Handler::get_handle () const
63 return this->peer_acceptor_.get_handle ();
66 int
67 My_Accept_Handler::handle_input (ACE_HANDLE)
70 if (this->peer_acceptor_.accept(this->stream_, 0) == -1) {
71 ACE_ERROR((LM_ERROR, ACE_TEXT ("%p\n"),
72 ACE_TEXT ("peer_acceptor.accept")));
73 ACE_OS::exit(1);
76 ACE_DEBUG ((LM_DEBUG,
77 ACE_TEXT ("My_Accept_Handler::handle_input\n")));
79 // Close the opened stream, else it'll leak a handle. Don't close
80 // the acceptor here, though, because get_handle() needs it to
81 // correctly allow removal from the reactor later. It gets closed
82 // in the destructor.
83 this->stream_.close ();
85 return 0;
88 u_short
89 My_Accept_Handler::port ()
91 return this->addr_.get_port_number();
94 long max_connected_port = 0;
96 #if defined (ACE_HAS_IPV6)
97 static void *
98 client (void *arg)
100 ACE_INET_Addr *remote_addr = reinterpret_cast<ACE_INET_Addr *> (arg);
102 ACE_INET_Addr server_addr (remote_addr->get_port_number (),
103 "::1");
105 ACE_SOCK_Stream cli_stream;
107 ACE_SOCK_Connector con;
108 ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
110 ACE_DEBUG ((LM_DEBUG,
111 ACE_TEXT ("(%P|%t) Connecting to port %d\n"),
112 server_addr.get_port_number()));
114 // Initiate connection with server; don't wait forever
115 if (con.connect (cli_stream,
116 server_addr,
117 &timeout) == -1)
119 ACE_ERROR ((LM_ERROR,
120 ACE_TEXT ("(%P|%t) %p\n"),
121 ACE_TEXT ("connection failed")));
123 return 0;
126 // if connect succesful, what is the max port number we connected
127 // up to now.
128 int connected_port = server_addr.get_port_number ();
130 if (connected_port > max_connected_port)
131 max_connected_port = connected_port;
133 cli_stream.close ();
135 return 0;
137 #endif /*ACE_HAS_IPV6*/
140 run_main (int argc, ACE_TCHAR *argv[])
142 ACE_START_TEST (ACE_TEXT ("Max_Default_Port_Test_IPV6"));
144 ACE_UNUSED_ARG (argc);
145 ACE_UNUSED_ARG (argv);
147 #if defined (ACE_HAS_IPV6)
148 u_short max_listened_port = 0;
150 //Ports beyond 65279 were said to bad on NT sp 3.
151 for (u_short idx = USHRT_MAX; idx != USHRT_MAX - 300; --idx)
153 ACE_INET_Addr addr (idx, "::");
155 My_Accept_Handler *eh = new My_Accept_Handler (addr);
158 if ( ACE_Reactor::instance()->register_handler (
160 ACE_Event_Handler::ACCEPT_MASK) == -1)
162 ACE_ERROR_RETURN ((LM_ERROR,
163 ACE_TEXT ("%p\n"),
164 ACE_TEXT ("Failed to register event handler")),
168 ACE_DEBUG ((LM_DEBUG, "Registered event handler at %d\n", idx));
170 ACE_Time_Value tv (1);
172 #if defined (ACE_HAS_THREADS)
174 if (ACE_Thread_Manager::instance ()->spawn_n
176 ACE_THR_FUNC (client),
177 reinterpret_cast<void *> (&addr),
178 THR_NEW_LWP | THR_DETACHED) == -1)
180 ACE_ERROR_RETURN ((LM_ERROR,
181 ACE_TEXT ("(%P|%t) %p\n"),
182 ACE_TEXT ("thread create failed")),
185 ACE_Thread_Manager::instance ()->wait ();
187 #else
188 ACE_UNUSED_ARG (client);
189 ACE_ERROR ((LM_ERROR,
190 ACE_TEXT ("(%P|%t) only one thread may be run in a process on this platform\n%a"),
191 1));
192 #endif //ACE_HAS_THREADS
194 if (ACE_Reactor::instance()->handle_events (tv) == -1 )
196 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
197 ACE_TEXT ("Reactor::handle_events")),
201 // see if I can register a reactor at this port.
202 if (eh->port () == idx)
204 if (idx > max_listened_port)
205 max_listened_port = idx;
207 else
209 ACE_ERROR_RETURN ((LM_ERROR,
210 ACE_TEXT ("Test Fail, listening port %d\n"),
211 eh->port()),
215 ACE_Reactor::instance()->remove_handler (
217 ACE_Event_Handler::ACCEPT_MASK |
218 ACE_Event_Handler::DONT_CALL);
219 delete eh;
222 ACE_DEBUG ((LM_DEBUG,
223 "Value of ACE_MAX_DEFAULT_PORT %d\n",
224 ACE_MAX_DEFAULT_PORT));
226 ACE_DEBUG ((LM_DEBUG,
227 "Highest port value I can listen at %d\n",
228 max_listened_port));
230 ACE_DEBUG ((LM_DEBUG,
231 "Highest port value I can connect to %d\n",
232 max_connected_port));
234 if ((max_listened_port == ACE_MAX_DEFAULT_PORT) &&
235 (max_connected_port == ACE_MAX_DEFAULT_PORT))
237 ACE_DEBUG ((LM_DEBUG,
238 ACE_TEXT ("Valid ACE_MAX_DEFAULT_PORT value: %d\n"),
239 max_listened_port));
241 else
243 ACE_ERROR ((LM_ERROR,
244 ACE_TEXT ("Invalid ACE_MAX_DEFAULT_PORT ")
245 ACE_TEXT ("or %d port may be busy; got to %d\n"),
246 ACE_MAX_DEFAULT_PORT, max_listened_port));
250 #endif /* ACE_HAS_IPV6 */
252 ACE_END_TEST;
253 return 0;