more log refactor
This commit is contained in:
@@ -663,14 +663,15 @@ impl Network {
|
||||
let peer_socket_addr = descriptor
|
||||
.remote
|
||||
.to_socket_addr()
|
||||
.map_err(|_| "unable to get socket address".to_owned())?;
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error))?;
|
||||
if let Some(ph) =
|
||||
self.find_best_udp_protocol_handler(&peer_socket_addr, &descriptor.local)
|
||||
{
|
||||
ph.clone()
|
||||
.send_message(data, peer_socket_addr)
|
||||
.await
|
||||
.map_err(|_| "unable to send udp message".to_owned())?;
|
||||
.map_err(logthru_net!())?;
|
||||
// Data was consumed
|
||||
return Ok(None);
|
||||
}
|
||||
@@ -683,11 +684,7 @@ impl Network {
|
||||
.get_connection(descriptor)
|
||||
{
|
||||
// connection exists, send over it
|
||||
entry
|
||||
.conn
|
||||
.send(data)
|
||||
.await
|
||||
.map_err(|_| "failed to send tcp or ws message".to_owned())?;
|
||||
entry.conn.send(data).await.map_err(logthru_net!())?;
|
||||
|
||||
// Data was consumed
|
||||
return Ok(None);
|
||||
@@ -706,24 +703,22 @@ impl Network {
|
||||
) -> Result<(), String> {
|
||||
match &dial_info {
|
||||
DialInfo::UDP(_) => {
|
||||
let peer_socket_addr = dial_info
|
||||
.to_socket_addr()
|
||||
.map_err(|_| "failed to resolve dial info for UDP dial info".to_owned())?;
|
||||
let peer_socket_addr = dial_info.to_socket_addr().map_err(logthru_net!())?;
|
||||
|
||||
RawUdpProtocolHandler::send_unbound_message(data, peer_socket_addr)
|
||||
.await
|
||||
.map_err(|_| "failed to send unbound message to UDP dial info".to_owned())
|
||||
.map_err(logthru_net!())
|
||||
}
|
||||
DialInfo::TCP(_) => {
|
||||
let peer_socket_addr = dial_info
|
||||
.to_socket_addr()
|
||||
.map_err(|_| "failed to resolve dial info for TCP dial info".to_owned())?;
|
||||
let peer_socket_addr = dial_info.to_socket_addr().map_err(logthru_net!())?;
|
||||
RawTcpProtocolHandler::send_unbound_message(data, peer_socket_addr)
|
||||
.await
|
||||
.map_err(|_| "failed to connect to TCP dial info".to_owned())
|
||||
.map_err(logthru_net!())
|
||||
}
|
||||
DialInfo::WS(_) => Err("WS protocol does not support unbound messages".to_owned()),
|
||||
DialInfo::WSS(_) => Err("WSS protocol does not support unbound messages".to_owned()),
|
||||
DialInfo::WS(_) => Err("WS protocol does not support unbound messages".to_owned())
|
||||
.map_err(logthru_net!(error)),
|
||||
DialInfo::WSS(_) => Err("WSS protocol does not support unbound messages".to_owned())
|
||||
.map_err(logthru_net!(error)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -736,38 +731,33 @@ impl Network {
|
||||
|
||||
let conn = match &dial_info {
|
||||
DialInfo::UDP(_) => {
|
||||
let peer_socket_addr = dial_info
|
||||
.to_socket_addr()
|
||||
.map_err(|_| "failed to resolve dial info for UDP dial info".to_owned())?;
|
||||
let peer_socket_addr = dial_info.to_socket_addr().map_err(logthru_net!())?;
|
||||
if let Some(ph) = self.find_best_udp_protocol_handler(&peer_socket_addr, &None) {
|
||||
return ph
|
||||
.send_message(data, peer_socket_addr)
|
||||
.await
|
||||
.map_err(|_| "failed to send message to UDP dial info".to_owned());
|
||||
.map_err(logthru_net!());
|
||||
} else {
|
||||
return Err("no appropriate UDP protocol handler for dial_info".to_owned());
|
||||
return Err("no appropriate UDP protocol handler for dial_info".to_owned())
|
||||
.map_err(logthru_net!(error));
|
||||
}
|
||||
}
|
||||
DialInfo::TCP(_) => {
|
||||
let peer_socket_addr = dial_info
|
||||
.to_socket_addr()
|
||||
.map_err(|_| "failed to resolve dial info for TCP dial info".to_owned())?;
|
||||
let peer_socket_addr = dial_info.to_socket_addr().map_err(logthru_net!())?;
|
||||
let some_local_addr = self.find_best_tcp_local_address(&peer_socket_addr);
|
||||
RawTcpProtocolHandler::connect(network_manager, some_local_addr, peer_socket_addr)
|
||||
.await
|
||||
.map_err(|_| "failed to connect to TCP dial info".to_owned())?
|
||||
.map_err(logthru_net!())?
|
||||
}
|
||||
DialInfo::WS(_) => WebsocketProtocolHandler::connect(network_manager, dial_info)
|
||||
.await
|
||||
.map_err(|_| "failed to connect to WS dial info".to_owned())?,
|
||||
.map_err(logthru_net!(error))?,
|
||||
DialInfo::WSS(_) => WebsocketProtocolHandler::connect(network_manager, dial_info)
|
||||
.await
|
||||
.map_err(|_| "failed to connect to WSS dial info".to_owned())?,
|
||||
.map_err(logthru_net!(error))?,
|
||||
};
|
||||
|
||||
conn.send(data)
|
||||
.await
|
||||
.map_err(|_| "failed to send data to dial info".to_owned())
|
||||
conn.send(data).await.map_err(logthru_net!(error))
|
||||
}
|
||||
|
||||
pub async fn send_data(&self, node_ref: NodeRef, data: Vec<u8>) -> Result<(), String> {
|
||||
|
@@ -210,7 +210,7 @@ impl RawTcpProtocolHandler {
|
||||
socket
|
||||
.connect(&remote_socket2_addr)
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!("addr={}", remote_socket_addr))?;
|
||||
.map_err(logthru_net!(error "addr={}", remote_socket_addr))?;
|
||||
let std_stream: std::net::TcpStream = socket.into();
|
||||
let ts = TcpStream::from(std_stream);
|
||||
|
||||
|
@@ -63,10 +63,10 @@ impl RawUdpProtocolHandler {
|
||||
|
||||
pub async fn send_message(&self, data: Vec<u8>, socket_addr: SocketAddr) -> Result<(), String> {
|
||||
if data.len() > MAX_MESSAGE_SIZE {
|
||||
return Err("sending too large UDP message".to_owned());
|
||||
return Err("sending too large UDP message".to_owned()).map_err(logthru_net!(error));
|
||||
}
|
||||
|
||||
trace!(
|
||||
log_net!(
|
||||
"sending UDP message of length {} to {}",
|
||||
data.len(),
|
||||
socket_addr
|
||||
@@ -76,9 +76,11 @@ impl RawUdpProtocolHandler {
|
||||
let len = socket
|
||||
.send_to(&data, socket_addr)
|
||||
.await
|
||||
.map_err(|e| format!("{}", e))?;
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error "failed udp send: addr={}", socket_addr))?;
|
||||
|
||||
if len != data.len() {
|
||||
Err("UDP partial send".to_owned())
|
||||
Err("UDP partial send".to_owned()).map_err(logthru_net!(error))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
@@ -89,10 +91,10 @@ impl RawUdpProtocolHandler {
|
||||
socket_addr: SocketAddr,
|
||||
) -> Result<(), String> {
|
||||
if data.len() > MAX_MESSAGE_SIZE {
|
||||
return Err("sending too large unbound UDP message".to_owned());
|
||||
return Err("sending too large unbound UDP message".to_owned())
|
||||
.map_err(logthru_net!(error));
|
||||
}
|
||||
xxx continue here
|
||||
trace!(
|
||||
log_net!(
|
||||
"sending unbound message of length {} to {}",
|
||||
data.len(),
|
||||
socket_addr
|
||||
@@ -107,13 +109,15 @@ impl RawUdpProtocolHandler {
|
||||
};
|
||||
let socket = UdpSocket::bind(local_socket_addr)
|
||||
.await
|
||||
.map_err(|e| format!("{}", e))?;
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error "failed to bind unbound udp socket"))?;
|
||||
let len = socket
|
||||
.send_to(&data, socket_addr)
|
||||
.await
|
||||
.map_err(|e| format!("{}", e))?;
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error "failed unbound udp send: addr={}", socket_addr))?;
|
||||
if len != data.len() {
|
||||
Err("UDP partial unbound send".to_owned())
|
||||
Err("UDP partial unbound send".to_owned()).map_err(logthru_net!(error))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
@@ -101,6 +101,7 @@ where
|
||||
.send(Message::binary(message))
|
||||
.await
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error "failed to send websocket message"))
|
||||
})
|
||||
}
|
||||
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, String>> {
|
||||
@@ -112,17 +113,18 @@ where
|
||||
let out = match inner.ws_stream.next().await {
|
||||
Some(Ok(Message::Binary(v))) => v,
|
||||
Some(Ok(_)) => {
|
||||
return Err("Unexpected WS message type".to_owned());
|
||||
return Err("Unexpected WS message type".to_owned())
|
||||
.map_err(logthru_net!(error));
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
return Err(e.to_string());
|
||||
return Err(e.to_string()).map_err(logthru_net!(error));
|
||||
}
|
||||
None => {
|
||||
return Err("WS stream closed".to_owned());
|
||||
return Err("WS stream closed".to_owned()).map_err(logthru_net!());
|
||||
}
|
||||
};
|
||||
if out.len() > MAX_MESSAGE_SIZE {
|
||||
Err("sending too large WS message".to_owned())
|
||||
Err("sending too large WS message".to_owned()).map_err(logthru_net!(error))
|
||||
} else {
|
||||
Ok(out)
|
||||
}
|
||||
@@ -189,7 +191,10 @@ impl WebsocketProtocolHandler {
|
||||
{
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
return Err(format!("failed to peek stream: {:?}", e));
|
||||
if e.kind() == io::ErrorKind::TimedOut {
|
||||
return Err(e).map_err(map_to_string).map_err(logthru_net!());
|
||||
}
|
||||
return Err(e).map_err(map_to_string).map_err(logthru_net!(error));
|
||||
}
|
||||
}
|
||||
// Check for websocket path
|
||||
@@ -261,13 +266,16 @@ impl WebsocketProtocolHandler {
|
||||
|
||||
let tcp_stream = TcpStream::connect(format!("{}:{}", &domain, &port))
|
||||
.await
|
||||
.map_err(|e| format!("failed to connect tcp stream: {}", e))?;
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error))?;
|
||||
let local_addr = tcp_stream
|
||||
.local_addr()
|
||||
.map_err(|e| format!("can't get local address for tcp stream: {}", e))?;
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error))?;
|
||||
let peer_socket_addr = tcp_stream
|
||||
.peer_addr()
|
||||
.map_err(|e| format!("can't get peer address for tcp stream: {}", e))?;
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error))?;
|
||||
let peer_addr = PeerAddress::new(
|
||||
Address::from_socket_addr(peer_socket_addr),
|
||||
peer_socket_addr.port(),
|
||||
@@ -279,10 +287,12 @@ impl WebsocketProtocolHandler {
|
||||
let tls_stream = connector
|
||||
.connect(domain, tcp_stream)
|
||||
.await
|
||||
.map_err(|e| format!("can't connect tls: {}", e))?;
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error))?;
|
||||
let (ws_stream, _response) = client_async(request, tls_stream)
|
||||
.await
|
||||
.map_err(|e| format!("wss negotation failed: {}", e))?;
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error))?;
|
||||
let conn = NetworkConnection::Wss(WebsocketNetworkConnection::new(tls, ws_stream));
|
||||
network_manager
|
||||
.on_new_connection(
|
||||
@@ -294,7 +304,8 @@ impl WebsocketProtocolHandler {
|
||||
} else {
|
||||
let (ws_stream, _response) = client_async(request, tcp_stream)
|
||||
.await
|
||||
.map_err(|e| format!("ws negotiate failed: {}", e))?;
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error))?;
|
||||
let conn = NetworkConnection::Ws(WebsocketNetworkConnection::new(tls, ws_stream));
|
||||
network_manager
|
||||
.on_new_connection(
|
||||
|
@@ -12,14 +12,14 @@ impl Network {
|
||||
async fn request_public_address(&self, node_ref: NodeRef) -> Option<SocketAddr> {
|
||||
let routing_table = self.routing_table();
|
||||
let rpc = routing_table.rpc_processor();
|
||||
let info_answer = match rpc.rpc_call_info(node_ref.clone()).await {
|
||||
Err(e) => {
|
||||
trace!("failed to get info answer from {:?}: {:?}", node_ref, e);
|
||||
return None;
|
||||
}
|
||||
Ok(ia) => ia,
|
||||
};
|
||||
info_answer.sender_info.socket_address
|
||||
rpc.rpc_call_info(node_ref.clone())
|
||||
.await
|
||||
.map_err(logthru_net!(
|
||||
"failed to get info answer from {:?}",
|
||||
node_ref
|
||||
))
|
||||
.map(|info_answer| info_answer.sender_info.socket_address)
|
||||
.unwrap_or(None)
|
||||
}
|
||||
|
||||
// find fast peers with a particular address type, and ask them to tell us what our external address is
|
||||
@@ -31,7 +31,7 @@ impl Network {
|
||||
let routing_table = self.routing_table();
|
||||
let peers = routing_table.get_fast_nodes_of_type(protocol_address_type);
|
||||
if peers.is_empty() {
|
||||
trace!("no peers of type '{:?}'", protocol_address_type);
|
||||
log_net!("no peers of type '{:?}'", protocol_address_type);
|
||||
return None;
|
||||
}
|
||||
for peer in peers {
|
||||
@@ -44,7 +44,7 @@ impl Network {
|
||||
return Some((sa, peer));
|
||||
}
|
||||
}
|
||||
trace!("no peers responded with an external address");
|
||||
log_net!("no peers responded with an external address");
|
||||
None
|
||||
}
|
||||
|
||||
@@ -78,19 +78,13 @@ impl Network {
|
||||
) -> bool {
|
||||
let routing_table = self.routing_table();
|
||||
let rpc = routing_table.rpc_processor();
|
||||
match rpc
|
||||
.rpc_call_validate_dial_info(node_ref.clone(), dial_info, redirect, alternate_port)
|
||||
rpc.rpc_call_validate_dial_info(node_ref.clone(), dial_info, redirect, alternate_port)
|
||||
.await
|
||||
{
|
||||
Err(e) => {
|
||||
error!(
|
||||
"failed to send validate_dial_info to {:?}: {:?}",
|
||||
node_ref, e
|
||||
);
|
||||
false
|
||||
}
|
||||
Ok(val) => val,
|
||||
}
|
||||
.map_err(logthru_net!(
|
||||
"failed to send validate_dial_info to {:?}",
|
||||
node_ref
|
||||
))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
async fn try_port_mapping<I: AsRef<[SocketAddr]>>(
|
||||
@@ -103,7 +97,7 @@ impl Network {
|
||||
}
|
||||
|
||||
pub async fn update_udpv4_dialinfo_task_routine(self, _l: u64, _t: u64) -> Result<(), String> {
|
||||
trace!("looking for udpv4 public dial info");
|
||||
log_net!("looking for udpv4 public dial info");
|
||||
let routing_table = self.routing_table();
|
||||
|
||||
let mut retry_count = {
|
||||
@@ -148,7 +142,7 @@ impl Network {
|
||||
break;
|
||||
} else {
|
||||
// UDP firewall?
|
||||
warn!("UDP static public dial info not reachable. UDP firewall may be blocking inbound to {:?} for {:?}",external1_dial_info, node_b);
|
||||
log_net!("UDP static public dial info not reachable. UDP firewall may be blocking inbound to {:?} for {:?}",external1_dial_info, node_b);
|
||||
}
|
||||
} else {
|
||||
// There is -some NAT-
|
||||
@@ -260,7 +254,7 @@ impl Network {
|
||||
}
|
||||
|
||||
pub async fn update_tcpv4_dialinfo_task_routine(self, _l: u64, _t: u64) -> Result<(), String> {
|
||||
trace!("looking for tcpv4 public dial info");
|
||||
log_net!("looking for tcpv4 public dial info");
|
||||
// xxx
|
||||
//Err("unimplemented".to_owned())
|
||||
Ok(())
|
||||
|
Reference in New Issue
Block a user