Skip to content

Commit

Permalink
Cope with clients using the broadcast address
Browse files Browse the repository at this point in the history
Some clients use the broadcast address (255.255.255.255) to reach the
TFTP server in their network. This currently doesn't work because the
server then use this address as the source address in its responses.
But the broadcast address is not a valid source address and the
sendmsg call fail with an `invalid argument` error.

To cope with this check if the source address is the broadcast address
and if so replace it with 0.0.0.0 to use the OS default.

Signed-off-by: Alban Bedel <[email protected]>
  • Loading branch information
AlbanBedel committed Oct 11, 2023
1 parent 90a0661 commit 01410ee
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ func (s *Server) Shutdown() {
func (s *Server) handlePacket(localAddr net.IP, remoteAddr *net.UDPAddr, buffer []byte, n, maxBlockLen int, listener chan []byte) error {
s.Lock()
defer s.Unlock()

// Cope with packets received on the broadcast address
// We can't use this address as the source address in responses
// so fallback to the OS default.
if localAddr.Equal(net.IPv4bcast) {
localAddr = net.IPv4zero
}

// handlePacket is always called with maxBlockLen = blockLength (above, in processRequest).
// As a result, the block size would always be capped at 512 bytes, even when the tftp
// client indicated to use a larger value. So override that value. And make sure to
Expand Down

0 comments on commit 01410ee

Please sign in to comment.