Repository navigation
Expand file tree
/
Copy pathsession.cpp
More file actions
3575 lines (3347 loc) · 156 KB
/
Copy pathsession.cpp
File metadata and controls
3575 lines (3347 loc) · 156 KB
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
// SPDX-License-Identifier: GPL-2.0-only WITH GoodNet-linking-exception
/// @file plugins/links/ice/session.cpp
/// @brief ICE session FSM implementation per RFC 8445.
///
/// Threading model:
/// - Every state mutation runs on `strand_` (or during the synchronous
/// prefix of `gather()` before any strand work has been posted).
/// - `nominated_mu_` guards the nominated-pair tuple so the hot send
/// path can sample without bouncing off the strand.
/// - `close()` is safe from any thread; it drops carrier subscriptions
/// and cancels timers without touching FSM state on the strand.
///
/// I/O routes through `carrier_` (a borrowed `gn::sdk::LinkCarrier*`
/// resolved by the parent `IceLink`). Each remote endpoint we talk to
/// gets its own carrier conn id via `ensure_remote_cid`; the per-cid
/// `on_data` callback drives `on_carrier_data` which feeds the STUN
/// parsing path.
#include "session.hpp"
#include "dbg.hpp"
#include <stdexec/execution.hpp>
#include <exec/timed_thread_scheduler.hpp>
#include <exec/start_detached.hpp>
namespace exec = experimental::execution;
#include <asio/bind_executor.hpp>
#include <asio/dispatch.hpp>
#include <asio/post.hpp>
#include <sdk/cpp/uri.hpp>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <future>
#include <memory>
#include <mutex>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <system_error>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sodium/randombytes.h>
namespace gn::link::ice {
namespace {
std::string endpoint_key(const std::string& ip, uint16_t port) {
return ip + ":" + std::to_string(port);
}
std::string endpoint_uri(const std::string& ip, uint16_t port) {
return "udp://" + ip + ":" + std::to_string(port);
}
std::string endpoint_uri_tcp(const std::string& ip, uint16_t port) {
return "tcp://" + ip + ":" + std::to_string(port);
}
/// Split a `cfg_.stun_servers` entry (always bare `host:port` per the
/// `link_ice.cpp` normalisation pipeline; see `push_stun`) into its
/// host + port halves through the SDK's canonical URI parser. Prepends
/// `stun://` so `gn::parse_uri` (sdk/cpp/uri.hpp) handles the
/// `host:port` split, port-range, and trailing-garbage rejection.
/// Symmetric with `parse_service_uri` (dns_ext_client.cpp) — same
/// parser for every form of STUN-server input.
///
/// When the entry carries no `:` at all, fall back to the historic
/// default-port behaviour (`3478`, RFC 5389 §9) so an operator that
/// configured only a hostname still gets a working probe.
///
/// Returns nullopt on every failure the SDK parser rejects: empty
/// host, malformed port, port > 65535, trailing garbage, control
/// bytes. Callers `continue` past such entries.
std::optional<std::pair<std::string, std::uint16_t>>
split_stun_host_port(std::string_view server) {
if (server.empty()) return std::nullopt;
if (server.find(':') == std::string_view::npos) {
return std::make_pair(std::string(server),
static_cast<std::uint16_t>(3478));
}
std::string canonical;
canonical.reserve(7 + server.size());
canonical.append("stun://").append(server);
const auto parts = gn::parse_uri(canonical);
if (!parts || parts->host.empty() || parts->port == 0) {
return std::nullopt;
}
return std::make_pair(parts->host, parts->port);
}
} // namespace
static std::string random_string(size_t len) {
static constexpr char chars[] = "abcdefghijklmnopqrstuvwxyz0123456789";
static constexpr uint32_t nchars = sizeof(chars) - 1;
std::string out(len, '\0');
for (size_t i = 0; i < len; ++i)
out[i] = chars[randombytes_uniform(nchars)];
return out;
}
std::string IceSession::generate_ufrag() { return random_string(8); }
std::string IceSession::generate_pwd() { return random_string(24); }
IceSession::IceSession(asio::io_context& io,
gn::sdk::LinkCarrier* carrier,
gn::sdk::LinkCarrier* carrier_tcp,
gn::sdk::LinkCarrier* carrier_tls,
const IceConfig& cfg,
const std::string& peer_id, bool controlling,
IceSessionCallbacks callbacks,
std::shared_ptr<MdnsManager> mdns,
IcePortmapClient* portmap_ext,
exec::timed_thread_context* timer_ctx
)
: io_(io), strand_(asio::make_strand(io.get_executor())),
timer_ctx_(timer_ctx),
carrier_(carrier), carrier_tcp_(carrier_tcp),
carrier_tls_(carrier_tls),
portmap_ext_(portmap_ext), cfg_(cfg),
peer_id_(peer_id),
controlling_(cfg.lite_mode ? false : controlling),
callbacks_(std::move(callbacks)),
mdns_(std::move(mdns)),
last_activity_(std::chrono::steady_clock::now())
{
/// Constructor: no observers exist yet, so the mutex isn't
/// strictly required, but locking keeps the publication pattern
/// uniform and lets TSan reason about all writes through the
/// same edge as the public accessors.
std::lock_guard lk(local_state_mu_);
local_ufrag_ = generate_ufrag();
local_pwd_ = generate_pwd();
randombytes_buf(&tiebreaker_, sizeof(tiebreaker_));
/// Seed the effective MTU with the conservative configured floor.
/// Active probing replaces this on the first successful ACK once
/// the FSM reaches `Connected`.
effective_mtu_.store(static_cast<std::uint32_t>(cfg_.path_mtu),
std::memory_order_release);
}
IceSession::~IceSession() {
/// Strand-bound work has already been cancelled by `close()` in
/// the typical teardown path. The dtor's only remaining duty is
/// to release per-cid carrier subscriptions; the carrier itself
/// outlives the session (owned by IceLink) so the unsubscribe
/// calls are safe even if the session is being unwound after the
/// io_context has stopped.
state_.store(SessionState::Failed, std::memory_order_release);
++check_gen_;
++keepalive_gen_;
++gather_gen_;
++turn_allocate_gen_;
++turn_backup_gen_;
++nomination_wait_gen_;
++pmtu_probe_gen_;
if (turn_) turn_->close();
if (turn_backup_probe_) turn_backup_probe_->close();
if (mdns_ && !mdns_local_hostname_.empty()) {
mdns_->unregister_name(mdns_local_hostname_);
mdns_local_hostname_.clear();
}
/// Best-effort portmap release. Matches the TURN-deallocation
/// pattern above: ignore failure since the router will time the
/// mapping out on its own once `lifetime_s` elapses without a
/// renewal.
release_portmap_mapping();
for (auto& [cid, ep] : cid_to_endpoint_udp_) {
if (!carrier_) continue;
(void)carrier_->unsubscribe_data(cid);
(void)carrier_->disconnect(cid);
}
for (auto& [cid, ep] : cid_to_endpoint_tcp_) {
if (!carrier_tcp_) continue;
(void)carrier_tcp_->unsubscribe_data(cid);
(void)carrier_tcp_->disconnect(cid);
}
}
// ── Public API ──────────────────────────────────────────────────────────────
void IceSession::gather() {
ICE_DBG("gather", "peer=%s controlling=%d stun_srvs=%zu",
peer_id_.c_str(), controlling_ ? 1 : 0,
cfg_.stun_servers.size());
/// Synchronous prefix runs on the caller before any strand work is
/// posted, so direct `local_candidates_` mutation is race-free.
state_.store(SessionState::Gathering, std::memory_order_release);
gather_host_candidates();
ICE_DBG("gather", "peer=%s after host_gather local_cands=%zu",
peer_id_.c_str(), local_candidates_.size());
/// Optional NAT mapping via the `gn.link.portmap` extension. Runs
/// after host gather so `local_port_` is known. The portmap
/// extension is synchronous (request returns the mapping or
/// fails); the srflx candidate is pushed inline and ready before
/// the FSM moves on to TURN / STUN. Missing extension or
/// zero-protocol mask is a transparent no-op.
gather_portmap();
/// TURN allocation runs in parallel with STUN; the relay candidate
/// arrives via callback once `gn.ice.turn` answers.
gather_relay();
if (cfg_.stun_servers.empty()) {
if (!turn_gathering_) {
on_gathering_complete();
}
/// else: TURN allocation is in flight; it will call
/// on_gathering_complete() when done (or on its timeout).
} else {
asio::post(strand_, [this, self = shared_from_this()] {
start_multi_stun_probes();
/// RFC 6544 §4.4 TCP srflx probes ride the same pending
/// transaction set as UDP probes so a single gather
/// timeout covers both. No-op when no TCP carrier is
/// wired or `tcp_candidates_enabled` is false.
gather_tcp_server_reflexive();
});
}
}
void IceSession::set_remote(const std::string& ufrag, const std::string& pwd,
std::vector<Candidate> candidates) {
/// Caller dispatches to the strand; this body assumes single-writer.
remote_ufrag_ = ufrag;
remote_pwd_ = pwd;
remote_candidates_ = std::move(candidates);
last_activity_.store(std::chrono::steady_clock::now(), std::memory_order_release);
auto cur = state_.load(std::memory_order_acquire);
if (cur == SessionState::WaitingRemote || cur == SessionState::Gathering) {
if (!local_candidates_.empty()) {
state_.store(SessionState::Checking, std::memory_order_release);
build_check_list();
begin_checks();
}
}
/// Kick off mDNS resolution for any HostMdns entries the peer
/// advertised. Resolutions complete asynchronously and fold
/// resolved IPs back into the check list via the callback path
/// in `resolve_remote_mdns_candidates`. The pairs built above
/// against the non-HostMdns subset start checking immediately.
resolve_remote_mdns_candidates();
}
void IceSession::send(std::span<const uint8_t> data) {
last_activity_.store(std::chrono::steady_clock::now(), std::memory_order_release);
auto buf = std::make_shared<std::vector<uint8_t>>(data.begin(), data.end());
ICE_DBG("send-app-enq", "peer=%s len=%zu state=%d",
peer_id_.c_str(), buf->size(),
(int)state_.load(std::memory_order_acquire));
asio::post(strand_, [this, self = shared_from_this(), buf] {
const auto st = state_.load(std::memory_order_acquire);
if (st != SessionState::Connected) {
if (pending_app_data_.size() >= kPendingAppDataMaxFrames) {
pending_app_data_.pop_front();
}
pending_app_data_.push_back(std::move(*buf));
ICE_DBG("send-app-park", "peer=%s state=%d pending=%zu",
peer_id_.c_str(), (int)st, pending_app_data_.size());
return;
}
send_on_strand(*buf);
});
}
void IceSession::deliver_in_order(std::span<const uint8_t> raw) {
if (raw.size() < 4) return;
const std::uint32_t seq = (std::uint32_t(raw[0]) << 24)
| (std::uint32_t(raw[1]) << 16)
| (std::uint32_t(raw[2]) << 8)
| std::uint32_t(raw[3]);
const auto data = raw.subspan(4);
if (seq < recv_data_seq_) return; // duplicate / replay
if (seq == recv_data_seq_) {
if (callbacks_.on_data)
callbacks_.on_data(peer_id_, data);
++recv_data_seq_;
while (true) {
auto it = recv_reorder_buf_.find(recv_data_seq_);
if (it == recv_reorder_buf_.end()) break;
if (callbacks_.on_data)
callbacks_.on_data(peer_id_, std::span{it->second});
recv_reorder_buf_.erase(it);
++recv_data_seq_;
}
return;
}
// Out-of-order: buffer up to kReorderWindow entries
static constexpr std::size_t kReorderWindow = 64;
if (recv_reorder_buf_.size() >= kReorderWindow) {
// Advance past the gap to avoid deadlock on window overflow
recv_data_seq_ = recv_reorder_buf_.begin()->first;
ICE_DBG("ordered-rx-flush",
"peer=%s window overflow at seq=%u",
peer_id_.c_str(), recv_data_seq_);
}
recv_reorder_buf_[seq] =
std::vector<uint8_t>(data.begin(), data.end());
}
void IceSession::send_on_strand(const std::vector<uint8_t>& buf) {
std::string ip;
uint16_t port;
bool relay;
{
std::lock_guard lk(nominated_mu_);
ip = nominated_ip_;
port = nominated_port_;
relay = uses_relay_;
}
const auto tx = nominated_transport_.load(std::memory_order_acquire);
const bool use_tcp = transport_is_tcp(tx);
// When ordered delivery is active, prepend a 4-byte big-endian sequence
// number to every outbound app-data frame on the UDP path (TCP is already
// ordered by the transport; adding a seqno there wastes 4 bytes/frame).
const bool add_seqno = cfg_.ordered_delivery && !use_tcp;
std::vector<uint8_t> framed_buf;
const std::vector<uint8_t>* send_ptr = &buf;
if (add_seqno) {
const auto seq = send_data_seq_.fetch_add(1, std::memory_order_relaxed);
framed_buf.reserve(4 + buf.size());
framed_buf.push_back(static_cast<uint8_t>((seq >> 24) & 0xFF));
framed_buf.push_back(static_cast<uint8_t>((seq >> 16) & 0xFF));
framed_buf.push_back(static_cast<uint8_t>((seq >> 8) & 0xFF));
framed_buf.push_back(static_cast<uint8_t>( seq & 0xFF));
framed_buf.insert(framed_buf.end(), buf.begin(), buf.end());
send_ptr = &framed_buf;
}
if (relay && turn_) {
ICE_DBG("send-app-out", "peer=%s len=%zu dst=%s:%u relay=1 seqno=%d",
peer_id_.c_str(), send_ptr->size(), ip.c_str(), port,
add_seqno ? 1 : 0);
turn_->send_indication(ip, port, *send_ptr);
return;
}
auto cid = nominated_cid_.load(std::memory_order_acquire);
if (cid == 0) {
cid = ensure_remote_cid(ip, port);
if (cid == 0) {
ICE_DBG("send-app-drop", "peer=%s reason=no-cid dst=%s:%u",
peer_id_.c_str(), ip.c_str(), port);
return;
}
nominated_cid_.store(cid, std::memory_order_release);
}
ICE_DBG("send-app-out", "peer=%s len=%zu dst=%s:%u cid=%llu tcp=%d seqno=%d",
peer_id_.c_str(), send_ptr->size(), ip.c_str(), port,
static_cast<unsigned long long>(cid),
use_tcp ? 1 : 0, add_seqno ? 1 : 0);
if (use_tcp) {
if (carrier_tcp_) {
const auto tcp_framed = frame_tcp_stun(*send_ptr);
(void)carrier_tcp_->send(cid, tcp_framed);
}
} else {
if (carrier_) (void)carrier_->send(cid, *send_ptr);
}
}
void IceSession::flush_pending_app_data() {
if (!pending_app_data_.empty()) {
ICE_DBG("flush-app", "peer=%s count=%zu",
peer_id_.c_str(), pending_app_data_.size());
}
while (!pending_app_data_.empty()) {
auto buf = std::move(pending_app_data_.front());
pending_app_data_.pop_front();
send_on_strand(buf);
}
}
void IceSession::add_remote_candidates(const std::string& ufrag,
const std::string& pwd,
std::vector<Candidate> cands,
bool end_of_candidates) {
asio::dispatch(strand_, [self = shared_from_this(),
ufrag, pwd,
cands = std::move(cands),
end_of_candidates]() mutable {
ICE_DBG("add-remote",
"peer=%s ufrag=%s incoming=%zu eoc=%d existing_remote=%zu",
self->peer_id_.c_str(), ufrag.c_str(), cands.size(),
end_of_candidates ? 1 : 0,
self->remote_candidates_.size());
for (const auto& c : cands) {
ICE_DBG("add-remote.cand",
"peer=%s type=%d tx=%d %s:%u prio=%u",
self->peer_id_.c_str(), static_cast<int>(c.type),
static_cast<int>(c.transport),
c.address_str().c_str(), c.port, c.priority);
}
const bool creds_changed =
!self->remote_ufrag_.empty() && self->remote_ufrag_ != ufrag;
if (creds_changed) {
/// Peer signalled new credentials — RFC 8445 §9 restart on
/// their side. Drop our cached remote state so the next
/// build_check_list operates on the fresh set only.
self->remote_candidates_.clear();
self->check_list_.clear();
self->current_check_ = 0;
}
self->remote_ufrag_ = ufrag;
self->remote_pwd_ = pwd;
/// EOC is sticky once set — even if a later batch arrives
/// (operator bug, retransmission of trailing trickle frame)
/// we keep treating the candidate set as complete. Under
/// regular nomination an EOC arrival speeds up the commit:
/// the controller no longer waits for a possibly-higher
/// priority pair to appear later via trickle, and picks the
/// best valid pair right now.
if (end_of_candidates) {
self->remote_end_of_candidates_ = true;
if (self->controlling_
&& !self->cfg_.aggressive_nomination
&& !self->nominee_selected_) {
CheckPair* best = nullptr;
for (auto& p : self->check_list_) {
if (p.valid && (!best || p.priority > best->priority)) {
best = &p;
}
}
if (best) {
self->nominee_selected_ = true;
best->nominee_check_sent = true;
self->send_check(*best);
}
}
}
/// Merge each new candidate, deduping by (ip, port, type,
/// transport). The transport key matters: a UDP host
/// candidate at 10.0.0.1:40000 is a different reachable
/// endpoint from a TCP-active host candidate at the same
/// (ip, port) — they ride distinct sockets on both ends,
/// produce distinct check pairs, and route via
/// `carrier_udp_` vs `carrier_tcp_`. Without the transport
/// component a TCP fallback set (RFC 6544) collapses into
/// a single UDP entry and the FSM never tries the TCP
/// pairs. Address comparison goes through `address_str()`
/// to keep IPv4 / IPv6 logic in one place.
for (auto& c : cands) {
const auto ip = c.address_str();
const bool dup = std::ranges::any_of(
self->remote_candidates_,
[&](const Candidate& e) {
return e.address_str() == ip
&& e.port == c.port
&& e.type == c.type
&& e.transport == c.transport;
});
if (!dup) {
/// Install a TURN CreatePermission entry for the peer
/// IP before the merge so the relay-local check pair
/// produced by the next `build_check_list` can fire
/// SendIndication packets at this peer without the
/// server dropping them as "no permission". The
/// TurnClient dedupes via `permissions_`, so duplicate
/// calls (same IP across multiple candidates) are
/// harmless. When the allocation isn't up yet the
/// `on_alloc` callback in `gather_relay` does the
/// same sweep across the current remote set.
if (self->turn_ && self->turn_->is_allocated()) {
self->turn_->create_permission(ip);
}
self->remote_candidates_.push_back(std::move(c));
}
}
const auto cur = self->state_.load(std::memory_order_acquire);
if (self->remote_candidates_.empty()) return;
if (cur == SessionState::WaitingRemote ||
cur == SessionState::Gathering) {
if (!self->local_candidates_.empty()) {
/// Trickle ICE: publish the local candidate set the
/// moment we have one alongside the remote OFFER —
/// BEFORE state transition + checks. This way the
/// ANSWER goes out even if begin_checks blocks or
/// the strand gets tied up with the connectivity
/// check loop afterwards. srflx candidates trickle
/// out as later deltas under the same outbound
/// queue.
/// Only the responder (controlled agent) emits a signal
/// here — this produces the ANSWER. The initiator
/// (controlling) must NOT re-emit; doing so would send
/// a second OFFER to the peer and restart their session.
///
/// Send the answer immediately with whatever candidates
/// are available (host + srflx at minimum). If a TURN
/// allocation is still in flight, the relay candidate
/// will be trickled once on_alloc fires (trickle ICE,
/// RFC 8838), so the peer's check list gets updated
/// without blocking the initial answer.
if (self->callbacks_.on_gathered && !self->controlling_) {
self->gathered_signal_fired_ = true;
self->callbacks_.on_gathered(self->peer_id_);
}
self->state_.store(SessionState::Checking,
std::memory_order_release);
self->build_check_list();
self->begin_checks();
}
return;
}
if (cur == SessionState::Checking) {
/// Rebuild includes both old pairs (will resend checks —
/// harmless because txn ids match the freshly generated
/// ones) and new pairs from the trickle batch. Sorting by
/// pair priority keeps the highest-quality fresh candidate
/// at the head of the queue.
self->build_check_list();
self->current_check_ = 0;
self->run_next_check();
}
/// Trickle batches may carry fresh HostMdns entries — resolve
/// them after the immediate build_check_list so non-mDNS
/// pairs start probing without waiting on resolver latency.
self->resolve_remote_mdns_candidates();
});
}
void IceSession::set_peer_signal_flags(std::uint32_t flags) {
asio::dispatch(strand_, [self = shared_from_this(), flags] {
const auto prev = self->peer_signal_flags_.exchange(
flags, std::memory_order_acq_rel);
const bool symmetric_changed =
((prev ^ flags) & ICE_SIGNAL_FLAG_SYMMETRIC) != 0;
if (symmetric_changed
&& self->state_.load(std::memory_order_acquire)
== SessionState::Checking
&& !self->remote_candidates_.empty()
&& !self->local_candidates_.empty()) {
/// Predicted-port pairs depend on the peer flag; rebuild
/// the list so the freshly-known stride lands in the
/// queue.
self->build_check_list();
self->current_check_ = 0;
self->run_next_check();
}
});
}
std::uint32_t IceSession::local_signal_flags() const noexcept {
std::uint32_t f = 0;
if (cfg_.lite_mode) f |= ICE_SIGNAL_FLAG_LITE;
if (symmetric_stride_.load(std::memory_order_acquire) != 0) {
f |= ICE_SIGNAL_FLAG_SYMMETRIC;
}
return f;
}
void IceSession::restart() {
/// All state mutations are serialised on the strand; the call site
/// usually arrives off-strand from the operator or kernel thread.
asio::dispatch(strand_, [self = shared_from_this()] {
auto cur = self->state_.load(std::memory_order_acquire);
if (cur == SessionState::Failed) return;
/// Cancel all in-flight timers + drop pending state. The
/// keepalive on a Connected session also stops because it
/// short-circuits on state != Connected.
++self->check_gen_;
++self->keepalive_gen_;
++self->gather_gen_;
++self->turn_allocate_gen_;
++self->turn_backup_gen_;
++self->nomination_wait_gen_;
self->nomination_wait_armed_ = false;
if (self->turn_) self->turn_->close();
if (self->turn_backup_probe_) self->turn_backup_probe_->close();
self->turn_.reset();
self->turn_backup_probe_.reset();
self->turn_backups_.clear();
self->turn_attempt_idx_ = 0;
self->turn_backup_probe_idx_ = 0;
self->turn_last_failover_ = std::chrono::steady_clock::time_point{};
{
/// Publish credential + candidate-set wipe to off-strand
/// readers (`local_ufrag()`, `local_pwd()`,
/// `local_candidates()`).
std::lock_guard lk(self->local_state_mu_);
self->local_ufrag_ = IceSession::generate_ufrag();
self->local_pwd_ = IceSession::generate_pwd();
self->local_candidates_.clear();
}
/// Drop the previous mDNS registration so the post-restart
/// gather can mint a fresh `<uuid>.local` per the draft.
/// The new gather phase calls `register_name` again under
/// `gather_host_candidates`.
if (self->mdns_ && !self->mdns_local_hostname_.empty()) {
self->mdns_->unregister_name(self->mdns_local_hostname_);
self->mdns_local_hostname_.clear();
}
self->remote_candidates_.clear();
self->remote_ufrag_.clear();
self->remote_pwd_.clear();
self->check_list_.clear();
self->pending_stun_probes_.clear();
self->current_check_ = 0;
self->consent_missed_.store(0, std::memory_order_release);
self->consent_recovery_attempts_ = 0;
self->nominee_selected_ = false;
self->remote_end_of_candidates_ = false;
self->gathered_signal_fired_ = false;
self->stun_gathered_ = false;
self->turn_gathering_ = false;
self->nominated_cid_.store(0, std::memory_order_release);
self->peer_signal_flags_.store(0, std::memory_order_release);
self->symmetric_stride_.store(0, std::memory_order_release);
self->first_observed_srflx_port_ = 0;
self->have_first_srflx_port_ = false;
{
std::lock_guard lk(self->nominated_mu_);
self->nominated_ip_.clear();
self->nominated_port_ = 0;
self->uses_relay_ = false;
}
/// Drop carrier conn ids accumulated during the previous
/// gather/check phases. The local listen port (shared with
/// IceLink's other sessions) stays bound — only the per-peer
/// mappings are torn down so the next gather negotiates fresh
/// composer_connect's for the new candidate set.
self->tear_down_cids();
self->carrier_initialized_ = false;
self->local_port_ = 0;
self->state_.store(SessionState::New, std::memory_order_release);
self->gather();
});
}
void IceSession::close() {
/// Mark the FSM dead synchronously so concurrent `send` calls
/// stop touching the carrier immediately. The actual subscription
/// teardown runs on the strand to avoid racing the carrier
/// dispatcher with our own unsubscribe call.
state_.store(SessionState::Failed, std::memory_order_release);
asio::dispatch(strand_, [self = shared_from_this()] {
++self->check_gen_;
++self->keepalive_gen_;
++self->gather_gen_;
++self->turn_allocate_gen_;
++self->turn_backup_gen_;
++self->nomination_wait_gen_;
self->nomination_wait_armed_ = false;
if (self->turn_) self->turn_->close();
if (self->turn_backup_probe_) self->turn_backup_probe_->close();
if (self->mdns_ && !self->mdns_local_hostname_.empty()) {
self->mdns_->unregister_name(self->mdns_local_hostname_);
self->mdns_local_hostname_.clear();
}
/// Tear down any portmap mapping we punched during gather so
/// the router reclaims the external port the moment we go
/// away. Mirrors the TURN deallocation above — best-effort,
/// the renewal table inside the portmap plugin clears even
/// when the router's reply never arrives.
self->release_portmap_mapping();
self->tear_down_cids();
self->nominated_cid_.store(0, std::memory_order_release);
});
}
void IceSession::tear_down_cids() {
for (auto& [cid, t] : tcp_connect_timers_) {
if (t) t->cancel();
}
tcp_connect_timers_.clear();
for (auto& [cid, ep] : cid_to_endpoint_udp_) {
if (!carrier_) continue;
(void)carrier_->unsubscribe_data(cid);
(void)carrier_->disconnect(cid);
}
for (auto& [cid, ep] : cid_to_endpoint_tcp_) {
if (!carrier_tcp_) continue;
(void)carrier_tcp_->unsubscribe_data(cid);
(void)carrier_tcp_->disconnect(cid);
}
endpoint_to_cid_.clear();
cid_to_endpoint_udp_.clear();
cid_to_endpoint_tcp_.clear();
tcp_rx_buffers_.clear();
}
// ── Gathering (strand or synchronous) ───────────────────────────────────────
void IceSession::gather_host_candidates() {
/// The shared UDP carrier is bound lazily on the first session
/// that needs it; subsequent sessions on the same `IceLink` just
/// learn the existing local port. A missing carrier (test stub
/// without `gn.link.udp`) leaves us with port 0 and zero host
/// candidates — gather still completes, the FSM just transitions
/// straight to WaitingRemote with nothing to advertise.
if (carrier_ && !carrier_initialized_) {
(void)carrier_->listen("udp://0.0.0.0:0");
local_port_ = carrier_->listen_port();
carrier_initialized_ = true;
}
if (local_port_ == 0) return;
/// draft-ietf-mmusic-mdns-ice-candidates §3.1: when host
/// obfuscation is enabled, the link advertises a SINGLE
/// `<uuid>.local` per session (the IP family on the wire is
/// IPv4 by convention — peers don't try to match on family for
/// HostMdns candidates, the resolver fans out to whatever A /
/// AAAA records the responder returns). The mDNS manager
/// records the name → local-IP mapping; remote peers query for
/// the name and discover the IP only if they're on the same
/// LAN segment.
const bool obfuscate = cfg_.mdns_obfuscate_host_candidates
&& mdns_ != nullptr;
if (obfuscate) {
mdns_local_hostname_ = generate_uuid_v4() + ".local";
mdns_->register_name(mdns_local_hostname_);
Candidate c{};
c.type = CandidateType::HostMdns;
c.family = AddressFamily::IPv4;
c.port = local_port_;
c.priority = compute_priority(CandidateType::HostMdns, 65535, 1);
c.hostname = mdns_local_hostname_;
if (candidate_allowed(CandidateType::Host, c.family,
cfg_.candidate_filter_flags)) {
std::lock_guard lk(local_state_mu_);
local_candidates_.push_back(c);
}
/// Skip raw-IP host candidates: the whole point of mDNS
/// obfuscation is to NOT leak interior addresses. The IPv4
/// HostMdns above stands in for the entire host candidate
/// set; the resolver on the peer side discovers v4 + v6 via
/// the responder's A / AAAA records.
return;
}
struct ifaddrs* iflist = nullptr;
if (getifaddrs(&iflist) != 0) return;
for (auto* ifa = iflist; ifa; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr) continue;
if (ifa->ifa_flags & IFF_LOOPBACK) continue;
if (!(ifa->ifa_flags & IFF_UP)) continue;
Candidate c{};
c.type = CandidateType::Host;
c.port = local_port_;
if (ifa->ifa_addr->sa_family == AF_INET) {
auto* sin = reinterpret_cast<struct sockaddr_in*>(ifa->ifa_addr);
c.family = AddressFamily::IPv4;
std::memcpy(c.addr.data(), &sin->sin_addr, 4);
c.priority = compute_priority(CandidateType::Host, 65535, 1);
if (candidate_allowed(c.type, c.family,
cfg_.candidate_filter_flags)) {
std::lock_guard lk(local_state_mu_);
local_candidates_.push_back(c);
}
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
auto* sin6 = reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr);
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) continue;
c.family = AddressFamily::IPv6;
std::memcpy(c.addr.data(), &sin6->sin6_addr, 16);
c.priority = compute_priority(CandidateType::Host, 65534, 1);
if (candidate_allowed(c.type, c.family,
cfg_.candidate_filter_flags)) {
std::lock_guard lk(local_state_mu_);
local_candidates_.push_back(c);
}
}
}
freeifaddrs(iflist);
gather_tcp_host_candidates();
}
void IceSession::gather_portmap() {
/// Skip every short-circuit case the user-facing contract calls
/// out as a graceful-degradation point.
if (portmap_ext_ == nullptr) return;
if (local_port_ == 0) return;
/// Honour the same operator filter mask that gates host / srflx
/// / relay gather. Portmap candidates are emitted with type
/// `Srflx`, so a `host-only` filter drops them; a `relay-only`
/// filter also drops them. The `exclude-ipv4` flag drops them
/// because the portmap protocols answer with an IPv4 external
/// address by convention (PCP supports v6 but the in-tree plugin
/// currently surfaces v4 mappings only).
const auto filter = cfg_.candidate_filter_flags;
if ((filter & kCandidateFilterHostOnly) != 0) return;
if ((filter & kCandidateFilterRelayOnly) != 0) return;
/// Probe the supported-protocols mask first. Zero means no
/// portmap protocol is reachable from this network — the
/// extension is present but every probe came back silent. Skip
/// the request entirely so we don't hammer the underlying
/// discovery loop on every gather.
const std::uint32_t supported = portmap_ext_->supported_protocols();
if ((supported & (GN_PORTMAP_PROTO_UPNP
| GN_PORTMAP_PROTO_PCP
| GN_PORTMAP_PROTO_NATPMP)) == 0) {
return;
}
/// Ask the router for a UDP mapping with the conventional ICE
/// hint values: lifetime 3600 s (matches the typical NAT-PMP
/// default before the plugin's own renewal cadence kicks in),
/// no external-port hint (let the router pick — symmetric NATs
/// will refuse a fixed external port and we don't want to bias
/// the choice).
gn_portmap_mapping_t mapping{};
const bool ok = portmap_ext_->request(GN_PORTMAP_UDP,
local_port_,
/*external_port_hint=*/0,
/*lifetime_hint_s=*/3600,
&mapping);
if (!ok) return;
/// Defensive parse of the router-reported ext_ip — a malformed
/// blob (NUL-terminated dotted-decimal that doesn't lex as v4 or
/// v6) is treated as a failed mapping. The plugin contract says
/// the field is NUL-terminated; trust but verify.
if (mapping.ext_port == 0) return;
const std::string ext_ip(mapping.ext_ip);
if (ext_ip.empty()) return;
Candidate c{};
c.type = CandidateType::Srflx;
c.port = mapping.ext_port;
c.set_address(ext_ip);
if (c.family != AddressFamily::IPv4
&& c.family != AddressFamily::IPv6) {
/// `set_address` leaves `family` unset when the string lexes
/// as neither v4 nor v6 — drop the mapping rather than push
/// a garbage candidate. We do NOT call release here: the
/// underlying plugin still has a valid renewal entry and
/// will time it out on its own.
return;
}
/// Operator-filter gate (exclude-ipv4 / exclude-ipv6). Run after
/// the address parse so the family check is authoritative.
if (!candidate_allowed(c.type, c.family, filter)) {
return;
}
/// Priority — RFC 8445 §5.1.2.1 type-preference 100 for Srflx.
/// The local-pref slot is set to 65534 (one rung below the
/// STUN-srflx default of 65535) so STUN-discovered srflx
/// candidates outrank the portmap entry on identical
/// (component, type) tuples. Component id stays at 2 — the
/// (type_pref, local_pref, component) triple gives the portmap
/// candidate a distinct priority slot so the check-list ordering
/// is deterministic.
c.priority = compute_priority(CandidateType::Srflx, 65534, 2);
/// Foundation — RFC 8445 §5.1.1.3. The synthetic server string
/// `"portmap"` distinguishes portmap-allocated srflx from
/// STUN-allocated srflx so the foundation tuple stays unique.
/// Peers compute their own foundations from the wire-recovered
/// fields; the foundation is intentionally not on the wire.
c.foundation = compute_foundation(CandidateType::Srflx,
ext_ip,
/*server=*/"portmap",
c.transport);
{
std::lock_guard lk(local_state_mu_);
local_candidates_.push_back(std::move(c));
}
/// Stash the mapping for release on teardown. We track only the
/// most-recent mapping per session because a gather cycle issues
/// exactly one portmap request for `local_port_`; an ICE
/// restart re-runs gather and overwrites the slot after the
/// previous mapping is released.
portmap_mapping_ = mapping;
}
void IceSession::release_portmap_mapping() noexcept {
if (portmap_ext_ == nullptr) return;
if (!portmap_mapping_.has_value()) return;
const auto m = *portmap_mapping_;
portmap_mapping_.reset();
/// Best-effort — the underlying plugin handles router-side
/// failure (lifetime expiry, router reboot) on its own.
(void)portmap_ext_->release(m.protocol, m.int_port);
}
void IceSession::gather_tcp_host_candidates() {
/// RFC 6544 emits one TCP candidate per interface IP per transport
/// flavour (active / passive / simultaneous-open). The TCP carrier
/// is wired separately from UDP; absent carrier means no usable
/// TCP fallback, so the gather skips the TCP slot entirely. The
/// candidate filter flags (host-only / relay-only / exclude-ipv4
/// / exclude-ipv6) reuse the same gate as UDP candidates so an
/// operator that excludes host candidates also drops the TCP
/// host slot.
if (!cfg_.tcp_candidates_enabled) return;
if (carrier_tcp_ == nullptr) return;
if (local_port_ == 0) return;
/// Walk the freshly-emitted UDP host candidates, mirroring each
/// as TCP active / passive / simultaneous-open. `push_back` on
/// `local_candidates_` may reallocate and invalidate references
/// — so the iteration copies the source candidate by value
/// before pushing into the same vector.
std::lock_guard lk(local_state_mu_);
const std::size_t udp_end = local_candidates_.size();
static constexpr std::array<TransportType, 3> kTcpVariants = {
TransportType::TcpActive,
TransportType::TcpPassive,
TransportType::TcpSimultaneousOpen,
};
for (std::size_t i = 0; i < udp_end; ++i) {
Candidate src = local_candidates_[i];
if (src.type != CandidateType::Host) continue;
if (src.transport != TransportType::Udp) continue;
for (auto variant : kTcpVariants) {
Candidate c = src;
c.transport = variant;
local_candidates_.push_back(std::move(c));
}
}
}
void IceSession::resolve_remote_mdns_candidates() {
/// Walk the remote set for HostMdns entries that still hold an
/// unresolved hostname (`!c.hostname.empty()` && IP all zeroes).
/// Each gets a one-shot mDNS query through the shared manager;
/// the resolver callback dispatches back onto this strand,
/// rewrites the entry in place with the resolved IP, and rebuilds
/// the check list so the new pair joins the queue.
if (!mdns_) return;
/// Snapshot indices to avoid mutating remote_candidates_ during
/// iteration; resolution callbacks land on the strand later and
/// mutate the vector then.
std::vector<std::pair<std::size_t, std::string>> queue;
for (std::size_t i = 0; i < remote_candidates_.size(); ++i) {
const auto& c = remote_candidates_[i];
if (c.type != CandidateType::HostMdns) continue;
if (c.hostname.empty()) continue;
if (!is_mdns_local_name(c.hostname)) continue;
queue.emplace_back(i, c.hostname);
}
if (queue.empty()) return;
const auto timeout = std::chrono::milliseconds(
cfg_.mdns_resolve_timeout_ms > 0
? cfg_.mdns_resolve_timeout_ms : 5000);
for (auto& [idx, hostname] : queue) {
auto weak_self = std::weak_ptr<IceSession>(shared_from_this());
/// The resolver invokes the callback on its own strand. Hop
/// back to ours before touching `remote_candidates_`.
mdns_->resolve(hostname, timeout,
[weak_self, idx, hostname](const MdnsResolveResult& r) {
auto self = weak_self.lock();
if (!self) return;
asio::post(self->strand_,
[self, idx, hostname, r] {
if (self->state_.load(std::memory_order_acquire)
== SessionState::Failed) return;
if (idx >= self->remote_candidates_.size()) return;
auto& slot = self->remote_candidates_[idx];
if (slot.type != CandidateType::HostMdns) return;
if (slot.hostname != hostname) return;
if (!r.resolved
|| (r.ipv4.empty() && r.ipv6.empty())) {
/// Drop the unresolved entry. Erasing
/// shifts subsequent indices but every
/// queued callback captures its own
/// `idx`; since we used the index AT
/// THE TIME the query was issued and