diff options
author | Jake McGinty <me@jake.su> | 2018-04-16 18:43:52 -0700 |
---|---|---|
committer | Jake McGinty <me@jake.su> | 2018-04-22 14:08:41 -0700 |
commit | 0cc9ecc94a31ecca99c334c1b06df70f650ec82b (patch) | |
tree | 1f429f220a5888b79596f82dc3a63f45454eed13 | |
parent | udp: return back "magic" endpoint in recv_from (diff) | |
download | wireguard-rs-0cc9ecc94a31ecca99c334c1b06df70f650ec82b.tar.xz wireguard-rs-0cc9ecc94a31ecca99c334c1b06df70f650ec82b.zip |
global: SocketAddr -> Endpoint
-rw-r--r-- | Cargo.lock | 56 | ||||
-rw-r--r-- | src/interface/config.rs | 4 | ||||
-rw-r--r-- | src/interface/peer_server.rs | 14 | ||||
-rw-r--r-- | src/peer.rs | 11 | ||||
-rw-r--r-- | src/types.rs | 3 | ||||
-rw-r--r-- | src/udp/frame.rs | 12 | ||||
-rw-r--r-- | src/udp/mod.rs | 29 |
7 files changed, 72 insertions, 57 deletions
@@ -176,28 +176,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "criterion" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)", - "criterion-plot 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "criterion-stats 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "criterion-plot 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "criterion-stats 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "handlebars 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", "itertools-num 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)", "simplelog 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "criterion-plot" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -207,7 +207,7 @@ dependencies = [ [[package]] name = "criterion-stats" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -430,8 +430,8 @@ dependencies = [ "pest_derive 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -650,7 +650,7 @@ dependencies = [ [[package]] name = "nix" version = "0.11.0-pre" -source = "git+https://github.com/mcginty/nix?branch=ipv6-pktinfo#aaafeb51f77bbe2846a0f6b3d3144e35eff5d759" +source = "git+https://github.com/mcginty/nix?branch=ipv6-pktinfo#7eb2f413dc31ad47e285c196401903d31d9596a6" dependencies = [ "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "cc 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -847,14 +847,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "regex-syntax 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "regex-syntax" -version = "0.5.3" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -936,23 +936,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "1.0.37" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde_derive" -version = "1.0.37" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive_internals 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive_internals 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)", "syn 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_derive_internals" -version = "0.23.0" +version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -961,13 +961,13 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1482,7 +1482,7 @@ dependencies = [ "chacha20-poly1305-aead 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "criterion 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "criterion 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "daemonize 0.2.3 (git+https://github.com/mcginty/daemonize)", "derive_deref 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1555,9 +1555,9 @@ dependencies = [ "checksum clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0f16b89cbb9ee36d87483dc939fe9f1e13c05898d56d7b230a0d4dff033a536" "checksum colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b0aa3473e85a3161b59845d6096b289bb577874cafeaf75ea1b1beaa6572c7fc" "checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e" -"checksum criterion 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4556caa5b5e69626a0b69c4892baa6ab518cf2e802384a5eeef15dd4416d4771" -"checksum criterion-plot 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "029914bacd6bb0a521429ff8df0c6c79be1d99f5c4bb85475caabb78f5e06da6" -"checksum criterion-stats 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a57a868c589ef2208c0f441e816810e16bfd9cf6a6ea6548f53938b8a530d362" +"checksum criterion 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4f11151e2961d0483e5eb7a2ede5ed8071a460d04d2b7c89e8257aa5502b0e0b" +"checksum criterion-plot 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9f7f7c88a8d341dd9fd9e31a72ca2ca24428db79afb491852873b2c784e037e6" +"checksum criterion-stats 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "dd48feb0253b2968ff3085e7f3fba6738c9ff859f420a2fb81a48986eb66da36" "checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3" "checksum crossbeam-deque 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c1bdc73742c36f7f35ebcda81dbb33a7e0d33757d03a06d9ddca762712ec5ea2" "checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150" @@ -1632,7 +1632,7 @@ dependencies = [ "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "aec3f58d903a7d2a9dc2bf0e41a746f4530e0cab6b615494e058f67a3ef947fb" -"checksum regex-syntax 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b2550876c31dc914696a6c2e01cbce8afba79a93c8ae979d2fe051c0230b3756" +"checksum regex-syntax 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "bd90079345f4a4c3409214734ae220fd773c6f2e8a543d07370c6c1c369cfbfb" "checksum ring 0.13.0-alpha (registry+https://github.com/rust-lang/crates.io-index)" = "946e5e2b336032275e23152755ec2a0610ede0302101d3666fdb686795d26535" "checksum rust-crypto 0.2.36 (git+https://github.com/mcginty/rust-crypto)" = "<none>" "checksum rustc-demangle 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11fb43a206a04116ffd7cfcf9bcb941f8eb6cc7ff667272246b0a1c74259a3cb" @@ -1643,10 +1643,10 @@ dependencies = [ "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" -"checksum serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)" = "d3bcee660dcde8f52c3765dd9ca5ee36b4bf35470a738eb0bd5a8752b0389645" -"checksum serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)" = "f1711ab8b208541fa8de00425f6a577d90f27bb60724d2bb5fd911314af9668f" -"checksum serde_derive_internals 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)" = "89b340a48245bc03ddba31d0ff1709c118df90edc6adabaca4aac77aea181cce" -"checksum serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "5c508584d9913df116b91505eec55610a2f5b16e9ed793c46e4d0152872b3e74" +"checksum serde 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)" = "4c36359ac1a823e00db02a243376ced650f088dc1f6259bbf828e4668e3c7399" +"checksum serde_derive 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)" = "f0477feff739386f5bca8e13fa43d96a4e834904d538f503906c8179f9205f50" +"checksum serde_derive_internals 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9d30c4596450fd7bbda79ef15559683f9a79ac0193ea819db90000d7e1cae794" +"checksum serde_json 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "8f6f1f77b969caa064f347544d703efacaf4854b84831096a5dc206a8aedbc27" "checksum simplelog 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce595117de34b75e057b41e99079e43e9fcc4e5ec9c7ba5f2fea55321f0c624e" "checksum slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d807fd58c4181bbabed77cb3b891ba9748241a552bcc5be698faaebefc54f46e" "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" diff --git a/src/interface/config.rs b/src/interface/config.rs index c40be20..574876d 100644 --- a/src/interface/config.rs +++ b/src/interface/config.rs @@ -12,10 +12,12 @@ use hex; use interface::{SharedState, State}; use interface::grim_reaper::GrimReaper; use peer::Peer; +use std::net::SocketAddr; use std::{cell::RefCell, iter::Iterator, rc::Rc, mem, str}; use std::fs::{create_dir, remove_file}; use std::path::{Path, PathBuf}; use tokio_core::reactor::Handle; +use udp::Endpoint; use types::PeerInfo; use hex::FromHex; use x25519_dalek as x25519; @@ -56,7 +58,7 @@ impl UpdateEvent { "replace_peers" => { events.push(UpdateEvent::RemoveAllPeers); }, "preshared_key" => { info.psk = Some(<[u8; 32]>::from_hex(&value)?); }, "persistent_keepalive_interval" => { info.keepalive = Some(value.parse()?); }, - "endpoint" => { info.endpoint = Some(value.parse()?); }, + "endpoint" => { info.endpoint = Some(value.parse::<SocketAddr>()?.into()); }, "replace_allowed_ips" => { replace_allowed_ips = true; }, "remove" => { remove_pending_peer = true; }, "public_key" => { diff --git a/src/interface/peer_server.rs b/src/interface/peer_server.rs index 2b05b94..9f680fd 100644 --- a/src/interface/peer_server.rs +++ b/src/interface/peer_server.rs @@ -15,7 +15,7 @@ use byteorder::{ByteOrder, LittleEndian}; use failure::{Error, err_msg}; use futures::{Async, Future, Stream, Sink, Poll, unsync::mpsc}; use rand::{self, Rng}; -use udp::{UdpSocket, PeerServerMessage, UdpChannel}; +use udp::{Endpoint, UdpSocket, PeerServerMessage, UdpChannel}; use tokio_core::reactor::Handle; struct Channel<T> { @@ -110,7 +110,7 @@ impl PeerServer { } } - fn handle_ingress_packet(&mut self, addr: SocketAddr, packet: Vec<u8>) -> Result<(), Error> { + fn handle_ingress_packet(&mut self, addr: Endpoint, packet: Vec<u8>) -> Result<(), Error> { trace!("got a UDP packet from {:?} of length {}, packet type {}", &addr, packet.len(), packet[0]); match packet.try_into()? { @@ -121,7 +121,7 @@ impl PeerServer { } } - fn handle_ingress_handshake_init(&mut self, addr: SocketAddr, packet: &Initiation) -> Result<(), Error> { + fn handle_ingress_handshake_init(&mut self, addr: Endpoint, packet: &Initiation) -> Result<(), Error> { ensure!(packet.len() == 148, "handshake init packet length is incorrect"); let mut state = self.shared_state.borrow_mut(); { @@ -139,7 +139,7 @@ impl PeerServer { .ok_or_else(|| err_msg("unknown peer pubkey"))?.clone(); let index = Self::unused_index(&mut state); - let (response, dead_index) = peer_ref.borrow_mut().complete_incoming_handshake(addr, index, handshake)?; + let (response, dead_index) = peer_ref.borrow_mut().complete_incoming_handshake(addr.clone(), index, handshake)?; if let Some(index) = dead_index { let _ = state.index_map.remove(&index); } @@ -152,7 +152,7 @@ impl PeerServer { } // TODO use the address to update endpoint if it changes i suppose - fn handle_ingress_handshake_resp(&mut self, addr: SocketAddr, packet: &Response) -> Result<(), Error> { + fn handle_ingress_handshake_resp(&mut self, addr: Endpoint, packet: &Response) -> Result<(), Error> { ensure!(packet.len() == 92, "handshake resp packet length is incorrect"); let mut state = self.shared_state.borrow_mut(); { @@ -200,7 +200,7 @@ impl PeerServer { Ok(()) } - fn handle_ingress_cookie_reply(&mut self, _addr: SocketAddr, packet: &CookieReply) -> Result<(), Error> { + fn handle_ingress_cookie_reply(&mut self, _addr: Endpoint, packet: &CookieReply) -> Result<(), Error> { let state = self.shared_state.borrow_mut(); let peer_ref = state.index_map.get(&packet.our_index()).ok_or_else(|| err_msg("unknown our_index"))?.clone(); let mut peer = peer_ref.borrow_mut(); @@ -208,7 +208,7 @@ impl PeerServer { peer.consume_cookie_reply(packet) } - fn handle_ingress_transport(&mut self, addr: SocketAddr, packet: &Transport) -> Result<(), Error> { + fn handle_ingress_transport(&mut self, addr: Endpoint, packet: &Transport) -> Result<(), Error> { let peer_ref = self.shared_state.borrow().index_map.get(&packet.our_index()) .ok_or_else(|| err_msg("unknown our_index"))?.clone(); diff --git a/src/peer.rs b/src/peer.rs index 9aaf422..a7ee5f7 100644 --- a/src/peer.rs +++ b/src/peer.rs @@ -18,6 +18,7 @@ use hex; use time::{Tai64n, Timestamp}; use snow; use types::PeerInfo; +use udp::Endpoint; pub struct Peer { pub info : PeerInfo, @@ -218,7 +219,7 @@ impl Peer { indices } - pub fn initiate_new_session(&mut self, private_key: &[u8], index: u32) -> Result<(SocketAddr, Vec<u8>, Option<u32>), Error> { + pub fn initiate_new_session(&mut self, private_key: &[u8], index: u32) -> Result<(Endpoint, Vec<u8>, Option<u32>), Error> { let noise = noise::build_initiator(private_key, &self.info.pub_key, &self.info.psk)?; let mut session = Session::new(noise, index); let endpoint = self.info.endpoint.ok_or_else(|| err_msg("no known peer endpoint"))?; @@ -260,7 +261,7 @@ impl Peer { /// and generates a response. /// /// Returns: the response packet (type 0x02), and an optional dead session index that was removed. - pub fn complete_incoming_handshake(&mut self, addr: SocketAddr, index: u32, incomplete: IncompleteIncomingHandshake) -> Result<(Vec<u8>, Option<u32>), Error> { + pub fn complete_incoming_handshake(&mut self, addr: Endpoint, index: u32, incomplete: IncompleteIncomingHandshake) -> Result<(Vec<u8>, Option<u32>), Error> { let IncompleteIncomingHandshake { timestamp, their_index, mut noise } = incomplete; if let Some(ref last_tai64n) = self.last_handshake_tai64n { @@ -312,7 +313,7 @@ impl Peer { self.cookie.consume_reply(reply) } - pub fn process_incoming_handshake_response(&mut self, addr: SocketAddr, packet: &Response) -> Result<Option<u32>, Error> { + pub fn process_incoming_handshake_response(&mut self, addr: Endpoint, packet: &Response) -> Result<Option<u32>, Error> { let mut session = mem::replace(&mut self.sessions.next, None).ok_or_else(|| err_msg("no next session"))?; let _ = session.noise.read_message(packet.noise_bytes(), &mut [])?; session = session.into_transport_mode()?; @@ -327,7 +328,7 @@ impl Peer { Ok(dead.map(|session| session.our_index)) } - pub fn handle_incoming_transport(&mut self, addr: SocketAddr, packet: &Transport) + pub fn handle_incoming_transport(&mut self, addr: Endpoint, packet: &Transport) -> Result<(Vec<u8>, SessionTransition), Error> { let mut raw_packet = vec![0u8; packet.len()]; @@ -376,7 +377,7 @@ impl Peer { Ok((raw_packet, transition)) } - pub fn handle_outgoing_transport(&mut self, packet: &[u8]) -> Result<(SocketAddr, Vec<u8>), Error> { + pub fn handle_outgoing_transport(&mut self, packet: &[u8]) -> Result<(Endpoint, Vec<u8>), Error> { let session = self.sessions.current.as_mut().ok_or_else(|| err_msg("no current noise session"))?; let endpoint = self.info.endpoint.ok_or_else(|| err_msg("no known peer endpoint"))?; let padding = PADDING_MULTIPLE - (packet.len() % PADDING_MULTIPLE); diff --git a/src/types.rs b/src/types.rs index 9f2597b..cc65105 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,12 +1,13 @@ use base64; use std::fmt::{self, Display, Formatter}; use std::net::{IpAddr, SocketAddr}; +use udp::Endpoint; #[derive(Clone, Debug, Default)] pub struct PeerInfo { pub pub_key: [u8; 32], pub psk: Option<[u8; 32]>, - pub endpoint: Option<SocketAddr>, + pub endpoint: Option<Endpoint>, pub allowed_ips: Vec<(IpAddr, u32)>, pub keepalive: Option<u16>, } diff --git a/src/udp/frame.rs b/src/udp/frame.rs index e8f3b69..239a58d 100644 --- a/src/udp/frame.rs +++ b/src/udp/frame.rs @@ -5,7 +5,7 @@ use std::os::unix::io::{AsRawFd, RawFd}; use failure::Error; use futures::{Async, Future, Poll, Stream, Sink, StartSend, AsyncSink, future, stream, unsync::mpsc}; use nix::sys::socket::{sockopt, setsockopt}; -use udp::UdpSocket; +use udp::{Endpoint, UdpSocket}; use tokio_core::reactor::Handle; use std::net::Ipv6Addr; @@ -20,7 +20,7 @@ pub struct UdpFramed { codec: VecUdpCodec, rd: Vec<u8>, wr: Vec<u8>, - out_addr: SocketAddr, + out_addr: Endpoint, flushed: bool, } @@ -89,7 +89,7 @@ pub fn new(socket: UdpSocket) -> UdpFramed { UdpFramed { socket, codec: VecUdpCodec {}, - out_addr: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0)), + out_addr: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0)).into(), rd: vec![0; 64 * 1024], wr: Vec::with_capacity(8 * 1024), flushed: true, @@ -136,14 +136,14 @@ fn v6_mapped_to_v4(addr: Ipv6Addr) -> Option<Ipv4Addr> { } } -pub type PeerServerMessage = (SocketAddr, Vec<u8>); +pub type PeerServerMessage = (Endpoint, Vec<u8>); pub struct VecUdpCodec; impl VecUdpCodec { - fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> io::Result<PeerServerMessage> { + fn decode(&mut self, src: &Endpoint, buf: &[u8]) -> io::Result<PeerServerMessage> { Ok((*src, buf.to_vec())) } - fn encode(&mut self, msg: PeerServerMessage, buf: &mut Vec<u8>) -> SocketAddr { + fn encode(&mut self, msg: PeerServerMessage, buf: &mut Vec<u8>) -> Endpoint { let (mut addr, mut data) = msg; buf.append(&mut data); addr diff --git a/src/udp/mod.rs b/src/udp/mod.rs index b6b6799..b6bca9e 100644 --- a/src/udp/mod.rs +++ b/src/udp/mod.rs @@ -23,6 +23,10 @@ use socket2::{Socket, Domain, Type, Protocol}; use tokio_core::reactor::{Handle, PollEvented}; +mod frame; +pub use self::frame::{UdpChannel, UdpFramed, VecUdpCodec, PeerServerMessage}; +use std::ops::Deref; + /// An I/O object representing a UDP socket. pub struct UdpSocket { io4: PollEvented<mio::net::UdpSocket>, @@ -30,9 +34,10 @@ pub struct UdpSocket { handle: Handle, } +#[derive(Clone, Copy, Debug)] pub struct Endpoint { pub addr: SocketAddr, - pub pktinfo: PktInfo, + pub pktinfo: Option<PktInfo>, } impl Deref for Endpoint { @@ -43,19 +48,25 @@ impl Deref for Endpoint { } } +impl From<SocketAddr> for Endpoint { + fn from(addr: SocketAddr) -> Self { + Endpoint { + addr, + pktinfo: None + } + } +} + /// IPV6_RECVPKTINFO is missing from the libc crate. Value taken from https://git.io/vxNel. pub const IPV6_RECVPKTINFO : i32 = 61; pub const IP_PKTINFO : i32 = 26; +#[derive(Clone, Copy, Debug)] pub enum PktInfo { V4(in_pktinfo), V6(in6_pktinfo), } -mod frame; -pub use self::frame::{UdpChannel, UdpFramed, VecUdpCodec, PeerServerMessage}; -use std::ops::Deref; - impl UdpSocket { pub fn bind(port: u16, handle: Handle) -> io::Result<UdpSocket> { let socket4 = Socket::new(Domain::ipv4(), Type::dgram(), Some(Protocol::udp()))?; @@ -154,7 +165,7 @@ impl UdpSocket { /// /// Address type can be any implementer of `ToSocketAddrs` trait. See its /// documentation for concrete examples. - pub fn send_to(&self, buf: &[u8], target: &SocketAddr) -> io::Result<usize> { + pub fn send_to(&self, buf: &[u8], target: &Endpoint) -> io::Result<usize> { let io = self.get_io(target); if let Async::NotReady = io.poll_write() { return Err(io::ErrorKind::WouldBlock.into()) @@ -197,16 +208,16 @@ impl UdpSocket { info.ipi_ifindex); Ok((msg.bytes, Endpoint { addr: addr.to_std(), - pktinfo: PktInfo::V4(info.clone()) + pktinfo: Some(PktInfo::V4(info.clone())) })) }, Some(ControlMessage::Ipv6PacketInfo(info)) => { trace!("ipv6 cmsg (\n ipi6_addr: {:?},\n ipi6_ifindex: {}\n)", - Ipv6Addr::from(info.ipi6_addr.s6_addr), + Ipv6Addr::from(info.ipi6_addr), info.ipi6_ifindex); Ok((msg.bytes, Endpoint { addr: addr.to_std(), - pktinfo: PktInfo::V6(info.clone()) + pktinfo: Some(PktInfo::V6(info.clone())) })) }, _ => Err(io::Error::new(io::ErrorKind::Other, "missing pktinfo")) |