eth, whisper: fix msg.Payload reads

This commit is contained in:
Felix Lange 2015-03-04 13:12:50 +01:00
parent 7964f30dcb
commit 6e7e5d5fd5
2 changed files with 33 additions and 34 deletions

View File

@ -3,7 +3,6 @@ package eth
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
@ -188,33 +187,37 @@ func (self *ethProtocol) handle() error {
case BlockHashesMsg: case BlockHashesMsg:
msgStream := rlp.NewStream(msg.Payload) msgStream := rlp.NewStream(msg.Payload)
var err error if _, err := msgStream.List(); err != nil {
return err
}
var i int var i int
iter := func() (hash []byte, ok bool) { iter := func() (hash []byte, ok bool) {
hash, err = msgStream.Bytes() hash, err := msgStream.Bytes()
if err == nil { if err == rlp.EOL {
i++ return nil, false
ok = true } else if err != nil {
} else {
if err != io.EOF {
self.protoError(ErrDecode, "msg %v: after %v hashes : %v", msg, i, err) self.protoError(ErrDecode, "msg %v: after %v hashes : %v", msg, i, err)
return nil, false
} }
i++
return hash, true
} }
return
}
self.blockPool.AddBlockHashes(iter, self.id) self.blockPool.AddBlockHashes(iter, self.id)
case GetBlocksMsg: case GetBlocksMsg:
msgStream := rlp.NewStream(msg.Payload) msgStream := rlp.NewStream(msg.Payload)
if _, err := msgStream.List(); err != nil {
return err
}
var blocks []interface{} var blocks []interface{}
var i int var i int
for { for {
i++ i++
var hash []byte var hash []byte
if err := msgStream.Decode(&hash); err != nil { if err := msgStream.Decode(&hash); err != nil {
if err == io.EOF { if err == rlp.EOL {
break break
} else { } else {
return self.protoError(ErrDecode, "msg %v: %v", msg, err) return self.protoError(ErrDecode, "msg %v: %v", msg, err)
@ -232,10 +235,13 @@ func (self *ethProtocol) handle() error {
case BlocksMsg: case BlocksMsg:
msgStream := rlp.NewStream(msg.Payload) msgStream := rlp.NewStream(msg.Payload)
if _, err := msgStream.List(); err != nil {
return err
}
for { for {
var block types.Block var block types.Block
if err := msgStream.Decode(&block); err != nil { if err := msgStream.Decode(&block); err != nil {
if err == io.EOF { if err == rlp.EOL {
break break
} else { } else {
return self.protoError(ErrDecode, "msg %v: %v", msg, err) return self.protoError(ErrDecode, "msg %v: %v", msg, err)

View File

@ -2,10 +2,10 @@ package whisper
import ( import (
"fmt" "fmt"
"io/ioutil"
"time" "time"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
"gopkg.in/fatih/set.v0" "gopkg.in/fatih/set.v0"
) )
@ -77,8 +77,7 @@ func (self *peer) broadcast(envelopes []*Envelope) error {
} }
if i > 0 { if i > 0 {
msg := p2p.NewMsg(envelopesMsg, envs[:i]...) if err := p2p.EncodeMsg(self.ws, envelopesMsg, envs[:i]...); err != nil {
if err := self.ws.WriteMsg(msg); err != nil {
return err return err
} }
self.peer.DebugDetailln("broadcasted", i, "message(s)") self.peer.DebugDetailln("broadcasted", i, "message(s)")
@ -93,34 +92,28 @@ func (self *peer) addKnown(envelope *Envelope) {
func (self *peer) handleStatus() error { func (self *peer) handleStatus() error {
ws := self.ws ws := self.ws
if err := ws.WriteMsg(self.statusMsg()); err != nil { if err := ws.WriteMsg(self.statusMsg()); err != nil {
return err return err
} }
msg, err := ws.ReadMsg() msg, err := ws.ReadMsg()
if err != nil { if err != nil {
return err return err
} }
if msg.Code != statusMsg { if msg.Code != statusMsg {
return fmt.Errorf("peer send %x before status msg", msg.Code) return fmt.Errorf("peer send %x before status msg", msg.Code)
} }
s := rlp.NewStream(msg.Payload)
data, err := ioutil.ReadAll(msg.Payload) if _, err := s.List(); err != nil {
return fmt.Errorf("bad status message: %v", err)
}
pv, err := s.Uint()
if err != nil { if err != nil {
return err return fmt.Errorf("bad status message: %v", err)
} }
if pv != protocolVersion {
if len(data) == 0 {
return fmt.Errorf("malformed status. data len = 0")
}
if pv := data[0]; pv != protocolVersion {
return fmt.Errorf("protocol version mismatch %d != %d", pv, protocolVersion) return fmt.Errorf("protocol version mismatch %d != %d", pv, protocolVersion)
} }
return msg.Discard() // ignore anything after protocol version
return nil
} }
func (self *peer) statusMsg() p2p.Msg { func (self *peer) statusMsg() p2p.Msg {