Skip to contentSkip to conversation

slirp: add SOCKS5 support

PATCH Superseded Laurent Vivier 2 participants 4 messages Mar 28, 2017 – Apr 3, 2017 Networking

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover slirp: add SOCKS5 support

This patch implements the SOCKS5 client part for "-net user" backend.

It allows to route all internet traffic of the virtual machine to a SOCKS5 server.

But all the local traffic (to the host) is sent to the host. It is needed because this SOCKS5 client doesn't route UDP traffic, and this allows to use the host DNS server.

I've tested this using public SOCKS5 proxy list found on the WEB, and using TOR server on my host.

Used with TOR, all the TCP connections are sent to the TOR network and this allows to insert a virtual machine directly in the TOR network without needing more configuration in the virtual machine.

But be aware that all DNS requests will be sent to the host that can forward them to internet with its own IP address. So confidentiality will not be as good as with the TOR browser which hides in the TOR network all the DNS requests.

If you want to test this:

- with a public SOCKS5 server, ask google for "socks5 proxy address" and start QEMU with, for instance:

qemu-system-x86_64 -net nic,model=e1000 -net user,proxy-server=46.105.121.37:63066 ...

if needed, you can provide user/password using secret objects framework:
"-object secret,id=sec0,data=password,format=raw \
-net user,...,proxy-user=user,proxy-secretid=sec0"

- with a local TOR proxy:

sudo systemctl start tor
qemu-system-x86_64 -net nic,model=e1000 -net user,proxy-server=localhost:9050 ...

You can check your IP address is the one of the proxy by connecting to http://check.torproject.org with a browser inside the VM.

