1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* A test for nonblocking connect. Functions tested include PR_Connect,
* PR_Poll, and PR_GetConnectStatus.
*
* The test should be invoked with a host name, for example:
* nbconn www.netscape.com
* It will do a nonblocking connect to port 80 (HTTP) on that host,
* and when connected, issue the "GET /" HTTP command.
*
* You should run this test in three ways:
* 1. To a known web site, such as www.netscape.com. The HTML of the
* top-level page at the web site should be printed.
* 2. To a machine not running a web server at port 80. This test should
* fail. Ideally the error code should be PR_CONNECT_REFUSED_ERROR.
* But it is possible to return PR_UNKNOWN_ERROR on certain platforms.
* 3. To an unreachable machine, for example, a machine that is off line.
* The test should fail after the connect times out. Ideally the
* error code should be PR_IO_TIMEOUT_ERROR, but it is possible to
* return PR_UNKNOWN_ERROR on certain platforms.
*/
#include "nspr.h"
#include "plgetopt.h"
#include <stdio.h>
#include <string.h>
#define SERVER_MAX_BIND_COUNT 100
#define DATA_BUF_SIZE 256
#define TCP_SERVER_PORT 10000
#define TCP_UNUSED_PORT 211
typedef struct Server_Param {
PRFileDesc *sp_fd; /* server port */
} Server_Param;
static void PR_CALLBACK TCP_Server(void *arg);
int _debug_on;
#define DPRINTF(arg) if (_debug_on) printf arg
static PRIntn connection_success_test();
static PRIntn connection_failure_test();
int main(int argc, char **argv)
{
PRHostEnt he;
char buf[1024];
PRNetAddr addr;
PRPollDesc pd;
PRStatus rv;
PRSocketOptionData optData;
const char *hostname = NULL;
PRIntn default_case, n, bytes_read, bytes_sent;
PRInt32 failed_already = 0;
/*
* -d debug mode
*/
PLOptStatus os;
PLOptState *opt = PL_CreateOptState(argc, argv, "d");
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
{
if (PL_OPT_BAD == os) continue;
switch (opt->option)
{
case 0: /* debug mode */
hostname = opt->value;
break;
case 'd': /* debug mode */
_debug_on = 1;
break;
default:
break;
}
}
PL_DestroyOptState(opt);
PR_STDIO_INIT();
if (hostname)
default_case = 0;
else
default_case = 1;
if (default_case) {
/*
* In the default case the following tests are executed:
* 1. successful connection: a server thread accepts a connection
* from the main thread
* 2. unsuccessful connection: the main thread tries to connect to a
* nonexistent port and expects to get an error
*/
rv = connection_success_test();
if (rv == 0)
rv = connection_failure_test();
return rv;
} else {
PRFileDesc *sock;
if (PR_GetHostByName(argv[1], buf, sizeof(buf), &he) == PR_FAILURE) {
printf( "Unknown host: %s\n", argv[1]);
exit(1);
} else {
printf( "host: %s\n", buf);
}
PR_EnumerateHostEnt(0, &he, 80, &addr);
sock = PR_NewTCPSocket();
optData.option = PR_SockOpt_Nonblocking;
optData.value.non_blocking = PR_TRUE;
PR_SetSocketOption(sock, &optData);
rv = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
if (rv == PR_FAILURE && PR_GetError() == PR_IN_PROGRESS_ERROR) {
printf( "Connect in progress\n");
}
pd.fd = sock;
pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT;
n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
if (n == -1) {
printf( "PR_Poll failed\n");
exit(1);
}
printf( "PR_Poll returns %d\n", n);
if (pd.out_flags & PR_POLL_READ) {
printf( "PR_POLL_READ\n");
}
if (pd.out_flags & PR_POLL_WRITE) {
printf( "PR_POLL_WRITE\n");
}
if (pd.out_flags & PR_POLL_EXCEPT) {
printf( "PR_POLL_EXCEPT\n");
}
if (pd.out_flags & PR_POLL_ERR) {
printf( "PR_POLL_ERR\n");
}
if (pd.out_flags & PR_POLL_NVAL) {
printf( "PR_POLL_NVAL\n");
}
if (PR_GetConnectStatus(&pd) == PR_SUCCESS) {
printf("PR_GetConnectStatus: connect succeeded\n");
PR_Write(sock, "GET /\r\n\r\n", 9);
PR_Shutdown(sock, PR_SHUTDOWN_SEND);
pd.in_flags = PR_POLL_READ;
while (1) {
n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
printf( "poll returns %d\n", n);
n = PR_Read(sock, buf, sizeof(buf));
printf( "read returns %d\n", n);
if (n <= 0) {
break;
}
PR_Write(PR_STDOUT, buf, n);
}
} else {
if (PR_GetError() == PR_IN_PROGRESS_ERROR) {
printf( "PR_GetConnectStatus: connect still in progress\n");
exit(1);
}
printf( "PR_GetConnectStatus: connect failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
}
PR_Close(sock);
printf( "PASS\n");
return 0;
}
}
/*
* TCP Server
* Server Thread
* Accept a connection from the client and write some data
*/
static void PR_CALLBACK
TCP_Server(void *arg)
{
Server_Param *sp = (Server_Param *) arg;
PRFileDesc *sockfd, *newsockfd;
char data_buf[DATA_BUF_SIZE];
PRIntn rv, bytes_read;
sockfd = sp->sp_fd;
if ((newsockfd = PR_Accept(sockfd, NULL,
PR_INTERVAL_NO_TIMEOUT)) == NULL) {
fprintf(stderr,"ERROR - PR_Accept failed: (%d,%d)\n",
PR_GetError(), PR_GetOSError());
return;
}
bytes_read = 0;
while (bytes_read != DATA_BUF_SIZE) {
rv = PR_Read(newsockfd, data_buf + bytes_read ,
DATA_BUF_SIZE - bytes_read);
if (rv < 0) {
fprintf(stderr,"Error - PR_Read failed: (%d, %d)\n",
PR_GetError(), PR_GetOSError());
PR_Close(newsockfd);
return;
}
PR_ASSERT(rv != 0);
bytes_read += rv;
}
DPRINTF(("Bytes read from client - %d\n",bytes_read));
rv = PR_Write(newsockfd, data_buf,DATA_BUF_SIZE);
if (rv < 0) {
fprintf(stderr,"Error - PR_Write failed: (%d, %d)\n",
PR_GetError(), PR_GetOSError());
PR_Close(newsockfd);
return;
}
PR_ASSERT(rv == DATA_BUF_SIZE);
DPRINTF(("Bytes written to client - %d\n",rv));
PR_Close(newsockfd);
}
/*
* test for successful connection using a non-blocking socket
*/
static PRIntn
connection_success_test()
{
PRFileDesc *sockfd = NULL, *conn_fd = NULL;
PRNetAddr netaddr;
PRInt32 i, rv;
PRPollDesc pd;
PRSocketOptionData optData;
PRThread *thr = NULL;
Server_Param sp;
char send_buf[DATA_BUF_SIZE], recv_buf[DATA_BUF_SIZE];
PRIntn default_case, n, bytes_read, bytes_sent;
PRIntn failed_already = 0;
/*
* Create a tcp socket
*/
if ((sockfd = PR_NewTCPSocket()) == NULL) {
fprintf(stderr,"Error - PR_NewTCPSocket failed\n");
failed_already=1;
goto def_exit;
}
memset(&netaddr, 0 , sizeof(netaddr));
netaddr.inet.family = PR_AF_INET;
netaddr.inet.port = PR_htons(TCP_SERVER_PORT);
netaddr.inet.ip = PR_htonl(PR_INADDR_ANY);
/*
* try a few times to bind server's address, if addresses are in
* use
*/
i = 0;
while (PR_Bind(sockfd, &netaddr) < 0) {
if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) {
netaddr.inet.port += 2;
if (i++ < SERVER_MAX_BIND_COUNT)
continue;
}
fprintf(stderr,"ERROR - PR_Bind failed: (%d,%d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
if (PR_Listen(sockfd, 32) < 0) {
fprintf(stderr,"ERROR - PR_Listen failed: (%d,%d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
if (PR_GetSockName(sockfd, &netaddr) < 0) {
fprintf(stderr,"ERROR - PR_GetSockName failed: (%d,%d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
if ((conn_fd = PR_NewTCPSocket()) == NULL) {
fprintf(stderr,"Error - PR_NewTCPSocket failed\n");
failed_already=1;
goto def_exit;
}
optData.option = PR_SockOpt_Nonblocking;
optData.value.non_blocking = PR_TRUE;
PR_SetSocketOption(conn_fd, &optData);
rv = PR_Connect(conn_fd, &netaddr, PR_INTERVAL_NO_TIMEOUT);
if (rv == PR_FAILURE) {
if (PR_GetError() == PR_IN_PROGRESS_ERROR) {
DPRINTF(("Connect in progress\n"));
} else {
fprintf(stderr,"Error - PR_Connect failed: (%d, %d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
}
/*
* Now create a thread to accept a connection
*/
sp.sp_fd = sockfd;
thr = PR_CreateThread(PR_USER_THREAD, TCP_Server, (void *)&sp,
PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
if (thr == NULL) {
fprintf(stderr,"Error - PR_CreateThread failed: (%d,%d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
DPRINTF(("Created TCP_Server thread [0x%x]\n",thr));
pd.fd = conn_fd;
pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT;
n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
if (n == -1) {
fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
if (PR_GetConnectStatus(&pd) == PR_SUCCESS) {
PRInt32 rv;
DPRINTF(("Connection successful\n"));
/*
* Write some data, read it back and check data integrity to
* make sure the connection is good
*/
pd.in_flags = PR_POLL_WRITE;
bytes_sent = 0;
memset(send_buf, 'a', DATA_BUF_SIZE);
while (bytes_sent != DATA_BUF_SIZE) {
rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
if (rv < 0) {
fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
PR_ASSERT((rv == 1) && (pd.out_flags == PR_POLL_WRITE));
rv = PR_Write(conn_fd, send_buf + bytes_sent,
DATA_BUF_SIZE - bytes_sent);
if (rv < 0) {
fprintf(stderr,"Error - PR_Write failed: (%d, %d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
PR_ASSERT(rv > 0);
bytes_sent += rv;
}
DPRINTF(("Bytes written to server - %d\n",bytes_sent));
PR_Shutdown(conn_fd, PR_SHUTDOWN_SEND);
pd.in_flags = PR_POLL_READ;
bytes_read = 0;
memset(recv_buf, 0, DATA_BUF_SIZE);
while (bytes_read != DATA_BUF_SIZE) {
rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
if (rv < 0) {
fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
PR_ASSERT((rv == 1) && (pd.out_flags == PR_POLL_READ));
rv = PR_Read(conn_fd, recv_buf + bytes_read ,
DATA_BUF_SIZE - bytes_read);
if (rv < 0) {
fprintf(stderr,"Error - PR_Read failed: (%d, %d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
PR_ASSERT(rv != 0);
bytes_read += rv;
}
DPRINTF(("Bytes read from server - %d\n",bytes_read));
/*
* verify the data read
*/
if (memcmp(send_buf, recv_buf, DATA_BUF_SIZE) != 0) {
fprintf(stderr,"ERROR - data corruption\n");
failed_already=1;
goto def_exit;
}
DPRINTF(("Data integrity verified\n"));
} else {
fprintf(stderr,"PR_GetConnectStatus: connect failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already = 1;
goto def_exit;
}
def_exit:
if (thr) {
PR_JoinThread(thr);
thr = NULL;
}
if (sockfd) {
PR_Close(sockfd);
sockfd = NULL;
}
if (conn_fd) {
PR_Close(conn_fd);
conn_fd = NULL;
}
if (failed_already)
return 1;
else
return 0;
}
/*
* test for connection to a nonexistent port using a non-blocking socket
*/
static PRIntn
connection_failure_test()
{
PRFileDesc *sockfd = NULL, *conn_fd = NULL;
PRNetAddr netaddr;
PRInt32 i, rv;
PRPollDesc pd;
PRSocketOptionData optData;
PRIntn n, failed_already = 0;
/*
* Create a tcp socket
*/
if ((sockfd = PR_NewTCPSocket()) == NULL) {
fprintf(stderr,"Error - PR_NewTCPSocket failed\n");
failed_already=1;
goto def_exit;
}
memset(&netaddr, 0 , sizeof(netaddr));
netaddr.inet.family = PR_AF_INET;
netaddr.inet.port = PR_htons(TCP_SERVER_PORT);
netaddr.inet.ip = PR_htonl(PR_INADDR_ANY);
/*
* try a few times to bind server's address, if addresses are in
* use
*/
i = 0;
while (PR_Bind(sockfd, &netaddr) < 0) {
if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) {
netaddr.inet.port += 2;
if (i++ < SERVER_MAX_BIND_COUNT)
continue;
}
fprintf(stderr,"ERROR - PR_Bind failed: (%d,%d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
if (PR_GetSockName(sockfd, &netaddr) < 0) {
fprintf(stderr,"ERROR - PR_GetSockName failed: (%d,%d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
#ifdef AIX
/*
* On AIX, set to unused/reserved port
*/
netaddr.inet.port = PR_htons(TCP_UNUSED_PORT);
#endif
if ((conn_fd = PR_NewTCPSocket()) == NULL) {
fprintf(stderr,"Error - PR_NewTCPSocket failed\n");
failed_already=1;
goto def_exit;
}
optData.option = PR_SockOpt_Nonblocking;
optData.value.non_blocking = PR_TRUE;
PR_SetSocketOption(conn_fd, &optData);
rv = PR_Connect(conn_fd, &netaddr, PR_INTERVAL_NO_TIMEOUT);
if (rv == PR_FAILURE) {
DPRINTF(("PR_Connect to a non-listen port failed: (%d, %d)\n",
PR_GetError(), PR_GetOSError()));
} else {
PR_ASSERT(rv == PR_SUCCESS);
fprintf(stderr,"Error - PR_Connect succeeded, expected to fail\n");
failed_already=1;
goto def_exit;
}
pd.fd = conn_fd;
pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT;
n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
if (n == -1) {
fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
goto def_exit;
}
if (PR_GetConnectStatus(&pd) == PR_SUCCESS) {
PRInt32 rv;
fprintf(stderr,"PR_GetConnectStatus succeeded, expected to fail\n");
failed_already = 1;
goto def_exit;
}
rv = PR_GetError();
DPRINTF(("Connection failed, successfully with PR_Error %d\n",rv));
def_exit:
if (sockfd) {
PR_Close(sockfd);
sockfd = NULL;
}
if (conn_fd) {
PR_Close(conn_fd);
conn_fd = NULL;
}
if (failed_already)
return 1;
else
return 0;
}
|