-------------------------------------------------------------------------*\ * Connects or returns error message \*-------------------------------------------------------------------------*/
| 158 | * Connects or returns error message |
| 159 | \*-------------------------------------------------------------------------*/ |
| 160 | int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { |
| 161 | int err; |
| 162 | /* avoid calling on closed sockets */ |
| 163 | if (*ps == SOCKET_INVALID) return IO_CLOSED; |
| 164 | /* call connect until done or failed without being interrupted */ |
| 165 | do if (connect(*ps, addr, len) == 0) return IO_DONE; |
| 166 | while ((err = errno) == EINTR); |
| 167 | /* if connection failed immediately, return error code */ |
| 168 | if (err != EINPROGRESS && err != EAGAIN) return err; |
| 169 | /* zero timeout case optimization */ |
| 170 | if (timeout_iszero(tm)) return IO_TIMEOUT; |
| 171 | /* wait until we have the result of the connection attempt or timeout */ |
| 172 | err = socket_waitfd(ps, WAITFD_C, tm); |
| 173 | if (err == IO_CLOSED) { |
| 174 | if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE; |
| 175 | else return errno; |
| 176 | } else return err; |
| 177 | } |
| 178 | |
| 179 | /*-------------------------------------------------------------------------*\ |
| 180 | * Accept with timeout |
no test coverage detected