Merge pull request #965 from subtly/patch-1

Better UDP & interop. Limit all received datagrams to 1280bytes.
This commit is contained in:
Felix Lange 2015-05-13 20:34:10 +02:00
commit b2119d8931
2 changed files with 20 additions and 4 deletions

@ -402,7 +402,7 @@ func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) ([]byte,
// readLoop runs in its own goroutine. it handles incoming UDP packets. // readLoop runs in its own goroutine. it handles incoming UDP packets.
func (t *udp) readLoop() { func (t *udp) readLoop() {
defer t.conn.Close() defer t.conn.Close()
buf := make([]byte, 4096) // TODO: good buffer size buf := make([]byte, 1280)
for { for {
nbytes, from, err := t.conn.ReadFromUDP(buf) nbytes, from, err := t.conn.ReadFromUDP(buf)
if err != nil { if err != nil {
@ -510,9 +510,15 @@ func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte
closestrpc[i] = nodeToRPC(n) closestrpc[i] = nodeToRPC(n)
} }
t.send(from, neighborsPacket, neighbors{ t.send(from, neighborsPacket, neighbors{
Nodes: closestrpc, Nodes: closestrpc[:13],
Expiration: uint64(time.Now().Add(expiration).Unix()), Expiration: uint64(time.Now().Add(expiration).Unix()),
}) })
if len(closestrpc) > 13 {
t.send(from, neighborsPacket, neighbors{
Nodes: closestrpc[13:],
Expiration: uint64(time.Now().Add(expiration).Unix()),
})
}
return nil return nil
} }

@ -163,9 +163,9 @@ func TestUDP_findnode(t *testing.T) {
)) ))
// check that closest neighbors are returned. // check that closest neighbors are returned.
test.packetIn(nil, findnodePacket, &findnode{Target: testTarget, Expiration: futureExp}) test.packetIn(nil, findnodePacket, &findnode{Target: testTarget, Expiration: futureExp})
expected := test.table.closest(targetHash, bucketSize)
test.waitPacketOut(func(p *neighbors) { test.waitPacketOut(func(p *neighbors) {
expected := test.table.closest(targetHash, bucketSize) if len(p.Nodes) != 13 {
if len(p.Nodes) != bucketSize {
t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), bucketSize) t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), bucketSize)
} }
for i := range p.Nodes { for i := range p.Nodes {
@ -174,6 +174,16 @@ func TestUDP_findnode(t *testing.T) {
} }
} }
}) })
test.waitPacketOut(func(p *neighbors) {
if len(p.Nodes) != 3 {
t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), bucketSize)
}
for i := range p.Nodes {
if p.Nodes[i].ID != expected.entries[i + 13].ID {
t.Errorf("result mismatch at %d:\n got: %v\n want: %v", i, p.Nodes[i], expected.entries[i])
}
}
})
} }
func TestUDP_findnodeMultiReply(t *testing.T) { func TestUDP_findnodeMultiReply(t *testing.T) {