From 5ecb8bdd8a44422a59f5cbc9abedd1d1717c4247 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Tue, 22 Sep 2015 16:04:48 +0200 Subject: [PATCH] Correct success/error handling for multicast/broadcast sends --- lib/beacon/broadcast.go | 23 ++++++++++++++++++----- lib/beacon/multicast.go | 21 ++++++++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/beacon/broadcast.go b/lib/beacon/broadcast.go index c4ad41099..4910d91ee 100644 --- a/lib/beacon/broadcast.go +++ b/lib/beacon/broadcast.go @@ -124,12 +124,14 @@ func (w *broadcastWriter) Serve() { l.Debugln("addresses:", dsts) } + success := 0 for _, ip := range dsts { dst := &net.UDPAddr{IP: ip, Port: w.port} w.conn.SetWriteDeadline(time.Now().Add(time.Second)) _, err := w.conn.WriteTo(bs, dst) w.conn.SetWriteDeadline(time.Time{}) + if err, ok := err.(net.Error); ok && err.Timeout() { // Write timeouts should not happen. We treat it as a fatal // error on the socket. @@ -138,23 +140,34 @@ func (w *broadcastWriter) Serve() { } w.setError(err) return - } else if err, ok := err.(net.Error); ok && err.Temporary() { + } + + if err, ok := err.(net.Error); ok && err.Temporary() { // A transient error. Lets hope for better luck in the future. if debug { l.Debugln(err) } continue - } else if err != nil { + } + + if err != nil { // Some other error that we don't expect. Bail and retry. if debug { l.Debugln(err) } w.setError(err) return - } else if debug { - l.Debugf("sent %d bytes to %s", len(bs), dst) - w.setError(nil) } + + if debug { + l.Debugf("sent %d bytes to %s", len(bs), dst) + } + + success++ + } + + if success > 0 { + w.setError(nil) } } } diff --git a/lib/beacon/multicast.go b/lib/beacon/multicast.go index 030944c96..8b9ce6455 100644 --- a/lib/beacon/multicast.go +++ b/lib/beacon/multicast.go @@ -124,19 +124,26 @@ func (w *multicastWriter) Serve() { return } - var success int - + success := 0 for _, intf := range intfs { wcm.IfIndex = intf.Index pconn.SetWriteDeadline(time.Now().Add(time.Second)) _, err = pconn.WriteTo(bs, wcm, gaddr) pconn.SetWriteDeadline(time.Time{}) - if err != nil && debug { - l.Debugln(err, "on write to", gaddr, intf.Name) - } else if debug { - l.Debugf("sent %d bytes to %v on %s", len(bs), gaddr, intf.Name) - success++ + + if err != nil { + if debug { + l.Debugln(err, "on write to", gaddr, intf.Name) + } + w.setError(err) + continue } + + if debug { + l.Debugf("sent %d bytes to %v on %s", len(bs), gaddr, intf.Name) + } + + success++ } if success > 0 {