if ((*sock = socket (AF_INET, SOCK_STREAM, 0)) == -1)
return CONSOCKERR;
+ if (opt.bind_address != NULL)
+ {
+ /* Bind the client side to the requested address. */
+ if (bind (*sock, (struct sockaddr *) opt.bind_address,
+ sizeof (*opt.bind_address)))
+ return CONSOCKERR;
+ }
+
/* Connect the socket to the remote host. */
if (connect (*sock, (struct sockaddr *) &sock_name, sizeof (sock_name)))
{
return NOCONERROR;
}
+int
+test_socket_open (int sock)
+{
+#ifdef HAVE_SELECT
+ fd_set check_set;
+ struct timeval to;
+
+ /* Check if we still have a valid (non-EOF) connection. From Andrew
+ * Maholski's code in the Unix Socket FAQ. */
+
+ FD_ZERO (&check_set);
+ FD_SET (sock, &check_set);
+
+ /* Wait one microsecond */
+ to.tv_sec = 0;
+ to.tv_usec = 1;
+
+ /* If we get a timeout, then that means still connected */
+ if (select (sock + 1, &check_set, NULL, NULL, &to) == 0)
+ {
+ /* Connection is valid (not EOF), so continue */
+ return 1;
+ }
+ else
+ return 0;
+#else
+ /* Without select, it's hard to know for sure. */
+ return 1;
+#endif
+}
+
/* Bind the local port PORT. This does all the necessary work, which
is creating a socket, setting SO_REUSEADDR option on it, then
calling bind() and listen(). If *PORT is 0, a random port is
if (setsockopt (msock, SOL_SOCKET, SO_REUSEADDR,
(char *)&optval, sizeof (optval)) < 0)
return CONSOCKERR;
- srv.sin_family = AF_INET;
- srv.sin_addr.s_addr = htonl (INADDR_ANY);
+
+ if (opt.bind_address == NULL)
+ {
+ srv.sin_family = AF_INET;
+ srv.sin_addr.s_addr = htonl (INADDR_ANY);
+ }
+ else
+ srv = *opt.bind_address;
+
srv.sin_port = htons (*port);
if (bind (msock, addr, sizeof (struct sockaddr_in)) < 0)
{
Returns 1 if FD is accessible, 0 for timeout and -1 for error in
select(). */
-static int
+int
select_fd (int fd, int maxtime, int writep)
{
fd_set fds, exceptfds;