Compare commits

..

No commits in common. "13f7d0208639e878e6657d9549b41356011d52a9" and "a6cc124c568783609754e9f6e0bf702df75caf58" have entirely different histories.

8 changed files with 70 additions and 177 deletions

View File

@ -5,9 +5,8 @@ This project follows [semantic versioning](http://semver.org).
### UNRELEASED ### UNRELEASED
- [added] Support for creating shell completions - [added] Support for creating shell completions
- [added] Support for hook scripts to handle certain situations
- [removed] Removed dummy device type - [removed] Removed dummy device type
- [changed] Updated dependencies - [changed] Updated depdendencies
- [changed] Changed Rust version to 1.49.0 - [changed] Changed Rust version to 1.49.0
- [fixed] Added missing peer address propagation - [fixed] Added missing peer address propagation

24
Cargo.lock generated
View File

@ -52,9 +52,9 @@ checksum = "5988cb1d626264ac94100be357308f29ff7cbdd3b36bda27f450a4ee3f713426"
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.5.0" version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f07aa6688c702439a1be0307b6a94dffe1168569e45b9500c1372bc580740d59" checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
@ -147,9 +147,9 @@ dependencies = [
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.2" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" checksum = "4060f4657be78b8e766215b02b18a2e862d83745545de804638e2b545e81aee6"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"libc", "libc",
@ -167,9 +167,9 @@ dependencies = [
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.18" version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8"
dependencies = [ dependencies = [
"libc", "libc",
] ]
@ -463,15 +463,15 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.121" version = "1.0.120"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6159e3c76cab06f6bc466244d43b35e77e9500cd685da87620addadc2a4c40b1" checksum = "166b2349061381baf54a58e4b13c89369feb0ef2eaa57198899e2312aac30aab"
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.121" version = "1.0.120"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3fcab8778dc651bc65cfab2e4eb64996f3c912b74002fb379c94517e1f27c46" checksum = "0ca2a8cb5805ce9e3b95435e3765b7b553cecc762d938d409434338386cb5775"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -619,9 +619,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.59" version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07cb8b1b4ebf86a89ee88cbd201b022b94138c623644d035185c84d3f41b7e66" checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -26,8 +26,7 @@ use crate::{
device::{Device, Type}, device::{Device, Type},
error::Error, error::Error,
messages::{ messages::{
AddrList, NodeInfo, PeerInfo, MESSAGE_TYPE_CLOSE, MESSAGE_TYPE_DATA, MESSAGE_TYPE_KEEPALIVE, AddrList, NodeInfo, PeerInfo, MESSAGE_TYPE_CLOSE, MESSAGE_TYPE_DATA, MESSAGE_TYPE_KEEPALIVE, MESSAGE_TYPE_NODE_INFO
MESSAGE_TYPE_NODE_INFO
}, },
net::{mapped_addr, Socket}, net::{mapped_addr, Socket},
payload::Protocol, payload::Protocol,
@ -36,7 +35,7 @@ use crate::{
table::ClaimTable, table::ClaimTable,
traffic::TrafficStats, traffic::TrafficStats,
types::{Address, Mode, NodeId, Range, RangeList}, types::{Address, Mode, NodeId, Range, RangeList},
util::{addr_nice, bytes_to_hex, resolve, CtrlC, Duration, MsgBuffer, StatsdMsg, Time, TimeSource} util::{addr_nice, resolve, CtrlC, Duration, MsgBuffer, StatsdMsg, Time, TimeSource}
}; };
pub type Hash = BuildHasherDefault<FnvHasher>; pub type Hash = BuildHasherDefault<FnvHasher>;
@ -126,9 +125,6 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
info!("Auto-claiming {} due to interface address", range); info!("Auto-claiming {} due to interface address", range);
claims.push(range); claims.push(range);
} }
Err(Error::DeviceIo(_, e)) if e.kind() == io::ErrorKind::AddrNotAvailable => {
info!("No address set on interface.")
}
Err(e) => error!("{}", e) Err(e) => error!("{}", e)
} }
} }
@ -229,7 +225,6 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
self.own_addresses.push(pfw.get_internal_ip().into()); self.own_addresses.push(pfw.get_internal_ip().into());
self.own_addresses.push(pfw.get_external_ip().into()); self.own_addresses.push(pfw.get_external_ip().into());
} }
// TODO: detect address changes and call event
Ok(()) Ok(())
} }
@ -284,13 +279,6 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
return Ok(()) return Ok(())
} }
} }
if !addrs.is_empty() {
self.config.call_hook(
"peer_connecting",
vec![("PEER", format!("{:?}", addr_nice(addrs[0]))), ("IFNAME", self.device.ifname().to_owned())],
true
);
}
// Send a message to each resolved address // Send a message to each resolved address
for a in addrs { for a in addrs {
// Ignore error this time // Ignore error this time
@ -466,7 +454,6 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
self.next_stats_out = now + STATS_INTERVAL; self.next_stats_out = now + STATS_INTERVAL;
self.traffic.period(Some(5)); self.traffic.period(Some(5));
} }
// TODO: every 5 minutes: EVENT periodic
if let Some(peers) = self.beacon_serializer.get_cmd_results() { if let Some(peers) = self.beacon_serializer.get_cmd_results() {
debug!("Loaded beacon with peers: {:?}", peers); debug!("Loaded beacon with peers: {:?}", peers);
for peer in peers { for peer in peers {
@ -641,16 +628,6 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
fn add_new_peer(&mut self, addr: SocketAddr, info: NodeInfo) -> Result<(), Error> { fn add_new_peer(&mut self, addr: SocketAddr, info: NodeInfo) -> Result<(), Error> {
info!("Added peer {}", addr_nice(addr)); info!("Added peer {}", addr_nice(addr));
self.config.call_hook(
"peer_connected",
vec![
("PEER", format!("{:?}", addr_nice(addr))),
("IFNAME", self.device.ifname().to_owned()),
("CLAIMS", info.claims.iter().map(|r| format!("{:?}", r)).collect::<Vec<String>>().join(" ")),
("NODE_ID", bytes_to_hex(&info.node_id)),
],
true
);
if let Some(init) = self.pending_inits.remove(&addr) { if let Some(init) = self.pending_inits.remove(&addr) {
self.peers.insert(addr, PeerData { self.peers.insert(addr, PeerData {
addrs: info.addrs.clone(), addrs: info.addrs.clone(),
@ -668,18 +645,9 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
} }
fn remove_peer(&mut self, addr: SocketAddr) { fn remove_peer(&mut self, addr: SocketAddr) {
if let Some(peer) = self.peers.remove(&addr) { if let Some(_peer) = self.peers.remove(&addr) {
info!("Closing connection to {}", addr_nice(addr)); info!("Closing connection to {}", addr_nice(addr));
self.table.remove_claims(addr); self.table.remove_claims(addr);
self.config.call_hook(
"peer_disconnected",
vec![
("PEER", format!("{:?}", addr)),
("IFNAME", self.device.ifname().to_owned()),
("NODE_ID", bytes_to_hex(&peer.node_id)),
],
true
);
} }
} }
@ -793,14 +761,6 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
let msg_result = init.handle_message(data); let msg_result = init.handle_message(data);
match msg_result { match msg_result {
Ok(res) => { Ok(res) => {
self.config.call_hook(
"peer_connecting",
vec![
("PEER", format!("{:?}", addr_nice(src))),
("IFNAME", self.device.ifname().to_owned()),
],
true
);
self.pending_inits.insert(src, init); self.pending_inits.insert(src, init);
Ok(res) Ok(res)
} }
@ -840,11 +800,6 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
debug!("Fatal crypto init error from {}: {}", src, e); debug!("Fatal crypto init error from {}: {}", src, e);
info!("Closing pending connection to {} due to error in crypto init", addr_nice(src)); info!("Closing pending connection to {} due to error in crypto init", addr_nice(src));
self.pending_inits.remove(&src); self.pending_inits.remove(&src);
self.config.call_hook(
"peer_disconnected",
vec![("PEER", format!("{:?}", addr_nice(src))), ("IFNAME", self.device.ifname().to_owned())],
true
);
} }
Err(e @ Error::CryptoInit(_)) => { Err(e @ Error::CryptoInit(_)) => {
debug!("Recoverable init error from {}: {}", src, e); debug!("Recoverable init error from {}: {}", src, e);
@ -876,7 +831,6 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
let waiter = try_fail!(WaitImpl::new(&self.socket, &self.device, 1000), "Failed to setup poll: {}"); let waiter = try_fail!(WaitImpl::new(&self.socket, &self.device, 1000), "Failed to setup poll: {}");
let mut buffer = MsgBuffer::new(SPACE_BEFORE); let mut buffer = MsgBuffer::new(SPACE_BEFORE);
let mut poll_error = false; let mut poll_error = false;
self.config.call_hook("vpn_started", vec![("IFNAME", self.device.ifname())], true);
for evt in waiter { for evt in waiter {
match evt { match evt {
WaitResult::Error(err) => { WaitResult::Error(err) => {
@ -902,7 +856,6 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
} }
} }
info!("Shutting down..."); info!("Shutting down...");
self.config.call_hook("vpn_shutdown", vec![("IFNAME", self.device.ifname())], true);
buffer.clear(); buffer.clear();
self.broadcast_msg(MESSAGE_TYPE_CLOSE, &mut buffer).ok(); self.broadcast_msg(MESSAGE_TYPE_CLOSE, &mut buffer).ok();
if let Some(ref path) = self.config.beacon_store { if let Some(ref path) = self.config.beacon_store {

View File

@ -4,20 +4,19 @@
use super::{device::Type, types::Mode, util::Duration}; use super::{device::Type, types::Mode, util::Duration};
pub use crate::crypto::Config as CryptoConfig; pub use crate::crypto::Config as CryptoConfig;
use crate::util::run_cmd;
use std::{ use std::{
cmp::max, cmp::max,
collections::HashMap, net::{IpAddr, Ipv6Addr, SocketAddr}
ffi::OsStr,
net::{IpAddr, Ipv6Addr, SocketAddr},
process::Command,
thread
}; };
use structopt::{clap::Shell, StructOpt}; use structopt::StructOpt;
use structopt::clap::Shell;
pub const DEFAULT_PEER_TIMEOUT: u16 = 300; pub const DEFAULT_PEER_TIMEOUT: u16 = 300;
pub const DEFAULT_PORT: u16 = 3210; pub const DEFAULT_PORT: u16 = 3210;
fn parse_listen(addr: &str) -> SocketAddr { fn parse_listen(addr: &str) -> SocketAddr {
if let Some(addr) = addr.strip_prefix("*:") { if let Some(addr) = addr.strip_prefix("*:") {
let port = try_fail!(addr.parse::<u16>(), "Invalid port: {}"); let port = try_fail!(addr.parse::<u16>(), "Invalid port: {}");
@ -62,9 +61,7 @@ pub struct Config {
pub statsd_server: Option<String>, pub statsd_server: Option<String>,
pub statsd_prefix: Option<String>, pub statsd_prefix: Option<String>,
pub user: Option<String>, pub user: Option<String>,
pub group: Option<String>, pub group: Option<String>
pub hook: Option<String>,
pub hooks: HashMap<String, String>
} }
impl Default for Config { impl Default for Config {
@ -97,9 +94,7 @@ impl Default for Config {
statsd_server: None, statsd_server: None,
statsd_prefix: None, statsd_prefix: None,
user: None, user: None,
group: None, group: None
hook: None,
hooks: HashMap::new()
} }
} }
} }
@ -204,12 +199,6 @@ impl Config {
if !file.crypto.algorithms.is_empty() { if !file.crypto.algorithms.is_empty() {
self.crypto.algorithms = file.crypto.algorithms.clone(); self.crypto.algorithms = file.crypto.algorithms.clone();
} }
if let Some(val) = file.hook {
self.hook = Some(val)
}
for (k, v) in file.hooks {
self.hooks.insert(k, v);
}
} }
pub fn merge_args(&mut self, mut args: Args) { pub fn merge_args(&mut self, mut args: Args) {
@ -303,16 +292,6 @@ impl Config {
if !args.algorithms.is_empty() { if !args.algorithms.is_empty() {
self.crypto.algorithms = args.algorithms.clone(); self.crypto.algorithms = args.algorithms.clone();
} }
for s in args.hook {
if s.contains(':') {
let pos = s.find(':').unwrap();
let name = &s[..pos];
let hook = &s[pos+1..];
self.hooks.insert(name.to_string(), hook.to_string());
} else {
self.hook = Some(s);
}
}
} }
pub fn get_keepalive(&self) -> Duration { pub fn get_keepalive(&self) -> Duration {
@ -321,32 +300,9 @@ impl Config {
None => max(self.peer_timeout / 2 - 60, 1) None => max(self.peer_timeout / 2 - 60, 1)
} }
} }
pub fn call_hook(
&self, event: &'static str, envs: impl IntoIterator<Item = (&'static str, impl AsRef<OsStr>)>, detach: bool
) {
let mut script = None;
if let Some(ref s) = self.hook {
script = Some(s);
}
if let Some(ref s) = self.hooks.get(event) {
script = Some(s);
}
if script.is_none() {
return
}
let script = script.unwrap();
let mut cmd = Command::new("sh");
cmd.arg("-c").arg(script).envs(envs).env("EVENT", event);
debug!("Running event script: {:?}", cmd);
if detach {
thread::spawn(move || run_cmd(cmd));
} else {
run_cmd(cmd)
}
}
} }
#[derive(StructOpt, Debug, Default)] #[derive(StructOpt, Debug, Default)]
pub struct Args { pub struct Args {
/// Read configuration options from the specified file. /// Read configuration options from the specified file.
@ -507,11 +463,7 @@ pub struct Args {
/// Generate shell completions /// Generate shell completions
#[structopt(long)] #[structopt(long)]
pub completion: Option<Shell>, pub completion: Option<Shell>
/// Call script on event
#[structopt(long)]
pub hook: Vec<String>
} }
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)] #[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
@ -565,11 +517,10 @@ pub struct ConfigFile {
pub stats_file: Option<String>, pub stats_file: Option<String>,
pub statsd: Option<ConfigFileStatsd>, pub statsd: Option<ConfigFileStatsd>,
pub user: Option<String>, pub user: Option<String>,
pub group: Option<String>, pub group: Option<String>
pub hook: Option<String>,
pub hooks: HashMap<String, String>
} }
#[test] #[test]
fn config_file() { fn config_file() {
let config_file = " let config_file = "
@ -636,9 +587,7 @@ statsd:
statsd: Some(ConfigFileStatsd { statsd: Some(ConfigFileStatsd {
server: Some("example.com:1234".to_string()), server: Some("example.com:1234".to_string()),
prefix: Some("prefix".to_string()) prefix: Some("prefix".to_string())
}), })
hook: None,
hooks: HashMap::new()
}) })
} }
@ -672,12 +621,9 @@ fn default_config_as_default() {
statsd_server: None, statsd_server: None,
statsd_prefix: None, statsd_prefix: None,
user: None, user: None,
group: None, group: None
hook: None,
hooks: HashMap::new()
}; };
let default_config_file = let default_config_file = serde_yaml::from_str::<ConfigFile>(include_str!("../assets/example.net.disabled")).unwrap();
serde_yaml::from_str::<ConfigFile>(include_str!("../assets/example.net.disabled")).unwrap();
default_config.merge_file(default_config_file); default_config.merge_file(default_config_file);
assert_eq!(default_config, Config::default()); assert_eq!(default_config, Config::default());
} }
@ -718,9 +664,7 @@ fn config_merge() {
statsd: Some(ConfigFileStatsd { statsd: Some(ConfigFileStatsd {
server: Some("example.com:1234".to_string()), server: Some("example.com:1234".to_string()),
prefix: Some("prefix".to_string()) prefix: Some("prefix".to_string())
}), })
hook: None,
hooks: HashMap::new()
}); });
assert_eq!(config, Config { assert_eq!(config, Config {
device_type: Type::Tun, device_type: Type::Tun,
@ -809,8 +753,6 @@ fn config_merge() {
stats_file: Some("/var/log/vpncloud-mynet.stats".to_string()), stats_file: Some("/var/log/vpncloud-mynet.stats".to_string()),
statsd_server: Some("example.com:2345".to_string()), statsd_server: Some("example.com:2345".to_string()),
statsd_prefix: Some("prefix2".to_string()), statsd_prefix: Some("prefix2".to_string()),
daemonize: true, daemonize: true
hook: None,
hooks: HashMap::new()
}); });
} }

