-------------------------------------------------------------------------*\ * Tries to connect to remote address (address, port) \*-------------------------------------------------------------------------*/
| 399 | * Tries to connect to remote address (address, port) |
| 400 | \*-------------------------------------------------------------------------*/ |
| 401 | const char *inet_tryconnect(p_socket ps, int *family, const char *address, |
| 402 | const char *serv, p_timeout tm, struct addrinfo *connecthints) |
| 403 | { |
| 404 | struct addrinfo *iterator = NULL, *resolved = NULL; |
| 405 | const char *err = NULL; |
| 406 | /* try resolving */ |
| 407 | err = socket_gaistrerror(getaddrinfo(address, serv, |
| 408 | connecthints, &resolved)); |
| 409 | if (err != NULL) { |
| 410 | if (resolved) freeaddrinfo(resolved); |
| 411 | return err; |
| 412 | } |
| 413 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { |
| 414 | timeout_markstart(tm); |
| 415 | /* create new socket if necessary. if there was no |
| 416 | * bind, we need to create one for every new family |
| 417 | * that shows up while iterating. if there was a |
| 418 | * bind, all families will be the same and we will |
| 419 | * not enter this branch. */ |
| 420 | if (*family != iterator->ai_family) { |
| 421 | socket_destroy(ps); |
| 422 | err = socket_strerror(socket_create(ps, iterator->ai_family, |
| 423 | iterator->ai_socktype, iterator->ai_protocol)); |
| 424 | if (err != NULL) { |
| 425 | freeaddrinfo(resolved); |
| 426 | return err; |
| 427 | } |
| 428 | *family = iterator->ai_family; |
| 429 | /* all sockets initially non-blocking */ |
| 430 | socket_setnonblocking(ps); |
| 431 | } |
| 432 | /* try connecting to remote address */ |
| 433 | err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, |
| 434 | (socklen_t) iterator->ai_addrlen, tm)); |
| 435 | /* if success, break out of loop */ |
| 436 | if (err == NULL) break; |
| 437 | } |
| 438 | freeaddrinfo(resolved); |
| 439 | /* here, if err is set, we failed */ |
| 440 | return err; |
| 441 | } |
| 442 | |
| 443 | /*-------------------------------------------------------------------------*\ |
| 444 | * Tries to accept a socket |
no test coverage detected