Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/ftp * implement parseport(), which takes a string an...



details:   https://anonhg.NetBSD.org/src/rev/0f006516d409
branches:  trunk
changeset: 495784:0f006516d409
user:      lukem <lukem%NetBSD.org@localhost>
date:      Sun Aug 06 08:51:22 2000 +0000

description:
* implement parseport(), which takes a string and attempts to convert
  it to a numeric port number
* use parseport() in parse_url() and hookup()
* don't try and lookup the port number using getaddrinfo(), as it's too hard
  to separate a failed host name lookup from a failed service name lookup.
  this was causing lossage on systems that don't have `http' in services(5)
  (such as solaris), but only crept in when we started using getaddrinfo()
  unconditionally.

diffstat:

 usr.bin/ftp/extern.h  |   3 ++-
 usr.bin/ftp/fetch.c   |  26 +++++++++-----------------
 usr.bin/ftp/ftp.c     |  12 +++++++-----
 usr.bin/ftp/util.c    |  37 +++++++++++++++++++++++++++++++++++--
 usr.bin/ftp/version.h |   4 ++--
 5 files changed, 55 insertions(+), 27 deletions(-)

diffs (203 lines):

diff -r fd48db15aa2d -r 0f006516d409 usr.bin/ftp/extern.h
--- a/usr.bin/ftp/extern.h      Sun Aug 06 07:05:50 2000 +0000
+++ b/usr.bin/ftp/extern.h      Sun Aug 06 08:51:22 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.58 2000/07/18 07:16:53 lukem Exp $        */
+/*     $NetBSD: extern.h,v 1.59 2000/08/06 08:51:22 lukem Exp $        */
 
 /*-
  * Copyright (c) 1996-2000 The NetBSD Foundation, Inc.
@@ -180,6 +180,7 @@
 void   opts(int, char **);
 void   newer(int, char **);
 void   page(int, char **);
+int    parseport(const char *, int);
 int    parserate(int, char **, int);
 void   progressmeter(int);
 char   *prompt(void);
diff -r fd48db15aa2d -r 0f006516d409 usr.bin/ftp/fetch.c
--- a/usr.bin/ftp/fetch.c       Sun Aug 06 07:05:50 2000 +0000
+++ b/usr.bin/ftp/fetch.c       Sun Aug 06 08:51:22 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fetch.c,v 1.121 2000/08/01 22:47:27 lukem Exp $        */
+/*     $NetBSD: fetch.c,v 1.122 2000/08/06 08:51:22 lukem Exp $        */
 
 /*-
  * Copyright (c) 1997-2000 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: fetch.c,v 1.121 2000/08/01 22:47:27 lukem Exp $");
+__RCSID("$NetBSD: fetch.c,v 1.122 2000/08/06 08:51:22 lukem Exp $");
 #endif /* not lint */
 
 /*
@@ -386,20 +386,10 @@
        if (cp != NULL) {
                long    nport;
 
-               nport = strtol(cp, &ep, 10);
-               if (*ep != '\0' && ep == cp) {
-                       struct servent  *svp;
-
-                       svp = getservbyname(cp, "tcp");
-                       if (svp == NULL) {
-                               warnx("Unknown port `%s' in %s `%s'",
-                                   cp, desc, origurl);
-                               goto cleanup_parse_url;
-                       } else
-                               nport = ntohs(svp->s_port);
-               } else if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
-                       warnx("Invalid port `%s' in %s `%s'", cp, desc,
-                           origurl);
+               nport = parseport(cp, -1);
+               if (nport == -1) {
+                       warnx("Unknown port `%s' in %s `%s'",
+                           cp, desc, origurl);
                        goto cleanup_parse_url;
                }
                *portnum = nport;
@@ -656,7 +646,7 @@
                hints.ai_family = AF_UNSPEC;
                hints.ai_socktype = SOCK_STREAM;
                hints.ai_protocol = 0;
-               error = getaddrinfo(host, port, &hints, &res0);
+               error = getaddrinfo(host, NULL, &hints, &res0);
                if (error) {
                        warnx("%s", gai_strerror(error));
                        goto cleanup_fetch_url;
@@ -678,6 +668,8 @@
                        if (verbose && res != res0)
                                fprintf(ttyout, "Trying %s...\n", hbuf);
 
+                       ((struct sockaddr_in *)res->ai_addr)->sin_port =
+                           htons(portnum);
                        s = socket(res->ai_family, res->ai_socktype,
                                res->ai_protocol);
                        if (s < 0) {
diff -r fd48db15aa2d -r 0f006516d409 usr.bin/ftp/ftp.c
--- a/usr.bin/ftp/ftp.c Sun Aug 06 07:05:50 2000 +0000
+++ b/usr.bin/ftp/ftp.c Sun Aug 06 08:51:22 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ftp.c,v 1.107 2000/08/01 22:47:28 lukem Exp $  */
+/*     $NetBSD: ftp.c,v 1.108 2000/08/06 08:51:22 lukem Exp $  */
 
 /*-
  * Copyright (c) 1996-2000 The NetBSD Foundation, Inc.
@@ -103,7 +103,7 @@
 #if 0
 static char sccsid[] = "@(#)ftp.c      8.6 (Berkeley) 10/27/94";
 #else
-__RCSID("$NetBSD: ftp.c,v 1.107 2000/08/01 22:47:28 lukem Exp $");
+__RCSID("$NetBSD: ftp.c,v 1.108 2000/08/06 08:51:22 lukem Exp $");
 #endif
 #endif /* not lint */
 
