many fixes for bootstrap and public internet connectivity
This commit is contained in:
@@ -316,6 +316,11 @@ impl Network {
|
||||
&peer_socket_addr,
|
||||
&descriptor.local.map(|sa| sa.to_socket_addr()),
|
||||
) {
|
||||
log_net!(
|
||||
"send_data_to_existing_connection connectionless to {:?}",
|
||||
descriptor
|
||||
);
|
||||
|
||||
ph.clone()
|
||||
.send_message(data, peer_socket_addr)
|
||||
.await
|
||||
@@ -334,6 +339,8 @@ impl Network {
|
||||
|
||||
// Try to send to the exact existing connection if one exists
|
||||
if let Some(conn) = self.connection_manager().get_connection(descriptor).await {
|
||||
log_net!("send_data_to_existing_connection to {:?}", descriptor);
|
||||
|
||||
// connection exists, send over it
|
||||
conn.send(data).await.map_err(logthru_net!())?;
|
||||
|
||||
|
@@ -77,10 +77,15 @@ impl Network {
|
||||
protocol_handlers: &[Box<dyn ProtocolAcceptHandler>],
|
||||
) -> Result<Option<NetworkConnection>, String> {
|
||||
for ah in protocol_handlers.iter() {
|
||||
if let Some(nc) = ah.on_accept(stream.clone(), addr).await? {
|
||||
if let Some(nc) = ah
|
||||
.on_accept(stream.clone(), addr)
|
||||
.await
|
||||
.map_err(logthru_net!())?
|
||||
{
|
||||
return Ok(Some(nc));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
@@ -105,7 +110,7 @@ impl Network {
|
||||
let std_listener: std::net::TcpListener = socket.into();
|
||||
let listener = TcpListener::from(std_listener);
|
||||
|
||||
trace!("spawn_socket_listener: binding successful to {}", addr);
|
||||
debug!("spawn_socket_listener: binding successful to {}", addr);
|
||||
|
||||
// Create protocol handler records
|
||||
let listener_state = Arc::new(RwLock::new(ListenerState::new()));
|
||||
@@ -140,7 +145,7 @@ impl Network {
|
||||
};
|
||||
// XXX limiting
|
||||
|
||||
trace!("TCP connection from: {}", addr);
|
||||
log_net!("TCP connection from: {}", addr);
|
||||
|
||||
// Create a stream we can peek on
|
||||
let ps = AsyncPeekStream::new(tcp_stream);
|
||||
@@ -166,6 +171,7 @@ impl Network {
|
||||
|
||||
// Check is this could be TLS
|
||||
let ls = listener_state.read().clone();
|
||||
|
||||
let conn = if ls.tls_acceptor.is_some() && first_packet[0] == 0x16 {
|
||||
this.try_tls_handlers(
|
||||
ls.tls_acceptor.as_ref().unwrap(),
|
||||
@@ -178,28 +184,34 @@ impl Network {
|
||||
} else {
|
||||
this.try_handlers(ps, addr, &ls.protocol_handlers).await
|
||||
};
|
||||
|
||||
let conn = match conn {
|
||||
Ok(Some(c)) => c,
|
||||
Ok(Some(c)) => {
|
||||
log_net!("protocol handler found for {:?}: {:?}", addr, c);
|
||||
c
|
||||
}
|
||||
Ok(None) => {
|
||||
// No protocol handlers matched? drop it.
|
||||
log_net!(warn "no protocol handler for connection from {:?}", addr);
|
||||
return;
|
||||
}
|
||||
Err(_) => {
|
||||
Err(e) => {
|
||||
// Failed to negotiate connection? drop it.
|
||||
log_net!(warn "failed to negotiate connection from {:?}: {}", addr, e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Register the new connection in the connection manager
|
||||
if let Err(e) = connection_manager.on_new_connection(conn).await {
|
||||
error!("failed to register new connection: {}", e);
|
||||
log_net!(error "failed to register new connection: {}", e);
|
||||
}
|
||||
})
|
||||
.await;
|
||||
trace!("exited incoming loop for {}", addr);
|
||||
log_net!(debug "exited incoming loop for {}", addr);
|
||||
// Remove our listener state from this address if we're stopping
|
||||
this.inner.lock().listener_states.remove(&addr);
|
||||
trace!("listener state removed for {}", addr);
|
||||
log_net!(debug "listener state removed for {}", addr);
|
||||
|
||||
// If this happened our low-level listener socket probably died
|
||||
// so it's time to restart the network
|
||||
|
@@ -100,7 +100,7 @@ pub fn new_unbound_shared_tcp_socket(domain: Domain) -> Result<Socket, String> {
|
||||
let socket = Socket::new(domain, Type::STREAM, Some(Protocol::TCP))
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!("failed to create TCP socket"))?;
|
||||
if let Err(e) = socket.set_linger(None) {
|
||||
if let Err(e) = socket.set_linger(Some(core::time::Duration::from_secs(0))) {
|
||||
log_net!(error "Couldn't set TCP linger: {}", e);
|
||||
}
|
||||
if let Err(e) = socket.set_nodelay(true) {
|
||||
@@ -144,7 +144,7 @@ pub fn new_bound_first_tcp_socket(local_address: SocketAddr) -> Result<Socket, S
|
||||
let socket = Socket::new(domain, Type::STREAM, Some(Protocol::TCP))
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!("failed to create TCP socket"))?;
|
||||
if let Err(e) = socket.set_linger(None) {
|
||||
if let Err(e) = socket.set_linger(Some(core::time::Duration::from_secs(0))) {
|
||||
log_net!(error "Couldn't set TCP linger: {}", e);
|
||||
}
|
||||
if let Err(e) = socket.set_nodelay(true) {
|
||||
|
@@ -107,6 +107,7 @@ impl RawTcpProtocolHandler {
|
||||
stream: AsyncPeekStream,
|
||||
socket_addr: SocketAddr,
|
||||
) -> Result<Option<NetworkConnection>, String> {
|
||||
log_net!("TCP: on_accept_async: enter");
|
||||
let mut peekbuf: [u8; PEEK_DETECT_LEN] = [0u8; PEEK_DETECT_LEN];
|
||||
let peeklen = stream
|
||||
.peek(&mut peekbuf)
|
||||
@@ -125,7 +126,7 @@ impl RawTcpProtocolHandler {
|
||||
ProtocolNetworkConnection::RawTcp(RawTcpNetworkConnection::new(stream)),
|
||||
);
|
||||
|
||||
log_net!("on_accept_async from: {}", socket_addr);
|
||||
log_net!(debug "TCP: on_accept_async from: {}", socket_addr);
|
||||
|
||||
Ok(Some(conn))
|
||||
}
|
||||
|
@@ -127,7 +127,9 @@ impl WebsocketProtocolHandler {
|
||||
ps: AsyncPeekStream,
|
||||
socket_addr: SocketAddr,
|
||||
) -> Result<Option<NetworkConnection>, String> {
|
||||
log_net!("WS: on_accept_async: enter");
|
||||
let request_path_len = self.arc.request_path.len() + 2;
|
||||
|
||||
let mut peekbuf: Vec<u8> = vec![0u8; request_path_len];
|
||||
match io::timeout(
|
||||
Duration::from_micros(self.arc.connection_initial_timeout),
|
||||
@@ -143,6 +145,7 @@ impl WebsocketProtocolHandler {
|
||||
return Err(e).map_err(map_to_string).map_err(logthru_net!(error));
|
||||
}
|
||||
}
|
||||
|
||||
// Check for websocket path
|
||||
let matches_path = &peekbuf[0..request_path_len - 2] == self.arc.request_path.as_slice()
|
||||
&& (peekbuf[request_path_len - 2] == b' '
|
||||
@@ -150,14 +153,10 @@ impl WebsocketProtocolHandler {
|
||||
&& peekbuf[request_path_len - 1] == b' '));
|
||||
|
||||
if !matches_path {
|
||||
log_net!(
|
||||
"not websocket: request_path: {} peekbuf:{}",
|
||||
std::str::from_utf8(&self.arc.request_path).unwrap(),
|
||||
std::str::from_utf8(&peekbuf).unwrap()
|
||||
);
|
||||
log_net!("WS: not websocket");
|
||||
return Ok(None);
|
||||
}
|
||||
log_net!("found websocket");
|
||||
log_net!("WS: found websocket");
|
||||
|
||||
let ws_stream = accept_async(ps)
|
||||
.await
|
||||
@@ -182,6 +181,8 @@ impl WebsocketProtocolHandler {
|
||||
ProtocolNetworkConnection::WsAccepted(WebsocketNetworkConnection::new(ws_stream)),
|
||||
);
|
||||
|
||||
log_net!(debug "{}: on_accept_async from: {}", if self.arc.tls { "WSS" } else { "WS" }, socket_addr);
|
||||
|
||||
Ok(Some(conn))
|
||||
}
|
||||
|
||||
|
@@ -345,7 +345,11 @@ impl NetworkInterfaces {
|
||||
if changed {
|
||||
self.cache_best_addresses();
|
||||
|
||||
trace!("NetworkInterfaces refreshed: {:#?}?", self);
|
||||
//trace!("NetworkInterfaces refreshed: {:#?}?", self);
|
||||
trace!(
|
||||
"NetworkInterfaces refreshed: {:#?}?",
|
||||
self.interface_address_cache
|
||||
);
|
||||
}
|
||||
Ok(changed)
|
||||
}
|
||||
|
Reference in New Issue
Block a user