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