View File

@ -5,12 +5,12 @@
use std::{ use std::{
cmp, cmp,
collections::VecDeque, collections::VecDeque,
convert::TryInto,
fmt, fmt,
fs::{self, File}, fs::{self, File},
io::{self, BufRead, BufReader, Cursor, Error as IoError, Read, Write}, io::{self, BufRead, BufReader, Cursor, Error as IoError, Read, Write},
net::{Ipv4Addr, UdpSocket}, net::{Ipv4Addr, UdpSocket},
os::unix::io::{AsRawFd, RawFd}, os::unix::io::{AsRawFd, RawFd},
convert::TryInto,
str, str,
str::FromStr str::FromStr
}; };
@ -36,7 +36,7 @@ struct IfReq {
impl IfReq { impl IfReq {
fn new(name: &str) -> Self { fn new(name: &str) -> Self {
assert!(name.len() < libc::IF_NAMESIZE); assert!(name.len() < libc::IF_NAMESIZE);
let mut ifr_name = [0; libc::IF_NAMESIZE]; let mut ifr_name = [0 as u8; libc::IF_NAMESIZE];
ifr_name[..name.len()].clone_from_slice(name.as_bytes()); ifr_name[..name.len()].clone_from_slice(name.as_bytes());
Self { ifr_name, data: IfReqData { _dummy: [0; 24] } } Self { ifr_name, data: IfReqData { _dummy: [0; 24] } }
} }
@ -329,7 +329,7 @@ impl Device for MockDevice {
} }
fn ifname(&self) -> &str { fn ifname(&self) -> &str {
"mock0" unimplemented!()
} }
fn read(&mut self, buffer: &mut MsgBuffer) -> Result<(), Error> { fn read(&mut self, buffer: &mut MsgBuffer) -> Result<(), Error> {

View File

@ -140,7 +140,6 @@ fn setup_device(config: &Config) -> TunTapDevice {
config.device_name config.device_name
); );
info!("Opened device {}", device.ifname()); info!("Opened device {}", device.ifname());
config.call_hook("device_setup", vec![("IFNAME", device.ifname())], true);
if let Err(err) = device.set_mtu(None) { if let Err(err) = device.set_mtu(None) {
error!("Error setting optimal MTU on {}: {}", device.ifname(), err); error!("Error setting optimal MTU on {}: {}", device.ifname(), err);
} }
@ -160,7 +159,6 @@ fn setup_device(config: &Config) -> TunTapDevice {
warn!("Your networking configuration might be affected by a vulnerability (https://vpncloud.ddswd.de/docs/security/cve-2019-14899/), please change your rp_filter setting to 1 (currently {}).", val); warn!("Your networking configuration might be affected by a vulnerability (https://vpncloud.ddswd.de/docs/security/cve-2019-14899/), please change your rp_filter setting to 1 (currently {}).", val);
} }
} }
config.call_hook("device_configured", vec![("IFNAME", device.ifname())], true);
device device
} }

