diff --git a/conn.go b/conn.go index e867aec..a7b04c3 100644 --- a/conn.go +++ b/conn.go @@ -456,6 +456,10 @@ func (conn *Conn) handlePacket(b []byte) error { if len(b) == 0 { return errZeroPacket } + if conn.closing.Load() != 0 { + // Don't continue handling packets if the connection is being closed. + return nil + } handled, err := conn.handler.handle(conn, b) if err != nil { return fmt.Errorf("handle packet: %w", err) @@ -623,7 +627,7 @@ func (conn *Conn) writeTo(p []byte, raddr net.Addr) error { if _, err := conn.conn.WriteTo(p, raddr); errors.Is(err, net.ErrClosed) { return fmt.Errorf("write to: %w", err) } else if err != nil { - conn.handler.log().Error("write to: " + err.Error()) + conn.handler.log().Error("write to: "+err.Error(), "raddr", raddr.String()) } return nil } diff --git a/dial.go b/dial.go index 6e16d42..f019e06 100644 --- a/dial.go +++ b/dial.go @@ -218,6 +218,7 @@ func (dialer Dialer) DialContext(ctx context.Context, address string) (*Conn, er if err != nil { return nil, dialer.error("dial", err) } + dialer.ErrorLog = dialer.ErrorLog.With("src", "dialer", "raddr", conn.RemoteAddr().String()) cs := &connState{conn: conn, raddr: conn.RemoteAddr(), id: atomic.AddInt64(&dialerID, 1), ticker: time.NewTicker(time.Second / 2)} defer cs.ticker.Stop() @@ -268,7 +269,7 @@ func (dialer Dialer) clientListen(rakConn *Conn, conn net.Conn) { } // Errors reading a packet other than the connection being // closed may be worth logging. - dialer.ErrorLog.Error("client: " + err.Error()) + dialer.ErrorLog.Error("handle packet: " + err.Error()) } } } diff --git a/handler.go b/handler.go index 261d20c..735f1a3 100644 --- a/handler.go +++ b/handler.go @@ -70,9 +70,10 @@ func (h listenerConnectionHandler) handleUnconnected(b []byte, addr net.Addr) er // In some cases, the client will keep trying to send datagrams // while it has already timed out. In this case, we should not return // an error. + h.log().Debug("unexpected datagram", "raddr", addr.String()) return nil } - return fmt.Errorf("unknown packet received (id=%x, len=%v)", b[0], len(b)) + return fmt.Errorf("unknown unconnected packet (id=%x, len=%v)", b[0], len(b)) } // handleUnconnectedPing handles an unconnected ping packet stored in buffer b, diff --git a/listener.go b/listener.go index 6b743ad..d081174 100644 --- a/listener.go +++ b/listener.go @@ -83,6 +83,8 @@ func (conf ListenConfig) Listen(address string) (*Listener, error) { if conf.ErrorLog == nil { conf.ErrorLog = slog.New(internal.DiscardHandler{}) } + conf.ErrorLog = conf.ErrorLog.With("src", "listener") + if conf.BlockDuration == 0 { conf.BlockDuration = time.Second * 10 } @@ -184,7 +186,7 @@ func (listener *Listener) listen() { continue } if err = listener.handle(b[:n], addr); err != nil && !errors.Is(err, net.ErrClosed) { - listener.conf.ErrorLog.Error("listener: handle packet: "+err.Error(), "address", addr.String(), "block-duration", max(0, listener.conf.BlockDuration)) + listener.conf.ErrorLog.Error("handle packet: "+err.Error(), "raddr", addr.String(), "block-duration", max(0, listener.conf.BlockDuration)) listener.sec.block(addr) } }