aboutsummaryrefslogtreecommitdiffstats
path: root/src/udp/mod.rs
diff options
context:
space:
mode:
authorJake McGinty <me@jake.su>2018-04-16 18:43:52 -0700
committerJake McGinty <me@jake.su>2018-04-22 14:08:41 -0700
commit0cc9ecc94a31ecca99c334c1b06df70f650ec82b (patch)
tree1f429f220a5888b79596f82dc3a63f45454eed13 /src/udp/mod.rs
parentudp: return back "magic" endpoint in recv_from (diff)
downloadwireguard-rs-0cc9ecc94a31ecca99c334c1b06df70f650ec82b.tar.xz
wireguard-rs-0cc9ecc94a31ecca99c334c1b06df70f650ec82b.zip
global: SocketAddr -> Endpoint
Diffstat (limited to '')
-rw-r--r--src/udp/mod.rs29
1 files changed, 20 insertions, 9 deletions
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"))