v2:
- use secret objects framework to provide password
- add documentation for new parameters in qapi-schema.json
- s/passwd/password/g
- I didn't move proxy paramaters to a substruct as I didn't

    find a way to set them from the command line :(

Laurent Vivier (1):
slirp: add SOCKS5 support

 net/slirp.c         |  39 +++++++-
 qapi-schema.json    |   9 ++
 qemu-options.hx     |  11 ++
 slirp/Makefile.objs |   2 +-
 slirp/ip_icmp.c     |   2 +-
 slirp/libslirp.h    |   3 +
 slirp/slirp.c       |  66 +++++++++++-
 slirp/slirp.h       |   6 ++
 slirp/socket.h      |   4 +
 slirp/socks5.c      | 284 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 slirp/socks5.h      |  85 ++++++++++++++++
 slirp/tcp_subr.c    |  21 +++-
 slirp/udp.c         |   9 ++
 slirp/udp6.c        |   2 +-
 14 files changed, 531 insertions(+), 12 deletions(-)
 create mode 100644 slirp/socks5.c
 create mode 100644 slirp/socks5.h
Signature · 1 line
2.9.3
#1 lore raw

PATCH v2 1/1 slirp: add SOCKS5 support

When the VM is used behind a firewall, This allows the use of a SOCKS5 proxy server to connect the VM IP stack directly to the Internet.

This implementation doesn't manage UDP packets, so they are simply dropped (as with restrict=on), except for the localhost as we need it for DNS.

---
 net/slirp.c         |  39 +++++++-
 qapi-schema.json    |   9 ++
 qemu-options.hx     |  11 ++
 slirp/Makefile.objs |   2 +-
 slirp/ip_icmp.c     |   2 +-
 slirp/libslirp.h    |   3 +
 slirp/slirp.c       |  66 +++++++++++-
 slirp/slirp.h       |   6 ++
 slirp/socket.h      |   4 +
 slirp/socks5.c      | 284 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 slirp/socks5.h      |  85 ++++++++++++++++
 slirp/tcp_subr.c    |  21 +++-
 slirp/udp.c         |   9 ++
 slirp/udp6.c        |   2 +-
 14 files changed, 531 insertions(+), 12 deletions(-)
 create mode 100644 slirp/socks5.c
 create mode 100644 slirp/socks5.h
net/slirp.c +37 -2
index f97ec23..8a5dc3f 100644
@@ -41,6 +41,7 @@
 #include "sysemu/sysemu.h"
 #include "qemu/cutils.h"
 #include "qapi/error.h"
+#include "crypto/secret.h"
 
 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
 {
@@ -139,6 +140,33 @@ static void net_slirp_cleanup(NetClientState *nc)
     QTAILQ_REMOVE(&slirp_stacks, s, entry);
 }
 
+static int net_slirp_add_proxy(SlirpState *s, const char *proxy_server,
+                               const char *proxy_user,
+                               const char *proxy_secretid)
+{
+    InetSocketAddress *addr;
+    char *password = NULL;
+    int ret;
+
+    if (proxy_server == NULL) {
+        return 0;
+    }
+
+    if (proxy_secretid) {
+        password = qcrypto_secret_lookup_as_utf8(proxy_secretid, &error_fatal);
+    }
+
+    addr = inet_parse(proxy_server, &error_fatal);
+
+    ret = slirp_add_proxy(s->slirp, addr->host, atoi(addr->port),
+                          proxy_user, password);
+
+    qapi_free_InetSocketAddress(addr);
+    g_free(password);
+
+    return ret;
+}
+
 static NetClientInfo net_slirp_info = {
     .type = NET_CLIENT_DRIVER_USER,
     .size = sizeof(SlirpState),
@@ -155,7 +183,8 @@ static int net_slirp_init(NetClientState *peer, const char *model,
                           const char *bootfile, const char *vdhcp_start,
                           const char *vnameserver, const char *vnameserver6,
                           const char *smb_export, const char *vsmbserver,
-                          const char **dnssearch)
+                          const char **dnssearch, const char *proxy_server,
+                          const char *proxy_user, const char *proxy_secretid)
 {
     /* default settings according to historic slirp */
     struct in_addr net  = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
@@ -361,6 +390,11 @@ static int net_slirp_init(NetClientState *peer, const char *model,
     }
 #endif
 
+    if (net_slirp_add_proxy(s, proxy_server,
+                            proxy_user, proxy_secretid) < 0) {
+        goto error;
+    }
+
     s->exit_notifier.notify = slirp_smb_exit;
     qemu_add_exit_notifier(&s->exit_notifier);
     return 0;
@@ -878,7 +912,8 @@ int net_init_slirp(const Netdev *netdev, const char *name,
                          user->ipv6_host, user->hostname, user->tftp,
                          user->bootfile, user->dhcpstart,
                          user->dns, user->ipv6_dns, user->smb,
-                         user->smbserver, dnssearch);
+                         user->smbserver, dnssearch, user->proxy_server,
+                         user->proxy_user, user->proxy_secretid);
 
     while (slirp_configs) {
         config = slirp_configs;
qapi-schema.json +9 -0
index b921994..1799ae2 100644
@@ -3658,6 +3658,12 @@
 #
 # @guestfwd: forward guest TCP connections
 #
+# @proxy-server: address of the SOCKS5 proxy server to use (since 2.10)
+#
+# @proxy-user: username to use with the proxy server (since 2.10)
+#
+# @proxy-secretid: secret id to use for the proxy server password (since 2.10)
+#
 # Since: 1.2
 ##
 { 'struct': 'NetdevUserOptions',
@@ -3680,6 +3686,9 @@
     '*ipv6-dns':         'str',
     '*smb':       'str',
     '*smbserver': 'str',
+    '*proxy-server': 'str',
+    '*proxy-user':   'str',
+    '*proxy-secretid': 'str',
     '*hostfwd':   ['String'],
     '*guestfwd':  ['String'] } }
 
qemu-options.hx +11 -0
index 99af8ed..e625d1a 100644
@@ -1645,6 +1645,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
 #ifndef _WIN32
                                              "[,smb=dir[,smbserver=addr]]\n"
 #endif
+    "         [,proxy-server=addr:port[,proxy-user=user,proxy-secretid=id]]\n"
     "                configure a user mode network backend with ID 'str',\n"
     "                its DHCP server and optional services\n"
 #endif
@@ -1883,6 +1884,16 @@ Note that a SAMBA server must be installed on the host OS.
 QEMU was tested successfully with smbd versions from Red Hat 9,
 Fedora Core 3 and OpenSUSE 11.x.
 
+@item proxy-server=@var{addr}:@var{port}[,proxy-user=@var{user},proxy-secretid=@var{secretid}]]
+If you provide a SOCKS5 proxy server address @var{addr} and a port number @var{port},
+QEMU will use it to connect to Internet. If the proxy server needs an user id and a password
+the values are provided with proxy-user and proxy-secretid (via secret object).
+
+For example, to connect to a TOR proxy server on the host, use the following:
+@example
+qemu-system-i386 -net user,proxy-server=localhost:9050
+@end example
+
 @item hostfwd=[tcp|udp]:[@var{hostaddr}]:@var{hostport}-[@var{guestaddr}]:@var{guestport}
 Redirect incoming TCP or UDP connections to the host port @var{hostport} to
 the guest IP address @var{guestaddr} on guest port @var{guestport}. If
