From 5c0f0480bc951dcc22d719256087da3da0034330 Mon Sep 17 00:00:00 2001 From: Scott Feldman Date: Tue, 9 Jan 2024 16:19:44 -0800 Subject: [PATCH] fix wifinina UDP send Fix an error I introduced in porting wifinina to netdev. The driver was starting a client on the socket once, during Connect. The first UDP send on the socket would succeed, any subsequent sends would fail. The fix is to start the client on the socket for each UDP send. I think I see the logic in this design, so the fix makes sense. If the device was sending to many UDP clients, it could use a single socket, but change the dst addr for each send. The pkt data would be queued to hw just once, and then sent from hw to each client dst addr. This would be a real efficient way to multicast to many clients. --- wifinina/wifinina.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/wifinina/wifinina.go b/wifinina/wifinina.go index 3c7af39..c0c3a66 100644 --- a/wifinina/wifinina.go +++ b/wifinina/wifinina.go @@ -165,7 +165,8 @@ type hwerr uint8 type socket struct { protocol int - ip netip.AddrPort + laddr netip.AddrPort // Set in Bind() + raddr netip.AddrPort // Set in Connect() inuse bool } @@ -603,7 +604,7 @@ func (w *wifinina) Bind(sockfd int, ip netip.AddrPort) error { w.startServer(sock, ip.Port(), protoModeUDP) } - socket.ip = ip + socket.laddr = ip return nil } @@ -638,7 +639,8 @@ func (w *wifinina) Connect(sockfd int, host string, ip netip.AddrPort) error { case netdev.IPPROTO_TLS: w.startClient(sock, host, 0, ip.Port(), protoModeTLS) case netdev.IPPROTO_UDP: - w.startClient(sock, "", toUint32(ip.Addr().As4()), ip.Port(), protoModeUDP) + // See start in sendUDP() + socket.raddr = ip return nil } @@ -667,7 +669,7 @@ func (w *wifinina) Listen(sockfd int, backlog int) error { switch socket.protocol { case netdev.IPPROTO_TCP: - w.startServer(sock, socket.ip.Port(), protoModeTCP) + w.startServer(sock, socket.laddr.Port(), protoModeTCP) case netdev.IPPROTO_UDP: default: return netdev.ErrProtocolNotSupported @@ -794,7 +796,10 @@ func (w *wifinina) sendTCP(sock sock, buf []byte, deadline time.Time) (int, erro return -1, netdev.ErrTimeout } -func (w *wifinina) sendUDP(sock sock, buf []byte, deadline time.Time) (int, error) { +func (w *wifinina) sendUDP(sock sock, raddr netip.AddrPort, buf []byte, deadline time.Time) (int, error) { + + // Start a client for each send + w.startClient(sock, "", toUint32(raddr.Addr().As4()), raddr.Port(), protoModeUDP) // Queue it ok := w.insertDataBuf(sock, buf) @@ -826,7 +831,7 @@ func (w *wifinina) sendChunk(sockfd int, buf []byte, deadline time.Time) (int, e case netdev.IPPROTO_TCP, netdev.IPPROTO_TLS: return w.sendTCP(sock, buf, deadline) case netdev.IPPROTO_UDP: - return w.sendUDP(sock, buf, deadline) + return w.sendUDP(sock, socket.raddr, buf, deadline) } return -1, netdev.ErrProtocolNotSupported