Try to set the local address for a newly-created socket. * Return -1 if this fails. */
| 135 | /* Try to set the local address for a newly-created socket. |
| 136 | * Return -1 if this fails. */ |
| 137 | int try_bind_local(int s, int ai_family, int ai_socktype, |
| 138 | const char *bind_addr) |
| 139 | { |
| 140 | int error; |
| 141 | struct addrinfo bhints, *bres_all, *r; |
| 142 | |
| 143 | memset(&bhints, 0, sizeof bhints); |
| 144 | bhints.ai_family = ai_family; |
| 145 | bhints.ai_socktype = ai_socktype; |
| 146 | bhints.ai_flags = AI_PASSIVE; |
| 147 | if ((error = getaddrinfo(bind_addr, NULL, &bhints, &bres_all))) { |
| 148 | rprintf(FERROR, RSYNC_NAME ": getaddrinfo %s: %s\n", |
| 149 | bind_addr, gai_strerror(error)); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | for (r = bres_all; r; r = r->ai_next) { |
| 154 | if (bind(s, r->ai_addr, r->ai_addrlen) == -1) |
| 155 | continue; |
| 156 | freeaddrinfo(bres_all); |
| 157 | return s; |
| 158 | } |
| 159 | |
| 160 | /* no error message; there might be some problem that allows |
| 161 | * creation of the socket but not binding, perhaps if the |
| 162 | * machine has no ipv6 address of this name. */ |
| 163 | freeaddrinfo(bres_all); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | /* connect() timeout handler based on alarm() */ |
| 168 | static void contimeout_handler(UNUSED(int val)) |
no test coverage detected