slirp/Makefile.objs +1 -1
index 1baa1f1..ce6d643 100644
@@ -2,4 +2,4 @@ common-obj-y = cksum.o if.o ip_icmp.o ip6_icmp.o ip6_input.o ip6_output.o \
                ip_input.o ip_output.o dnssearch.o dhcpv6.o
 common-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
 common-obj-y += tcp_subr.o tcp_timer.o udp.o udp6.o bootp.o tftp.o arp_table.o \
-                ndp_table.o
+                ndp_table.o socks5.o
slirp/ip_icmp.c +1 -1
index 5ffc7a6..ed5e3eb 100644
@@ -154,7 +154,7 @@ icmp_input(struct mbuf *m, int hlen)
     ip->ip_len += hlen;	             /* since ip_input subtracts this */
     if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
       icmp_reflect(m);
-    } else if (slirp->restricted) {
+    } else if (slirp->restricted || slirp->proxy_server) {
         goto freeit;
     } else {
       struct socket *so;
slirp/libslirp.h +3 -0
index f90f0f5..e6fc3f3 100644
@@ -26,6 +26,9 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error);
 
 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len);
 
+int slirp_add_proxy(Slirp *slirp, const char *server, int port,
+                    const char *user, const char *password);
+
 /* you must provide the following functions: */
 void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len);
 
slirp/slirp.c +62 -4

97 lines not on this page · raw message

