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 /// Binds a TCP socket without immediately listening or connecting.
101 BindTcp {
102 socket_id: SocketId,
103 addr: SocketAddr,
104 only_v6: bool,
105 reuse_port: bool,
106 reuse_addr: bool,
107 },
108 /// Opens a UDP socket that listens on a specific IP and Port combination
109 /// Multiple servers (processes or threads) can bind to the same port if they each set
110 /// the reuse-port and-or reuse-addr flags
111 BindUdp {
112 socket_id: SocketId,
113 addr: SocketAddr,
114 reuse_port: bool,
115 reuse_addr: bool,
116 },
117 /// Creates a socket that can be used to send and receive ICMP packets
118 /// from a paritcular IP address
119 BindIcmp { socket_id: SocketId, addr: IpAddr },
120 /// Opens a TCP connection to a particular destination IP address and port
121 ConnectTcp {
122 socket_id: SocketId,
123 addr: SocketAddr,
124 peer: SocketAddr,
125 },
126 /// Performs DNS resolution for a specific hostname
127 Resolve {
128 host: String,
129 port: Option<u16>,
130 dns_server: Option<IpAddr>,
131 },
132 /// Closes the socket
133 Close,
134 /// Converts a bound TCP socket into a listening socket.
135 ListenBound,
136 /// Converts a bound TCP socket into a connected TCP stream.
137 ConnectBound { peer: SocketAddr },
138 /// Begins the process of accepting a socket and returns it later
139 BeginAccept(SocketId),
140 /// Returns the local address of this TCP listener
141 GetAddrLocal,
142 /// Returns the address (IP and Port) of the peer socket that this
143 /// is connected to
144 GetAddrPeer,
145 /// Sets how many network hops the packets are permitted for new connections
146 SetTtl(u32),
147 /// Returns the maximum number of network hops before packets are dropped
148 GetTtl,
149 /// Returns the status/state of the socket
150 GetStatus,
151 /// Determines how long the socket will remain in a TIME_WAIT
152 /// after it disconnects (only the one that initiates the close will
153 /// be in a TIME_WAIT state thus the clients should always do this rather
154 /// than the server)
155 SetLinger(Option<Duration>),
156 /// Returns how long the socket will remain in a TIME_WAIT
157 /// after it disconnects
158 GetLinger,
159 /// Tells the raw socket and its backing switch that all packets
160 /// should be received by this socket even if they are not
161 /// destined for this device
162 SetPromiscuous(bool),
163 /// Returns if the socket is running in promiscuous mode whereby it
164 /// will receive all packets even if they are not destined for the
165 /// local interface
166 GetPromiscuous,
167 /// Sets the receive buffer size which acts as a throttle for how
168 /// much data is buffered on this side of the pipe
169 SetRecvBufSize(u64),
170 /// Size of the receive buffer that holds all data that has not
171 /// yet been read
172 GetRecvBufSize,
173 /// Sets the size of the send buffer which will hold the bytes of
174 /// data while they are being sent over to the peer
175 SetSendBufSize(u64),
176 /// Size of the send buffer that holds all data that is currently
177 /// being transmitted.
178 GetSendBufSize,
179 /// When NO_DELAY is set the data that needs to be transmitted to
180 /// the peer is sent immediately rather than waiting for a bigger
181 /// batch of data, this reduces latency but increases encapsulation
182 /// overhead.
183 SetNoDelay(bool),
184 /// Indicates if the NO_DELAY flag is set which means that data
185 /// is immediately sent to the peer without waiting. This reduces
186 /// latency but increases encapsulation overhead.
187 GetNoDelay,
188 /// When KEEP_ALIVE is set the connection will periodically send
189 /// an empty data packet to the server to make sure the connection
190 /// stays alive.
191 SetKeepAlive(bool),
192 /// Indicates if the KEEP_ALIVE flag is set which means that the
193 /// socket will periodically send an empty data packet to keep
194 /// the connection alive.
195 GetKeepAlive,
196 /// When DONT_ROUTE is set the packet will be sent directly
197 /// to the interface without passing through the routing logic.
198 SetDontRoute(bool),
199 /// Indicates if the packet will pass straight through to
200 /// the interface bypassing the routing logic.
201 GetDontRoute,
202 /// Shuts down either the READER or WRITER sides of the socket
203 /// connection.
204 Shutdown(Shutdown),
205 /// Return true if the socket is closed
206 IsClosed,
207 /// Sets a flag that means that the UDP socket is able
208 /// to receive and process broadcast packets.
209 SetBroadcast(bool),
210 /// Indicates if the SO_BROADCAST flag is set which means
211 /// that the UDP socket will receive and process broadcast
212 /// packets
213 GetBroadcast,
214 /// Sets 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 IPv4 addresses
217 SetMulticastLoopV4(bool),
218 /// Gets a flag that indicates if multicast packets that
219 /// this socket is a member of will be looped back to
220 /// the sending socket. This applies to IPv4 addresses
221 GetMulticastLoopV4,
222 /// Sets a flag that indicates if multicast packets that
223 /// this socket is a member of will be looped back to
224 /// the sending socket. This applies to IPv6 addresses
225 SetMulticastLoopV6(bool),
226 /// Gets a flag that indicates if multicast packets that
227 /// this socket is a member of will be looped back to
228 /// the sending socket. This applies to IPv6 addresses
229 GetMulticastLoopV6,
230 /// Sets the TTL for IPv4 multicast packets which is the
231 /// number of network hops before the packet is dropped
232 SetMulticastTtlV4(u32),
233 /// Gets the TTL for IPv4 multicast packets which is the
234 /// number of network hops before the packet is dropped
235 GetMulticastTtlV4,
236 /// Tells this interface that it will subscribe to a
237 /// particular multicast address. This applies to IPv4 addresses
238 JoinMulticastV4 {
239 multiaddr: Ipv4Addr,
240 iface: Ipv4Addr,
241 },
242 /// Tells this interface that it will unsubscribe to a
243 /// particular multicast address. This applies to IPv4 addresses
244 LeaveMulticastV4 {
245 multiaddr: Ipv4Addr,
246 iface: Ipv4Addr,
247 },
248 /// Tells this interface that it will subscribe to a
249 /// particular multicast address. This applies to IPv6 addresses
250 JoinMulticastV6 { multiaddr: Ipv6Addr, iface: u32 },
251 /// Tells this interface that it will unsubscribe to a
252 /// particular multicast address. This applies to IPv6 addresses
253 LeaveMulticastV6 { multiaddr: Ipv6Addr, iface: u32 },
254}
255
256#[derive(Clone, Debug, Serialize, Deserialize)]
257pub enum ResponseType {
258 /// Nothing is returned (or noop)
259 None,
260 /// An error has occurred
261 Err(NetworkError),
262 /// Represents a duration of time
263 Duration(Duration),
264 /// Represents an amount (e.g. amount of bytes)
265 Amount(u64),
266 /// Returns a flag of true or false
267 Flag(bool),
268 /// List of IP addresses
269 IpAddressList(Vec<IpAddr>),
270 /// A single IP address
271 IpAddress(IpAddr),
272 /// List of socket addresses
273 SocketAddrList(Vec<SocketAddr>),
274 /// A single IP address
275 SocketAddr(SocketAddr),
276 /// Represents a MAC address
277 Mac([u8; 6]),
278 /// List of CIDR routes from a routing table
279 CidrList(Vec<IpCidr>),
280 /// List of IP routes from a routing table
281 RouteList(Vec<IpRoute>),
282 /// Reference to a socket
283 Socket(SocketId),
284 /// The TTL of a packet
285 Ttl(u32),
286 /// The status of the socket
287 Status(SocketStatus),
288}
289
290/// Message sent by the client to the server
291#[derive(Clone, Debug, Serialize, Deserialize)]
292pub enum MessageRequest {
293 Interface {
294 req: RequestType,
295 req_id: Option<u64>,
296 },
297 Socket {
298 socket: SocketId,
299 req: RequestType,
300 req_id: Option<u64>,
301 },
302 Send {
303 socket: SocketId,
304 data: Vec<u8>,
305 req_id: Option<u64>,
306 },
307 SendTo {
308 socket: SocketId,
309 data: Vec<u8>,
310 addr: SocketAddr,
311 req_id: Option<u64>,
312 },
313 Reconnect,
314}
315
316/// Message sent by the server back to a client
317#[derive(Clone, Debug, Serialize, Deserialize)]
318pub enum MessageResponse {
319 ResponseToRequest {
320 req_id: u64,
321 res: ResponseType,
322 },
323 Recv {
324 socket_id: SocketId,
325 data: Vec<u8>,
326 },
327 RecvWithAddr {
328 socket_id: SocketId,
329 data: Vec<u8>,
330 addr: SocketAddr,
331 },
332 Sent {
333 socket_id: SocketId,
334 req_id: u64,
335 amount: u64,
336 },
337 SendError {
338 socket_id: SocketId,
339 req_id: u64,
340 error: NetworkError,
341 },
342 FinishAccept {
343 socket_id: SocketId,
344 child_id: SocketId,
345 addr: SocketAddr,
346 },
347 Closed {
348 socket_id: SocketId,
349 },
350}