1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * This test is the same as acceptread.c except that it uses the
|
---|
40 | * emulated acceptread method instead of the regular acceptread.
|
---|
41 | */
|
---|
42 |
|
---|
43 | #include <prio.h>
|
---|
44 | #include <prprf.h>
|
---|
45 | #include <prinit.h>
|
---|
46 | #include <prnetdb.h>
|
---|
47 | #include <prinrval.h>
|
---|
48 | #include <prthread.h>
|
---|
49 | #include <pprio.h>
|
---|
50 |
|
---|
51 | #include <plerror.h>
|
---|
52 |
|
---|
53 | #include <stdlib.h>
|
---|
54 |
|
---|
55 | #define DEFAULT_PORT 12273
|
---|
56 | #define GET "GET / HTTP/1.0\n\n"
|
---|
57 | static PRFileDesc *std_out, *err_out;
|
---|
58 | static PRIntervalTime write_dally, accept_timeout;
|
---|
59 | static PRDescIdentity emu_layer_ident;
|
---|
60 | static PRIOMethods emu_layer_methods;
|
---|
61 |
|
---|
62 | /* the acceptread method in emu_layer_methods */
|
---|
63 | static PRInt32 PR_CALLBACK emu_AcceptRead(PRFileDesc *sd, PRFileDesc **nd,
|
---|
64 | PRNetAddr **raddr, void *buf, PRInt32 amount, PRIntervalTime timeout)
|
---|
65 | {
|
---|
66 | return PR_EmulateAcceptRead(sd, nd, raddr, buf, amount, timeout);
|
---|
67 | }
|
---|
68 |
|
---|
69 | static PRStatus PrintAddress(const PRNetAddr* address)
|
---|
70 | {
|
---|
71 | char buffer[100];
|
---|
72 | PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
|
---|
73 | if (PR_FAILURE == rv) PL_FPrintError(err_out, "PR_NetAddrToString");
|
---|
74 | else PR_fprintf(
|
---|
75 | std_out, "Accepted connection from (0x%p)%s:%d\n",
|
---|
76 | address, buffer, address->inet.port);
|
---|
77 | return rv;
|
---|
78 | } /* PrintAddress */
|
---|
79 |
|
---|
80 | static void ConnectingThread(void *arg)
|
---|
81 | {
|
---|
82 | PRInt32 nbytes;
|
---|
83 | char buf[1024];
|
---|
84 | PRFileDesc *sock;
|
---|
85 | PRNetAddr peer_addr, *addr;
|
---|
86 |
|
---|
87 | addr = (PRNetAddr*)arg;
|
---|
88 |
|
---|
89 | sock = PR_NewTCPSocket();
|
---|
90 | if (sock == NULL)
|
---|
91 | {
|
---|
92 | PL_FPrintError(err_out, "PR_NewTCPSocket (client) failed");
|
---|
93 | PR_ProcessExit(1);
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (PR_Connect(sock, addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE)
|
---|
97 | {
|
---|
98 | PL_FPrintError(err_out, "PR_Connect (client) failed");
|
---|
99 | PR_ProcessExit(1);
|
---|
100 | }
|
---|
101 | if (PR_GetPeerName(sock, &peer_addr) == PR_FAILURE)
|
---|
102 | {
|
---|
103 | PL_FPrintError(err_out, "PR_GetPeerName (client) failed");
|
---|
104 | PR_ProcessExit(1);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /*
|
---|
108 | ** Then wait between the connection coming up and sending the expected
|
---|
109 | ** data. At some point in time, the server should fail due to a timeou
|
---|
110 | ** on the AcceptRead() operation, which according to the document is
|
---|
111 | ** only due to the read() portion.
|
---|
112 | */
|
---|
113 | PR_Sleep(write_dally);
|
---|
114 |
|
---|
115 | nbytes = PR_Send(sock, GET, sizeof(GET), 0, PR_INTERVAL_NO_TIMEOUT);
|
---|
116 | if (nbytes == -1) PL_FPrintError(err_out, "PR_Send (client) failed");
|
---|
117 |
|
---|
118 | nbytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
|
---|
119 | if (nbytes == -1) PL_FPrintError(err_out, "PR_Recv (client) failed");
|
---|
120 | else
|
---|
121 | {
|
---|
122 | PR_fprintf(std_out, "PR_Recv (client) succeeded: %d bytes\n", nbytes);
|
---|
123 | buf[sizeof(buf) - 1] = '\0';
|
---|
124 | PR_fprintf(std_out, "%s\n", buf);
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (PR_FAILURE == PR_Shutdown(sock, PR_SHUTDOWN_BOTH))
|
---|
128 | PL_FPrintError(err_out, "PR_Shutdown (client) failed");
|
---|
129 |
|
---|
130 | if (PR_FAILURE == PR_Close(sock))
|
---|
131 | PL_FPrintError(err_out, "PR_Close (client) failed");
|
---|
132 |
|
---|
133 | return;
|
---|
134 | } /* ConnectingThread */
|
---|
135 |
|
---|
136 | #define BUF_SIZE 117
|
---|
137 | static void AcceptingThread(void *arg)
|
---|
138 | {
|
---|
139 | PRStatus rv;
|
---|
140 | PRInt32 bytes;
|
---|
141 | PRSize buf_size = BUF_SIZE;
|
---|
142 | PRUint8 buf[BUF_SIZE + (2 * sizeof(PRNetAddr)) + 32];
|
---|
143 | PRNetAddr *accept_addr, *listen_addr = (PRNetAddr*)arg;
|
---|
144 | PRFileDesc *accept_sock, *listen_sock = PR_NewTCPSocket();
|
---|
145 | PRFileDesc *layer;
|
---|
146 | PRSocketOptionData sock_opt;
|
---|
147 |
|
---|
148 | if (NULL == listen_sock)
|
---|
149 | {
|
---|
150 | PL_FPrintError(err_out, "PR_NewTCPSocket (server) failed");
|
---|
151 | PR_ProcessExit(1);
|
---|
152 | }
|
---|
153 | layer = PR_CreateIOLayerStub(emu_layer_ident, &emu_layer_methods);
|
---|
154 | if (NULL == layer)
|
---|
155 | {
|
---|
156 | PL_FPrintError(err_out, "PR_CreateIOLayerStub (server) failed");
|
---|
157 | PR_ProcessExit(1);
|
---|
158 | }
|
---|
159 | if (PR_PushIOLayer(listen_sock, PR_TOP_IO_LAYER, layer) == PR_FAILURE)
|
---|
160 | {
|
---|
161 | PL_FPrintError(err_out, "PR_PushIOLayer (server) failed");
|
---|
162 | PR_ProcessExit(1);
|
---|
163 | }
|
---|
164 | sock_opt.option = PR_SockOpt_Reuseaddr;
|
---|
165 | sock_opt.value.reuse_addr = PR_TRUE;
|
---|
166 | rv = PR_SetSocketOption(listen_sock, &sock_opt);
|
---|
167 | if (PR_FAILURE == rv)
|
---|
168 | {
|
---|
169 | PL_FPrintError(err_out, "PR_SetSocketOption (server) failed");
|
---|
170 | PR_ProcessExit(1);
|
---|
171 | }
|
---|
172 | rv = PR_Bind(listen_sock, listen_addr);
|
---|
173 | if (PR_FAILURE == rv)
|
---|
174 | {
|
---|
175 | PL_FPrintError(err_out, "PR_Bind (server) failed");
|
---|
176 | PR_ProcessExit(1);
|
---|
177 | }
|
---|
178 | rv = PR_Listen(listen_sock, 10);
|
---|
179 | if (PR_FAILURE == rv)
|
---|
180 | {
|
---|
181 | PL_FPrintError(err_out, "PR_Listen (server) failed");
|
---|
182 | PR_ProcessExit(1);
|
---|
183 | }
|
---|
184 | bytes = PR_AcceptRead(
|
---|
185 | listen_sock, &accept_sock, &accept_addr, buf, buf_size, accept_timeout);
|
---|
186 |
|
---|
187 | if (-1 == bytes) PL_FPrintError(err_out, "PR_AcceptRead (server) failed");
|
---|
188 | else
|
---|
189 | {
|
---|
190 | PrintAddress(accept_addr);
|
---|
191 | PR_fprintf(
|
---|
192 | std_out, "(Server) read [0x%p..0x%p) %s\n",
|
---|
193 | buf, &buf[BUF_SIZE], buf);
|
---|
194 | bytes = PR_Write(accept_sock, buf, bytes);
|
---|
195 | rv = PR_Shutdown(accept_sock, PR_SHUTDOWN_BOTH);
|
---|
196 | if (PR_FAILURE == rv)
|
---|
197 | PL_FPrintError(err_out, "PR_Shutdown (server) failed");
|
---|
198 | }
|
---|
199 |
|
---|
200 | if (-1 != bytes)
|
---|
201 | {
|
---|
202 | rv = PR_Close(accept_sock);
|
---|
203 | if (PR_FAILURE == rv)
|
---|
204 | PL_FPrintError(err_out, "PR_Close (server) failed");
|
---|
205 | }
|
---|
206 |
|
---|
207 | rv = PR_Close(listen_sock);
|
---|
208 | if (PR_FAILURE == rv)
|
---|
209 | PL_FPrintError(err_out, "PR_Close (server) failed");
|
---|
210 | } /* AcceptingThread */
|
---|
211 |
|
---|
212 | PRIntn main(PRIntn argc, char **argv)
|
---|
213 | {
|
---|
214 | PRHostEnt he;
|
---|
215 | PRStatus status;
|
---|
216 | PRIntn next_index;
|
---|
217 | PRUint16 port_number;
|
---|
218 | char netdb_buf[PR_NETDB_BUF_SIZE];
|
---|
219 | PRNetAddr client_addr, server_addr;
|
---|
220 | PRThread *client_thread, *server_thread;
|
---|
221 | PRIntervalTime delta = PR_MillisecondsToInterval(500);
|
---|
222 |
|
---|
223 | err_out = PR_STDERR;
|
---|
224 | std_out = PR_STDOUT;
|
---|
225 | accept_timeout = PR_SecondsToInterval(2);
|
---|
226 | emu_layer_ident = PR_GetUniqueIdentity("Emulated AcceptRead");
|
---|
227 | emu_layer_methods = *PR_GetDefaultIOMethods();
|
---|
228 | emu_layer_methods.acceptread = emu_AcceptRead;
|
---|
229 |
|
---|
230 | if (argc != 2 && argc != 3) port_number = DEFAULT_PORT;
|
---|
231 | else port_number = (PRUint16)atoi(argv[(argc == 2) ? 1 : 2]);
|
---|
232 |
|
---|
233 | status = PR_InitializeNetAddr(PR_IpAddrAny, port_number, &server_addr);
|
---|
234 | if (PR_SUCCESS != status)
|
---|
235 | {
|
---|
236 | PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
|
---|
237 | PR_ProcessExit(1);
|
---|
238 | }
|
---|
239 | if (argc < 3)
|
---|
240 | {
|
---|
241 | status = PR_InitializeNetAddr(
|
---|
242 | PR_IpAddrLoopback, port_number, &client_addr);
|
---|
243 | if (PR_SUCCESS != status)
|
---|
244 | {
|
---|
245 | PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
|
---|
246 | PR_ProcessExit(1);
|
---|
247 | }
|
---|
248 | }
|
---|
249 | else
|
---|
250 | {
|
---|
251 | status = PR_GetHostByName(
|
---|
252 | argv[1], netdb_buf, sizeof(netdb_buf), &he);
|
---|
253 | if (status == PR_FAILURE)
|
---|
254 | {
|
---|
255 | PL_FPrintError(err_out, "PR_GetHostByName failed");
|
---|
256 | PR_ProcessExit(1);
|
---|
257 | }
|
---|
258 | next_index = PR_EnumerateHostEnt(0, &he, port_number, &client_addr);
|
---|
259 | if (next_index == -1)
|
---|
260 | {
|
---|
261 | PL_FPrintError(err_out, "PR_EnumerateHostEnt failed");
|
---|
262 | PR_ProcessExit(1);
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | for (
|
---|
267 | write_dally = 0;
|
---|
268 | write_dally < accept_timeout + (2 * delta);
|
---|
269 | write_dally += delta)
|
---|
270 | {
|
---|
271 | PR_fprintf(
|
---|
272 | std_out, "Testing w/ write_dally = %d msec\n",
|
---|
273 | PR_IntervalToMilliseconds(write_dally));
|
---|
274 | server_thread = PR_CreateThread(
|
---|
275 | PR_USER_THREAD, AcceptingThread, &server_addr,
|
---|
276 | PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
277 | if (server_thread == NULL)
|
---|
278 | {
|
---|
279 | PL_FPrintError(err_out, "PR_CreateThread (server) failed");
|
---|
280 | PR_ProcessExit(1);
|
---|
281 | }
|
---|
282 |
|
---|
283 | PR_Sleep(delta); /* let the server pot thicken */
|
---|
284 |
|
---|
285 | client_thread = PR_CreateThread(
|
---|
286 | PR_USER_THREAD, ConnectingThread, &client_addr,
|
---|
287 | PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
288 | if (client_thread == NULL)
|
---|
289 | {
|
---|
290 | PL_FPrintError(err_out, "PR_CreateThread (client) failed");
|
---|
291 | PR_ProcessExit(1);
|
---|
292 | }
|
---|
293 |
|
---|
294 | if (PR_JoinThread(client_thread) == PR_FAILURE)
|
---|
295 | PL_FPrintError(err_out, "PR_JoinThread (client) failed");
|
---|
296 |
|
---|
297 | if (PR_JoinThread(server_thread) == PR_FAILURE)
|
---|
298 | PL_FPrintError(err_out, "PR_JoinThread (server) failed");
|
---|
299 | }
|
---|
300 |
|
---|
301 | return 0;
|
---|
302 | }
|
---|