slirp/slirp.h +6 -0
index 3877f66..9db58ed 100644
@@ -214,6 +214,12 @@ struct Slirp {
     char *tftp_prefix;
     struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
 
+    /* proxy */
+    char *proxy_server;
+    int proxy_port;
+    char *proxy_user;
+    char *proxy_password;
+
     ArpTable arp_table;
     NdpTable ndp_table;
 
slirp/socket.h +4 -0
index 8feed2a..232f8e5 100644
@@ -8,6 +8,8 @@
 #ifndef SLIRP_SOCKET_H
 #define SLIRP_SOCKET_H
 
+#include "socks5.h"
+
 #define SO_EXPIRE 240000
 #define SO_EXPIREFAST 10000
 
@@ -70,6 +72,8 @@ struct socket {
   struct sbuf so_rcv;		/* Receive buffer */
   struct sbuf so_snd;		/* Send buffer */
   void * extra;			/* Extra pointer */
+
+  socks5_state_t so_proxy_state;
 };
 
 
slirp/socks5.c new file +284 -0

284 lines not on this page · raw message

slirp/socks5.h new file +85 -0

85 lines not on this page · raw message

slirp/tcp_subr.c +18 -3
index ed16e18..5e394fd 100644
@@ -40,6 +40,7 @@
 
 #include "qemu/osdep.h"
 #include "slirp.h"
+#include "socks5.h"
 
 /* patchable/settable parameters for tcp */
 /* Don't do rfc1323 performance enhancements */
@@ -394,11 +395,21 @@ tcp_sockclosed(struct tcpcb *tp)
 int tcp_fconnect(struct socket *so, unsigned short af)
 {
   int ret=0;
+  Slirp *slirp = so->slirp;
 
   DEBUG_CALL("tcp_fconnect");
   DEBUG_ARG("so = %p", so);
 
-  ret = so->s = qemu_socket(af, SOCK_STREAM, 0);
+  /* local traffic doesn't go to the proxy */
+  if (slirp->proxy_server &&
+      !(af == AF_INET &&
+        (so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
+        slirp->vnetwork_addr.s_addr)) {
+    ret = so->s = socks5_socket(&so->so_proxy_state);
+  } else {
+    ret = so->s = qemu_socket(af, SOCK_STREAM, 0);
+  }
+
   if (ret >= 0) {
     int opt, s=so->s;
     struct sockaddr_storage addr;
@@ -413,8 +424,12 @@ int tcp_fconnect(struct socket *so, unsigned short af)
     sotranslate_out(so, &addr);
 
     /* We don't care what port we get */
-    ret = connect(s, (struct sockaddr *)&addr, sockaddr_size(&addr));
-
+    if (so->so_proxy_state) {
+      ret = socks5_connect(s, slirp->proxy_server, slirp->proxy_port,
+                           &so->so_proxy_state);
+    } else {
+      ret = connect(s, (struct sockaddr *)&addr, sockaddr_size(&addr));
+    }
     /*
      * If it's not in progress, it failed, so we just return 0,
      * without clearing SS_NOFDREF
slirp/udp.c +9 -0
index 227d779..1f4b39c 100644
@@ -160,6 +160,15 @@ udp_input(register struct mbuf *m, int iphlen)
             goto bad;
         }
 
+        /* as our SOCKS5 client is not able to route UDP packets,
+         * only allow local UDP traffic (we need it for DNS)
+         */
+        if (slirp->proxy_server &&
+            (ip->ip_dst.s_addr & slirp->vnetwork_mask.s_addr) !=
+            slirp->vnetwork_addr.s_addr) {
+            goto bad;
+        }
+
 	/*
 	 * Locate pcb for datagram.
 	 */
slirp/udp6.c +1 -1
index 9fa314b..995181d 100644
@@ -22,7 +22,7 @@ void udp6_input(struct mbuf *m)
     DEBUG_CALL("udp6_input");
     DEBUG_ARG("m = %lx", (long)m);
 
-    if (slirp->restricted) {
+    if (slirp->restricted || slirp->proxy_server) {
         goto bad;
     }
 
Signature · 1 line
2.9.3
#2 lore raw

Hello,

Thanks for the patch!

slirp/slirp.c void slirp_pollfds_poll(GArray *pollfds, int select_error) · line 627

Quoted 8 lines · Laurent Vivier, on mar. 28 mars 2017 21:07:56 +0200, wrote:
slirp/slirp.c +4 -0
@@ -617,6 +621,10 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
                  * Check sockets for reading
                  */
                 else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
+                    if (so->so_state & SS_ISFCONNECTING) {
+                        socks5_recv(so->s, &so->so_proxy_state);
+                        continue;
+                    }

It looks odd to be calling socks5_recv in all cases. Shouldn't this check for so_proxy_state != 0?

slirp/slirp.c void slirp_pollfds_poll(GArray *pollfds, int select_error) · line 658

Quoted 11 lines
slirp/slirp.c +2 -4
@@ -645,11 +653,19 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
                     /*
                      * Check for non-blocking, still-connecting sockets
                      */
-                    if (so->so_state & SS_ISFCONNECTING) {
-                        /* Connected */
-                        so->so_state &= ~SS_ISFCONNECTING;
 
-                        ret = send(so->s, (const void *) &ret, 0, 0);
+                    if (so->so_state & SS_ISFCONNECTING) {
+                        ret = socks5_send(so->s, slirp->proxy_user,

Ditto. Socks5 and non-socks5 code should be properly separated I guess.

slirp/slirp.c int slirp_add_exec(Slirp *slirp, int do_pty, const void *args, · line 1095

Quoted 12 lines
slirp/slirp.c +8 -0
@@ -1069,6 +1085,48 @@ int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
                     htons(guest_port));
 }
 
+int slirp_add_proxy(Slirp *slirp, const char *server, int port,
+                    const char *user, const char *password)
+{
+    int fd;
+    socks5_state_t state;
+    struct sockaddr_storage addr;
+
+    /* check the connection */

Please be a bit more verbose :) For instance:

slirp/socks5.c line 1

Quoted 12 lines
(quoted diff) +1 -0
+ /* just check that the connection to the socks5 server works with

the given credentials, and close without doing anything with it. */

slirp/socks5.c new file +1 -0
new file mode 100644
index 0000000..1c5e071
@@ -0,0 +1,284 @@
+/* some parts from nmap/ncat GPLv2 */

One can't just steal code like this. You *have* to copy over the copyright notice, since there's notably the author information.

slirp/socks5.c line 20

Quoted 13 lines
slirp/socks5.c new file +13 -0
+static int socks5_proxy_connect(int fd, const char *server, int port)
+{
+    struct sockaddr_in saddr;
+    struct hostent *he;
+
+    he = gethostbyname(server);
+    if (he == NULL) {
+        errno = EINVAL;
+        return -1;
+    }
+    saddr.sin_family = AF_INET;
+    saddr.sin_addr = *(struct in_addr *)he->h_addr;
+    saddr.sin_port = htons(port);

Please add a TODO: v6 version

slirp/socks5.c line 100

Quoted 7 lines
slirp/socks5.c new file +7 -0
+static int socks5_recv_authenticate(int fd)
+{
+    char socksbuf[2];
+    if (recv(fd, socksbuf, 2, 0) != 2) {
+        return -1;
+    }
+    if (socksbuf[0] != 1 || socksbuf[1] != 0) {

These look like magic numbers :) Please document what they represent.

slirp/socks5.c line 116

Quoted 7 lines
slirp/socks5.c new file +7 -0
+    int len;
+
+    memset(&socks5msg, 0, sizeof(socks5msg));
+
+    socks5msg.ver = SOCKS5_VERSION;
+    socks5msg.cmd = SOCKS_CONNECT;
+    socks5msg.rsv = 0;

Please rather set len to 4 here, and increment later on

slirp/socks5.c line 124

Quoted 7 lines
slirp/socks5.c new file +7 -0
+    switch (addr->ss_family) {
+    case AF_INET: {
+            struct sockaddr_in *a = (struct sockaddr_in *)addr;
+
+            socks5msg.atyp = SOCKS5_ATYP_IPv4;
+            memcpy(socks5msg.dst, &a->sin_addr, sizeof(a->sin_addr));
+            len = sizeof(a->sin_addr);

here

slirp/socks5.c line 134

Quoted 10 lines
slirp/socks5.c new file +10 -0
+            memcpy(socks5msg.dst + len, &a->sin_port, sizeof(a->sin_port));
+            len += sizeof(a->sin_port);
+        }
+        break;
+    case AF_INET6: {
+            struct sockaddr_in6 *a = (struct sockaddr_in6 *)addr;
+
+            socks5msg.atyp = SOCKS5_ATYP_IPv6;
+            memcpy(socks5msg.dst, &a->sin6_addr, sizeof(a->sin6_addr));
+            len = sizeof(a->sin6_addr);

and there.

slirp/socks5.c line 143

Quoted 9 lines
slirp/socks5.c new file +9 -0
+            memcpy(socks5msg.dst + len, &a->sin6_port, sizeof(a->sin6_port));
+            len += sizeof(a->sin6_port);
+        }
+        break;
+    default:
+        errno = EINVAL;
+        return -1;
+    }
+    len += 4;

and not have this here, where people wonder what that is :)

slirp/socks5.c line 152

Quoted 5 lines
slirp/socks5.c new file +5 -0
+static int socks5_recv_connect(int fd)
+{
+    struct socks5_request socks5msg;
+
+    if (recv(fd, &socks5msg, 4, 0) != 4) {

Thinking about it (there are more like that above and below). You can theoretically have a short read here... So please at least leave a note that we actually should manage buffering of recv()s.

Quoted 7 lines
slirp/socks5.c new file +7 -0
+++ b/slirp/socks5.h
+struct socks4_data {
+    char version;
+    char type;
+    unsigned short port;
+    uint32_t address;
+    char data[SOCKS_BUFF_SIZE]; /* to hold FQDN and username */

slirp/socks5.c line 76

Quoted 10 lines
slirp/socks5.c new file +10 -0
+} __attribute__((packed));
+
+/* SOCKS4 protocol responses */
+#define SOCKS4_VERSION          4
+#define SOCKS_CONNECT           1
+#define SOCKS_BIND              2
+#define SOCKS4_CONN_ACC         90
+#define SOCKS4_CONN_REF         91
+#define SOCKS4_CONN_IDENT       92
+#define SOCKS4_CONN_IDENTDIFF   93

These are unused, better just drop them?

slirp/socks5.c tcp_sockclosed(struct tcpcb *tp) · line 403

Quoted 11 lines
slirp/socks5.c new file +2 -1
@@ -394,11 +395,21 @@ tcp_sockclosed(struct tcpcb *tp)
 int tcp_fconnect(struct socket *so, unsigned short af)
 {
   int ret=0;
+  Slirp *slirp = so->slirp;
 
   DEBUG_CALL("tcp_fconnect");
   DEBUG_ARG("so = %p", so);
 
-  ret = so->s = qemu_socket(af, SOCK_STREAM, 0);
+  /* local traffic doesn't go to the proxy */

Please fix the comment into "only pass local traffic through the proxy", to actually reflect what the following "if" does :)

slirp/socks5.c tcp_sockclosed(struct tcpcb *tp) · line 408

Quoted 5 lines
slirp/socks5.c new file +5 -0
+  if (slirp->proxy_server &&
+      !(af == AF_INET &&
+        (so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
+        slirp->vnetwork_addr.s_addr)) {
+    ret = so->s = socks5_socket(&so->so_proxy_state);

Please do support AF_INET6 too here, otherwise when working with an ipv6 host setup it will break.

slirp/udp6.c void udp6_input(struct mbuf *m) · line 26

Quoted 11 lines
slirp/udp6.c +1 -1
index 9fa314b..995181d 100644
@@ -22,7 +22,7 @@ void udp6_input(struct mbuf *m)
     DEBUG_CALL("udp6_input");
     DEBUG_ARG("m = %lx", (long)m);
 
-    if (slirp->restricted) {
+    if (slirp->restricted || slirp->proxy_server) {
         goto bad;

Ditto: please do support ipv6 here. It shouldn't be hard, it should be a matter of using the same test as v4 :)

Samuel

#3 lore raw
Quoted 1 line · Samuel Thibault

Hello,

Hi,

Quoted 15 lines

Thanks for the patch!

slirp/slirp.c void slirp_pollfds_poll(GArray *pollfds, int select_error) · line 627

Quoted 8 lines · Laurent Vivier, on mar. 28 mars 2017 21:07:56 +0200, wrote:
slirp/slirp.c +4 -0
@@ -617,6 +621,10 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
                  * Check sockets for reading
                  */
                 else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
+                    if (so->so_state & SS_ISFCONNECTING) {
+                        socks5_recv(so->s, &so->so_proxy_state);
+                        continue;
+                    }

It looks odd to be calling socks5_recv in all cases. Shouldn't this check for so_proxy_state != 0?

We don't need, because we poll for G_IO_IN in SS_ISFCONNECTING state only if so_proxy_state != 0 (see slirp_pollfds_fill() part)

Quoted 8 lines
Quoted 8 lines
slirp/slirp.c +0 -3
@@ -645,11 +653,19 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
                     /*
                      * Check for non-blocking, still-connecting sockets
                      */
-                    if (so->so_state & SS_ISFCONNECTING) {
-                        /* Connected */
-                        so->so_state &= ~SS_ISFCONNECTING;
 
Quoted 5 lines

slirp/slirp.c void slirp_pollfds_poll(GArray *pollfds, int select_error) · line 658

Quoted 3 lines
slirp/slirp.c +2 -1
-                        ret = send(so->s, (const void *) &ret, 0, 0);
+                    if (so->so_state & SS_ISFCONNECTING) {
+                        ret = socks5_send(so->s, slirp->proxy_user,

Ditto. Socks5 and non-socks5 code should be properly separated I guess.

The socks5_send() behavior is based on so_proxy_state, and once the proxy is connected all must be as if we don't have a proxy, so it seems logic to interleave it there.

Quoted 4 lines
Quoted 4 lines
slirp/slirp.c +0 -0
@@ -1069,6 +1085,48 @@ int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
                     htons(guest_port));
 }
 
Quoted 14 lines

slirp/slirp.c int slirp_add_exec(Slirp *slirp, int do_pty, const void *args, · line 1095

Quoted 8 lines
slirp/slirp.c +8 -0
+int slirp_add_proxy(Slirp *slirp, const char *server, int port,
+                    const char *user, const char *password)
+{
+    int fd;
+    socks5_state_t state;
+    struct sockaddr_storage addr;
+
+    /* check the connection */

Please be a bit more verbose :) For instance:

Quoted 2 lines
(quoted diff) +1 -0
+ /* just check that the connection to the socks5 server works with

the given credentials, and close without doing anything with it. */

OK

Quoted 12 lines

slirp/socks5.c line 1

Quoted 8 lines
slirp/socks5.c new file +1 -0
new file mode 100644
index 0000000..1c5e071
@@ -0,0 +1,284 @@
+/* some parts from nmap/ncat GPLv2 */

One can't just steal code like this. You *have* to copy over the copyright notice, since there's notably the author information.

Sorry, I've been naive...

Well, for ncat.h license is 281 lines long (with clarifications and extensions) for 60 lines copied... I think it is better to rewrite this part from the RFC1928.

Quoted 7 lines
Quoted 7 lines
slirp/socks5.c new file +7 -0
+static int socks5_proxy_connect(int fd, const char *server, int port)
+{
+    struct sockaddr_in saddr;
+    struct hostent *he;
+
+    he = gethostbyname(server);
+    if (he == NULL) {
Quoted 8 lines

slirp/socks5.c line 20

Quoted 6 lines
slirp/socks5.c new file +6 -0
+        errno = EINVAL;
+        return -1;
+    }
+    saddr.sin_family = AF_INET;
+    saddr.sin_addr = *(struct in_addr *)he->h_addr;
+    saddr.sin_port = htons(port);

Please add a TODO: v6 version

OK

Quoted 10 lines

slirp/socks5.c line 100

Quoted 8 lines
slirp/socks5.c new file +7 -0
+static int socks5_recv_authenticate(int fd)
+{
+    char socksbuf[2];
+    if (recv(fd, socksbuf, 2, 0) != 2) {
+        return -1;
+    }
+    if (socksbuf[0] != 1 || socksbuf[1] != 0) {

These look like magic numbers :) Please document what they represent.

OK

Quoted 44 lines

slirp/socks5.c line 116

Quoted 8 lines
slirp/socks5.c new file +7 -0
+    int len;
+
+    memset(&socks5msg, 0, sizeof(socks5msg));
+
+    socks5msg.ver = SOCKS5_VERSION;
+    socks5msg.cmd = SOCKS_CONNECT;
+    socks5msg.rsv = 0;

Please rather set len to 4 here, and increment later on

slirp/socks5.c line 124

Quoted 7 lines
slirp/socks5.c new file +7 -0
+    switch (addr->ss_family) {
+    case AF_INET: {
+            struct sockaddr_in *a = (struct sockaddr_in *)addr;
+
+            socks5msg.atyp = SOCKS5_ATYP_IPv4;
+            memcpy(socks5msg.dst, &a->sin_addr, sizeof(a->sin_addr));
+            len = sizeof(a->sin_addr);

here

slirp/socks5.c line 134

Quoted 10 lines
slirp/socks5.c new file +10 -0
+            memcpy(socks5msg.dst + len, &a->sin_port, sizeof(a->sin_port));
+            len += sizeof(a->sin_port);
+        }
+        break;
+    case AF_INET6: {
+            struct sockaddr_in6 *a = (struct sockaddr_in6 *)addr;
+
+            socks5msg.atyp = SOCKS5_ATYP_IPv6;
+            memcpy(socks5msg.dst, &a->sin6_addr, sizeof(a->sin6_addr));
+            len = sizeof(a->sin6_addr);

and there.

slirp/socks5.c line 143

Quoted 9 lines
slirp/socks5.c new file +9 -0
+            memcpy(socks5msg.dst + len, &a->sin6_port, sizeof(a->sin6_port));
+            len += sizeof(a->sin6_port);
+        }
+        break;
+    default:
+        errno = EINVAL;
+        return -1;
+    }
+    len += 4;
Quoted 1 line

and not have this here, where people wonder what that is :)

OK

Quoted 10 lines

slirp/socks5.c line 152

Quoted 6 lines
slirp/socks5.c new file +5 -0
+static int socks5_recv_connect(int fd)
+{
+    struct socks5_request socks5msg;
+
+    if (recv(fd, &socks5msg, 4, 0) != 4) {

Thinking about it (there are more like that above and below). You can theoretically have a short read here... So please at least leave a note that we actually should manage buffering of recv()s.

OK

Quoted 12 lines
Quoted 12 lines
slirp/socks5.c new file +11 -0
+++ b/slirp/socks5.h
+struct socks4_data {
+    char version;
+    char type;
+    unsigned short port;
+    uint32_t address;
+    char data[SOCKS_BUFF_SIZE]; /* to hold FQDN and username */
+} __attribute__((packed));
+
+/* SOCKS4 protocol responses */
+#define SOCKS4_VERSION          4
Quoted 8 lines

slirp/socks5.c line 76

Quoted 6 lines
slirp/socks5.c new file +6 -0
+#define SOCKS_CONNECT           1
+#define SOCKS_BIND              2
+#define SOCKS4_CONN_ACC         90
+#define SOCKS4_CONN_REF         91
+#define SOCKS4_CONN_IDENT       92
+#define SOCKS4_CONN_IDENTDIFF   93

These are unused, better just drop them?

I will rewrite this to avoid copy from ncat.

Quoted 16 lines

slirp/socks5.c tcp_sockclosed(struct tcpcb *tp) · line 403

Quoted 12 lines
slirp/socks5.c new file +2 -1
@@ -394,11 +395,21 @@ tcp_sockclosed(struct tcpcb *tp)
 int tcp_fconnect(struct socket *so, unsigned short af)
 {
   int ret=0;
+  Slirp *slirp = so->slirp;
 
   DEBUG_CALL("tcp_fconnect");
   DEBUG_ARG("so = %p", so);
 
-  ret = so->s = qemu_socket(af, SOCK_STREAM, 0);
+  /* local traffic doesn't go to the proxy */

Please fix the comment into "only pass local traffic through the proxy", to actually reflect what the following "if" does :)

Quoted 8 lines

slirp/socks5.c tcp_sockclosed(struct tcpcb *tp) · line 408

Quoted 5 lines
slirp/socks5.c new file +5 -0
+  if (slirp->proxy_server &&
+      !(af == AF_INET &&
+        (so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
+        slirp->vnetwork_addr.s_addr)) {
+    ret = so->s = socks5_socket(&so->so_proxy_state);

Please do support AF_INET6 too here, otherwise when working with an ipv6 host setup it will break.

OK

Quoted 9 lines
Quoted 9 lines
slirp/udp6.c +0 -0
index 9fa314b..995181d 100644
@@ -22,7 +22,7 @@ void udp6_input(struct mbuf *m)
     DEBUG_CALL("udp6_input");
     DEBUG_ARG("m = %lx", (long)m);
 
Quoted 6 lines

slirp/udp6.c void udp6_input(struct mbuf *m) · line 26

Quoted 3 lines
slirp/udp6.c +1 -1
-    if (slirp->restricted) {
+    if (slirp->restricted || slirp->proxy_server) {
         goto bad;

Ditto: please do support ipv6 here. It shouldn't be hard, it should be a matter of using the same test as v4 :)

OK

Thanks,
Laurent