Use hop limit instead of ttl
Use hop limit instead of ttl for method and member names.v0.7.x
parent
ee60efefd3
commit
c5c2264cdc
|
@ -628,7 +628,7 @@ impl<'b, 'c> InterfaceInner<'b, 'c> {
|
|||
dst_addr: ipv4_repr.src_addr,
|
||||
protocol: IpProtocol::Icmp,
|
||||
payload_len: icmp_repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
};
|
||||
Packet::Icmpv4((ipv4_reply_repr, icmp_repr))
|
||||
} else {
|
||||
|
@ -961,7 +961,7 @@ mod test {
|
|||
dst_addr: Ipv4Address::BROADCAST,
|
||||
protocol: IpProtocol::Unknown(0x0c),
|
||||
payload_len: 0,
|
||||
ttl: 0x40
|
||||
hop_limit: 0x40
|
||||
});
|
||||
|
||||
let frame = {
|
||||
|
@ -993,7 +993,7 @@ mod test {
|
|||
dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
|
||||
protocol: IpProtocol::Unknown(0x0c),
|
||||
payload_len: 0,
|
||||
ttl: 0x40
|
||||
hop_limit: 0x40
|
||||
});
|
||||
|
||||
// emit the above repr to a frame
|
||||
|
@ -1015,7 +1015,7 @@ mod test {
|
|||
dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
|
||||
protocol: IpProtocol::Unknown(12),
|
||||
payload_len: 0,
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
},
|
||||
data: &NO_BYTES
|
||||
};
|
||||
|
@ -1026,7 +1026,7 @@ mod test {
|
|||
dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x02]),
|
||||
protocol: IpProtocol::Icmp,
|
||||
payload_len: icmp_repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
},
|
||||
icmp_repr
|
||||
));
|
||||
|
@ -1062,7 +1062,7 @@ mod test {
|
|||
dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
|
||||
protocol: IpProtocol::Udp,
|
||||
payload_len: udp_repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
});
|
||||
|
||||
// Emit the representations to a packet
|
||||
|
@ -1080,7 +1080,7 @@ mod test {
|
|||
dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
|
||||
protocol: IpProtocol::Udp,
|
||||
payload_len: udp_repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
},
|
||||
data: &data
|
||||
};
|
||||
|
@ -1090,7 +1090,7 @@ mod test {
|
|||
dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x02]),
|
||||
protocol: IpProtocol::Icmp,
|
||||
payload_len: icmp_repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
},
|
||||
icmp_repr
|
||||
));
|
||||
|
@ -1105,7 +1105,7 @@ mod test {
|
|||
dst_addr: Ipv4Address::BROADCAST,
|
||||
protocol: IpProtocol::Udp,
|
||||
payload_len: udp_repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
});
|
||||
|
||||
// Emit the representations to a packet
|
||||
|
@ -1153,7 +1153,7 @@ mod test {
|
|||
dst_addr: Ipv4Address::BROADCAST,
|
||||
protocol: IpProtocol::Udp,
|
||||
payload_len: udp_repr.buffer_len(),
|
||||
ttl: 0x40
|
||||
hop_limit: 0x40
|
||||
});
|
||||
|
||||
{
|
||||
|
@ -1298,7 +1298,7 @@ mod test {
|
|||
dst_addr: Ipv4Address::new(0x7f, 0x00, 0x00, 0x01),
|
||||
protocol: IpProtocol::Icmp,
|
||||
payload_len: 24,
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
};
|
||||
let ip_repr = IpRepr::Ipv4(ipv4_repr);
|
||||
|
||||
|
|
|
@ -98,7 +98,7 @@ pub struct IcmpSocket<'a, 'b: 'a> {
|
|||
/// The endpoint this socket is communicating with
|
||||
endpoint: Endpoint,
|
||||
/// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||
ttl: Option<u8>
|
||||
hop_limit: Option<u8>
|
||||
}
|
||||
|
||||
impl<'a, 'b> IcmpSocket<'a, 'b> {
|
||||
|
@ -110,7 +110,7 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
|
|||
rx_buffer: rx_buffer,
|
||||
tx_buffer: tx_buffer,
|
||||
endpoint: Endpoint::default(),
|
||||
ttl: None
|
||||
hop_limit: None
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -122,29 +122,29 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
|
|||
|
||||
/// Return the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||
///
|
||||
/// See also the [set_ttl](#method.set_ttl) method
|
||||
pub fn ttl(&self) -> Option<u8> {
|
||||
self.ttl
|
||||
/// See also the [set_hop_limit](#method.set_hop_limit) method
|
||||
pub fn hop_limit(&self) -> Option<u8> {
|
||||
self.hop_limit
|
||||
}
|
||||
|
||||
/// Set the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||
///
|
||||
/// A socket without an explicitly set TTL value uses the default [IANA recommended]
|
||||
/// A socket without an explicitly set hop limit value uses the default [IANA recommended]
|
||||
/// value (64).
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if a TTL value of 0 is given. See [RFC 1122 § 3.2.1.7].
|
||||
/// This function panics if a hop limit value of 0 is given. See [RFC 1122 § 3.2.1.7].
|
||||
///
|
||||
/// [IANA recommended]: https://www.iana.org/assignments/ip-parameters/ip-parameters.xhtml
|
||||
/// [RFC 1122 § 3.2.1.7]: https://tools.ietf.org/html/rfc1122#section-3.2.1.7
|
||||
pub fn set_ttl(&mut self, ttl: Option<u8>) {
|
||||
// A host MUST NOT send a datagram with a Time-to-Live (TTL) value of 0
|
||||
if let Some(0) = ttl {
|
||||
pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) {
|
||||
// A host MUST NOT send a datagram with a hop limit value of 0
|
||||
if let Some(0) = hop_limit {
|
||||
panic!("the time-to-live value of a packet must not be zero")
|
||||
}
|
||||
|
||||
self.ttl = ttl
|
||||
self.hop_limit = hop_limit
|
||||
}
|
||||
|
||||
/// Bind the socket to the given endpoint.
|
||||
|
@ -328,7 +328,7 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
|
|||
where F: FnOnce((IpRepr, Icmpv4Repr)) -> Result<()>
|
||||
{
|
||||
let handle = self.meta.handle;
|
||||
let ttl = self.ttl.unwrap_or(64);
|
||||
let hop_limit = self.hop_limit.unwrap_or(64);
|
||||
let checksum = &caps.checksum;
|
||||
self.tx_buffer.dequeue_one_with(|packet_buf| {
|
||||
net_trace!("{}:{}: sending {} octets",
|
||||
|
@ -342,7 +342,7 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
|
|||
dst_addr: ipv4_addr,
|
||||
protocol: IpProtocol::Icmp,
|
||||
payload_len: repr.buffer_len(),
|
||||
ttl: ttl,
|
||||
hop_limit: hop_limit,
|
||||
});
|
||||
emit((ip_repr, repr))
|
||||
},
|
||||
|
@ -406,7 +406,7 @@ mod test {
|
|||
dst_addr: REMOTE_IPV4,
|
||||
protocol: IpProtocol::Icmp,
|
||||
payload_len: 24,
|
||||
ttl: 0x40
|
||||
hop_limit: 0x40
|
||||
});
|
||||
|
||||
static REMOTE_IP_REPR: IpRepr = IpRepr::Ipv4(Ipv4Repr {
|
||||
|
@ -414,7 +414,7 @@ mod test {
|
|||
dst_addr: LOCAL_IPV4,
|
||||
protocol: IpProtocol::Icmp,
|
||||
payload_len: 24,
|
||||
ttl: 0x40
|
||||
hop_limit: 0x40
|
||||
});
|
||||
|
||||
#[test]
|
||||
|
@ -463,7 +463,7 @@ mod test {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_ttl() {
|
||||
fn test_set_hop_limit() {
|
||||
let mut s = socket(buffer(0), buffer(1));
|
||||
let caps = DeviceCapabilities::default();
|
||||
|
||||
|
@ -471,7 +471,7 @@ mod test {
|
|||
let mut packet = Icmpv4Packet::new(&mut bytes);
|
||||
ECHO_REPR.emit(&mut packet, &caps.checksum);
|
||||
|
||||
s.set_ttl(Some(0x2a));
|
||||
s.set_hop_limit(Some(0x2a));
|
||||
|
||||
assert_eq!(s.send_slice(&packet.into_inner()[..], REMOTE_IP), Ok(()));
|
||||
assert_eq!(s.dispatch(&caps, |(ip_repr, _)| {
|
||||
|
@ -480,7 +480,7 @@ mod test {
|
|||
dst_addr: REMOTE_IPV4,
|
||||
protocol: IpProtocol::Icmp,
|
||||
payload_len: ECHO_REPR.buffer_len(),
|
||||
ttl: 0x2a,
|
||||
hop_limit: 0x2a,
|
||||
}));
|
||||
Ok(())
|
||||
}), Ok(()));
|
||||
|
@ -554,7 +554,7 @@ mod test {
|
|||
dst_addr: REMOTE_IPV4,
|
||||
protocol: IpProtocol::Icmp,
|
||||
payload_len: 12,
|
||||
ttl: 0x40
|
||||
hop_limit: 0x40
|
||||
},
|
||||
data: data
|
||||
};
|
||||
|
@ -563,7 +563,7 @@ mod test {
|
|||
dst_addr: LOCAL_IP,
|
||||
protocol: IpProtocol::Icmp,
|
||||
payload_len: icmp_repr.buffer_len(),
|
||||
ttl: 0x40
|
||||
hop_limit: 0x40
|
||||
};
|
||||
|
||||
assert!(!socket.can_recv());
|
||||
|
|
|
@ -268,7 +268,7 @@ mod test {
|
|||
dst_addr: Ipv4Address([10, 0, 0, 2]),
|
||||
protocol: IpProtocol::Unknown(IP_PROTO),
|
||||
payload_len: 4,
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
});
|
||||
const PACKET_BYTES: [u8; 24] = [
|
||||
0x45, 0x00, 0x00, 0x18,
|
||||
|
|
|
@ -187,7 +187,7 @@ pub struct TcpSocket<'a> {
|
|||
/// Interval at which keep-alive packets will be sent.
|
||||
keep_alive: Option<u64>,
|
||||
/// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||
ttl: Option<u8>,
|
||||
hop_limit: Option<u8>,
|
||||
/// Address passed to listen(). Listen address is set when listen() is called and
|
||||
/// used every time the socket is reset back to the LISTEN state.
|
||||
listen_address: IpAddress,
|
||||
|
@ -245,7 +245,7 @@ impl<'a> TcpSocket<'a> {
|
|||
rx_buffer: rx_buffer,
|
||||
timeout: None,
|
||||
keep_alive: None,
|
||||
ttl: None,
|
||||
hop_limit: None,
|
||||
listen_address: IpAddress::default(),
|
||||
local_endpoint: IpEndpoint::default(),
|
||||
remote_endpoint: IpEndpoint::default(),
|
||||
|
@ -318,29 +318,29 @@ impl<'a> TcpSocket<'a> {
|
|||
|
||||
/// Return the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||
///
|
||||
/// See also the [set_ttl](#method.set_ttl) method
|
||||
pub fn ttl(&self) -> Option<u8> {
|
||||
self.ttl
|
||||
/// See also the [set_hop_limit](#method.set_hop_limit) method
|
||||
pub fn hop_limit(&self) -> Option<u8> {
|
||||
self.hop_limit
|
||||
}
|
||||
|
||||
/// Set the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||
///
|
||||
/// A socket without an explicitly set TTL value uses the default [IANA recommended]
|
||||
/// A socket without an explicitly set hop limit value uses the default [IANA recommended]
|
||||
/// value (64).
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if a TTL value of 0 is given. See [RFC 1122 § 3.2.1.7].
|
||||
/// This function panics if a hop limit value of 0 is given. See [RFC 1122 § 3.2.1.7].
|
||||
///
|
||||
/// [IANA recommended]: https://www.iana.org/assignments/ip-parameters/ip-parameters.xhtml
|
||||
/// [RFC 1122 § 3.2.1.7]: https://tools.ietf.org/html/rfc1122#section-3.2.1.7
|
||||
pub fn set_ttl(&mut self, ttl: Option<u8>) {
|
||||
// A host MUST NOT send a datagram with a Time-to-Live (TTL) value of 0
|
||||
if let Some(0) = ttl {
|
||||
pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) {
|
||||
// A host MUST NOT send a datagram with a hop limit value of 0
|
||||
if let Some(0) = hop_limit {
|
||||
panic!("the time-to-live value of a packet must not be zero")
|
||||
}
|
||||
|
||||
self.ttl = ttl
|
||||
self.hop_limit = hop_limit
|
||||
}
|
||||
|
||||
/// Return the local endpoint.
|
||||
|
@ -369,7 +369,7 @@ impl<'a> TcpSocket<'a> {
|
|||
self.rx_buffer.clear();
|
||||
self.keep_alive = None;
|
||||
self.timeout = None;
|
||||
self.ttl = None;
|
||||
self.hop_limit = None;
|
||||
self.listen_address = IpAddress::default();
|
||||
self.local_endpoint = IpEndpoint::default();
|
||||
self.remote_endpoint = IpEndpoint::default();
|
||||
|
@ -759,7 +759,7 @@ impl<'a> TcpSocket<'a> {
|
|||
dst_addr: ip_repr.src_addr(),
|
||||
protocol: IpProtocol::Tcp,
|
||||
payload_len: reply_repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
};
|
||||
(ip_reply_repr, reply_repr)
|
||||
}
|
||||
|
@ -1286,7 +1286,7 @@ impl<'a> TcpSocket<'a> {
|
|||
src_addr: self.local_endpoint.addr,
|
||||
dst_addr: self.remote_endpoint.addr,
|
||||
protocol: IpProtocol::Tcp,
|
||||
ttl: self.ttl.unwrap_or(64),
|
||||
hop_limit: self.hop_limit.unwrap_or(64),
|
||||
payload_len: 0
|
||||
}.lower(&[])?;
|
||||
|
||||
|
@ -1519,7 +1519,7 @@ mod test {
|
|||
const SEND_IP_TEMPL: IpRepr = IpRepr::Unspecified {
|
||||
src_addr: LOCAL_IP, dst_addr: REMOTE_IP,
|
||||
protocol: IpProtocol::Tcp, payload_len: 20,
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
};
|
||||
const SEND_TEMPL: TcpRepr<'static> = TcpRepr {
|
||||
src_port: REMOTE_PORT, dst_port: LOCAL_PORT,
|
||||
|
@ -1531,7 +1531,7 @@ mod test {
|
|||
const _RECV_IP_TEMPL: IpRepr = IpRepr::Unspecified {
|
||||
src_addr: REMOTE_IP, dst_addr: LOCAL_IP,
|
||||
protocol: IpProtocol::Tcp, payload_len: 20,
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
};
|
||||
const RECV_TEMPL: TcpRepr<'static> = TcpRepr {
|
||||
src_port: LOCAL_PORT, dst_port: REMOTE_PORT,
|
||||
|
@ -1548,7 +1548,7 @@ mod test {
|
|||
dst_addr: LOCAL_IP,
|
||||
protocol: IpProtocol::Tcp,
|
||||
payload_len: repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
};
|
||||
trace!("send: {}", repr);
|
||||
|
||||
|
@ -3469,19 +3469,19 @@ mod test {
|
|||
// =========================================================================================//
|
||||
|
||||
#[test]
|
||||
fn test_set_ttl() {
|
||||
fn test_set_hop_limit() {
|
||||
let mut s = socket_syn_received();
|
||||
let mut caps = DeviceCapabilities::default();
|
||||
caps.max_transmission_unit = 1520;
|
||||
|
||||
s.set_ttl(Some(0x2a));
|
||||
s.set_hop_limit(Some(0x2a));
|
||||
assert_eq!(s.dispatch(0, &caps, |(ip_repr, _)| {
|
||||
assert_eq!(ip_repr, IpRepr::Ipv4(Ipv4Repr {
|
||||
src_addr: Ipv4Address([10, 0, 0, 1]),
|
||||
dst_addr: Ipv4Address([10, 0, 0, 2]),
|
||||
protocol: IpProtocol::Tcp,
|
||||
payload_len: 24,
|
||||
ttl: 0x2a,
|
||||
hop_limit: 0x2a,
|
||||
}));
|
||||
Ok(())
|
||||
}), Ok(()));
|
||||
|
@ -3489,9 +3489,9 @@ mod test {
|
|||
|
||||
#[test]
|
||||
#[should_panic(expected = "the time-to-live value of a packet must not be zero")]
|
||||
fn test_set_ttl_zero() {
|
||||
fn test_set_hop_limit_zero() {
|
||||
let mut s = socket_syn_received();
|
||||
s.set_ttl(Some(0));
|
||||
s.set_hop_limit(Some(0));
|
||||
}
|
||||
|
||||
// =========================================================================================//
|
||||
|
@ -3633,7 +3633,7 @@ mod test {
|
|||
dst_addr: LOCAL_IP,
|
||||
protocol: IpProtocol::Tcp,
|
||||
payload_len: tcp_repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
};
|
||||
assert!(s.accepts(&ip_repr, &tcp_repr));
|
||||
|
||||
|
@ -3642,7 +3642,7 @@ mod test {
|
|||
dst_addr: LOCAL_IP,
|
||||
protocol: IpProtocol::Tcp,
|
||||
payload_len: tcp_repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
};
|
||||
assert!(!s.accepts(&ip_repr_wrong_src, &tcp_repr));
|
||||
|
||||
|
@ -3651,7 +3651,7 @@ mod test {
|
|||
dst_addr: OTHER_IP,
|
||||
protocol: IpProtocol::Tcp,
|
||||
payload_len: tcp_repr.buffer_len(),
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
};
|
||||
assert!(!s.accepts(&ip_repr_wrong_dst, &tcp_repr));
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ pub struct UdpSocket<'a, 'b: 'a> {
|
|||
rx_buffer: SocketBuffer<'a, 'b>,
|
||||
tx_buffer: SocketBuffer<'a, 'b>,
|
||||
/// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||
ttl: Option<u8>
|
||||
hop_limit: Option<u8>
|
||||
}
|
||||
|
||||
impl<'a, 'b> UdpSocket<'a, 'b> {
|
||||
|
@ -76,7 +76,7 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
|
|||
endpoint: IpEndpoint::default(),
|
||||
rx_buffer: rx_buffer,
|
||||
tx_buffer: tx_buffer,
|
||||
ttl: None
|
||||
hop_limit: None
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -94,29 +94,29 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
|
|||
|
||||
/// Return the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||
///
|
||||
/// See also the [set_ttl](#method.set_ttl) method
|
||||
pub fn ttl(&self) -> Option<u8> {
|
||||
self.ttl
|
||||
/// See also the [set_hop_limit](#method.set_hop_limit) method
|
||||
pub fn hop_limit(&self) -> Option<u8> {
|
||||
self.hop_limit
|
||||
}
|
||||
|
||||
/// Set the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||
///
|
||||
/// A socket without an explicitly set TTL value uses the default [IANA recommended]
|
||||
/// A socket without an explicitly set hop limit value uses the default [IANA recommended]
|
||||
/// value (64).
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if a TTL value of 0 is given. See [RFC 1122 § 3.2.1.7].
|
||||
/// This function panics if a hop limit value of 0 is given. See [RFC 1122 § 3.2.1.7].
|
||||
///
|
||||
/// [IANA recommended]: https://www.iana.org/assignments/ip-parameters/ip-parameters.xhtml
|
||||
/// [RFC 1122 § 3.2.1.7]: https://tools.ietf.org/html/rfc1122#section-3.2.1.7
|
||||
pub fn set_ttl(&mut self, ttl: Option<u8>) {
|
||||
// A host MUST NOT send a datagram with a Time-to-Live (TTL) value of 0
|
||||
if let Some(0) = ttl {
|
||||
pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) {
|
||||
// A host MUST NOT send a datagram with a hop limit value of 0
|
||||
if let Some(0) = hop_limit {
|
||||
panic!("the time-to-live value of a packet must not be zero")
|
||||
}
|
||||
|
||||
self.ttl = ttl
|
||||
self.hop_limit = hop_limit
|
||||
}
|
||||
|
||||
/// Bind the socket to the given endpoint.
|
||||
|
@ -225,7 +225,7 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
|
|||
where F: FnOnce((IpRepr, UdpRepr)) -> Result<()> {
|
||||
let handle = self.handle();
|
||||
let endpoint = self.endpoint;
|
||||
let ttl = self.ttl.unwrap_or(64);
|
||||
let hop_limit = self.hop_limit.unwrap_or(64);
|
||||
self.tx_buffer.dequeue_one_with(|packet_buf| {
|
||||
net_trace!("{}:{}:{}: sending {} octets",
|
||||
handle, endpoint,
|
||||
|
@ -241,7 +241,7 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
|
|||
dst_addr: packet_buf.endpoint.addr,
|
||||
protocol: IpProtocol::Udp,
|
||||
payload_len: repr.buffer_len(),
|
||||
ttl: ttl,
|
||||
hop_limit: hop_limit,
|
||||
};
|
||||
emit((ip_repr, repr))
|
||||
})
|
||||
|
@ -303,7 +303,7 @@ mod test {
|
|||
dst_addr: REMOTE_IP,
|
||||
protocol: IpProtocol::Udp,
|
||||
payload_len: 8 + 6,
|
||||
ttl: 64,
|
||||
hop_limit: 64,
|
||||
};
|
||||
const LOCAL_UDP_REPR: UdpRepr = UdpRepr {
|
||||
src_port: LOCAL_PORT,
|
||||
|
@ -366,7 +366,7 @@ mod test {
|
|||
dst_addr: Ipv4Address([10, 0, 0, 1]),
|
||||
protocol: IpProtocol::Udp,
|
||||
payload_len: 8 + 6,
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
});
|
||||
const REMOTE_UDP_REPR: UdpRepr = UdpRepr {
|
||||
src_port: REMOTE_PORT,
|
||||
|
@ -420,11 +420,11 @@ mod test {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_ttl() {
|
||||
fn test_set_hop_limit() {
|
||||
let mut s = socket(buffer(0), buffer(1));
|
||||
assert_eq!(s.bind(LOCAL_END), Ok(()));
|
||||
|
||||
s.set_ttl(Some(0x2a));
|
||||
s.set_hop_limit(Some(0x2a));
|
||||
assert_eq!(s.send_slice(b"abcdef", REMOTE_END), Ok(()));
|
||||
assert_eq!(s.dispatch(|(ip_repr, _)| {
|
||||
assert_eq!(ip_repr, IpRepr::Unspecified{
|
||||
|
@ -432,7 +432,7 @@ mod test {
|
|||
dst_addr: REMOTE_IP,
|
||||
protocol: IpProtocol::Udp,
|
||||
payload_len: 8 + 6,
|
||||
ttl: 0x2a,
|
||||
hop_limit: 0x2a,
|
||||
});
|
||||
Ok(())
|
||||
}), Ok(()));
|
||||
|
@ -440,9 +440,9 @@ mod test {
|
|||
|
||||
#[test]
|
||||
#[should_panic(expected = "the time-to-live value of a packet must not be zero")]
|
||||
fn test_set_ttl_zero() {
|
||||
fn test_set_hop_limit_zero() {
|
||||
let mut s = socket(buffer(0), buffer(1));
|
||||
s.set_ttl(Some(0));
|
||||
s.set_hop_limit(Some(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -463,7 +463,7 @@ mod test {
|
|||
dst_addr: Ipv4Address([10, 0, 0, 10]),
|
||||
protocol: IpProtocol::Udp,
|
||||
payload_len: 8 + 6,
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
});
|
||||
|
||||
let mut port_bound_socket = socket(buffer(1), buffer(0));
|
||||
|
|
|
@ -235,8 +235,26 @@ impl<T: AsRef<[u8]>> PrettyPrint for Frame<T> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
// Tests that are valid with any combination of
|
||||
// "proto-*" features.
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_broadcast() {
|
||||
assert!(Address::BROADCAST.is_broadcast());
|
||||
assert!(!Address::BROADCAST.is_unicast());
|
||||
assert!(Address::BROADCAST.is_multicast());
|
||||
assert!(Address::BROADCAST.is_local());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "proto-ipv4")]
|
||||
mod test_ipv4 {
|
||||
// Tests that are valid only with "proto-ipv4"
|
||||
use super::*;
|
||||
|
||||
static FRAME_BYTES: [u8; 64] =
|
||||
|
@ -279,12 +297,49 @@ mod test {
|
|||
frame.payload_mut().copy_from_slice(&PAYLOAD_BYTES[..]);
|
||||
assert_eq!(&frame.into_inner()[..], &FRAME_BYTES[..]);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "proto-ipv6")]
|
||||
mod test_ipv6 {
|
||||
// Tests that are valid only with "proto-ipv6"
|
||||
use super::*;
|
||||
|
||||
static FRAME_BYTES: [u8; 54] =
|
||||
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
|
||||
0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
|
||||
0x86, 0xdd,
|
||||
0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01];
|
||||
|
||||
static PAYLOAD_BYTES: [u8; 40] =
|
||||
[0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01];
|
||||
|
||||
#[test]
|
||||
fn test_broadcast() {
|
||||
assert!(Address::BROADCAST.is_broadcast());
|
||||
assert!(!Address::BROADCAST.is_unicast());
|
||||
assert!(Address::BROADCAST.is_multicast());
|
||||
assert!(Address::BROADCAST.is_local());
|
||||
fn test_deconstruct() {
|
||||
let frame = Frame::new(&FRAME_BYTES[..]);
|
||||
assert_eq!(frame.dst_addr(), Address([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]));
|
||||
assert_eq!(frame.src_addr(), Address([0x11, 0x12, 0x13, 0x14, 0x15, 0x16]));
|
||||
assert_eq!(frame.ethertype(), EtherType::Ipv6);
|
||||
assert_eq!(frame.payload(), &PAYLOAD_BYTES[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_construct() {
|
||||
let mut bytes = vec![0xa5; 54];
|
||||
let mut frame = Frame::new(&mut bytes);
|
||||
frame.set_dst_addr(Address([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]));
|
||||
frame.set_src_addr(Address([0x11, 0x12, 0x13, 0x14, 0x15, 0x16]));
|
||||
frame.set_ethertype(EtherType::Ipv6);
|
||||
assert_eq!(PAYLOAD_BYTES.len(), frame.payload_mut().len());
|
||||
frame.payload_mut().copy_from_slice(&PAYLOAD_BYTES[..]);
|
||||
assert_eq!(&frame.into_inner()[..], &FRAME_BYTES[..]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -423,7 +423,7 @@ impl<'a> Repr<'a> {
|
|||
dst_addr: ip_packet.dst_addr(),
|
||||
protocol: ip_packet.protocol(),
|
||||
payload_len: payload.len(),
|
||||
ttl: ip_packet.ttl()
|
||||
hop_limit: ip_packet.hop_limit()
|
||||
},
|
||||
data: payload
|
||||
})
|
||||
|
|
106
src/wire/ip.rs
106
src/wire/ip.rs
|
@ -336,7 +336,7 @@ pub enum Repr {
|
|||
dst_addr: Address,
|
||||
protocol: Protocol,
|
||||
payload_len: usize,
|
||||
ttl: u8
|
||||
hop_limit: u8
|
||||
},
|
||||
Ipv4(Ipv4Repr),
|
||||
#[cfg(feature = "proto-ipv6")]
|
||||
|
@ -429,12 +429,12 @@ impl Repr {
|
|||
}
|
||||
|
||||
/// Return the TTL value.
|
||||
pub fn ttl(&self) -> u8 {
|
||||
pub fn hop_limit(&self) -> u8 {
|
||||
match self {
|
||||
&Repr::Unspecified { ttl, .. } => ttl,
|
||||
&Repr::Ipv4(Ipv4Repr { ttl, .. }) => ttl,
|
||||
&Repr::Unspecified { hop_limit, .. } => hop_limit,
|
||||
&Repr::Ipv4(Ipv4Repr { hop_limit, .. }) => hop_limit,
|
||||
#[cfg(feature = "proto-ipv6")]
|
||||
&Repr::Ipv6(Ipv6Repr { hop_limit, ..}) => hop_limit,
|
||||
&Repr::Ipv6(Ipv6Repr { hop_limit, ..}) => hop_limit,
|
||||
&Repr::__Nonexhaustive => unreachable!()
|
||||
}
|
||||
}
|
||||
|
@ -469,13 +469,13 @@ impl Repr {
|
|||
&Repr::Unspecified {
|
||||
src_addr: Address::Ipv4(src_addr),
|
||||
dst_addr: Address::Ipv4(dst_addr),
|
||||
protocol, payload_len, ttl
|
||||
protocol, payload_len, hop_limit
|
||||
} => {
|
||||
Ok(Repr::Ipv4(Ipv4Repr {
|
||||
src_addr: src_addr,
|
||||
dst_addr: dst_addr,
|
||||
protocol: protocol,
|
||||
payload_len: payload_len, ttl
|
||||
payload_len: payload_len, hop_limit
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -483,21 +483,21 @@ impl Repr {
|
|||
&Repr::Unspecified {
|
||||
src_addr: Address::Ipv6(src_addr),
|
||||
dst_addr: Address::Ipv6(dst_addr),
|
||||
protocol, payload_len, ttl
|
||||
protocol, payload_len, hop_limit
|
||||
} => {
|
||||
Ok(Repr::Ipv6(Ipv6Repr {
|
||||
src_addr: src_addr,
|
||||
dst_addr: dst_addr,
|
||||
next_header: protocol,
|
||||
payload_len: payload_len,
|
||||
hop_limit: ttl
|
||||
hop_limit: hop_limit
|
||||
}))
|
||||
}
|
||||
|
||||
&Repr::Unspecified {
|
||||
src_addr: Address::Unspecified,
|
||||
dst_addr: Address::Ipv4(dst_addr),
|
||||
protocol, payload_len, ttl
|
||||
protocol, payload_len, hop_limit
|
||||
} => {
|
||||
let mut src_addr = None;
|
||||
for cidr in fallback_src_addrs {
|
||||
|
@ -511,7 +511,7 @@ impl Repr {
|
|||
}
|
||||
Ok(Repr::Ipv4(Ipv4Repr {
|
||||
src_addr: src_addr.ok_or(Error::Unaddressable)?,
|
||||
dst_addr, protocol, payload_len, ttl
|
||||
dst_addr, protocol, payload_len, hop_limit
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -519,7 +519,7 @@ impl Repr {
|
|||
&Repr::Unspecified {
|
||||
src_addr: Address::Unspecified,
|
||||
dst_addr: Address::Ipv6(dst_addr),
|
||||
protocol, payload_len, ttl
|
||||
protocol, payload_len, hop_limit
|
||||
} => {
|
||||
// Find a fallback address to use
|
||||
match fallback_src_addrs.iter().filter_map(|cidr| match cidr.address() {
|
||||
|
@ -530,7 +530,7 @@ impl Repr {
|
|||
Ok(Repr::Ipv6(Ipv6Repr {
|
||||
src_addr: addr,
|
||||
next_header: protocol,
|
||||
hop_limit: ttl,
|
||||
hop_limit: hop_limit,
|
||||
dst_addr, payload_len
|
||||
})),
|
||||
None => Err(Error::Unaddressable)
|
||||
|
@ -745,27 +745,27 @@ mod test {
|
|||
|
||||
assert_eq!(
|
||||
Repr::Unspecified{
|
||||
src_addr: IpAddress::Ipv4(ip_addr_a),
|
||||
dst_addr: IpAddress::Ipv4(ip_addr_b),
|
||||
protocol: proto,
|
||||
ttl: 0x2a,
|
||||
src_addr: IpAddress::Ipv4(ip_addr_a),
|
||||
dst_addr: IpAddress::Ipv4(ip_addr_b),
|
||||
protocol: proto,
|
||||
hop_limit: 0x2a,
|
||||
payload_len,
|
||||
}.lower(&[]),
|
||||
Ok(Repr::Ipv4(Ipv4Repr{
|
||||
src_addr: ip_addr_a,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
ttl: 0x2a,
|
||||
src_addr: ip_addr_a,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
hop_limit: 0x2a,
|
||||
payload_len
|
||||
}))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Repr::Unspecified{
|
||||
src_addr: IpAddress::Unspecified,
|
||||
dst_addr: IpAddress::Ipv4(ip_addr_b),
|
||||
protocol: proto,
|
||||
ttl: 64,
|
||||
src_addr: IpAddress::Unspecified,
|
||||
dst_addr: IpAddress::Ipv4(ip_addr_b),
|
||||
protocol: proto,
|
||||
hop_limit: 64,
|
||||
payload_len
|
||||
}.lower(&[]),
|
||||
Err(Error::Unaddressable)
|
||||
|
@ -773,44 +773,44 @@ mod test {
|
|||
|
||||
assert_eq!(
|
||||
Repr::Unspecified{
|
||||
src_addr: IpAddress::Unspecified,
|
||||
dst_addr: IpAddress::Ipv4(ip_addr_b),
|
||||
protocol: proto,
|
||||
ttl: 64,
|
||||
src_addr: IpAddress::Unspecified,
|
||||
dst_addr: IpAddress::Ipv4(ip_addr_b),
|
||||
protocol: proto,
|
||||
hop_limit: 64,
|
||||
payload_len
|
||||
}.lower(&[IpCidr::new(IpAddress::Ipv4(ip_addr_a), 24)]),
|
||||
Ok(Repr::Ipv4(Ipv4Repr{
|
||||
src_addr: ip_addr_a,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
ttl: 64,
|
||||
src_addr: ip_addr_a,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
hop_limit: 64,
|
||||
payload_len
|
||||
}))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Repr::Ipv4(Ipv4Repr{
|
||||
src_addr: ip_addr_a,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
ttl: 255,
|
||||
src_addr: ip_addr_a,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
hop_limit: 255,
|
||||
payload_len
|
||||
}).lower(&[]),
|
||||
Ok(Repr::Ipv4(Ipv4Repr{
|
||||
src_addr: ip_addr_a,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
ttl: 255,
|
||||
src_addr: ip_addr_a,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
hop_limit: 255,
|
||||
payload_len
|
||||
}))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Repr::Ipv4(Ipv4Repr{
|
||||
src_addr: Ipv4Address::UNSPECIFIED,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
ttl: 255,
|
||||
src_addr: Ipv4Address::UNSPECIFIED,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
hop_limit: 255,
|
||||
payload_len
|
||||
}).lower(&[]),
|
||||
Err(Error::Unaddressable)
|
||||
|
@ -818,17 +818,17 @@ mod test {
|
|||
|
||||
assert_eq!(
|
||||
Repr::Ipv4(Ipv4Repr{
|
||||
src_addr: Ipv4Address::UNSPECIFIED,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
ttl: 64,
|
||||
src_addr: Ipv4Address::UNSPECIFIED,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
hop_limit: 64,
|
||||
payload_len
|
||||
}).lower(&[IpCidr::new(IpAddress::Ipv4(ip_addr_a), 24)]),
|
||||
Ok(Repr::Ipv4(Ipv4Repr{
|
||||
src_addr: ip_addr_a,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
ttl: 64,
|
||||
src_addr: ip_addr_a,
|
||||
dst_addr: ip_addr_b,
|
||||
protocol: proto,
|
||||
hop_limit: 64,
|
||||
payload_len
|
||||
}))
|
||||
);
|
||||
|
|
|
@ -257,7 +257,7 @@ impl<T: AsRef<[u8]>> Packet<T> {
|
|||
|
||||
/// Return the time to live field.
|
||||
#[inline]
|
||||
pub fn ttl(&self) -> u8 {
|
||||
pub fn hop_limit(&self) -> u8 {
|
||||
let data = self.buffer.as_ref();
|
||||
data[field::TTL]
|
||||
}
|
||||
|
@ -391,7 +391,7 @@ impl<T: AsRef<[u8]> + AsMut<[u8]>> Packet<T> {
|
|||
|
||||
/// Set the time to live field.
|
||||
#[inline]
|
||||
pub fn set_ttl(&mut self, value: u8) {
|
||||
pub fn set_hop_limit(&mut self, value: u8) {
|
||||
let data = self.buffer.as_mut();
|
||||
data[field::TTL] = value
|
||||
}
|
||||
|
@ -452,7 +452,7 @@ pub struct Repr {
|
|||
pub dst_addr: Address,
|
||||
pub protocol: Protocol,
|
||||
pub payload_len: usize,
|
||||
pub ttl: u8
|
||||
pub hop_limit: u8
|
||||
}
|
||||
|
||||
impl Repr {
|
||||
|
@ -479,7 +479,7 @@ impl Repr {
|
|||
dst_addr: packet.dst_addr(),
|
||||
protocol: packet.protocol(),
|
||||
payload_len: payload_len,
|
||||
ttl: packet.ttl()
|
||||
hop_limit: packet.hop_limit()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -502,7 +502,7 @@ impl Repr {
|
|||
packet.set_more_frags(false);
|
||||
packet.set_dont_frag(true);
|
||||
packet.set_frag_offset(0);
|
||||
packet.set_ttl(self.ttl);
|
||||
packet.set_hop_limit(self.hop_limit);
|
||||
packet.set_protocol(self.protocol);
|
||||
packet.set_src_addr(self.src_addr);
|
||||
packet.set_dst_addr(self.dst_addr);
|
||||
|
@ -522,8 +522,8 @@ impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> {
|
|||
Ok(repr) => write!(f, "{}", repr),
|
||||
Err(err) => {
|
||||
write!(f, "IPv4 ({})", err)?;
|
||||
write!(f, " src={} dst={} proto={} ttl={}",
|
||||
self.src_addr(), self.dst_addr(), self.protocol(), self.ttl())?;
|
||||
write!(f, " src={} dst={} proto={} hop_limit={}",
|
||||
self.src_addr(), self.dst_addr(), self.protocol(), self.hop_limit())?;
|
||||
if self.version() != 4 {
|
||||
write!(f, " ver={}", self.version())?;
|
||||
}
|
||||
|
@ -620,7 +620,7 @@ mod test {
|
|||
assert_eq!(packet.more_frags(), true);
|
||||
assert_eq!(packet.dont_frag(), true);
|
||||
assert_eq!(packet.frag_offset(), 0x203 * 8);
|
||||
assert_eq!(packet.ttl(), 0x1a);
|
||||
assert_eq!(packet.hop_limit(), 0x1a);
|
||||
assert_eq!(packet.protocol(), Protocol::Icmp);
|
||||
assert_eq!(packet.checksum(), 0xd56e);
|
||||
assert_eq!(packet.src_addr(), Address([0x11, 0x12, 0x13, 0x14]));
|
||||
|
@ -643,7 +643,7 @@ mod test {
|
|||
packet.set_more_frags(true);
|
||||
packet.set_dont_frag(true);
|
||||
packet.set_frag_offset(0x203 * 8);
|
||||
packet.set_ttl(0x1a);
|
||||
packet.set_hop_limit(0x1a);
|
||||
packet.set_protocol(Protocol::Icmp);
|
||||
packet.set_src_addr(Address([0x11, 0x12, 0x13, 0x14]));
|
||||
packet.set_dst_addr(Address([0x21, 0x22, 0x23, 0x24]));
|
||||
|
@ -691,7 +691,7 @@ mod test {
|
|||
dst_addr: Address([0x21, 0x22, 0x23, 0x24]),
|
||||
protocol: Protocol::Icmp,
|
||||
payload_len: 4,
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -572,6 +572,8 @@ impl fmt::Display for Repr {
|
|||
|
||||
use super::pretty_print::{PrettyPrint, PrettyIndent};
|
||||
|
||||
// TODO: This is very similar to the implementation for IPv4. Make
|
||||
// a way to have less copy and pasted code here.
|
||||
impl<T: AsRef<[u8]>> PrettyPrint for Packet<T> {
|
||||
fn pretty_print(buffer: &AsRef<[u8]>, f: &mut fmt::Formatter,
|
||||
indent: &mut PrettyIndent) -> fmt::Result {
|
||||
|
|
|
@ -53,7 +53,7 @@ let repr = Ipv4Repr {
|
|||
dst_addr: Ipv4Address::new(10, 0, 0, 2),
|
||||
protocol: IpProtocol::Tcp,
|
||||
payload_len: 10,
|
||||
ttl: 64
|
||||
hop_limit: 64
|
||||
};
|
||||
let mut buffer = vec![0; repr.buffer_len() + repr.payload_len];
|
||||
{ // emission
|
||||
|
|
Loading…
Reference in New Issue