virtual_net/
meta.rs

1use std::net::IpAddr;
2use std::net::Ipv4Addr;
3use std::net::Ipv6Addr;
4use std::net::SocketAddr;
5use std::time::Duration;
6
7use serde::{Deserialize, Serialize};
8
9pub use super::IpCidr;
10pub use super::IpRoute;
11pub use super::NetworkError;
12pub use super::SocketStatus;
13pub use super::StreamSecurity;
14
15/// Represents a socket ID
16#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct SocketId(u64);
18
19impl From<u64> for SocketId {
20    fn from(value: u64) -> Self {
21        Self(value)
22    }
23}
24
25#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
26pub enum FrameSerializationFormat {
27    Bincode,
28    #[cfg(feature = "json")]
29    Json,
30    #[cfg(feature = "messagepack")]
31    MessagePack,
32    #[cfg(feature = "cbor")]
33    Cbor,
34}
35
36/// Possible values which can be passed to the `TcpStream::shutdown` method.
37#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
38pub enum Shutdown {
39    /// Shut down the reading portion of the stream.
40    Read,
41    /// Shut down the writing portion of the stream.
42    Write,
43    /// Shut down both the reading and writing portions of the stream.
44    Both,
45}
46
47#[derive(Clone, Debug, Serialize, Deserialize)]
48pub enum RequestType {
49    /// Bridges this local network with a remote network, which is required in
50    /// order to make lower level networking calls (such as UDP/TCP)
51    Bridge {
52        network: String,
53        access_token: String,
54        security: StreamSecurity,
55    },
56    /// Flushes all the data by ensuring a full round trip is completed
57    Flush,
58    /// Disconnects from the remote network essentially unbridging it
59    Unbridge,
60    /// Acquires an IP address on the network and configures the routing tables
61    DhcpAcquire,
62    /// Adds a static IP address to the interface with a netmask prefix
63    IpAdd { ip: IpAddr, prefix: u8 },
64    /// Removes a static (or dynamic) IP address from the interface
65    IpRemove(IpAddr),
66    /// Clears all the assigned IP addresses for this interface
67    IpClear,
68    /// Lists all the IP addresses currently assigned to this interface
69    GetIpList,
70    /// Returns the hardware MAC address for this interface
71    GetMac,
72    /// Adds a default gateway to the routing table
73    GatewaySet(IpAddr),
74    /// Adds a specific route to the routing table
75    RouteAdd {
76        cidr: IpCidr,
77        via_router: IpAddr,
78        preferred_until: Option<Duration>,
79        expires_at: Option<Duration>,
80    },
81    /// Removes a routing rule from the routing table
82    RouteRemove(IpAddr),
83    /// Clears the routing table for this interface
84    RouteClear,
85    /// Lists all the routes defined in the routing table for this interface
86    GetRouteList,
87    /// Creates a low level socket that can read and write Ethernet packets
88    /// directly to the interface
89    BindRaw(SocketId),
90    /// Lists for TCP connections on a specific IP and Port combination
91    /// Multiple servers (processes or threads) can bind to the same port if they each set
92    /// the reuse-port and-or reuse-addr flags
93    ListenTcp {
94        socket_id: SocketId,
95        addr: SocketAddr,
96        only_v6: bool,
97        reuse_port: bool,
98        reuse_addr: bool,
99    },
100    /// Opens a UDP socket that listens on a specific IP and Port combination
101    /// Multiple servers (processes or threads) can bind to the same port if they each set
102    /// the reuse-port and-or reuse-addr flags
103    BindUdp {
104        socket_id: SocketId,
105        addr: SocketAddr,
106        reuse_port: bool,
107        reuse_addr: bool,
108    },
109    /// Creates a socket that can be used to send and receive ICMP packets
110    /// from a paritcular IP address
111    BindIcmp { socket_id: SocketId, addr: IpAddr },
112    /// Opens a TCP connection to a particular destination IP address and port
113    ConnectTcp {
114        socket_id: SocketId,
115        addr: SocketAddr,
116        peer: SocketAddr,
117    },
118    /// Performs DNS resolution for a specific hostname
119    Resolve {
120        host: String,
121        port: Option<u16>,
122        dns_server: Option<IpAddr>,
123    },
124    /// Closes the socket
125    Close,
126    /// Begins the process of accepting a socket and returns it later
127    BeginAccept(SocketId),
128    /// Returns the local address of this TCP listener
129    GetAddrLocal,
130    /// Returns the address (IP and Port) of the peer socket that this
131    /// is conencted to
132    GetAddrPeer,
133    /// Sets how many network hops the packets are permitted for new connections
134    SetTtl(u32),
135    /// Returns the maximum number of network hops before packets are dropped
136    GetTtl,
137    /// Returns the status/state of the socket
138    GetStatus,
139    /// Determines how long the socket will remain in a TIME_WAIT
140    /// after it disconnects (only the one that initiates the close will
141    /// be in a TIME_WAIT state thus the clients should always do this rather
142    /// than the server)
143    SetLinger(Option<Duration>),
144    /// Returns how long the socket will remain in a TIME_WAIT
145    /// after it disconnects
146    GetLinger,
147    /// Tells the raw socket and its backing switch that all packets
148    /// should be received by this socket even if they are not
149    /// destined for this device
150    SetPromiscuous(bool),
151    /// Returns if the socket is running in promiscuous mode whereby it
152    /// will receive all packets even if they are not destined for the
153    /// local interface
154    GetPromiscuous,
155    /// Sets the receive buffer size which acts as a trottle for how
156    /// much data is buffered on this side of the pipe
157    SetRecvBufSize(u64),
158    /// Size of the receive buffer that holds all data that has not
159    /// yet been read
160    GetRecvBufSize,
161    /// Sets the size of the send buffer which will hold the bytes of
162    /// data while they are being sent over to the peer
163    SetSendBufSize(u64),
164    /// Size of the send buffer that holds all data that is currently
165    /// being transmitted.
166    GetSendBufSize,
167    /// When NO_DELAY is set the data that needs to be transmitted to
168    /// the peer is sent immediately rather than waiting for a bigger
169    /// batch of data, this reduces latency but increases encapsulation
170    /// overhead.
171    SetNoDelay(bool),
172    /// Indicates if the NO_DELAY flag is set which means that data
173    /// is immediately sent to the peer without waiting. This reduces
174    /// latency but increases encapsulation overhead.
175    GetNoDelay,
176    /// When KEEP_ALIVE is set the connection will periodically send
177    /// an empty data packet to the server to make sure the connection
178    /// stays alive.
179    SetKeepAlive(bool),
180    /// Indicates if the KEEP_ALIVE flag is set which means that the
181    /// socket will periodically send an empty data packet to keep
182    /// the connection alive.
183    GetKeepAlive,
184    /// When DONT_ROUTE is set the packet will be sent directly
185    /// to the interface without passing through the routing logic.
186    SetDontRoute(bool),
187    /// Indicates if the packet will pass straight through to
188    /// the interface bypassing the routing logic.
189    GetDontRoute,
190    /// Shuts down either the READER or WRITER sides of the socket
191    /// connection.
192    Shutdown(Shutdown),
193    /// Return true if the socket is closed
194    IsClosed,
195    /// Sets a flag that means that the UDP socket is able
196    /// to receive and process broadcast packets.
197    SetBroadcast(bool),
198    /// Indicates if the SO_BROADCAST flag is set which means
199    /// that the UDP socket will receive and process broadcast
200    /// packets
201    GetBroadcast,
202    /// Sets a flag that indicates if multicast packets that
203    /// this socket is a member of will be looped back to
204    /// the sending socket. This applies to IPv4 addresses
205    SetMulticastLoopV4(bool),
206    /// Gets a flag that indicates if multicast packets that
207    /// this socket is a member of will be looped back to
208    /// the sending socket. This applies to IPv4 addresses
209    GetMulticastLoopV4,
210    /// Sets a flag that indicates if multicast packets that
211    /// this socket is a member of will be looped back to
212    /// the sending socket. This applies to IPv6 addresses
213    SetMulticastLoopV6(bool),
214    /// Gets a flag that indicates if multicast packets that
215    /// this socket is a member of will be looped back to
216    /// the sending socket. This applies to IPv6 addresses
217    GetMulticastLoopV6,
218    /// Sets the TTL for IPv4 multicast packets which is the
219    /// number of network hops before the packet is dropped
220    SetMulticastTtlV4(u32),
221    /// Gets the TTL for IPv4 multicast packets which is the
222    /// number of network hops before the packet is dropped
223    GetMulticastTtlV4,
224    /// Tells this interface that it will subscribe to a
225    /// particular multicast address. This applies to IPv4 addresses
226    JoinMulticastV4 {
227        multiaddr: Ipv4Addr,
228        iface: Ipv4Addr,
229    },
230    /// Tells this interface that it will unsubscribe to a
231    /// particular multicast address. This applies to IPv4 addresses
232    LeaveMulticastV4 {
233        multiaddr: Ipv4Addr,
234        iface: Ipv4Addr,
235    },
236    /// Tells this interface that it will subscribe to a
237    /// particular multicast address. This applies to IPv6 addresses
238    JoinMulticastV6 { multiaddr: Ipv6Addr, iface: u32 },
239    /// Tells this interface that it will unsubscribe to a
240    /// particular multicast address. This applies to IPv6 addresses
241    LeaveMulticastV6 { multiaddr: Ipv6Addr, iface: u32 },
242}
243
244#[derive(Clone, Debug, Serialize, Deserialize)]
245pub enum ResponseType {
246    /// Nothing is returned (or noop)
247    None,
248    /// An error has occurred
249    Err(NetworkError),
250    /// Represents a duration of time
251    Duration(Duration),
252    /// Represents an amount (e.g. amount of bytes)
253    Amount(u64),
254    /// Returns a flag of true or false
255    Flag(bool),
256    /// List of IP addresses
257    IpAddressList(Vec<IpAddr>),
258    /// A single IP address
259    IpAddress(IpAddr),
260    /// List of socket addresses
261    SocketAddrList(Vec<SocketAddr>),
262    /// A single IP address
263    SocketAddr(SocketAddr),
264    /// Represents a MAC address
265    Mac([u8; 6]),
266    /// List of CIDR routes from a routing table
267    CidrList(Vec<IpCidr>),
268    /// List of IP routes from a routing table
269    RouteList(Vec<IpRoute>),
270    /// Reference to a socket
271    Socket(SocketId),
272    /// The TTL of a packet
273    Ttl(u32),
274    /// The status of the socket
275    Status(SocketStatus),
276}
277
278/// Message sent by the client to the server
279#[derive(Clone, Debug, Serialize, Deserialize)]
280pub enum MessageRequest {
281    Interface {
282        req: RequestType,
283        req_id: Option<u64>,
284    },
285    Socket {
286        socket: SocketId,
287        req: RequestType,
288        req_id: Option<u64>,
289    },
290    Send {
291        socket: SocketId,
292        data: Vec<u8>,
293        req_id: Option<u64>,
294    },
295    SendTo {
296        socket: SocketId,
297        data: Vec<u8>,
298        addr: SocketAddr,
299        req_id: Option<u64>,
300    },
301    Reconnect,
302}
303
304/// Message sent by the server back to a client
305#[derive(Clone, Debug, Serialize, Deserialize)]
306pub enum MessageResponse {
307    ResponseToRequest {
308        req_id: u64,
309        res: ResponseType,
310    },
311    Recv {
312        socket_id: SocketId,
313        data: Vec<u8>,
314    },
315    RecvWithAddr {
316        socket_id: SocketId,
317        data: Vec<u8>,
318        addr: SocketAddr,
319    },
320    Sent {
321        socket_id: SocketId,
322        req_id: u64,
323        amount: u64,
324    },
325    SendError {
326        socket_id: SocketId,
327        req_id: u64,
328        error: NetworkError,
329    },
330    FinishAccept {
331        socket_id: SocketId,
332        child_id: SocketId,
333        addr: SocketAddr,
334    },
335    Closed {
336        socket_id: SocketId,
337    },
338}