From: wessels <> Date: Thu, 8 Apr 1999 04:06:45 +0000 (+0000) Subject: command line option to specify local socket address X-Git-Tag: SQUID_3_0_PRE1~2295 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2c08acd943d5053852041b3e5d5ced892db552c0;p=thirdparty%2Fsquid.git command line option to specify local socket address --- diff --git a/src/client.cc b/src/client.cc index aae534a7a1..ccab450c7e 100644 --- a/src/client.cc +++ b/src/client.cc @@ -2,7 +2,7 @@ /* - * $Id: client.cc,v 1.81 1999/01/24 02:26:22 wessels Exp $ + * $Id: client.cc,v 1.82 1999/04/07 22:06:45 wessels Exp $ * * DEBUG: section 0 WWW Client * AUTHOR: Harvest Derived @@ -42,6 +42,7 @@ #endif /* Local functions */ +static int client_comm_bind(int, char *); static int client_comm_connect(int, char *, u_short, struct timeval *); static void usage(const char *progname); static int Now(struct timeval *); @@ -57,7 +58,7 @@ static void usage(const char *progname) { fprintf(stderr, - "Usage: %s [-arsv] [-i IMS] [-h host] [-p port] [-m method] [-t count] [-I ping-interval] [-H 'strings'] url\n" + "Usage: %s [-arsv] [-i IMS] [-h remote host] [-l local host] [-p port] [-m method] [-t count] [-I ping-interval] [-H 'strings'] url\n" "Options:\n" " -P file PUT request.\n" " -a Do NOT include Accept: header.\n" @@ -66,6 +67,7 @@ usage(const char *progname) " -v Verbose. Print outgoing message to stderr.\n" " -i IMS If-Modified-Since time (in Epoch seconds).\n" " -h host Retrieve URL from cache on hostname. Default is localhost.\n" + " -l host Specify a local IP address to bind to. Default is none.\n" " -p port Port number of cache. Default is %d.\n" " -m method Request method, default is GET.\n" " -t count Trace count cache-hops\n" @@ -87,7 +89,8 @@ main(int argc, char *argv[]) int opt_noaccept = 0; int opt_put = 0; int opt_verbose = 0; - char url[BUFSIZ], msg[BUFSIZ], buf[BUFSIZ], hostname[BUFSIZ]; + char *hostname, *localhost; + char url[BUFSIZ], msg[BUFSIZ], buf[BUFSIZ]; char extra_hdrs[BUFSIZ]; const char *method = "GET"; extern char *optarg; @@ -99,7 +102,8 @@ main(int argc, char *argv[]) long ping_min = 0, ping_max = 0, ping_sum = 0, ping_mean = 0; /* set the defaults */ - strcpy(hostname, "localhost"); + hostname = "localhost"; + localhost = NULL; extra_hdrs[0] = '\0'; port = CACHE_HTTP_PORT; to_stdout = 1; @@ -114,14 +118,18 @@ main(int argc, char *argv[]) strcpy(url, argv[argc - 1]); if (url[0] == '-') usage(argv[0]); - while ((c = getopt(argc, argv, "ah:P:i:km:p:rsvt:g:p:I:H:?")) != -1) + while ((c = getopt(argc, argv, "ah:l:P:i:km:p:rsvt:g:p:I:H:?")) != -1) switch (c) { case 'a': opt_noaccept = 1; break; - case 'h': /* host:arg */ + case 'h': /* remote host */ if (optarg != NULL) - strcpy(hostname, optarg); + hostname = optarg; + break; + case 'l': /* local host */ + if (optarg != NULL) + localhost = optarg; break; case 's': /* silent */ to_stdout = 0; @@ -252,6 +260,10 @@ main(int argc, char *argv[]) perror("client: socket"); exit(1); } + if (localhost && client_comm_bind(conn, localhost) < 0) { + perror("client: bind"); + exit(1); + } if (client_comm_connect(conn, hostname, port, ping ? &tv1 : NULL) < 0) { if (errno == 0) { fprintf(stderr, "client: ERROR: Cannot connect to %s:%d: Host unknown.\n", hostname, port); @@ -337,6 +349,25 @@ main(int argc, char *argv[]) return 0; } +static int +client_comm_bind(int sock, char *local_host) +{ + static const struct hostent *hp = NULL; + static struct sockaddr_in from_addr; + + /* Set up the source socket address from which to send. */ + if (hp == NULL) { + from_addr.sin_family = AF_INET; + + if ((hp = gethostbyname(local_host)) == 0) { + return (-1); + } + xmemcpy(&from_addr.sin_addr, hp->h_addr, hp->h_length); + from_addr.sin_port = 0; + } + return bind(sock, (struct sockaddr *) &from_addr, sizeof(struct sockaddr_in)); +} + static int client_comm_connect(int sock, char *dest_host, u_short dest_port, struct timeval *tvp) {