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 | #ifdef WIN32
|
---|
39 | #include <windows.h>
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #ifdef XP_OS2_VACPP
|
---|
43 | #include <io.h> /* for close() */
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #include "prinit.h"
|
---|
47 | #include "prio.h"
|
---|
48 | #include "prlog.h"
|
---|
49 | #include "prprf.h"
|
---|
50 | #include "prnetdb.h"
|
---|
51 |
|
---|
52 | #ifndef XP_MAC
|
---|
53 | #include "private/pprio.h"
|
---|
54 | #else
|
---|
55 | #include "pprio.h"
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #define CLIENT_LOOPS 5
|
---|
59 | #define BUF_SIZE 128
|
---|
60 |
|
---|
61 | #include <stdio.h>
|
---|
62 | #include <string.h>
|
---|
63 | #include <stdlib.h>
|
---|
64 |
|
---|
65 | static void
|
---|
66 | clientThreadFunc(void *arg)
|
---|
67 | {
|
---|
68 | PRUint16 port = (PRUint16) arg;
|
---|
69 | PRFileDesc *sock;
|
---|
70 | PRNetAddr addr;
|
---|
71 | char buf[BUF_SIZE];
|
---|
72 | int i;
|
---|
73 |
|
---|
74 | addr.inet.family = PR_AF_INET;
|
---|
75 | addr.inet.port = PR_htons(port);
|
---|
76 | addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
|
---|
77 | PR_snprintf(buf, sizeof(buf), "%hu", port);
|
---|
78 |
|
---|
79 | for (i = 0; i < 5; i++) {
|
---|
80 | sock = PR_NewTCPSocket();
|
---|
81 | PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
|
---|
82 |
|
---|
83 | PR_Write(sock, buf, sizeof(buf));
|
---|
84 | PR_Close(sock);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | int main(int argc, char **argv)
|
---|
89 | {
|
---|
90 | PRFileDesc *listenSock1, *listenSock2;
|
---|
91 | PRFileDesc *badFD;
|
---|
92 | PRUint16 listenPort1, listenPort2;
|
---|
93 | PRNetAddr addr;
|
---|
94 | char buf[BUF_SIZE];
|
---|
95 | PRThread *clientThread;
|
---|
96 | PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
|
---|
97 | PRIntn npds;
|
---|
98 | PRInt32 retVal;
|
---|
99 | PRInt32 sd, rv;
|
---|
100 | struct sockaddr_in saddr;
|
---|
101 | PRIntn saddr_len;
|
---|
102 | PRUint16 listenPort3;
|
---|
103 | PRFileDesc *socket_poll_fd;
|
---|
104 | PRIntn i, j;
|
---|
105 |
|
---|
106 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
|
---|
107 | PR_STDIO_INIT();
|
---|
108 |
|
---|
109 | printf("This program tests PR_Poll with sockets.\n");
|
---|
110 | printf("Timeout, error reporting, and normal operation are tested.\n\n");
|
---|
111 |
|
---|
112 | /* Create two listening sockets */
|
---|
113 | if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
|
---|
114 | fprintf(stderr, "Can't create a new TCP socket\n");
|
---|
115 | exit(1);
|
---|
116 | }
|
---|
117 | addr.inet.family = PR_AF_INET;
|
---|
118 | addr.inet.ip = PR_htonl(PR_INADDR_ANY);
|
---|
119 | addr.inet.port = PR_htons(0);
|
---|
120 | if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
|
---|
121 | fprintf(stderr, "Can't bind socket\n");
|
---|
122 | exit(1);
|
---|
123 | }
|
---|
124 | if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
|
---|
125 | fprintf(stderr, "PR_GetSockName failed\n");
|
---|
126 | exit(1);
|
---|
127 | }
|
---|
128 | listenPort1 = PR_ntohs(addr.inet.port);
|
---|
129 | if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
|
---|
130 | fprintf(stderr, "Can't listen on a socket\n");
|
---|
131 | exit(1);
|
---|
132 | }
|
---|
133 |
|
---|
134 | if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
|
---|
135 | fprintf(stderr, "Can't create a new TCP socket\n");
|
---|
136 | exit(1);
|
---|
137 | }
|
---|
138 | addr.inet.family = PR_AF_INET;
|
---|
139 | addr.inet.ip = PR_htonl(PR_INADDR_ANY);
|
---|
140 | addr.inet.port = PR_htons(0);
|
---|
141 | if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
|
---|
142 | fprintf(stderr, "Can't bind socket\n");
|
---|
143 | exit(1);
|
---|
144 | }
|
---|
145 | if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
|
---|
146 | fprintf(stderr, "PR_GetSockName failed\n");
|
---|
147 | exit(1);
|
---|
148 | }
|
---|
149 | listenPort2 = PR_ntohs(addr.inet.port);
|
---|
150 | if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
|
---|
151 | fprintf(stderr, "Can't listen on a socket\n");
|
---|
152 | exit(1);
|
---|
153 | }
|
---|
154 | /* Set up the poll descriptor array */
|
---|
155 | pds = pds0;
|
---|
156 | other_pds = pds1;
|
---|
157 | memset(pds, 0, sizeof(pds));
|
---|
158 | npds = 0;
|
---|
159 | pds[npds].fd = listenSock1;
|
---|
160 | pds[npds].in_flags = PR_POLL_READ;
|
---|
161 | npds++;
|
---|
162 | pds[npds].fd = listenSock2;
|
---|
163 | pds[npds].in_flags = PR_POLL_READ;
|
---|
164 | npds++;
|
---|
165 |
|
---|
166 | sd = socket(AF_INET, SOCK_STREAM, 0);
|
---|
167 | PR_ASSERT(sd >= 0);
|
---|
168 | memset((char *) &saddr, 0, sizeof(saddr));
|
---|
169 | saddr.sin_family = AF_INET;
|
---|
170 | saddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
---|
171 | saddr.sin_port = htons(0);
|
---|
172 |
|
---|
173 | rv = bind(sd, (struct sockaddr *)&saddr, sizeof(saddr));
|
---|
174 | PR_ASSERT(rv == 0);
|
---|
175 | saddr_len = sizeof(saddr);
|
---|
176 | rv = getsockname(sd, (struct sockaddr *) &saddr, &saddr_len);
|
---|
177 | PR_ASSERT(rv == 0);
|
---|
178 | listenPort3 = ntohs(saddr.sin_port);
|
---|
179 |
|
---|
180 | rv = listen(sd, 5);
|
---|
181 | PR_ASSERT(rv == 0);
|
---|
182 | pds[npds].fd = socket_poll_fd = PR_CreateSocketPollFd(sd);
|
---|
183 | PR_ASSERT(pds[npds].fd);
|
---|
184 | pds[npds].in_flags = PR_POLL_READ;
|
---|
185 | npds++;
|
---|
186 | PR_snprintf(buf, sizeof(buf),
|
---|
187 | "The server thread is listening on ports %hu, %hu and %hu\n\n",
|
---|
188 | listenPort1, listenPort2, listenPort3);
|
---|
189 | printf("%s", buf);
|
---|
190 |
|
---|
191 | /* Testing timeout */
|
---|
192 | printf("PR_Poll should time out in 5 seconds\n");
|
---|
193 | retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
|
---|
194 | if (retVal != 0) {
|
---|
195 | PR_snprintf(buf, sizeof(buf),
|
---|
196 | "PR_Poll should time out and return 0, but it returns %ld\n",
|
---|
197 | retVal);
|
---|
198 | fprintf(stderr, "%s", buf);
|
---|
199 | exit(1);
|
---|
200 | }
|
---|
201 | printf("PR_Poll timed out. Test passed.\n\n");
|
---|
202 |
|
---|
203 | /* Testing bad fd */
|
---|
204 | printf("PR_Poll should detect a bad file descriptor\n");
|
---|
205 | if ((badFD = PR_NewTCPSocket()) == NULL) {
|
---|
206 | fprintf(stderr, "Can't create a TCP socket\n");
|
---|
207 | exit(1);
|
---|
208 | }
|
---|
209 |
|
---|
210 | pds[npds].fd = badFD;
|
---|
211 | pds[npds].in_flags = PR_POLL_READ;
|
---|
212 | npds++;
|
---|
213 | PR_Close(badFD); /* make the fd bad */
|
---|
214 | #if 0
|
---|
215 | retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
|
---|
216 | if (retVal != 1 || (unsigned short) pds[2].out_flags != PR_POLL_NVAL) {
|
---|
217 | fprintf(stderr, "Failed to detect the bad fd: "
|
---|
218 | "PR_Poll returns %d, out_flags is 0x%hx\n",
|
---|
219 | retVal, pds[npds - 1].out_flags);
|
---|
220 | exit(1);
|
---|
221 | }
|
---|
222 | printf("PR_Poll detected the bad fd. Test passed.\n\n");
|
---|
223 | #endif
|
---|
224 | npds--;
|
---|
225 |
|
---|
226 | clientThread = PR_CreateThread(PR_USER_THREAD,
|
---|
227 | clientThreadFunc, (void *) listenPort1,
|
---|
228 | PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
|
---|
229 | PR_UNJOINABLE_THREAD, 0);
|
---|
230 | if (clientThread == NULL) {
|
---|
231 | fprintf(stderr, "can't create thread\n");
|
---|
232 | exit(1);
|
---|
233 | }
|
---|
234 |
|
---|
235 | clientThread = PR_CreateThread(PR_USER_THREAD,
|
---|
236 | clientThreadFunc, (void *) listenPort2,
|
---|
237 | PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
|
---|
238 | PR_UNJOINABLE_THREAD, 0);
|
---|
239 | if (clientThread == NULL) {
|
---|
240 | fprintf(stderr, "can't create thread\n");
|
---|
241 | exit(1);
|
---|
242 | }
|
---|
243 |
|
---|
244 | clientThread = PR_CreateThread(PR_USER_THREAD,
|
---|
245 | clientThreadFunc, (void *) listenPort3,
|
---|
246 | PR_PRIORITY_NORMAL, PR_GLOBAL_BOUND_THREAD,
|
---|
247 | PR_UNJOINABLE_THREAD, 0);
|
---|
248 | if (clientThread == NULL) {
|
---|
249 | fprintf(stderr, "can't create thread\n");
|
---|
250 | exit(1);
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | printf("Three client threads are created. Each of them will\n");
|
---|
255 | printf("send data to one of the three ports the server is listening on.\n");
|
---|
256 | printf("The data they send is the port number. Each of them send\n");
|
---|
257 | printf("the data five times, so you should see ten lines below,\n");
|
---|
258 | printf("interleaved in an arbitrary order.\n");
|
---|
259 |
|
---|
260 | /* 30 events total */
|
---|
261 | i = 0;
|
---|
262 | while (i < 30) {
|
---|
263 | PRPollDesc *tmp;
|
---|
264 | int nextIndex;
|
---|
265 | int nEvents = 0;
|
---|
266 |
|
---|
267 | retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
|
---|
268 | PR_ASSERT(retVal != 0); /* no timeout */
|
---|
269 | if (retVal == -1) {
|
---|
270 | fprintf(stderr, "PR_Poll failed\n");
|
---|
271 | exit(1);
|
---|
272 | }
|
---|
273 |
|
---|
274 | nextIndex = 3;
|
---|
275 | /* the three listening sockets */
|
---|
276 | for (j = 0; j < 3; j++) {
|
---|
277 | other_pds[j] = pds[j];
|
---|
278 | PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
|
---|
279 | && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
|
---|
280 | if (pds[j].out_flags & PR_POLL_READ) {
|
---|
281 | PRFileDesc *sock;
|
---|
282 |
|
---|
283 | nEvents++;
|
---|
284 | if (j == 2) {
|
---|
285 | int newsd;
|
---|
286 | newsd = accept(PR_FileDesc2NativeHandle(pds[j].fd), NULL, 0);
|
---|
287 | if (newsd == -1) {
|
---|
288 | fprintf(stderr, "accept() failed\n");
|
---|
289 | exit(1);
|
---|
290 | }
|
---|
291 | other_pds[nextIndex].fd = PR_CreateSocketPollFd(newsd);
|
---|
292 | PR_ASSERT(other_pds[nextIndex].fd);
|
---|
293 | other_pds[nextIndex].in_flags = PR_POLL_READ;
|
---|
294 | } else {
|
---|
295 | sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
|
---|
296 | if (sock == NULL) {
|
---|
297 | fprintf(stderr, "PR_Accept() failed\n");
|
---|
298 | exit(1);
|
---|
299 | }
|
---|
300 | other_pds[nextIndex].fd = sock;
|
---|
301 | other_pds[nextIndex].in_flags = PR_POLL_READ;
|
---|
302 | }
|
---|
303 | nextIndex++;
|
---|
304 | } else if (pds[j].out_flags & PR_POLL_ERR) {
|
---|
305 | fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
|
---|
306 | exit(1);
|
---|
307 | } else if (pds[j].out_flags & PR_POLL_NVAL) {
|
---|
308 | fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n",
|
---|
309 | PR_FileDesc2NativeHandle(pds[j].fd));
|
---|
310 | exit(1);
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | for (j = 3; j < npds; j++) {
|
---|
315 | PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
|
---|
316 | && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
|
---|
317 | if (pds[j].out_flags & PR_POLL_READ) {
|
---|
318 | PRInt32 nBytes;
|
---|
319 |
|
---|
320 | nEvents++;
|
---|
321 | /* XXX: This call is a hack and should be fixed */
|
---|
322 | if (PR_GetDescType(pds[j].fd) == (PRDescType) 0) {
|
---|
323 | nBytes = recv(PR_FileDesc2NativeHandle(pds[j].fd), buf,
|
---|
324 | sizeof(buf), 0);
|
---|
325 | if (nBytes == -1) {
|
---|
326 | fprintf(stderr, "recv() failed\n");
|
---|
327 | exit(1);
|
---|
328 | }
|
---|
329 | printf("Server read %d bytes from native fd %d\n",nBytes,
|
---|
330 | PR_FileDesc2NativeHandle(pds[j].fd));
|
---|
331 | #ifdef WIN32
|
---|
332 | closesocket((SOCKET)PR_FileDesc2NativeHandle(pds[j].fd));
|
---|
333 | #else
|
---|
334 | close(PR_FileDesc2NativeHandle(pds[j].fd));
|
---|
335 | #endif
|
---|
336 | PR_DestroySocketPollFd(pds[j].fd);
|
---|
337 | } else {
|
---|
338 | nBytes = PR_Read(pds[j].fd, buf, sizeof(buf));
|
---|
339 | if (nBytes == -1) {
|
---|
340 | fprintf(stderr, "PR_Read() failed\n");
|
---|
341 | exit(1);
|
---|
342 | }
|
---|
343 | PR_Close(pds[j].fd);
|
---|
344 | }
|
---|
345 | /* Just to be safe */
|
---|
346 | buf[BUF_SIZE - 1] = '\0';
|
---|
347 | printf("The server received \"%s\" from a client\n", buf);
|
---|
348 | } else if (pds[j].out_flags & PR_POLL_ERR) {
|
---|
349 | fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
|
---|
350 | exit(1);
|
---|
351 | } else if (pds[j].out_flags & PR_POLL_NVAL) {
|
---|
352 | fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
|
---|
353 | exit(1);
|
---|
354 | } else {
|
---|
355 | other_pds[nextIndex] = pds[j];
|
---|
356 | nextIndex++;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | PR_ASSERT(retVal == nEvents);
|
---|
361 | /* swap */
|
---|
362 | tmp = pds;
|
---|
363 | pds = other_pds;
|
---|
364 | other_pds = tmp;
|
---|
365 | npds = nextIndex;
|
---|
366 | i += nEvents;
|
---|
367 | }
|
---|
368 | PR_DestroySocketPollFd(socket_poll_fd);
|
---|
369 |
|
---|
370 | printf("All tests finished\n");
|
---|
371 | PR_Cleanup();
|
---|
372 | return 0;
|
---|
373 | }
|
---|