aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJake McGinty <me@jake.su>2018-04-25 01:30:00 -0700
committerJake McGinty <me@jake.su>2018-04-25 01:30:00 -0700
commit3a2be07f72d02751d04285e7a92ec047c0f96412 (patch)
treedf2946089dd97bf701f3a899562d1943b105be8c /src
parenttests: first stab at the script working with userspace and kernel (diff)
downloadwireguard-rs-3a2be07f72d02751d04285e7a92ec047c0f96412.tar.xz
wireguard-rs-3a2be07f72d02751d04285e7a92ec047c0f96412.zip
udp: use latest nix draft, which uses latest libc
Diffstat (limited to 'src')
-rw-r--r--src/interface/config.rs2
-rw-r--r--src/interface/peer_server.rs2
-rw-r--r--src/peer.rs8
-rw-r--r--src/udp/frame.rs2
-rw-r--r--src/udp/mod.rs41
5 files changed, 27 insertions, 28 deletions
diff --git a/src/interface/config.rs b/src/interface/config.rs
index 488b5bb..d8a7a9d 100644
--- a/src/interface/config.rs
+++ b/src/interface/config.rs
@@ -258,7 +258,7 @@ impl ConfigurationService {
} else {
info.allowed_ips.extend_from_slice(&peer.info.allowed_ips);
}
- info.endpoint = info.endpoint.or(peer.info.endpoint);
+ info.endpoint = info.endpoint.or(peer.info.endpoint.clone());
info.keepalive = info.keepalive.or(peer.info.keepalive);
info.psk = info.psk.or(peer.info.psk);
state.router.add_allowed_ips(&info.allowed_ips, &peer_ref);
diff --git a/src/interface/peer_server.rs b/src/interface/peer_server.rs
index 4de6eec..b541caa 100644
--- a/src/interface/peer_server.rs
+++ b/src/interface/peer_server.rs
@@ -146,7 +146,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);
}
diff --git a/src/peer.rs b/src/peer.rs
index 4899893..7115a6f 100644
--- a/src/peer.rs
+++ b/src/peer.rs
@@ -221,7 +221,7 @@ impl Peer {
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"))?;
+ let endpoint = self.info.endpoint.as_ref().ok_or_else(|| err_msg("no known peer endpoint"))?;
let mut packet = vec![0; 148];
let tai64n = Tai64n::now();
@@ -242,7 +242,7 @@ impl Peer {
None
};
- Ok((endpoint, packet, dead_index))
+ Ok((endpoint.clone(), packet, dead_index))
}
pub fn process_incoming_handshake(private_key: &[u8], packet: &Initiation) -> Result<IncompleteIncomingHandshake, Error> {
@@ -378,7 +378,7 @@ impl Peer {
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 endpoint = self.info.endpoint.as_ref().ok_or_else(|| err_msg("no known peer endpoint"))?;
let padding = PADDING_MULTIPLE - (packet.len() % PADDING_MULTIPLE);
let padded_len = packet.len() + padding;
let mut out_packet = vec![0u8; padded_len + TRANSPORT_OVERHEAD];
@@ -395,7 +395,7 @@ impl Peer {
self.tx_bytes += len as u64;
session.last_sent = Timestamp::now();
out_packet.truncate(TRANSPORT_HEADER_SIZE + len);
- Ok((endpoint, out_packet))
+ Ok((endpoint.clone(), out_packet))
}
pub fn to_config_string(&self) -> String {
diff --git a/src/udp/frame.rs b/src/udp/frame.rs
index b0f1dd4..09418bb 100644
--- a/src/udp/frame.rs
+++ b/src/udp/frame.rs
@@ -141,7 +141,7 @@ pub type PeerServerMessage = (Endpoint, Vec<u8>);
pub struct VecUdpCodec;
impl VecUdpCodec {
fn decode(&mut self, src: &Endpoint, buf: &[u8]) -> io::Result<PeerServerMessage> {
- Ok((*src, buf.to_vec()))
+ Ok((src.clone(), buf.to_vec()))
}
fn encode(&mut self, msg: PeerServerMessage, buf: &mut Vec<u8>) -> Endpoint {
diff --git a/src/udp/mod.rs b/src/udp/mod.rs
index 55fae54..7de1fb1 100644
--- a/src/udp/mod.rs
+++ b/src/udp/mod.rs
@@ -5,13 +5,11 @@ use std::net::{self, SocketAddr, SocketAddrV4, SocketAddrV6, Ipv4Addr, Ipv6Addr}
use std::os::unix::io::{AsRawFd, RawFd};
use futures::{Async, Future, Poll};
-use libc;
+use libc::{self, in_pktinfo, in6_pktinfo, in_addr, in6_addr};
use mio;
use nix::{self, errno::Errno};
use nix::sys::{uio::IoVec,
socket::{
- in6_pktinfo,
- in_pktinfo,
CmsgSpace,
ControlMessage,
InetAddr,
@@ -42,12 +40,22 @@ pub struct UdpSocket {
// but this is for simplicity because nix only offers a to_std() that returns
// `SocketAddr` from its `SockAddr`, so it makes the code cleaner with little
// performance impact.
-#[derive(Clone, Copy, Debug)]
+#[derive(Clone)]
pub enum Endpoint {
V4(SocketAddr, Option<in_pktinfo>),
V6(SocketAddr, Option<in6_pktinfo>)
}
+impl fmt::Debug for Endpoint {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match *self {
+ Endpoint::V4(addr, pktinfo) => write!(f, "Endpoint::V4({}, ...)", addr),
+ Endpoint::V6(addr, pktinfo) => write!(f, "Endpoint::V6({}, ...)", addr),
+
+ }
+ }
+}
+
impl Endpoint {
fn addr(&self) -> SocketAddr {
match *self {
@@ -84,7 +92,7 @@ impl From<SocketAddr> for Endpoint {
}
}
-#[derive(Clone, Copy, Debug)]
+#[derive(Clone)]
pub enum PktInfo {
V4(in_pktinfo),
V6(in6_pktinfo),
@@ -169,18 +177,9 @@ impl UdpSocket {
}
let cmsgs = match *target {
- Endpoint::V4(addr, Some(ref pktinfo)) => {
- trace!("sending cmsg: {:?}", pktinfo);
- vec![ControlMessage::Ipv4PacketInfo(pktinfo)]
- },
- Endpoint::V6(addr, Some(ref pktinfo)) => {
- trace!("sending cmsg: {:?}", pktinfo);
- vec![ControlMessage::Ipv6PacketInfo(pktinfo)]
- },
- _ => {
- trace!("not sending any pktinfo");
- vec![]
- }
+ Endpoint::V4(addr, Some(ref pktinfo)) => vec![ControlMessage::Ipv4PacketInfo(pktinfo)],
+ Endpoint::V6(addr, Some(ref pktinfo)) => vec![ControlMessage::Ipv6PacketInfo(pktinfo)],
+ _ => vec![]
};
let res = sendmsg(io.get_ref().as_raw_fd(),
@@ -234,11 +233,11 @@ impl UdpSocket {
match msg.cmsgs().next() {
Some(ControlMessage::Ipv4PacketInfo(info)) => {
trace!("ipv4 cmsg (\n ipi_addr: {:?},\n ipi_spec_dst: {:?},\n ipi_ifindex: {}\n)",
- Ipv4Addr::from(info.ipi_addr),
- Ipv4Addr::from(info.ipi_spec_dst),
+ Ipv4Addr::from(info.ipi_addr.s_addr),
+ Ipv4Addr::from(info.ipi_spec_dst.s_addr),
info.ipi_ifindex);
let endpoint = Endpoint::V4(addr.to_std(), Some(in_pktinfo {
- ipi_addr : [0u8; 4],
+ ipi_addr : in_addr { s_addr: 0 },
ipi_spec_dst: info.ipi_addr,
ipi_ifindex : info.ipi_ifindex,
}));
@@ -246,7 +245,7 @@ impl UdpSocket {
},
Some(ControlMessage::Ipv6PacketInfo(info)) => {
trace!("ipv6 cmsg (\n ipi6_addr: {:?},\n ipi6_ifindex: {}\n)",
- Ipv6Addr::from(info.ipi6_addr),
+ Ipv6Addr::from(info.ipi6_addr.s6_addr),
info.ipi6_ifindex);
let endpoint = Endpoint::V6(addr.to_std(), Some(*info));
Ok((msg.bytes, endpoint))