2016-10-24 00:02:02 +03:00
|
|
|
// Copyright 2016 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Message types that transit over the network and related serialization code.
|
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
|
2016-10-29 16:51:24 +03:00
|
|
|
use num::FromPrimitive;
|
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
use futures::future::{ok, Future};
|
2016-12-11 06:11:49 +03:00
|
|
|
use tokio_core::net::TcpStream;
|
2017-08-10 03:54:10 +03:00
|
|
|
use tokio_io::io::{read_exact, write_all};
|
2016-12-11 06:11:49 +03:00
|
|
|
|
|
|
|
use core::consensus::MAX_MSG_LEN;
|
2017-02-08 00:52:17 +03:00
|
|
|
use core::core::BlockHeader;
|
|
|
|
use core::core::hash::Hash;
|
|
|
|
use core::core::target::Difficulty;
|
2017-11-01 02:32:33 +03:00
|
|
|
use core::ser::{self, Readable, Reader, Writeable, Writer};
|
2016-10-26 08:06:13 +03:00
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
use types::*;
|
|
|
|
|
2016-10-26 08:06:13 +03:00
|
|
|
/// Current latest version of the protocol
|
|
|
|
pub const PROTOCOL_VERSION: u32 = 1;
|
2017-02-10 07:16:34 +03:00
|
|
|
|
2016-10-26 08:06:13 +03:00
|
|
|
/// Grin's user agent with current version (TODO externalize)
|
|
|
|
pub const USER_AGENT: &'static str = "MW/Grin 0.1";
|
2016-10-24 00:02:02 +03:00
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
/// Magic number expected in the header of every message
|
2017-06-01 01:47:52 +03:00
|
|
|
const MAGIC: [u8; 2] = [0x1e, 0xc5];
|
2016-10-25 07:35:10 +03:00
|
|
|
|
2016-12-11 06:11:49 +03:00
|
|
|
/// Size in bytes of a message header
|
|
|
|
pub const HEADER_LEN: u64 = 11;
|
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
/// Codes for each error that can be produced reading a message.
|
2017-08-10 03:54:10 +03:00
|
|
|
#[allow(dead_code)]
|
2016-10-26 08:06:13 +03:00
|
|
|
pub enum ErrCodes {
|
2016-10-26 21:21:45 +03:00
|
|
|
UnsupportedVersion = 100,
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
/// Types of messages
|
2016-10-29 16:51:24 +03:00
|
|
|
enum_from_primitive! {
|
2017-02-02 06:05:17 +03:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2016-11-01 20:42:33 +03:00
|
|
|
pub enum Type {
|
2017-11-01 02:32:33 +03:00
|
|
|
Error,
|
|
|
|
Hand,
|
|
|
|
Shake,
|
|
|
|
Ping,
|
|
|
|
Pong,
|
|
|
|
GetPeerAddrs,
|
|
|
|
PeerAddrs,
|
|
|
|
GetHeaders,
|
|
|
|
Headers,
|
|
|
|
GetBlock,
|
|
|
|
Block,
|
|
|
|
Transaction,
|
2016-11-01 20:42:33 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
|
2016-12-11 06:11:49 +03:00
|
|
|
/// Future combinator to read any message where the body is a Readable. Reads
|
|
|
|
/// the header first, handles its validation and then reads the Readable body,
|
|
|
|
/// allocating buffers of the right size.
|
2017-02-27 07:08:40 +03:00
|
|
|
pub fn read_msg<T>(conn: TcpStream) -> Box<Future<Item = (TcpStream, T), Error = Error>>
|
2017-10-26 20:48:51 +03:00
|
|
|
where
|
|
|
|
T: Readable + 'static,
|
2016-12-11 06:11:49 +03:00
|
|
|
{
|
|
|
|
let read_header = read_exact(conn, vec![0u8; HEADER_LEN as usize])
|
2017-02-27 07:08:40 +03:00
|
|
|
.from_err()
|
2016-12-11 06:11:49 +03:00
|
|
|
.and_then(|(reader, buf)| {
|
|
|
|
let header = try!(ser::deserialize::<MsgHeader>(&mut &buf[..]));
|
|
|
|
if header.msg_len > MAX_MSG_LEN {
|
|
|
|
// TODO add additional restrictions on a per-message-type basis to avoid 20MB
|
2017-11-01 02:32:33 +03:00
|
|
|
// pings
|
2017-02-27 07:08:40 +03:00
|
|
|
return Err(Error::Serialization(ser::Error::TooLargeReadErr));
|
2016-12-11 06:11:49 +03:00
|
|
|
}
|
|
|
|
Ok((reader, header))
|
|
|
|
});
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
let read_msg = read_header
|
|
|
|
.and_then(|(reader, header)| {
|
2017-02-27 07:08:40 +03:00
|
|
|
read_exact(reader, vec![0u8; header.msg_len as usize]).from_err()
|
2016-12-11 06:11:49 +03:00
|
|
|
})
|
|
|
|
.and_then(|(reader, buf)| {
|
|
|
|
let body = try!(ser::deserialize(&mut &buf[..]));
|
|
|
|
Ok((reader, body))
|
|
|
|
});
|
|
|
|
Box::new(read_msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Future combinator to write a full message from a Writeable payload.
|
|
|
|
/// Serializes the payload first and then sends the message header and that
|
|
|
|
/// payload.
|
2017-10-26 20:48:51 +03:00
|
|
|
pub fn write_msg<T>(
|
|
|
|
conn: TcpStream,
|
|
|
|
msg: T,
|
|
|
|
msg_type: Type,
|
|
|
|
) -> Box<Future<Item = TcpStream, Error = Error>>
|
|
|
|
where
|
|
|
|
T: Writeable + 'static,
|
2016-12-11 06:11:49 +03:00
|
|
|
{
|
|
|
|
let write_msg = ok((conn)).and_then(move |conn| {
|
|
|
|
// prepare the body first so we know its serialized length
|
|
|
|
let mut body_buf = vec![];
|
2017-08-10 03:54:10 +03:00
|
|
|
ser::serialize(&mut body_buf, &msg).unwrap();
|
2016-12-11 06:11:49 +03:00
|
|
|
|
2016-12-12 00:04:52 +03:00
|
|
|
// build and serialize the header using the body size
|
2016-12-11 06:11:49 +03:00
|
|
|
let mut header_buf = vec![];
|
|
|
|
let blen = body_buf.len() as u64;
|
2017-08-10 03:54:10 +03:00
|
|
|
ser::serialize(&mut header_buf, &MsgHeader::new(msg_type, blen)).unwrap();
|
2016-12-12 00:04:52 +03:00
|
|
|
|
|
|
|
// send the whole thing
|
2016-12-11 06:11:49 +03:00
|
|
|
write_all(conn, header_buf)
|
|
|
|
.and_then(|(conn, _)| write_all(conn, body_buf))
|
|
|
|
.map(|(conn, _)| conn)
|
2017-02-27 07:08:40 +03:00
|
|
|
.from_err()
|
2016-12-11 06:11:49 +03:00
|
|
|
});
|
|
|
|
Box::new(write_msg)
|
|
|
|
}
|
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
/// Header of any protocol message, used to identify incoming messages.
|
|
|
|
pub struct MsgHeader {
|
2017-06-01 01:47:52 +03:00
|
|
|
magic: [u8; 2],
|
2017-02-03 02:51:48 +03:00
|
|
|
/// Type of the message.
|
2016-10-31 04:23:52 +03:00
|
|
|
pub msg_type: Type,
|
2017-02-03 02:51:48 +03:00
|
|
|
/// Tota length of the message in bytes.
|
2016-12-11 06:11:49 +03:00
|
|
|
pub msg_len: u64,
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MsgHeader {
|
2017-02-03 02:51:48 +03:00
|
|
|
/// Creates a new message header.
|
2016-12-11 06:11:49 +03:00
|
|
|
pub fn new(msg_type: Type, len: u64) -> MsgHeader {
|
2016-10-31 04:23:52 +03:00
|
|
|
MsgHeader {
|
|
|
|
magic: MAGIC,
|
|
|
|
msg_type: msg_type,
|
2016-12-11 06:11:49 +03:00
|
|
|
msg_len: len,
|
2016-10-31 04:23:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 20:42:33 +03:00
|
|
|
/// Serialized length of the header in bytes
|
|
|
|
pub fn serialized_len(&self) -> u64 {
|
2016-12-11 06:11:49 +03:00
|
|
|
HEADER_LEN
|
2016-11-01 20:42:33 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for MsgHeader {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2017-09-29 21:44:25 +03:00
|
|
|
ser_multiwrite!(
|
|
|
|
writer,
|
|
|
|
[write_u8, self.magic[0]],
|
|
|
|
[write_u8, self.magic[1]],
|
|
|
|
[write_u8, self.msg_type as u8],
|
|
|
|
[write_u64, self.msg_len]
|
|
|
|
);
|
2016-11-01 05:03:12 +03:00
|
|
|
Ok(())
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for MsgHeader {
|
2016-10-25 07:35:10 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<MsgHeader, ser::Error> {
|
|
|
|
try!(reader.expect_u8(MAGIC[0]));
|
|
|
|
try!(reader.expect_u8(MAGIC[1]));
|
2016-12-11 06:11:49 +03:00
|
|
|
let (t, len) = ser_multiread!(reader, read_u8, read_u64);
|
2016-11-01 20:42:33 +03:00
|
|
|
match Type::from_u8(t) {
|
2017-11-01 02:32:33 +03:00
|
|
|
Some(ty) => Ok(MsgHeader {
|
|
|
|
magic: MAGIC,
|
|
|
|
msg_type: ty,
|
|
|
|
msg_len: len,
|
|
|
|
}),
|
2016-11-01 20:42:33 +03:00
|
|
|
None => Err(ser::Error::CorruptedData),
|
2016-10-28 00:28:02 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// First part of a handshake, sender advertises its version and
|
|
|
|
/// characteristics.
|
2016-10-24 00:02:02 +03:00
|
|
|
pub struct Hand {
|
2016-10-26 08:06:13 +03:00
|
|
|
/// protocol version of the sender
|
|
|
|
pub version: u32,
|
|
|
|
/// capabilities of the sender
|
|
|
|
pub capabilities: Capabilities,
|
|
|
|
/// randomly generated for each handshake, helps detect self
|
|
|
|
pub nonce: u64,
|
2017-02-08 00:52:17 +03:00
|
|
|
/// total difficulty accumulated by the sender, used to check whether sync
|
|
|
|
/// may
|
|
|
|
/// be needed
|
|
|
|
pub total_difficulty: Difficulty,
|
2016-10-26 08:06:13 +03:00
|
|
|
/// network address of the sender
|
|
|
|
pub sender_addr: SockAddr,
|
|
|
|
/// network address of the receiver
|
|
|
|
pub receiver_addr: SockAddr,
|
|
|
|
/// name of version of the software
|
|
|
|
pub user_agent: String,
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
impl Writeable for Hand {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2017-09-29 21:44:25 +03:00
|
|
|
ser_multiwrite!(
|
|
|
|
writer,
|
|
|
|
[write_u32, self.version],
|
|
|
|
[write_u32, self.capabilities.bits()],
|
|
|
|
[write_u64, self.nonce]
|
|
|
|
);
|
2017-08-10 03:54:10 +03:00
|
|
|
self.total_difficulty.write(writer).unwrap();
|
|
|
|
self.sender_addr.write(writer).unwrap();
|
|
|
|
self.receiver_addr.write(writer).unwrap();
|
2016-11-01 20:42:33 +03:00
|
|
|
writer.write_bytes(&self.user_agent)
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for Hand {
|
2016-10-25 07:35:10 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<Hand, ser::Error> {
|
2017-02-08 00:52:17 +03:00
|
|
|
let (version, capab, nonce) = ser_multiread!(reader, read_u32, read_u32, read_u64);
|
|
|
|
let total_diff = try!(Difficulty::read(reader));
|
2016-10-26 08:06:13 +03:00
|
|
|
let sender_addr = try!(SockAddr::read(reader));
|
|
|
|
let receiver_addr = try!(SockAddr::read(reader));
|
|
|
|
let ua = try!(reader.read_vec());
|
2016-10-28 00:28:02 +03:00
|
|
|
let user_agent = try!(String::from_utf8(ua).map_err(|_| ser::Error::CorruptedData));
|
2017-11-01 02:32:33 +03:00
|
|
|
let capabilities = try!(Capabilities::from_bits(capab).ok_or(ser::Error::CorruptedData,));
|
2016-10-26 08:06:13 +03:00
|
|
|
Ok(Hand {
|
2016-10-25 07:35:10 +03:00
|
|
|
version: version,
|
2016-10-26 08:06:13 +03:00
|
|
|
capabilities: capabilities,
|
2016-10-28 00:28:02 +03:00
|
|
|
nonce: nonce,
|
2017-02-08 00:52:17 +03:00
|
|
|
total_difficulty: total_diff,
|
2016-10-26 08:06:13 +03:00
|
|
|
sender_addr: sender_addr,
|
2016-10-25 07:35:10 +03:00
|
|
|
receiver_addr: receiver_addr,
|
|
|
|
user_agent: user_agent,
|
2016-10-26 08:06:13 +03:00
|
|
|
})
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Second part of a handshake, receiver of the first part replies with its own
|
|
|
|
/// version and characteristics.
|
2016-10-24 00:02:02 +03:00
|
|
|
pub struct Shake {
|
2016-10-26 08:06:13 +03:00
|
|
|
/// sender version
|
|
|
|
pub version: u32,
|
|
|
|
/// sender capabilities
|
|
|
|
pub capabilities: Capabilities,
|
2017-02-08 00:52:17 +03:00
|
|
|
/// total difficulty accumulated by the sender, used to check whether sync
|
|
|
|
/// may
|
|
|
|
/// be needed
|
|
|
|
pub total_difficulty: Difficulty,
|
2016-10-26 08:06:13 +03:00
|
|
|
/// name of version of the software
|
|
|
|
pub user_agent: String,
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|
|
|
|
|
2016-10-26 08:06:13 +03:00
|
|
|
impl Writeable for Shake {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2017-09-29 21:44:25 +03:00
|
|
|
ser_multiwrite!(
|
|
|
|
writer,
|
|
|
|
[write_u32, self.version],
|
|
|
|
[write_u32, self.capabilities.bits()]
|
|
|
|
);
|
2017-08-10 03:54:10 +03:00
|
|
|
self.total_difficulty.write(writer).unwrap();
|
|
|
|
writer.write_bytes(&self.user_agent).unwrap();
|
2016-11-01 05:03:12 +03:00
|
|
|
Ok(())
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for Shake {
|
2016-10-25 07:35:10 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<Shake, ser::Error> {
|
2017-02-08 00:52:17 +03:00
|
|
|
let (version, capab) = ser_multiread!(reader, read_u32, read_u32);
|
|
|
|
let total_diff = try!(Difficulty::read(reader));
|
|
|
|
let ua = try!(reader.read_vec());
|
2016-10-26 08:06:13 +03:00
|
|
|
let user_agent = try!(String::from_utf8(ua).map_err(|_| ser::Error::CorruptedData));
|
2017-11-01 02:32:33 +03:00
|
|
|
let capabilities = try!(Capabilities::from_bits(capab).ok_or(ser::Error::CorruptedData,));
|
2016-10-26 08:06:13 +03:00
|
|
|
Ok(Shake {
|
2016-10-25 07:35:10 +03:00
|
|
|
version: version,
|
2016-10-26 08:06:13 +03:00
|
|
|
capabilities: capabilities,
|
2017-02-08 00:52:17 +03:00
|
|
|
total_difficulty: total_diff,
|
2016-10-25 07:35:10 +03:00
|
|
|
user_agent: user_agent,
|
2016-10-26 08:06:13 +03:00
|
|
|
})
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
/// Ask for other peers addresses, required for network discovery.
|
|
|
|
pub struct GetPeerAddrs {
|
|
|
|
/// Filters on the capabilities we'd like the peers to have
|
|
|
|
pub capabilities: Capabilities,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for GetPeerAddrs {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2016-10-29 22:36:45 +03:00
|
|
|
writer.write_u32(self.capabilities.bits())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for GetPeerAddrs {
|
2016-10-29 22:36:45 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<GetPeerAddrs, ser::Error> {
|
|
|
|
let capab = try!(reader.read_u32());
|
2017-11-01 02:32:33 +03:00
|
|
|
let capabilities = try!(Capabilities::from_bits(capab).ok_or(ser::Error::CorruptedData,));
|
|
|
|
Ok(GetPeerAddrs {
|
|
|
|
capabilities: capabilities,
|
|
|
|
})
|
2016-10-29 22:36:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Peer addresses we know of that are fresh enough, in response to
|
|
|
|
/// GetPeerAddrs.
|
|
|
|
pub struct PeerAddrs {
|
|
|
|
pub peers: Vec<SockAddr>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for PeerAddrs {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2016-11-01 20:42:33 +03:00
|
|
|
try!(writer.write_u32(self.peers.len() as u32));
|
2016-10-29 22:36:45 +03:00
|
|
|
for p in &self.peers {
|
2017-08-10 03:54:10 +03:00
|
|
|
p.write(writer).unwrap();
|
2016-10-29 22:36:45 +03:00
|
|
|
}
|
2016-11-01 20:42:33 +03:00
|
|
|
Ok(())
|
2016-10-29 22:36:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for PeerAddrs {
|
2016-10-29 22:36:45 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<PeerAddrs, ser::Error> {
|
|
|
|
let peer_count = try!(reader.read_u32());
|
2017-02-19 05:42:34 +03:00
|
|
|
if peer_count > MAX_PEER_ADDRS {
|
2016-12-11 06:11:49 +03:00
|
|
|
return Err(ser::Error::TooLargeReadErr);
|
2017-02-19 05:42:34 +03:00
|
|
|
} else if peer_count == 0 {
|
|
|
|
return Ok(PeerAddrs { peers: vec![] });
|
|
|
|
}
|
|
|
|
// let peers = try_map_vec!([0..peer_count], |_| SockAddr::read(reader));
|
|
|
|
let mut peers = Vec::with_capacity(peer_count as usize);
|
|
|
|
for _ in 0..peer_count {
|
|
|
|
peers.push(SockAddr::read(reader)?);
|
2016-10-29 22:36:45 +03:00
|
|
|
}
|
|
|
|
Ok(PeerAddrs { peers: peers })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
/// We found some issue in the communication, sending an error back, usually
|
|
|
|
/// followed by closing the connection.
|
2016-10-24 00:02:02 +03:00
|
|
|
pub struct PeerError {
|
2016-10-26 08:06:13 +03:00
|
|
|
/// error code
|
|
|
|
pub code: u32,
|
|
|
|
/// slightly more user friendly message
|
|
|
|
pub message: String,
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
|
|
|
|
impl Writeable for PeerError {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2016-11-01 20:42:33 +03:00
|
|
|
ser_multiwrite!(writer, [write_u32, self.code], [write_bytes, &self.message]);
|
2016-11-01 05:03:12 +03:00
|
|
|
Ok(())
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for PeerError {
|
2016-10-25 07:35:10 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<PeerError, ser::Error> {
|
|
|
|
let (code, msg) = ser_multiread!(reader, read_u32, read_vec);
|
2017-11-01 02:32:33 +03:00
|
|
|
let message = try!(String::from_utf8(msg).map_err(|_| ser::Error::CorruptedData,));
|
2016-10-26 08:06:13 +03:00
|
|
|
Ok(PeerError {
|
2016-10-25 07:35:10 +03:00
|
|
|
code: code,
|
|
|
|
message: message,
|
2016-10-26 08:06:13 +03:00
|
|
|
})
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 00:28:02 +03:00
|
|
|
/// Only necessary so we can implement Readable and Writeable. Rust disallows
|
|
|
|
/// implementing traits when both types are outside of this crate (which is the
|
|
|
|
/// case for SocketAddr and Readable/Writeable).
|
2016-10-26 08:06:13 +03:00
|
|
|
pub struct SockAddr(pub SocketAddr);
|
|
|
|
|
|
|
|
impl Writeable for SockAddr {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2016-10-26 08:06:13 +03:00
|
|
|
match self.0 {
|
|
|
|
SocketAddr::V4(sav4) => {
|
2017-09-29 21:44:25 +03:00
|
|
|
ser_multiwrite!(
|
|
|
|
writer,
|
|
|
|
[write_u8, 0],
|
|
|
|
[write_fixed_bytes, &sav4.ip().octets().to_vec()],
|
|
|
|
[write_u16, sav4.port()]
|
|
|
|
);
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
2016-10-26 08:06:13 +03:00
|
|
|
SocketAddr::V6(sav6) => {
|
2016-11-01 05:03:12 +03:00
|
|
|
try!(writer.write_u8(1));
|
2016-10-26 08:06:13 +03:00
|
|
|
for seg in &sav6.ip().segments() {
|
2016-11-01 05:03:12 +03:00
|
|
|
try!(writer.write_u16(*seg));
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
2016-11-01 05:03:12 +03:00
|
|
|
try!(writer.write_u16(sav6.port()));
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-01 05:03:12 +03:00
|
|
|
Ok(())
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for SockAddr {
|
2016-10-26 08:06:13 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<SockAddr, ser::Error> {
|
|
|
|
let v4_or_v6 = try!(reader.read_u8());
|
2016-10-25 07:35:10 +03:00
|
|
|
if v4_or_v6 == 0 {
|
2016-10-26 08:06:13 +03:00
|
|
|
let ip = try!(reader.read_fixed_bytes(4));
|
|
|
|
let port = try!(reader.read_u16());
|
2017-09-29 21:44:25 +03:00
|
|
|
Ok(SockAddr(SocketAddr::V4(SocketAddrV4::new(
|
|
|
|
Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]),
|
|
|
|
port,
|
|
|
|
))))
|
2016-10-25 07:35:10 +03:00
|
|
|
} else {
|
2016-10-29 22:36:45 +03:00
|
|
|
let ip = try_map_vec!([0..8], |_| reader.read_u16());
|
2016-10-26 08:06:13 +03:00
|
|
|
let port = try!(reader.read_u16());
|
2017-09-29 21:44:25 +03:00
|
|
|
Ok(SockAddr(SocketAddr::V6(SocketAddrV6::new(
|
2017-11-01 02:32:33 +03:00
|
|
|
Ipv6Addr::new(ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7]),
|
2017-09-29 21:44:25 +03:00
|
|
|
port,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
))))
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 06:11:49 +03:00
|
|
|
|
2017-02-08 00:52:17 +03:00
|
|
|
/// Serializable wrapper for the block locator.
|
|
|
|
pub struct Locator {
|
|
|
|
pub hashes: Vec<Hash>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for Locator {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2017-02-08 00:52:17 +03:00
|
|
|
writer.write_u8(self.hashes.len() as u8)?;
|
|
|
|
for h in &self.hashes {
|
|
|
|
h.write(writer)?
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for Locator {
|
2017-02-08 00:52:17 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<Locator, ser::Error> {
|
|
|
|
let len = reader.read_u8()?;
|
|
|
|
let mut hashes = Vec::with_capacity(len as usize);
|
|
|
|
for _ in 0..len {
|
|
|
|
hashes.push(Hash::read(reader)?);
|
|
|
|
}
|
|
|
|
Ok(Locator { hashes: hashes })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serializable wrapper for a list of block headers.
|
|
|
|
pub struct Headers {
|
|
|
|
pub headers: Vec<BlockHeader>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for Headers {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2017-02-08 00:52:17 +03:00
|
|
|
writer.write_u16(self.headers.len() as u16)?;
|
|
|
|
for h in &self.headers {
|
|
|
|
h.write(writer)?
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for Headers {
|
2017-02-08 00:52:17 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<Headers, ser::Error> {
|
|
|
|
let len = reader.read_u16()?;
|
|
|
|
let mut headers = Vec::with_capacity(len as usize);
|
|
|
|
for _ in 0..len {
|
|
|
|
headers.push(BlockHeader::read(reader)?);
|
|
|
|
}
|
|
|
|
Ok(Headers { headers: headers })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 06:11:49 +03:00
|
|
|
/// Placeholder for messages like Ping and Pong that don't send anything but
|
|
|
|
/// the header.
|
|
|
|
pub struct Empty {}
|
|
|
|
|
|
|
|
impl Writeable for Empty {
|
2017-08-10 03:54:10 +03:00
|
|
|
fn write<W: Writer>(&self, _: &mut W) -> Result<(), ser::Error> {
|
2016-12-11 06:11:49 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for Empty {
|
2017-08-10 03:54:10 +03:00
|
|
|
fn read(_: &mut Reader) -> Result<Empty, ser::Error> {
|
2016-12-11 06:11:49 +03:00
|
|
|
Ok(Empty {})
|
|
|
|
}
|
|
|
|
}
|