View File

@ -1,6 +1,5 @@
use super::{device::Type, types::Mode, util::Duration}; use super::{device::Type, types::Mode, util::Duration};
use crate::config::{ConfigFile, ConfigFileBeacon, ConfigFileDevice, ConfigFileStatsd, CryptoConfig}; use crate::config::{ConfigFile, ConfigFileBeacon, ConfigFileDevice, ConfigFileStatsd, CryptoConfig};
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy)] #[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy)]
pub enum OldCryptoMethod { pub enum OldCryptoMethod {
@ -113,11 +112,12 @@ impl OldConfigFile {
pid_file: self.pid_file, pid_file: self.pid_file,
port_forwarding: self.port_forwarding, port_forwarding: self.port_forwarding,
stats_file: self.stats_file, stats_file: self.stats_file,
statsd: Some(ConfigFileStatsd { prefix: self.statsd_prefix, server: self.statsd_server }), statsd: Some(ConfigFileStatsd {
prefix: self.statsd_prefix,
server: self.statsd_server
}),
switch_timeout: self.dst_timeout, switch_timeout: self.dst_timeout,
user: self.user, user: self.user
hook: None,
hooks: HashMap::new()
} }
} }
} }

View File

@ -2,31 +2,31 @@
// Copyright (C) 2015-2020 Dennis Schwerdel // Copyright (C) 2015-2020 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md) // This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::process::Command;
use std::{ use std::{
fmt, fmt,
net::{Ipv4Addr, SocketAddr, ToSocketAddrs, UdpSocket}, net::{Ipv4Addr, SocketAddr, ToSocketAddrs, UdpSocket},
sync::atomic::{AtomicIsize, Ordering}, sync::atomic::{AtomicIsize, Ordering}
}; };
use crate::error::Error; use crate::error::Error;
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))] use time;
use time;
use signal::{trap::Trap, Signal}; use signal::{trap::Trap, Signal};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::time::Instant; use std::time::Instant;
pub type Duration = u32; pub type Duration = u32;
pub type Time = i64; pub type Time = i64;
#[derive(Clone)] #[derive(Clone)]
pub struct MsgBuffer { pub struct MsgBuffer {
space_before: usize, space_before: usize,
buffer: [u8; 65535], buffer: [u8; 65535],
start: usize, start: usize,
end: usize, end: usize
} }
impl MsgBuffer { impl MsgBuffer {
@ -98,6 +98,7 @@ impl MsgBuffer {
} }
} }
const HEX_CHARS: &[u8] = b"0123456789abcdef"; const HEX_CHARS: &[u8] = b"0123456789abcdef";
pub fn bytes_to_hex(bytes: &[u8]) -> String { pub fn bytes_to_hex(bytes: &[u8]) -> String {
@ -112,12 +113,13 @@ pub fn bytes_to_hex(bytes: &[u8]) -> String {
pub fn addr_nice(addr: SocketAddr) -> SocketAddr { pub fn addr_nice(addr: SocketAddr) -> SocketAddr {
if let SocketAddr::V6(v6addr) = addr { if let SocketAddr::V6(v6addr) = addr {
if let Some(ip) = v6addr.ip().to_ipv4() { if let Some(ip) = v6addr.ip().to_ipv4() {
return (ip, addr.port()).into(); return (ip, addr.port()).into()
} }
} }
addr addr
} }
pub struct Encoder; pub struct Encoder;
impl Encoder { impl Encoder {
@ -170,6 +172,7 @@ impl Encoder {
} }
} }
macro_rules! fail { macro_rules! fail {
($format:expr) => ( { ($format:expr) => ( {
use std::process; use std::process;
@ -212,14 +215,17 @@ pub fn get_internal_ip() -> Ipv4Addr {
} }
} }
#[allow(unknown_lints, clippy::needless_pass_by_value)] #[allow(unknown_lints, clippy::needless_pass_by_value)]
pub fn resolve<Addr: ToSocketAddrs + fmt::Debug>(addr: Addr) -> Result<SmallVec<[SocketAddr; 4]>, Error> { pub fn resolve<Addr: ToSocketAddrs + fmt::Debug>(addr: Addr) -> Result<SmallVec<[SocketAddr; 4]>, Error> {
let mut addrs = let mut addrs =
addr.to_socket_addrs().map_err(|_| Error::NameUnresolvable(format!("{:?}", addr)))?.collect::<SmallVec<_>>(); addr.to_socket_addrs().map_err(|_| Error::NameUnresolvable(format!("{:?}", addr)))?.collect::<SmallVec<_>>();
// Try IPv4 first as it usually is faster // Try IPv4 first as it usually is faster
addrs.sort_by_key(|addr| match *addr { addrs.sort_by_key(|addr| {
SocketAddr::V4(_) => 4, match *addr {
SocketAddr::V6(_) => 6, SocketAddr::V4(_) => 4,
SocketAddr::V6(_) => 6
}
}); });
// Remove duplicates in addrs (why are there duplicates???) // Remove duplicates in addrs (why are there duplicates???)
addrs.dedup(); addrs.dedup();
@ -233,6 +239,7 @@ macro_rules! addr {
}}; }};
} }
pub struct Bytes(pub u64); pub struct Bytes(pub u64);
impl fmt::Display for Bytes { impl fmt::Display for Bytes {
@ -241,30 +248,31 @@ impl fmt::Display for Bytes {
if size >= 512.0 { if size >= 512.0 {
size /= 1024.0; size /= 1024.0;
} else { } else {
return write!(formatter, "{:.0} B", size); return write!(formatter, "{:.0} B", size)
} }
if size >= 512.0 { if size >= 512.0 {
size /= 1024.0; size /= 1024.0;
} else { } else {
return write!(formatter, "{:.1} KiB", size); return write!(formatter, "{:.1} KiB", size)
} }
if size >= 512.0 { if size >= 512.0 {
size /= 1024.0; size /= 1024.0;
} else { } else {
return write!(formatter, "{:.1} MiB", size); return write!(formatter, "{:.1} MiB", size)
} }
if size >= 512.0 { if size >= 512.0 {
size /= 1024.0; size /= 1024.0;
} else { } else {
return write!(formatter, "{:.1} GiB", size); return write!(formatter, "{:.1} GiB", size)
} }
write!(formatter, "{:.1} TiB", size) write!(formatter, "{:.1} TiB", size)
} }
} }
pub struct CtrlC { pub struct CtrlC {
dummy_time: Instant, dummy_time: Instant,
trap: Trap, trap: Trap
} }
impl CtrlC { impl CtrlC {
@ -285,6 +293,7 @@ impl Default for CtrlC {
} }
} }
pub trait TimeSource: Sync + Copy + Send + 'static { pub trait TimeSource: Sync + Copy + Send + 'static {
fn now() -> Time; fn now() -> Time;
} }
@ -327,6 +336,7 @@ impl TimeSource for MockTimeSource {
} }
} }
/// Helper function that multiplies the base62 data in buf[0..buflen] by 16 and adds m to it /// Helper function that multiplies the base62 data in buf[0..buflen] by 16 and adds m to it
fn base62_add_mult_16(buf: &mut [u8], mut buflen: usize, m: u8) -> usize { fn base62_add_mult_16(buf: &mut [u8], mut buflen: usize, m: u8) -> usize {
let mut d: usize = m as usize; let mut d: usize = m as usize;
@ -346,7 +356,7 @@ fn base62_add_mult_16(buf: &mut [u8], mut buflen: usize, m: u8) -> usize {
const BASE62: [char; 62] = [ const BASE62: [char; 62] = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
]; ];
pub fn to_base62(data: &[u8]) -> String { pub fn to_base62(data: &[u8]) -> String {
@ -372,7 +382,7 @@ pub fn from_base62(data: &str) -> Result<Vec<u8>, char> {
'0'..='9' => ((c as usize) % ('0' as usize)), '0'..='9' => ((c as usize) % ('0' as usize)),
'A'..='Z' => ((c as usize) % ('A' as usize)) + 10, 'A'..='Z' => ((c as usize) % ('A' as usize)) + 10,
'a'..='z' => ((c as usize) % ('a' as usize)) + 36, 'a'..='z' => ((c as usize) % ('a' as usize)) + 36,
_ => return Err(c), _ => return Err(c)
}; };
for item in &mut buf { for item in &mut buf {
val += *item as usize * 62; val += *item as usize * 62;
@ -387,10 +397,11 @@ pub fn from_base62(data: &str) -> Result<Vec<u8>, char> {
Ok(buf) Ok(buf)
} }
#[derive(Default)] #[derive(Default)]
pub struct StatsdMsg { pub struct StatsdMsg {
entries: Vec<String>, entries: Vec<String>,
key: Vec<String>, key: Vec<String>
} }
impl StatsdMsg { impl StatsdMsg {
@ -415,16 +426,6 @@ impl StatsdMsg {
} }
} }
pub fn run_cmd(mut cmd: Command) {
match cmd.status() {
Ok(status) => {
if !status.success() {
error!("Command returned error: {:?}", status.code())
}
}
Err(e) => error!("Failed to execute command {:?}: {}", cmd, e),
}
}
#[test] #[test]
fn base62() { fn base62() {