1
0
mirror of https://github.com/moparisthebest/wget synced 2024-07-03 16:38:41 -04:00

[svn] Rewrote bindport and acceptport to use a more standard calling convention.

This commit is contained in:
hniksic 2003-11-13 09:05:06 -08:00
parent a0bae929ea
commit 0f99d1f220
6 changed files with 82 additions and 51 deletions

View File

@ -1,3 +1,10 @@
2003-11-13 Hrvoje Niksic <hniksic@xemacs.org>
* connect.c (bind_local): Renamed bindport to bind_local; return
the socket directly. Updated callers.
(accept_connection): Renamed acceptport to accept_connection;
return the created socket directly. Updated callers.
2003-11-13 Hrvoje Niksic <hniksic@xemacs.org> 2003-11-13 Hrvoje Niksic <hniksic@xemacs.org>
* init.c (defaults): Turn on opt.ipv4_only if we're compiling with * init.c (defaults): Turn on opt.ipv4_only if we're compiling with

View File

@ -404,12 +404,21 @@ test_socket_open (int sock)
#endif #endif
} }
/* Create a socket and bind it to PORT locally. Calling accept() on /* Create a socket, bind it to local interface BIND_ADDRESS on port
such a socket waits for and accepts incoming TCP connections. The *PORT, set up a listen backlog, and return the resulting socket, or
resulting socket is stored to LOCAL_SOCK. */ -1 in case of error.
uerr_t BIND_ADDRESS is the address of the interface to bind to. If it is
bindport (const ip_address *bind_address, int *port, int *local_sock) NULL, the socket is bound to the default address. PORT should
point to the port number that will be used for the binding. If
that number is 0, the system will choose a suitable port, and the
chosen value will be written to *PORT.
Calling accept() on such a socket waits for and accepts incoming
TCP connections. */
int
bind_local (const ip_address *bind_address, int *port)
{ {
int sock; int sock;
int family = AF_INET; int family = AF_INET;
@ -426,8 +435,9 @@ bindport (const ip_address *bind_address, int *port, int *local_sock)
family = AF_INET6; family = AF_INET6;
#endif #endif
if ((sock = socket (family, SOCK_STREAM, 0)) < 0) sock = socket (family, SOCK_STREAM, 0);
return CONSOCKERR; if (sock < 0)
return -1;
#ifdef SO_REUSEADDR #ifdef SO_REUSEADDR
setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, setopt_ptr, setopt_size); setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, setopt_ptr, setopt_size);
@ -445,16 +455,22 @@ bindport (const ip_address *bind_address, int *port, int *local_sock)
if (bind (sock, sa, sockaddr_size (sa)) < 0) if (bind (sock, sa, sockaddr_size (sa)) < 0)
{ {
xclose (sock); xclose (sock);
return BINDERR; return -1;
} }
DEBUGP (("Local socket fd %d bound.\n", sock)); DEBUGP (("Local socket fd %d bound.\n", sock));
if (!*port)
/* If *PORT is 0, find out which port we've bound to. */
if (*port == 0)
{ {
socklen_t sa_len = sockaddr_size (sa); socklen_t sa_len = sockaddr_size (sa);
if (getsockname (sock, sa, &sa_len) < 0) if (getsockname (sock, sa, &sa_len) < 0)
{ {
/* If we can't find out the socket's local address ("name"),
something is seriously wrong with the socket, and it's
unusable for us anyway because we must know the chosen
port. */
xclose (sock); xclose (sock);
return CONPORTERR; return -1;
} }
sockaddr_get_data (sa, NULL, port); sockaddr_get_data (sa, NULL, port);
DEBUGP (("binding to address %s using port %i.\n", DEBUGP (("binding to address %s using port %i.\n",
@ -463,10 +479,9 @@ bindport (const ip_address *bind_address, int *port, int *local_sock)
if (listen (sock, 1) < 0) if (listen (sock, 1) < 0)
{ {
xclose (sock); xclose (sock);
return LISTENERR; return -1;
} }
*local_sock = sock; return sock;
return BINDOK;
} }
#ifdef HAVE_SELECT #ifdef HAVE_SELECT
@ -504,27 +519,40 @@ select_fd (int fd, double maxtime, int wait_for)
} }
#endif /* HAVE_SELECT */ #endif /* HAVE_SELECT */
/* Accept a connection on LOCAL_SOCK, and store the new socket to /* Like a call to accept(), but with the added check for timeout.
*SOCK. It blocks the caller until a connection is established. If
no connection is established for opt.connect_timeout seconds, the In other words, accept a client connection on LOCAL_SOCK, and
return the new socket used for communication with the client.
LOCAL_SOCK should have been bound, e.g. using bind_local().
The caller is blocked until a connection is established. If no
connection is established for opt.connect_timeout seconds, the
function exits with an error status. */ function exits with an error status. */
uerr_t int
acceptport (int local_sock, int *sock) accept_connection (int local_sock)
{ {
int sock;
/* We don't need the values provided by accept, but accept
apparently requires them to be present. */
struct sockaddr_storage ss; struct sockaddr_storage ss;
struct sockaddr *sa = (struct sockaddr *)&ss; struct sockaddr *sa = (struct sockaddr *)&ss;
socklen_t addrlen = sizeof (ss); socklen_t addrlen = sizeof (ss);
#ifdef HAVE_SELECT #ifdef HAVE_SELECT
if (opt.connect_timeout) if (opt.connect_timeout)
if (select_fd (local_sock, opt.connect_timeout, WAIT_FOR_READ) <= 0) {
return ACCEPTERR; int test = select_fd (local_sock, opt.connect_timeout, WAIT_FOR_READ);
if (test == 0)
errno = ETIMEDOUT;
if (test <= 0)
return -1;
}
#endif #endif
if ((*sock = accept (local_sock, sa, &addrlen)) < 0) sock = accept (local_sock, sa, &addrlen);
return ACCEPTERR; DEBUGP (("Accepted client at socket %d.\n", sock));
DEBUGP (("Created socket fd %d.\n", *sock)); return sock;
return ACCEPTOK;
} }
/* Get the IP address associated with the connection on FD and store /* Get the IP address associated with the connection on FD and store

View File

@ -47,8 +47,8 @@ enum {
int connect_to_host PARAMS ((const char *, int)); int connect_to_host PARAMS ((const char *, int));
int connect_to_ip PARAMS ((const ip_address *, int, const char *)); int connect_to_ip PARAMS ((const ip_address *, int, const char *));
uerr_t bindport PARAMS ((const ip_address *, int *, int *)); int bind_local PARAMS ((const ip_address *, int *));
uerr_t acceptport PARAMS ((int, int *)); int accept_connection PARAMS ((int));
enum { enum {
ENDPOINT_LOCAL, ENDPOINT_LOCAL,

View File

@ -286,7 +286,7 @@ ftp_port (struct rbuf *rbuf, int *local_sock)
/* Get the address of this side of the connection. */ /* Get the address of this side of the connection. */
if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL)) if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL))
return BINDERR; return FTPSYSERR;
assert (addr.type == IPV4_ADDRESS); assert (addr.type == IPV4_ADDRESS);
@ -294,9 +294,9 @@ ftp_port (struct rbuf *rbuf, int *local_sock)
port = 0; port = 0;
/* Bind the port. */ /* Bind the port. */
err = bindport (&addr, &port, local_sock); *local_sock = bind_local (&addr, &port);
if (err != BINDOK) if (*local_sock < 0)
return err; return FTPSYSERR;
/* Construct the argument of PORT (of the form a,b,c,d,e,f). */ /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
ip_address_to_port_repr (&addr, port, bytes, sizeof (bytes)); ip_address_to_port_repr (&addr, port, bytes, sizeof (bytes));
@ -383,7 +383,7 @@ ftp_lprt (struct rbuf *rbuf, int *local_sock)
/* Get the address of this side of the connection. */ /* Get the address of this side of the connection. */
if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL)) if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL))
return BINDERR; return FTPSYSERR;
assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS); assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
@ -391,9 +391,9 @@ ftp_lprt (struct rbuf *rbuf, int *local_sock)
port = 0; port = 0;
/* Bind the port. */ /* Bind the port. */
err = bindport (&addr, &port, local_sock); *local_sock = bind_local (&addr, &port);
if (err != BINDOK) if (*local_sock < 0)
return err; return FTPSYSERR;
/* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */ /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes)); ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes));
@ -467,7 +467,7 @@ ftp_eprt (struct rbuf *rbuf, int *local_sock)
/* Get the address of this side of the connection. */ /* Get the address of this side of the connection. */
if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL)) if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL))
return BINDERR; return FTPSYSERR;
assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS); assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
@ -475,9 +475,9 @@ ftp_eprt (struct rbuf *rbuf, int *local_sock)
port = 0; port = 0;
/* Bind the port. */ /* Bind the port. */
err = bindport (&addr, &port, local_sock); *local_sock = bind_local (&addr, &port);
if (err != BINDOK) if (*local_sock < 0)
return err; return FTPSYSERR;
/* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */ /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
ip_address_to_eprt_repr (&addr, port, bytes, sizeof (bytes)); ip_address_to_eprt_repr (&addr, port, bytes, sizeof (bytes));

