1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
use super::super::udp::*;
use super::super::Endpoint;
use std::io;
use std::net::{SocketAddr, UdpSocket};
use std::sync::Arc;
#[derive(Clone)]
pub struct LinuxUDP(Arc<UdpSocket>);
pub struct LinuxOwner(Arc<UdpSocket>);
impl Endpoint for SocketAddr {
fn clear_src(&mut self) {}
fn from_address(addr: SocketAddr) -> Self {
addr
}
fn into_address(&self) -> SocketAddr {
*self
}
}
impl Reader<SocketAddr> for LinuxUDP {
type Error = io::Error;
fn read(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr), Self::Error> {
self.0.recv_from(buf)
}
}
impl Writer<SocketAddr> for LinuxUDP {
type Error = io::Error;
fn write(&self, buf: &[u8], dst: &SocketAddr) -> Result<(), Self::Error> {
self.0.send_to(buf, dst)?;
Ok(())
}
}
impl Owner for LinuxOwner {
type Error = io::Error;
fn get_port(&self) -> u16 {
self.0.local_addr().unwrap().port() // todo handle
}
fn get_fwmark(&self) -> Option<u32> {
None
}
fn set_fwmark(&mut self, _value: Option<u32>) -> Result<(), Self::Error> {
Ok(())
}
}
impl Drop for LinuxOwner {
fn drop(&mut self) {
// TODO: close udp bind
}
}
impl UDP for LinuxUDP {
type Error = io::Error;
type Endpoint = SocketAddr;
type Reader = Self;
type Writer = Self;
}
impl PlatformUDP for LinuxUDP {
type Owner = LinuxOwner;
fn bind(port: u16) -> Result<(Vec<Self::Reader>, Self::Writer, Self::Owner), Self::Error> {
let socket = UdpSocket::bind(format!("0.0.0.0:{}", port))?;
let socket = Arc::new(socket);
Ok((
vec![LinuxUDP(socket.clone())],
LinuxUDP(socket.clone()),
LinuxOwner(socket),
))
}
}
|