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: 2c0c1ea76a4f
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: dc9ac44ba754
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Dec 27, 2016

  1. Copy the full SHA
    52259d3 View commit details
  2. Copy the full SHA
    dc9ac44 View commit details
Showing with 38 additions and 3 deletions.
  1. +8 −0 examples/smoltcpserver.rs
  2. +23 −1 src/socket/tcp.rs
  3. +7 −2 src/wire/ip.rs
8 changes: 8 additions & 0 deletions examples/smoltcpserver.rs
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@ fn main() {
let mut iface = EthernetInterface::new(device, arp_cache,
hardware_addr, protocol_addrs, sockets);

let mut tcp_6969_connected = false;
loop {
match iface.poll() {
Ok(()) => (),
@@ -97,6 +98,13 @@ fn main() {

{
let socket: &mut TcpSocket = iface.sockets()[1].as_socket();
if socket.is_connected() && !tcp_6969_connected {
debug!("tcp connected");
} else if !socket.is_connected() && tcp_6969_connected {
debug!("tcp disconnected");
}
tcp_6969_connected = socket.is_connected();

if socket.can_recv() {
let data = {
let mut data = socket.recv(128).unwrap().to_owned();
24 changes: 23 additions & 1 deletion src/socket/tcp.rs
Original file line number Diff line number Diff line change
@@ -254,7 +254,7 @@ impl<'a> TcpSocket<'a> {
Ok(())
}

/// Return whether the connection is open.
/// Return whether the socket is open.
///
/// This function returns true if the socket will process incoming or dispatch outgoing
/// packets. Note that this does not mean that it is possible to send or receive data through
@@ -267,6 +267,24 @@ impl<'a> TcpSocket<'a> {
}
}

/// Return whether a connection is established.
///
/// This function returns true if the socket is actively exchanging packets with
/// a remote endpoint. Note that this does not mean that it is possible to send or receive
/// data through the socket; for that, use [can_send](#method.can_send) or
/// [can_recv](#method.can_recv).
///
/// If a connection is established, [abort](#method.close) will send a reset to
/// the remote endpoint.
pub fn is_connected(&self) -> bool {
match self.state {
State::Closed => false,
State::TimeWait => false,
State::Listen => false,
_ => true
}
}

/// Return whether the transmit half of the full-duplex connection is open.
///
/// This function returns true if it's possible to send data and have it arrive
@@ -491,6 +509,8 @@ impl<'a> TcpSocket<'a> {

// RSTs in SYN_RECEIVED flip the socket back to the LISTEN state.
(State::SynReceived, TcpRepr { control: TcpControl::Rst, .. }) => {
net_trace!("tcp:{}:{}: received RST",
self.local_endpoint, self.remote_endpoint);
self.local_endpoint.addr = self.listen_address;
self.remote_endpoint = IpEndpoint::default();
self.set_state(State::Listen);
@@ -499,6 +519,8 @@ impl<'a> TcpSocket<'a> {

// RSTs in any other state close the socket.
(_, TcpRepr { control: TcpControl::Rst, .. }) => {
net_trace!("tcp:{}:{}: received RST",
self.local_endpoint, self.remote_endpoint);
self.local_endpoint = IpEndpoint::default();
self.remote_endpoint = IpEndpoint::default();
self.set_state(State::Closed);
9 changes: 7 additions & 2 deletions src/wire/ip.rs
Original file line number Diff line number Diff line change
@@ -229,6 +229,11 @@ pub mod checksum {

use super::*;

fn propagate_carries(word: u32) -> u16 {
let sum = (word >> 16) + (word & 0xffff);
((sum >> 16) as u16) + (sum as u16)
}

/// Compute an RFC 1071 compliant checksum (without the final complement).
pub fn data(data: &[u8]) -> u16 {
let mut accum: u32 = 0;
@@ -241,7 +246,7 @@ pub mod checksum {
}
accum += word;
}
(((accum >> 16) as u16) + (accum as u16))
propagate_carries(accum)
}

/// Combine several RFC 1071 compliant checksums.
@@ -250,7 +255,7 @@ pub mod checksum {
for &word in checksums {
accum += word as u32;
}
(((accum >> 16) as u16) + (accum as u16))
propagate_carries(accum)
}

/// Compute an IP pseudo header checksum.