virtual_net/
composite.rs

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use std::net::SocketAddr;
use std::task::{Context, Poll};

use crate::{Ipv4Addr, Ipv6Addr, NetworkError, VirtualIoSource, VirtualTcpListener};
use derivative::Derivative;
use virtual_mio::ArcInterestHandler;

#[derive(Derivative)]
#[derivative(Debug)]
pub struct CompositeTcpListener {
    #[derivative(Debug = "ignore")]
    ports: Vec<Box<dyn VirtualTcpListener + Sync>>,
}

impl CompositeTcpListener {
    pub fn new() -> Self {
        Self { ports: Vec::new() }
    }

    pub fn add_port(&mut self, port: Box<dyn VirtualTcpListener + Sync>) {
        self.ports.push(port);
    }
}

impl Default for CompositeTcpListener {
    fn default() -> Self {
        Self::new()
    }
}

impl VirtualIoSource for CompositeTcpListener {
    fn remove_handler(&mut self) {
        for port in self.ports.iter_mut() {
            port.remove_handler();
        }
    }

    fn poll_read_ready(&mut self, cx: &mut Context<'_>) -> Poll<crate::Result<usize>> {
        for port in self.ports.iter_mut() {
            if let Poll::Ready(ready) = port.poll_read_ready(cx) {
                return Poll::Ready(ready);
            }
        }
        Poll::Pending
    }

    fn poll_write_ready(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<crate::Result<usize>> {
        for port in self.ports.iter_mut() {
            if let Poll::Ready(ready) = port.poll_write_ready(cx) {
                return Poll::Ready(ready);
            }
        }
        Poll::Pending
    }
}

impl VirtualTcpListener for CompositeTcpListener {
    fn try_accept(
        &mut self,
    ) -> crate::Result<(Box<dyn crate::VirtualTcpSocket + Sync>, SocketAddr)> {
        let mut ret = NetworkError::Unsupported;
        for port in self.ports.iter_mut() {
            match port.try_accept() {
                Ok(ret) => return Ok(ret),
                Err(err) => {
                    ret = err;
                }
            }
        }
        Err(ret)
    }

    fn set_handler(
        &mut self,
        handler: Box<dyn crate::InterestHandler + Send + Sync>,
    ) -> crate::Result<()> {
        let handler = ArcInterestHandler::new(handler);
        for port in self.ports.iter_mut() {
            port.set_handler(Box::new(handler.clone()))?;
        }
        Ok(())
    }

    fn addr_local(&self) -> crate::Result<SocketAddr> {
        if self.ports.len() > 1 {
            let addr = self.ports.first().unwrap().addr_local()?;
            if addr.is_ipv4() {
                Ok(SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), addr.port()))
            } else {
                Ok(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), addr.port()))
            }
        } else if let Some(addr) = self.ports.first() {
            addr.addr_local()
        } else {
            Err(NetworkError::Unsupported)
        }
    }

    fn set_ttl(&mut self, ttl: u8) -> crate::Result<()> {
        for port in self.ports.iter_mut() {
            match port.set_ttl(ttl) {
                Ok(()) | Err(NetworkError::Unsupported) => {}
                Err(err) => return Err(err),
            }
        }
        Ok(())
    }

    fn ttl(&self) -> crate::Result<u8> {
        let mut ret = u8::MAX;
        for port in self.ports.iter() {
            if let Ok(ttl) = port.ttl() {
                ret = ret.min(ttl)
            }
        }
        Ok(ret)
    }
}