Skip to content
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
32 changes: 32 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on: [push, pull_request]

name: Continuous integration

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: build

test:
name: Test Suite
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
target
src/ips.txt
.idea/
shell.nix
27 changes: 0 additions & 27 deletions .travis.yml

This file was deleted.

59 changes: 33 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
name = "pinger"
version = "0.1.0"
authors = ["Wouter de Vries <wouter@wouter-web.nl>"]
edition = "2018"

[dependencies]
libc = "0.2.11"
argparse = "0.2.1"
libc = "0.2.84"
argparse = "0.2.2"
time = "0.1.35"
byteorder = "0.5.3"
byteorder = "1.4.2"
anyhow = "1.0"
35 changes: 26 additions & 9 deletions src/main.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@

mod net;
mod tbf;
extern crate libc;
extern crate argparse;
extern crate time;
use std::io::{self, BufRead};
use argparse::{ArgumentParser, Store};
use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
// Read arguments from commandline
Expand Down Expand Up @@ -56,21 +54,25 @@ fn main() {

// Setup socket & (optionally) bind address
let sockv4 = net::new_icmpv4_socket().expect("Could not create socket (v4)");
let sockv6 = net::new_icmpv6_socket().expect("Could not create socket (v6)");

if !saddr.is_empty() {
net::bind_to_ip(sockv4, &saddr).expect("Could not bind socket to source address");
let addr = net::string_to_sockaddr(&saddr);
if let Some(addr) = addr {
if let net::SockAddr::V4(_addr) = addr {
net::bind_to_ip(sockv4, &saddr).expect("Could not bind socket to source address");
} else if let net::SockAddr::V6(_addr) = addr {
net::bind_to_ip(sockv6, &saddr).expect("Could not bind socket to source address");
}
}
}

let sockv6 = net::new_icmpv6_socket().expect("Could not create socket (v6)");


// Read from STDIN
let mut buffer = String::new();
let stdin = io::stdin();
let mut handle = stdin.lock();

// Create new ICMP-header (IPv4 and IPv6)
let icmp4header = net::ICMP4Header::echo_request(identifier, sequence_number).to_byte_array();
let icmp6header = net::ICMP6Header::echo_request(identifier, sequence_number).to_byte_array();

// Initialize TokenBucketFilter for rate-limiting
let mut tbf = tbf::TokenBucketFilter::new(rate);
Expand All @@ -79,6 +81,21 @@ fn main() {
while handle.read_line(&mut buffer).unwrap() > 0 {
// Ratelimit
tbf.take();

// Finding timestamp
let now = SystemTime::now();
let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
//let addr = string_to_sockaddr(buffer.trim());
//if let Some(addr) = addr {
// println!("Could send packet");
//} else {
// println!("Could not send packet");
//}

//Err(anyhow!(format!("Invalid IP-Address: {}", buffer.trim()).to_string()))

let icmp4header = net::ICMP4Header::echo_request(identifier, sequence_number, since_the_epoch.as_secs(), since_the_epoch.subsec_nanos(), buffer.trim()).to_byte_array();
let icmp6header = net::ICMP6Header::echo_request(identifier, sequence_number, since_the_epoch.as_secs(), since_the_epoch.subsec_nanos()).to_byte_array();

// Send packet
let result = net::send_packet(sockv4, sockv6, buffer.trim(), &icmp4header, &icmp6header);
Expand Down
Loading