@@ -169,20 +169,21 @@
 char *
 hookup(char *host, char *port)
 {
-       int s = -1, len, error;
+       int s = -1, len, error, portnum;
        struct addrinfo hints, *res, *res0;
        char hbuf[MAXHOSTNAMELEN];
        static char hostnamebuf[MAXHOSTNAMELEN];
        char *cause = "unknown";
-       
+
        memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
        memset((char *)&myctladdr, 0, sizeof (myctladdr));
        memset(&hints, 0, sizeof(hints));
+       portnum = parseport(port, FTP_PORT);
        hints.ai_flags = AI_CANONNAME;
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = 0;
-       error = getaddrinfo(host, port, &hints, &res0);
+       error = getaddrinfo(host, NULL, &hints, &res0);
        if (error) {
                warnx("%s", gai_strerror(error));
                code = -1;
@@ -226,6 +227,7 @@
                            hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST);
                        fprintf(ttyout, "Trying %s...\n", hbuf);
                }
+               ((struct sockaddr_in *)res->ai_addr)->sin_port = htons(portnum);
                s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
                if (s < 0) {
                        cause = "socket";
diff -r fd48db15aa2d -r 0f006516d409 usr.bin/ftp/util.c
--- a/usr.bin/ftp/util.c        Sun Aug 06 07:05:50 2000 +0000
+++ b/usr.bin/ftp/util.c        Sun Aug 06 08:51:22 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: util.c,v 1.99 2000/08/01 22:47:29 lukem Exp $  */
+/*     $NetBSD: util.c,v 1.100 2000/08/06 08:51:22 lukem Exp $ */
 
 /*-
  * Copyright (c) 1997-2000 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: util.c,v 1.99 2000/08/01 22:47:29 lukem Exp $");
+__RCSID("$NetBSD: util.c,v 1.100 2000/08/06 08:51:22 lukem Exp $");
 #endif /* not lint */
 
 /*
@@ -1425,6 +1425,39 @@
 }
 
 /*
+ * Parse `port' into a TCP port number, defaulting to `defport' if `port' is
+ * an unknown service name. If defport != -1, print a warning upon bad parse.
+ */
+int
+parseport(const char *port, int defport)
+{
+       int      rv;
+       long     nport;
+       char    *p, *ep;
+
+       p = xstrdup(port);
+       nport = strtol(p, &ep, 10);
+       if (*ep != '\0' && ep == p) {
+               struct servent  *svp;
+
+               svp = getservbyname(port, "tcp");
+               if (svp == NULL) {
+ badparseport:
+                       if (defport != -1)
+                               warnx("Unknown port `%s', using port %d",
+                                   port, defport);
+                       rv = defport;
+               } else
+                       rv = ntohs(svp->s_port);
+       } else if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0')
+               goto badparseport;
+       else
+               rv = nport;
+       free(p);
+       return (rv);
+}
+
+/*
  * Determine if given string is an IPv6 address or not.
  * Return 1 for yes, 0 for no
  */
diff -r fd48db15aa2d -r 0f006516d409 usr.bin/ftp/version.h
--- a/usr.bin/ftp/version.h     Sun Aug 06 07:05:50 2000 +0000
+++ b/usr.bin/ftp/version.h     Sun Aug 06 08:51:22 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: version.h,v 1.19 2000/08/01 22:47:30 lukem Exp $       */
+/*     $NetBSD: version.h,v 1.20 2000/08/06 08:51:23 lukem Exp $       */
 /*-
  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -40,5 +40,5 @@
 #endif
 
 #ifndef FTP_VERSION
-#define        FTP_VERSION     "20000802"
+#define        FTP_VERSION     "20000806"
 #endif



Home | Main Index | Thread Index | Old Index