Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebased #12 - Updated to Rust 2018, ran Rustfmt, and fixed Clippy lints #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "tokio-ping"
version = "0.3.0"
version = "0.3.1"
edition = "2018"
license = "MIT/Apache-2.0"
authors = ["Fedor Gogolev <[email protected]>"]
documentation = "https://docs.rs/tokio-ping"
Expand All @@ -10,16 +11,18 @@ keywords = ["tokio", "icmp", "ping"]
categories = ["network-programming", "asynchronous"]

[dependencies]
failure = "0.1.1"
failure = "0.1.3"
futures = "0.1"
libc = "0.2"
mio = "0.6"
rand = "0.4"
rand = "0.7"
socket2 = "0.3"
tokio-executor="0.1.2"
time = "0.1"
tokio = "0.1.13"
tokio-executor="0.1.5"
tokio-reactor = "0.1.1"
tokio-timer = "0.2.3"
parking_lot = "0.5.5"
parking_lot = "0.9"

[dev-dependencies]
tokio = "0.1"
tokio = "0.1"
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ tokio-ping is an asynchronous ICMP pinging library.
Note, sending and receiving ICMP packets requires privileges.

```rust
extern crate futures;
extern crate tokio;

extern crate tokio_ping;

use futures::{Future, Stream};

fn main() {
Expand Down
4 changes: 1 addition & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ fn main() {
})
});

tokio::run(future.map_err(|err| {
eprintln!("Error: {}", err)
}))
tokio::run(future.map_err(|err| eprintln!("Error: {}", err)))
}
20 changes: 9 additions & 11 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::fmt;

use failure::{Fail, Context, Backtrace};
use failure::{Backtrace, Context, Fail};

#[derive(Debug)]
pub struct Error {
inner: Context<ErrorKind>
inner: Context<ErrorKind>,
}

impl Fail for Error {
fn cause(&self) -> Option<&Fail> {
fn cause(&self) -> Option<&dyn Fail> {
self.inner.cause()
}

Expand All @@ -25,22 +25,22 @@ impl fmt::Display for Error {

impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error { inner: Context::new(kind) }
Error {
inner: Context::new(kind),
}
}
}

impl From<Context<ErrorKind>> for Error {
fn from(inner: Context<ErrorKind>) -> Error {
Error { inner: inner }
Error { inner }
}
}

impl From<::std::io::Error> for Error {
fn from(error: ::std::io::Error) -> Self {
Error {
inner: Context::new(ErrorKind::IoError {
error
})
inner: Context::new(ErrorKind::IoError { error }),
}
}
}
Expand All @@ -52,7 +52,5 @@ pub enum ErrorKind {
#[fail(display = "internal error")]
InternalError,
#[fail(display = "io error: {}", error)]
IoError {
error: ::std::io::Error
}
IoError { error: ::std::io::Error },
}
20 changes: 2 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
//! Note, sending and receiving ICMP packets requires privileges.
//!
//! ```rust,no_run
//! extern crate futures;
//! extern crate tokio;
//!
//! extern crate tokio_ping;
//!
//! use futures::{Future, Stream};
//!
//! fn main() {
Expand All @@ -35,21 +30,10 @@
//! }
//! ```

#[macro_use] extern crate failure;
#[macro_use] extern crate futures;
extern crate libc;
extern crate mio;
extern crate rand;
extern crate socket2;
extern crate parking_lot;
extern crate tokio_executor;
extern crate tokio_reactor;
extern crate tokio_timer;

mod errors;
mod packet;
mod ping;
mod socket;

pub use errors::Error;
pub use ping::{Pinger, PingChain, PingChainStream, PingFuture};
pub use self::errors::Error;
pub use self::ping::{PingChain, PingChainStream, PingFuture, Pinger};
17 changes: 10 additions & 7 deletions src/packet/icmp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use failure::Fail;
use std::io::Write;

pub const HEADER_SIZE: usize = 8;
Expand Down Expand Up @@ -50,8 +51,8 @@ impl<'a> EchoRequest<'a> {
buffer[6] = (self.seq_cnt >> 8) as u8;
buffer[7] = self.seq_cnt as u8;

if let Err(_) = (&mut buffer[8..]).write(self.payload) {
return Err(Error::InvalidSize)
if (&mut buffer[8..]).write(self.payload).is_err() {
return Err(Error::InvalidSize);
}

write_checksum(buffer);
Expand All @@ -62,19 +63,19 @@ impl<'a> EchoRequest<'a> {
pub struct EchoReply<'a> {
pub ident: u16,
pub seq_cnt: u16,
pub payload: &'a [u8]
pub payload: &'a [u8],
}

impl<'a> EchoReply<'a> {
pub fn decode<P: Proto>(buffer: &'a [u8]) -> Result<Self, Error> {
if buffer.as_ref().len() < HEADER_SIZE {
return Err(Error::InvalidSize)
if buffer.len() < HEADER_SIZE {
return Err(Error::InvalidSize);
}

let type_ = buffer[0];
let code = buffer[1];
if type_ != P::ECHO_REPLY_TYPE && code != P::ECHO_REPLY_CODE {
return Err(Error::InvalidPacket)
return Err(Error::InvalidPacket);
}

let ident = (u16::from(buffer[4]) << 8) + u16::from(buffer[5]);
Expand All @@ -83,7 +84,9 @@ impl<'a> EchoReply<'a> {
let payload = &buffer[HEADER_SIZE..];

Ok(EchoReply {
ident, seq_cnt, payload
ident,
seq_cnt,
payload,
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/packet/ipv4.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use failure::Fail;

#[derive(Debug, Fail)]
pub enum Error {
#[fail(display = "too small header")]
Expand Down Expand Up @@ -54,7 +56,7 @@ impl<'a> IpV4Packet<'a> {
};

Ok(Self {
protocol: protocol,
protocol,
data: &data[header_size..],
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/packet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod icmp;
mod ipv4;

pub use self::icmp::{HEADER_SIZE as ICMP_HEADER_SIZE, IcmpV4, IcmpV6, EchoRequest, EchoReply};
pub use self::icmp::{EchoReply, EchoRequest, IcmpV4, IcmpV6, HEADER_SIZE as ICMP_HEADER_SIZE};

pub use self::ipv4::{IpV4Packet, IpV4Protocol};
Loading