Skip to content

Commit

Permalink
fix TCP connect/bind, undo unnecessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mzohreva committed Apr 6, 2020
1 parent c63b5cb commit bb5bf68
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
3 changes: 1 addition & 2 deletions enclave-runner/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ impl Library {
usercall_ext: Option<Box<dyn UsercallExtension>>,
forward_panics: bool,
) -> Library {
let enclave = EnclaveState::library(tcss, usercall_ext, forward_panics);
Library {
enclave,
enclave: EnclaveState::library(tcss, usercall_ext, forward_panics),
address,
size,
}
Expand Down
23 changes: 5 additions & 18 deletions enclave-runner/src/usercalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::RefCell;
use std::collections::VecDeque;
use std::io::{self, ErrorKind as IoErrorKind, Read, Result as IoResult};
use std::net::ToSocketAddrs;
use std::result::Result as StdResult;
use std::str;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
Expand Down Expand Up @@ -180,7 +179,7 @@ pub trait AsyncStream: AsyncRead + AsyncWrite + 'static + Send + Sync {
impl<S: AsyncRead + AsyncWrite + Sync + Send + 'static> AsyncStream for S {}

/// AsyncListener lets an implementation implement a slightly modified form of `std::net::TcpListener::accept`.
pub trait AsyncListener: Send {
pub trait AsyncListener: 'static + Send {
/// The enclave may optionally request the local or peer addresses
/// be returned in `local_addr` or `peer_addr`, respectively.
/// If `local_addr` and/or `peer_addr` are not `None`, they will point to an empty `String`.
Expand Down Expand Up @@ -1156,16 +1155,15 @@ impl<'tcs> IOHandlerInput<'tcs> {
if let Some(stream_ext) = self
.enclave
.usercall_ext
.bind_stream(&addr, local_addr_str.as_mut())?
.bind_stream(addr, local_addr_str.as_mut())?
{
if let Some(local_addr) = local_addr {
local_addr.set(local_addr_str.unwrap().into_bytes());
}
return Ok(self.alloc_fd(AsyncFileDesc::listener(stream_ext)).await);
}

// !!! see if there's a better way
let socket = tokio::net::TcpListener::bind(&addr.to_socket_addrs()?.as_mut_slice()[0]).await?;
let socket = tokio::net::TcpListener::bind(addr).await?;
if let Some(local_addr) = local_addr {
local_addr.set(socket.local_addr()?.to_string().into_bytes());
}
Expand Down Expand Up @@ -1205,7 +1203,7 @@ impl<'tcs> IOHandlerInput<'tcs> {
let mut local_addr_str = local_addr.as_ref().map(|_| String::new());
let mut peer_addr_str = peer_addr.as_ref().map(|_| String::new());
if let Some(stream_ext) = self.enclave.usercall_ext.connect_stream(
&addr,
addr,
local_addr_str.as_mut(),
peer_addr_str.as_mut(),
).await? {
Expand All @@ -1218,18 +1216,7 @@ impl<'tcs> IOHandlerInput<'tcs> {
return Ok(self.alloc_fd(AsyncFileDesc::stream(stream_ext)).await);
}

// try to connect to all socket addresses one by one
let mut stream = None;
for socket_addr in addr.to_socket_addrs()? {
stream = Some(tokio::net::TcpStream::connect(&socket_addr).await);
if stream.as_ref().unwrap().is_ok() {
break;
}
}
let stream = match stream {
None => return Err(IoErrorKind::InvalidInput.into()),
Some(s) => s?
};
let stream = tokio::net::TcpStream::connect(addr).await?;

if let Some(local_addr) = local_addr {
match stream.local_addr() {
Expand Down
2 changes: 1 addition & 1 deletion sgxs-tools/src/sgx_detect/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ impl RunEnclaveProdWl {
let mut builder = EnclaveBuilder::new_from_memory(enclave);
builder.attributes(sig.attributes).sigstruct(sig);

let mut lib = builder.build_library(enclave_loader)?;
let lib = builder.build_library(enclave_loader)?;

unsafe {
match lib.call(!0, 0, 0, 0, 0) {
Expand Down

0 comments on commit bb5bf68

Please sign in to comment.