diff mbox

slirp: fix guest network access with darwin host

Message ID 1461846412-12596-1-git-send-email-samuel.thibault@ens-lyon.org (mailing list archive)
State New, archived
Headers show

Commit Message

Samuel Thibault April 28, 2016, 12:26 p.m. UTC
On darwin, connect, sendto and friends want the exact size of the sockaddr,
not more.  This adds the sockaddr_size helper to be used when passing a
sockaddr size to such function, and makes use of it for sendto and connect
calls.

Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
---
 slirp/ip_icmp.c  |  2 +-
 slirp/socket.c   |  2 +-
 slirp/socket.h   | 12 ++++++++++++
 slirp/tcp_subr.c |  2 +-
 4 files changed, 15 insertions(+), 3 deletions(-)

Comments

Thomas Huth April 28, 2016, 6:54 p.m. UTC | #1
On 28.04.2016 14:26, Samuel Thibault wrote:
> On darwin, connect, sendto and friends want the exact size of the sockaddr,
> not more.  This adds the sockaddr_size helper to be used when passing a
> sockaddr size to such function, and makes use of it for sendto and connect
> calls.
> 
> Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
> ---
>  slirp/ip_icmp.c  |  2 +-
>  slirp/socket.c   |  2 +-
>  slirp/socket.h   | 12 ++++++++++++
>  slirp/tcp_subr.c |  2 +-
>  4 files changed, 15 insertions(+), 3 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>
diff mbox

Patch

diff --git a/slirp/ip_icmp.c b/slirp/ip_icmp.c
index 590dada..5ffc7a6 100644
--- a/slirp/ip_icmp.c
+++ b/slirp/ip_icmp.c
@@ -186,7 +186,7 @@  icmp_input(struct mbuf *m, int hlen)
       sotranslate_out(so, &addr);
 
       if(sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
-		(struct sockaddr *)&addr, sizeof(addr)) == -1) {
+		(struct sockaddr *)&addr, sockaddr_size(&addr)) == -1) {
 	DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
 		    errno,strerror(errno)));
 	icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
diff --git a/slirp/socket.c b/slirp/socket.c
index bd97b2d..a10eff1 100644
--- a/slirp/socket.c
+++ b/slirp/socket.c
@@ -627,7 +627,7 @@  sosendto(struct socket *so, struct mbuf *m)
 
 	/* Don't care what port we get */
 	ret = sendto(so->s, m->m_data, m->m_len, 0,
-		     (struct sockaddr *)&addr, sizeof(addr));
+		     (struct sockaddr *)&addr, sockaddr_size(&addr));
 	if (ret < 0)
 		return -1;
 
diff --git a/slirp/socket.h b/slirp/socket.h
index 7dca506..b602e69 100644
--- a/slirp/socket.h
+++ b/slirp/socket.h
@@ -122,6 +122,18 @@  static inline int sockaddr_equal(struct sockaddr_storage *a,
     return 0;
 }
 
+static inline socklen_t sockaddr_size(struct sockaddr_storage *a)
+{
+    switch (a->ss_family) {
+    case AF_INET:
+        return sizeof(struct sockaddr_in);
+    case AF_INET6:
+        return sizeof(struct sockaddr_in6);
+    default:
+        g_assert_not_reached();
+    }
+}
+
 struct socket *solookup(struct socket **, struct socket *,
         struct sockaddr_storage *, struct sockaddr_storage *);
 struct socket *socreate(Slirp *);
diff --git a/slirp/tcp_subr.c b/slirp/tcp_subr.c
index 32ff452..6b9fef2 100644
--- a/slirp/tcp_subr.c
+++ b/slirp/tcp_subr.c
@@ -413,7 +413,7 @@  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,sizeof (addr));
+    ret = connect(s, (struct sockaddr *)&addr, sockaddr_size(&addr));
 
     /*
      * If it's not in progress, it failed, so we just return 0,