Skip to content

Commit 61a6ab1

Browse files
committed
Storing TCP connection info
1 parent c203721 commit 61a6ab1

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fortanix-vme/fortanix-vme-runner/src/lib.rs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,56 @@ impl Listener {
8989
}
9090
}
9191

92+
#[derive(Debug)]
93+
struct Connection {
94+
// Preliminary work for PLAT-367
95+
#[allow(dead_code)]
96+
remote: Addr,
97+
// Preliminary work for PLAT-367
98+
#[allow(dead_code)]
99+
runner: Addr,
100+
}
101+
102+
impl Connection {
103+
fn from_tcp_stream(stream: &TcpStream) -> Self {
104+
let tcp_remote = stream.peer_addr().unwrap().into();
105+
let tcp_runner = stream.local_addr().unwrap().into();
106+
Connection {
107+
remote: tcp_remote,
108+
runner: tcp_runner,
109+
}
110+
}
111+
}
112+
113+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
114+
struct ConnectionKey {
115+
enclave: VsockAddr,
116+
runner: VsockAddr,
117+
}
118+
119+
impl ConnectionKey {
120+
fn from_vsock_stream(runner_enclave: &VsockStream<Std>) -> Self {
121+
let runner_cid = runner_enclave.local_addr().unwrap().cid();
122+
let runner_port = runner_enclave.local_addr().unwrap().port();
123+
let enclave_cid = runner_enclave.peer_addr().unwrap().cid();
124+
let enclave_port = runner_enclave.peer_addr().unwrap().port();
125+
Self::connection_key(enclave_cid, enclave_port, runner_cid, runner_port)
126+
}
127+
128+
fn from_addresses(enclave: VsockAddr, runner: VsockAddr) -> Self {
129+
ConnectionKey {
130+
enclave,
131+
runner,
132+
}
133+
}
134+
135+
fn connection_key(enclave_cid: u32, enclave_port: u32, runner_cid: u32, runner_port: u32) -> Self {
136+
let enclave = VsockAddr::new(enclave_cid, enclave_port);
137+
let runner = VsockAddr::new(runner_cid, runner_port);
138+
Self::from_addresses(enclave, runner)
139+
}
140+
}
141+
92142
pub struct Server {
93143
command_listener: Mutex<VsockListener>,
94144
/// Tracks information about TCP sockets that are currently listening for new connections. For
@@ -97,6 +147,7 @@ pub struct Server {
97147
/// connection. It then locates the ListenerInfo and finds the information it needs to set up a
98148
/// new vsock connection to the enclave
99149
listeners: Mutex<FnvHashMap<VsockAddr, Arc<Mutex<Listener>>>>,
150+
connections: Mutex<FnvHashMap<ConnectionKey, Arc<Mutex<Connection>>>>,
100151
}
101152

102153
impl Server {
@@ -208,8 +259,14 @@ impl Server {
208259
// Wait for incoming connection from enclave
209260
let (mut proxy, _proxy_addr) = proxy_server.accept()?;
210261

262+
// Store connection info
263+
let k = self.add_connection(&proxy, &remote_socket);
264+
211265
// Pass messages between remote server <-> enclave
212266
Self::proxy_connection((&mut remote_socket, remote_name), (&mut proxy, "proxy"));
267+
268+
// Remove connection info
269+
self.remove_connection(&k);
213270
Ok(())
214271
}
215272

@@ -221,6 +278,31 @@ impl Server {
221278
self.listeners.lock().unwrap().get(&addr).cloned()
222279
}
223280

281+
// Preliminary work for PLAT-367
282+
#[allow(dead_code)]
283+
fn connection(&self, enclave: VsockAddr, runner: VsockAddr) -> Option<Arc<Mutex<Connection>>> {
284+
let k = ConnectionKey::from_addresses(enclave, runner);
285+
self.connections
286+
.lock()
287+
.unwrap()
288+
.get(&k)
289+
.cloned()
290+
}
291+
292+
fn add_connection(&self, runner_enclave: &VsockStream<Std>, runner_remote: &TcpStream) -> ConnectionKey {
293+
let k = ConnectionKey::from_vsock_stream(runner_enclave);
294+
let info = Connection::from_tcp_stream(runner_remote);
295+
self.connections.lock().unwrap().insert(k.clone(), Arc::new(Mutex::new(info)));
296+
k
297+
}
298+
299+
fn remove_connection(&self, k: &ConnectionKey) {
300+
self.connections
301+
.lock()
302+
.unwrap()
303+
.remove(&k);
304+
}
305+
224306
/*
225307
* +-----------+
226308
* | remote |
@@ -272,10 +354,11 @@ impl Server {
272354
match listener.listener.accept() {
273355
Ok((mut conn, peer)) => {
274356
let vsock = Vsock::new::<Std>()?;
357+
let runner_addr = vsock.addr::<Std>()?;
275358
let response = Response::IncomingConnection{
276359
local: conn.local_addr()?.into(),
277360
peer: peer.into(),
278-
proxy_port: vsock.addr::<Std>()?.port(),
361+
proxy_port: runner_addr.port(),
279362
};
280363
Self::log_communication(
281364
"runner",
@@ -288,7 +371,9 @@ impl Server {
288371
enclave.write(&serde_cbor::ser::to_vec(&response).unwrap())?;
289372
let _ = thread::Builder::new().spawn(move || {
290373
let mut proxy = vsock.connect_with_cid_port(enclave_addr.cid(), enclave_addr.port()).unwrap();
374+
//let k = self.add_connection(&proxy, &conn);
291375
Self::proxy_connection((&mut conn, "remote"), (&mut proxy, "proxy"));
376+
//self.remove_connection(&k);
292377
});
293378
Ok(())
294379
},
@@ -350,6 +435,7 @@ impl Server {
350435
Ok(Server {
351436
command_listener: Mutex::new(command_listener),
352437
listeners: Mutex::new(FnvHashMap::default()),
438+
connections: Mutex::new(FnvHashMap::default()),
353439
})
354440
}
355441

0 commit comments

Comments
 (0)