Fixed reading chunked EIP8 handshake (#1712)

* Fixed reading chunked EIP8 handshake

* Added missing break
This commit is contained in:
Arkadiy Paronyan 2016-07-26 16:04:14 +02:00 committed by Gav Wood
parent 6c15a47acc
commit 53a975d1dc
2 changed files with 28 additions and 32 deletions

View File

@ -77,7 +77,7 @@ impl<Socket: GenericSocket> GenericConnection<Socket> {
/// Readable IO handler. Called when there is some data to be read. /// Readable IO handler. Called when there is some data to be read.
pub fn readable(&mut self) -> io::Result<Option<Bytes>> { pub fn readable(&mut self) -> io::Result<Option<Bytes>> {
if self.rec_size == 0 || self.rec_buf.len() >= self.rec_size { if self.rec_size == 0 || self.rec_buf.len() >= self.rec_size {
warn!(target:"network", "Unexpected connection read"); return Ok(None);
} }
let sock_ref = <Socket as Read>::by_ref(&mut self.socket); let sock_ref = <Socket as Read>::by_ref(&mut self.socket);
loop { loop {

View File

@ -128,31 +128,27 @@ impl Handshake {
/// Readable IO handler. Drives the state change. /// Readable IO handler. Drives the state change.
pub fn readable<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo) -> Result<(), UtilError> where Message: Send + Clone { pub fn readable<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo) -> Result<(), UtilError> where Message: Send + Clone {
if !self.expired() { if !self.expired() {
while let Some(data) = try!(self.connection.readable()) {
match self.state { match self.state {
HandshakeState::New => {} HandshakeState::New => {},
HandshakeState::StartSession => {},
HandshakeState::ReadingAuth => { HandshakeState::ReadingAuth => {
if let Some(data) = try!(self.connection.readable()) {
try!(self.read_auth(io, host.secret(), &data)); try!(self.read_auth(io, host.secret(), &data));
};
}, },
HandshakeState::ReadingAuthEip8 => { HandshakeState::ReadingAuthEip8 => {
if let Some(data) = try!(self.connection.readable()) {
try!(self.read_auth_eip8(io, host.secret(), &data)); try!(self.read_auth_eip8(io, host.secret(), &data));
};
}, },
HandshakeState::ReadingAck => { HandshakeState::ReadingAck => {
if let Some(data) = try!(self.connection.readable()) {
try!(self.read_ack(host.secret(), &data)); try!(self.read_ack(host.secret(), &data));
};
}, },
HandshakeState::ReadingAckEip8 => { HandshakeState::ReadingAckEip8 => {
if let Some(data) = try!(self.connection.readable()) {
try!(self.read_ack_eip8(host.secret(), &data)); try!(self.read_ack_eip8(host.secret(), &data));
};
}, },
HandshakeState::StartSession => { }
if self.state == HandshakeState::StartSession {
io.clear_timer(self.connection.token).ok(); io.clear_timer(self.connection.token).ok();
}, break;
}
} }
} }
Ok(()) Ok(())
@ -180,7 +176,7 @@ impl Handshake {
fn read_auth<Message>(&mut self, io: &IoContext<Message>, secret: &Secret, data: &[u8]) -> Result<(), UtilError> where Message: Send + Clone { fn read_auth<Message>(&mut self, io: &IoContext<Message>, secret: &Secret, data: &[u8]) -> Result<(), UtilError> where Message: Send + Clone {
trace!(target: "network", "Received handshake auth from {:?}", self.connection.remote_addr_str()); trace!(target: "network", "Received handshake auth from {:?}", self.connection.remote_addr_str());
if data.len() != V4_AUTH_PACKET_SIZE { if data.len() != V4_AUTH_PACKET_SIZE {
debug!(target:"net", "Wrong auth packet size"); debug!(target: "network", "Wrong auth packet size");
return Err(From::from(NetworkError::BadProtocol)); return Err(From::from(NetworkError::BadProtocol));
} }
self.auth_cipher = data.to_vec(); self.auth_cipher = data.to_vec();
@ -197,7 +193,7 @@ impl Handshake {
// Try to interpret as EIP-8 packet // Try to interpret as EIP-8 packet
let total = (((data[0] as u16) << 8 | (data[1] as u16)) as usize) + 2; let total = (((data[0] as u16) << 8 | (data[1] as u16)) as usize) + 2;
if total < V4_AUTH_PACKET_SIZE { if total < V4_AUTH_PACKET_SIZE {
debug!(target:"net", "Wrong EIP8 auth packet size"); debug!(target: "network", "Wrong EIP8 auth packet size");
return Err(From::from(NetworkError::BadProtocol)); return Err(From::from(NetworkError::BadProtocol));
} }
let rest = total - data.len(); let rest = total - data.len();
@ -226,7 +222,7 @@ impl Handshake {
fn read_ack(&mut self, secret: &Secret, data: &[u8]) -> Result<(), UtilError> { fn read_ack(&mut self, secret: &Secret, data: &[u8]) -> Result<(), UtilError> {
trace!(target: "network", "Received handshake ack from {:?}", self.connection.remote_addr_str()); trace!(target: "network", "Received handshake ack from {:?}", self.connection.remote_addr_str());
if data.len() != V4_ACK_PACKET_SIZE { if data.len() != V4_ACK_PACKET_SIZE {
debug!(target:"net", "Wrong ack packet size"); debug!(target: "network", "Wrong ack packet size");
return Err(From::from(NetworkError::BadProtocol)); return Err(From::from(NetworkError::BadProtocol));
} }
self.ack_cipher = data.to_vec(); self.ack_cipher = data.to_vec();
@ -240,7 +236,7 @@ impl Handshake {
// Try to interpret as EIP-8 packet // Try to interpret as EIP-8 packet
let total = (((data[0] as u16) << 8 | (data[1] as u16)) as usize) + 2; let total = (((data[0] as u16) << 8 | (data[1] as u16)) as usize) + 2;
if total < V4_ACK_PACKET_SIZE { if total < V4_ACK_PACKET_SIZE {
debug!(target:"net", "Wrong EIP8 ack packet size"); debug!(target: "network", "Wrong EIP8 ack packet size");
return Err(From::from(NetworkError::BadProtocol)); return Err(From::from(NetworkError::BadProtocol));
} }
let rest = total - data.len(); let rest = total - data.len();