dht: Improve bad KRPC message screening

This commit is contained in:
Matt Joiner 2014-12-01 17:01:55 -06:00
parent c7283182b6
commit fec0ac6ea5
1 changed files with 10 additions and 7 deletions

View File

@ -289,14 +289,17 @@ func (s *Server) processPacket(b []byte, addr dHTAddr) {
var d Msg
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%s", s, err, func() string {
if se.What == io.ErrUnexpectedEOF {
return ""
func() {
if se, ok := err.(*bencode.SyntaxError); ok {
if b[se.Offset] == 0 {
return
}
return fmt.Sprintf(": %q", b)
}())
if se.Offset == 0 {
return
}
}
log.Printf("%s: received bad krpc message: %s: %q", s, err, b)
}()
return
}
s.mu.Lock()