Skip to content

Commit e4d2773

Browse files
nethernet: Fix packet truncation and listener blocking forever (#24)
* Fix packet truncation and listener blocking forever * simplify
1 parent f8a6fb6 commit e4d2773

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

discovery/listener.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ func (l *Listener) Responses() map[uint64][]byte {
253253

254254
// listen continuously reads packets received in the conn and calls handlePacket.
255255
func (l *Listener) listen() {
256+
b := make([]byte, maxUDPPacketSize)
256257
for {
257-
b := make([]byte, 1024)
258258
n, addr, err := l.conn.ReadFrom(b)
259259
if err != nil {
260260
if !errors.Is(err, net.ErrClosed) {
@@ -269,6 +269,9 @@ func (l *Listener) listen() {
269269
}
270270
}
271271

272+
// maxUDPPacketSize is 65,535 bytes, the maximum UDP payload size.
273+
const maxUDPPacketSize = 65535
274+
272275
// write writes the packet to the destination address using the network ID of Listener.
273276
func (l *Listener) write(pk Packet, addr net.Addr) error {
274277
b := Marshal(pk, l.conf.NetworkID)

listener.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,10 @@ func (l *Listener) handleConn(conn *Conn, d *description, channelsReady <-chan s
460460
}
461461

462462
select {
463+
case <-ctx.Done():
464+
err = ctx.Err()
465+
case <-conn.ctx.Done():
466+
err = context.Cause(conn.ctx)
463467
case <-l.closed:
464468
_ = conn.Close()
465469
case l.incoming <- conn:

0 commit comments

Comments
 (0)