dht: Handle truncated KRPC packets better, requires an update to bencode package

This commit is contained in:
Matt Joiner 2014-11-18 21:54:53 -06:00
parent e1f720c770
commit a87566b9f6
1 changed files with 10 additions and 1 deletions

View File

@ -278,7 +278,12 @@ func (s *Server) processPacket(b []byte, addr dHTAddr) {
err := bencode.Unmarshal(b, &d)
if err != nil {
if se, ok := err.(*bencode.SyntaxError); !ok || se.Offset != 0 {
log.Printf("%s: received bad krpc message: %s: %q", s, err, b)
log.Printf("%s: received bad krpc message: %s%s", s, err, func() string {
if se.What == io.ErrUnexpectedEOF {
return ""
}
return fmt.Sprintf(": %q", b)
}())
}
return
}
@ -309,6 +314,10 @@ func (s *Server) serve() error {
if err != nil {
return err
}
if n == len(b) {
logonce.Stderr.Printf("received dht packet exceeds buffer size")
continue
}
s.processPacket(b[:n], newDHTAddr(addr.(*net.UDPAddr)))
}
}