7 /* connect operation with timeout
9 /* #include <sys/socket.h>
10 /* #include <timed_connect.h>
12 /* int timed_connect(fd, buf, buf_len, timeout)
14 /* struct sockaddr *buf;
18 /* timed_connect() implement a BSD socket connect() operation that is
23 /* File descriptor in the range 0..FD_SETSIZE. This descriptor
24 /* must be set to non-blocking mode prior to calling timed_connect().
26 /* Socket address buffer pointer.
28 /* Size of socket address buffer.
30 /* The deadline in seconds. This must be a number > 0.
32 /* Panic: interface violations.
33 /* When the operation does not complete within the deadline, the
34 /* result value is -1, and errno is set to ETIMEDOUT.
35 /* All other returns are identical to those of a blocking connect(2)
40 /* A common error is to call timed_connect() without enabling
41 /* non-blocking I/O on the socket. In that case, the \fItimeout\fR
42 /* parameter takes no effect.
46 /* The Secure Mailer license must be distributed with this software.
49 /* IBM T.J. Watson Research
51 /* Yorktown Heights, NY 10598, USA
57 #include <sys/socket.h>
60 /* Utility library. */
64 #include "sane_connect.h"
65 #include "timed_connect.h"
67 /* timed_connect - connect with deadline */
69 int timed_connect(int sock
, struct sockaddr
* sa
, int len
, int timeout
)
72 SOCKOPT_SIZE error_len
;
75 * Sanity check. Just like with timed_wait(), the timeout must be a
79 msg_panic("timed_connect: bad timeout: %d", timeout
);
82 * Start the connection, and handle all possible results.
84 if (sane_connect(sock
, sa
, len
) == 0)
86 if (errno
!= EINPROGRESS
)
90 * A connection is in progress. Wait for a limited amount of time for
91 * something to happen. If nothing happens, report an error.
93 if (write_wait(sock
, timeout
) < 0)
97 * Something happened. Some Solaris 2 versions have getsockopt() itself
98 * return the error, instead of returning it via the parameter list.
101 error_len
= sizeof(error
);
102 if (getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, (char *) &error
, &error_len
) < 0)