Skip to content

Commit

Permalink
fix wifinina UDP send
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
scottfeldman authored and deadprogram committed Jan 10, 2024
1 parent 8642886 commit 5c0f048
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions wifinina/wifinina.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5c0f048

Please sign in to comment.