diff options
author | Glenn L McGrath | 2003-12-20 01:47:18 +0000 |
---|---|---|
committer | Glenn L McGrath | 2003-12-20 01:47:18 +0000 |
commit | ffccf6eb5de311a3db8c3d7f7496e2f0cad69a23 (patch) | |
tree | 859f5849c30de6cb69bf6336af6d2228402f2395 | |
parent | 03d8091859f45a6bb5e3aadc110b279e789399f2 (diff) | |
download | busybox-ffccf6eb5de311a3db8c3d7f7496e2f0cad69a23.zip busybox-ffccf6eb5de311a3db8c3d7f7496e2f0cad69a23.tar.gz |
Change interface to bb_lookup_host, dont try and set port inside this
function as there is no gracefull way of handling failures.
Rename bb_getport to bb_lookup_port, allow a default port to be
specified so it always returns a correct value.
Modify ftpgetput/rdate/wget to use the new interface.
wget/rdate now use etc/services with a falback default value.
-rw-r--r-- | include/libbb.h | 4 | ||||
-rw-r--r-- | libbb/xconnect.c | 33 | ||||
-rw-r--r-- | networking/ftpgetput.c | 3 | ||||
-rw-r--r-- | networking/telnet.c | 10 | ||||
-rw-r--r-- | networking/wget.c | 35 | ||||
-rw-r--r-- | util-linux/rdate.c | 3 |
6 files changed, 39 insertions, 49 deletions
diff --git a/include/libbb.h b/include/libbb.h index 1367ee6..b2592c8 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -297,8 +297,8 @@ extern struct hostent *xgethostbyname2(const char *name, int af); extern int create_icmp_socket(void); extern int create_icmp6_socket(void); extern int xconnect(struct sockaddr_in *s_addr); -extern int bb_getport(const char *port); -extern void bb_lookup_host(struct sockaddr_in *s_in, const char *host, const char *port); +extern unsigned short bb_lookup_port(const char *port, unsigned short default_port); +extern void bb_lookup_host(struct sockaddr_in *s_in, const char *host); //#warning wrap this? char *dirname (char *path); diff --git a/libbb/xconnect.c b/libbb/xconnect.c index 2cbc840..b3619fd 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c @@ -18,30 +18,31 @@ #include <arpa/inet.h> #include "libbb.h" -int bb_getport(const char *port) +/* Return network byte ordered port number for a service. + * If "port" is a number use it as the port. + * If "port" is a name it is looked up in /etc/services, if it isnt found return + * default_port + */ +unsigned short bb_lookup_port(const char *port, unsigned short default_port) { - int port_nr; + unsigned short port_nr = htons(default_port); + if (port) { char *endptr; - struct servent *tserv; + long port_long = strtol(port, &endptr, 10); - if (!port) { - return -1; - } - port_nr=strtol(port, &endptr, 10); - if (errno != 0 || *endptr!='\0' || endptr==port || port_nr < 1 || port_nr > 65536) - { - if (port_nr==0 && (tserv = getservbyname(port, "tcp")) != NULL) { + if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) { + struct servent *tserv = getservbyname(port, "tcp"); + if (tserv) { port_nr = tserv->s_port; - } else { - return -1; } } else { - port_nr = htons(port_nr); + port_nr = htons(port_long); + } } return port_nr; } -void bb_lookup_host(struct sockaddr_in *s_in, const char *host, const char *port) +void bb_lookup_host(struct sockaddr_in *s_in, const char *host) { struct hostent *he; @@ -49,10 +50,6 @@ void bb_lookup_host(struct sockaddr_in *s_in, const char *host, const char *port s_in->sin_family = AF_INET; he = xgethostbyname(host); memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length); - - if (port) { - s_in->sin_port=bb_getport(port); - } } int xconnect(struct sockaddr_in *s_addr) diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c index 8724739..cc7a2a7 100644 --- a/networking/ftpgetput.c +++ b/networking/ftpgetput.c @@ -356,7 +356,8 @@ int ftpgetput_main(int argc, char **argv) * and we want to connect to only one IP... */ server->s_in = &s_in; server->host = argv[optind]; - bb_lookup_host(&s_in, server->host, NULL); + bb_lookup_host(&s_in, server->host); + s_in.sin_port = bb_lookup_port(server->port, 21); if (verbose_flag) { fprintf(stdout, "Connecting to %s[%s]:%s\n", server->host, inet_ntoa(s_in.sin_addr), server->port); diff --git a/networking/telnet.c b/networking/telnet.c index 92ddfae..110c9d1 100644 --- a/networking/telnet.c +++ b/networking/telnet.c @@ -570,8 +570,6 @@ static void cookmode(void) extern int telnet_main(int argc, char** argv) { - char *host; - char *port; int len; struct sockaddr_in s_in; #ifdef USE_POLL @@ -597,12 +595,12 @@ extern int telnet_main(int argc, char** argv) G.termios_raw = G.termios_def; cfmakeraw(&G.termios_raw); - if (argc < 2) bb_show_usage(); - port = (argc > 2)? argv[2] : "23"; + if (argc < 2) + bb_show_usage(); - host = argv[1]; + bb_lookup_host(&s_in, argv[1]); + s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", 23); - bb_lookup_host(&s_in, host, port); G.netfd = xconnect(&s_in); setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one); diff --git a/networking/wget.c b/networking/wget.c index 3e74ed3..9f5dbaf 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -40,7 +40,7 @@ struct host_info { }; static void parse_url(char *url, struct host_info *h); -static FILE *open_socket(struct sockaddr_in *s_in, int port); +static FILE *open_socket(struct sockaddr_in *s_in); static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc); static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf); @@ -286,10 +286,11 @@ int wget_main(int argc, char **argv) /* We want to do exactly _one_ DNS lookup, since some * sites (i.e. ftp.us.debian.org) use round-robin DNS * and we want to connect to only one IP... */ - bb_lookup_host(&s_in, server.host, NULL); + bb_lookup_host(&s_in, server.host); + s_in.sin_port = server.port; if (quiet_flag==FALSE) { fprintf(stdout, "Connecting to %s[%s]:%d\n", - server.host, inet_ntoa(s_in.sin_addr), server.port); + server.host, inet_ntoa(s_in.sin_addr), ntohs(server.port)); } if (proxy || !target.is_ftp) { @@ -306,7 +307,7 @@ int wget_main(int argc, char **argv) * Open socket to http server */ if (sfp) fclose(sfp); - sfp = open_socket(&s_in, server.port); + sfp = open_socket(&s_in); /* * Send HTTP request. @@ -418,7 +419,7 @@ read_response: if (! target.user) target.user = bb_xstrdup("anonymous:busybox@"); - sfp = open_socket(&s_in, server.port); + sfp = open_socket(&s_in); if (ftpcmd(NULL, NULL, sfp, buf) != 220) close_delete_and_die("%s", buf+4); @@ -461,7 +462,8 @@ read_response: port = atoi(s+1); s = strrchr(buf, ','); port += atoi(s+1) * 256; - dfp = open_socket(&s_in, port); + s_in.sin_port = htons(port); + dfp = open_socket(&s_in); if (do_continue) { sprintf(buf, "REST %ld", beg_range); @@ -535,11 +537,11 @@ void parse_url(char *url, struct host_info *h) char *cp, *sp, *up, *pp; if (strncmp(url, "http://", 7) == 0) { - h->port = 80; + h->port = bb_lookup_port("http", 80); h->host = url + 7; h->is_ftp = 0; } else if (strncmp(url, "ftp://", 6) == 0) { - h->port = 21; + h->port = bb_lookup_port("ftp", 21); h->host = url + 6; h->is_ftp = 1; } else @@ -586,21 +588,12 @@ void parse_url(char *url, struct host_info *h) } -FILE *open_socket(struct sockaddr_in *s_in, int port) +FILE *open_socket(struct sockaddr_in *s_in) { - int fd; FILE *fp; - if (port>0 && port < 65536) { - s_in->sin_port=htons(port); - } - - fd=xconnect(s_in); - - /* - * Get the server onto a stdio stream. - */ - if ((fp = fdopen(fd, "r+")) == NULL) + fp = fdopen(xconnect(s_in), "r+"); + if (fp == NULL) bb_perror_msg_and_die("fdopen()"); return fp; @@ -842,7 +835,7 @@ progressmeter(int flag) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: wget.c,v 1.62 2003/12/19 12:08:56 bug1 Exp $ + * $Id: wget.c,v 1.63 2003/12/20 01:47:18 bug1 Exp $ */ diff --git a/util-linux/rdate.c b/util-linux/rdate.c index 0b5f8e4..a317cda 100644 --- a/util-linux/rdate.c +++ b/util-linux/rdate.c @@ -50,7 +50,8 @@ static time_t askremotedate(const char *host) struct sockaddr_in s_in; int fd; - bb_lookup_host(&s_in, host, "time"); + bb_lookup_host(&s_in, host); + s_in.sin_port = bb_lookup_port("time", 37); /* Add a timeout for dead or non accessable servers */ alarm(10); |