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 | #include <prio.h>
|
---|
39 | #include <prprf.h>
|
---|
40 | #include <prinit.h>
|
---|
41 | #include <prnetdb.h>
|
---|
42 | #include <prinrval.h>
|
---|
43 | #include <prthread.h>
|
---|
44 |
|
---|
45 | #include <plerror.h>
|
---|
46 |
|
---|
47 | #include <stdlib.h>
|
---|
48 |
|
---|
49 | #define DEFAULT_PORT 12273
|
---|
50 | #define GET "GET / HTTP/1.0\n\n"
|
---|
51 | static PRFileDesc *std_out, *err_out;
|
---|
52 | static PRIntervalTime write_dally, accept_timeout;
|
---|
53 |
|
---|
54 | static PRStatus PrintAddress(const PRNetAddr* address)
|
---|
55 | {
|
---|
56 | char buffer[100];
|
---|
57 | PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
|
---|
58 | if (PR_FAILURE == rv) PL_FPrintError(err_out, "PR_NetAddrToString");
|
---|
59 | else PR_fprintf(
|
---|
60 | std_out, "Accepted connection from (0x%p)%s:%d\n",
|
---|
61 | address, buffer, address->inet.port);
|
---|
62 | return rv;
|
---|
63 | } /* PrintAddress */
|
---|
64 |
|
---|
65 | static void ConnectingThread(void *arg)
|
---|
66 | {
|
---|
67 | PRInt32 nbytes;
|
---|
68 | char buf[1024];
|
---|
69 | PRFileDesc *sock;
|
---|
70 | PRNetAddr peer_addr, *addr;
|
---|
71 |
|
---|
72 | addr = (PRNetAddr*)arg;
|
---|
73 |
|
---|
74 | sock = PR_NewTCPSocket();
|
---|
75 | if (sock == NULL)
|
---|
76 | {
|
---|
77 | PL_FPrintError(err_out, "PR_NewTCPSocket (client) failed");
|
---|
78 | PR_ProcessExit(1);
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (PR_Connect(sock, addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE)
|
---|
82 | {
|
---|
83 | PL_FPrintError(err_out, "PR_Connect (client) failed");
|
---|
84 | PR_ProcessExit(1);
|
---|
85 | }
|
---|
86 | if (PR_GetPeerName(sock, &peer_addr) == PR_FAILURE)
|
---|
87 | {
|
---|
88 | PL_FPrintError(err_out, "PR_GetPeerName (client) failed");
|
---|
89 | PR_ProcessExit(1);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | ** Then wait between the connection coming up and sending the expected
|
---|
94 | ** data. At some point in time, the server should fail due to a timeou
|
---|
95 | ** on the AcceptRead() operation, which according to the document is
|
---|
96 | ** only due to the read() portion.
|
---|
97 | */
|
---|
98 | PR_Sleep(write_dally);
|
---|
99 |
|
---|
100 | nbytes = PR_Send(sock, GET, sizeof(GET), 0, PR_INTERVAL_NO_TIMEOUT);
|
---|
101 | if (nbytes == -1) PL_FPrintError(err_out, "PR_Send (client) failed");
|
---|
102 |
|
---|
103 | nbytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
|
---|
104 | if (nbytes == -1) PL_FPrintError(err_out, "PR_Recv (client) failed");
|
---|
105 | else
|
---|
106 | {
|
---|
107 | PR_fprintf(std_out, "PR_Recv (client) succeeded: %d bytes\n", nbytes);
|
---|
108 | buf[sizeof(buf) - 1] = '\0';
|
---|
109 | PR_fprintf(std_out, "%s\n", buf);
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (PR_FAILURE == PR_Shutdown(sock, PR_SHUTDOWN_BOTH))
|
---|
113 | PL_FPrintError(err_out, "PR_Shutdown (client) failed");
|
---|
114 |
|
---|
115 | if (PR_FAILURE == PR_Close(sock))
|
---|
116 | PL_FPrintError(err_out, "PR_Close (client) failed");
|
---|
117 |
|
---|
118 | return;
|
---|
119 | } /* ConnectingThread */
|
---|
120 |
|
---|
121 | #define BUF_SIZE 117
|
---|
122 | static void AcceptingThread(void *arg)
|
---|
123 | {
|
---|
124 | PRStatus rv;
|
---|
125 | PRInt32 bytes;
|
---|
126 | PRSize buf_size = BUF_SIZE;
|
---|
127 | PRUint8 buf[BUF_SIZE + (2 * sizeof(PRNetAddr)) + 32];
|
---|
128 | PRNetAddr *accept_addr, *listen_addr = (PRNetAddr*)arg;
|
---|
129 | PRFileDesc *accept_sock, *listen_sock = PR_NewTCPSocket();
|
---|
130 | PRSocketOptionData sock_opt;
|
---|
131 |
|
---|
132 | if (NULL == listen_sock)
|
---|
133 | {
|
---|
134 | PL_FPrintError(err_out, "PR_NewTCPSocket (server) failed");
|
---|
135 | PR_ProcessExit(1);
|
---|
136 | }
|
---|
137 | sock_opt.option = PR_SockOpt_Reuseaddr;
|
---|
138 | sock_opt.value.reuse_addr = PR_TRUE;
|
---|
139 | rv = PR_SetSocketOption(listen_sock, &sock_opt);
|
---|
140 | if (PR_FAILURE == rv)
|
---|
141 | {
|
---|
142 | PL_FPrintError(err_out, "PR_SetSocketOption (server) failed");
|
---|
143 | PR_ProcessExit(1);
|
---|
144 | }
|
---|
145 | rv = PR_Bind(listen_sock, listen_addr);
|
---|
146 | if (PR_FAILURE == rv)
|
---|
147 | {
|
---|
148 | PL_FPrintError(err_out, "PR_Bind (server) failed");
|
---|
149 | PR_ProcessExit(1);
|
---|
150 | }
|
---|
151 | rv = PR_Listen(listen_sock, 10);
|
---|
152 | if (PR_FAILURE == rv)
|
---|
153 | {
|
---|
154 | PL_FPrintError(err_out, "PR_Listen (server) failed");
|
---|
155 | PR_ProcessExit(1);
|
---|
156 | }
|
---|
157 | bytes = PR_AcceptRead(
|
---|
158 | listen_sock, &accept_sock, &accept_addr, buf, buf_size, accept_timeout);
|
---|
159 |
|
---|
160 | if (-1 == bytes) PL_FPrintError(err_out, "PR_AcceptRead (server) failed");
|
---|
161 | else
|
---|
162 | {
|
---|
163 | PrintAddress(accept_addr);
|
---|
164 | PR_fprintf(
|
---|
165 | std_out, "(Server) read [0x%p..0x%p) %s\n",
|
---|
166 | buf, &buf[BUF_SIZE], buf);
|
---|
167 | bytes = PR_Write(accept_sock, buf, bytes);
|
---|
168 | rv = PR_Shutdown(accept_sock, PR_SHUTDOWN_BOTH);
|
---|
169 | if (PR_FAILURE == rv)
|
---|
170 | PL_FPrintError(err_out, "PR_Shutdown (server) failed");
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (-1 != bytes)
|
---|
174 | {
|
---|
175 | rv = PR_Close(accept_sock);
|
---|
176 | if (PR_FAILURE == rv)
|
---|
177 | PL_FPrintError(err_out, "PR_Close (server) failed");
|
---|
178 | }
|
---|
179 |
|
---|
180 | rv = PR_Close(listen_sock);
|
---|
181 | if (PR_FAILURE == rv)
|
---|
182 | PL_FPrintError(err_out, "PR_Close (server) failed");
|
---|
183 | } /* AcceptingThread */
|
---|
184 |
|
---|
185 | PRIntn main(PRIntn argc, char **argv)
|
---|
186 | {
|
---|
187 | PRHostEnt he;
|
---|
188 | PRStatus status;
|
---|
189 | PRIntn next_index;
|
---|
190 | PRUint16 port_number;
|
---|
191 | char netdb_buf[PR_NETDB_BUF_SIZE];
|
---|
192 | PRNetAddr client_addr, server_addr;
|
---|
193 | PRThread *client_thread, *server_thread;
|
---|
194 | PRIntervalTime delta = PR_MillisecondsToInterval(500);
|
---|
195 |
|
---|
196 | err_out = PR_STDERR;
|
---|
197 | std_out = PR_STDOUT;
|
---|
198 | accept_timeout = PR_SecondsToInterval(2);
|
---|
199 |
|
---|
200 | if (argc != 2 && argc != 3) port_number = DEFAULT_PORT;
|
---|
201 | else port_number = (PRUint16)atoi(argv[(argc == 2) ? 1 : 2]);
|
---|
202 |
|
---|
203 | status = PR_InitializeNetAddr(PR_IpAddrAny, port_number, &server_addr);
|
---|
204 | if (PR_SUCCESS != status)
|
---|
205 | {
|
---|
206 | PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
|
---|
207 | PR_ProcessExit(1);
|
---|
208 | }
|
---|
209 | if (argc < 3)
|
---|
210 | {
|
---|
211 | status = PR_InitializeNetAddr(
|
---|
212 | PR_IpAddrLoopback, port_number, &client_addr);
|
---|
213 | if (PR_SUCCESS != status)
|
---|
214 | {
|
---|
215 | PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
|
---|
216 | PR_ProcessExit(1);
|
---|
217 | }
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | status = PR_GetHostByName(
|
---|
222 | argv[1], netdb_buf, sizeof(netdb_buf), &he);
|
---|
223 | if (status == PR_FAILURE)
|
---|
224 | {
|
---|
225 | PL_FPrintError(err_out, "PR_GetHostByName failed");
|
---|
226 | PR_ProcessExit(1);
|
---|
227 | }
|
---|
228 | next_index = PR_EnumerateHostEnt(0, &he, port_number, &client_addr);
|
---|
229 | if (next_index == -1)
|
---|
230 | {
|
---|
231 | PL_FPrintError(err_out, "PR_EnumerateHostEnt failed");
|
---|
232 | PR_ProcessExit(1);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | for (
|
---|
237 | write_dally = 0;
|
---|
238 | write_dally < accept_timeout + (2 * delta);
|
---|
239 | write_dally += delta)
|
---|
240 | {
|
---|
241 | PR_fprintf(
|
---|
242 | std_out, "Testing w/ write_dally = %d msec\n",
|
---|
243 | PR_IntervalToMilliseconds(write_dally));
|
---|
244 | server_thread = PR_CreateThread(
|
---|
245 | PR_USER_THREAD, AcceptingThread, &server_addr,
|
---|
246 | PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
247 | if (server_thread == NULL)
|
---|
248 | {
|
---|
249 | PL_FPrintError(err_out, "PR_CreateThread (server) failed");
|
---|
250 | PR_ProcessExit(1);
|
---|
251 | }
|
---|
252 |
|
---|
253 | PR_Sleep(delta); /* let the server pot thicken */
|
---|
254 |
|
---|
255 | client_thread = PR_CreateThread(
|
---|
256 | PR_USER_THREAD, ConnectingThread, &client_addr,
|
---|
257 | PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
258 | if (client_thread == NULL)
|
---|
259 | {
|
---|
260 | PL_FPrintError(err_out, "PR_CreateThread (client) failed");
|
---|
261 | PR_ProcessExit(1);
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (PR_JoinThread(client_thread) == PR_FAILURE)
|
---|
265 | PL_FPrintError(err_out, "PR_JoinThread (client) failed");
|
---|
266 |
|
---|
267 | if (PR_JoinThread(server_thread) == PR_FAILURE)
|
---|
268 | PL_FPrintError(err_out, "PR_JoinThread (server) failed");
|
---|
269 | }
|
---|
270 |
|
---|
271 | return 0;
|
---|
272 | }
|
---|