/*
- * $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
#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 *);
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"
" -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"
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;
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;
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;
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);
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)
{