Add a feature for mapping a host unix socket to a guest tcp socket
PATCH v4 1/1 Add a feature for mapping a host unix socket to a guest tcp socket
This patch adds the ability to map a host unix socket to a guest tcp socket when using the slirp backend. This feature was added in libslirp version 4.7.0.
A new syntax for unix socket: -hostfwd=unix:hostpath-[guestaddr]:guestport
---
A reworked solution. Now the host address is not parsed when using a unix socket. Documentation has also been added.
docs/system/devices/net.rst | 2 +- hmp-commands.hx | 4 +- net/slirp.c | 102 +++++++++++++++++++++++++++--------- qapi/net.json | 2 +- qemu-options.hx | 11 +++- 5 files changed, 91 insertions(+), 30 deletions(-)
docs/system/devices/net.rst +1 -1
@@ -79,7 +79,7 @@ those sockets. To allow ping for GID 100 (usually users group):: When using the built-in TFTP server, the router is also the TFTP server. -When using the ``'-netdev user,hostfwd=...'`` option, TCP or UDP +When using the ``'-netdev user,hostfwd=...'`` option, TCP, UDP or UNIX connections can be redirected from the host to the guest. It allows for example to redirect X11, telnet or SSH connections.
hmp-commands.hx +2 -2
@@ -1357,8 +1357,8 @@ ERST { .name = "hostfwd_add", .args_type = "arg1:s,arg2:s?", - .params = "[netdev_id] [tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport", - .help = "redirect TCP or UDP connections from host to guest (requires -net user)", + .params = "[netdev_id] [tcp|udp|unix]:[[hostaddr]:hostport|hostpath]-[guestaddr]:guestport", + .help = "redirect TCP, UDP or UNIX connections from host to guest (requires -net user)", .cmd = hmp_hostfwd_add, }, #endif
net/slirp.c +78 -24
136 lines not on this page · raw message
qapi/net.json +1 -1
@@ -281,7 +281,7 @@ # # @smbserver: IP address of the built-in SMB server # -# @hostfwd: redirect incoming TCP or UDP host connections to guest +# @hostfwd: redirect incoming TCP, UDP or UNIX host connections to guest # endpoints # # @guestfwd: forward guest TCP connections
qemu-options.hx +9 -2
@@ -3317,8 +3317,8 @@ SRST Note that a SAMBA server must be installed on the host OS. - ``hostfwd=[tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport`` - Redirect incoming TCP or UDP connections to the host port + ``hostfwd=[tcp|udp|unix]:[[hostaddr]:hostport|hostpath]-[guestaddr]:guestport`` + Redirect incoming TCP, UDP or UNIX connections to the host port hostport to the guest IP address guestaddr on guest port guestport. If guestaddr is not specified, its value is x.x.x.15 (default first address given by the built-in DHCP server). By
@@ -3348,6 +3348,13 @@ SRST Then when you use on the host ``telnet localhost 5555``, you connect to the guest telnet server. + To redirect host unix socket /tmp/vm to guest tcp socket 23 use + following: + + .. parsed-literal:: + # on the host + |qemu_system| -nic user,hostfwd=unix:/tmp/vm-:23 + ``guestfwd=[tcp]:server:port-dev``; \ ``guestfwd=[tcp]:server:port-cmd:command`` Forward guest TCP connections to the IP address server on port port to the character device dev or to a program executed by
Signature · 1 line
2.50.1
We're almost there :)
Viktor Kurilko, le mar. 05 août 2025 21:52:50 +0700, a ecrit:
Quoted 17 lines
net/slirp.c +3
-8@@ -824,30 +827,81 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) is_udp =0 ; }else if (!strcmp (buf,"udp" )) { is_udp =1 ; - }else { - fail_reason ="Bad protocol name" ; - goto fail_syntax; } - -if (get_str_sep (buf,sizeof (buf), &p,':' ) <0 ) { - fail_reason ="Missing : separator" ; - goto fail_syntax; +#if !defined(WIN32 ) &&SLIRP_CHECK_VERSION (4 ,7 ,0 ) + else if (!strcmp(buf, "unix")) { + is_unix =1 ; } -if (buf[0 ] !='\0' && !inet_aton (buf, &host_addr.sin_addr)) {
net/slirp.c static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) · line 841
Quoted 10 lines
net/slirp.c +4
-3- fail_reason ="Bad host address" ; +#endif + else { + fail_reason ="Bad protocol name" ; goto fail_syntax; } -if (get_str_sep (buf,sizeof (buf), &p,'-' ) <0 ) { - fail_reason ="Bad host port separator" ; +if (get_str_sep (buf,sizeof (buf), &p, is_unix ?'-' :':' ) <0 ) {
This factorization looks odd. I don't think it's actually more readable to try to factorize some separator parsing. I'd say rather have an if (unix) part and else (ip) part, which both make their own get_str_sep calls, and factorize just the parsing of the guest part.
Quoted 9 lines
net/slirp.c +9
-0+#endif + { +if (buf[0 ] !='\0' && !inet_aton (buf, &host_addr.in.sin_addr)) { + fail_reason ="Bad host address" ; + goto fail_syntax; + } + +if (get_str_sep (buf,sizeof (buf), &p,'-' ) <0 ) { + fail_reason ="Bad host port separator" ;
net/slirp.c static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) · line 901
Quoted 10 lines
net/slirp.c +10
-0+ goto fail_syntax; + } + + err =qemu_strtoi (buf, &end,0 , &host_port); +if (err || host_port <0 || host_port >65535 ) { + fail_reason ="Bad host port" ; + goto fail_syntax; + } + host_addr.in.sin_family =AF_INET ; + host_addr.in.sin_addr.s_addr =INADDR_ANY ;
This is overwriting the inet_aton call above?
net/slirp.c static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) · line 907
Quoted 7 lines
net/slirp.c +2
-1+ host_addr.in.sin_port =htons (host_port); + host_addr_size =sizeof (host_addr.in); } - host_addr.sin_port =htons (host_port);if (get_str_sep (buf,sizeof (buf), &p,':' ) <0 ) { fail_reason ="Missing guest address" ;
Samuel