From nobody Thu Nov 14 23:41:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1666348777769865.8204055281841; Fri, 21 Oct 2022 03:39:37 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olpR6-0001jp-TB for importer@patchew.org; Fri, 21 Oct 2022 06:39:34 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2c-0003qk-2L for importer@patchew.org; Fri, 21 Oct 2022 05:10:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2M-0003RW-OK for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:57 -0400 Received: from mout.kundenserver.de ([217.72.192.73]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2I-0001eU-BS for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:54 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MYN3E-1oXyNi3IgM-00VS5K; Fri, 21 Oct 2022 11:09:41 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Cc: Thomas Huth , xen-devel@lists.xenproject.org, "Dr. David Alan Gilbert" , Laurent Vivier , Anthony Perard , Stefan Weil , David Gibson , Stefano Stabellini , Paul Durrant , Eric Blake , "Michael S. Tsirkin" , Jason Wang , Paolo Bonzini , Markus Armbruster , Samuel Thibault , Greg Kurz , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Stefano Brivio , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v14 10/17] net: dgram: make dgram_dst generic Date: Fri, 21 Oct 2022 11:09:15 +0200 Message-Id: <20221021090922.170074-11-lvivier@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221021090922.170074-1-lvivier@redhat.com> References: <20221021090922.170074-1-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:mVD/M1LlMvHfxLyz+io1y8fQZ+rSM1FQEF+POP1VXOpyJrA8Ppz UfU9pvOBQhjYVxGt8Ekh8r26kukC+rIyR09Zjm5PyUuOd8zBuOkCjCM3j8uIp6dQG811qw/ IW5YO8fncXi4SWcz4alhom/gLum3z0+8JqBNn73FuD0zIGU1dw601PHGRkNcjzjIZGskqcr 0H3Tg8adsdHE36OnDTPOw== X-UI-Out-Filterresults: notjunk:1;V03:K0:o2nAJVUvtSo=:ws5laaF9m86nNqe2XOMp4F M61/rlcA+DCYltjAVsPue0Ca5FpJDqvcwPtE/jkqv8RJz7+unX8RqXv8hn+U/B1QlfTqh5UCi quge/NGCcOveyJ6R1pApaLNr5TOFcK76tV4z5wWvTgaiuKFJTRb8sb8B7vMR2037f2GY9f/CD KUOlsXkYP/ZCPzoOqyITEiVyqMBIRyFdSp3Oi+Ta5GOlSfULYdIr4/eG+PdLvMmewtnRvzW5O /W48OGCSUu9IgYwVdi4SPK1snGT936zHHTA7MOAO7v4n1VCnYS+KIaKRqq+ONp0gLHswxoaaV SKmBwSa27A+et2lnKw9HZPjf4YqjYSwvj5FKwy1ZTbydhjHJc1lwxSWmKlB+GxA5EusZ0K4t6 tGvKYEnZ0C3J8UUucWpSIRHPr+h+66WOWtVhsCysB63zGfxScXl3kUTrC6/WpmDQIJYzq598e /2vDo0NTG69nBctGcw0GOrArlUncOxfzys3nPi8rDDDkoH7m6CnEg9xK+GWTmlQ7Xdyt/yWrP tU3LD08n5e3NJ+9/BaPdtJKg6n1/LtLi7jdLAl7zFLrzrODZsmWXmWrCIBJW/c2wwOJNyPqpF arY2zhCyslydGtoM58SDwNj3IxPSxFgFE6QeiI2/OMLPQhPBSBZDA7MvzQ11gCY7n30eMk6Lw 7/ZFfJo5HZGumR2uWA9w4nTYlTCdYLoSCeYwLGClVD/N165SezruRUJNySdI9P1IoQKIGhL3m ISz+m1gPXVA6O5xBmf/1PWfuDg6Z4psVkc+L0z2vWm8ko/cjPb8Dvoo85Z9g104mKIw/k7ZpE c2QsAnr Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: permerror client-ip=217.72.192.73; envelope-from=lvivier@redhat.com; helo=mout.kundenserver.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_FAIL=0.001, SPF_HELO_NONE=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666348779722100003 dgram_dst is a sockaddr_in structure. To be able to use it with unix socket, use a pointer to a generic sockaddr structure. Rename it dest_addr, and store socket length in dest_len. Signed-off-by: Laurent Vivier Reviewed-by: Stefano Brivio Acked-by: Michael S. Tsirkin Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- net/dgram.c | 82 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/net/dgram.c b/net/dgram.c index 5339585b8202..e20be9ca79d3 100644 --- a/net/dgram.c +++ b/net/dgram.c @@ -40,9 +40,11 @@ typedef struct NetDgramState { NetClientState nc; int fd; SocketReadState rs; - struct sockaddr_in dgram_dst; /* contains destination iff connectionle= ss */ bool read_poll; /* waiting to receive data? */ bool write_poll; /* waiting to transmit data? */ + /* contains destination iff connectionless */ + struct sockaddr *dest_addr; + socklen_t dest_len; } NetDgramState; =20 static void net_dgram_send(void *opaque); @@ -84,10 +86,8 @@ static ssize_t net_dgram_receive(NetClientState *nc, ssize_t ret; =20 do { - if (s->dgram_dst.sin_family !=3D AF_UNIX) { - ret =3D sendto(s->fd, buf, size, 0, - (struct sockaddr *)&s->dgram_dst, - sizeof(s->dgram_dst)); + if (s->dest_addr) { + ret =3D sendto(s->fd, buf, size, 0, s->dest_addr, s->dest_len); } else { ret =3D send(s->fd, buf, size, 0); } @@ -244,6 +244,9 @@ static void net_dgram_cleanup(NetClientState *nc) close(s->fd); s->fd =3D -1; } + g_free(s->dest_addr); + s->dest_addr =3D NULL; + s->dest_len =3D 0; } =20 static NetClientInfo net_dgram_socket_info =3D { @@ -260,7 +263,7 @@ static NetDgramState *net_dgram_fd_init(NetClientState = *peer, SocketAddress *mcast, Error **errp) { - struct sockaddr_in saddr; + struct sockaddr_in *saddr =3D NULL; int newfd; NetClientState *nc; NetDgramState *s; @@ -275,31 +278,32 @@ static NetDgramState *net_dgram_fd_init(NetClientStat= e *peer, qapi_free_SocketAddress(sa); =20 /* - * fd passed: multicast: "learn" dgram_dst address from bound address = and + * fd passed: multicast: "learn" dest_addr address from bound address = and * save it. Because this may be "shared" socket from a "master" proces= s, * datagrams would be recv() by ONLY ONE process: we must "clone" this * dgram socket --jjo */ =20 if (is_fd && mcast !=3D NULL) { - if (convert_host_port(&saddr, mcast->u.inet.host, - mcast->u.inet.port, errp) < 0) { + saddr =3D g_new(struct sockaddr_in, 1); + + if (convert_host_port(saddr, mcast->u.inet.host, mcast->u.inet= .port, + errp) < 0) { goto err; } /* must be bound */ - if (saddr.sin_addr.s_addr =3D=3D 0) { + if (saddr->sin_addr.s_addr =3D=3D 0) { error_setg(errp, "can't setup multicast destination addres= s"); goto err; } /* clone dgram socket */ - newfd =3D net_dgram_mcast_create(&saddr, NULL, errp); + newfd =3D net_dgram_mcast_create(saddr, NULL, errp); if (newfd < 0) { goto err; } /* clone newfd to fd, close newfd */ dup2(newfd, fd); close(newfd); - } =20 nc =3D qemu_new_net_client(&net_dgram_socket_info, peer, model, name); @@ -311,21 +315,20 @@ static NetDgramState *net_dgram_fd_init(NetClientStat= e *peer, net_dgram_read_poll(s, true); =20 /* mcast: save bound address as dst */ - if (is_fd && mcast !=3D NULL) { - s->dgram_dst =3D saddr; + if (saddr) { + g_assert(s->dest_addr =3D=3D NULL); + s->dest_addr =3D (struct sockaddr *)saddr; + s->dest_len =3D sizeof(*saddr); qemu_set_info_str(nc, "fd=3D%d (cloned mcast=3D%s:%d)", fd, - inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)= ); + inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_por= t)); } else { - if (sa_type =3D=3D SOCKET_ADDRESS_TYPE_UNIX) { - s->dgram_dst.sin_family =3D AF_UNIX; - } - qemu_set_info_str(nc, "fd=3D%d %s", fd, SocketAddressType_str(sa_t= ype)); } =20 return s; =20 err: + g_free(saddr); closesocket(fd); return NULL; } @@ -339,21 +342,24 @@ static int net_dgram_mcast_init(NetClientState *peer, { NetDgramState *s; int fd, ret; - struct sockaddr_in saddr; + struct sockaddr_in *saddr; =20 if (remote->type !=3D SOCKET_ADDRESS_TYPE_INET) { error_setg(errp, "multicast only support inet type"); return -1; } =20 - if (convert_host_port(&saddr, remote->u.inet.host, remote->u.inet.port, + saddr =3D g_new(struct sockaddr_in, 1); + if (convert_host_port(saddr, remote->u.inet.host, remote->u.inet.port, errp) < 0) { + g_free(saddr); return -1; } =20 if (!local) { - fd =3D net_dgram_mcast_create(&saddr, NULL, errp); + fd =3D net_dgram_mcast_create(saddr, NULL, errp); if (fd < 0) { + g_free(saddr); return -1; } } else { @@ -362,13 +368,15 @@ static int net_dgram_mcast_init(NetClientState *peer, struct in_addr localaddr; =20 if (inet_aton(local->u.inet.host, &localaddr) =3D=3D 0) { + g_free(saddr); error_setg(errp, "localaddr '%s' is not a valid IPv4 addre= ss", local->u.inet.host); return -1; } =20 - fd =3D net_dgram_mcast_create(&saddr, &localaddr, errp); + fd =3D net_dgram_mcast_create(saddr, &localaddr, errp); if (fd < 0) { + g_free(saddr); return -1; } break; @@ -376,16 +384,19 @@ static int net_dgram_mcast_init(NetClientState *peer, case SOCKET_ADDRESS_TYPE_FD: fd =3D monitor_fd_param(monitor_cur(), local->u.fd.str, errp); if (fd =3D=3D -1) { + g_free(saddr); return -1; } ret =3D qemu_socket_try_set_nonblock(fd); if (ret < 0) { + g_free(saddr); error_setg_errno(errp, -ret, "%s: Can't use file descripto= r %d", name, fd); return -1; } break; default: + g_free(saddr); error_setg(errp, "only support inet or fd type for local"); return -1; } @@ -395,13 +406,17 @@ static int net_dgram_mcast_init(NetClientState *peer, local->type =3D=3D SOCKET_ADDRESS_TYPE_FD, remote, errp); if (!s) { + g_free(saddr); return -1; } =20 - s->dgram_dst =3D saddr; + g_assert(s->dest_addr =3D=3D NULL); + s->dest_addr =3D (struct sockaddr *)saddr; + s->dest_len =3D sizeof(*saddr); + + qemu_set_info_str(&s->nc, "mcast=3D%s:%d", inet_ntoa(saddr->sin_addr), + ntohs(saddr->sin_port)); =20 - qemu_set_info_str(&s->nc, "mcast=3D%s:%d", inet_ntoa(saddr.sin_addr), - ntohs(saddr.sin_port)); return 0; =20 } @@ -412,9 +427,10 @@ int net_init_dgram(const Netdev *netdev, const char *n= ame, { NetDgramState *s; int fd, ret; - struct sockaddr_in raddr_in; - struct sockaddr_in laddr_in; SocketAddress *remote, *local; + struct sockaddr *dest_addr; + struct sockaddr_in laddr_in, raddr_in; + socklen_t dest_len; =20 assert(netdev->type =3D=3D NET_CLIENT_DRIVER_DGRAM); =20 @@ -491,6 +507,10 @@ int net_init_dgram(const Netdev *netdev, const char *n= ame, return -1; } qemu_socket_set_nonblock(fd); + + dest_len =3D sizeof(raddr_in); + dest_addr =3D g_malloc(dest_len); + memcpy(dest_addr, &raddr_in, dest_len); break; case SOCKET_ADDRESS_TYPE_FD: fd =3D monitor_fd_param(monitor_cur(), local->u.fd.str, errp); @@ -503,6 +523,8 @@ int net_init_dgram(const Netdev *netdev, const char *na= me, name, fd); return -1; } + dest_addr =3D NULL; + dest_len =3D 0; break; default: error_setg(errp, "only support inet or fd type for local"); @@ -515,7 +537,9 @@ int net_init_dgram(const Netdev *netdev, const char *na= me, } =20 if (remote) { - s->dgram_dst =3D raddr_in; + g_assert(s->dest_addr =3D=3D NULL); + s->dest_addr =3D dest_addr; + s->dest_len =3D dest_len; } =20 switch (local->type) { --=20 2.37.3