Merge pull request #395 from crawford/clippy

Various cleanup suggested by cargo clippy
v0.7.x
Dario Nieuwenhuis 2020-12-26 23:53:25 +01:00 committed by GitHub
commit 29cc59c89d
20 changed files with 409 additions and 409 deletions

View File

@ -91,7 +91,7 @@ fn main() {
let client = match socket.recv() { let client = match socket.recv() {
Ok((data, endpoint)) => { Ok((data, endpoint)) => {
debug!("udp:6969 recv data: {:?} from {}", debug!("udp:6969 recv data: {:?} from {}",
str::from_utf8(data.as_ref()).unwrap(), endpoint); str::from_utf8(data).unwrap(), endpoint);
Some(endpoint) Some(endpoint)
} }
Err(_) => None Err(_) => None

View File

@ -288,22 +288,22 @@ enum Packet<'a> {
impl<'a> Packet<'a> { impl<'a> Packet<'a> {
fn neighbor_addr(&self) -> Option<IpAddress> { fn neighbor_addr(&self) -> Option<IpAddress> {
match self { match *self {
&Packet::None => None, Packet::None => None,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Packet::Arp(_) => None, Packet::Arp(_) => None,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Packet::Icmpv4((ref ipv4_repr, _)) => Some(ipv4_repr.dst_addr.into()), Packet::Icmpv4((ref ipv4_repr, _)) => Some(ipv4_repr.dst_addr.into()),
#[cfg(feature = "proto-igmp")] #[cfg(feature = "proto-igmp")]
&Packet::Igmp((ref ipv4_repr, _)) => Some(ipv4_repr.dst_addr.into()), Packet::Igmp((ref ipv4_repr, _)) => Some(ipv4_repr.dst_addr.into()),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Packet::Icmpv6((ref ipv6_repr, _)) => Some(ipv6_repr.dst_addr.into()), Packet::Icmpv6((ref ipv6_repr, _)) => Some(ipv6_repr.dst_addr.into()),
#[cfg(feature = "socket-raw")] #[cfg(feature = "socket-raw")]
&Packet::Raw((ref ip_repr, _)) => Some(ip_repr.dst_addr()), Packet::Raw((ref ip_repr, _)) => Some(ip_repr.dst_addr()),
#[cfg(feature = "socket-udp")] #[cfg(feature = "socket-udp")]
&Packet::Udp((ref ip_repr, _)) => Some(ip_repr.dst_addr()), Packet::Udp((ref ip_repr, _)) => Some(ip_repr.dst_addr()),
#[cfg(feature = "socket-tcp")] #[cfg(feature = "socket-tcp")]
&Packet::Tcp((ref ip_repr, _)) => Some(ip_repr.dst_addr()) Packet::Tcp((ref ip_repr, _)) => Some(ip_repr.dst_addr())
} }
} }
} }
@ -723,7 +723,7 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
pub fn has_solicited_node(&self, addr: Ipv6Address) -> bool { pub fn has_solicited_node(&self, addr: Ipv6Address) -> bool {
self.ip_addrs.iter().find(|cidr| { self.ip_addrs.iter().find(|cidr| {
match *cidr { match *cidr {
&IpCidr::Ipv6(cidr) if cidr.address() != Ipv6Address::LOOPBACK=> { IpCidr::Ipv6(cidr) if cidr.address() != Ipv6Address::LOOPBACK=> {
// Take the lower order 24 bits of the IPv6 address and // Take the lower order 24 bits of the IPv6 address and
// append those bits to FF02:0:0:0:0:1:FF00::/104. // append those bits to FF02:0:0:0:0:1:FF00::/104.
addr.as_bytes()[14..] == cidr.address().as_bytes()[14..] addr.as_bytes()[14..] == cidr.address().as_bytes()[14..]
@ -744,8 +744,8 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
pub fn ipv4_address(&self) -> Option<Ipv4Address> { pub fn ipv4_address(&self) -> Option<Ipv4Address> {
self.ip_addrs.iter() self.ip_addrs.iter()
.filter_map( .filter_map(
|addr| match addr { |addr| match *addr {
&IpCidr::Ipv4(cidr) => Some(cidr.address()), IpCidr::Ipv4(cidr) => Some(cidr.address()),
_ => None, _ => None,
}) })
.next() .next()
@ -1510,7 +1510,7 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
let tx_len = EthernetFrame::<&[u8]>::buffer_len(buffer_len); let tx_len = EthernetFrame::<&[u8]>::buffer_len(buffer_len);
tx_token.consume(timestamp, tx_len, |tx_buffer| { tx_token.consume(timestamp, tx_len, |tx_buffer| {
debug_assert!(tx_buffer.as_ref().len() == tx_len); debug_assert!(tx_buffer.as_ref().len() == tx_len);
let mut frame = EthernetFrame::new_unchecked(tx_buffer.as_mut()); let mut frame = EthernetFrame::new_unchecked(tx_buffer);
frame.set_src_addr(self.ethernet_addr); frame.set_src_addr(self.ethernet_addr);
f(frame); f(frame);
@ -1558,24 +1558,24 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
if dst_addr.is_multicast() { if dst_addr.is_multicast() {
let b = dst_addr.as_bytes(); let b = dst_addr.as_bytes();
let hardware_addr = let hardware_addr =
match dst_addr { match *dst_addr {
&IpAddress::Unspecified => IpAddress::Unspecified =>
None, None,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&IpAddress::Ipv4(_addr) => IpAddress::Ipv4(_addr) =>
Some(EthernetAddress::from_bytes(&[ Some(EthernetAddress::from_bytes(&[
0x01, 0x00, 0x01, 0x00,
0x5e, b[1] & 0x7F, 0x5e, b[1] & 0x7F,
b[2], b[3], b[2], b[3],
])), ])),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&IpAddress::Ipv6(_addr) => IpAddress::Ipv6(_addr) =>
Some(EthernetAddress::from_bytes(&[ Some(EthernetAddress::from_bytes(&[
0x33, 0x33, 0x33, 0x33,
b[12], b[13], b[12], b[13],
b[14], b[15], b[14], b[15],
])), ])),
&IpAddress::__Nonexhaustive => IpAddress::__Nonexhaustive =>
unreachable!() unreachable!()
}; };
match hardware_addr { match hardware_addr {

View File

@ -166,18 +166,18 @@ pub type Result<T> = core::result::Result<T, Error>;
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Error::Exhausted => write!(f, "buffer space exhausted"), Error::Exhausted => write!(f, "buffer space exhausted"),
&Error::Illegal => write!(f, "illegal operation"), Error::Illegal => write!(f, "illegal operation"),
&Error::Unaddressable => write!(f, "unaddressable destination"), Error::Unaddressable => write!(f, "unaddressable destination"),
&Error::Finished => write!(f, "operation finished"), Error::Finished => write!(f, "operation finished"),
&Error::Truncated => write!(f, "truncated packet"), Error::Truncated => write!(f, "truncated packet"),
&Error::Checksum => write!(f, "checksum error"), Error::Checksum => write!(f, "checksum error"),
&Error::Unrecognized => write!(f, "unrecognized packet"), Error::Unrecognized => write!(f, "unrecognized packet"),
&Error::Fragmented => write!(f, "fragmented packet"), Error::Fragmented => write!(f, "fragmented packet"),
&Error::Malformed => write!(f, "malformed packet"), Error::Malformed => write!(f, "malformed packet"),
&Error::Dropped => write!(f, "dropped by socket"), Error::Dropped => write!(f, "dropped by socket"),
&Error::__Nonexhaustive => unreachable!() Error::__Nonexhaustive => unreachable!()
} }
} }
} }

View File

@ -316,9 +316,9 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
pub(crate) fn process(&mut self, ip_repr: &IpRepr, icmp_repr: &IcmpRepr, pub(crate) fn process(&mut self, ip_repr: &IpRepr, icmp_repr: &IcmpRepr,
_cksum: &ChecksumCapabilities) -> Result<()> { _cksum: &ChecksumCapabilities) -> Result<()> {
match icmp_repr { match *icmp_repr {
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&IcmpRepr::Ipv4(ref icmp_repr) => { IcmpRepr::Ipv4(ref icmp_repr) => {
let packet_buf = self.rx_buffer.enqueue(icmp_repr.buffer_len(), let packet_buf = self.rx_buffer.enqueue(icmp_repr.buffer_len(),
ip_repr.src_addr())?; ip_repr.src_addr())?;
icmp_repr.emit(&mut Icmpv4Packet::new_unchecked(packet_buf), icmp_repr.emit(&mut Icmpv4Packet::new_unchecked(packet_buf),
@ -328,7 +328,7 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
self.meta.handle, icmp_repr.buffer_len(), packet_buf.len()); self.meta.handle, icmp_repr.buffer_len(), packet_buf.len());
}, },
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&IcmpRepr::Ipv6(ref icmp_repr) => { IcmpRepr::Ipv6(ref icmp_repr) => {
let packet_buf = self.rx_buffer.enqueue(icmp_repr.buffer_len(), let packet_buf = self.rx_buffer.enqueue(icmp_repr.buffer_len(),
ip_repr.src_addr())?; ip_repr.src_addr())?;
icmp_repr.emit(&ip_repr.src_addr(), &ip_repr.dst_addr(), icmp_repr.emit(&ip_repr.src_addr(), &ip_repr.dst_addr(),

View File

@ -69,8 +69,8 @@ pub(crate) enum PollAt {
impl PollAt { impl PollAt {
#[cfg(feature = "socket-tcp")] #[cfg(feature = "socket-tcp")]
fn is_ingress(&self) -> bool { fn is_ingress(&self) -> bool {
match self { match *self {
&PollAt::Ingress => true, PollAt::Ingress => true,
_ => false, _ => false,
} }
} }

View File

@ -115,7 +115,7 @@ impl<'a, 'b> RawSocket<'a, 'b> {
net_trace!("{}:{}:{}: buffer to send {} octets", net_trace!("{}:{}:{}: buffer to send {} octets",
self.meta.handle, self.ip_version, self.ip_protocol, self.meta.handle, self.ip_version, self.ip_protocol,
packet_buf.len()); packet_buf.len());
Ok(packet_buf.as_mut()) Ok(packet_buf)
} }
/// Enqueue a packet to send, and fill it from a slice. /// Enqueue a packet to send, and fill it from a slice.
@ -165,8 +165,8 @@ impl<'a, 'b> RawSocket<'a, 'b> {
let header_len = ip_repr.buffer_len(); let header_len = ip_repr.buffer_len();
let total_len = header_len + payload.len(); let total_len = header_len + payload.len();
let packet_buf = self.rx_buffer.enqueue(total_len, ())?; let packet_buf = self.rx_buffer.enqueue(total_len, ())?;
ip_repr.emit(&mut packet_buf.as_mut()[..header_len], &checksum_caps); ip_repr.emit(&mut packet_buf[..header_len], &checksum_caps);
packet_buf.as_mut()[header_len..].copy_from_slice(payload); packet_buf[header_len..].copy_from_slice(payload);
net_trace!("{}:{}:{}: receiving {} octets", net_trace!("{}:{}:{}: receiving {} octets",
self.meta.handle, self.ip_version, self.ip_protocol, self.meta.handle, self.ip_version, self.ip_protocol,
@ -179,10 +179,10 @@ impl<'a, 'b> RawSocket<'a, 'b> {
where F: FnOnce((IpRepr, &[u8])) -> Result<()> { where F: FnOnce((IpRepr, &[u8])) -> Result<()> {
fn prepare<'a>(protocol: IpProtocol, buffer: &'a mut [u8], fn prepare<'a>(protocol: IpProtocol, buffer: &'a mut [u8],
_checksum_caps: &ChecksumCapabilities) -> Result<(IpRepr, &'a [u8])> { _checksum_caps: &ChecksumCapabilities) -> Result<(IpRepr, &'a [u8])> {
match IpVersion::of_packet(buffer.as_ref())? { match IpVersion::of_packet(buffer)? {
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
IpVersion::Ipv4 => { IpVersion::Ipv4 => {
let mut packet = Ipv4Packet::new_checked(buffer.as_mut())?; let mut packet = Ipv4Packet::new_checked(buffer)?;
if packet.protocol() != protocol { return Err(Error::Unaddressable) } if packet.protocol() != protocol { return Err(Error::Unaddressable) }
if _checksum_caps.ipv4.tx() { if _checksum_caps.ipv4.tx() {
packet.fill_checksum(); packet.fill_checksum();
@ -198,7 +198,7 @@ impl<'a, 'b> RawSocket<'a, 'b> {
} }
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
IpVersion::Ipv6 => { IpVersion::Ipv6 => {
let packet = Ipv6Packet::new_checked(buffer.as_mut())?; let packet = Ipv6Packet::new_checked(buffer)?;
if packet.next_header() != protocol { return Err(Error::Unaddressable) } if packet.next_header() != protocol { return Err(Error::Unaddressable) }
let packet = Ipv6Packet::new_unchecked(&*packet.into_inner()); let packet = Ipv6Packet::new_unchecked(&*packet.into_inner());
let ipv6_repr = Ipv6Repr::parse(&packet)?; let ipv6_repr = Ipv6Repr::parse(&packet)?;
@ -213,7 +213,7 @@ impl<'a, 'b> RawSocket<'a, 'b> {
let ip_protocol = self.ip_protocol; let ip_protocol = self.ip_protocol;
let ip_version = self.ip_version; let ip_version = self.ip_version;
self.tx_buffer.dequeue_with(|&mut (), packet_buf| { self.tx_buffer.dequeue_with(|&mut (), packet_buf| {
match prepare(ip_protocol, packet_buf.as_mut(), &checksum_caps) { match prepare(ip_protocol, packet_buf, &checksum_caps) {
Ok((ip_repr, raw_packet)) => { Ok((ip_repr, raw_packet)) => {
net_trace!("{}:{}:{}: sending {} octets", net_trace!("{}:{}:{}: sending {} octets",
handle, ip_version, ip_protocol, handle, ip_version, ip_protocol,

View File

@ -75,7 +75,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
ManagedSlice::Owned(ref mut sockets) => { ManagedSlice::Owned(ref mut sockets) => {
sockets.push(None); sockets.push(None);
let index = sockets.len() - 1; let index = sockets.len() - 1;
return put(index, &mut sockets[index], socket) put(index, &mut sockets[index], socket)
} }
} }
} }
@ -139,25 +139,25 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
pub fn prune(&mut self) { pub fn prune(&mut self) {
for (index, item) in self.sockets.iter_mut().enumerate() { for (index, item) in self.sockets.iter_mut().enumerate() {
let mut may_remove = false; let mut may_remove = false;
if let &mut Some(Item { refs: 0, ref mut socket }) = item { if let Some(Item { refs: 0, ref mut socket }) = *item {
match socket { match *socket {
#[cfg(feature = "socket-raw")] #[cfg(feature = "socket-raw")]
&mut Socket::Raw(_) => Socket::Raw(_) =>
may_remove = true, may_remove = true,
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))] #[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
&mut Socket::Icmp(_) => Socket::Icmp(_) =>
may_remove = true, may_remove = true,
#[cfg(feature = "socket-udp")] #[cfg(feature = "socket-udp")]
&mut Socket::Udp(_) => Socket::Udp(_) =>
may_remove = true, may_remove = true,
#[cfg(feature = "socket-tcp")] #[cfg(feature = "socket-tcp")]
&mut Socket::Tcp(ref mut socket) => Socket::Tcp(ref mut socket) =>
if socket.state() == TcpState::Closed { if socket.state() == TcpState::Closed {
may_remove = true may_remove = true
} else { } else {
socket.close() socket.close()
}, },
&mut Socket::__Nonexhaustive(_) => unreachable!() Socket::__Nonexhaustive(_) => unreachable!()
} }
} }
if may_remove { if may_remove {

View File

@ -34,18 +34,18 @@ pub enum State {
impl fmt::Display for State { impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&State::Closed => write!(f, "CLOSED"), State::Closed => write!(f, "CLOSED"),
&State::Listen => write!(f, "LISTEN"), State::Listen => write!(f, "LISTEN"),
&State::SynSent => write!(f, "SYN-SENT"), State::SynSent => write!(f, "SYN-SENT"),
&State::SynReceived => write!(f, "SYN-RECEIVED"), State::SynReceived => write!(f, "SYN-RECEIVED"),
&State::Established => write!(f, "ESTABLISHED"), State::Established => write!(f, "ESTABLISHED"),
&State::FinWait1 => write!(f, "FIN-WAIT-1"), State::FinWait1 => write!(f, "FIN-WAIT-1"),
&State::FinWait2 => write!(f, "FIN-WAIT-2"), State::FinWait2 => write!(f, "FIN-WAIT-2"),
&State::CloseWait => write!(f, "CLOSE-WAIT"), State::CloseWait => write!(f, "CLOSE-WAIT"),
&State::Closing => write!(f, "CLOSING"), State::Closing => write!(f, "CLOSING"),
&State::LastAck => write!(f, "LAST-ACK"), State::LastAck => write!(f, "LAST-ACK"),
&State::TimeWait => write!(f, "TIME-WAIT") State::TimeWait => write!(f, "TIME-WAIT")
} }
} }
} }
@ -1898,7 +1898,7 @@ mod test {
let _ = log::set_logger(&LOGGER); let _ = log::set_logger(&LOGGER);
log::set_max_level(log::LevelFilter::Trace); log::set_max_level(log::LevelFilter::Trace);
println!(""); println!();
} }
fn socket() -> TcpSocket<'static> { fn socket() -> TcpSocket<'static> {

View File

@ -290,16 +290,16 @@ impl Repr {
/// Return the length of a packet that will be emitted from this high-level representation. /// Return the length of a packet that will be emitted from this high-level representation.
pub fn buffer_len(&self) -> usize { pub fn buffer_len(&self) -> usize {
match self { match *self {
&Repr::EthernetIpv4 { .. } => field::TPA(6, 4).end, Repr::EthernetIpv4 { .. } => field::TPA(6, 4).end,
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
/// Emit a high-level representation into an Address Resolution Protocol packet. /// Emit a high-level representation into an Address Resolution Protocol packet.
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, packet: &mut Packet<T>) { pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, packet: &mut Packet<T>) {
match self { match *self {
&Repr::EthernetIpv4 { Repr::EthernetIpv4 {
operation, operation,
source_hardware_addr, source_protocol_addr, source_hardware_addr, source_protocol_addr,
target_hardware_addr, target_protocol_addr target_hardware_addr, target_protocol_addr
@ -314,7 +314,7 @@ impl Repr {
packet.set_target_hardware_addr(target_hardware_addr.as_bytes()); packet.set_target_hardware_addr(target_hardware_addr.as_bytes());
packet.set_target_protocol_addr(target_protocol_addr.as_bytes()); packet.set_target_protocol_addr(target_protocol_addr.as_bytes());
}, },
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
} }
@ -340,8 +340,8 @@ impl<T: AsRef<[u8]>> fmt::Display for Packet<T> {
impl fmt::Display for Repr { impl fmt::Display for Repr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Repr::EthernetIpv4 { Repr::EthernetIpv4 {
operation, operation,
source_hardware_addr, source_protocol_addr, source_hardware_addr, source_protocol_addr,
target_hardware_addr, target_protocol_addr target_hardware_addr, target_protocol_addr
@ -351,7 +351,7 @@ impl fmt::Display for Repr {
target_hardware_addr, target_protocol_addr, target_hardware_addr, target_protocol_addr,
operation) operation)
}, },
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
} }

View File

@ -14,11 +14,11 @@ enum_with_unknown! {
impl fmt::Display for EtherType { impl fmt::Display for EtherType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&EtherType::Ipv4 => write!(f, "IPv4"), EtherType::Ipv4 => write!(f, "IPv4"),
&EtherType::Ipv6 => write!(f, "IPv6"), EtherType::Ipv6 => write!(f, "IPv6"),
&EtherType::Arp => write!(f, "ARP"), EtherType::Arp => write!(f, "ARP"),
&EtherType::Unknown(id) => write!(f, "0x{:04x}", id) EtherType::Unknown(id) => write!(f, "0x{:04x}", id)
} }
} }
} }

View File

@ -34,18 +34,18 @@ enum_with_unknown! {
impl fmt::Display for Message { impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Message::EchoReply => write!(f, "echo reply"), Message::EchoReply => write!(f, "echo reply"),
&Message::DstUnreachable => write!(f, "destination unreachable"), Message::DstUnreachable => write!(f, "destination unreachable"),
&Message::Redirect => write!(f, "message redirect"), Message::Redirect => write!(f, "message redirect"),
&Message::EchoRequest => write!(f, "echo request"), Message::EchoRequest => write!(f, "echo request"),
&Message::RouterAdvert => write!(f, "router advertisement"), Message::RouterAdvert => write!(f, "router advertisement"),
&Message::RouterSolicit => write!(f, "router solicitation"), Message::RouterSolicit => write!(f, "router solicitation"),
&Message::TimeExceeded => write!(f, "time exceeded"), Message::TimeExceeded => write!(f, "time exceeded"),
&Message::ParamProblem => write!(f, "parameter problem"), Message::ParamProblem => write!(f, "parameter problem"),
&Message::Timestamp => write!(f, "timestamp"), Message::Timestamp => write!(f, "timestamp"),
&Message::TimestampReply => write!(f, "timestamp reply"), Message::TimestampReply => write!(f, "timestamp reply"),
&Message::Unknown(id) => write!(f, "{}", id) Message::Unknown(id) => write!(f, "{}", id)
} }
} }
} }
@ -90,40 +90,40 @@ enum_with_unknown! {
impl fmt::Display for DstUnreachable { impl fmt::Display for DstUnreachable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&DstUnreachable::NetUnreachable => DstUnreachable::NetUnreachable =>
write!(f, "destination network unreachable"), write!(f, "destination network unreachable"),
&DstUnreachable::HostUnreachable => DstUnreachable::HostUnreachable =>
write!(f, "destination host unreachable"), write!(f, "destination host unreachable"),
&DstUnreachable::ProtoUnreachable => DstUnreachable::ProtoUnreachable =>
write!(f, "destination protocol unreachable"), write!(f, "destination protocol unreachable"),
&DstUnreachable::PortUnreachable => DstUnreachable::PortUnreachable =>
write!(f, "destination port unreachable"), write!(f, "destination port unreachable"),
&DstUnreachable::FragRequired => DstUnreachable::FragRequired =>
write!(f, "fragmentation required, and DF flag set"), write!(f, "fragmentation required, and DF flag set"),
&DstUnreachable::SrcRouteFailed => DstUnreachable::SrcRouteFailed =>
write!(f, "source route failed"), write!(f, "source route failed"),
&DstUnreachable::DstNetUnknown => DstUnreachable::DstNetUnknown =>
write!(f, "destination network unknown"), write!(f, "destination network unknown"),
&DstUnreachable::DstHostUnknown => DstUnreachable::DstHostUnknown =>
write!(f, "destination host unknown"), write!(f, "destination host unknown"),
&DstUnreachable::SrcHostIsolated => DstUnreachable::SrcHostIsolated =>
write!(f, "source host isolated"), write!(f, "source host isolated"),
&DstUnreachable::NetProhibited => DstUnreachable::NetProhibited =>
write!(f, "network administratively prohibited"), write!(f, "network administratively prohibited"),
&DstUnreachable::HostProhibited => DstUnreachable::HostProhibited =>
write!(f, "host administratively prohibited"), write!(f, "host administratively prohibited"),
&DstUnreachable::NetUnreachToS => DstUnreachable::NetUnreachToS =>
write!(f, "network unreachable for ToS"), write!(f, "network unreachable for ToS"),
&DstUnreachable::HostUnreachToS => DstUnreachable::HostUnreachToS =>
write!(f, "host unreachable for ToS"), write!(f, "host unreachable for ToS"),
&DstUnreachable::CommProhibited => DstUnreachable::CommProhibited =>
write!(f, "communication administratively prohibited"), write!(f, "communication administratively prohibited"),
&DstUnreachable::HostPrecedViol => DstUnreachable::HostPrecedViol =>
write!(f, "host precedence violation"), write!(f, "host precedence violation"),
&DstUnreachable::PrecedCutoff => DstUnreachable::PrecedCutoff =>
write!(f, "precedence cutoff in effect"), write!(f, "precedence cutoff in effect"),
&DstUnreachable::Unknown(id) => DstUnreachable::Unknown(id) =>
write!(f, "{}", id) write!(f, "{}", id)
} }
} }
@ -455,8 +455,8 @@ impl<'a> Repr<'a> {
pub fn emit<T>(&self, packet: &mut Packet<&mut T>, checksum_caps: &ChecksumCapabilities) pub fn emit<T>(&self, packet: &mut Packet<&mut T>, checksum_caps: &ChecksumCapabilities)
where T: AsRef<[u8]> + AsMut<[u8]> + ?Sized { where T: AsRef<[u8]> + AsMut<[u8]> + ?Sized {
packet.set_msg_code(0); packet.set_msg_code(0);
match self { match *self {
&Repr::EchoRequest { ident, seq_no, data } => { Repr::EchoRequest { ident, seq_no, data } => {
packet.set_msg_type(Message::EchoRequest); packet.set_msg_type(Message::EchoRequest);
packet.set_msg_code(0); packet.set_msg_code(0);
packet.set_echo_ident(ident); packet.set_echo_ident(ident);
@ -465,7 +465,7 @@ impl<'a> Repr<'a> {
packet.data_mut()[..data_len].copy_from_slice(&data[..data_len]) packet.data_mut()[..data_len].copy_from_slice(&data[..data_len])
}, },
&Repr::EchoReply { ident, seq_no, data } => { Repr::EchoReply { ident, seq_no, data } => {
packet.set_msg_type(Message::EchoReply); packet.set_msg_type(Message::EchoReply);
packet.set_msg_code(0); packet.set_msg_code(0);
packet.set_echo_ident(ident); packet.set_echo_ident(ident);
@ -474,7 +474,7 @@ impl<'a> Repr<'a> {
packet.data_mut()[..data_len].copy_from_slice(&data[..data_len]) packet.data_mut()[..data_len].copy_from_slice(&data[..data_len])
}, },
&Repr::DstUnreachable { reason, header, data } => { Repr::DstUnreachable { reason, header, data } => {
packet.set_msg_type(Message::DstUnreachable); packet.set_msg_type(Message::DstUnreachable);
packet.set_msg_code(reason.into()); packet.set_msg_code(reason.into());
@ -484,7 +484,7 @@ impl<'a> Repr<'a> {
payload.copy_from_slice(&data[..]) payload.copy_from_slice(&data[..])
} }
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
if checksum_caps.icmpv4.tx() { if checksum_caps.icmpv4.tx() {
@ -516,17 +516,17 @@ impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> {
impl<'a> fmt::Display for Repr<'a> { impl<'a> fmt::Display for Repr<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Repr::EchoRequest { ident, seq_no, data } => Repr::EchoRequest { ident, seq_no, data } =>
write!(f, "ICMPv4 echo request id={} seq={} len={}", write!(f, "ICMPv4 echo request id={} seq={} len={}",
ident, seq_no, data.len()), ident, seq_no, data.len()),
&Repr::EchoReply { ident, seq_no, data } => Repr::EchoReply { ident, seq_no, data } =>
write!(f, "ICMPv4 echo reply id={} seq={} len={}", write!(f, "ICMPv4 echo reply id={} seq={} len={}",
ident, seq_no, data.len()), ident, seq_no, data.len()),
&Repr::DstUnreachable { reason, .. } => Repr::DstUnreachable { reason, .. } =>
write!(f, "ICMPv4 destination unreachable ({})", write!(f, "ICMPv4 destination unreachable ({})",
reason), reason),
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
} }

View File

@ -77,21 +77,21 @@ impl Message {
impl fmt::Display for Message { impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Message::DstUnreachable => write!(f, "destination unreachable"), Message::DstUnreachable => write!(f, "destination unreachable"),
&Message::PktTooBig => write!(f, "packet too big"), Message::PktTooBig => write!(f, "packet too big"),
&Message::TimeExceeded => write!(f, "time exceeded"), Message::TimeExceeded => write!(f, "time exceeded"),
&Message::ParamProblem => write!(f, "parameter problem"), Message::ParamProblem => write!(f, "parameter problem"),
&Message::EchoReply => write!(f, "echo reply"), Message::EchoReply => write!(f, "echo reply"),
&Message::EchoRequest => write!(f, "echo request"), Message::EchoRequest => write!(f, "echo request"),
&Message::RouterSolicit => write!(f, "router solicitation"), Message::RouterSolicit => write!(f, "router solicitation"),
&Message::RouterAdvert => write!(f, "router advertisement"), Message::RouterAdvert => write!(f, "router advertisement"),
&Message::NeighborSolicit => write!(f, "neighbor solicitation"), Message::NeighborSolicit => write!(f, "neighbor solicitation"),
&Message::NeighborAdvert => write!(f, "neighbor advert"), Message::NeighborAdvert => write!(f, "neighbor advert"),
&Message::Redirect => write!(f, "redirect"), Message::Redirect => write!(f, "redirect"),
&Message::MldQuery => write!(f, "multicast listener query"), Message::MldQuery => write!(f, "multicast listener query"),
&Message::MldReport => write!(f, "multicast listener report"), Message::MldReport => write!(f, "multicast listener report"),
&Message::Unknown(id) => write!(f, "{}", id) Message::Unknown(id) => write!(f, "{}", id)
} }
} }
} }
@ -118,22 +118,22 @@ enum_with_unknown! {
impl fmt::Display for DstUnreachable { impl fmt::Display for DstUnreachable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&DstUnreachable::NoRoute => DstUnreachable::NoRoute =>
write!(f, "no route to destination"), write!(f, "no route to destination"),
&DstUnreachable::AdminProhibit => DstUnreachable::AdminProhibit =>
write!(f, "communication with destination administratively prohibited"), write!(f, "communication with destination administratively prohibited"),
&DstUnreachable::BeyondScope => DstUnreachable::BeyondScope =>
write!(f, "beyond scope of source address"), write!(f, "beyond scope of source address"),
&DstUnreachable::AddrUnreachable => DstUnreachable::AddrUnreachable =>
write!(f, "address unreachable"), write!(f, "address unreachable"),
&DstUnreachable::PortUnreachable => DstUnreachable::PortUnreachable =>
write!(f, "port unreachable"), write!(f, "port unreachable"),
&DstUnreachable::FailedPolicy => DstUnreachable::FailedPolicy =>
write!(f, "source address failed ingress/egress policy"), write!(f, "source address failed ingress/egress policy"),
&DstUnreachable::RejectRoute => DstUnreachable::RejectRoute =>
write!(f, "reject route to destination"), write!(f, "reject route to destination"),
&DstUnreachable::Unknown(id) => DstUnreachable::Unknown(id) =>
write!(f, "{}", id) write!(f, "{}", id)
} }
} }
@ -153,14 +153,14 @@ enum_with_unknown! {
impl fmt::Display for ParamProblem { impl fmt::Display for ParamProblem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&ParamProblem::ErroneousHdrField => ParamProblem::ErroneousHdrField =>
write!(f, "erroneous header field."), write!(f, "erroneous header field."),
&ParamProblem::UnrecognizedNxtHdr => ParamProblem::UnrecognizedNxtHdr =>
write!(f, "unrecognized next header type."), write!(f, "unrecognized next header type."),
&ParamProblem::UnrecognizedOption => ParamProblem::UnrecognizedOption =>
write!(f, "unrecognized IPv6 option."), write!(f, "unrecognized IPv6 option."),
&ParamProblem::Unknown(id) => ParamProblem::Unknown(id) =>
write!(f, "{}", id) write!(f, "{}", id)
} }
} }
@ -178,12 +178,12 @@ enum_with_unknown! {
impl fmt::Display for TimeExceeded { impl fmt::Display for TimeExceeded {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&TimeExceeded::HopLimitExceeded => TimeExceeded::HopLimitExceeded =>
write!(f, "hop limit exceeded in transit"), write!(f, "hop limit exceeded in transit"),
&TimeExceeded::FragReassemExceeded => TimeExceeded::FragReassemExceeded =>
write!(f, "fragment reassembly time exceeded"), write!(f, "fragment reassembly time exceeded"),
&TimeExceeded::Unknown(id) => TimeExceeded::Unknown(id) =>
write!(f, "{}", id) write!(f, "{}", id)
} }
} }
@ -667,15 +667,15 @@ impl<'a> Repr<'a> {
payload.copy_from_slice(&data[..]); payload.copy_from_slice(&data[..]);
} }
match self { match *self {
&Repr::DstUnreachable { reason, header, data } => { Repr::DstUnreachable { reason, header, data } => {
packet.set_msg_type(Message::DstUnreachable); packet.set_msg_type(Message::DstUnreachable);
packet.set_msg_code(reason.into()); packet.set_msg_code(reason.into());
emit_contained_packet(packet.payload_mut(), header, &data); emit_contained_packet(packet.payload_mut(), header, &data);
}, },
&Repr::PktTooBig { mtu, header, data } => { Repr::PktTooBig { mtu, header, data } => {
packet.set_msg_type(Message::PktTooBig); packet.set_msg_type(Message::PktTooBig);
packet.set_msg_code(0); packet.set_msg_code(0);
packet.set_pkt_too_big_mtu(mtu); packet.set_pkt_too_big_mtu(mtu);
@ -683,14 +683,14 @@ impl<'a> Repr<'a> {
emit_contained_packet(packet.payload_mut(), header, &data); emit_contained_packet(packet.payload_mut(), header, &data);
}, },
&Repr::TimeExceeded { reason, header, data } => { Repr::TimeExceeded { reason, header, data } => {
packet.set_msg_type(Message::TimeExceeded); packet.set_msg_type(Message::TimeExceeded);
packet.set_msg_code(reason.into()); packet.set_msg_code(reason.into());
emit_contained_packet(packet.payload_mut(), header, &data); emit_contained_packet(packet.payload_mut(), header, &data);
}, },
&Repr::ParamProblem { reason, pointer, header, data } => { Repr::ParamProblem { reason, pointer, header, data } => {
packet.set_msg_type(Message::ParamProblem); packet.set_msg_type(Message::ParamProblem);
packet.set_msg_code(reason.into()); packet.set_msg_code(reason.into());
packet.set_param_problem_ptr(pointer); packet.set_param_problem_ptr(pointer);
@ -698,7 +698,7 @@ impl<'a> Repr<'a> {
emit_contained_packet(packet.payload_mut(), header, &data); emit_contained_packet(packet.payload_mut(), header, &data);
}, },
&Repr::EchoRequest { ident, seq_no, data } => { Repr::EchoRequest { ident, seq_no, data } => {
packet.set_msg_type(Message::EchoRequest); packet.set_msg_type(Message::EchoRequest);
packet.set_msg_code(0); packet.set_msg_code(0);
packet.set_echo_ident(ident); packet.set_echo_ident(ident);
@ -707,7 +707,7 @@ impl<'a> Repr<'a> {
packet.payload_mut()[..data_len].copy_from_slice(&data[..data_len]) packet.payload_mut()[..data_len].copy_from_slice(&data[..data_len])
}, },
&Repr::EchoReply { ident, seq_no, data } => { Repr::EchoReply { ident, seq_no, data } => {
packet.set_msg_type(Message::EchoReply); packet.set_msg_type(Message::EchoReply);
packet.set_msg_code(0); packet.set_msg_code(0);
packet.set_echo_ident(ident); packet.set_echo_ident(ident);
@ -717,15 +717,15 @@ impl<'a> Repr<'a> {
}, },
#[cfg(feature = "ethernet")] #[cfg(feature = "ethernet")]
&Repr::Ndisc(ndisc) => { Repr::Ndisc(ndisc) => {
ndisc.emit(packet) ndisc.emit(packet)
}, },
&Repr::Mld(mld) => { Repr::Mld(mld) => {
mld.emit(packet) mld.emit(packet)
}, },
&Repr::__Nonexhaustive => unreachable!(), Repr::__Nonexhaustive => unreachable!(),
} }
if checksum_caps.icmpv6.tx() { if checksum_caps.icmpv6.tx() {

View File

@ -38,12 +38,12 @@ mod field {
impl fmt::Display for Message { impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Message::MembershipQuery => write!(f, "membership query"), Message::MembershipQuery => write!(f, "membership query"),
&Message::MembershipReportV2 => write!(f, "version 2 membership report"), Message::MembershipReportV2 => write!(f, "version 2 membership report"),
&Message::LeaveGroup => write!(f, "leave group"), Message::LeaveGroup => write!(f, "leave group"),
&Message::MembershipReportV1 => write!(f, "version 1 membership report"), Message::MembershipReportV1 => write!(f, "version 1 membership report"),
&Message::Unknown(id) => write!(f, "{}", id), Message::Unknown(id) => write!(f, "{}", id),
} }
} }
} }
@ -251,8 +251,8 @@ impl Repr {
pub fn emit<T>(&self, packet: &mut Packet<&mut T>) pub fn emit<T>(&self, packet: &mut Packet<&mut T>)
where T: AsRef<[u8]> + AsMut<[u8]> + ?Sized where T: AsRef<[u8]> + AsMut<[u8]> + ?Sized
{ {
match self { match *self {
&Repr::MembershipQuery { Repr::MembershipQuery {
max_resp_time, max_resp_time,
group_addr, group_addr,
version version
@ -266,7 +266,7 @@ impl Repr {
} }
packet.set_group_address(group_addr); packet.set_group_address(group_addr);
} }
&Repr::MembershipReport { Repr::MembershipReport {
group_addr, group_addr,
version, version,
} => { } => {
@ -277,7 +277,7 @@ impl Repr {
packet.set_max_resp_code(0); packet.set_max_resp_code(0);
packet.set_group_address(group_addr); packet.set_group_address(group_addr);
} }
&Repr::LeaveGroup { group_addr } => { Repr::LeaveGroup { group_addr } => {
packet.set_msg_type(Message::LeaveGroup); packet.set_msg_type(Message::LeaveGroup);
packet.set_group_address(group_addr); packet.set_group_address(group_addr);
} }
@ -327,8 +327,8 @@ impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> {
impl<'a> fmt::Display for Repr { impl<'a> fmt::Display for Repr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Repr::MembershipQuery { Repr::MembershipQuery {
max_resp_time, max_resp_time,
group_addr, group_addr,
version, version,
@ -339,7 +339,7 @@ impl<'a> fmt::Display for Repr {
group_addr, group_addr,
version) version)
} }
&Repr::MembershipReport { Repr::MembershipReport {
group_addr, group_addr,
version, version,
} => { } => {
@ -348,7 +348,7 @@ impl<'a> fmt::Display for Repr {
group_addr, group_addr,
version) version)
} }
&Repr::LeaveGroup { group_addr } => { Repr::LeaveGroup { group_addr } => {
write!(f, "IGMP leave group group_addr={})", group_addr) write!(f, "IGMP leave group group_addr={})", group_addr)
} }
} }
@ -363,8 +363,8 @@ impl<T: AsRef<[u8]>> PrettyPrint for Packet<T> {
indent: &mut PrettyIndent) indent: &mut PrettyIndent)
-> fmt::Result { -> fmt::Result {
match Packet::new_checked(buffer) { match Packet::new_checked(buffer) {
Err(err) => write!(f, "{}({})\n", indent, err), Err(err) => writeln!(f, "{}({})", indent, err),
Ok(packet) => write!(f, "{}{}\n", indent, packet), Ok(packet) => writeln!(f, "{}{}", indent, packet),
} }
} }
} }

View File

@ -38,13 +38,13 @@ impl Version {
impl fmt::Display for Version { impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Version::Unspecified => write!(f, "IPv?"), Version::Unspecified => write!(f, "IPv?"),
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Version::Ipv4 => write!(f, "IPv4"), Version::Ipv4 => write!(f, "IPv4"),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Version::Ipv6 => write!(f, "IPv6"), Version::Ipv6 => write!(f, "IPv6"),
&Version::__Nonexhaustive => unreachable!() Version::__Nonexhaustive => unreachable!()
} }
} }
} }
@ -67,18 +67,18 @@ enum_with_unknown! {
impl fmt::Display for Protocol { impl fmt::Display for Protocol {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Protocol::HopByHop => write!(f, "Hop-by-Hop"), Protocol::HopByHop => write!(f, "Hop-by-Hop"),
&Protocol::Icmp => write!(f, "ICMP"), Protocol::Icmp => write!(f, "ICMP"),
&Protocol::Igmp => write!(f, "IGMP"), Protocol::Igmp => write!(f, "IGMP"),
&Protocol::Tcp => write!(f, "TCP"), Protocol::Tcp => write!(f, "TCP"),
&Protocol::Udp => write!(f, "UDP"), Protocol::Udp => write!(f, "UDP"),
&Protocol::Ipv6Route => write!(f, "IPv6-Route"), Protocol::Ipv6Route => write!(f, "IPv6-Route"),
&Protocol::Ipv6Frag => write!(f, "IPv6-Frag"), Protocol::Ipv6Frag => write!(f, "IPv6-Frag"),
&Protocol::Icmpv6 => write!(f, "ICMPv6"), Protocol::Icmpv6 => write!(f, "ICMPv6"),
&Protocol::Ipv6NoNxt => write!(f, "IPv6-NoNxt"), Protocol::Ipv6NoNxt => write!(f, "IPv6-NoNxt"),
&Protocol::Ipv6Opts => write!(f, "IPv6-Opts"), Protocol::Ipv6Opts => write!(f, "IPv6-Opts"),
&Protocol::Unknown(id) => write!(f, "0x{:02x}", id) Protocol::Unknown(id) => write!(f, "0x{:02x}", id)
} }
} }
} }
@ -115,73 +115,73 @@ impl Address {
/// Return an address as a sequence of octets, in big-endian. /// Return an address as a sequence of octets, in big-endian.
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
match self { match *self {
&Address::Unspecified => &[], Address::Unspecified => &[],
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Address::Ipv4(ref addr) => addr.as_bytes(), Address::Ipv4(ref addr) => addr.as_bytes(),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Address::Ipv6(ref addr) => addr.as_bytes(), Address::Ipv6(ref addr) => addr.as_bytes(),
&Address::__Nonexhaustive => unreachable!() Address::__Nonexhaustive => unreachable!()
} }
} }
/// Query whether the address is a valid unicast address. /// Query whether the address is a valid unicast address.
pub fn is_unicast(&self) -> bool { pub fn is_unicast(&self) -> bool {
match self { match *self {
&Address::Unspecified => false, Address::Unspecified => false,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Address::Ipv4(addr) => addr.is_unicast(), Address::Ipv4(addr) => addr.is_unicast(),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Address::Ipv6(addr) => addr.is_unicast(), Address::Ipv6(addr) => addr.is_unicast(),
&Address::__Nonexhaustive => unreachable!() Address::__Nonexhaustive => unreachable!()
} }
} }
/// Query whether the address is a valid multicast address. /// Query whether the address is a valid multicast address.
pub fn is_multicast(&self) -> bool { pub fn is_multicast(&self) -> bool {
match self { match *self {
&Address::Unspecified => false, Address::Unspecified => false,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Address::Ipv4(addr) => addr.is_multicast(), Address::Ipv4(addr) => addr.is_multicast(),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Address::Ipv6(addr) => addr.is_multicast(), Address::Ipv6(addr) => addr.is_multicast(),
&Address::__Nonexhaustive => unreachable!() Address::__Nonexhaustive => unreachable!()
} }
} }
/// Query whether the address is the broadcast address. /// Query whether the address is the broadcast address.
pub fn is_broadcast(&self) -> bool { pub fn is_broadcast(&self) -> bool {
match self { match *self {
&Address::Unspecified => false, Address::Unspecified => false,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Address::Ipv4(addr) => addr.is_broadcast(), Address::Ipv4(addr) => addr.is_broadcast(),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Address::Ipv6(_) => false, Address::Ipv6(_) => false,
&Address::__Nonexhaustive => unreachable!() Address::__Nonexhaustive => unreachable!()
} }
} }
/// Query whether the address falls into the "unspecified" range. /// Query whether the address falls into the "unspecified" range.
pub fn is_unspecified(&self) -> bool { pub fn is_unspecified(&self) -> bool {
match self { match *self {
&Address::Unspecified => true, Address::Unspecified => true,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Address::Ipv4(addr) => addr.is_unspecified(), Address::Ipv4(addr) => addr.is_unspecified(),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Address::Ipv6(addr) => addr.is_unspecified(), Address::Ipv6(addr) => addr.is_unspecified(),
&Address::__Nonexhaustive => unreachable!() Address::__Nonexhaustive => unreachable!()
} }
} }
/// Return an unspecified address that has the same IP version as `self`. /// Return an unspecified address that has the same IP version as `self`.
pub fn to_unspecified(&self) -> Address { pub fn to_unspecified(&self) -> Address {
match self { match *self {
&Address::Unspecified => Address::Unspecified, Address::Unspecified => Address::Unspecified,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Address::Ipv4(_) => Address::Ipv4(Ipv4Address::UNSPECIFIED), Address::Ipv4(_) => Address::Ipv4(Ipv4Address::UNSPECIFIED),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Address::Ipv6(_) => Address::Ipv6(Ipv6Address::UNSPECIFIED), Address::Ipv6(_) => Address::Ipv6(Ipv6Address::UNSPECIFIED),
&Address::__Nonexhaustive => unreachable!() Address::__Nonexhaustive => unreachable!()
} }
} }
@ -260,13 +260,13 @@ impl From<Ipv6Address> for Address {
impl fmt::Display for Address { impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Address::Unspecified => write!(f, "*"), Address::Unspecified => write!(f, "*"),
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Address::Ipv4(addr) => write!(f, "{}", addr), Address::Ipv4(addr) => write!(f, "{}", addr),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Address::Ipv6(addr) => write!(f, "{}", addr), Address::Ipv6(addr) => write!(f, "{}", addr),
&Address::__Nonexhaustive => unreachable!() Address::__Nonexhaustive => unreachable!()
} }
} }
} }
@ -304,23 +304,23 @@ impl Cidr {
/// Return the IP address of this CIDR block. /// Return the IP address of this CIDR block.
pub fn address(&self) -> Address { pub fn address(&self) -> Address {
match self { match *self {
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Cidr::Ipv4(cidr) => Address::Ipv4(cidr.address()), Cidr::Ipv4(cidr) => Address::Ipv4(cidr.address()),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Cidr::Ipv6(cidr) => Address::Ipv6(cidr.address()), Cidr::Ipv6(cidr) => Address::Ipv6(cidr.address()),
&Cidr::__Nonexhaustive => unreachable!() Cidr::__Nonexhaustive => unreachable!()
} }
} }
/// Return the prefix length of this CIDR block. /// Return the prefix length of this CIDR block.
pub fn prefix_len(&self) -> u8 { pub fn prefix_len(&self) -> u8 {
match self { match *self {
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Cidr::Ipv4(cidr) => cidr.prefix_len(), Cidr::Ipv4(cidr) => cidr.prefix_len(),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Cidr::Ipv6(cidr) => cidr.prefix_len(), Cidr::Ipv6(cidr) => cidr.prefix_len(),
&Cidr::__Nonexhaustive => unreachable!() Cidr::__Nonexhaustive => unreachable!()
} }
} }
@ -383,12 +383,12 @@ impl From<Ipv6Cidr> for Cidr {
impl fmt::Display for Cidr { impl fmt::Display for Cidr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Cidr::Ipv4(cidr) => write!(f, "{}", cidr), Cidr::Ipv4(cidr) => write!(f, "{}", cidr),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Cidr::Ipv6(cidr) => write!(f, "{}", cidr), Cidr::Ipv6(cidr) => write!(f, "{}", cidr),
&Cidr::__Nonexhaustive => unreachable!() Cidr::__Nonexhaustive => unreachable!()
} }
} }
} }
@ -504,88 +504,88 @@ impl From<Ipv6Repr> for Repr {
impl Repr { impl Repr {
/// Return the protocol version. /// Return the protocol version.
pub fn version(&self) -> Version { pub fn version(&self) -> Version {
match self { match *self {
&Repr::Unspecified { .. } => Version::Unspecified, Repr::Unspecified { .. } => Version::Unspecified,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Repr::Ipv4(_) => Version::Ipv4, Repr::Ipv4(_) => Version::Ipv4,
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Repr::Ipv6(_) => Version::Ipv6, Repr::Ipv6(_) => Version::Ipv6,
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
/// Return the source address. /// Return the source address.
pub fn src_addr(&self) -> Address { pub fn src_addr(&self) -> Address {
match self { match *self {
&Repr::Unspecified { src_addr, .. } => src_addr, Repr::Unspecified { src_addr, .. } => src_addr,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Repr::Ipv4(repr) => Address::Ipv4(repr.src_addr), Repr::Ipv4(repr) => Address::Ipv4(repr.src_addr),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Repr::Ipv6(repr) => Address::Ipv6(repr.src_addr), Repr::Ipv6(repr) => Address::Ipv6(repr.src_addr),
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
/// Return the destination address. /// Return the destination address.
pub fn dst_addr(&self) -> Address { pub fn dst_addr(&self) -> Address {
match self { match *self {
&Repr::Unspecified { dst_addr, .. } => dst_addr, Repr::Unspecified { dst_addr, .. } => dst_addr,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Repr::Ipv4(repr) => Address::Ipv4(repr.dst_addr), Repr::Ipv4(repr) => Address::Ipv4(repr.dst_addr),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Repr::Ipv6(repr) => Address::Ipv6(repr.dst_addr), Repr::Ipv6(repr) => Address::Ipv6(repr.dst_addr),
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
/// Return the protocol. /// Return the protocol.
pub fn protocol(&self) -> Protocol { pub fn protocol(&self) -> Protocol {
match self { match *self {
&Repr::Unspecified { protocol, .. } => protocol, Repr::Unspecified { protocol, .. } => protocol,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Repr::Ipv4(repr) => repr.protocol, Repr::Ipv4(repr) => repr.protocol,
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Repr::Ipv6(repr) => repr.next_header, Repr::Ipv6(repr) => repr.next_header,
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
/// Return the payload length. /// Return the payload length.
pub fn payload_len(&self) -> usize { pub fn payload_len(&self) -> usize {
match self { match *self {
&Repr::Unspecified { payload_len, .. } => payload_len, Repr::Unspecified { payload_len, .. } => payload_len,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Repr::Ipv4(repr) => repr.payload_len, Repr::Ipv4(repr) => repr.payload_len,
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Repr::Ipv6(repr) => repr.payload_len, Repr::Ipv6(repr) => repr.payload_len,
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
/// Set the payload length. /// Set the payload length.
pub fn set_payload_len(&mut self, length: usize) { pub fn set_payload_len(&mut self, length: usize) {
match self { match *self {
&mut Repr::Unspecified { ref mut payload_len, .. } => Repr::Unspecified { ref mut payload_len, .. } =>
*payload_len = length, *payload_len = length,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&mut Repr::Ipv4(Ipv4Repr { ref mut payload_len, .. }) => Repr::Ipv4(Ipv4Repr { ref mut payload_len, .. }) =>
*payload_len = length, *payload_len = length,
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&mut Repr::Ipv6(Ipv6Repr { ref mut payload_len, .. }) => Repr::Ipv6(Ipv6Repr { ref mut payload_len, .. }) =>
*payload_len = length, *payload_len = length,
&mut Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
/// Return the TTL value. /// Return the TTL value.
pub fn hop_limit(&self) -> u8 { pub fn hop_limit(&self) -> u8 {
match self { match *self {
&Repr::Unspecified { hop_limit, .. } => hop_limit, Repr::Unspecified { hop_limit, .. } => hop_limit,
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Repr::Ipv4(Ipv4Repr { hop_limit, .. }) => hop_limit, Repr::Ipv4(Ipv4Repr { hop_limit, .. }) => hop_limit,
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Repr::Ipv6(Ipv6Repr { hop_limit, ..}) => hop_limit, Repr::Ipv6(Ipv6Repr { hop_limit, ..}) => hop_limit,
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
@ -722,16 +722,16 @@ impl Repr {
/// # Panics /// # Panics
/// This function panics if invoked on an unspecified representation. /// This function panics if invoked on an unspecified representation.
pub fn buffer_len(&self) -> usize { pub fn buffer_len(&self) -> usize {
match self { match *self {
&Repr::Unspecified { .. } => Repr::Unspecified { .. } =>
panic!("unspecified IP representation"), panic!("unspecified IP representation"),
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Repr::Ipv4(repr) => Repr::Ipv4(repr) =>
repr.buffer_len(), repr.buffer_len(),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Repr::Ipv6(repr) => Repr::Ipv6(repr) =>
repr.buffer_len(), repr.buffer_len(),
&Repr::__Nonexhaustive => Repr::__Nonexhaustive =>
unreachable!() unreachable!()
} }
} }
@ -741,16 +741,16 @@ impl Repr {
/// # Panics /// # Panics
/// This function panics if invoked on an unspecified representation. /// This function panics if invoked on an unspecified representation.
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, buffer: T, _checksum_caps: &ChecksumCapabilities) { pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, buffer: T, _checksum_caps: &ChecksumCapabilities) {
match self { match *self {
&Repr::Unspecified { .. } => Repr::Unspecified { .. } =>
panic!("unspecified IP representation"), panic!("unspecified IP representation"),
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
&Repr::Ipv4(repr) => Repr::Ipv4(repr) =>
repr.emit(&mut Ipv4Packet::new_unchecked(buffer), &_checksum_caps), repr.emit(&mut Ipv4Packet::new_unchecked(buffer), &_checksum_caps),
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
&Repr::Ipv6(repr) => Repr::Ipv6(repr) =>
repr.emit(&mut Ipv6Packet::new_unchecked(buffer)), repr.emit(&mut Ipv6Packet::new_unchecked(buffer)),
&Repr::__Nonexhaustive => Repr::__Nonexhaustive =>
unreachable!() unreachable!()
} }
} }
@ -879,11 +879,11 @@ pub fn pretty_print_ip_payload<T: Into<Repr>>(f: &mut fmt::Formatter, indent: &m
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
Protocol::Icmp => { Protocol::Icmp => {
indent.increase(f)?; indent.increase(f)?;
Icmpv4Packet::<&[u8]>::pretty_print(&payload.as_ref(), f, indent) Icmpv4Packet::<&[u8]>::pretty_print(&payload, f, indent)
} }
Protocol::Udp => { Protocol::Udp => {
indent.increase(f)?; indent.increase(f)?;
match UdpPacket::<&[u8]>::new_checked(payload.as_ref()) { match UdpPacket::<&[u8]>::new_checked(payload) {
Err(err) => write!(f, "{}({})", indent, err), Err(err) => write!(f, "{}({})", indent, err),
Ok(udp_packet) => { Ok(udp_packet) => {
match UdpRepr::parse(&udp_packet, &repr.src_addr(), match UdpRepr::parse(&udp_packet, &repr.src_addr(),
@ -901,7 +901,7 @@ pub fn pretty_print_ip_payload<T: Into<Repr>>(f: &mut fmt::Formatter, indent: &m
} }
Protocol::Tcp => { Protocol::Tcp => {
indent.increase(f)?; indent.increase(f)?;
match TcpPacket::<&[u8]>::new_checked(payload.as_ref()) { match TcpPacket::<&[u8]>::new_checked(payload) {
Err(err) => write!(f, "{}({})", indent, err), Err(err) => write!(f, "{}({})", indent, err),
Ok(tcp_packet) => { Ok(tcp_packet) => {
match TcpRepr::parse(&tcp_packet, &repr.src_addr(), match TcpRepr::parse(&tcp_packet, &repr.src_addr(),

View File

@ -13,10 +13,10 @@ enum_with_unknown! {
impl fmt::Display for Type { impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Type::Pad1 => write!(f, "Pad1"), Type::Pad1 => write!(f, "Pad1"),
&Type::PadN => write!(f, "PadN"), Type::PadN => write!(f, "PadN"),
&Type::Unknown(id) => write!(f, "{}", id) Type::Unknown(id) => write!(f, "{}", id)
} }
} }
} }
@ -39,12 +39,12 @@ enum_with_unknown! {
impl fmt::Display for FailureType { impl fmt::Display for FailureType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&FailureType::Skip => write!(f, "skip"), FailureType::Skip => write!(f, "skip"),
&FailureType::Discard => write!(f, "discard"), FailureType::Discard => write!(f, "discard"),
&FailureType::DiscardSendAll => write!(f, "discard and send error"), FailureType::DiscardSendAll => write!(f, "discard and send error"),
&FailureType::DiscardSendUnicast => write!(f, "discard and send error if unicast"), FailureType::DiscardSendUnicast => write!(f, "discard and send error if unicast"),
&FailureType::Unknown(id) => write!(f, "Unknown({})", id), FailureType::Unknown(id) => write!(f, "Unknown({})", id),
} }
} }
} }
@ -247,23 +247,23 @@ impl<'a> Repr<'a> {
/// Return the length of a header that will be emitted from this high-level representation. /// Return the length of a header that will be emitted from this high-level representation.
pub fn buffer_len(&self) -> usize { pub fn buffer_len(&self) -> usize {
match self { match *self {
&Repr::Pad1 => 1, Repr::Pad1 => 1,
&Repr::PadN(length) => Repr::PadN(length) =>
field::DATA(length).end, field::DATA(length).end,
&Repr::Unknown{ length, .. } => Repr::Unknown{ length, .. } =>
field::DATA(length).end, field::DATA(length).end,
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
/// Emit a high-level representation into an IPv6 Extension Header Option. /// Emit a high-level representation into an IPv6 Extension Header Option.
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]> + ?Sized>(&self, opt: &mut Ipv6Option<&'a mut T>) { pub fn emit<T: AsRef<[u8]> + AsMut<[u8]> + ?Sized>(&self, opt: &mut Ipv6Option<&'a mut T>) {
match self { match *self {
&Repr::Pad1 => Repr::Pad1 =>
opt.set_option_type(Type::Pad1), opt.set_option_type(Type::Pad1),
&Repr::PadN(len) => { Repr::PadN(len) => {
opt.set_option_type(Type::PadN); opt.set_option_type(Type::PadN);
opt.set_data_len(len); opt.set_data_len(len);
// Ensure all padding bytes are set to zero. // Ensure all padding bytes are set to zero.
@ -271,13 +271,13 @@ impl<'a> Repr<'a> {
*x = 0 *x = 0
} }
} }
&Repr::Unknown{ type_, length, data } => { Repr::Unknown{ type_, length, data } => {
opt.set_option_type(type_); opt.set_option_type(type_);
opt.set_data_len(length); opt.set_data_len(length);
opt.data_mut().copy_from_slice(&data[..length as usize]); opt.data_mut().copy_from_slice(&data[..length as usize]);
} }
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
} }
@ -351,15 +351,15 @@ impl<'a> Iterator for Ipv6OptionsIterator<'a> {
impl<'a> fmt::Display for Repr<'a> { impl<'a> fmt::Display for Repr<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "IPv6 Option ")?; write!(f, "IPv6 Option ")?;
match self { match *self {
&Repr::Pad1 => Repr::Pad1 =>
write!(f, "{} ", Type::Pad1), write!(f, "{} ", Type::Pad1),
&Repr::PadN(len) => Repr::PadN(len) =>
write!(f, "{} length={} ", Type::PadN, len), write!(f, "{} length={} ", Type::PadN, len),
&Repr::Unknown{ type_, length, .. } => Repr::Unknown{ type_, length, .. } =>
write!(f, "{} length={} ", type_, length), write!(f, "{} length={} ", type_, length),
&Repr::__Nonexhaustive => unreachable!() Repr::__Nonexhaustive => unreachable!()
} }
} }
} }

View File

@ -36,15 +36,15 @@ enum_with_unknown! {
impl fmt::Display for Type { impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Type::Type0 => write!(f, "Type0"), Type::Type0 => write!(f, "Type0"),
&Type::Nimrod => write!(f, "Nimrod"), Type::Nimrod => write!(f, "Nimrod"),
&Type::Type2 => write!(f, "Type2"), Type::Type2 => write!(f, "Type2"),
&Type::Rpl => write!(f, "Rpl"), Type::Rpl => write!(f, "Rpl"),
&Type::Experiment1 => write!(f, "Experiment1"), Type::Experiment1 => write!(f, "Experiment1"),
&Type::Experiment2 => write!(f, "Experiment2"), Type::Experiment2 => write!(f, "Experiment2"),
&Type::Reserved => write!(f, "Reserved"), Type::Reserved => write!(f, "Reserved"),
&Type::Unknown(id) => write!(f, "{}", id) Type::Unknown(id) => write!(f, "{}", id)
} }
} }
} }
@ -465,8 +465,8 @@ impl<'a> Repr<'a> {
/// Emit a high-level representation into an IPv6 Routing Header. /// Emit a high-level representation into an IPv6 Routing Header.
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]> + ?Sized>(&self, header: &mut Header<&mut T>) { pub fn emit<T: AsRef<[u8]> + AsMut<[u8]> + ?Sized>(&self, header: &mut Header<&mut T>) {
match self { match *self {
&Repr::Type2 { next_header, length, segments_left, home_address } => { Repr::Type2 { next_header, length, segments_left, home_address } => {
header.set_next_header(next_header); header.set_next_header(next_header);
header.set_header_len(length); header.set_header_len(length);
header.set_routing_type(Type::Type2); header.set_routing_type(Type::Type2);
@ -474,7 +474,7 @@ impl<'a> Repr<'a> {
header.clear_reserved(); header.clear_reserved();
header.set_home_address(home_address); header.set_home_address(home_address);
} }
&Repr::Rpl { next_header, length, segments_left, cmpr_i, cmpr_e, pad, addresses } => { Repr::Rpl { next_header, length, segments_left, cmpr_i, cmpr_e, pad, addresses } => {
header.set_next_header(next_header); header.set_next_header(next_header);
header.set_header_len(length); header.set_header_len(length);
header.set_routing_type(Type::Rpl); header.set_routing_type(Type::Rpl);
@ -486,24 +486,24 @@ impl<'a> Repr<'a> {
header.set_addresses(addresses); header.set_addresses(addresses);
} }
&Repr::__Nonexhaustive => unreachable!(), Repr::__Nonexhaustive => unreachable!(),
} }
} }
} }
impl<'a> fmt::Display for Repr<'a> { impl<'a> fmt::Display for Repr<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match *self {
&Repr::Type2 { next_header, length, segments_left, home_address } => { Repr::Type2 { next_header, length, segments_left, home_address } => {
write!(f, "IPv6 Routing next_hdr={} length={} type={} seg_left={} home_address={}", write!(f, "IPv6 Routing next_hdr={} length={} type={} seg_left={} home_address={}",
next_header, length, Type::Type2, segments_left, home_address) next_header, length, Type::Type2, segments_left, home_address)
} }
&Repr::Rpl { next_header, length, segments_left, cmpr_i, cmpr_e, pad, .. } => { Repr::Rpl { next_header, length, segments_left, cmpr_i, cmpr_e, pad, .. } => {
write!(f, "IPv6 Routing next_hdr={} length={} type={} seg_left={} cmpr_i={} cmpr_e={} pad={}", write!(f, "IPv6 Routing next_hdr={} length={} type={} seg_left={} cmpr_i={} cmpr_e={} pad={}",
next_header, length, Type::Rpl, segments_left, cmpr_i, cmpr_e, pad) next_header, length, Type::Rpl, segments_left, cmpr_i, cmpr_e, pad)
} }
&Repr::__Nonexhaustive => unreachable!(), Repr::__Nonexhaustive => unreachable!(),
} }
} }
} }

View File

@ -370,8 +370,8 @@ impl<'a> Repr<'a> {
pub fn emit<T>(&self, packet: &mut Packet<&mut T>) pub fn emit<T>(&self, packet: &mut Packet<&mut T>)
where T: AsRef<[u8]> + AsMut<[u8]> + ?Sized { where T: AsRef<[u8]> + AsMut<[u8]> + ?Sized {
match self { match *self {
&Repr::RouterSolicit { lladdr } => { Repr::RouterSolicit { lladdr } => {
packet.set_msg_type(Message::RouterSolicit); packet.set_msg_type(Message::RouterSolicit);
packet.set_msg_code(0); packet.set_msg_code(0);
packet.clear_reserved(); packet.clear_reserved();
@ -381,7 +381,7 @@ impl<'a> Repr<'a> {
} }
}, },
&Repr::RouterAdvert { hop_limit, flags, router_lifetime, reachable_time, Repr::RouterAdvert { hop_limit, flags, router_lifetime, reachable_time,
retrans_time, lladdr, mtu, prefix_info } => { retrans_time, lladdr, mtu, prefix_info } => {
packet.set_msg_type(Message::RouterAdvert); packet.set_msg_type(Message::RouterAdvert);
packet.set_msg_code(0); packet.set_msg_code(0);
@ -410,7 +410,7 @@ impl<'a> Repr<'a> {
} }
}, },
&Repr::NeighborSolicit { target_addr, lladdr } => { Repr::NeighborSolicit { target_addr, lladdr } => {
packet.set_msg_type(Message::NeighborSolicit); packet.set_msg_type(Message::NeighborSolicit);
packet.set_msg_code(0); packet.set_msg_code(0);
packet.clear_reserved(); packet.clear_reserved();
@ -422,7 +422,7 @@ impl<'a> Repr<'a> {
} }
}, },
&Repr::NeighborAdvert { flags, target_addr, lladdr } => { Repr::NeighborAdvert { flags, target_addr, lladdr } => {
packet.set_msg_type(Message::NeighborAdvert); packet.set_msg_type(Message::NeighborAdvert);
packet.set_msg_code(0); packet.set_msg_code(0);
packet.clear_reserved(); packet.clear_reserved();
@ -435,7 +435,7 @@ impl<'a> Repr<'a> {
} }
}, },
&Repr::Redirect { target_addr, dest_addr, lladdr, redirected_hdr } => { Repr::Redirect { target_addr, dest_addr, lladdr, redirected_hdr } => {
packet.set_msg_type(Message::Redirect); packet.set_msg_type(Message::Redirect);
packet.set_msg_code(0); packet.set_msg_code(0);
packet.clear_reserved(); packet.clear_reserved();

View File

@ -24,12 +24,12 @@ enum_with_unknown! {
impl fmt::Display for Type { impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
&Type::SourceLinkLayerAddr => write!(f, "source link-layer address"), Type::SourceLinkLayerAddr => write!(f, "source link-layer address"),
&Type::TargetLinkLayerAddr => write!(f, "target link-layer address"), Type::TargetLinkLayerAddr => write!(f, "target link-layer address"),
&Type::PrefixInformation => write!(f, "prefix information"), Type::PrefixInformation => write!(f, "prefix information"),
&Type::RedirectedHeader => write!(f, "redirected header"), Type::RedirectedHeader => write!(f, "redirected header"),
&Type::Mtu => write!(f, "mtu"), Type::Mtu => write!(f, "mtu"),
&Type::Unknown(id) => write!(f, "{}", id) Type::Unknown(id) => write!(f, "{}", id)
} }
} }
} }
@ -504,18 +504,18 @@ impl<'a> Repr<'a> {
/// Emit a high-level representation into an NDISC Option. /// Emit a high-level representation into an NDISC Option.
pub fn emit<T>(&self, opt: &mut NdiscOption<&'a mut T>) pub fn emit<T>(&self, opt: &mut NdiscOption<&'a mut T>)
where T: AsRef<[u8]> + AsMut<[u8]> + ?Sized { where T: AsRef<[u8]> + AsMut<[u8]> + ?Sized {
match self { match *self {
&Repr::SourceLinkLayerAddr(addr) => { Repr::SourceLinkLayerAddr(addr) => {
opt.set_option_type(Type::SourceLinkLayerAddr); opt.set_option_type(Type::SourceLinkLayerAddr);
opt.set_data_len(1); opt.set_data_len(1);
opt.set_link_layer_addr(addr); opt.set_link_layer_addr(addr);
}, },
&Repr::TargetLinkLayerAddr(addr) => { Repr::TargetLinkLayerAddr(addr) => {
opt.set_option_type(Type::TargetLinkLayerAddr); opt.set_option_type(Type::TargetLinkLayerAddr);
opt.set_data_len(1); opt.set_data_len(1);
opt.set_link_layer_addr(addr); opt.set_link_layer_addr(addr);
}, },
&Repr::PrefixInformation(PrefixInformation { Repr::PrefixInformation(PrefixInformation {
prefix_len, flags, valid_lifetime, prefix_len, flags, valid_lifetime,
preferred_lifetime, prefix preferred_lifetime, prefix
}) => { }) => {
@ -528,7 +528,7 @@ impl<'a> Repr<'a> {
opt.set_preferred_lifetime(preferred_lifetime); opt.set_preferred_lifetime(preferred_lifetime);
opt.set_prefix(prefix); opt.set_prefix(prefix);
}, },
&Repr::RedirectedHeader(RedirectedHeader { Repr::RedirectedHeader(RedirectedHeader {
header, data header, data
}) => { }) => {
let data_len = data.len() / 8; let data_len = data.len() / 8;
@ -541,12 +541,12 @@ impl<'a> Repr<'a> {
let payload = &mut ip_packet.into_inner()[header.buffer_len()..]; let payload = &mut ip_packet.into_inner()[header.buffer_len()..];
payload.copy_from_slice(&data[..data_len]); payload.copy_from_slice(&data[..data_len]);
} }
&Repr::Mtu(mtu) => { Repr::Mtu(mtu) => {
opt.set_option_type(Type::Mtu); opt.set_option_type(Type::Mtu);
opt.set_data_len(1); opt.set_data_len(1);
opt.set_mtu(mtu); opt.set_mtu(mtu);
} }
&Repr::Unknown { type_: id, length, data } => { Repr::Unknown { type_: id, length, data } => {
opt.set_option_type(Type::Unknown(id)); opt.set_option_type(Type::Unknown(id));
opt.set_data_len(length); opt.set_data_len(length);
opt.data_mut().copy_from_slice(data); opt.data_mut().copy_from_slice(data);
@ -558,29 +558,29 @@ impl<'a> Repr<'a> {
impl<'a> fmt::Display for Repr<'a> { impl<'a> fmt::Display for Repr<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "NDISC Option: ")?; write!(f, "NDISC Option: ")?;
match self { match *self {
&Repr::SourceLinkLayerAddr(addr) => { Repr::SourceLinkLayerAddr(addr) => {
write!(f, "SourceLinkLayer addr={}", addr) write!(f, "SourceLinkLayer addr={}", addr)
}, },
&Repr::TargetLinkLayerAddr(addr) => { Repr::TargetLinkLayerAddr(addr) => {
write!(f, "TargetLinkLayer addr={}", addr) write!(f, "TargetLinkLayer addr={}", addr)
}, },
&Repr::PrefixInformation(PrefixInformation { Repr::PrefixInformation(PrefixInformation {
prefix, prefix_len, prefix, prefix_len,
.. ..
}) => { }) => {
write!(f, "PrefixInformation prefix={}/{}", prefix, prefix_len) write!(f, "PrefixInformation prefix={}/{}", prefix, prefix_len)
}, },
&Repr::RedirectedHeader(RedirectedHeader { Repr::RedirectedHeader(RedirectedHeader {
header, header,
.. ..
}) => { }) => {
write!(f, "RedirectedHeader header={}", header) write!(f, "RedirectedHeader header={}", header)
}, },
&Repr::Mtu(mtu) => { Repr::Mtu(mtu) => {
write!(f, "MTU mtu={}", mtu) write!(f, "MTU mtu={}", mtu)
}, },
&Repr::Unknown { type_: id, length, .. } => { Repr::Unknown { type_: id, length, .. } => {
write!(f, "Unknown({}) length={}", id, length) write!(f, "Unknown({}) length={}", id, length)
} }
} }
@ -596,7 +596,7 @@ impl<T: AsRef<[u8]>> PrettyPrint for NdiscOption<T> {
Err(err) => return write!(f, "{}({})", indent, err), Err(err) => return write!(f, "{}({})", indent, err),
Ok(ndisc) => { Ok(ndisc) => {
match Repr::parse(&ndisc) { match Repr::parse(&ndisc) {
Err(_) => return Ok(()), Err(_) => Ok(()),
Ok(repr) => { Ok(repr) => {
write!(f, "{}{}", indent, repr) write!(f, "{}{}", indent, repr)
} }

View File

@ -48,7 +48,7 @@ impl PrettyIndent {
/// Increase indentation level. /// Increase indentation level.
pub fn increase(&mut self, f: &mut fmt::Formatter) -> fmt::Result { pub fn increase(&mut self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\n")?; writeln!(f)?;
self.level += 1; self.level += 1;
Ok(()) Ok(())
} }

View File

@ -645,28 +645,28 @@ impl<'a> TcpOption<'a> {
} }
pub fn buffer_len(&self) -> usize { pub fn buffer_len(&self) -> usize {
match self { match *self {
&TcpOption::EndOfList => 1, TcpOption::EndOfList => 1,
&TcpOption::NoOperation => 1, TcpOption::NoOperation => 1,
&TcpOption::MaxSegmentSize(_) => 4, TcpOption::MaxSegmentSize(_) => 4,
&TcpOption::WindowScale(_) => 3, TcpOption::WindowScale(_) => 3,
&TcpOption::SackPermitted => 2, TcpOption::SackPermitted => 2,
&TcpOption::SackRange(s) => s.iter().filter(|s| s.is_some()).count() * 8 + 2, TcpOption::SackRange(s) => s.iter().filter(|s| s.is_some()).count() * 8 + 2,
&TcpOption::Unknown { data, .. } => 2 + data.len() TcpOption::Unknown { data, .. } => 2 + data.len()
} }
} }
pub fn emit<'b>(&self, buffer: &'b mut [u8]) -> &'b mut [u8] { pub fn emit<'b>(&self, buffer: &'b mut [u8]) -> &'b mut [u8] {
let length; let length;
match self { match *self {
&TcpOption::EndOfList => { TcpOption::EndOfList => {
length = 1; length = 1;
// There may be padding space which also should be initialized. // There may be padding space which also should be initialized.
for p in buffer.iter_mut() { for p in buffer.iter_mut() {
*p = field::OPT_END; *p = field::OPT_END;
} }
} }
&TcpOption::NoOperation => { TcpOption::NoOperation => {
length = 1; length = 1;
buffer[0] = field::OPT_NOP; buffer[0] = field::OPT_NOP;
} }