Discussion:
SOCKS4 proxy and multi interface
Dmitri Shubin
2011-04-20 16:44:59 UTC
Permalink
Hello all!

I have a problem downloading file with FTP protocol using SOCKS4 proxy.
It perfectly works with easy interface but fails when using multi one.

Verbose output is as follows:

$ ./multi-single
* About to connect() to proxy localhost port 2121 (#0)
* Trying 127.0.0.1... * connected
* SOCKS4 request granted.
* Connected to localhost (127.0.0.1) port 2121 (#0)
* SOCKS4 reply has wrong version, version should be 4.
* Closing connection #0

strace shows that it tries to connect to proxy twice:

socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
...
connect(3, {sa_family=AF_INET, sin_port=htons(2121),
sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in
progress)
...
sendto(3, "\4\1\0\25\177\0\0\1\0", 9, MSG_NOSIGNAL, NULL, 0) = 9
<------------- ask proxy to connect
...
recvfrom(3, "\0Z\0\0\0\0\0\0", 8, 0, NULL, NULL) = 8
<------------------- proxy ACK's
...
sendto(3, "\4\1\0\25\177\0\0\1\0", 9, MSG_NOSIGNAL, NULL, 0) = 9
<--------- ask proxy again !!!!!
...
recvfrom(3, "220 (vsF", 8, 0, NULL, NULL) = 8 <------- hello from FTP
but ACK from proxy is expected

I use SSH (OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008) to
create SOCKS4 proxy:
$ ssh -N -D localhost:2121 localhost

To test I used modified version of multi-single.c example
(docs/examples/multi-single.c) from curl-7.21.5 distribution:
Here is diff:

diff -Nru curl-7.21.5/docs/examples/multi-single.c
curl-7.21.5.new/docs/examples/multi-single.c
--- curl-7.21.5/docs/examples/multi-single.c 2011-03-19
18:17:12.000000000 +0300
+++ curl-7.21.5.new/docs/examples/multi-single.c 2011-04-20
20:19:20.000000000 +0400
@@ -44,7 +44,11 @@
http_handle = curl_easy_init();

/* set the options (I left out a few, you'll get the point anyway) */
- curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
+ /*curl_easy_setopt(http_handle, CURLOPT_URL,
"http://www.example.com/");*/
+ curl_easy_setopt(http_handle, CURLOPT_URL, "ftp://localhost/dd.txt");
+ curl_easy_setopt(http_handle, CURLOPT_PROXY, "localhost:2121");
+ curl_easy_setopt(http_handle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
+ curl_easy_setopt(http_handle, CURLOPT_VERBOSE, (long)1);

/* init a multi stack */
multi_handle = curl_multi_init();

Thanks!
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Dan Fandrich
2011-04-20 17:44:02 UTC
Permalink
Post by Dmitri Shubin
I have a problem downloading file with FTP protocol using SOCKS4 proxy.
It perfectly works with easy interface but fails when using multi one.
KNOWN_BUG #65 affects FTP over SOCKS, but the issue describes problems
on the data channel only. Is the issue you're seeing on the control
channel only? It sounds like it's happening well before the data
channel is ever established.
Post by Dmitri Shubin
Dan
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Dmitri Shubin
2011-04-21 11:50:40 UTC
Permalink
Post by Dan Fandrich
Post by Dmitri Shubin
I have a problem downloading file with FTP protocol using SOCKS4 proxy.
It perfectly works with easy interface but fails when using multi one.
KNOWN_BUG #65 affects FTP over SOCKS, but the issue describes problems
on the data channel only. Is the issue you're seeing on the control
channel only? It sounds like it's happening well before the data
channel is ever established.
OOPS, didn't checked this list first.
Yes, AFAIU even control channel is unable to login to FTP.

BTW I have a server that once per day tries to download several files
from FTP via SOCKS4 proxy and strangely enough sometimes it's able to
download them! Probably it happens when connection from the previous
download attempt wasn't closed.

In my case this simple patch fixed the problem, but I'm pretty sure it
breaks something else :)
(here I removed call to Curl_connected_proxy() from multi_runsingle())

--- curl-7.21.5/lib/multi.c 2011-04-05 19:07:27.000000000 +0400
+++ curl-7.21.5.new/lib/multi.c 2011-04-21 14:35:54.000000000 +0400
@@ -1142,7 +1142,7 @@
&connected);
if(connected) {
/* see if we need to do any proxy magic first once we connected */
- easy->result = Curl_connected_proxy(easy->easy_conn);
+ /*easy->result = Curl_connected_proxy(easy->easy_conn);*/

if(!easy->result)
/* if everything is still fine we do the protocol-specific
connect

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Daniel Stenberg
2011-04-28 08:08:50 UTC
Permalink
In my case this simple patch fixed the problem, but I'm pretty sure it breaks
something else :)
Thanks for your report and the initial patch. Please have a look at the
attached patch. It seems to fix the problem for me and with this I can run
test case 564 just fine!
--
/ daniel.haxx.se
Daniel Stenberg
2011-04-20 18:00:03 UTC
Permalink
I have a problem downloading file with FTP protocol using SOCKS4 proxy. It
perfectly works with easy interface but fails when using multi one.
Test case 564 is exactly that (FTP over a SOCKS4 proxy using the multi
interface), and that is listed in DISABLED right now probably for this reason.
I can't recall exactly...

Time to dig up the debugger and start poking on the code to make it work!
--
/ daniel.haxx.se
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Loading...