View File

@ -705,8 +705,8 @@ Error in server response, closing control connection.\n"));
if (!pasv_mode_open) /* Try to use a port command if PASV failed */ if (!pasv_mode_open) /* Try to use a port command if PASV failed */
{ {
err = ftp_do_port (&con->rbuf, &local_sock); err = ftp_do_port (&con->rbuf, &local_sock);
/* FTPRERR, WRITEFAILED, bindport (CONSOCKERR, CONPORTERR, BINDERR, /* FTPRERR, WRITEFAILED, bindport (FTPSYSERR), HOSTERR,
LISTENERR), HOSTERR, FTPPORTERR */ FTPPORTERR */
switch (err) switch (err)
{ {
case FTPRERR: case FTPRERR:
@ -738,13 +738,11 @@ Error in server response, closing control connection.\n"));
rbuf_uninitialize (&con->rbuf); rbuf_uninitialize (&con->rbuf);
return err; return err;
break; break;
case CONPORTERR: case BINDERR: case LISTENERR: case FTPSYSERR:
/* What now? These problems are local... */
logputs (LOG_VERBOSE, "\n"); logputs (LOG_VERBOSE, "\n");
logprintf (LOG_NOTQUIET, _("Bind error (%s).\n"), logprintf (LOG_NOTQUIET, _("Bind error (%s).\n"),
strerror (errno)); strerror (errno));
xclose (dtsock); xclose (dtsock);
xclose (local_sock);
return err; return err;
break; break;
case FTPPORTERR: case FTPPORTERR:
@ -963,10 +961,10 @@ Error in server response, closing control connection.\n"));
if (!pasv_mode_open) /* we are not using pasive mode so we need if (!pasv_mode_open) /* we are not using pasive mode so we need
to accept */ to accept */
{ {
/* Open the data transmission socket by calling acceptport(). */ /* Wait for the server to connect to the address we're waiting
err = acceptport (local_sock, &dtsock); at. */
/* Possible errors: ACCEPTERR. */ dtsock = accept_connection (local_sock);
if (err == ACCEPTERR) if (dtsock < 0)
{ {
logprintf (LOG_NOTQUIET, "accept: %s\n", strerror (errno)); logprintf (LOG_NOTQUIET, "accept: %s\n", strerror (errno));
return err; return err;
@ -1261,8 +1259,7 @@ ftp_loop_internal (struct url *u, struct fileinfo *f, ccon *con)
return err; return err;
break; break;
case CONSOCKERR: case CONERROR: case FTPSRVERR: case FTPRERR: case CONSOCKERR: case CONERROR: case FTPSRVERR: case FTPRERR:
case WRITEFAILED: case FTPUNKNOWNTYPE: case CONPORTERR: case WRITEFAILED: case FTPUNKNOWNTYPE: case FTPSYSERR:
case BINDERR: case LISTENERR: case ACCEPTERR:
case FTPPORTERR: case FTPLOGREFUSED: case FTPINVPASV: case FTPPORTERR: case FTPLOGREFUSED: case FTPINVPASV:
printwhat (count, opt.ntry); printwhat (count, opt.ntry);
/* non-fatal errors */ /* non-fatal errors */

View File

@ -210,8 +210,7 @@ typedef enum
{ {
NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR, NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
CONIMPOSSIBLE, NEWLOCATION, NOTENOUGHMEM, CONPORTERR, CONIMPOSSIBLE, NEWLOCATION, NOTENOUGHMEM, CONPORTERR,
BINDERR, BINDOK, LISTENERR, ACCEPTERR, ACCEPTOK, CONCLOSED, FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR, FTPSYSERR,
CONCLOSED, FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR,
FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR, FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR,
FTPREXC, FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR, FTPREXC, FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR,
FOPENERR, FWRITEERR, HOK, HLEXC, HEOF, FOPENERR, FWRITEERR, HOK, HLEXC, HEOF,