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
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
// Copyright 2024 Saorsa Labs Ltd.
//
// This Saorsa Network Software is licensed under the General Public License (GPL), version 3.
// Please see the file LICENSE-GPL, or visit <http://www.gnu.org/licenses/> for the full text.
//
// Full details available at https://saorsalabs.com/licenses
use std::{
collections::{HashMap, VecDeque, hash_map},
convert::TryFrom,
fmt, mem,
net::{IpAddr, SocketAddr},
ops::{Index, IndexMut},
sync::Arc,
};
use indexmap::IndexMap;
use bytes::{BufMut, Bytes, BytesMut};
use rand::{Rng, RngCore, SeedableRng, rngs::StdRng};
use rustc_hash::FxHashMap;
use rustls;
use slab::Slab;
use thiserror::Error;
use tracing::{debug, error, trace, warn};
use crate::{
Duration, INITIAL_MTU, Instant, MAX_CID_SIZE, MIN_INITIAL_SIZE, RESET_TOKEN_SIZE, ResetToken,
Side, Transmit, TransportConfig, TransportError,
cid_generator::ConnectionIdGenerator,
coding::BufMutExt,
config::{ClientConfig, EndpointConfig, ServerConfig},
connection::{Connection, ConnectionError, SideArgs},
crypto::{self, Keys, UnsupportedVersion},
frame,
nat_traversal_api::PeerId,
packet::{
FixedLengthConnectionIdParser, Header, InitialHeader, InitialPacket, PacketDecodeError,
PacketNumber, PartialDecode, ProtectedInitialHeader,
},
relay::RelayStatisticsCollector,
shared::{
ConnectionEvent, ConnectionEventInner, ConnectionId, DatagramConnectionEvent, EcnCodepoint,
EndpointEvent, EndpointEventInner, IssuedCid,
},
token::{IncomingToken, InvalidRetryTokenError},
transport_parameters::{PreferredAddress, TransportParameters},
};
/// A queued relay request for bootstrap nodes
#[derive(Debug, Clone)]
struct RelayQueueItem {
/// Target peer ID for the relay
target_peer_id: PeerId,
/// Frame to be relayed
frame: frame::PunchMeNow,
/// When this relay request was created
created_at: Instant,
/// Number of relay attempts made
attempts: u32,
/// Last attempt time
last_attempt: Option<Instant>,
}
/// Relay queue management for bootstrap nodes
#[derive(Debug)]
struct RelayQueue {
/// Pending relay requests with insertion order and O(1) access
pending: IndexMap<u64, RelayQueueItem>,
/// Next sequence number for insertion order
next_seq: u64,
/// Maximum queue size to prevent memory exhaustion
max_queue_size: usize,
/// Timeout for relay requests
request_timeout: Duration,
/// Maximum retry attempts per request
max_retries: u32,
/// Minimum interval between retry attempts
retry_interval: Duration,
/// Rate limiting: track recent relay requests per peer
rate_limiter: HashMap<PeerId, VecDeque<Instant>>,
/// Maximum relays per peer per time window
max_relays_per_peer: usize,
/// Rate limiting time window
rate_limit_window: Duration,
/// Last time rate limiter was cleaned up (to avoid cleaning on every check)
last_rate_limit_cleanup: Option<Instant>,
}
/// Address discovery statistics
#[derive(Debug, Default, Clone)]
pub struct AddressDiscoveryStats {
/// Number of OBSERVED_ADDRESS frames sent
pub frames_sent: u64,
/// Number of OBSERVED_ADDRESS frames received
pub frames_received: u64,
/// Number of unique addresses discovered
pub addresses_discovered: u64,
/// Number of address changes detected
pub address_changes_detected: u64,
}
/// Relay statistics for monitoring and debugging
#[derive(Debug, Default, Clone)]
pub struct RelayStats {
/// Total relay requests received
pub requests_received: u64,
/// Successfully relayed requests
pub requests_relayed: u64,
/// Failed relay requests (peer not found)
pub requests_failed: u64,
/// Requests dropped due to queue full
pub requests_dropped: u64,
/// Requests timed out
pub requests_timed_out: u64,
/// Requests dropped due to rate limiting
pub requests_rate_limited: u64,
/// Current queue size
pub current_queue_size: usize,
}
impl RelayQueue {
/// Create a new relay queue with default settings
fn new() -> Self {
Self {
pending: IndexMap::new(),
next_seq: 0,
max_queue_size: 1000, // Reasonable default
request_timeout: Duration::from_secs(30), // 30 second timeout
max_retries: 3,
retry_interval: Duration::from_millis(500), // 500ms between retries
rate_limiter: HashMap::new(),
max_relays_per_peer: 10, // Max 10 relays per peer per time window
rate_limit_window: Duration::from_secs(60), // 1 minute window
last_rate_limit_cleanup: None,
}
}
/// Add a relay request to the queue
fn enqueue(&mut self, target_peer_id: PeerId, frame: frame::PunchMeNow, now: Instant) -> bool {
// Check queue size limit
if self.pending.len() >= self.max_queue_size {
warn!(
"Relay queue full, dropping request for peer {:?}",
target_peer_id
);
return false;
}
// Check rate limit for this peer
if !self.check_rate_limit(target_peer_id, now) {
warn!(
"Rate limit exceeded for peer {:?}, dropping relay request",
target_peer_id
);
return false;
}
let item = RelayQueueItem {
target_peer_id,
frame,
created_at: now,
attempts: 0,
last_attempt: None,
};
let seq = self.next_seq;
self.next_seq += 1;
self.pending.insert(seq, item);
// Record this request for rate limiting
self.record_relay_request(target_peer_id, now);
trace!(
"Queued relay request for peer {:?}, queue size: {}",
target_peer_id,
self.pending.len()
);
true
}
/// Check if a relay request is within rate limits
fn check_rate_limit(&mut self, peer_id: PeerId, now: Instant) -> bool {
// Only clean up periodically (every 10 seconds) to reduce overhead
const CLEANUP_INTERVAL: Duration = Duration::from_secs(10);
let should_cleanup = self
.last_rate_limit_cleanup
.is_none_or(|last| now.saturating_duration_since(last) >= CLEANUP_INTERVAL);
if should_cleanup {
self.cleanup_rate_limiter(now);
self.last_rate_limit_cleanup = Some(now);
}
// Check current request count for this peer
if let Some(requests) = self.rate_limiter.get(&peer_id) {
requests.len() < self.max_relays_per_peer
} else {
true // No previous requests, allow
}
}
/// Record a relay request for rate limiting
fn record_relay_request(&mut self, peer_id: PeerId, now: Instant) {
self.rate_limiter.entry(peer_id).or_default().push_back(now);
}
/// Clean up old rate limiting entries
fn cleanup_rate_limiter(&mut self, now: Instant) {
self.rate_limiter.retain(|_, requests| {
requests.retain(|&request_time| {
now.saturating_duration_since(request_time) <= self.rate_limit_window
});
!requests.is_empty()
});
}
/// Get the next relay request that's ready to be processed
fn next_ready(&mut self, now: Instant) -> Option<RelayQueueItem> {
// Find the first request that's ready to be retried
let mut expired_keys = Vec::new();
let mut ready_key = None;
for (seq, item) in &self.pending {
// Check if request has timed out
if now.saturating_duration_since(item.created_at) > self.request_timeout {
expired_keys.push(*seq);
continue;
}
// Check if it's ready for retry
if item.attempts == 0
|| item
.last_attempt
.is_none_or(|last| now.saturating_duration_since(last) >= self.retry_interval)
{
ready_key = Some(*seq);
break;
}
}
// Remove expired items
for key in expired_keys {
if let Some(expired) = self.pending.shift_remove(&key) {
debug!(
"Relay request for peer {:?} timed out after {:?}",
expired.target_peer_id,
now.saturating_duration_since(expired.created_at)
);
}
}
// Return ready item if found
if let Some(key) = ready_key {
if let Some(mut item) = self.pending.shift_remove(&key) {
item.attempts += 1;
item.last_attempt = Some(now);
return Some(item);
}
}
None
}
/// Requeue a failed relay request if it hasn't exceeded max retries
fn requeue_failed(&mut self, item: RelayQueueItem) {
if item.attempts < self.max_retries {
trace!(
"Requeuing failed relay request for peer {:?}, attempt {}/{}",
item.target_peer_id, item.attempts, self.max_retries
);
let seq = self.next_seq;
self.next_seq += 1;
self.pending.insert(seq, item);
} else {
debug!(
"Dropping relay request for peer {:?} after {} failed attempts",
item.target_peer_id, item.attempts
);
}
}
/// Clean up expired requests and return number of items cleaned
fn cleanup_expired(&mut self, now: Instant) -> usize {
let initial_len = self.pending.len();
let timeout = self.request_timeout;
// Use retain for O(n) in-place removal instead of O(n) collect + O(n) remove
self.pending.retain(|_seq, item| {
let expired = now.saturating_duration_since(item.created_at) > timeout;
if expired {
debug!(
"Removing expired relay request for peer {:?}",
item.target_peer_id
);
}
!expired
});
initial_len - self.pending.len()
}
/// Get current queue length
fn len(&self) -> usize {
self.pending.len()
}
}
/// The main entry point to the library
///
/// This object performs no I/O whatsoever. Instead, it consumes incoming packets and
/// connection-generated events via `handle` and `handle_event`.
pub struct Endpoint {
rng: StdRng,
index: ConnectionIndex,
connections: Slab<ConnectionMeta>,
local_cid_generator: Box<dyn ConnectionIdGenerator>,
config: Arc<EndpointConfig>,
server_config: Option<Arc<ServerConfig>>,
/// Whether the underlying UDP socket promises not to fragment packets
allow_mtud: bool,
/// Time at which a stateless reset was most recently sent
last_stateless_reset: Option<Instant>,
/// Buffered Initial and 0-RTT messages for pending incoming connections
incoming_buffers: Slab<IncomingBuffer>,
all_incoming_buffers_total_bytes: u64,
/// Mapping from peer IDs to connection handles for relay functionality
peer_connections: HashMap<PeerId, ConnectionHandle>,
/// Relay queue for bootstrap nodes
relay_queue: RelayQueue,
/// Relay statistics
relay_stats: RelayStats,
/// Comprehensive relay statistics collector
relay_stats_collector: RelayStatisticsCollector,
/// Whether address discovery is enabled (default: true)
address_discovery_enabled: bool,
/// Address change callback
address_change_callback: Option<Box<dyn Fn(Option<SocketAddr>, SocketAddr) + Send + Sync>>,
/// Pending relay events to be sent to other connections
/// These are generated when a coordinator receives a PUNCH_ME_NOW with target_peer_id
pending_relay_events: Vec<(ConnectionHandle, ConnectionEvent)>,
}
impl Endpoint {
/// Create a new endpoint
///
/// `allow_mtud` enables path MTU detection when requested by `Connection` configuration for
/// better performance. This requires that outgoing packets are never fragmented, which can be
/// achieved via e.g. the `IPV6_DONTFRAG` socket option.
///
/// If `rng_seed` is provided, it will be used to initialize the endpoint's rng (having priority
/// over the rng seed configured in [`EndpointConfig`]). Note that the `rng_seed` parameter will
/// be removed in a future release, so prefer setting it to `None` and configuring rng seeds
/// using [`EndpointConfig::rng_seed`].
pub fn new(
config: Arc<EndpointConfig>,
server_config: Option<Arc<ServerConfig>>,
allow_mtud: bool,
rng_seed: Option<[u8; 32]>,
) -> Self {
let rng_seed = rng_seed.or(config.rng_seed);
Self {
rng: rng_seed.map_or(StdRng::from_entropy(), StdRng::from_seed),
index: ConnectionIndex::default(),
connections: Slab::new(),
local_cid_generator: (config.connection_id_generator_factory.as_ref())(),
config,
server_config,
allow_mtud,
last_stateless_reset: None,
incoming_buffers: Slab::new(),
all_incoming_buffers_total_bytes: 0,
peer_connections: HashMap::new(),
relay_queue: RelayQueue::new(),
relay_stats: RelayStats::default(),
relay_stats_collector: RelayStatisticsCollector::new(),
address_discovery_enabled: true, // Default to enabled
address_change_callback: None,
pending_relay_events: Vec::new(),
}
}
/// Replace the server configuration, affecting new incoming connections only
pub fn set_server_config(&mut self, server_config: Option<Arc<ServerConfig>>) {
self.server_config = server_config;
}
/// Register a peer ID with a connection handle for relay functionality
pub fn register_peer(&mut self, peer_id: PeerId, connection_handle: ConnectionHandle) {
self.peer_connections.insert(peer_id, connection_handle);
trace!(
"Registered peer {:?} with connection {:?}",
peer_id, connection_handle
);
}
/// Unregister a peer ID from the connection mapping
pub fn unregister_peer(&mut self, peer_id: &PeerId) {
if let Some(handle) = self.peer_connections.remove(peer_id) {
trace!(
"Unregistered peer {:?} from connection {:?}",
peer_id, handle
);
}
}
/// Look up a connection handle for a given peer ID
pub fn lookup_peer_connection(&self, peer_id: &PeerId) -> Option<ConnectionHandle> {
self.peer_connections.get(peer_id).copied()
}
/// Queue a frame for relay to a target peer
pub(crate) fn queue_frame_for_peer(
&mut self,
peer_id: &PeerId,
frame: frame::PunchMeNow,
) -> bool {
self.relay_stats.requests_received += 1;
if let Some(ch) = self.lookup_peer_connection(peer_id) {
// Peer is currently connected, try to relay immediately
if self.relay_frame_to_connection(ch, frame.clone()) {
self.relay_stats.requests_relayed += 1;
// Record successful rate limiting decision
self.relay_stats_collector.record_rate_limit(true);
trace!(
"Immediately relayed frame to peer {:?} via connection {:?}",
peer_id, ch
);
return true;
}
}
// Peer not connected or immediate relay failed, queue for later
let now = Instant::now();
if self.relay_queue.enqueue(*peer_id, frame, now) {
self.relay_stats.current_queue_size = self.relay_queue.len();
// Record successful rate limiting decision
self.relay_stats_collector.record_rate_limit(true);
trace!("Queued relay request for peer {:?}", peer_id);
true
} else {
// Check if it was rate limited or queue full
if !self.relay_queue.check_rate_limit(*peer_id, now) {
self.relay_stats.requests_rate_limited += 1;
// Record rate limiting rejection
self.relay_stats_collector.record_rate_limit(false);
// Record error
self.relay_stats_collector.record_error("rate_limited");
} else {
self.relay_stats.requests_dropped += 1;
// Record error for queue full
self.relay_stats_collector
.record_error("resource_exhausted");
}
false
}
}
/// Attempt to relay a frame to a specific connection
fn relay_frame_to_connection(
&mut self,
ch: ConnectionHandle,
frame: frame::PunchMeNow,
) -> bool {
// Queue the PunchMeNow frame to the connection via a connection event
let event = ConnectionEvent(ConnectionEventInner::QueuePunchMeNow(frame));
if self.connections.get(ch.0).is_some() {
// Store the event to be processed by the high-level layer
// The high-level endpoint will drain these and send to the appropriate connections
tracing::info!("Queueing PUNCH_ME_NOW relay event for connection {:?}", ch);
self.pending_relay_events.push((ch, event));
true
} else {
tracing::warn!("Cannot relay PUNCH_ME_NOW: connection {:?} not found", ch);
false
}
}
/// Drain pending relay events that need to be sent to connections
///
/// This returns events that were queued when a coordinator received a PUNCH_ME_NOW
/// with target_peer_id set. The high-level layer should process these by sending
/// the events to the appropriate connections.
pub fn drain_relay_events(
&mut self,
) -> impl Iterator<Item = (ConnectionHandle, ConnectionEvent)> + '_ {
self.pending_relay_events.drain(..)
}
/// Set the peer ID for an existing connection
pub fn set_connection_peer_id(&mut self, connection_handle: ConnectionHandle, peer_id: PeerId) {
if let Some(connection) = self.connections.get_mut(connection_handle.0) {
connection.peer_id = Some(peer_id);
self.register_peer(peer_id, connection_handle);
// Process any queued relay requests for this peer
self.process_queued_relays_for_peer(peer_id);
}
}
/// Process queued relay requests for a specific peer that just connected
fn process_queued_relays_for_peer(&mut self, peer_id: PeerId) {
let mut processed = 0;
// Collect only the sequence numbers for items to process (avoid cloning items)
let keys_to_process: Vec<u64> = self
.relay_queue
.pending
.iter()
.filter_map(|(seq, item)| {
if item.target_peer_id == peer_id {
Some(*seq)
} else {
None
}
})
.collect();
// Remove and process items by key
for key in keys_to_process {
if let Some(item) = self.relay_queue.pending.shift_remove(&key) {
if let Some(ch) = self.lookup_peer_connection(&peer_id) {
if self.relay_frame_to_connection(ch, item.frame.clone()) {
self.relay_stats.requests_relayed += 1;
processed += 1;
trace!("Processed queued relay for peer {:?}", peer_id);
} else {
// Failed to relay, requeue
self.relay_queue.requeue_failed(item);
self.relay_stats.requests_failed += 1;
}
}
}
}
self.relay_stats.current_queue_size = self.relay_queue.len();
if processed > 0 {
debug!("Processed {processed} queued relay requests for peer {peer_id:?}");
}
}
/// Process pending relay requests (should be called periodically)
pub fn process_relay_queue(&mut self) {
let now = Instant::now();
let mut processed = 0;
let mut failed = 0;
// Process ready relay requests
while let Some(item) = self.relay_queue.next_ready(now) {
if let Some(ch) = self.lookup_peer_connection(&item.target_peer_id) {
if self.relay_frame_to_connection(ch, item.frame.clone()) {
self.relay_stats.requests_relayed += 1;
processed += 1;
trace!(
"Successfully relayed frame to peer {:?}",
item.target_peer_id
);
} else {
// Failed to relay, requeue for retry
self.relay_queue.requeue_failed(item);
self.relay_stats.requests_failed += 1;
// Record connection failure error
self.relay_stats_collector
.record_error("connection_failure");
failed += 1;
}
} else {
// Peer not connected, requeue for later
self.relay_queue.requeue_failed(item);
// Record peer not found error
self.relay_stats_collector.record_error("peer_not_found");
failed += 1;
}
}
// Clean up expired requests
let expired = self.relay_queue.cleanup_expired(now);
if expired > 0 {
self.relay_stats.requests_timed_out += expired as u64;
// Record timeout errors for each expired request
for _ in 0..expired {
self.relay_stats_collector.record_error("request_timeout");
}
debug!("Cleaned up {} expired relay requests", expired);
}
self.relay_stats.current_queue_size = self.relay_queue.len();
if processed > 0 || failed > 0 {
trace!(
"Relay queue processing: {} processed, {} failed, {} in queue",
processed,
failed,
self.relay_queue.len()
);
}
}
/// Get relay statistics for monitoring
pub fn relay_stats(&self) -> &RelayStats {
&self.relay_stats
}
/// Get comprehensive relay statistics for monitoring and analysis
pub fn comprehensive_relay_stats(&self) -> crate::relay::RelayStatistics {
// Update the collector with current queue stats before collecting
self.relay_stats_collector
.update_queue_stats(&self.relay_stats);
self.relay_stats_collector.collect_statistics()
}
/// Get relay statistics collector for external registration of components
pub fn relay_stats_collector(&self) -> &RelayStatisticsCollector {
&self.relay_stats_collector
}
/// Get relay queue length
pub fn relay_queue_len(&self) -> usize {
self.relay_queue.len()
}
/// Process `EndpointEvent`s emitted from related `Connection`s
///
/// In turn, processing this event may return a `ConnectionEvent` for the same `Connection`.
pub fn handle_event(
&mut self,
ch: ConnectionHandle,
event: EndpointEvent,
) -> Option<ConnectionEvent> {
use EndpointEventInner::*;
match event.0 {
EndpointEventInner::NeedIdentifiers(now, n) => {
return Some(self.send_new_identifiers(now, ch, n));
}
ResetToken(remote, token) => {
if let Some(old) = self.connections[ch].reset_token.replace((remote, token)) {
self.index.connection_reset_tokens.remove(old.0, old.1);
}
if self.index.connection_reset_tokens.insert(remote, token, ch) {
warn!("duplicate reset token");
}
}
RetireConnectionId(now, seq, allow_more_cids) => {
if let Some(cid) = self.connections[ch].loc_cids.remove(&seq) {
trace!("peer retired CID {}: {}", seq, cid);
self.index.retire(cid);
if allow_more_cids {
return Some(self.send_new_identifiers(now, ch, 1));
}
}
}
RelayPunchMeNow(target_peer_id, punch_me_now) => {
// Handle relay request from bootstrap node
let peer_id = PeerId(target_peer_id);
if self.queue_frame_for_peer(&peer_id, punch_me_now) {
trace!(
"Successfully queued PunchMeNow frame for relay to peer {:?}",
peer_id
);
} else {
warn!("Failed to queue PunchMeNow relay for peer {:?}", peer_id);
}
}
SendAddressFrame(add_address_frame) => {
// Convert to a connection event so the connection queues the frame for transmit
return Some(ConnectionEvent(ConnectionEventInner::QueueAddAddress(
add_address_frame,
)));
}
NatCandidateValidated { address, challenge } => {
// Handle successful NAT traversal candidate validation
trace!(
"NAT candidate validation succeeded for {} with challenge {:016x}",
address, challenge
);
// The validation success is primarily handled by the connection-level state machine
// This event serves as notification to the endpoint for potential coordination
// with other components or logging/metrics collection
debug!("NAT candidate {} validated successfully", address);
}
TryConnectTo {
request_id,
target_address,
timeout_ms,
requester_connection,
requested_at: _,
} => {
// Handle TryConnectTo request from a peer
// This is used for NAT callback testing - a peer asks us to try connecting
// to a target to verify connectivity
trace!(
"TryConnectTo request received: request_id={}, target={}, timeout={}ms, from={}",
request_id, target_address, timeout_ms, requester_connection
);
// Since the endpoint is synchronous and we can't spawn async tasks here,
// we'll queue a response. The actual connection attempt would need to be
// handled by the higher-level async runtime.
// For now, we queue a "not implemented" response to acknowledge the request.
debug!(
"TryConnectTo: endpoint received callback request for {}",
target_address
);
// TODO: In the async wrapper (high_level/mod.rs), implement the actual
// connection attempt and send back the TryConnectToResponse.
// For now, this event is acknowledged but not acted upon at the endpoint level.
}
Drained => {
if let Some(conn) = self.connections.try_remove(ch.0) {
self.index.remove(&conn);
// Clean up peer connection mapping if this connection has a peer ID
if let Some(peer_id) = conn.peer_id {
self.peer_connections.remove(&peer_id);
trace!("Cleaned up peer connection mapping for {:?}", peer_id);
}
} else {
// This indicates a bug in downstream code, which could cause spurious
// connection loss instead of this error if the CID was (re)allocated prior to
// the illegal call.
error!(id = ch.0, "unknown connection drained");
}
}
}
None
}
/// Process an incoming UDP datagram
pub fn handle(
&mut self,
now: Instant,
remote: SocketAddr,
local_ip: Option<IpAddr>,
ecn: Option<EcnCodepoint>,
data: BytesMut,
buf: &mut Vec<u8>,
) -> Option<DatagramEvent> {
// Partially decode packet or short-circuit if unable
let datagram_len = data.len();
let event = match PartialDecode::new(
data,
&FixedLengthConnectionIdParser::new(self.local_cid_generator.cid_len()),
&self.config.supported_versions,
self.config.grease_quic_bit,
) {
Ok((first_decode, remaining)) => DatagramConnectionEvent {
now,
remote,
ecn,
first_decode,
remaining,
},
Err(PacketDecodeError::UnsupportedVersion {
src_cid,
dst_cid,
version,
}) => {
if self.server_config.is_none() {
debug!("dropping packet with unsupported version");
return None;
}
trace!("sending version negotiation");
// Negotiate versions
Header::VersionNegotiate {
random: self.rng.r#gen::<u8>() | 0x40,
src_cid: dst_cid,
dst_cid: src_cid,
}
.encode(buf);
// Grease with a reserved version
buf.write::<u32>(match version {
0x0a1a_2a3a => 0x0a1a_2a4a,
_ => 0x0a1a_2a3a,
});
for &version in &self.config.supported_versions {
buf.write(version);
}
return Some(DatagramEvent::Response(Transmit {
destination: remote,
ecn: None,
size: buf.len(),
segment_size: None,
src_ip: local_ip,
}));
}
Err(e) => {
trace!("malformed header: {}", e);
return None;
}
};
let addresses = FourTuple { remote, local_ip };
let dst_cid = event.first_decode.dst_cid();
if let Some(route_to) = self.index.get(&addresses, &event.first_decode) {
// Handle packet on existing connection
match route_to {
RouteDatagramTo::Incoming(incoming_idx) => {
let incoming_buffer = &mut self.incoming_buffers[incoming_idx];
let Some(config) = &self.server_config else {
debug!("no server config available to buffer incoming datagram");
return None;
};
if incoming_buffer
.total_bytes
.checked_add(datagram_len as u64)
.is_some_and(|n| n <= config.incoming_buffer_size)
&& self
.all_incoming_buffers_total_bytes
.checked_add(datagram_len as u64)
.is_some_and(|n| n <= config.incoming_buffer_size_total)
{
incoming_buffer.datagrams.push(event);
incoming_buffer.total_bytes += datagram_len as u64;
self.all_incoming_buffers_total_bytes += datagram_len as u64;
}
None
}
RouteDatagramTo::Connection(ch) => Some(DatagramEvent::ConnectionEvent(
ch,
ConnectionEvent(ConnectionEventInner::Datagram(event)),
)),
}
} else if event.first_decode.initial_header().is_some() {
// Potentially create a new connection
self.handle_first_packet(datagram_len, event, addresses, buf)
} else if event.first_decode.has_long_header() {
debug!(
"ignoring non-initial packet for unknown connection {}",
dst_cid
);
None
} else if !event.first_decode.is_initial()
&& self.local_cid_generator.validate(dst_cid).is_err()
{
// If we got this far, we're receiving a seemingly valid packet for an unknown
// connection. Send a stateless reset if possible.
debug!("dropping packet with invalid CID");
None
} else if dst_cid.is_empty() {
trace!("dropping unrecognized short packet without ID");
None
} else {
self.stateless_reset(now, datagram_len, addresses, *dst_cid, buf)
.map(DatagramEvent::Response)
}
}
fn stateless_reset(
&mut self,
now: Instant,
inciting_dgram_len: usize,
addresses: FourTuple,
dst_cid: ConnectionId,
buf: &mut Vec<u8>,
) -> Option<Transmit> {
if self
.last_stateless_reset
.is_some_and(|last| last + self.config.min_reset_interval > now)
{
debug!("ignoring unexpected packet within minimum stateless reset interval");
return None;
}
/// Minimum amount of padding for the stateless reset to look like a short-header packet
const MIN_PADDING_LEN: usize = 5;
// Prevent amplification attacks and reset loops by ensuring we pad to at most 1 byte
// smaller than the inciting packet.
let max_padding_len = match inciting_dgram_len.checked_sub(RESET_TOKEN_SIZE) {
Some(headroom) if headroom > MIN_PADDING_LEN => headroom - 1,
_ => {
debug!(
"ignoring unexpected {} byte packet: not larger than minimum stateless reset size",
inciting_dgram_len
);
return None;
}
};
debug!(
"sending stateless reset for {} to {}",
dst_cid, addresses.remote
);
self.last_stateless_reset = Some(now);
// Resets with at least this much padding can't possibly be distinguished from real packets
const IDEAL_MIN_PADDING_LEN: usize = MIN_PADDING_LEN + MAX_CID_SIZE;
// Always randomize padding length to prevent fingerprinting
let padding_len = if max_padding_len <= MIN_PADDING_LEN {
// Minimum case: no room for randomization
max_padding_len
} else if max_padding_len <= IDEAL_MIN_PADDING_LEN {
// Small packet: randomize within available range
self.rng.gen_range(MIN_PADDING_LEN..=max_padding_len)
} else {
// Normal case: randomize above ideal minimum
self.rng.gen_range(IDEAL_MIN_PADDING_LEN..max_padding_len)
};
buf.reserve(padding_len + RESET_TOKEN_SIZE);
buf.resize(padding_len, 0);
self.rng.fill_bytes(&mut buf[0..padding_len]);
buf[0] = 0b0100_0000 | (buf[0] >> 2);
buf.extend_from_slice(&ResetToken::new(&*self.config.reset_key, dst_cid));
debug_assert!(buf.len() < inciting_dgram_len);
Some(Transmit {
destination: addresses.remote,
ecn: None,
size: buf.len(),
segment_size: None,
src_ip: addresses.local_ip,
})
}
/// Initiate a connection
pub fn connect(
&mut self,
now: Instant,
config: ClientConfig,
remote: SocketAddr,
server_name: &str,
) -> Result<(ConnectionHandle, Connection), ConnectError> {
if self.cids_exhausted() {
return Err(ConnectError::CidsExhausted);
}
if remote.port() == 0 || remote.ip().is_unspecified() {
return Err(ConnectError::InvalidRemoteAddress(remote));
}
if !self.config.supported_versions.contains(&config.version) {
return Err(ConnectError::UnsupportedVersion);
}
let remote_id = (config.initial_dst_cid_provider)();
trace!(initial_dcid = %remote_id);
let ch = ConnectionHandle(self.connections.vacant_key());
let loc_cid = self.new_cid(ch);
let params = TransportParameters::new(
&config.transport,
&self.config,
self.local_cid_generator.as_ref(),
loc_cid,
None,
&mut self.rng,
)?;
let tls = config
.crypto
.start_session(config.version, server_name, ¶ms)?;
let conn = self.add_connection(
ch,
config.version,
remote_id,
loc_cid,
remote_id,
FourTuple {
remote,
local_ip: None,
},
now,
tls,
config.transport,
SideArgs::Client {
token_store: config.token_store,
server_name: server_name.into(),
},
);
Ok((ch, conn))
}
fn send_new_identifiers(
&mut self,
now: Instant,
ch: ConnectionHandle,
num: u64,
) -> ConnectionEvent {
let mut ids = vec![];
for _ in 0..num {
let id = self.new_cid(ch);
let meta = &mut self.connections[ch];
let sequence = meta.cids_issued;
meta.cids_issued += 1;
meta.loc_cids.insert(sequence, id);
ids.push(IssuedCid {
sequence,
id,
reset_token: ResetToken::new(&*self.config.reset_key, id),
});
}
ConnectionEvent(ConnectionEventInner::NewIdentifiers(ids, now))
}
/// Generate a connection ID for `ch`
fn new_cid(&mut self, ch: ConnectionHandle) -> ConnectionId {
loop {
let cid = self.local_cid_generator.generate_cid();
if cid.is_empty() {
// Zero-length CID; nothing to track
debug_assert_eq!(self.local_cid_generator.cid_len(), 0);
return cid;
}
if let hash_map::Entry::Vacant(e) = self.index.connection_ids.entry(cid) {
e.insert(ch);
break cid;
}
}
}
fn handle_first_packet(
&mut self,
datagram_len: usize,
event: DatagramConnectionEvent,
addresses: FourTuple,
buf: &mut Vec<u8>,
) -> Option<DatagramEvent> {
let dst_cid = event.first_decode.dst_cid();
let Some(header) = event.first_decode.initial_header() else {
debug!(
"unable to extract initial header for connection {}",
dst_cid
);
return None;
};
let crypto = {
let Some(server_config) = &self.server_config else {
debug!("packet for unrecognized connection {}", dst_cid);
return self
.stateless_reset(event.now, datagram_len, addresses, *dst_cid, buf)
.map(DatagramEvent::Response);
};
if datagram_len < MIN_INITIAL_SIZE as usize {
debug!("ignoring short initial for connection {}", dst_cid);
return None;
}
match server_config.crypto.initial_keys(header.version, dst_cid) {
Ok(keys) => keys,
Err(UnsupportedVersion) => {
debug!(
"ignoring initial packet version {:#x} unsupported by cryptographic layer",
header.version
);
return None;
}
}
};
if let Err(reason) = self.early_validate_first_packet(header) {
return Some(DatagramEvent::Response(self.initial_close(
header.version,
addresses,
&crypto,
&header.src_cid,
reason,
buf,
)));
}
let packet = match event.first_decode.finish(Some(&*crypto.header.remote)) {
Ok(packet) => packet,
Err(e) => {
trace!("unable to decode initial packet: {}", e);
return None;
}
};
if !packet.reserved_bits_valid() {
debug!("dropping connection attempt with invalid reserved bits");
return None;
}
let Header::Initial(header) = packet.header else {
debug!("unexpected non-initial packet in handle_first_packet()");
return None;
};
let token = match self.server_config.as_ref() {
Some(sc) => match IncomingToken::from_header(&header, sc, addresses.remote) {
Ok(token) => token,
Err(InvalidRetryTokenError) => {
debug!("rejecting invalid retry token");
return Some(DatagramEvent::Response(self.initial_close(
header.version,
addresses,
&crypto,
&header.src_cid,
TransportError::INVALID_TOKEN(""),
buf,
)));
}
},
None => {
debug!("rejecting invalid retry token");
return Some(DatagramEvent::Response(self.initial_close(
header.version,
addresses,
&crypto,
&header.src_cid,
TransportError::INVALID_TOKEN(""),
buf,
)));
}
};
let incoming_idx = self.incoming_buffers.insert(IncomingBuffer::default());
self.index
.insert_initial_incoming(header.dst_cid, incoming_idx);
Some(DatagramEvent::NewConnection(Incoming {
received_at: event.now,
addresses,
ecn: event.ecn,
packet: InitialPacket {
header,
header_data: packet.header_data,
payload: packet.payload,
},
rest: event.remaining,
crypto,
token,
incoming_idx,
improper_drop_warner: IncomingImproperDropWarner,
}))
}
/// Attempt to accept this incoming connection (an error may still occur)
// AcceptError cannot be made smaller without semver breakage
#[allow(clippy::result_large_err)]
pub fn accept(
&mut self,
mut incoming: Incoming,
now: Instant,
buf: &mut Vec<u8>,
server_config: Option<Arc<ServerConfig>>,
) -> Result<(ConnectionHandle, Connection), AcceptError> {
let remote_address_validated = incoming.remote_address_validated();
incoming.improper_drop_warner.dismiss();
let incoming_buffer = self.incoming_buffers.remove(incoming.incoming_idx);
self.all_incoming_buffers_total_bytes -= incoming_buffer.total_bytes;
let packet_number = incoming.packet.header.number.expand(0);
let InitialHeader {
src_cid,
dst_cid,
version,
..
} = incoming.packet.header;
let server_config = match server_config.or_else(|| self.server_config.clone()) {
Some(sc) => sc,
None => {
return Err(AcceptError {
cause: ConnectionError::TransportError(
crate::transport_error::Error::INTERNAL_ERROR(""),
),
response: None,
});
}
};
if server_config
.transport
.max_idle_timeout
.is_some_and(|timeout| {
incoming.received_at + Duration::from_millis(timeout.into()) <= now
})
{
debug!("abandoning accept of stale initial");
self.index.remove_initial(dst_cid);
return Err(AcceptError {
cause: ConnectionError::TimedOut,
response: None,
});
}
if self.cids_exhausted() {
debug!("refusing connection");
self.index.remove_initial(dst_cid);
return Err(AcceptError {
cause: ConnectionError::CidsExhausted,
response: Some(self.initial_close(
version,
incoming.addresses,
&incoming.crypto,
&src_cid,
TransportError::CONNECTION_REFUSED(""),
buf,
)),
});
}
if incoming
.crypto
.packet
.remote
.decrypt(
packet_number,
&incoming.packet.header_data,
&mut incoming.packet.payload,
)
.is_err()
{
debug!(packet_number, "failed to authenticate initial packet");
self.index.remove_initial(dst_cid);
return Err(AcceptError {
cause: TransportError::PROTOCOL_VIOLATION("authentication failed").into(),
response: None,
});
};
let ch = ConnectionHandle(self.connections.vacant_key());
let loc_cid = self.new_cid(ch);
let mut params = TransportParameters::new(
&server_config.transport,
&self.config,
self.local_cid_generator.as_ref(),
loc_cid,
Some(&server_config),
&mut self.rng,
)?;
params.stateless_reset_token = Some(ResetToken::new(&*self.config.reset_key, loc_cid));
params.original_dst_cid = Some(incoming.token.orig_dst_cid);
params.retry_src_cid = incoming.token.retry_src_cid;
let mut pref_addr_cid = None;
if server_config.has_preferred_address() {
let cid = self.new_cid(ch);
pref_addr_cid = Some(cid);
params.preferred_address = Some(PreferredAddress {
address_v4: server_config.preferred_address_v4,
address_v6: server_config.preferred_address_v6,
connection_id: cid,
stateless_reset_token: ResetToken::new(&*self.config.reset_key, cid),
});
}
let tls = match server_config.crypto.clone().start_session(version, ¶ms) {
Ok(session) => session,
Err(e) => {
return Err(AcceptError {
cause: ConnectionError::TransportError(TransportError::INTERNAL_ERROR(
format!("server session start failed: {e}"),
)),
response: None,
});
}
};
let transport_config = server_config.transport.clone();
let mut conn = self.add_connection(
ch,
version,
dst_cid,
loc_cid,
src_cid,
incoming.addresses,
incoming.received_at,
tls,
transport_config,
SideArgs::Server {
server_config,
pref_addr_cid,
path_validated: remote_address_validated,
},
);
self.index.insert_initial(dst_cid, ch);
match conn.handle_first_packet(
incoming.received_at,
incoming.addresses.remote,
incoming.ecn,
packet_number,
incoming.packet,
incoming.rest,
) {
Ok(()) => {
trace!(id = ch.0, icid = %dst_cid, "new connection");
for event in incoming_buffer.datagrams {
conn.handle_event(ConnectionEvent(ConnectionEventInner::Datagram(event)))
}
Ok((ch, conn))
}
Err(e) => {
debug!("handshake failed: {}", e);
self.handle_event(ch, EndpointEvent(EndpointEventInner::Drained));
let response = match e {
ConnectionError::TransportError(ref e) => Some(self.initial_close(
version,
incoming.addresses,
&incoming.crypto,
&src_cid,
e.clone(),
buf,
)),
_ => None,
};
Err(AcceptError { cause: e, response })
}
}
}
/// Check if we should refuse a connection attempt regardless of the packet's contents
fn early_validate_first_packet(
&mut self,
header: &ProtectedInitialHeader,
) -> Result<(), TransportError> {
let Some(config) = &self.server_config else {
return Err(TransportError::INTERNAL_ERROR(""));
};
if self.cids_exhausted() || self.incoming_buffers.len() >= config.max_incoming {
return Err(TransportError::CONNECTION_REFUSED(""));
}
// RFC9000 §7.2 dictates that initial (client-chosen) destination CIDs must be at least 8
// bytes. If this is a Retry packet, then the length must instead match our usual CID
// length. If we ever issue non-Retry address validation tokens via `NEW_TOKEN`, then we'll
// also need to validate CID length for those after decoding the token.
if header.dst_cid.len() < 8
&& (header.token_pos.is_empty()
|| header.dst_cid.len() != self.local_cid_generator.cid_len())
{
debug!(
"rejecting connection due to invalid DCID length {}",
header.dst_cid.len()
);
return Err(TransportError::PROTOCOL_VIOLATION(
"invalid destination CID length",
));
}
Ok(())
}
/// Reject this incoming connection attempt
pub fn refuse(&mut self, incoming: Incoming, buf: &mut Vec<u8>) -> Transmit {
self.clean_up_incoming(&incoming);
incoming.improper_drop_warner.dismiss();
self.initial_close(
incoming.packet.header.version,
incoming.addresses,
&incoming.crypto,
&incoming.packet.header.src_cid,
TransportError::CONNECTION_REFUSED(""),
buf,
)
}
/// Respond with a retry packet, requiring the client to retry with address validation
///
/// Errors if `incoming.may_retry()` is false.
pub fn retry(&mut self, incoming: Incoming, buf: &mut Vec<u8>) -> Result<Transmit, RetryError> {
if !incoming.may_retry() {
return Err(RetryError::incoming(incoming));
}
let Some(server_config_arc) = self.server_config.clone() else {
return Err(RetryError::incoming(incoming));
};
// First Initial
// The peer will use this as the DCID of its following Initials. Initial DCIDs are
// looked up separately from Handshake/Data DCIDs, so there is no risk of collision
// with established connections. In the unlikely event that a collision occurs
// between two connections in the initial phase, both will fail fast and may be
// retried by the application layer.
let loc_cid = self.local_cid_generator.generate_cid();
let token = match crate::token_v2::encode_retry_token_with_rng(
&server_config_arc.token_key,
incoming.addresses.remote,
&incoming.packet.header.dst_cid,
server_config_arc.time_source.now(),
&mut self.rng,
) {
Ok(token) => token,
Err(err) => {
error!(?err, "failed to encode retry token");
return Err(RetryError::incoming(incoming));
}
};
let header = Header::Retry {
src_cid: loc_cid,
dst_cid: incoming.packet.header.src_cid,
version: incoming.packet.header.version,
};
let encode = match header.try_encode(buf) {
Ok(encode) => encode,
Err(_) => {
error!("failed to encode retry header due to varint overflow");
return Err(RetryError::incoming(incoming));
}
};
self.clean_up_incoming(&incoming);
incoming.improper_drop_warner.dismiss();
buf.put_slice(&token);
buf.extend_from_slice(&server_config_arc.crypto.retry_tag(
incoming.packet.header.version,
&incoming.packet.header.dst_cid,
buf,
));
encode.finish(buf, &*incoming.crypto.header.local, None);
Ok(Transmit {
destination: incoming.addresses.remote,
ecn: None,
size: buf.len(),
segment_size: None,
src_ip: incoming.addresses.local_ip,
})
}
/// Ignore this incoming connection attempt, not sending any packet in response
///
/// Doing this actively, rather than merely dropping the [`Incoming`], is necessary to prevent
/// memory leaks due to state within [`Endpoint`] tracking the incoming connection.
pub fn ignore(&mut self, incoming: Incoming) {
self.clean_up_incoming(&incoming);
incoming.improper_drop_warner.dismiss();
}
/// Clean up endpoint data structures associated with an `Incoming`.
fn clean_up_incoming(&mut self, incoming: &Incoming) {
self.index.remove_initial(incoming.packet.header.dst_cid);
let incoming_buffer = self.incoming_buffers.remove(incoming.incoming_idx);
self.all_incoming_buffers_total_bytes -= incoming_buffer.total_bytes;
}
fn add_connection(
&mut self,
ch: ConnectionHandle,
version: u32,
init_cid: ConnectionId,
loc_cid: ConnectionId,
rem_cid: ConnectionId,
addresses: FourTuple,
now: Instant,
tls: Box<dyn crypto::Session>,
transport_config: Arc<TransportConfig>,
side_args: SideArgs,
) -> Connection {
let mut rng_seed = [0; 32];
self.rng.fill_bytes(&mut rng_seed);
let side = side_args.side();
let pref_addr_cid = side_args.pref_addr_cid();
let conn = Connection::new(
self.config.clone(),
transport_config,
init_cid,
loc_cid,
rem_cid,
addresses.remote,
addresses.local_ip,
tls,
self.local_cid_generator.as_ref(),
now,
version,
self.allow_mtud,
rng_seed,
side_args,
);
let mut cids_issued = 0;
let mut loc_cids = FxHashMap::default();
loc_cids.insert(cids_issued, loc_cid);
cids_issued += 1;
if let Some(cid) = pref_addr_cid {
debug_assert_eq!(cids_issued, 1, "preferred address cid seq must be 1");
loc_cids.insert(cids_issued, cid);
cids_issued += 1;
}
let id = self.connections.insert(ConnectionMeta {
init_cid,
cids_issued,
loc_cids,
addresses,
side,
reset_token: None,
peer_id: None,
});
debug_assert_eq!(id, ch.0, "connection handle allocation out of sync");
self.index.insert_conn(addresses, loc_cid, ch, side);
conn
}
fn initial_close(
&mut self,
version: u32,
addresses: FourTuple,
crypto: &Keys,
remote_id: &ConnectionId,
reason: TransportError,
buf: &mut Vec<u8>,
) -> Transmit {
// We don't need to worry about CID collisions in initial closes because the peer
// shouldn't respond, and if it does, and the CID collides, we'll just drop the
// unexpected response.
let local_id = self.local_cid_generator.generate_cid();
let number = PacketNumber::U8(0);
let header = Header::Initial(InitialHeader {
dst_cid: *remote_id,
src_cid: local_id,
number,
token: Bytes::new(),
version,
});
let partial_encode = match header.try_encode(buf) {
Ok(encode) => encode,
Err(_) => {
error!("failed to encode initial close header due to varint overflow");
header.encode(buf)
}
};
let max_len =
INITIAL_MTU as usize - partial_encode.header_len - crypto.packet.local.tag_len();
let close = frame::Close::from(reason);
if close.try_encode(buf, max_len).is_err() {
error!("failed to encode initial close frame due to varint overflow");
close.encode(buf, max_len);
}
buf.resize(buf.len() + crypto.packet.local.tag_len(), 0);
partial_encode.finish(buf, &*crypto.header.local, Some((0, &*crypto.packet.local)));
Transmit {
destination: addresses.remote,
ecn: None,
size: buf.len(),
segment_size: None,
src_ip: addresses.local_ip,
}
}
/// Access the configuration used by this endpoint
pub fn config(&self) -> &EndpointConfig {
&self.config
}
/// Enable or disable address discovery for this endpoint
///
/// Address discovery is enabled by default. When enabled, the endpoint will:
/// - Send OBSERVED_ADDRESS frames to peers to inform them of their reflexive addresses
/// - Process received OBSERVED_ADDRESS frames to learn about its own reflexive addresses
/// - Integrate discovered addresses with NAT traversal for improved connectivity
pub fn enable_address_discovery(&mut self, enabled: bool) {
self.address_discovery_enabled = enabled;
// Note: Existing connections will continue with their current setting.
// New connections will use the updated setting.
}
/// Check if address discovery is enabled
pub fn address_discovery_enabled(&self) -> bool {
self.address_discovery_enabled
}
/// Get all discovered addresses across all connections
///
/// Returns a list of unique socket addresses that have been observed
/// by remote peers and reported via OBSERVED_ADDRESS frames.
///
/// Note: This returns an empty vector in the current implementation.
/// Applications should track discovered addresses at the connection level.
pub fn discovered_addresses(&self) -> Vec<SocketAddr> {
// TODO: Implement address tracking at the endpoint level
Vec::new()
}
/// Set a callback to be invoked when an address change is detected
///
/// The callback receives the old address (if any) and the new address.
/// Only one callback can be set at a time; setting a new callback replaces the previous one.
pub fn set_address_change_callback<F>(&mut self, callback: F)
where
F: Fn(Option<SocketAddr>, SocketAddr) + Send + Sync + 'static,
{
self.address_change_callback = Some(Box::new(callback));
}
/// Clear the address change callback
pub fn clear_address_change_callback(&mut self) {
self.address_change_callback = None;
}
/// Get address discovery statistics
///
/// Note: This returns default statistics in the current implementation.
/// Applications should track statistics at the connection level.
pub fn address_discovery_stats(&self) -> AddressDiscoveryStats {
// TODO: Implement statistics tracking at the endpoint level
AddressDiscoveryStats::default()
}
/// Number of connections that are currently open
pub fn open_connections(&self) -> usize {
self.connections.len()
}
/// Counter for the number of bytes currently used
/// in the buffers for Initial and 0-RTT messages for pending incoming connections
pub fn incoming_buffer_bytes(&self) -> u64 {
self.all_incoming_buffers_total_bytes
}
#[cfg(test)]
#[allow(dead_code)]
pub(crate) fn known_connections(&self) -> usize {
let x = self.connections.len();
debug_assert_eq!(x, self.index.connection_ids_initial.len());
// Not all connections have known reset tokens
debug_assert!(x >= self.index.connection_reset_tokens.0.len());
// Not all connections have unique remotes, and 0-length CIDs might not be in use.
debug_assert!(x >= self.index.incoming_connection_remotes.len());
debug_assert!(x >= self.index.outgoing_connection_remotes.len());
x
}
#[cfg(test)]
#[allow(dead_code)]
pub(crate) fn known_cids(&self) -> usize {
self.index.connection_ids.len()
}
/// Whether we've used up 3/4 of the available CID space
///
/// We leave some space unused so that `new_cid` can be relied upon to finish quickly. We don't
/// bother to check when CID longer than 4 bytes are used because 2^40 connections is a lot.
fn cids_exhausted(&self) -> bool {
self.local_cid_generator.cid_len() <= 4
&& self.local_cid_generator.cid_len() != 0
&& (2usize.pow(self.local_cid_generator.cid_len() as u32 * 8)
- self.index.connection_ids.len())
< 2usize.pow(self.local_cid_generator.cid_len() as u32 * 8 - 2)
}
}
impl fmt::Debug for Endpoint {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Endpoint")
.field("rng", &self.rng)
.field("index", &self.index)
.field("connections", &self.connections)
.field("config", &self.config)
.field("server_config", &self.server_config)
// incoming_buffers too large
.field("incoming_buffers.len", &self.incoming_buffers.len())
.field(
"all_incoming_buffers_total_bytes",
&self.all_incoming_buffers_total_bytes,
)
.finish()
}
}
/// Buffered Initial and 0-RTT messages for a pending incoming connection
#[derive(Default)]
struct IncomingBuffer {
datagrams: Vec<DatagramConnectionEvent>,
total_bytes: u64,
}
/// Part of protocol state incoming datagrams can be routed to
#[derive(Copy, Clone, Debug)]
enum RouteDatagramTo {
Incoming(usize),
Connection(ConnectionHandle),
}
/// Maps packets to existing connections
#[derive(Default, Debug)]
struct ConnectionIndex {
/// Identifies connections based on the initial DCID the peer utilized
///
/// Uses a standard `HashMap` to protect against hash collision attacks.
///
/// Used by the server, not the client.
connection_ids_initial: HashMap<ConnectionId, RouteDatagramTo>,
/// Identifies connections based on locally created CIDs
///
/// Uses a cheaper hash function since keys are locally created
connection_ids: FxHashMap<ConnectionId, ConnectionHandle>,
/// Identifies incoming connections with zero-length CIDs
///
/// Uses a standard `HashMap` to protect against hash collision attacks.
incoming_connection_remotes: HashMap<FourTuple, ConnectionHandle>,
/// Identifies outgoing connections with zero-length CIDs
///
/// We don't yet support explicit source addresses for client connections, and zero-length CIDs
/// require a unique four-tuple, so at most one client connection with zero-length local CIDs
/// may be established per remote. We must omit the local address from the key because we don't
/// necessarily know what address we're sending from, and hence receiving at.
///
/// Uses a standard `HashMap` to protect against hash collision attacks.
outgoing_connection_remotes: HashMap<SocketAddr, ConnectionHandle>,
/// Reset tokens provided by the peer for the CID each connection is currently sending to
///
/// Incoming stateless resets do not have correct CIDs, so we need this to identify the correct
/// recipient, if any.
connection_reset_tokens: ResetTokenTable,
}
impl ConnectionIndex {
/// Associate an incoming connection with its initial destination CID
fn insert_initial_incoming(&mut self, dst_cid: ConnectionId, incoming_key: usize) {
if dst_cid.is_empty() {
return;
}
self.connection_ids_initial
.insert(dst_cid, RouteDatagramTo::Incoming(incoming_key));
}
/// Remove an association with an initial destination CID
fn remove_initial(&mut self, dst_cid: ConnectionId) {
if dst_cid.is_empty() {
return;
}
let removed = self.connection_ids_initial.remove(&dst_cid);
debug_assert!(removed.is_some());
}
/// Associate a connection with its initial destination CID
fn insert_initial(&mut self, dst_cid: ConnectionId, connection: ConnectionHandle) {
if dst_cid.is_empty() {
return;
}
self.connection_ids_initial
.insert(dst_cid, RouteDatagramTo::Connection(connection));
}
/// Associate a connection with its first locally-chosen destination CID if used, or otherwise
/// its current 4-tuple
fn insert_conn(
&mut self,
addresses: FourTuple,
dst_cid: ConnectionId,
connection: ConnectionHandle,
side: Side,
) {
match dst_cid.len() {
0 => match side {
Side::Server => {
self.incoming_connection_remotes
.insert(addresses, connection);
}
Side::Client => {
self.outgoing_connection_remotes
.insert(addresses.remote, connection);
}
},
_ => {
self.connection_ids.insert(dst_cid, connection);
}
}
}
/// Discard a connection ID
fn retire(&mut self, dst_cid: ConnectionId) {
self.connection_ids.remove(&dst_cid);
}
/// Remove all references to a connection
fn remove(&mut self, conn: &ConnectionMeta) {
if conn.side.is_server() {
self.remove_initial(conn.init_cid);
}
for cid in conn.loc_cids.values() {
self.connection_ids.remove(cid);
}
self.incoming_connection_remotes.remove(&conn.addresses);
self.outgoing_connection_remotes
.remove(&conn.addresses.remote);
if let Some((remote, token)) = conn.reset_token {
self.connection_reset_tokens.remove(remote, token);
}
}
/// Find the existing connection that `datagram` should be routed to, if any
fn get(&self, addresses: &FourTuple, datagram: &PartialDecode) -> Option<RouteDatagramTo> {
let dst_cid = datagram.dst_cid();
let is_empty_cid = dst_cid.is_empty();
// Fast path: Try most common lookup first (non-empty CID)
if !is_empty_cid {
if let Some(&ch) = self.connection_ids.get(dst_cid) {
return Some(RouteDatagramTo::Connection(ch));
}
}
// Initial/0RTT packet lookup
if datagram.is_initial() || datagram.is_0rtt() {
if let Some(&ch) = self.connection_ids_initial.get(dst_cid) {
return Some(ch);
}
}
// Empty CID lookup (less common, do after fast path)
if is_empty_cid {
// Check incoming connections first (servers handle more incoming)
if let Some(&ch) = self.incoming_connection_remotes.get(addresses) {
return Some(RouteDatagramTo::Connection(ch));
}
if let Some(&ch) = self.outgoing_connection_remotes.get(&addresses.remote) {
return Some(RouteDatagramTo::Connection(ch));
}
}
// Stateless reset token lookup (least common, do last)
let data = datagram.data();
if data.len() < RESET_TOKEN_SIZE {
return None;
}
self.connection_reset_tokens
.get(addresses.remote, &data[data.len() - RESET_TOKEN_SIZE..])
.cloned()
.map(RouteDatagramTo::Connection)
}
}
#[derive(Debug)]
pub(crate) struct ConnectionMeta {
init_cid: ConnectionId,
/// Number of local connection IDs that have been issued in NEW_CONNECTION_ID frames.
cids_issued: u64,
loc_cids: FxHashMap<u64, ConnectionId>,
/// Remote/local addresses the connection began with
///
/// Only needed to support connections with zero-length CIDs, which cannot migrate, so we don't
/// bother keeping it up to date.
addresses: FourTuple,
side: Side,
/// Reset token provided by the peer for the CID we're currently sending to, and the address
/// being sent to
reset_token: Option<(SocketAddr, ResetToken)>,
/// Peer ID for this connection, used for relay functionality
peer_id: Option<PeerId>,
}
/// Internal identifier for a `Connection` currently associated with an endpoint
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct ConnectionHandle(pub usize);
impl From<ConnectionHandle> for usize {
fn from(x: ConnectionHandle) -> Self {
x.0
}
}
impl Index<ConnectionHandle> for Slab<ConnectionMeta> {
type Output = ConnectionMeta;
fn index(&self, ch: ConnectionHandle) -> &ConnectionMeta {
&self[ch.0]
}
}
impl IndexMut<ConnectionHandle> for Slab<ConnectionMeta> {
fn index_mut(&mut self, ch: ConnectionHandle) -> &mut ConnectionMeta {
&mut self[ch.0]
}
}
/// Event resulting from processing a single datagram
pub enum DatagramEvent {
/// The datagram is redirected to its `Connection`
ConnectionEvent(ConnectionHandle, ConnectionEvent),
/// The datagram may result in starting a new `Connection`
NewConnection(Incoming),
/// Response generated directly by the endpoint
Response(Transmit),
}
/// An incoming connection for which the server has not yet begun its part of the handshake.
pub struct Incoming {
received_at: Instant,
addresses: FourTuple,
ecn: Option<EcnCodepoint>,
packet: InitialPacket,
rest: Option<BytesMut>,
crypto: Keys,
token: IncomingToken,
incoming_idx: usize,
improper_drop_warner: IncomingImproperDropWarner,
}
impl Incoming {
/// The local IP address which was used when the peer established the connection
///
/// This has the same behavior as [`Connection::local_ip`].
pub fn local_ip(&self) -> Option<IpAddr> {
self.addresses.local_ip
}
/// The peer's UDP address
pub fn remote_address(&self) -> SocketAddr {
self.addresses.remote
}
/// Whether the socket address that is initiating this connection has been validated
///
/// This means that the sender of the initial packet has proved that they can receive traffic
/// sent to `self.remote_address()`.
///
/// If `self.remote_address_validated()` is false, `self.may_retry()` is guaranteed to be true.
/// The inverse is not guaranteed.
pub fn remote_address_validated(&self) -> bool {
self.token.validated
}
/// Whether it is legal to respond with a retry packet
///
/// If `self.remote_address_validated()` is false, `self.may_retry()` is guaranteed to be true.
/// The inverse is not guaranteed.
pub fn may_retry(&self) -> bool {
self.token.retry_src_cid.is_none()
}
/// The original destination connection ID sent by the client
pub fn orig_dst_cid(&self) -> &ConnectionId {
&self.token.orig_dst_cid
}
}
impl fmt::Debug for Incoming {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Incoming")
.field("addresses", &self.addresses)
.field("ecn", &self.ecn)
// packet doesn't implement debug
// rest is too big and not meaningful enough
.field("token", &self.token)
.field("incoming_idx", &self.incoming_idx)
// improper drop warner contains no information
.finish_non_exhaustive()
}
}
struct IncomingImproperDropWarner;
impl IncomingImproperDropWarner {
fn dismiss(self) {
mem::forget(self);
}
}
impl Drop for IncomingImproperDropWarner {
fn drop(&mut self) {
warn!(
"quinn_proto::Incoming dropped without passing to Endpoint::accept/refuse/retry/ignore \
(may cause memory leak and eventual inability to accept new connections)"
);
}
}
/// Errors in the parameters being used to create a new connection
///
/// These arise before any I/O has been performed.
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum ConnectError {
/// The endpoint can no longer create new connections
///
/// Indicates that a necessary component of the endpoint has been dropped or otherwise disabled.
#[error("endpoint stopping")]
EndpointStopping,
/// The connection could not be created because not enough of the CID space is available
///
/// Try using longer connection IDs
#[error("CIDs exhausted")]
CidsExhausted,
/// The given server name was malformed
#[error("invalid server name: {0}")]
InvalidServerName(String),
/// The remote [`SocketAddr`] supplied was malformed
///
/// Examples include attempting to connect to port 0, or using an inappropriate address family.
#[error("invalid remote address: {0}")]
InvalidRemoteAddress(SocketAddr),
/// No default client configuration was set up
///
/// Use `Endpoint::connect_with` to specify a client configuration.
#[error("no default client config")]
NoDefaultClientConfig,
/// The local endpoint does not support the QUIC version specified in the client configuration
#[error("unsupported QUIC version")]
UnsupportedVersion,
/// A TLS-related error occurred during connection establishment
#[error("TLS error: {0}")]
TlsError(String),
/// Failed to encode transport parameters for the handshake
#[error("transport parameters encoding failed: {0}")]
TransportParameters(crate::transport_parameters::Error),
}
/// Error type for attempting to accept an [`Incoming`]
#[derive(Debug)]
pub struct AcceptError {
/// Underlying error describing reason for failure
pub cause: ConnectionError,
/// Optional response to transmit back
pub response: Option<Transmit>,
}
impl From<rustls::Error> for ConnectError {
fn from(error: rustls::Error) -> Self {
ConnectError::TlsError(error.to_string())
}
}
impl From<crate::transport_error::Error> for AcceptError {
fn from(error: crate::transport_error::Error) -> Self {
Self {
cause: ConnectionError::TransportError(error),
response: None,
}
}
}
/// Error for attempting to retry an [`Incoming`] which already bears a token from a previous retry
#[derive(Debug, Error)]
pub enum RetryError {
/// Retry was attempted with an invalid or already-consumed Incoming.
#[error("retry() with invalid Incoming")]
Incoming(Box<Incoming>),
}
impl RetryError {
/// Create a retry error carrying the original Incoming.
pub fn incoming(incoming: Incoming) -> Self {
Self::Incoming(Box::new(incoming))
}
/// Get the [`Incoming`]
pub fn into_incoming(self) -> Incoming {
match self {
Self::Incoming(incoming) => *incoming,
}
}
}
/// Reset Tokens which are associated with peer socket addresses
///
/// The standard `HashMap` is used since both `SocketAddr` and `ResetToken` are
/// peer generated and might be usable for hash collision attacks.
#[derive(Default, Debug)]
struct ResetTokenTable(HashMap<SocketAddr, HashMap<ResetToken, ConnectionHandle>>);
impl ResetTokenTable {
fn insert(&mut self, remote: SocketAddr, token: ResetToken, ch: ConnectionHandle) -> bool {
self.0
.entry(remote)
.or_default()
.insert(token, ch)
.is_some()
}
fn remove(&mut self, remote: SocketAddr, token: ResetToken) {
use std::collections::hash_map::Entry;
match self.0.entry(remote) {
Entry::Vacant(_) => {}
Entry::Occupied(mut e) => {
e.get_mut().remove(&token);
if e.get().is_empty() {
e.remove_entry();
}
}
}
}
fn get(&self, remote: SocketAddr, token: &[u8]) -> Option<&ConnectionHandle> {
let token = ResetToken::from(<[u8; RESET_TOKEN_SIZE]>::try_from(token).ok()?);
self.0.get(&remote)?.get(&token)
}
}
/// Identifies a connection by the combination of remote and local addresses
///
/// Including the local ensures good behavior when the host has multiple IP addresses on the same
/// subnet and zero-length connection IDs are in use.
#[derive(Hash, Eq, PartialEq, Debug, Copy, Clone)]
struct FourTuple {
remote: SocketAddr,
// A single socket can only listen on a single port, so no need to store it explicitly
local_ip: Option<IpAddr>,
}