1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
use super::*;
use std::sync::atomic::Ordering;
/// Message count allowed to accumulate on the boundary switching to webrtc.
/// This prevents potentially faster to transfer webrtc messages from being
/// delivered out of order before trailing slower sbd messages are received.
const MAX_WEBRTC_BUF: usize = 512;
pub(crate) enum ConnCmd {
SigRecv(tx5_signal::SignalMessage),
SendMessage(Vec<u8>),
WebrtcMessage(Vec<u8>),
WebrtcReady,
WebrtcClosed,
}
/// Receive messages from a tx5 connection.
pub struct ConnRecv(CloseRecv<Vec<u8>>);
impl ConnRecv {
/// Receive up to 16KiB of message data.
pub async fn recv(&mut self) -> Option<Vec<u8>> {
self.0.recv().await
}
}
/// A tx5 connection.
pub struct Conn {
ready: Arc<tokio::sync::Semaphore>,
pub_key: PubKey,
cmd_send: CloseSend<ConnCmd>,
conn_task: tokio::task::JoinHandle<()>,
keepalive_task: tokio::task::JoinHandle<()>,
webrtc_ready: Arc<tokio::sync::Semaphore>,
send_msg_count: Arc<std::sync::atomic::AtomicU64>,
send_byte_count: Arc<std::sync::atomic::AtomicU64>,
recv_msg_count: Arc<std::sync::atomic::AtomicU64>,
recv_byte_count: Arc<std::sync::atomic::AtomicU64>,
hub_cmd_send: tokio::sync::mpsc::Sender<HubCmd>,
}
macro_rules! netaudit {
($lvl:ident, $($all:tt)*) => {
::tracing::event!(
target: "NETAUDIT",
::tracing::Level::$lvl,
m = "tx5-connection",
$($all)*
);
};
}
impl Drop for Conn {
fn drop(&mut self) {
netaudit!(DEBUG, pub_key = ?self.pub_key, a = "drop");
self.conn_task.abort();
self.keepalive_task.abort();
let hub_cmd_send = self.hub_cmd_send.clone();
let pub_key = self.pub_key.clone();
tokio::task::spawn(async move {
let _ = hub_cmd_send.send(HubCmd::Disconnect(pub_key)).await;
});
}
}
impl Conn {
#[cfg(test)]
pub(crate) fn test_kill_keepalive_task(&self) {
self.keepalive_task.abort();
}
pub(crate) fn priv_new(
webrtc_config: Vec<u8>,
is_polite: bool,
pub_key: PubKey,
client: Weak<tx5_signal::SignalConnection>,
config: Arc<tx5_signal::SignalConfig>,
hub_cmd_send: tokio::sync::mpsc::Sender<HubCmd>,
) -> (Arc<Self>, ConnRecv, CloseSend<ConnCmd>) {
netaudit!(
DEBUG,
webrtc_config = String::from_utf8_lossy(&webrtc_config).to_string(),
?pub_key,
?is_polite,
a = "open",
);
let send_msg_count = Arc::new(std::sync::atomic::AtomicU64::new(0));
let send_byte_count = Arc::new(std::sync::atomic::AtomicU64::new(0));
let recv_msg_count = Arc::new(std::sync::atomic::AtomicU64::new(0));
let recv_byte_count = Arc::new(std::sync::atomic::AtomicU64::new(0));
// zero len semaphore.. we actually just wait for the close
let ready = Arc::new(tokio::sync::Semaphore::new(0));
let webrtc_ready = Arc::new(tokio::sync::Semaphore::new(0));
let (mut msg_send, msg_recv) = CloseSend::channel();
let (cmd_send, mut cmd_recv) = CloseSend::channel();
let keepalive_dur = config.max_idle / 2;
let client2 = client.clone();
let pub_key2 = pub_key.clone();
let keepalive_task = tokio::task::spawn(async move {
loop {
tokio::time::sleep(keepalive_dur).await;
if let Some(client) = client2.upgrade() {
if client.send_keepalive(&pub_key2).await.is_err() {
break;
}
} else {
break;
}
}
});
let send_msg_count2 = send_msg_count.clone();
let send_byte_count2 = send_byte_count.clone();
let recv_msg_count2 = recv_msg_count.clone();
let recv_byte_count2 = recv_byte_count.clone();
let webrtc_ready2 = webrtc_ready.clone();
let ready2 = ready.clone();
let client2 = client.clone();
let pub_key2 = pub_key.clone();
let mut cmd_send3 = cmd_send.clone();
let conn_task = tokio::task::spawn(async move {
let client = match client2.upgrade() {
Some(client) => client,
None => return,
};
let mut webrtc_message_buffer = Vec::new();
let handshake_fut = async {
let nonce = client.send_handshake_req(&pub_key2).await?;
let mut got_peer_res = false;
let mut sent_our_res = false;
while let Some(cmd) = cmd_recv.recv().await {
match cmd {
ConnCmd::SigRecv(sig) => {
use tx5_signal::SignalMessage::*;
match sig {
HandshakeReq(oth_nonce) => {
client
.send_handshake_res(
&pub_key2, oth_nonce,
)
.await?;
sent_our_res = true;
}
HandshakeRes(res_nonce) => {
if res_nonce != nonce {
return Err(Error::other(
"nonce mismatch",
));
}
got_peer_res = true;
}
// Ignore all other message types...
// they may be from previous sessions
_ => (),
}
}
ConnCmd::SendMessage(_) => {
return Err(Error::other("send before ready"));
}
ConnCmd::WebrtcMessage(msg) => {
webrtc_message_buffer.push(msg);
}
ConnCmd::WebrtcReady => {
webrtc_ready2.close();
}
ConnCmd::WebrtcClosed => {
// only emitted by the webrtc module
// which at this point hasn't yet been initialized
unreachable!()
}
}
if got_peer_res && sent_our_res {
break;
}
}
Result::Ok(())
};
match tokio::time::timeout(config.max_idle, handshake_fut).await {
Err(_) | Ok(Err(_)) => {
client.close_peer(&pub_key2).await;
return;
}
Ok(Ok(_)) => (),
}
drop(client);
// closing the semaphore causes all the acquire awaits to end
ready2.close();
let (webrtc, mut webrtc_recv) = webrtc::Webrtc::new(
is_polite,
webrtc_config,
// MAYBE - make this configurable
4096,
);
let client3 = client2.clone();
let pub_key3 = pub_key2.clone();
let _webrtc_task = AbortTask(tokio::task::spawn(async move {
use webrtc::WebrtcEvt::*;
cmd_send3.set_close_on_drop(true);
while let Some(evt) = webrtc_recv.recv().await {
match evt {
GeneratedOffer(offer) => {
netaudit!(
TRACE,
pub_key = ?pub_key3,
offer = String::from_utf8_lossy(&offer).to_string(),
a = "send_offer",
);
if let Some(client) = client3.upgrade() {
if let Err(err) =
client.send_offer(&pub_key3, offer).await
{
netaudit!(
DEBUG,
pub_key = ?pub_key3,
?err,
a = "webrtc send_offer error",
);
break;
}
} else {
break;
}
}
GeneratedAnswer(answer) => {
netaudit!(
TRACE,
pub_key = ?pub_key3,
offer = String::from_utf8_lossy(&answer).to_string(),
a = "send_answer",
);
if let Some(client) = client3.upgrade() {
if let Err(err) =
client.send_answer(&pub_key3, answer).await
{
netaudit!(
DEBUG,
pub_key = ?pub_key3,
?err,
a = "webrtc send_answer error",
);
break;
}
} else {
break;
}
}
GeneratedIce(ice) => {
netaudit!(
TRACE,
pub_key = ?pub_key3,
offer = String::from_utf8_lossy(&ice).to_string(),
a = "send_ice",
);
if let Some(client) = client3.upgrade() {
if let Err(err) =
client.send_ice(&pub_key3, ice).await
{
netaudit!(
DEBUG,
pub_key = ?pub_key3,
?err,
a = "webrtc send_ice error",
);
break;
}
} else {
break;
}
}
Message(msg) => {
if cmd_send3
.send(ConnCmd::WebrtcMessage(msg))
.await
.is_err()
{
netaudit!(
DEBUG,
pub_key = ?pub_key3,
a = "webrtc cmd closed",
);
break;
}
}
Ready => {
if cmd_send3
.send(ConnCmd::WebrtcReady)
.await
.is_err()
{
netaudit!(
DEBUG,
pub_key = ?pub_key3,
a = "webrtc cmd closed",
);
break;
}
}
}
}
let _ = cmd_send3.send(ConnCmd::WebrtcClosed).await;
}));
msg_send.set_close_on_drop(true);
let mut recv_over_webrtc = false;
let mut send_over_webrtc = false;
loop {
let cmd =
tokio::time::timeout(config.max_idle, cmd_recv.recv())
.await;
let cmd = match cmd {
Err(_) => {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: connection idle",
);
break;
}
Ok(cmd) => cmd,
};
let cmd = match cmd {
None => {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: cmd_recv stream complete",
);
break;
}
Some(cmd) => cmd,
};
let mut slow_task = "unknown";
if breakable_timeout!(match cmd {
ConnCmd::SigRecv(sig) => {
use tx5_signal::SignalMessage::*;
match sig {
// invalid
HandshakeReq(_) | HandshakeRes(_) => {
slow_task = "sig-handshake";
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: unexpected handshake msg",
);
break;
}
Message(msg) => {
slow_task = "sig-message";
if recv_over_webrtc {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: unexpected sbd msg after recv_over_webrtc",
);
break;
} else {
recv_msg_count2
.fetch_add(1, Ordering::Relaxed);
recv_byte_count2.fetch_add(
msg.len() as u64,
Ordering::Relaxed,
);
if msg_send.send(msg).await.is_err() {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: msg_send closed",
);
break;
}
}
}
Offer(offer) => {
slow_task = "sig-offer";
netaudit!(
TRACE,
pub_key = ?pub_key2,
offer = String::from_utf8_lossy(&offer).to_string(),
a = "recv_offer",
);
if let Err(err) = webrtc.in_offer(offer).await {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
?err,
a = "close: webrtc in_offer error",
);
break;
}
}
Answer(answer) => {
slow_task = "sig-answer";
netaudit!(
TRACE,
pub_key = ?pub_key2,
offer = String::from_utf8_lossy(&answer).to_string(),
a = "recv_answer",
);
if let Err(err) = webrtc.in_answer(answer).await
{
netaudit!(
DEBUG,
pub_key = ?pub_key2,
?err,
a = "close: webrtc in_answer error",
);
break;
}
}
Ice(ice) => {
slow_task = "sig-ice";
netaudit!(
TRACE,
pub_key = ?pub_key2,
offer = String::from_utf8_lossy(&ice).to_string(),
a = "recv_ice",
);
if let Err(err) = webrtc.in_ice(ice).await {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
?err,
a = "close: webrtc in_ice error",
);
break;
}
}
WebrtcReady => {
slow_task = "sig-webrtc-ready";
recv_over_webrtc = true;
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "recv_over_webrtc",
);
for msg in webrtc_message_buffer.drain(..) {
// don't bump send metrics here,
// we bumped them on receive
if msg_send.send(msg).await.is_err() {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: msg_send closed",
);
break;
}
}
}
_ => (),
}
}
ConnCmd::SendMessage(msg) => {
slow_task = "send-message";
send_msg_count2.fetch_add(1, Ordering::Relaxed);
send_byte_count2
.fetch_add(msg.len() as u64, Ordering::Relaxed);
if send_over_webrtc {
if let Err(err) = webrtc.message(msg).await {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
?err,
a = "close: webrtc message error",
);
break;
}
} else if let Some(client) = client2.upgrade() {
if let Err(err) =
client.send_message(&pub_key2, msg).await
{
netaudit!(
DEBUG,
pub_key = ?pub_key2,
?err,
a = "close: sbd client send error",
);
break;
}
} else {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: sbd client closed",
);
break;
}
}
ConnCmd::WebrtcMessage(msg) => {
slow_task = "webrtc-message";
recv_msg_count2.fetch_add(1, Ordering::Relaxed);
recv_byte_count2
.fetch_add(msg.len() as u64, Ordering::Relaxed);
if recv_over_webrtc {
if msg_send.send(msg).await.is_err() {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: msg_send closed",
);
break;
}
} else {
webrtc_message_buffer.push(msg);
if webrtc_message_buffer.len() > MAX_WEBRTC_BUF {
// prevent memory fillup
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: webrtc buffer overflow",
);
break;
}
}
}
ConnCmd::WebrtcReady => {
slow_task = "webrtc-ready";
if let Some(client) = client2.upgrade() {
if let Err(err) =
client.send_webrtc_ready(&pub_key2).await
{
netaudit!(
DEBUG,
pub_key = ?pub_key2,
?err,
a = "close: sbd client ready error",
);
break;
}
} else {
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: sbd client closed",
);
break;
}
send_over_webrtc = true;
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "send_over_webrtc",
);
webrtc_ready2.close();
}
ConnCmd::WebrtcClosed => {
slow_task = "webrtc-closed";
netaudit!(
DEBUG,
pub_key = ?pub_key2,
a = "close: webrtc closed",
);
break;
}
}).is_err() {
tracing::warn!(slow_task, "slow app on conn loop task");
break;
}
}
// explicitly close the peer
if let Some(client) = client2.upgrade() {
client.close_peer(&pub_key2).await;
};
// the receiver side is closed because msg_send is dropped.
});
let mut cmd_send2 = cmd_send.clone();
cmd_send2.set_close_on_drop(true);
let this = Self {
ready,
pub_key,
cmd_send: cmd_send2,
conn_task,
keepalive_task,
webrtc_ready,
send_msg_count,
send_byte_count,
recv_msg_count,
recv_byte_count,
hub_cmd_send,
};
(Arc::new(this), ConnRecv(msg_recv), cmd_send)
}
/// Wait until this connection is ready to send / receive data.
pub async fn ready(&self) {
// this will error when we close the semaphore waking up the task
let _ = self.ready.acquire().await;
}
/// Wait until this connection is connected via webrtc.
/// Note, this will never resolve if we never successfully
/// connect over webrtc.
pub async fn webrtc_ready(&self) {
let _ = self.webrtc_ready.acquire().await;
}
/// Returns `true` if we sucessfully connected over webrtc.
pub fn is_using_webrtc(&self) -> bool {
self.webrtc_ready.is_closed()
}
/// The pub key of the remote peer this is connected to.
pub fn pub_key(&self) -> &PubKey {
&self.pub_key
}
/// Send up to 16KiB of message data.
pub async fn send(&self, msg: Vec<u8>) -> Result<()> {
self.cmd_send.send(ConnCmd::SendMessage(msg)).await
}
/// Get connection statistics.
pub fn get_stats(&self) -> ConnStats {
ConnStats {
send_msg_count: self.send_msg_count.load(Ordering::Relaxed),
send_byte_count: self.send_byte_count.load(Ordering::Relaxed),
recv_msg_count: self.recv_msg_count.load(Ordering::Relaxed),
recv_byte_count: self.recv_byte_count.load(Ordering::Relaxed),
}
}
}
/// Connection statistics.
#[derive(Default)]
pub struct ConnStats {
/// message count sent.
pub send_msg_count: u64,
/// byte count sent.
pub send_byte_count: u64,
/// message count received.
pub recv_msg_count: u64,
/// byte count received.
pub recv_byte_count: u64,
}