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