]> sjero.net Git - wget/blobdiff - src/connect.c
[svn] Committed C. Frankel's SSL patch.
[wget] / src / connect.c
index 175db605121680096a549a5d1f9cf4e3c5c458b3..6078f599847daa4224c0652a3d45d5a26fa6ccb9 100644 (file)
@@ -87,6 +87,14 @@ make_connection (int *sock, char *hostname, unsigned short port)
   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)))
     {
@@ -99,6 +107,37 @@ make_connection (int *sock, char *hostname, unsigned short port)
   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
@@ -118,8 +157,15 @@ bindport (unsigned short *port)
   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)
     {
@@ -155,7 +201,7 @@ bindport (unsigned short *port)
 
    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;