tor_proto/circuit/reactor.rs
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 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
//! Code to handle incoming cells on a circuit.
//!
//! ## On message validation
//!
//! There are three steps for validating an incoming message on a stream:
//!
//! 1. Is the message contextually appropriate? (e.g., no more than one
//! `CONNECTED` message per stream.) This is handled by calling
//! [`CmdChecker::check_msg`](crate::stream::CmdChecker::check_msg).
//! 2. Does the message comply with flow-control rules? (e.g., no more data than
//! we've gotten SENDMEs for.) For open streams, the stream itself handles
//! this; for half-closed streams, the reactor handles it using the
//! `halfstream` module.
//! 3. Does the message have an acceptable command type, and is the message
//! well-formed? For open streams, the streams themselves handle this check.
//! For half-closed streams, the reactor handles it by calling
//! `consume_checked_msg()`.
mod control;
mod extender;
pub(super) mod syncview;
use super::handshake::RelayCryptLayerProtocol;
use super::streammap::{EndSentStreamEnt, OpenStreamEnt, ShouldSendEnd, StreamEntMut};
use super::MutableState;
use crate::circuit::celltypes::{ClientCircChanMsg, CreateResponse};
use crate::circuit::handshake::{BoxedClientLayer, HandshakeRole};
use crate::circuit::unique_id::UniqId;
use crate::circuit::{
streammap, CircParameters, CircuitRxReceiver, Create2Wrap, CreateFastWrap, CreateHandshakeWrap,
};
use crate::congestion::sendme::{self, CircTag};
use crate::congestion::{CongestionControl, CongestionSignals};
use crate::crypto::binding::CircuitBinding;
use crate::crypto::cell::{
HopNum, InboundClientCrypt, InboundClientLayer, OutboundClientCrypt, OutboundClientLayer,
RelayCellBody, SENDME_TAG_LEN,
};
use crate::crypto::handshake::fast::CreateFastClient;
#[cfg(feature = "ntor_v3")]
use crate::crypto::handshake::ntor_v3::{NtorV3Client, NtorV3PublicKey};
use crate::memquota::{CircuitAccount, SpecificAccount as _, StreamAccount};
use crate::stream::{AnyCmdChecker, StreamStatus};
use crate::util::err::ReactorError;
use crate::util::sometimes_unbounded_sink::SometimesUnboundedSink;
use crate::util::SinkExt as _;
use crate::{Error, Result};
use control::{CommandHandler, ControlHandler};
use futures::stream::FuturesUnordered;
use std::borrow::Borrow;
use std::mem::size_of;
use std::pin::Pin;
use tor_cell::chancell::msg::{AnyChanMsg, HandshakeType, Relay};
use tor_cell::relaycell::msg::{AnyRelayMsg, End, Sendme, Truncated};
use tor_cell::relaycell::{
AnyRelayMsgOuter, RelayCellDecoder, RelayCellDecoderResult, RelayCellFormat, RelayCmd,
StreamId, UnparsedRelayMsg,
};
use tor_error::internal;
#[cfg(feature = "hs-service")]
use {
crate::stream::{DataCmdChecker, IncomingStreamRequest, IncomingStreamRequestFilter},
tor_cell::relaycell::msg::Begin,
};
use futures::channel::mpsc;
use futures::StreamExt;
use futures::{select_biased, FutureExt as _, SinkExt as _, Stream};
use oneshot_fused_workaround as oneshot;
use std::result::Result as StdResult;
use std::sync::{Arc, Mutex};
use std::task::Poll;
use crate::channel::{Channel, ChannelSender};
use crate::circuit::path;
use crate::circuit::{StreamMpscReceiver, StreamMpscSender};
use crate::crypto::handshake::ntor::{NtorClient, NtorPublicKey};
use crate::crypto::handshake::{ClientHandshake, KeyGenerator};
use derive_deftly::Deftly;
use derive_more::From;
use safelog::sensitive as sv;
use tor_async_utils::{SinkPrepareExt as _, SinkTrySend as _, SinkTrySendError as _};
use tor_cell::chancell::{AnyChanCell, CircId};
use tor_cell::chancell::{BoxedCellBody, ChanMsg};
use tor_linkspec::RelayIds;
use tor_llcrypto::pk;
use tor_memquota::derive_deftly_template_HasMemoryCost;
use tor_memquota::mq_queue::{self, ChannelSpec as _, MpscSpec};
use tracing::{debug, trace, warn};
use extender::HandshakeAuxDataHandler;
pub(super) use control::CtrlCmd;
pub(super) use control::CtrlMsg;
/// Initial value for outbound flow-control window on streams.
pub(super) const SEND_WINDOW_INIT: u16 = 500;
/// Initial value for inbound flow-control window on streams.
pub(super) const RECV_WINDOW_INIT: u16 = 500;
/// Size of the buffer used between the reactor and a `StreamReader`.
///
/// FIXME(eta): We pick 2× the receive window, which is very conservative (we arguably shouldn't
/// get sent more than the receive window anyway!). We might do due to things that
/// don't count towards the window though.
pub(super) const STREAM_READER_BUFFER: usize = (2 * RECV_WINDOW_INIT) as usize;
/// The type of a oneshot channel used to inform reactor users of the result of an operation.
pub(super) type ReactorResultChannel<T> = oneshot::Sender<Result<T>>;
/// MPSC queue containing stream requests
#[cfg(feature = "hs-service")]
type StreamReqSender = mq_queue::Sender<StreamReqInfo, MpscSpec>;
/// A handshake type, to be used when creating circuit hops.
#[derive(Clone, Debug)]
pub(super) enum CircuitHandshake {
/// Use the CREATE_FAST handshake.
CreateFast,
/// Use the ntor handshake.
Ntor {
/// The public key of the relay.
public_key: NtorPublicKey,
/// The Ed25519 identity of the relay, which is verified against the
/// identity held in the circuit's channel.
ed_identity: pk::ed25519::Ed25519Identity,
},
/// Use the ntor-v3 handshake.
#[cfg(feature = "ntor_v3")]
NtorV3 {
/// The public key of the relay.
public_key: NtorV3PublicKey,
},
}
/// A behavior to perform when closing a stream.
///
/// We don't use `Option<End>`` here, since the behavior of `SendNothing` is so surprising
/// that we shouldn't let it pass unremarked.
#[derive(Clone, Debug)]
pub(crate) enum CloseStreamBehavior {
/// Send nothing at all, so that the other side will not realize we have
/// closed the stream.
///
/// We should only do this for incoming onion service streams when we
/// want to black-hole the client's requests.
SendNothing,
/// Send an End cell, if we haven't already sent one.
SendEnd(End),
}
impl Default for CloseStreamBehavior {
fn default() -> Self {
Self::SendEnd(End::new_misc())
}
}
/// Represents the reactor's view of a single hop.
pub(super) struct CircHop {
/// Reactor unique ID. Used for logging.
unique_id: UniqId,
/// Hop number in the path.
hop_num: HopNum,
/// Map from stream IDs to streams.
///
/// We store this with the reactor instead of the circuit, since the
/// reactor needs it for every incoming cell on a stream, whereas
/// the circuit only needs it when allocating new streams.
///
/// NOTE: this is behind a mutex because the reactor polls the `StreamMap`s
/// of all hops concurrently, in a [`FuturesUnordered`]. Without the mutex,
/// this wouldn't be possible, because it would mean holding multiple
/// mutable references to `self` (the reactor). Note, however,
/// that there should never be any contention on this mutex:
/// we never create more than one [`Reactor::ready_streams_iterator`] stream
/// at a time, and we never clone/lock the hop's `StreamMap` outside of
///[`Reactor::ready_streams_iterator`].
///
// TODO: encapsulate the Vec<CircHop> into a separate CircHops structure,
// and hide its internals from the Reactor. The CircHops implementation
// should enforce the invariant described in the note above.
map: Arc<Mutex<streammap::StreamMap>>,
/// Congestion control object.
///
/// This object is also in charge of handling circuit level SENDME logic for this hop.
ccontrol: CongestionControl,
/// Decodes relay cells received from this hop.
inbound: RelayCellDecoder,
}
/// One or more [`RunOnceCmdInner`] to run inside [`Reactor::run_once`].
#[derive(From, Debug)]
enum RunOnceCmd {
/// Run a single `RunOnceCmdInner` command.
Single(RunOnceCmdInner),
/// Run multiple `RunOnceCmdInner` commands.
//
// Note: this whole enum *could* be replaced with Vec<RunOnceCmdInner>,
// but most of the time we're only going to have *one* RunOnceCmdInner
// to run per run_once() loop. The enum enables us avoid the extra heap
// allocation for the `RunOnceCmd::Single` case.
Multiple(Vec<RunOnceCmdInner>),
}
/// Instructions for running something in the reactor loop.
///
/// Run at the end of [`Reactor::run_once`].
//
// TODO: many of the variants of this enum have an identical CtrlMsg counterpart.
// We should consider making each variant a tuple variant and deduplicating the fields.
#[derive(educe::Educe)]
#[educe(Debug)]
enum RunOnceCmdInner {
/// Send a RELAY cell.
Send {
/// The cell to send.
cell: SendRelayCell,
/// A channel for sending completion notifications.
done: Option<ReactorResultChannel<()>>,
},
/// Send a given control message on this circuit, and install a control-message handler to
/// receive responses.
#[cfg(feature = "send-control-msg")]
SendMsgAndInstallHandler {
/// The message to send, if any
msg: Option<AnyRelayMsgOuter>,
/// A message handler to install.
///
/// If this is `None`, there must already be a message handler installed
#[educe(Debug(ignore))]
handler: Option<Box<dyn MetaCellHandler + Send + 'static>>,
/// A sender that we use to tell the caller that the message was sent
/// and the handler installed.
done: oneshot::Sender<Result<()>>,
},
/// Handle a SENDME message.
HandleSendMe {
/// The hop number.
hop: HopNum,
/// The SENDME message to handle.
sendme: Sendme,
},
/// Begin a stream with the provided hop in this circuit.
///
/// Uses the provided stream ID, and sends the provided message to that hop.
BeginStream {
/// The cell to send.
cell: Result<(SendRelayCell, StreamId)>,
/// Oneshot channel to notify on completion, with the allocated stream ID.
done: ReactorResultChannel<StreamId>,
},
/// Close the specified stream.
CloseStream {
/// The hop number.
hop_num: HopNum,
/// The ID of the stream to close.
sid: StreamId,
/// The stream-closing behavior.
behav: CloseStreamBehavior,
/// The reason for closing the stream.
reason: streammap::TerminateReason,
/// A channel for sending completion notifications.
done: Option<ReactorResultChannel<()>>,
},
/// Perform a clean shutdown on this circuit.
CleanShutdown,
}
// Cmd for sending a relay cell.
//
// The contents of this struct are passed to `send_relay_cell`
#[derive(educe::Educe)]
#[educe(Debug)]
pub(crate) struct SendRelayCell {
/// The hop number.
pub(crate) hop: HopNum,
/// Whether to use a RELAY_EARLY cell.
pub(crate) early: bool,
/// The cell to send.
pub(crate) cell: AnyRelayMsgOuter,
}
/// A [`RunOnceCmdInner`] command to execute at the end of [`Reactor::run_once`].
#[derive(From, Debug)]
enum SelectResult {
/// Run a single `RunOnceCmdInner` command.
Single(RunOnceCmdInner),
/// Handle a control message
HandleControl(CtrlMsg),
/// Handle an input message.
HandleCell(ClientCircChanMsg),
}
impl CircHop {
/// Create a new hop.
pub(super) fn new(
unique_id: UniqId,
hop_num: HopNum,
format: RelayCellFormat,
params: &CircParameters,
) -> Self {
CircHop {
unique_id,
hop_num,
map: Arc::new(Mutex::new(streammap::StreamMap::new())),
ccontrol: CongestionControl::new(¶ms.ccontrol),
inbound: RelayCellDecoder::new(format),
}
}
/// Start a stream. Creates an entry in the stream map with the given channels, and sends the
/// `message` to the provided hop.
fn begin_stream(
&mut self,
message: AnyRelayMsg,
sender: StreamMpscSender<UnparsedRelayMsg>,
rx: StreamMpscReceiver<AnyRelayMsg>,
cmd_checker: AnyCmdChecker,
) -> Result<(SendRelayCell, StreamId)> {
let send_window = sendme::StreamSendWindow::new(SEND_WINDOW_INIT);
let r = self.map.lock().expect("lock poisoned").add_ent(
sender,
rx,
send_window,
cmd_checker,
)?;
let cell = AnyRelayMsgOuter::new(Some(r), message);
Ok((
SendRelayCell {
hop: self.hop_num,
early: false,
cell,
},
r,
))
}
/// Close the stream associated with `id` because the stream was
/// dropped.
///
/// If we have not already received an END cell on this stream, send one.
/// If no END cell is specified, an END cell with the reason byte set to
/// REASON_MISC will be sent.
fn close_stream(
&mut self,
id: StreamId,
message: CloseStreamBehavior,
why: streammap::TerminateReason,
) -> Result<Option<SendRelayCell>> {
let should_send_end = self.map.lock().expect("lock poisoned").terminate(id, why)?;
trace!(
"{}: Ending stream {}; should_send_end={:?}",
self.unique_id,
id,
should_send_end
);
// TODO: I am about 80% sure that we only send an END cell if
// we didn't already get an END cell. But I should double-check!
if let (ShouldSendEnd::Send, CloseStreamBehavior::SendEnd(end_message)) =
(should_send_end, message)
{
let end_cell = AnyRelayMsgOuter::new(Some(id), end_message.into());
let cell = SendRelayCell {
hop: self.hop_num,
early: false,
cell: end_cell,
};
return Ok(Some(cell));
}
Ok(None)
}
}
/// An object that's waiting for a meta cell (one not associated with a stream) in order to make
/// progress.
///
/// # Background
///
/// The `Reactor` can't have async functions that send and receive cells, because its job is to
/// send and receive cells: if one of its functions tried to do that, it would just hang forever.
///
/// To get around this problem, the reactor can send some cells, and then make one of these
/// `MetaCellHandler` objects, which will be run when the reply arrives.
pub(super) trait MetaCellHandler: Send {
/// The hop we're expecting the message to come from. This is compared against the hop
/// from which we actually receive messages, and an error is thrown if the two don't match.
fn expected_hop(&self) -> HopNum;
/// Called when the message we were waiting for arrives.
///
/// Gets a copy of the `Reactor` in order to do anything it likes there.
///
/// If this function returns an error, the reactor will shut down.
fn handle_msg(
&mut self,
msg: UnparsedRelayMsg,
reactor: &mut Reactor,
) -> Result<MetaCellDisposition>;
}
/// A possible successful outcome of giving a message to a [`MsgHandler`](super::msghandler::MsgHandler).
#[derive(Debug, Clone)]
#[cfg_attr(feature = "send-control-msg", visibility::make(pub))]
#[non_exhaustive]
pub(super) enum MetaCellDisposition {
/// The message was consumed; the handler should remain installed.
#[cfg(feature = "send-control-msg")]
Consumed,
/// The message was consumed; the handler should be uninstalled.
ConversationFinished,
/// The message was consumed; the circuit should be closed.
#[cfg(feature = "send-control-msg")]
CloseCirc,
// TODO: Eventually we might want the ability to have multiple handlers
// installed, and to let them say "not for me, maybe for somebody else?".
// But right now we don't need that.
}
/// Unwrap the specified [`Option`], returning a [`ReactorError::Shutdown`] if it is `None`.
///
/// This is a macro instead of a function to work around borrowck errors
/// in the select! from run_once().
macro_rules! unwrap_or_shutdown {
($self:expr, $res:expr, $reason:expr) => {{
match $res {
None => {
trace!("{}: reactor shutdown due to {}", $self.unique_id, $reason);
Err(ReactorError::Shutdown)
}
Some(v) => Ok(v),
}
}};
}
/// Object to handle incoming cells and background tasks on a circuit
///
/// This type is returned when you finish a circuit; you need to spawn a
/// new task that calls `run()` on it.
#[must_use = "If you don't call run() on a reactor, the circuit won't work."]
pub struct Reactor {
/// Receiver for control messages for this reactor, sent by `ClientCirc` objects.
///
/// This channel is polled in [`Reactor::run_once`], but only if the `chan_sender` sink
/// is ready to accept cells.
control: mpsc::UnboundedReceiver<CtrlMsg>,
/// Receiver for command messages for this reactor, sent by `ClientCirc` objects.
///
/// This channel is polled in [`Reactor::run_once`].
///
/// NOTE: this is a separate channel from `control`, because some messages
/// have higher priority and need to be handled even if the `chan_sender` is not
/// ready (whereas `control` messages are not read until the `chan_sender` sink
/// is ready to accept cells).
command: mpsc::UnboundedReceiver<CtrlCmd>,
/// The channel this circuit is attached to.
channel: Arc<Channel>,
/// Sender object used to actually send cells.
///
/// NOTE: Control messages could potentially add unboundedly to this, although that's
/// not likely to happen (and isn't triggereable from the network, either).
chan_sender: SometimesUnboundedSink<AnyChanCell, ChannelSender>,
/// A oneshot sender that is used to alert other tasks when this reactor is
/// finally dropped.
///
/// It is a sender for Void because we never actually want to send anything here;
/// we only want to generate canceled events.
#[allow(dead_code)] // the only purpose of this field is to be dropped.
reactor_closed_tx: oneshot::Sender<void::Void>,
/// Input stream, on which we receive ChanMsg objects from this circuit's
/// channel.
// TODO: could use a SPSC channel here instead.
input: CircuitRxReceiver,
/// The cryptographic state for this circuit for inbound cells.
/// This object is divided into multiple layers, each of which is
/// shared with one hop of the circuit.
crypto_in: InboundClientCrypt,
/// The cryptographic state for this circuit for outbound cells.
crypto_out: OutboundClientCrypt,
/// List of hops state objects used by the reactor
hops: Vec<CircHop>,
/// Mutable information about this circuit, shared with
/// [`ClientCirc`](super::ClientCirc).
mutable: Arc<Mutex<MutableState>>,
/// An identifier for logging about this reactor's circuit.
unique_id: UniqId,
/// This circuit's identifier on the upstream channel.
channel_id: CircId,
/// A handler for a meta cell, together with a result channel to notify on completion.
meta_handler: Option<Box<dyn MetaCellHandler + Send>>,
/// A handler for incoming stream requests.
#[cfg(feature = "hs-service")]
incoming_stream_req_handler: Option<IncomingStreamRequestHandler>,
/// Memory quota account
#[allow(dead_code)] // Partly here to keep it alive as long as the circuit
memquota: CircuitAccount,
}
/// Information about an incoming stream request.
#[cfg(feature = "hs-service")]
#[derive(Debug, Deftly)]
#[derive_deftly(HasMemoryCost)]
pub(super) struct StreamReqInfo {
/// The [`IncomingStreamRequest`].
pub(super) req: IncomingStreamRequest,
/// The ID of the stream being requested.
pub(super) stream_id: StreamId,
/// The [`HopNum`].
//
// TODO: When we add support for exit relays, we need to turn this into an Option<HopNum>.
// (For outbound messages (towards relays), there is only one hop that can send them: the client.)
//
// TODO: For onion services, we might be able to enforce the HopNum earlier: we would never accept an
// incoming stream request from two separate hops. (There is only one that's valid.)
pub(super) hop_num: HopNum,
/// A channel for receiving messages from this stream.
#[deftly(has_memory_cost(indirect_size = "0"))] // estimate
pub(super) receiver: StreamMpscReceiver<UnparsedRelayMsg>,
/// A channel for sending messages to be sent on this stream.
#[deftly(has_memory_cost(indirect_size = "size_of::<AnyRelayMsg>()"))] // estimate
pub(super) msg_tx: StreamMpscSender<AnyRelayMsg>,
/// The memory quota account to be used for this stream
#[deftly(has_memory_cost(indirect_size = "0"))] // estimate (it contains an Arc)
pub(super) memquota: StreamAccount,
}
/// Data required for handling an incoming stream request.
#[cfg(feature = "hs-service")]
#[derive(educe::Educe)]
#[educe(Debug)]
struct IncomingStreamRequestHandler {
/// A sender for sharing information about an incoming stream request.
incoming_sender: StreamReqSender,
/// A [`AnyCmdChecker`] for validating incoming stream requests.
cmd_checker: AnyCmdChecker,
/// The hop to expect incoming stream requests from.
hop_num: HopNum,
/// An [`IncomingStreamRequestFilter`] for checking whether the user wants
/// this request, or wants to reject it immediately.
#[educe(Debug(ignore))]
filter: Box<dyn IncomingStreamRequestFilter>,
}
impl Reactor {
/// Create a new circuit reactor.
///
/// The reactor will send outbound messages on `channel`, receive incoming
/// messages on `input`, and identify this circuit by the channel-local
/// [`CircId`] provided.
///
/// The internal unique identifier for this circuit will be `unique_id`.
#[allow(clippy::type_complexity)] // TODO
pub(super) fn new(
channel: Arc<Channel>,
channel_id: CircId,
unique_id: UniqId,
input: CircuitRxReceiver,
memquota: CircuitAccount,
) -> (
Self,
mpsc::UnboundedSender<CtrlMsg>,
mpsc::UnboundedSender<CtrlCmd>,
oneshot::Receiver<void::Void>,
Arc<Mutex<MutableState>>,
) {
let crypto_out = OutboundClientCrypt::new();
let (control_tx, control_rx) = mpsc::unbounded();
let (command_tx, command_rx) = mpsc::unbounded();
let mutable = Arc::new(Mutex::new(MutableState::default()));
let (reactor_closed_tx, reactor_closed_rx) = oneshot::channel();
let chan_sender = SometimesUnboundedSink::new(channel.sender());
let reactor = Reactor {
control: control_rx,
command: command_rx,
reactor_closed_tx,
channel,
chan_sender,
input,
crypto_in: InboundClientCrypt::new(),
hops: vec![],
unique_id,
channel_id,
crypto_out,
meta_handler: None,
#[cfg(feature = "hs-service")]
incoming_stream_req_handler: None,
mutable: mutable.clone(),
memquota,
};
(reactor, control_tx, command_tx, reactor_closed_rx, mutable)
}
/// Launch the reactor, and run until the circuit closes or we
/// encounter an error.
///
/// Once this method returns, the circuit is dead and cannot be
/// used again.
pub async fn run(mut self) -> Result<()> {
trace!("{}: Running circuit reactor", self.unique_id);
let result: Result<()> = loop {
match self.run_once().await {
Ok(()) => (),
Err(ReactorError::Shutdown) => break Ok(()),
Err(ReactorError::Err(e)) => break Err(e),
}
};
trace!("{}: Circuit reactor stopped: {:?}", self.unique_id, result);
result
}
/// Helper for run: doesn't mark the circuit closed on finish. Only
/// processes one cell or control message.
async fn run_once(&mut self) -> StdResult<(), ReactorError> {
if self.hops.is_empty() {
self.wait_for_create().await?;
return Ok(());
}
let mut ready_streams = self.ready_streams_iterator();
// Note: We don't actually use the returned SinkSendable,
// and continue writing to the SometimesUboundedSink :(
let (cmd, _sendable) = select_biased! {
res = self.command.next() => {
let cmd = unwrap_or_shutdown!(self, res, "command channel drop")?;
return CommandHandler::new(self).handle(cmd);
},
res = self.chan_sender
.prepare_send_from(async {
select_biased! {
// Check whether we've got a control message pending.
ret = self.control.next() => {
let msg = unwrap_or_shutdown!(self, ret, "control drop")?;
Ok::<_, ReactorError>(Some(SelectResult::HandleControl(msg)))
},
// Check whether we've got an input message pending.
ret = self.input.next() => {
let cell = unwrap_or_shutdown!(self, ret, "input drop")?;
Ok(Some(SelectResult::HandleCell(cell)))
},
ret = ready_streams.next().fuse() => {
match ret {
Some(cmd) => {
let cmd = cmd?;
Ok(Some(SelectResult::Single(cmd)))
},
None => {
// There are no ready streams (for example, they may all be
// blocked due to congestion control), so there is nothing
// to do.
Ok(None)
}
}
}
}
}) => res?,
};
let cmd = cmd?;
let cmd = match cmd {
None => None,
Some(SelectResult::Single(cmd)) => Some(RunOnceCmd::Single(cmd)),
Some(SelectResult::HandleControl(ctrl)) => {
Some(RunOnceCmd::Single(ControlHandler::new(self).handle(ctrl)?))
}
Some(SelectResult::HandleCell(cell)) => self.handle_cell(cell)?,
};
if let Some(cmd) = cmd {
self.handle_run_once_cmd(cmd).await?;
}
Ok(())
}
/// Handle a [`RunOnceCmd`].
async fn handle_run_once_cmd(&mut self, cmd: RunOnceCmd) -> StdResult<(), ReactorError> {
match cmd {
RunOnceCmd::Single(cmd) => return self.handle_single_run_once_cmd(cmd).await,
RunOnceCmd::Multiple(cmds) => {
// While we know `sendable` is ready to accept *one* cell,
// we can't be certain it will be able to accept *all* of the cells
// that need to be sent here. This means we *may* end up buffering
// in its underlying SometimesUnboundedSink! That is OK, because
// RunOnceCmd::Multiple is only used for handling packed cells.
for cmd in cmds {
self.handle_single_run_once_cmd(cmd).await?;
}
}
}
Ok(())
}
/// Handle a [`RunOnceCmd`].
async fn handle_single_run_once_cmd(
&mut self,
cmd: RunOnceCmdInner,
) -> StdResult<(), ReactorError> {
match cmd {
RunOnceCmdInner::Send { cell, done } => {
// TODO: check the cc window
let res = self.send_relay_cell(cell).await;
if let Some(done) = done {
// Don't care if the receiver goes away
let _ = done.send(res.clone());
}
res?;
}
#[cfg(feature = "send-control-msg")]
RunOnceCmdInner::SendMsgAndInstallHandler { msg, handler, done } => {
let cell: Result<Option<SendRelayCell>> =
self.prepare_msg_and_install_handler(msg, handler);
match cell {
Ok(Some(cell)) => {
let outcome = self.send_relay_cell(cell).await;
// don't care if receiver goes away.
let _ = done.send(outcome.clone());
outcome?;
}
Ok(None) => {
// don't care if receiver goes away.
let _ = done.send(Ok(()));
}
Err(e) => {
// don't care if receiver goes away.
let _ = done.send(Err(e.clone()));
return Err(e.into());
}
}
}
RunOnceCmdInner::BeginStream { cell, done } => {
match cell {
Ok((cell, stream_id)) => {
let outcome = self.send_relay_cell(cell).await;
// don't care if receiver goes away.
let _ = done.send(outcome.clone().map(|_| stream_id));
outcome?;
}
Err(e) => {
// don't care if receiver goes away.
let _ = done.send(Err(e.clone()));
return Err(e.into());
}
}
}
RunOnceCmdInner::CloseStream {
hop_num,
sid,
behav,
reason,
done,
} => {
let res: Result<()> = async {
if let Some(hop) = self.hop_mut(hop_num) {
let res = hop.close_stream(sid, behav, reason)?;
if let Some(cell) = res {
self.send_relay_cell(cell).await?;
}
}
Ok(())
}
.await;
if let Some(done) = done {
// don't care if the sender goes away
let _ = done.send(res);
}
}
RunOnceCmdInner::HandleSendMe { hop, sendme } => {
// NOTE: it's okay to await. We are only awaiting on the congestion_signals
// future which *should* resolve immediately
let signals = self.congestion_signals().await;
self.handle_sendme(hop, sendme, signals)?;
}
RunOnceCmdInner::CleanShutdown => {
trace!("{}: reactor shutdown due to handled cell", self.unique_id);
return Err(ReactorError::Shutdown);
}
}
Ok(())
}
/// Returns a [`Stream`] of [`RunOnceCmdInner`] to poll from the main loop.
///
/// The iterator contains at most one [`RunOnceCmdInner`] for each hop,
/// representing the instructions for handling the ready-item, if any,
/// of its highest priority stream.
///
/// IMPORTANT: this stream locks the stream map mutexes of each `CircHop`!
/// To avoid contention, never create more than one [`Reactor::ready_streams_iterator`]
/// stream at a time!
fn ready_streams_iterator(&self) -> impl Stream<Item = Result<RunOnceCmdInner>> {
self.hops
.iter()
.enumerate()
.filter_map(|(i, hop)| {
if !hop.ccontrol.can_send() {
// We can't send anything on this hop that counts towards SENDME windows.
//
// In theory we could send messages that don't count towards
// windows (like `RESOLVE`), and process end-of-stream
// events (to send an `END`), but it's probably not worth
// doing an O(N) iteration over flow-control-ready streams
// to see if that's the case.
//
// This *doesn't* block outgoing flow-control messages (e.g.
// SENDME), which are initiated via the control-message
// channel, handled above.
//
// TODO: Consider revisiting. OTOH some extra throttling when circuit-level
// congestion control has "bottomed out" might not be so bad, and the
// alternatives have complexity and/or performance costs.
return None;
}
let hop_num = HopNum::from(i as u8);
let hop_map = Arc::clone(&self.hops[i].map);
Some(async move {
futures::future::poll_fn(move |cx| {
// Process an outbound message from the first ready stream on
// this hop. The stream map implements round robin scheduling to
// ensure fairness across streams.
// TODO: Consider looping here to process multiple ready
// streams. Need to be careful though to balance that with
// continuing to service incoming and control messages.
let mut hop_map = hop_map.lock().expect("lock poisoned");
let Some((sid, msg)) = hop_map.poll_ready_streams_iter(cx).next() else {
// No ready streams for this hop.
return Poll::Pending;
};
if msg.is_none() {
return Poll::Ready(Ok(RunOnceCmdInner::CloseStream {
hop_num,
sid,
behav: CloseStreamBehavior::default(),
reason: streammap::TerminateReason::StreamTargetClosed,
done: None,
}));
};
let msg = hop_map.take_ready_msg(sid).expect("msg disappeared");
#[allow(unused)] // unused in non-debug builds
let Some(StreamEntMut::Open(s)) = hop_map.get_mut(sid) else {
panic!("Stream {sid} disappeared");
};
debug_assert!(
s.can_send(&msg),
"Stream {sid} produced a message it can't send: {msg:?}"
);
let cell = SendRelayCell {
hop: hop_num,
early: false,
cell: AnyRelayMsgOuter::new(Some(sid), msg),
};
Poll::Ready(Ok(RunOnceCmdInner::Send { cell, done: None }))
})
.await
})
})
.collect::<FuturesUnordered<_>>()
}
/// Return the congestion signals for this reactor. This is used by congestion control module.
///
/// Note: This is only async because we need a Context to check the sink for readiness.
async fn congestion_signals(&mut self) -> CongestionSignals {
futures::future::poll_fn(|cx| -> Poll<CongestionSignals> {
Poll::Ready(CongestionSignals::new(
self.chan_sender.poll_ready_unpin_bool(cx).unwrap_or(false),
self.chan_sender.n_queued(),
))
})
.await
}
/// Wait for a [`CtrlMsg::Create`] to come along to set up the circuit.
///
/// Returns an error if an unexpected `CtrlMsg` is received.
async fn wait_for_create(&mut self) -> StdResult<(), ReactorError> {
let msg = select_biased! {
res = self.command.next() => {
let cmd = unwrap_or_shutdown!(self, res, "shutdown channel drop")?;
match cmd {
CtrlCmd::Shutdown => return self.handle_shutdown().map(|_| ()),
#[cfg(test)]
CtrlCmd::AddFakeHop {
relay_cell_format: format,
fwd_lasthop,
rev_lasthop,
params,
done,
} => {
self.handle_add_fake_hop(format, fwd_lasthop, rev_lasthop, ¶ms, done);
return Ok(())
},
_ => {
trace!("reactor shutdown due to unexpected command: {:?}", cmd);
return Err(Error::CircProto(format!("Unexpected control {cmd:?} on client circuit")).into());
}
}
},
res = self.control.next() => unwrap_or_shutdown!(self, res, "control drop")?,
};
match msg {
CtrlMsg::Create {
recv_created,
handshake,
params,
done,
} => {
self.handle_create(recv_created, handshake, ¶ms, done)
.await
}
_ => {
trace!("reactor shutdown due to unexpected cell: {:?}", msg);
Err(Error::CircProto(format!("Unexpected {msg:?} cell on client circuit")).into())
}
}
}
/// Handle a [`CtrlMsg::Create`] message.
async fn handle_create(
&mut self,
recv_created: oneshot::Receiver<CreateResponse>,
handshake: CircuitHandshake,
params: &CircParameters,
done: ReactorResultChannel<()>,
) -> StdResult<(), ReactorError> {
let ret = match handshake {
CircuitHandshake::CreateFast => self.create_firsthop_fast(recv_created, params).await,
CircuitHandshake::Ntor {
public_key,
ed_identity,
} => {
self.create_firsthop_ntor(recv_created, ed_identity, public_key, params)
.await
}
#[cfg(feature = "ntor_v3")]
CircuitHandshake::NtorV3 { public_key } => {
self.create_firsthop_ntor_v3(recv_created, public_key, params)
.await
}
};
let _ = done.send(ret); // don't care if sender goes away
// TODO: maybe we don't need to flush here?
// (we could let run_once() handle all the flushing)
self.chan_sender.flush().await?;
Ok(())
}
/// Handle a shutdown request.
fn handle_shutdown(&self) -> StdResult<Option<RunOnceCmdInner>, ReactorError> {
trace!(
"{}: reactor shutdown due to explicit request",
self.unique_id
);
Err(ReactorError::Shutdown)
}
/// Handle a [`CtrlMsg::AddFakeHop`] message.
#[cfg(test)]
fn handle_add_fake_hop(
&mut self,
format: RelayCellFormat,
fwd_lasthop: bool,
rev_lasthop: bool,
params: &CircParameters,
done: ReactorResultChannel<()>,
) {
use crate::circuit::test::DummyCrypto;
let dummy_peer_id = crate::circuit::OwnedChanTarget::builder()
.ed_identity([4; 32].into())
.rsa_identity([5; 20].into())
.build()
.expect("Could not construct fake hop");
let fwd = Box::new(DummyCrypto::new(fwd_lasthop));
let rev = Box::new(DummyCrypto::new(rev_lasthop));
let binding = None;
self.add_hop(
format,
path::HopDetail::Relay(dummy_peer_id),
fwd,
rev,
binding,
params,
);
let _ = done.send(Ok(()));
}
/// Helper: create the first hop of a circuit.
///
/// This is parameterized not just on the RNG, but a wrapper object to
/// build the right kind of create cell, and a handshake object to perform
/// the cryptographic handshake.
async fn create_impl<H, W, M>(
&mut self,
cell_protocol: RelayCryptLayerProtocol,
recvcreated: oneshot::Receiver<CreateResponse>,
wrap: &W,
key: &H::KeyType,
params: &CircParameters,
msg: &M,
) -> Result<()>
where
H: ClientHandshake + HandshakeAuxDataHandler,
W: CreateHandshakeWrap,
H::KeyGen: KeyGenerator,
M: Borrow<H::ClientAuxData>,
{
// We don't need to shut down the circuit on failure here, since this
// function consumes the PendingClientCirc and only returns
// a ClientCirc on success.
let (state, msg) = {
// done like this because holding the RNG across an await boundary makes the future
// non-Send
let mut rng = rand::thread_rng();
H::client1(&mut rng, key, msg)?
};
let create_cell = wrap.to_chanmsg(msg);
trace!(
"{}: Extending to hop 1 with {}",
self.unique_id,
create_cell.cmd()
);
self.send_msg(create_cell).await?;
let reply = recvcreated
.await
.map_err(|_| Error::CircProto("Circuit closed while waiting".into()))?;
let relay_handshake = wrap.decode_chanmsg(reply)?;
let (server_msg, keygen) = H::client2(state, relay_handshake)?;
H::handle_server_aux_data(self, params, &server_msg)?;
let relay_cell_format = cell_protocol.relay_cell_format();
let BoxedClientLayer { fwd, back, binding } =
cell_protocol.construct_layers(HandshakeRole::Initiator, keygen)?;
trace!("{}: Handshake complete; circuit created.", self.unique_id);
let peer_id = self.channel.target().clone();
self.add_hop(
relay_cell_format,
path::HopDetail::Relay(peer_id),
fwd,
back,
binding,
params,
);
Ok(())
}
/// Use the (questionable!) CREATE_FAST handshake to connect to the
/// first hop of this circuit.
///
/// There's no authentication in CREATE_FAST,
/// so we don't need to know whom we're connecting to: we're just
/// connecting to whichever relay the channel is for.
async fn create_firsthop_fast(
&mut self,
recvcreated: oneshot::Receiver<CreateResponse>,
params: &CircParameters,
) -> Result<()> {
// In a CREATE_FAST handshake, we can't negotiate a format other than this.
let protocol = RelayCryptLayerProtocol::Tor1(RelayCellFormat::V0);
let wrap = CreateFastWrap;
self.create_impl::<CreateFastClient, _, _>(protocol, recvcreated, &wrap, &(), params, &())
.await
}
/// Use the ntor handshake to connect to the first hop of this circuit.
///
/// Note that the provided keys must match the channel's target,
/// or the handshake will fail.
async fn create_firsthop_ntor(
&mut self,
recvcreated: oneshot::Receiver<CreateResponse>,
ed_identity: pk::ed25519::Ed25519Identity,
pubkey: NtorPublicKey,
params: &CircParameters,
) -> Result<()> {
// In an ntor handshake, we can't negotiate a format other than this.
let relay_cell_protocol = RelayCryptLayerProtocol::Tor1(RelayCellFormat::V0);
// Exit now if we have an Ed25519 or RSA identity mismatch.
let target = RelayIds::builder()
.ed_identity(ed_identity)
.rsa_identity(pubkey.id)
.build()
.expect("Unable to build RelayIds");
self.channel.check_match(&target)?;
let wrap = Create2Wrap {
handshake_type: HandshakeType::NTOR,
};
self.create_impl::<NtorClient, _, _>(
relay_cell_protocol,
recvcreated,
&wrap,
&pubkey,
params,
&(),
)
.await
}
/// Use the ntor-v3 handshake to connect to the first hop of this circuit.
///
/// Note that the provided key must match the channel's target,
/// or the handshake will fail.
#[cfg(feature = "ntor_v3")]
async fn create_firsthop_ntor_v3(
&mut self,
recvcreated: oneshot::Receiver<CreateResponse>,
pubkey: NtorV3PublicKey,
params: &CircParameters,
) -> Result<()> {
// Exit now if we have a mismatched key.
let target = RelayIds::builder()
.ed_identity(pubkey.id)
.build()
.expect("Unable to build RelayIds");
self.channel.check_match(&target)?;
// TODO: Add support for negotiating other formats.
let relay_cell_protocol = RelayCryptLayerProtocol::Tor1(RelayCellFormat::V0);
// TODO: Set client extensions. e.g. request congestion control
// if specified in `params`.
let client_extensions = [];
let wrap = Create2Wrap {
handshake_type: HandshakeType::NTOR_V3,
};
self.create_impl::<NtorV3Client, _, _>(
relay_cell_protocol,
recvcreated,
&wrap,
&pubkey,
params,
&client_extensions,
)
.await
}
/// Add a hop to the end of this circuit.
fn add_hop(
&mut self,
format: RelayCellFormat,
peer_id: path::HopDetail,
fwd: Box<dyn OutboundClientLayer + 'static + Send>,
rev: Box<dyn InboundClientLayer + 'static + Send>,
binding: Option<CircuitBinding>,
params: &CircParameters,
) {
let hop_num = (self.hops.len() as u8).into();
let hop = CircHop::new(self.unique_id, hop_num, format, params);
self.hops.push(hop);
self.crypto_in.add_layer(rev);
self.crypto_out.add_layer(fwd);
let mut mutable = self.mutable.lock().expect("poisoned lock");
Arc::make_mut(&mut mutable.path).push_hop(peer_id);
mutable.binding.push(binding);
}
/// Handle a RELAY cell on this circuit with stream ID 0.
fn handle_meta_cell(
&mut self,
hopnum: HopNum,
msg: UnparsedRelayMsg,
) -> Result<Option<RunOnceCmdInner>> {
// SENDME cells and TRUNCATED get handled internally by the circuit.
// TODO: This pattern (Check command, try to decode, map error) occurs
// several times, and would be good to extract simplify. Such
// simplification is obstructed by a couple of factors: First, that
// there is not currently a good way to get the RelayCmd from _type_ of
// a RelayMsg. Second, that decode() [correctly] consumes the
// UnparsedRelayMsg. I tried a macro-based approach, and didn't care
// for it. -nickm
if msg.cmd() == RelayCmd::SENDME {
let sendme = msg
.decode::<Sendme>()
.map_err(|e| Error::from_bytes_err(e, "sendme message"))?
.into_msg();
return Ok(Some(RunOnceCmdInner::HandleSendMe {
hop: hopnum,
sendme,
}));
}
if msg.cmd() == RelayCmd::TRUNCATED {
let truncated = msg
.decode::<Truncated>()
.map_err(|e| Error::from_bytes_err(e, "truncated message"))?
.into_msg();
let reason = truncated.reason();
debug!(
"{}: Truncated from hop {}. Reason: {} [{}]",
self.unique_id,
hopnum.display(),
reason.human_str(),
reason
);
return Ok(Some(RunOnceCmdInner::CleanShutdown));
}
trace!("{}: Received meta-cell {:?}", self.unique_id, msg);
// For all other command types, we'll only get them in response
// to another command, which should have registered a responder.
//
// TODO: that means that service-introduction circuits will need
// a different implementation, but that should be okay. We'll work
// something out.
if let Some(mut handler) = self.meta_handler.take() {
if handler.expected_hop() == hopnum {
// Somebody was waiting for a message -- maybe this message
let ret = handler.handle_msg(msg, self);
trace!(
"{}: meta handler completed with result: {:?}",
self.unique_id,
ret
);
match ret {
#[cfg(feature = "send-control-msg")]
Ok(MetaCellDisposition::Consumed) => {
self.meta_handler = Some(handler);
Ok(None)
}
Ok(MetaCellDisposition::ConversationFinished) => Ok(None),
#[cfg(feature = "send-control-msg")]
Ok(MetaCellDisposition::CloseCirc) => Ok(Some(RunOnceCmdInner::CleanShutdown)),
Err(e) => Err(e),
}
} else {
// Somebody wanted a message from a different hop! Put this
// one back.
self.meta_handler = Some(handler);
Err(Error::CircProto(format!(
"Unexpected {} cell from hop {} on client circuit",
msg.cmd(),
hopnum.display(),
)))
}
} else {
// No need to call shutdown here, since this error will
// propagate to the reactor shut it down.
Err(Error::CircProto(format!(
"Unexpected {} cell on client circuit",
msg.cmd()
)))
}
}
/// Handle a RELAY_SENDME cell on this circuit with stream ID 0.
fn handle_sendme(
&mut self,
hopnum: HopNum,
msg: Sendme,
signals: CongestionSignals,
) -> Result<Option<RunOnceCmdInner>> {
// No need to call "shutdown" on errors in this function;
// it's called from the reactor task and errors will propagate there.
let hop = self
.hop_mut(hopnum)
.ok_or_else(|| Error::CircProto(format!("Couldn't find hop {}", hopnum.display())))?;
let tag = match msg.into_tag() {
Some(v) => CircTag::try_from(v.as_slice())
.map_err(|_| Error::CircProto("malformed tag on circuit sendme".into()))?,
None => {
// Versions of Tor <=0.3.5 would omit a SENDME tag in this case;
// but we don't support those any longer.
return Err(Error::CircProto("missing tag on circuit sendme".into()));
}
};
// Update the CC object that we received a SENDME along with possible congestion signals.
hop.ccontrol.note_sendme_received(tag, signals)?;
Ok(None)
}
/// Send a message onto the circuit's channel.
///
/// If the channel is ready to accept messages, it will be sent immediately. If not, the message
/// will be enqueued for sending at a later iteration of the reactor loop.
///
/// # Note
///
/// Making use of the enqueuing capabilities of this function is discouraged! You should first
/// check whether the channel is ready to receive messages (`self.channel.poll_ready`), and
/// ideally use this to implement backpressure (such that you do not read from other sources
/// that would send here while you know you're unable to forward the messages on).
async fn send_msg(&mut self, msg: AnyChanMsg) -> Result<()> {
let cell = AnyChanCell::new(Some(self.channel_id), msg);
// Note: this future is always `Ready`, so await won't block.
Pin::new(&mut self.chan_sender).send_unbounded(cell).await?;
Ok(())
}
/// Encode `msg` and encrypt it, returning the resulting cell
/// and tag that should be expected for an authenticated SENDME sent
/// in response to that cell.
fn encode_relay_cell(
crypto_out: &mut OutboundClientCrypt,
hop: HopNum,
early: bool,
msg: AnyRelayMsgOuter,
) -> Result<(AnyChanMsg, &[u8; SENDME_TAG_LEN])> {
let mut body: RelayCellBody = msg
.encode(&mut rand::thread_rng())
.map_err(|e| Error::from_cell_enc(e, "relay cell body"))?
.into();
let tag = crypto_out.encrypt(&mut body, hop)?;
let msg = Relay::from(BoxedCellBody::from(body));
let msg = if early {
AnyChanMsg::RelayEarly(msg.into())
} else {
AnyChanMsg::Relay(msg)
};
Ok((msg, tag))
}
/// Encode `msg`, encrypt it, and send it to the 'hop'th hop.
///
/// If there is insufficient outgoing *circuit-level* or *stream-level*
/// SENDME window, an error is returned instead.
///
/// Does not check whether the cell is well-formed or reasonable.
async fn send_relay_cell(&mut self, msg: SendRelayCell) -> Result<()> {
let SendRelayCell {
hop,
early,
cell: msg,
} = msg;
let c_t_w = sendme::cmd_counts_towards_windows(msg.cmd());
let stream_id = msg.stream_id();
let hop_num = Into::<usize>::into(hop);
let circhop = &mut self.hops[hop_num];
// We need to apply stream-level flow control *before* encoding the message.
if c_t_w {
if let Some(stream_id) = stream_id {
let mut hop_map = circhop.map.lock().expect("lock poisoned");
let Some(StreamEntMut::Open(ent)) = hop_map.get_mut(stream_id) else {
warn!(
"{}: sending a relay cell for non-existent or non-open stream with ID {}!",
self.unique_id, stream_id
);
return Err(Error::CircProto(format!(
"tried to send a relay cell on non-open stream {}",
sv(stream_id),
)));
};
ent.take_capacity_to_send(msg.msg())?;
}
}
// NOTE(eta): Now that we've encrypted the cell, we *must* either send it or abort
// the whole circuit (e.g. by returning an error).
let (msg, tag) = Self::encode_relay_cell(&mut self.crypto_out, hop, early, msg)?;
// The cell counted for congestion control, inform our algorithm of such and pass down the
// tag for authenticated SENDMEs.
if c_t_w {
circhop.ccontrol.note_data_sent(tag)?;
}
let cell = AnyChanCell::new(Some(self.channel_id), msg);
Pin::new(&mut self.chan_sender).send_unbounded(cell).await?;
Ok(())
}
/// Try to install a given meta-cell handler to receive any unusual cells on
/// this circuit, along with a result channel to notify on completion.
fn set_meta_handler(&mut self, handler: Box<dyn MetaCellHandler + Send>) -> Result<()> {
if self.meta_handler.is_none() {
self.meta_handler = Some(handler);
Ok(())
} else {
Err(Error::from(internal!(
"Tried to install a meta-cell handler before the old one was gone."
)))
}
}
/// Try to install a given cell handler on this circuit.
#[cfg(feature = "hs-service")]
fn set_incoming_stream_req_handler(
&mut self,
handler: IncomingStreamRequestHandler,
) -> Result<()> {
if self.incoming_stream_req_handler.is_none() {
self.incoming_stream_req_handler = Some(handler);
Ok(())
} else {
Err(Error::from(internal!(
"Tried to install a BEGIN cell handler before the old one was gone."
)))
}
}
/// Prepare a `SendRelayCell` request, and install the given meta-cell handler.
fn prepare_msg_and_install_handler(
&mut self,
msg: Option<AnyRelayMsgOuter>,
handler: Option<Box<dyn MetaCellHandler + Send + 'static>>,
) -> Result<Option<SendRelayCell>> {
let msg = msg
.map(|msg| {
let handler = handler
.as_ref()
.or(self.meta_handler.as_ref())
.ok_or_else(|| internal!("tried to use an ended Conversation"))?;
Ok::<_, crate::Error>(SendRelayCell {
hop: handler.expected_hop(),
early: false,
cell: msg,
})
})
.transpose()?;
if let Some(handler) = handler {
self.set_meta_handler(handler)?;
}
Ok(msg)
}
/// Helper: process a cell on a channel. Most cells get ignored
/// or rejected; a few get delivered to circuits.
///
/// Return `CellStatus::CleanShutdown` if we should exit.
fn handle_cell(&mut self, cell: ClientCircChanMsg) -> Result<Option<RunOnceCmd>> {
trace!("{}: handling cell: {:?}", self.unique_id, cell);
use ClientCircChanMsg::*;
match cell {
Relay(r) => self.handle_relay_cell(r),
Destroy(d) => {
let reason = d.reason();
debug!(
"{}: Received DESTROY cell. Reason: {} [{}]",
self.unique_id,
reason.human_str(),
reason
);
self.handle_destroy_cell()
.map(|c| Some(RunOnceCmd::Single(c)))
}
}
}
/// Decode `cell`, returning its corresponding hop number, tag,
/// and decoded body.
fn decode_relay_cell(
&mut self,
cell: Relay,
) -> Result<(HopNum, CircTag, RelayCellDecoderResult)> {
let mut body = cell.into_relay_body().into();
// Decrypt the cell. If it's recognized, then find the
// corresponding hop.
let (hopnum, tag) = self.crypto_in.decrypt(&mut body)?;
// Make a copy of the authentication tag. TODO: I'd rather not
// copy it, but I don't see a way around it right now.
let tag = {
let mut tag_copy = [0_u8; SENDME_TAG_LEN];
// TODO(nickm): This could crash if the tag length changes. We'll
// have to refactor it then.
tag_copy.copy_from_slice(tag);
tag_copy
};
// Decode the cell.
let decode_res = self
.hop_mut(hopnum)
.ok_or_else(|| {
Error::from(internal!(
"Trying to decode cell from nonexistent hop {:?}",
hopnum
))
})?
.inbound
.decode(body.into())
.map_err(|e| Error::from_bytes_err(e, "relay cell"))?;
Ok((hopnum, tag.into(), decode_res))
}
/// React to a Relay or RelayEarly cell.
fn handle_relay_cell(&mut self, cell: Relay) -> Result<Option<RunOnceCmd>> {
let (hopnum, tag, decode_res) = self.decode_relay_cell(cell)?;
let c_t_w = decode_res.cmds().any(sendme::cmd_counts_towards_windows);
// Decrement the circuit sendme windows, and see if we need to
// send a sendme cell.
let send_circ_sendme = if c_t_w {
self.hop_mut(hopnum)
.ok_or_else(|| Error::CircProto("Sendme from nonexistent hop".into()))?
.ccontrol
.note_data_received()?
} else {
false
};
let mut run_once_cmds = vec![];
// If we do need to send a circuit-level SENDME cell, do so.
if send_circ_sendme {
// This always sends a V1 (tagged) sendme cell, and thereby assumes
// that SendmeEmitMinVersion is no more than 1. If the authorities
// every increase that parameter to a higher number, this will
// become incorrect. (Higher numbers are not currently defined.)
let sendme = Sendme::new_tag(tag.into());
let cell = AnyRelayMsgOuter::new(None, sendme.into());
run_once_cmds.push(RunOnceCmdInner::Send {
cell: SendRelayCell {
hop: hopnum,
early: false,
cell,
},
done: None,
});
// Inform congestion control of the SENDME we are sending. This is a circuit level one.
self.hop_mut(hopnum)
.ok_or_else(|| {
Error::from(internal!(
"Trying to send SENDME to nonexistent hop {:?}",
hopnum
))
})?
.ccontrol
.note_sendme_sent()?;
}
let (mut msgs, incomplete) = decode_res.into_parts();
while let Some(msg) = msgs.next() {
let msg_status = self.handle_relay_msg(hopnum, c_t_w, msg)?;
match msg_status {
None => continue,
Some(msg @ RunOnceCmdInner::CleanShutdown) => {
for m in msgs {
debug!(
"{id}: Ignoring relay msg received after triggering shutdown: {m:?}",
id = self.unique_id
);
}
if let Some(incomplete) = incomplete {
debug!(
"{id}: Ignoring partial relay msg received after triggering shutdown: {:?}",
incomplete,
id=self.unique_id,
);
}
run_once_cmds.push(msg);
return Ok(Some(RunOnceCmd::Multiple(run_once_cmds)));
}
Some(msg) => {
run_once_cmds.push(msg);
}
}
}
Ok(Some(RunOnceCmd::Multiple(run_once_cmds)))
}
/// Handle a single incoming relay message.
fn handle_relay_msg(
&mut self,
hopnum: HopNum,
cell_counts_toward_windows: bool,
msg: UnparsedRelayMsg,
) -> Result<Option<RunOnceCmdInner>> {
// If this msg wants/refuses to have a Stream ID, does it
// have/not have one?
let streamid = msg_streamid(&msg)?;
// If this doesn't have a StreamId, it's a meta cell,
// not meant for a particular stream.
let Some(streamid) = streamid else {
return self.handle_meta_cell(hopnum, msg);
};
let hop = self
.hop_mut(hopnum)
.ok_or_else(|| Error::CircProto("Cell from nonexistent hop!".into()))?;
let mut hop_map = hop.map.lock().expect("lock poisoned");
match hop_map.get_mut(streamid) {
Some(StreamEntMut::Open(ent)) => {
let message_closes_stream =
Self::deliver_msg_to_stream(streamid, ent, cell_counts_toward_windows, msg)?;
if message_closes_stream {
hop_map.ending_msg_received(streamid)?;
}
}
#[cfg(feature = "hs-service")]
Some(StreamEntMut::EndSent(_))
if matches!(
msg.cmd(),
RelayCmd::BEGIN | RelayCmd::BEGIN_DIR | RelayCmd::RESOLVE
) =>
{
// If the other side is sending us a BEGIN but hasn't yet acknowledged our END
// message, just remove the old stream from the map and stop waiting for a
// response
hop_map.ending_msg_received(streamid)?;
drop(hop_map);
return self.handle_incoming_stream_request(msg, streamid, hopnum);
}
Some(StreamEntMut::EndSent(EndSentStreamEnt { half_stream, .. })) => {
// We sent an end but maybe the other side hasn't heard.
match half_stream.handle_msg(msg)? {
StreamStatus::Open => {}
StreamStatus::Closed => {
hop_map.ending_msg_received(streamid)?;
}
}
}
#[cfg(feature = "hs-service")]
None if matches!(
msg.cmd(),
RelayCmd::BEGIN | RelayCmd::BEGIN_DIR | RelayCmd::RESOLVE
) =>
{
drop(hop_map);
return self.handle_incoming_stream_request(msg, streamid, hopnum);
}
_ => {
// No stream wants this message, or ever did.
return Err(Error::CircProto(
"Cell received on nonexistent stream!?".into(),
));
}
}
Ok(None)
}
/// Deliver `msg` to the specified open stream entry `ent`.
fn deliver_msg_to_stream(
streamid: StreamId,
ent: &mut OpenStreamEnt,
cell_counts_toward_windows: bool,
msg: UnparsedRelayMsg,
) -> Result<bool> {
// The stream for this message exists, and is open.
if msg.cmd() == RelayCmd::SENDME {
let _sendme = msg
.decode::<Sendme>()
.map_err(|e| Error::from_bytes_err(e, "Sendme message on stream"))?
.into_msg();
// We need to handle sendmes here, not in the stream's
// recv() method, or else we'd never notice them if the
// stream isn't reading.
ent.put_for_incoming_sendme()?;
return Ok(false);
}
let message_closes_stream = ent.cmd_checker.check_msg(&msg)? == StreamStatus::Closed;
if let Err(e) = Pin::new(&mut ent.sink).try_send(msg) {
if e.is_full() {
// If we get here, we either have a logic bug (!), or an attacker
// is sending us more cells than we asked for via congestion control.
return Err(Error::CircProto(format!(
"Stream sink would block; received too many cells on stream ID {}",
sv(streamid),
)));
}
if e.is_disconnected() && cell_counts_toward_windows {
// the other side of the stream has gone away; remember
// that we received a cell that we couldn't queue for it.
//
// Later this value will be recorded in a half-stream.
ent.dropped += 1;
}
}
Ok(message_closes_stream)
}
/// A helper for handling incoming stream requests.
#[cfg(feature = "hs-service")]
fn handle_incoming_stream_request(
&mut self,
msg: UnparsedRelayMsg,
stream_id: StreamId,
hop_num: HopNum,
) -> Result<Option<RunOnceCmdInner>> {
use syncview::ClientCircSyncView;
use tor_cell::relaycell::msg::EndReason;
use tor_error::into_internal;
use tor_log_ratelim::log_ratelim;
// We need to construct this early so that we don't double-borrow &mut self
let Some(handler) = self.incoming_stream_req_handler.as_mut() else {
return Err(Error::CircProto(
"Cannot handle BEGIN cells on this circuit".into(),
));
};
if hop_num != handler.hop_num {
return Err(Error::CircProto(format!(
"Expecting incoming streams from {}, but received {} cell from unexpected hop {}",
handler.hop_num.display(),
msg.cmd(),
hop_num.display()
)));
}
let message_closes_stream = handler.cmd_checker.check_msg(&msg)? == StreamStatus::Closed;
// TODO: we've already looked up the `hop` in handle_relay_cell, so we shouldn't
// have to look it up again! However, we can't pass the `&mut hop` reference from
// `handle_relay_cell` to this function, because that makes Rust angry (we'd be
// borrowing self as mutable more than once).
//
// TODO: we _could_ use self.hops.get_mut(..) instead self.hop_mut(..) inside
// handle_relay_cell to work around the problem described above
let hop = self
.hops
.get_mut(Into::<usize>::into(hop_num))
.ok_or(Error::CircuitClosed)?;
if message_closes_stream {
hop.map
.lock()
.expect("lock poisoned")
.ending_msg_received(stream_id)?;
return Ok(None);
}
let begin = msg
.decode::<Begin>()
.map_err(|e| Error::from_bytes_err(e, "Invalid Begin message"))?
.into_msg();
let req = IncomingStreamRequest::Begin(begin);
{
use crate::stream::IncomingStreamRequestDisposition::*;
let ctx = crate::stream::IncomingStreamRequestContext { request: &req };
// IMPORTANT: ClientCircSyncView::n_open_streams() (called via disposition() below)
// accesses the stream map mutexes!
//
// This means it's very important not to call this function while any of the hop's
// stream map mutex is held.
let view = ClientCircSyncView::new(&self.hops);
match handler.filter.as_mut().disposition(&ctx, &view)? {
Accept => {}
CloseCircuit => return Ok(Some(RunOnceCmdInner::CleanShutdown)),
RejectRequest(end) => {
let end_msg = AnyRelayMsgOuter::new(Some(stream_id), end.into());
let cell = SendRelayCell {
hop: hop_num,
early: false,
cell: end_msg,
};
return Ok(Some(RunOnceCmdInner::Send { cell, done: None }));
}
}
}
// TODO: Sadly, we need to look up `&mut hop` yet again,
// since we needed to pass `&self.hops` by reference to our filter above. :(
let hop = self
.hops
.get_mut(Into::<usize>::into(hop_num))
.ok_or(Error::CircuitClosed)?;
let memquota = StreamAccount::new(&self.memquota)?;
let (sender, receiver) = MpscSpec::new(STREAM_READER_BUFFER).new_mq(
self.chan_sender.as_inner().time_provider().clone(),
memquota.as_raw_account(),
)?;
let (msg_tx, msg_rx) = MpscSpec::new(super::CIRCUIT_BUFFER_SIZE).new_mq(
self.chan_sender.as_inner().time_provider().clone(),
memquota.as_raw_account(),
)?;
let send_window = sendme::StreamSendWindow::new(SEND_WINDOW_INIT);
let cmd_checker = DataCmdChecker::new_connected();
hop.map.lock().expect("lock poisoned").add_ent_with_id(
sender,
msg_rx,
send_window,
stream_id,
cmd_checker,
)?;
let outcome = Pin::new(&mut handler.incoming_sender).try_send(StreamReqInfo {
req,
stream_id,
hop_num,
msg_tx,
receiver,
memquota,
});
log_ratelim!("Delivering message to incoming stream handler"; outcome);
if let Err(e) = outcome {
if e.is_full() {
// The IncomingStreamRequestHandler's stream is full; it isn't
// handling requests fast enough. So instead, we reply with an
// END cell.
let end_msg = AnyRelayMsgOuter::new(
Some(stream_id),
End::new_with_reason(EndReason::RESOURCELIMIT).into(),
);
let cell = SendRelayCell {
hop: hop_num,
early: false,
cell: end_msg,
};
return Ok(Some(RunOnceCmdInner::Send { cell, done: None }));
} else if e.is_disconnected() {
// The IncomingStreamRequestHandler's stream has been dropped.
// In the Tor protocol as it stands, this always means that the
// circuit itself is out-of-use and should be closed. (See notes
// on `allow_stream_requests.`)
//
// Note that we will _not_ reach this point immediately after
// the IncomingStreamRequestHandler is dropped; we won't hit it
// until we next get an incoming request. Thus, if we do later
// want to add early detection for a dropped
// IncomingStreamRequestHandler, we need to do it elsewhere, in
// a different way.
debug!(
"{}: Incoming stream request receiver dropped",
self.unique_id
);
// This will _cause_ the circuit to get closed.
return Err(Error::CircuitClosed);
} else {
// There are no errors like this with the current design of
// futures::mpsc, but we shouldn't just ignore the possibility
// that they'll be added later.
return Err(Error::from((into_internal!(
"try_send failed unexpectedly"
))(e)));
}
}
Ok(None)
}
/// Helper: process a destroy cell.
#[allow(clippy::unnecessary_wraps)]
fn handle_destroy_cell(&mut self) -> Result<RunOnceCmdInner> {
// I think there is nothing more to do here.
Ok(RunOnceCmdInner::CleanShutdown)
}
/// Return the hop corresponding to `hopnum`, if there is one.
fn hop_mut(&mut self, hopnum: HopNum) -> Option<&mut CircHop> {
self.hops.get_mut(Into::<usize>::into(hopnum))
}
}
/// Return the stream ID of `msg`, if it has one.
///
/// Returns `Ok(None)` if `msg` is a meta cell.
fn msg_streamid(msg: &UnparsedRelayMsg) -> Result<Option<StreamId>> {
let cmd = msg.cmd();
let streamid = msg.stream_id();
if !cmd.accepts_streamid_val(streamid) {
return Err(Error::CircProto(format!(
"Invalid stream ID {} for relay command {}",
sv(StreamId::get_or_zero(streamid)),
msg.cmd()
)));
}
Ok(streamid)
}
impl Drop for Reactor {
fn drop(&mut self) {
let _ = self.channel.close_circuit(self.channel_id);
}
}
#[cfg(test)]
mod test {
// Tested in [`crate::circuit::test`].
}