From c81ae005f0718115c830875ab441690c794583ae Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Sun, 8 Jan 2012 12:36:27 -0800 Subject: [PATCH] ftp: Import NetBSD's latest version. Move determination of socket buffer sizes from startup to the first time a socket is used, as the previous logic assumed AF_INET sockets were available (which they may not be in an IPv6-only system). Use __dead Fix warnings. Obtained-from: NetBSD --- contrib/tnftp/cmds.c | 6 +++--- contrib/tnftp/extern.h | 10 +++------- contrib/tnftp/fetch.c | 10 ++++++---- contrib/tnftp/ftp.c | 37 ++++++++++++++++++++++++------------- contrib/tnftp/main.c | 40 +++++----------------------------------- contrib/tnftp/util.c | 30 ++++++++++++++++++++++++++++-- 6 files changed, 69 insertions(+), 64 deletions(-) diff --git a/contrib/tnftp/cmds.c b/contrib/tnftp/cmds.c index caa0960b34..467d7e6a19 100644 --- a/contrib/tnftp/cmds.c +++ b/contrib/tnftp/cmds.c @@ -1,4 +1,4 @@ -/* $NetBSD: cmds.c,v 1.131 2010/01/12 06:50:04 lukem Exp $ */ +/* $NetBSD: cmds.c,v 1.132 2011/09/16 15:39:26 joerg Exp $ */ /*- * Copyright (c) 1996-2009 The NetBSD Foundation, Inc. @@ -96,7 +96,7 @@ #if 0 static char sccsid[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94"; #else -__RCSID("$NetBSD: cmds.c,v 1.131 2010/01/12 06:50:04 lukem Exp $"); +__RCSID("$NetBSD: cmds.c,v 1.132 2011/09/16 15:39:26 joerg Exp $"); #endif #endif /* not lint */ @@ -142,7 +142,7 @@ static struct types { static sigjmp_buf jabort; static int confirm(const char *, const char *); -static void mintr(int); +__dead static void mintr(int); static void mabort(const char *); static void set_type(const char *); diff --git a/contrib/tnftp/extern.h b/contrib/tnftp/extern.h index 30341fa66e..bb71466133 100644 --- a/contrib/tnftp/extern.h +++ b/contrib/tnftp/extern.h @@ -1,4 +1,4 @@ -/* $NetBSD: extern.h,v 1.78 2010/03/04 21:40:53 lukem Exp $ */ +/* $NetBSD: extern.h,v 1.79 2011/09/16 15:39:26 joerg Exp $ */ /*- * Copyright (c) 1996-2009 The NetBSD Foundation, Inc. @@ -94,9 +94,6 @@ struct tm; struct addrinfo; void abort_remote(FILE *); -void abort_squared(int); -void abortpt(int); -void abortxfer(int); void account(int, char **); void ai_unmapped(struct addrinfo *); int another(int *, char ***, const char *); @@ -145,7 +142,7 @@ void help(int, char **); char *hookup(const char *, const char *); void idlecmd(int, char **); int initconn(void); -void intr(int); +__dead void intr(int); int isipv6addr(const char *); void list_vertical(StringList *); void lcd(int, char **); @@ -169,7 +166,7 @@ void page(int, char **); const char *parse_rfc2616time(struct tm *, const char *); int parserate(int, char **, int); char *prompt(void); -void proxabort(int); +__dead void proxabort(int); void proxtrans(const char *, const char *, const char *); void psabort(int); void pswitch(int); @@ -241,7 +238,6 @@ int togglevar(int, char **, int *, const char *); void unsetoption(int, char **); void updatelocalcwd(void); void updateremotecwd(void); -void usage(void); void user(int, char **); int ftp_connect(int, const struct sockaddr *, socklen_t); int ftp_listen(int, int); diff --git a/contrib/tnftp/fetch.c b/contrib/tnftp/fetch.c index 67b2442452..9fead8c6bc 100644 --- a/contrib/tnftp/fetch.c +++ b/contrib/tnftp/fetch.c @@ -1,4 +1,4 @@ -/* $NetBSD: fetch.c,v 1.193 2010/03/05 07:41:10 lukem Exp $ */ +/* $NetBSD: fetch.c,v 1.195 2011/12/10 05:53:58 lukem Exp $ */ /*- * Copyright (c) 1997-2009 The NetBSD Foundation, Inc. @@ -34,7 +34,7 @@ #include #ifndef lint -__RCSID("$NetBSD: fetch.c,v 1.193 2010/03/05 07:41:10 lukem Exp $"); +__RCSID("$NetBSD: fetch.c,v 1.195 2011/12/10 05:53:58 lukem Exp $"); #endif /* not lint */ /* @@ -52,6 +52,7 @@ __RCSID("$NetBSD: fetch.c,v 1.193 2010/03/05 07:41:10 lukem Exp $"); #include #include +#include #include #include #include @@ -74,7 +75,7 @@ typedef enum { CLASSIC_URL_T } url_t; -void aborthttp(int); +__dead static void aborthttp(int); #ifndef NO_AUTH static int auth_url(const char *, char **, const char *, const char *); static void base64_encode(const unsigned char *, size_t, unsigned char *); @@ -1125,6 +1126,7 @@ fetch_url(const char *url, const char *proxyenv, char *proxyauth, char *wwwauth) (void)xsignal(SIGQUIT, psummary); oldintr = xsignal(SIGINT, aborthttp); + assert(rcvbuf_size > 0); if ((size_t)rcvbuf_size > bufsize) { if (xferbuf) (void)free(xferbuf); @@ -1323,7 +1325,7 @@ fetch_url(const char *url, const char *proxyenv, char *proxyauth, char *wwwauth) /* * Abort a HTTP retrieval */ -void +static void aborthttp(int notused) { char msgbuf[100]; diff --git a/contrib/tnftp/ftp.c b/contrib/tnftp/ftp.c index 3eeb678a67..f9f5769a00 100644 --- a/contrib/tnftp/ftp.c +++ b/contrib/tnftp/ftp.c @@ -1,4 +1,4 @@ -/* $NetBSD: ftp.c,v 1.160 2010/03/05 07:41:10 lukem Exp $ */ +/* $NetBSD: ftp.c,v 1.163 2011/12/10 05:53:58 lukem Exp $ */ /*- * Copyright (c) 1996-2009 The NetBSD Foundation, Inc. @@ -92,7 +92,7 @@ #if 0 static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94"; #else -__RCSID("$NetBSD: ftp.c,v 1.160 2010/03/05 07:41:10 lukem Exp $"); +__RCSID("$NetBSD: ftp.c,v 1.163 2011/12/10 05:53:58 lukem Exp $"); #endif #endif /* not lint */ @@ -108,6 +108,7 @@ __RCSID("$NetBSD: ftp.c,v 1.160 2010/03/05 07:41:10 lukem Exp $"); #include #include +#include #include #include #include @@ -131,6 +132,7 @@ int ptflag = 0; char pasv[BUFSIZ]; /* passive port for proxy data connection */ static int empty(FILE *, FILE *, int); +__dead static void abort_squared(int); struct sockinet { union sockunion { @@ -537,7 +539,7 @@ empty(FILE *ecin, FILE *din, int sec) sigjmp_buf xferabort; -void +__dead static void abortxfer(int notused) { char msgbuf[100]; @@ -764,6 +766,7 @@ sendrequest(const char *cmd, const char *local, const char *remote, if (dout == NULL) goto abort; + assert(sndbuf_size > 0); if ((size_t)sndbuf_size > bufsize) { if (buf) (void)free(buf); @@ -1025,6 +1028,7 @@ recvrequest(const char *cmd, const char *volatile local, const char *remote, progress = 0; preserve = 0; } + assert(rcvbuf_size > 0); if ((size_t)rcvbuf_size > bufsize) { if (buf) (void)free(buf); @@ -1591,18 +1595,25 @@ initconn(void) UC(p[0]), UC(p[1])); break; #ifdef INET6 - case AF_INET6: - a = (char *)&data_addr.si_su.su_sin6.sin6_addr; - p = (char *)&data_addr.su_port; + case AF_INET6: { + uint8_t ua[sizeof(data_addr.si_su.su_sin6.sin6_addr)]; + uint8_t up[sizeof(data_addr.su_port)]; + + memcpy(ua, &data_addr.si_su.su_sin6.sin6_addr, + sizeof(ua)); + memcpy(up, &data_addr.su_port, sizeof(up)); + result = command( "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", 6, 16, - UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]), - UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]), - UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]), - UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]), - 2, UC(p[0]), UC(p[1])); + ua[0], ua[1], ua[2], ua[3], + ua[4], ua[5], ua[6], ua[7], + ua[8], ua[9], ua[10], ua[11], + ua[12], ua[13], ua[14], ua[15], + 2, + up[0], up[1]); break; + } #endif default: result = COMPLETE + 1; /* xxx */ @@ -1812,7 +1823,7 @@ pswitch(int flag) } } -void +__dead static void abortpt(int notused) { @@ -2024,7 +2035,7 @@ gunique(const char *local) * too impatient to wait or there's another problem then ftp really * needs to get back to a known state. */ -void +static void abort_squared(int dummy) { char msgbuf[100]; diff --git a/contrib/tnftp/main.c b/contrib/tnftp/main.c index 3724790cda..58234eed30 100644 --- a/contrib/tnftp/main.c +++ b/contrib/tnftp/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.117 2009/07/13 19:05:41 roy Exp $ */ +/* $NetBSD: main.c,v 1.120 2011/12/10 05:53:58 lukem Exp $ */ /*- * Copyright (c) 1996-2009 The NetBSD Foundation, Inc. @@ -98,7 +98,7 @@ __COPYRIGHT("@(#) Copyright (c) 1985, 1989, 1993, 1994\ #if 0 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94"; #else -__RCSID("$NetBSD: main.c,v 1.117 2009/07/13 19:05:41 roy Exp $"); +__RCSID("$NetBSD: main.c,v 1.120 2011/12/10 05:53:58 lukem Exp $"); #endif #endif /* not lint */ @@ -129,8 +129,8 @@ __RCSID("$NetBSD: main.c,v 1.117 2009/07/13 19:05:41 roy Exp $"); #define NO_PROXY "no_proxy" /* env var with list of non-proxied * hosts, comma or space separated */ +__dead static void usage(void); static void setupoption(const char *, const char *, const char *); -int main(int, char *[]); int main(int volatile argc, char **volatile argv) @@ -139,9 +139,8 @@ main(int volatile argc, char **volatile argv) struct passwd *pw; char *cp, *ep, *anonpass, *upload_path, *src_addr; const char *anonuser; - int dumbterm, s, isupload; + int dumbterm, isupload; size_t len; - socklen_t slen; tzset(); setlocale(LC_ALL, ""); @@ -204,35 +203,6 @@ main(int volatile argc, char **volatile argv) if (cp != NULL && strlcpy(netrc, cp, sizeof(netrc)) >= sizeof(netrc)) errx(1, "$NETRC `%s': %s", cp, strerror(ENAMETOOLONG)); - /* - * Get the default socket buffer sizes if we don't already have them. - * It doesn't matter which socket we do this to, because on the first - * call no socket buffer sizes will have been modified, so we are - * guaranteed to get the system defaults. - */ - s = socket(AF_INET, SOCK_STREAM, 0); - if (s == -1) - err(1, "Can't create socket to determine default socket sizes"); - slen = sizeof(rcvbuf_size); - if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, - (void *)&rcvbuf_size, &slen) == -1) - err(1, "Unable to get default rcvbuf size"); - slen = sizeof(sndbuf_size); - if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, - (void *)&sndbuf_size, &slen) == -1) - err(1, "Unable to get default sndbuf size"); - (void)close(s); - /* sanity check returned buffer sizes */ - if (rcvbuf_size <= 0) - rcvbuf_size = 8 * 1024; - if (sndbuf_size <= 0) - sndbuf_size = 8 * 1024; - - if (sndbuf_size > 8 * 1024 * 1024) - sndbuf_size = 8 * 1024 * 1024; - if (rcvbuf_size > 8 * 1024 * 1024) - rcvbuf_size = 8 * 1024 * 1024; - marg_sl = ftp_sl_init(); if ((tmpdir = getenv("TMPDIR")) == NULL) tmpdir = _PATH_TMP; @@ -737,7 +707,7 @@ cmdscanner(void) */ if (strchr(margv[0], ':') != NULL || !editing || - el_parse(el, margc, (const char **)margv) != 0) + el_parse(el, margc, (void *)margv) != 0) #endif /* !NO_EDITCOMPLETE */ fputs("?Invalid command.\n", ttyout); continue; diff --git a/contrib/tnftp/util.c b/contrib/tnftp/util.c index 4072db0ad1..3a59d790de 100644 --- a/contrib/tnftp/util.c +++ b/contrib/tnftp/util.c @@ -1,4 +1,4 @@ -/* $NetBSD: util.c,v 1.155 2010/06/05 13:59:39 lukem Exp $ */ +/* $NetBSD: util.c,v 1.156 2011/12/10 05:53:58 lukem Exp $ */ /*- * Copyright (c) 1997-2009 The NetBSD Foundation, Inc. @@ -64,7 +64,7 @@ #include #ifndef lint -__RCSID("$NetBSD: util.c,v 1.155 2010/06/05 13:59:39 lukem Exp $"); +__RCSID("$NetBSD: util.c,v 1.156 2011/12/10 05:53:58 lukem Exp $"); #endif /* not lint */ /* @@ -1080,6 +1080,32 @@ strsuftoi(const char *arg) void setupsockbufsize(int sock) { + socklen_t slen; + + if (0 == rcvbuf_size) { + slen = sizeof(rcvbuf_size); + if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, + (void *)&rcvbuf_size, &slen) == -1) + err(1, "Unable to determine rcvbuf size"); + if (rcvbuf_size <= 0) + rcvbuf_size = 8 * 1024; + if (rcvbuf_size > 8 * 1024 * 1024) + rcvbuf_size = 8 * 1024 * 1024; + DPRINTF("setupsockbufsize: rcvbuf_size determined as %d\n", + rcvbuf_size); + } + if (0 == sndbuf_size) { + slen = sizeof(sndbuf_size); + if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, + (void *)&sndbuf_size, &slen) == -1) + err(1, "Unable to determine sndbuf size"); + if (sndbuf_size <= 0) + sndbuf_size = 8 * 1024; + if (sndbuf_size > 8 * 1024 * 1024) + sndbuf_size = 8 * 1024 * 1024; + DPRINTF("setupsockbufsize: sndbuf_size determined as %d\n", + sndbuf_size); + } if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *)&sndbuf_size, sizeof(sndbuf_size)) == -1) -- 2.41.0