Add a feature for mapping a host unix socket to a guest tcp socket
PATCH v3 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.
---
Fixed initialization of the inet structure.
Quoted 1 line
Does that not behave oddly when given a path?
I didn't quite understand what you meant. If you mean that we try to parse the host address despite the fact that it doesn't make sense for a unix socket, then I added a check that in the case of using a unix socket, the host address should be empty. As an alternative solution, I can suggest specifying the unix socket path in the host address place and omitting the port initialization in this case.
net/slirp.c | 69 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 13 deletions(-)
net/slirp.c +56 -13
@@ -795,12 +795,13 @@ void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)static int slirp_hostfwd (SlirpState *s,const char *redir_str,Error **errp) { -struct sockaddr_in host_addr = { - .sin_family =AF_INET , - .sin_addr = { - .s_addr =INADDR_ANY , - }, - }; +union { +struct sockaddr_in in; +#if !defined(WIN32 ) &&SLIRP_CHECK_VERSION (4 ,7 ,0 ) +struct sockaddr_un un; +#endif + } host_addr = {0 }; +struct sockaddr_in guest_addr = { .sin_family =AF_INET , .sin_addr = {
@@ -814,6 +815,7 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)int is_udp;const char *end;const char *fail_reason ="Unknown reason" ; +socklen_t host_addr_size; p = redir_str;if (!p ||get_str_sep (buf,sizeof (buf), &p,':' ) <0 ) {
@@ -833,7 +835,7 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) fail_reason ="Missing : separator" ; goto fail_syntax; } -if (buf[0 ] !='\0' && !inet_aton (buf, &host_addr.sin_addr)) { +if (buf[0 ] !='\0' && !inet_aton (buf, &host_addr.in.sin_addr)) { fail_reason ="Bad host address" ; goto fail_syntax; }
@@ -842,12 +844,53 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) fail_reason ="Bad host port separator" ; 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; +#if !defined(WIN32 ) &&SLIRP_CHECK_VERSION (4 ,7 ,0 ) +if (buf[0 ] =='/' ) { +if (is_udp) { + fail_reason ="Mapping unix to udp is not supported" ; + goto fail_syntax; + } + +if (host_addr.in.sin_addr.s_addr !=0 ) { + fail_reason = +"the host address must be empty when using a unix socket" ; + goto fail_syntax; + } + +size_t path_len =strlen (buf); +if (path_len >sizeof (host_addr.un.sun_path) -1 ) { + fail_reason ="Unix socket path is too long" ; + goto fail_syntax; + } + +struct stat st; +if (stat (buf, &st) ==0 ) { +if (!S_ISSOCK (st.st_mode)) { + fail_reason ="file exists and it's not unix socket" ; + goto fail_syntax; + } + +if (unlink (buf) <0 ) { +error_setg_errno (errp, errno,"Failed to unlink '%s'" , buf); + goto fail_syntax; + } + } + host_addr.un.sun_family =AF_UNIX ; +memcpy (host_addr.un.sun_path, buf, path_len); + host_addr_size =sizeof (host_addr.un); + } else +#endif + { + 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 ; + 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" ;
@@ -867,7 +910,7 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)#if SLIRP_CHECK_VERSION (4 ,5 ,0 ) err =slirp_add_hostxfwd (s->slirp, - (struct sockaddr *) &host_addr,sizeof (host_addr), + (struct sockaddr *) &host_addr, host_addr_size, (struct sockaddr *) &guest_addr,sizeof (guest_addr), is_udp ?SLIRP_HOSTFWD_UDP :0 );#else
Signature · 1 line
2.50.1
Hello,
Viktor Kurilko, le lun. 04 août 2025 21:05:01 +0700, a ecrit:
Quoted 4 lines
Quoted 1 line
Does that not behave oddly when given a path?
I didn't quite understand what you meant. If you mean that we try to parse the host address despite the fact that it doesn't make sense for a unix socket,
Yes.
Quoted 4 lines
then I added a check that in the case of using a unix socket, the host address should be empty. As an alternative solution, I can suggest specifying the unix socket
path in the host address place and omitting the port initialization in this case.
-serial uses:
tcp:[host]:port[,server=on|off][,wait=on|off][,node‐ lay=on|off][,reconnect-ms=milliseconds]
The TCP Net Console has two modes of operation. It can
unix:path[,server=on|off][,wait=on|off][,reconnect-ms=millisec‐ onds]
A unix domain socket is used instead of a tcp socket. The
So I'd say we'd want to follow the same principle, i.e.
-hostfwd=unix:hostpath-[guestaddr]:guestport
BTW, your patch needs to update the various documentations of hostfwd, so users now that it's now possible to use unix sockets.
Samuel