--------------------- PatchSet 5231 Date: 2007/08/10 07:54:03 Author: amosjeffries Branch: squid3-ipv6 Tag: (none) Log: squidclient fixes: - SegFault: gethostbyname can fail. Use protected IPAddress:: version - Add VERSION to usage display of squidclient - Protocol unsupported. Use addrinfo here too. Members: tools/squidclient.cc:1.5.4.4->1.5.4.5 Index: squid3/tools/squidclient.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/tools/squidclient.cc,v retrieving revision 1.5.4.4 retrieving revision 1.5.4.5 diff -u -r1.5.4.4 -r1.5.4.5 --- squid3/tools/squidclient.cc 12 Jun 2007 12:44:52 -0000 1.5.4.4 +++ squid3/tools/squidclient.cc 10 Aug 2007 07:54:03 -0000 1.5.4.5 @@ -1,6 +1,6 @@ /* - * $Id: squidclient.cc,v 1.5.4.4 2007/06/12 12:44:52 amosjeffries Exp $ + * $Id: squidclient.cc,v 1.5.4.5 2007/08/10 07:54:03 amosjeffries Exp $ * * DEBUG: section 0 WWW Client * AUTHOR: Harvest Derived @@ -93,9 +93,9 @@ typedef void SIGHDLR(int sig); /* Local functions */ -static int client_comm_bind(int, const char *); +static int client_comm_bind(int, const IPAddress &); -static int client_comm_connect(int, const char *, u_short, struct timeval *); +static int client_comm_connect(int, const IPAddress &, struct timeval *); static void usage(const char *progname); static int Now(struct timeval *); @@ -115,6 +115,7 @@ usage(const char *progname) { fprintf(stderr, + "Version: %s\n" "Usage: %s [-arsv] [-i IMS] [-h remote host] [-l local host] [-p port] [-m method] [-t count] [-I ping-interval] [-H 'strings'] [-T timeout] url\n" "Options:\n" " -P file PUT request.\n" @@ -136,7 +137,7 @@ " -w password Proxy authentication password\n" " -U user WWW authentication username\n" " -W password WWW authentication password\n", - progname, CACHE_HTTP_PORT); + VERSION, progname, CACHE_HTTP_PORT); exit(1); } @@ -151,6 +152,7 @@ int opt_noaccept = 0; int opt_verbose = 0; const char *hostname, *localhost; + IPAddress iaddr; char url[BUFSIZ], msg[49152], buf[BUFSIZ]; char extra_hdrs[32768]; const char *method = "GET"; @@ -451,29 +453,46 @@ for (i = 0; loops == 0 || i < loops; i++) { int fsize = 0; + struct addrinfo *AI = NULL; + /* Connect to the server */ -#if USE_IPV6 - if ((conn = socket(PF_INET, SOCK_STREAM, 0)) < 0) { -#else - if ((conn = socket(PF_INET6, SOCK_STREAM, 0)) < 0) { -#endif + if(localhost) { + if( !iaddr.GetHostByName(localhost) ) { + fprintf(stderr, "client: ERROR: Cannot resolve %s: Host unknown.\n", localhost); + exit(1); + } + } + + iaddr.GetAddrInfo(AI); + + if ((conn = socket(AI->ai_family, AI->ai_socktype, 0)) < 0) { perror("client: socket"); + iaddr.FreeAddrInfo(AI); exit(1); } - if (localhost && client_comm_bind(conn, localhost) < 0) { + iaddr.FreeAddrInfo(AI); + + if (localhost && client_comm_bind(conn, iaddr) < 0) { perror("client: bind"); exit(1); } - if (client_comm_connect(conn, hostname, port, ping ? &tv1 : NULL) < 0) { + iaddr.SetEmpty(); + + iaddr.GetHostByName(hostname); + + iaddr.SetPort(port); + + if (client_comm_connect(conn, iaddr, ping ? &tv1 : NULL) < 0) { + char buf[MAX_IPSTRLEN]; + iaddr.ToURL(buf, MAX_IPSTRLEN); if (errno == 0) { - fprintf(stderr, "client: ERROR: Cannot connect to %s:%d: Host unknown.\n", hostname, port); + fprintf(stderr, "client: ERROR: Cannot connect to %s: Host unknown.\n", buf); } else { char tbuf[BUFSIZ]; - snprintf(tbuf, BUFSIZ, "client: ERROR: Cannot connect to %s:%d", - hostname, port); + snprintf(tbuf, BUFSIZ, "client: ERROR: Cannot connect to %s", buf); perror(tbuf); } @@ -588,21 +607,14 @@ } static int -client_comm_bind(int sock, const char *local_host) { +client_comm_bind(int sock, const IPAddress &addr) { int res; static struct addrinfo *AI = NULL; - IPAddress addr; - /* Set up the source socket address from which to send. */ - addr = *gethostbyname(local_host); - - if(addr.IsAnyAddr()) - return (-1); - addr.GetAddrInfo(AI); res = bind(sock, AI->ai_addr, AI->ai_addrlen); @@ -613,23 +625,14 @@ } static int -client_comm_connect(int sock, const char *dest_host, u_short dest_port, struct timeval *tvp) { +client_comm_connect(int sock, const IPAddress &addr, struct timeval *tvp) { int res; static struct addrinfo *AI = NULL; - IPAddress addr; - /* Set up the destination socket address for message to send to. */ - addr = *gethostbyname(dest_host); - - if(addr.IsAnyAddr()) - return (-1); - - addr.SetPort(dest_port); - addr.GetAddrInfo(AI); if (tvp)