Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: smoltcp-rs/smoltcp
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: fea462837d9c
Choose a base ref
...
head repository: smoltcp-rs/smoltcp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 581e7b3f6fc1
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Oct 24, 2017

  1. Copy the full SHA
    284db00 View commit details
  2. Simplify. NFC.

    whitequark committed Oct 24, 2017
    Copy the full SHA
    581e7b3 View commit details
Showing with 86 additions and 74 deletions.
  1. +1 −0 README.md
  2. +15 −22 src/iface/ethernet.rs
  3. +37 −26 src/socket/tcp.rs
  4. +33 −26 src/socket/udp.rs
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@ The only supported medium is Ethernet.
The only supported internetworking protocol is IPv4.

* IPv4 header checksum is generated and validated.
* IPv4 time-to-live value is configurable per socket, set to 64 by default.
* IPv4 fragmentation is **not** supported.
* IPv4 options are **not** supported and are silently ignored.
* IPv4 routes or default gateways are **not** supported.
37 changes: 15 additions & 22 deletions src/iface/ethernet.rs
Original file line number Diff line number Diff line change
@@ -171,33 +171,26 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
};

let response =
match self.process_ethernet(sockets, timestamp, &frame) {
Ok(response) => response,
Err(err) => {
net_debug!("cannot process ingress packet: {}", err);

if net_log_enabled!(debug) {
match EthernetFrame::new_checked(frame.as_ref()) {
Err(_) => {
net_debug!("packet dump follows:\n{:?}", frame.as_ref());
}
Ok(frame) => {
net_debug!("packet dump follows:\n{}", frame);
}
self.process_ethernet(sockets, timestamp, &frame).map_err(|err| {
net_debug!("cannot process ingress packet: {}", err);
if net_log_enabled!(debug) {
match EthernetFrame::new_checked(frame.as_ref()) {
Err(_) => {
net_debug!("packet dump follows:\n{:?}", frame.as_ref());
}
Ok(frame) => {
net_debug!("packet dump follows:\n{}", frame);
}
}
return Err(err)
}
};
err
})?;
processed_any = true;

match self.dispatch(timestamp, response) {
Ok(()) => (),
Err(err) => {
net_debug!("cannot dispatch response packet: {}", err);
return Err(err)
}
}
self.dispatch(timestamp, response).map_err(|err| {
net_debug!("cannot dispatch response packet: {}", err);
err
})?;
}
Ok(processed_any)
}
63 changes: 37 additions & 26 deletions src/socket/tcp.rs
Original file line number Diff line number Diff line change
@@ -324,21 +324,21 @@ impl<'a> TcpSocket<'a> {
/// 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]
/// value (`64`).
/// 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 TTL 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
match ttl {
Some(0) => { panic!("A TTL value of 0 is invalid for a sent packet"); },
catchall => self.ttl = catchall,
// A host MUST NOT send a datagram with a Time-to-Live (TTL) value of 0
if let Some(0) = ttl {
panic!("the time-to-live value of a packet must not be zero")
}

self.ttl = ttl
}

/// Return the local endpoint.
@@ -3410,6 +3410,36 @@ mod test {
}));
}

// =========================================================================================//
// Tests for time-to-live configuration.
// =========================================================================================//

#[test]
fn test_set_ttl() {
let mut s = socket_syn_received();
let mut caps = DeviceCapabilities::default();
caps.max_transmission_unit = 1520;

s.set_ttl(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,
}));
Ok(())
}), Ok(()));
}

#[test]
#[should_panic(expected = "the time-to-live value of a packet must not be zero")]
fn test_set_ttl_zero() {
let mut s = socket_syn_received();
s.set_ttl(Some(0));
}

// =========================================================================================//
// Tests for reassembly.
// =========================================================================================//
@@ -3530,23 +3560,4 @@ mod test {
};
assert!(!s.accepts(&ip_repr_wrong_dst, &tcp_repr));
}

#[test]
fn test_set_ttl() {
let mut s = socket_syn_received();
let mut caps = DeviceCapabilities::default();
caps.max_transmission_unit = 1520;

s.set_ttl(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,
}));
Ok(())
}), Ok(()));
}
}
59 changes: 33 additions & 26 deletions src/socket/udp.rs
Original file line number Diff line number Diff line change
@@ -107,21 +107,21 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
/// 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]
/// value (`64`).
/// 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 TTL 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
match ttl {
Some(0) => { panic!("A TTL value of 0 is invalid for a sent packet"); },
catchall => self.ttl = catchall,
// A host MUST NOT send a datagram with a Time-to-Live (TTL) value of 0
if let Some(0) = ttl {
panic!("the time-to-live value of a packet must not be zero")
}

self.ttl = ttl
}

/// Bind the socket to the given endpoint.
@@ -424,6 +424,32 @@ mod test {
Err(Error::Truncated));
}

#[test]
fn test_set_ttl() {
let mut s = socket(buffer(0), buffer(1));
assert_eq!(s.bind(LOCAL_END), Ok(()));

s.set_ttl(Some(0x2a));
assert_eq!(s.send_slice(b"abcdef", REMOTE_END), Ok(()));
assert_eq!(s.dispatch(|(ip_repr, _)| {
assert_eq!(ip_repr, IpRepr::Unspecified{
src_addr: LOCAL_IP,
dst_addr: REMOTE_IP,
protocol: IpProtocol::Udp,
payload_len: 8 + 6,
ttl: 0x2a,
});
Ok(())
}), Ok(()));
}

#[test]
#[should_panic(expected = "the time-to-live value of a packet must not be zero")]
fn test_set_ttl_zero() {
let mut s = socket(buffer(0), buffer(1));
s.set_ttl(Some(0));
}

#[test]
fn test_doesnt_accept_wrong_port() {
let mut socket = socket(buffer(1), buffer(0));
@@ -453,23 +479,4 @@ mod test {
assert_eq!(ip_bound_socket.bind(LOCAL_END), Ok(()));
assert!(!ip_bound_socket.accepts(&ip_repr, &REMOTE_UDP_REPR));
}

#[test]
fn test_set_ttl() {
let mut s = socket(buffer(0), buffer(1));
assert_eq!(s.bind(LOCAL_END), Ok(()));

s.set_ttl(Some(0x2a));
assert_eq!(s.send_slice(b"abcdef", REMOTE_END), Ok(()));
assert_eq!(s.dispatch(|(ip_repr, _)| {
assert_eq!(ip_repr, IpRepr::Unspecified{
src_addr: LOCAL_IP,
dst_addr: REMOTE_IP,
protocol: IpProtocol::Udp,
payload_len: 8 + 6,
ttl: 0x2a,
});
Ok(())
}), Ok(()));
}
}