diff options
author | Denys Vlasenko | 2018-02-06 15:48:12 +0100 |
---|---|---|
committer | Denys Vlasenko | 2018-02-06 15:48:12 +0100 |
commit | 1783ffa990814e2aac14e7ff5a4bbf2d5bebe3cc (patch) | |
tree | 7e792db0260222476ec186db33fec49a22039731 /networking/parse_pasv_epsv.c | |
parent | 403f2999f94937ba3f37db6d093832f636815bb9 (diff) | |
download | busybox-1783ffa990814e2aac14e7ff5a4bbf2d5bebe3cc.zip busybox-1783ffa990814e2aac14e7ff5a4bbf2d5bebe3cc.tar.gz |
wget: add EPSV support
function old new delta
parse_pasv_epsv - 151 +151
wget_main 2440 2382 -58
xconnect_ftpdata 223 94 -129
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 0/2 up/down: 151/-187) Total: -36 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/parse_pasv_epsv.c')
-rw-r--r-- | networking/parse_pasv_epsv.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/networking/parse_pasv_epsv.c b/networking/parse_pasv_epsv.c new file mode 100644 index 0000000..f320871 --- /dev/null +++ b/networking/parse_pasv_epsv.c @@ -0,0 +1,66 @@ +/* + * Utility routines. + * + * Copyright (C) 2018 Denys Vlasenko + * + * Licensed under GPLv2 or later, see file LICENSE in this source tree. + */ +//kbuild:lib-$(CONFIG_FTPGET) += ftpgetput.o +//kbuild:lib-$(CONFIG_FTPPUT) += ftpgetput.o +//kbuild:lib-$(CONFIG_WGET) += parse_pasv_epsv.o + +#include "libbb.h" + +int FAST_FUNC parse_pasv_epsv(char *buf) +{ +/* + * PASV command will not work for IPv6. RFC2428 describes + * IPv6-capable "extended PASV" - EPSV. + * + * "EPSV [protocol]" asks server to bind to and listen on a data port + * in specified protocol. Protocol is 1 for IPv4, 2 for IPv6. + * If not specified, defaults to "same as used for control connection". + * If server understood you, it should answer "229 <some text>(|||port|)" + * where "|" are literal pipe chars and "port" is ASCII decimal port#. + * + * There is also an IPv6-capable replacement for PORT (EPRT), + * but we don't need that. + * + * NB: PASV may still work for some servers even over IPv6. + * For example, vsftp happily answers + * "227 Entering Passive Mode (0,0,0,0,n,n)" and proceeds as usual. + */ + char *ptr; + int port; + + if (!ENABLE_FEATURE_IPV6 || buf[2] == '7' /* "227" */) { + /* Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]" + * Server's IP is N1.N2.N3.N4 (we ignore it) + * Server's port for data connection is P1*256+P2 */ + ptr = strrchr(buf, ')'); + if (ptr) *ptr = '\0'; + + ptr = strrchr(buf, ','); + if (!ptr) return -1; + *ptr = '\0'; + port = xatou_range(ptr + 1, 0, 255); + + ptr = strrchr(buf, ','); + if (!ptr) return -1; + *ptr = '\0'; + port += xatou_range(ptr + 1, 0, 255) * 256; + } else { + /* Response is "229 garbage(|||P1|)" + * Server's port for data connection is P1 */ + ptr = strrchr(buf, '|'); + if (!ptr) return -1; + *ptr = '\0'; + + ptr = strrchr(buf, '|'); + if (!ptr) return -1; + *ptr = '\0'; + port = xatou_range(ptr + 1, 0, 65535); + } + + return port; +} |