match <($MsgType)>::read(&mut reader) {
Ok(msg) => msg,
Err(e) => match e {
- msgs::DecodeError::UnknownRealmByte => return,
+ msgs::DecodeError::UnknownVersion => return,
msgs::DecodeError::UnknownRequiredFeature => return,
- msgs::DecodeError::BadPublicKey => return,
- msgs::DecodeError::BadSignature => return,
- msgs::DecodeError::BadText => return,
+ msgs::DecodeError::InvalidValue => return,
msgs::DecodeError::ExtraAddressesPerType => return,
msgs::DecodeError::BadLengthDescriptor => return,
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
- msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"),
msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
}
}
match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..])) {
Err(err) => {
let error_code = match err {
- msgs::DecodeError::UnknownRealmByte => 0x4000 | 1,
+ msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
_ => 0x2000 | 2, // Should never happen
};
return_err!("Unable to decode our hop data", error_code, &[0;0]);
/// An error in decoding a message or struct.
#[derive(Debug)]
pub enum DecodeError {
- /// Unknown realm byte in an OnionHopData packet
- UnknownRealmByte,
+ /// A version byte specified something we don't know how to handle.
+ /// Includes unknown realm byte in an OnionHopData packet
+ UnknownVersion,
/// Unknown feature mandating we fail to parse message
UnknownRequiredFeature,
- /// Failed to decode a public key (ie it's invalid)
- BadPublicKey,
- /// Failed to decode a signature (ie it's invalid)
- BadSignature,
- /// Value expected to be text wasn't decodable as text
- BadText,
+ /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0
+ /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc
+ InvalidValue,
/// Buffer too short
ShortRead,
/// node_announcement included more than one address of a given type!
BadLengthDescriptor,
/// Error from std::io
Io(::std::io::Error),
- /// 1 or 0 is not found for boolean value
- InvalidValue,
}
/// Tracks localfeatures which are only in init messages
impl Error for DecodeError {
fn description(&self) -> &str {
match *self {
- DecodeError::UnknownRealmByte => "Unknown realm byte in Onion packet",
+ DecodeError::UnknownVersion => "Unknown realm byte in Onion packet",
DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode",
- DecodeError::BadPublicKey => "Invalid public key in packet",
- DecodeError::BadSignature => "Invalid signature in packet",
- DecodeError::BadText => "Invalid text in packet",
+ DecodeError::InvalidValue => "Nonsense bytes didn't map to the type they were interpreted as",
DecodeError::ShortRead => "Packet extended beyond the provided bytes",
DecodeError::ExtraAddressesPerType => "More than one address of a single type",
DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
DecodeError::Io(ref e) => e.description(),
- DecodeError::InvalidValue => "0 or 1 is not found for boolean",
}
}
}
realm: {
let r: u8 = Readable::read(r)?;
if r != 0 {
- return Err(DecodeError::UnknownRealmByte);
+ return Err(DecodeError::UnknownVersion);
}
r
},
sz = cmp::min(data_len, sz);
match String::from_utf8(data[..sz as usize].to_vec()) {
Ok(s) => s,
- Err(_) => return Err(DecodeError::BadText),
+ Err(_) => return Err(DecodeError::InvalidValue),
}
}
})
Ok(x) => x,
Err(e) => {
match e {
- msgs::DecodeError::UnknownRealmByte => return Err(PeerHandleError{ no_connection_possible: false }),
+ msgs::DecodeError::UnknownVersion => return Err(PeerHandleError{ no_connection_possible: false }),
msgs::DecodeError::UnknownRequiredFeature => {
log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to udpate!");
continue;
},
- msgs::DecodeError::BadPublicKey => return Err(PeerHandleError{ no_connection_possible: false }),
- msgs::DecodeError::BadSignature => return Err(PeerHandleError{ no_connection_possible: false }),
- msgs::DecodeError::BadText => return Err(PeerHandleError{ no_connection_possible: false }),
+ msgs::DecodeError::InvalidValue => return Err(PeerHandleError{ no_connection_possible: false }),
msgs::DecodeError::ShortRead => return Err(PeerHandleError{ no_connection_possible: false }),
msgs::DecodeError::ExtraAddressesPerType => {
log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
},
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
- msgs::DecodeError::InvalidValue => panic!("should not happen with message decoding"),
}
}
};
let buf: [u8; 33] = Readable::read(r)?;
match PublicKey::from_slice(&Secp256k1::without_caps(), &buf) {
Ok(key) => Ok(key),
- Err(_) => return Err(DecodeError::BadPublicKey),
+ Err(_) => return Err(DecodeError::InvalidValue),
}
}
}
let buf: [u8; 64] = Readable::read(r)?;
match Signature::from_compact(&Secp256k1::without_caps(), &buf) {
Ok(sig) => Ok(sig),
- Err(_) => return Err(DecodeError::BadSignature),
+ Err(_) => return Err(DecodeError::InvalidValue),
}
}
}