Skip to content

Commit b1b76f6

Browse files
committed
Cleaning up transfer_data function
1 parent 8eadc2f commit b1b76f6

File tree

1 file changed

+31
-27
lines changed
  • fortanix-vme/fortanix-vme-runner/src

1 file changed

+31
-27
lines changed

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,35 @@ impl Connection {
123123
})
124124
}
125125

126+
fn transfer_data<S: StreamConnection, D: StreamConnection>(src: &mut S, src_name: &str, dst: &mut D, dst_name: &str) -> Result<usize, IoError> {
127+
let mut buff = [0; PROXY_BUFF_SIZE];
128+
let n = src.read(&mut buff[..])?;
129+
if n > 0 {
130+
ClientConnection::log_communication(
131+
log::Level::Debug,
132+
"runner",
133+
src.local_port().unwrap_or_default(),
134+
src_name,
135+
src.peer_port().unwrap_or_default(),
136+
&str::from_utf8(&buff[0..n]).unwrap_or_default(),
137+
Direction::Left,
138+
S::protocol(),
139+
Some(MAX_MESSAGE_LEN));
140+
dst.write_all(&buff[0..n])?;
141+
ClientConnection::log_communication(
142+
log::Level::Debug,
143+
dst_name,
144+
dst.peer_port().unwrap_or_default(),
145+
"runner",
146+
dst.local_port().unwrap_or_default(),
147+
&str::from_utf8(&buff[0..n]).unwrap_or_default(),
148+
Direction::Left,
149+
D::protocol(),
150+
Some(MAX_MESSAGE_LEN));
151+
}
152+
Ok(n)
153+
}
154+
126155
/// Exchanges messages between the remote server and enclave. Returns on error, or when one of
127156
/// the connections terminated
128157
pub fn proxy(&mut self) -> Result<(), IoError> {
@@ -143,13 +172,13 @@ impl Connection {
143172
// - reflect this change on the other connection
144173
// - avoid reading from the socket again
145174
// https://doc.rust-lang.org/std/io/trait.Read.html#tymethod.read
146-
if Server::transfer_data(remote, &self.remote_name, enclave, "enclave")? == 0 {
175+
if Self::transfer_data(remote, &self.remote_name, enclave, "enclave")? == 0 {
147176
enclave.shutdown(Shutdown::Write)?;
148177
golden_set.remove(remote.as_raw_fd());
149178
}
150179
}
151180
if read_set.contains(enclave.as_raw_fd()) {
152-
if Server::transfer_data(enclave, "enclave", remote, &self.remote_name)? == 0 {
181+
if Self::transfer_data(enclave, "enclave", remote, &self.remote_name)? == 0 {
153182
remote.shutdown(Shutdown::Write)?;
154183
golden_set.remove(enclave.as_raw_fd());
155184
}
@@ -270,31 +299,6 @@ pub struct Server {
270299
}
271300

272301
impl Server {
273-
fn transfer_data<S: StreamConnection, D: StreamConnection>(src: &mut S, src_name: &str, dst: &mut D, dst_name: &str) -> Result<usize, IoError> {
274-
let mut buff = [0; PROXY_BUFF_SIZE];
275-
let n = src.read(&mut buff[..])?;
276-
if n > 0 {
277-
ClientConnection::log_communication(
278-
"runner",
279-
src.local_port().unwrap_or_default(),
280-
src_name,
281-
src.peer_port().unwrap_or_default(),
282-
&str::from_utf8(&buff[0..n]).unwrap_or_default(),
283-
Direction::Left,
284-
S::protocol());
285-
dst.write_all(&buff[0..n])?;
286-
ClientConnection::log_communication(
287-
dst_name,
288-
dst.peer_port().unwrap_or_default(),
289-
"runner",
290-
dst.local_port().unwrap_or_default(),
291-
&str::from_utf8(&buff[0..n]).unwrap_or_default(),
292-
Direction::Left,
293-
D::protocol());
294-
}
295-
Ok(n)
296-
}
297-
298302
/*
299303
* +-----------+
300304
* | remote |

0 commit comments

Comments
 (0)