is 0.9.0

ICE (Interactive Connectivity Establishment) in Sans-IO style
Documentation
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
use crate::IceError;
use serde::ser::SerializeStruct;
use serde::{Deserialize, Serialize, Serializer};
use std::collections::hash_map::DefaultHasher;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::net::{IpAddr, SocketAddr};
use str0m_proto::{Protocol, TcpType};

/// ICE candidates are network addresses used to connect to a peer.
///
/// There are different kinds of ICE candidates. The simplest kind is a
/// host candidate which is a socket address on a local (host) network interface.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Candidate {
    /// An arbitrary string used in the freezing algorithm to
    /// group similar candidates.
    ///
    /// It is the same for two candidates that
    /// have the same type, base IP address, protocol (UDP, TCP, etc.),
    /// and STUN or TURN server.  If any of these are different, then the
    /// foundation will be different.
    ///
    /// For remote, this is communicated,  and locally it's calculated.
    foundation: Option<String>, // 1-32 "ice chars", ALPHA / DIGIT / "+" / "/"

    /// A component is a piece of a data stream.
    ///
    /// A data stream may require multiple components, each of which has to
    /// work in order for the data stream as a whole to work.  For RTP/RTCP
    /// data streams, unless RTP and RTCP are multiplexed in the same port,
    /// there are two components per data stream -- one for RTP, and one
    /// for RTCP.
    component_id: u16, // 1 for RTP, 2 for RTCP

    /// Protocol for the candidate.
    proto: Protocol,

    /// Priority.
    ///
    /// For remote, this is communicated, and locally it's (mostly) calculated.
    /// For local peer reflexive it is set.
    prio: Option<u32>, // 1-10 digits

    /// The actual address to use. This might be a host address, server reflex, relay etc.
    addr: SocketAddr, // ip/port

    /// The base address
    ///
    /// "Base" refers to the address an agent sends from for a
    /// particular candidate.  Thus, as a degenerate case, host candidates
    /// also have a base, but it's the same as the host candidate.
    ///
    /// * host - same as `addr`, i.e the local interface address
    /// * peer/server reflexive - the local interface address
    /// * relay - same as `addr`, the allocation on the TURN server
    base: Option<SocketAddr>, // the "base" used for local candidates.

    /// Type of candidate.
    kind: CandidateKind, // host/srflx/prflx/relay

    /// TCP connection role specified by the optional `tcptype` ICE candidate
    /// extension.
    tcptype: Option<TcpType>,

    /// Related address.
    ///
    /// For server-reflexive candidates, this is the internal IP/port the candidate corresponds to
    /// (the one behind the NAT, usually). For relay candidates, this is the mapped address selected
    /// by the TURN server.
    raddr: Option<SocketAddr>, // ip/port

    /// Ufrag.
    ///
    /// This is used to tie an ice candidate to a specific ICE session. It's important
    /// when trickle ICE is used in conjunction with ice restart, since it must be
    /// possible the ice agent to know whether a candidate appearing belongs to
    /// the current or previous session.
    ///
    /// This value is only set for incoming candidates. Once we use the candidate inside
    /// pairs, the field is blanked to not be confusing during ice-restarts.
    ufrag: Option<String>,

    /// The address of the local interface.
    ///
    /// * host - same as `addr`
    /// * peer/server reflexive - same as `base`
    /// * relay - set explicitly
    ///
    /// This is an extension to the ICE spec that we use to track
    /// local interfaces also for relayed candidates.
    local: SocketAddr,

    /// The ice agent might assign a local preference if we have multiple candidates
    /// that are the same type.
    local_preference: Option<u32>,

    /// If we discarded this candidate (for example due to being redundant
    /// against another candidate).
    discarded: bool,
}

impl fmt::Debug for Candidate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Candidate({}={}/{}", self.kind, self.addr, self.proto)?;
        if let Some(base) = self.base {
            if base != self.addr {
                write!(f, " base={base}")?;
            }
        }
        if let Some(raddr) = self.raddr {
            write!(f, " raddr={raddr}")?;
        }
        write!(f, " prio={}", self.prio())?;
        if self.discarded {
            write!(f, " discarded")?;
        }
        write!(f, ")")
    }
}

impl Candidate {
    /// Starts the typesafe builder.
    ///
    /// # Example
    ///
    /// ```
    /// # use is::Candidate;
    /// # use is::IceError;
    /// # use str0m_proto::TcpType;
    /// # use std::net::SocketAddr;
    ///
    /// let addr: SocketAddr = "192.168.1.1:12345".parse().unwrap();
    ///
    /// // A standard UDP host candidate
    /// let udp_host = Candidate::builder()
    ///     .udp()
    ///     .host(addr)
    ///     .build()?;
    ///
    /// // A TCP host candidate with a passive role
    /// let tcp_host = Candidate::builder()
    ///     .tcp()
    ///     .host(addr)
    ///     .tcptype(TcpType::Passive)
    ///     .build()?;
    ///
    /// # Ok::<(), IceError>(())
    /// ```
    pub fn builder() -> CandidateBuilder<NoProtocol, NoRoute> {
        CandidateBuilder {
            protocol_state: NoProtocol(()),
            route_state: NoRoute(()),
            foundation: None,
            component_id: 1, // Default RTP
            prio: None,
            tcptype: None,
            raddr: None,
            ufrag: None,
            local_preference: None,
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn new(
        foundation: Option<String>,
        component_id: u16,
        proto: Protocol,
        prio: Option<u32>,
        addr: SocketAddr,
        base: Option<SocketAddr>,
        kind: CandidateKind,
        raddr: Option<SocketAddr>,
        tcptype: Option<TcpType>,
        ufrag: Option<String>,
        local: SocketAddr,
    ) -> Self {
        Candidate {
            foundation,
            component_id,
            proto,
            prio,
            addr,
            base,
            kind,
            raddr,
            tcptype,
            ufrag,
            local,
            local_preference: None,
            discarded: false,
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub fn from_parts(
        foundation: String,
        component_id: u16,
        proto: Protocol,
        prio: u32,
        addr: SocketAddr,
        kind: CandidateKind,
        raddr: Option<SocketAddr>,
        tcptype: Option<TcpType>,
        ufrag: Option<String>,
    ) -> Self {
        Candidate::new(
            Some(foundation),
            component_id,
            proto,
            Some(prio),
            addr,
            None,
            kind,
            raddr,
            tcptype,
            ufrag,
            match kind {
                CandidateKind::Host => addr,
                CandidateKind::PeerReflexive
                | CandidateKind::ServerReflexive
                | CandidateKind::Relayed => Self::arbitrary_raddr(addr),
            },
        )
    }

    /// Creates a host ICE candidate.
    ///
    /// Host candidates are local sockets directly on the host.
    pub fn host(addr: SocketAddr, proto: impl TryInto<Protocol>) -> Result<Self, IceError> {
        if !is_valid_ip(addr.ip()) {
            return Err(IceError::BadCandidate(format!("invalid ip {}", addr.ip())));
        }

        Ok(Candidate::new(
            None,
            1, // only RTP
            parse_proto(proto)?,
            None,
            addr,
            Some(addr),
            CandidateKind::Host,
            None,
            None,
            None,
            addr,
        ))
    }

    /// Creates a server reflexive ICE candidate.
    ///
    /// Server reflexive candidates are local sockets mapped to external ip discovered
    /// via a STUN binding request.
    /// The `base` is the local interface that this address corresponds to.
    pub fn server_reflexive(
        addr: SocketAddr,
        base: SocketAddr,
        proto: impl TryInto<Protocol>,
    ) -> Result<Self, IceError> {
        if !is_valid_ip(addr.ip()) {
            return Err(IceError::BadCandidate(format!("invalid ip {}", addr.ip())));
        }

        if addr.is_ipv4() != base.is_ipv4() {
            return Err(IceError::BadCandidate(
                "addr and base are different IP versions".to_owned(),
            ));
        }

        Ok(Candidate::new(
            None,
            1, // only RTP
            parse_proto(proto)?,
            None,
            addr,
            Some(base),
            CandidateKind::ServerReflexive,
            Some(Self::arbitrary_raddr(addr)),
            None,
            None,
            base,
        ))
    }

    /// Creates a relayed ICE candidate.
    ///
    /// Relayed candidates are server sockets relaying traffic to a local socket.
    /// Allocate a TURN addr to use as a local candidate.
    ///
    /// * `addr` - The TURN server's allocated address for relaying traffic.
    ///   This is the address used for communication with the peer.
    /// * `local` - The local interface address for this candidate.
    ///   This is the address from which the TURN allocation request was sent.
    /// * `proto` - The transport protocol to use (UDP, TCP, etc.).
    pub fn relayed(
        addr: SocketAddr,
        local: SocketAddr,
        proto: impl TryInto<Protocol>,
    ) -> Result<Self, IceError> {
        if !is_valid_ip(addr.ip()) {
            return Err(IceError::BadCandidate(format!("invalid ip {}", addr.ip())));
        }

        Ok(Candidate::new(
            None,
            1, // only RTP
            parse_proto(proto)?,
            None,
            addr,
            Some(addr),
            CandidateKind::Relayed,
            Some(Self::arbitrary_raddr(addr)),
            None,
            None,
            local,
        ))
    }

    /// Creates a new ICE candidate from a candidate attribute string.
    ///
    /// Expects the format from RFC 5245 section 15.1, e.g.
    /// `candidate:1 1 udp 2113929471 203.0.113.100 10100 typ host`.
    pub fn from_sdp_string(s: &str) -> Result<Self, IceError> {
        crate::parse::parse_candidate(s)
    }

    /// Creates a peer reflexive ICE candidate.
    ///
    /// Peer reflexive candidates are NAT:ed addresses discovered via STUN
    /// binding responses. `addr` is the discovered address. `base` is the local
    /// (host) address inside the NAT we used to get this response.
    pub(crate) fn peer_reflexive(
        proto: impl TryInto<Protocol>,
        addr: SocketAddr,
        base: SocketAddr,
        prio: u32,
        found: Option<String>,
        ufrag: String,
    ) -> Self {
        Candidate::new(
            found,
            1, // only RTP
            parse_proto(proto).expect("internal call to have correct protocol"),
            Some(prio),
            addr,
            Some(base),
            CandidateKind::PeerReflexive,
            None,
            None,
            Some(ufrag),
            base,
        )
    }

    /// Create an arbitrary socket address, matching the format of the input address,
    /// for placement in the `raddr` field.
    ///
    /// For non-host candidates, Firefox (and perhaps others) require the SDP string to
    /// contain `raddr` and `rport` to correctly parse. While we could put honest values
    /// here, those honest values are likely private IP addresses that we would rather not
    /// expose to the world. Instead, browsers often spoof values to go here instead, and
    /// we do the same.
    ///
    /// Note that `raddr` and `rport` are only for diagnostic purposes, and have no
    /// bearing on ICE connectivity checks.
    fn arbitrary_raddr(s: SocketAddr) -> SocketAddr {
        use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
        match s {
            SocketAddr::V4(_) => SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)),
            SocketAddr::V6(_) => SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0)),
        }
    }

    #[cfg(test)]
    pub(crate) fn test_peer_rflx(
        addr: SocketAddr,
        base: SocketAddr,
        proto: impl TryInto<Protocol>,
    ) -> Self {
        Candidate::new(
            None,
            1, // only RTP
            parse_proto(proto).expect("internal test to have correct protocol"),
            None,
            addr,
            Some(base),
            CandidateKind::PeerReflexive,
            None,
            None,
            None,
            base,
        )
    }

    /// Candidate foundation.
    ///
    /// For local candidates this is calculated.
    pub(crate) fn foundation(&self) -> String {
        if let Some(v) = &self.foundation {
            return v.clone();
        }

        // Two candidates have the same foundation when all of the
        // following are true:
        let mut hasher = DefaultHasher::new();

        //  o  They have the same type (host, relayed, server reflexive, or peer
        //     reflexive).
        self.kind.hash(&mut hasher);

        //  o  Their bases have the same IP address (the ports can be different).
        self.base().ip().hash(&mut hasher);

        //  o  For reflexive and relayed candidates, the STUN or TURN servers
        //     used to obtain them have the same IP address (the IP address used
        //     by the agent to contact the STUN or TURN server).
        if let Some(raddr) = self.raddr {
            raddr.ip().hash(&mut hasher);
        }

        //  o  They were obtained using the same transport protocol (TCP, UDP).
        self.proto.hash(&mut hasher);

        let hash = hasher.finish();

        format!("{:08x}{hash:x}", self.prio().to_be())
    }

    /// Returns the priority value for the specified ICE candidate.
    ///
    /// The priority is a positive integer between 1 and 2^31 - 1 (inclusive), calculated
    /// according to the ICE specification defined in RFC 8445, Section 5.1.2.
    pub fn prio(&self) -> u32 {
        self.do_prio(false)
    }

    pub(crate) fn prio_prflx(&self) -> u32 {
        self.do_prio(true)
    }

    fn do_prio(&self, as_prflx: bool) -> u32 {
        // Remote candidates have their prio calculated on their side.
        if let Some(prio) = &self.prio {
            return *prio;
        }

        let kind = if as_prflx {
            CandidateKind::PeerReflexive
        } else {
            self.kind
        };

        // Per RFC5245 Sec. 4.1.2.1, the RECOMMENDED values for type preferences are
        // 126 for host candidates, 110 for peer-reflexive candidates, 100 for
        // server-reflexive candidates, and 0 for relayed candidates. The variations
        // for non-UDP protocols are taken from libwebrtc:
        // <https://webrtc.googlesource.com/src/+/refs/heads/main/p2p/base/port.h#68>
        let type_preference = match (kind, self.proto) {
            (CandidateKind::Host, Protocol::Udp) => 126,
            (CandidateKind::PeerReflexive, Protocol::Udp) => 110,
            (CandidateKind::ServerReflexive, _) => 100,
            (CandidateKind::Host, _) => 90,
            (CandidateKind::PeerReflexive, _) => 80,
            (CandidateKind::Relayed, Protocol::Udp) => 2,
            (CandidateKind::Relayed, Protocol::Tcp) => 1,
            (CandidateKind::Relayed, _) => 0,
        };

        // The recommended formula combines a preference for the candidate type
        // (server reflexive, peer reflexive, relayed, and host), a preference
        // for the IP address for which the candidate was obtained, and a
        // component ID using the following formula:
        //
        // priority = (2^24)*(type preference) +
        //     (2^8)*(local preference) +
        //     (2^0)*(256 - component ID)
        let prio =
            type_preference << 24 | self.local_preference() << 8 | (256 - self.component_id as u32);

        // https://datatracker.ietf.org/doc/html/rfc8445#section-5.1.2
        // MUST be a positive integer between 1 and (2**31 - 1)
        assert!(prio >= 1 && prio < 2_u32.pow(31));

        prio
    }

    pub(crate) fn local_preference(&self) -> u32 {
        self.local_preference
            .unwrap_or_else(|| if self.addr.is_ipv6() { 65_535 } else { 65_534 })
    }

    pub(crate) fn component_id(&self) -> u16 {
        self.component_id
    }

    /// Returns the address for the specified ICE candidate.
    pub fn addr(&self) -> SocketAddr {
        self.addr
    }

    /// Returns the _local_ address of this ICE candidate.
    ///
    /// This is an extension to the ICE spec that we use to track
    /// local interfaces also for relayed candidates.
    /// Specifically, for relayed candidates, the local interface
    /// is the address the agent uses to communicate with the TURN
    /// server using the TURN protocol.
    ///
    /// For all other candidates, this refers to either the
    /// candidate itself (host) or the base (srflx/prflx).
    pub fn local(&self) -> SocketAddr {
        self.local
    }

    /// Returns a reference to the String containing the transport protocol of
    /// the ICE candidate. For example tcp/udp/..
    pub fn proto(&self) -> Protocol {
        self.proto
    }

    pub(crate) fn base(&self) -> SocketAddr {
        self.base.unwrap_or(self.addr)
    }

    pub(crate) fn raddr(&self) -> Option<SocketAddr> {
        self.raddr
    }

    /// Returns the candidate's TCP role. Refer to [`TcpType`] for more details.
    pub fn tcptype(&self) -> Option<TcpType> {
        self.tcptype
    }

    /// Returns the kind of this candidate.
    pub fn kind(&self) -> CandidateKind {
        self.kind
    }

    pub(crate) fn set_local_preference(&mut self, v: u32) {
        self.local_preference = Some(v);
    }

    pub(crate) fn set_discarded(&mut self, discarded: bool) {
        self.discarded = discarded;
    }

    pub(crate) fn discarded(&self) -> bool {
        self.discarded
    }

    pub fn set_ufrag(&mut self, ufrag: &str) {
        self.ufrag = Some(ufrag.into());
    }

    pub fn ufrag(&self) -> Option<&str> {
        self.ufrag.as_deref()
    }

    pub(crate) fn clear_ufrag(&mut self) {
        self.ufrag = None;
    }

    /// Generates a candidate attribute string.
    pub fn to_sdp_string(&self) -> String {
        let mut s = format!(
            "candidate:{} {} {} {} {} {} typ {}",
            self.foundation(),
            self.component_id,
            self.proto,
            self.prio(),
            self.addr.ip(),
            self.addr.port(),
            self.kind
        );
        if let Some(raddr) = &self.raddr {
            s.push_str(&format!(" raddr {} rport {}", raddr.ip(), raddr.port()))
        }
        // https://datatracker.ietf.org/doc/html/rfc6544#section-4.5
        // the RFC requires tcptype to be defined strictly after raddr if exists.
        if let Some(tcptype) = &self.tcptype {
            s.push_str(&format!(" tcptype {}", tcptype));
        }
        if let Some(ufrag) = &self.ufrag {
            s.push_str(&format!(" ufrag {}", ufrag));
        }
        s
    }
}

impl fmt::Display for Candidate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_sdp_string())
    }
}

fn parse_proto(proto: impl TryInto<Protocol>) -> Result<Protocol, IceError> {
    proto
        .try_into()
        .map_err(|_| IceError::BadCandidate("invalid protocol".into()))
}

/// Type of candidate.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CandidateKind {
    /// Host (local network interface)
    Host,
    /// Prflx (Peer reflexive)
    PeerReflexive,
    /// Srflx (STUN)
    ServerReflexive,
    /// Relay (TURN)
    Relayed,
}

impl fmt::Display for CandidateKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let x = match self {
            CandidateKind::Host => "host",
            CandidateKind::PeerReflexive => "prflx",
            CandidateKind::ServerReflexive => "srflx",
            CandidateKind::Relayed => "relay",
        };
        write!(f, "{x}")
    }
}

fn is_valid_ip(ip: IpAddr) -> bool {
    match ip {
        IpAddr::V4(v) => {
            !v.is_link_local() && !v.is_broadcast() && !v.is_multicast() && !v.is_unspecified()
        }
        IpAddr::V6(v) => !v.is_multicast() && !v.is_unspecified(),
    }
}

/// Serialize [Candidate] into candidate info.
///
/// Always set `sdpMid` to null and `sdpMLineIndex` to 0, as we only support one media line.
///
/// e.g. serde_json would produce:
/// ```json
/// {
///  "candidate": "candidate:12044049749558888150 1 udp 2130706175 1.2.3.4 1234 typ host",
///  "sdpMid": null,
///  "sdpMLineIndex": 0
///  "usernameFragment": "ufrag"
/// }
/// ```
impl Serialize for Candidate {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut o = serializer.serialize_struct("CandidateInfo", 4)?;
        o.serialize_field("candidate", &self.to_sdp_string())?;
        o.serialize_field("sdpMid", &None::<()>)?;
        o.serialize_field("sdpMLineIndex", &0)?;
        o.serialize_field("usernameFragment", &self.ufrag())?;
        o.end()
    }
}

/// Deserialize [Candidate] from a candidate info.
///
/// Similar to [Candidate::serialize], we drop `sdpMid` and `sdpMLineIndex` when parsing.
impl<'de> Deserialize<'de> for Candidate {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct CandidateInfo {
            candidate: String,
            username_fragment: Option<String>,
        }

        let CandidateInfo {
            candidate,
            username_fragment,
        } = CandidateInfo::deserialize(deserializer)?;

        let mut candidate =
            Candidate::from_sdp_string(&candidate).map_err(serde::de::Error::custom)?;

        if let Some(ufrag) = username_fragment {
            candidate.set_ufrag(&ufrag);
        }

        Ok(candidate)
    }
}

#[doc(hidden)]
/// Marker for a builder requiring a protocol selection.
pub struct NoProtocol(());

#[doc(hidden)]
/// State indicating a transport protocol has been assigned to the builder.
pub struct HasProtocol<P> {
    _marker: PhantomData<P>,
    protocol: Protocol,
}

#[doc(hidden)]
/// Marker for a builder using the UDP protocol.
pub struct Udp(());

#[doc(hidden)]
/// Marker for a builder using a TCP-based protocol.
pub struct Tcp(());

#[doc(hidden)]
/// Marker indicating the candidate's network route (addresses and kind) is not yet defined.
pub struct NoRoute(());

#[doc(hidden)]
/// State containing the network route and addresses required to build a candidate.
pub struct HasRoute {
    kind: CandidateKind,
    addr: SocketAddr,
    base: SocketAddr,
    local: SocketAddr,
}

/// A typesafe builder for constructing a [`Candidate`].
///
/// This builder uses the Type State Pattern to enforce correct construction order and
/// protocol-specific constraints (such as preventing `tcptype` on UDP).
pub struct CandidateBuilder<P, R> {
    foundation: Option<String>,
    component_id: u16,
    prio: Option<u32>,
    tcptype: Option<TcpType>,
    raddr: Option<SocketAddr>,
    ufrag: Option<String>,
    local_preference: Option<u32>,

    protocol_state: P,
    route_state: R,
}

// Step 1: Protocol Selection
impl CandidateBuilder<NoProtocol, NoRoute> {
    /// Sets the protocol to UDP.
    pub fn udp(self) -> CandidateBuilder<HasProtocol<Udp>, NoRoute> {
        self.into_has_protocol(Protocol::Udp)
    }

    /// Sets the protocol to standard TCP.
    pub fn tcp(self) -> CandidateBuilder<HasProtocol<Tcp>, NoRoute> {
        self.into_has_protocol(Protocol::Tcp)
    }

    /// Sets the protocol to SSL-over-TCP.
    pub fn ssl_tcp(self) -> CandidateBuilder<HasProtocol<Tcp>, NoRoute> {
        self.into_has_protocol(Protocol::SslTcp)
    }

    /// Sets the protocol to TLS.
    pub fn tls(self) -> CandidateBuilder<HasProtocol<Tcp>, NoRoute> {
        self.into_has_protocol(Protocol::Tls)
    }

    fn into_has_protocol<NewP>(self, p: Protocol) -> CandidateBuilder<HasProtocol<NewP>, NoRoute> {
        CandidateBuilder {
            protocol_state: HasProtocol {
                _marker: PhantomData,
                protocol: p,
            },
            route_state: self.route_state,
            foundation: self.foundation,
            component_id: self.component_id,
            prio: self.prio,
            tcptype: self.tcptype,
            raddr: self.raddr,
            ufrag: self.ufrag,
            local_preference: self.local_preference,
        }
    }
}

// Step 2: Route Selection
impl<P> CandidateBuilder<HasProtocol<P>, NoRoute> {
    /// Configures as a host candidate.
    pub fn host(self, addr: SocketAddr) -> CandidateBuilder<HasProtocol<P>, HasRoute> {
        self.into_has_route(CandidateKind::Host, addr, addr, addr, None)
    }

    /// Configures as a Server Reflexive (STUN) candidate.
    /// `base` and `addr` must use the same IP version.
    pub fn server_reflexive(
        self,
        addr: SocketAddr,
        base: SocketAddr,
    ) -> CandidateBuilder<HasProtocol<P>, HasRoute> {
        self.into_has_route(
            CandidateKind::ServerReflexive,
            addr,
            base,
            base,
            Some(Candidate::arbitrary_raddr(addr)),
        )
    }

    /// Configures as a Relayed (TURN) candidate.
    /// Base is set to `addr`, and `local` is the interface address.
    pub fn relayed(
        self,
        addr: SocketAddr,
        local: SocketAddr,
    ) -> CandidateBuilder<HasProtocol<P>, HasRoute> {
        self.into_has_route(
            CandidateKind::Relayed,
            addr,
            addr,
            local,
            Some(Candidate::arbitrary_raddr(addr)),
        )
    }

    fn into_has_route(
        self,
        kind: CandidateKind,
        addr: SocketAddr,
        base: SocketAddr,
        local: SocketAddr,
        raddr: Option<SocketAddr>,
    ) -> CandidateBuilder<HasProtocol<P>, HasRoute> {
        CandidateBuilder {
            protocol_state: self.protocol_state,
            route_state: HasRoute {
                kind,
                addr,
                base,
                local,
            },
            raddr,
            foundation: self.foundation,
            component_id: self.component_id,
            prio: self.prio,
            tcptype: self.tcptype,
            ufrag: self.ufrag,
            local_preference: self.local_preference,
        }
    }
}

// Step 3: Final General Configurations and Build
impl<P> CandidateBuilder<HasProtocol<P>, HasRoute> {
    /// Consumes the builder and returns a [`Candidate`].
    pub fn build(self) -> Result<Candidate, IceError> {
        let route = &self.route_state;
        if !is_valid_ip(route.addr.ip()) {
            return Err(IceError::BadCandidate(format!(
                "Invalid IP: {}",
                route.addr.ip()
            )));
        }

        if route.addr.is_ipv4() != route.base.is_ipv4() {
            return Err(IceError::BadCandidate(format!(
                "address IP version mismatch: addr={} base={}",
                route.addr.ip(),
                route.base.ip(),
            )));
        }

        Ok(Candidate::new(
            self.foundation,
            self.component_id,
            self.protocol_state.protocol,
            self.prio,
            route.addr,
            Some(route.base),
            route.kind,
            self.raddr,
            self.tcptype,
            self.ufrag,
            route.local,
        ))
    }
}

impl<R> CandidateBuilder<HasProtocol<Tcp>, R> {
    /// Configures the TCP type (active, passive, so).
    pub fn tcptype(mut self, t: TcpType) -> Self {
        self.tcptype = Some(t);
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basic_serialize_deserialize() {
        let socket_addr = "1.2.3.4:9876".parse().unwrap();
        let candidates = [
            Candidate::host(socket_addr, Protocol::Udp).unwrap(),
            Candidate::host(socket_addr, Protocol::Tcp).unwrap(),
            Candidate::builder()
                .tcp()
                .host(socket_addr)
                .tcptype(TcpType::Passive)
                .build()
                .unwrap(),
        ];

        for c1 in &candidates {
            let json = serde_json::to_string(&c1).unwrap();
            let c2: Candidate = serde_json::from_str(&json).unwrap();
            assert_eq!(c1.proto, c2.proto);
            assert_eq!(c1.tcptype, c2.tcptype);

            // Can't test equality because foundation is calculated on the fly. Use string compare instead.
            assert_eq!(c1.to_string(), c2.to_string());
        }
    }

    #[test]
    fn serialize() {
        let socket_addr = "1.2.3.4:9876".parse().unwrap();
        let mut candidate = Candidate::host(socket_addr, Protocol::Udp).unwrap();
        let json = no_hash(serde_json::to_string(&candidate).unwrap());
        let got: serde_json::Value = serde_json::from_str(&json).unwrap();
        let expected = serde_json::json!({
            "candidate": "candidate:--- 1 udp 2130706175 1.2.3.4 9876 typ host",
            "sdpMid": null,
            "sdpMLineIndex": 0,
            "usernameFragment": null
        });
        assert_eq!(got, expected);

        // Add a username fragment
        candidate.ufrag = Some("ufrag".to_string());
        let json = no_hash(serde_json::to_string(&candidate).unwrap());
        let got: serde_json::Value = serde_json::from_str(&json).unwrap();
        let expected = serde_json::json!({
            "candidate": "candidate:--- 1 udp 2130706175 1.2.3.4 9876 typ host ufrag ufrag",
            "sdpMid": null,
            "sdpMLineIndex": 0,
            "usernameFragment": "ufrag"
        });
        assert_eq!(got, expected);
    }

    fn no_hash(mut s: String) -> String {
        let f = s.find("candidate:").unwrap();
        let t = s.find(" 1 ").unwrap();
        s.replace_range((f + 10)..t, "---");
        s
    }

    #[test]
    fn deserialize() {
        let json = serde_json::json!({
            "candidate": "candidate:12044049749558888150 1 udp 2130706175 1.2.3.4 9876 typ host ufrag ufrag",
            "sdpMid": "ignored",
            "sdpMLineIndex": 123,
            "usernameFragment": "ufrag"
        }).to_string();
        let candidate: Candidate = serde_json::from_str(&json).unwrap();
        assert_eq!(candidate.ufrag(), Some("ufrag"));
        assert_eq!(candidate.addr().to_string(), "1.2.3.4:9876");
        assert_eq!(candidate.base().to_string(), "1.2.3.4:9876");
        assert_eq!(candidate.kind(), CandidateKind::Host);
        assert_eq!(candidate.proto(), Protocol::Udp);
        assert_eq!(candidate.prio(), 2130706175);
        assert_eq!(candidate.component_id(), 1);
        assert_eq!(candidate.raddr(), None);
        assert!(!candidate.discarded());
    }

    #[test]
    fn to_string() {
        let local_addr = "7.8.9.0:2345".parse().unwrap();
        let socket_addr = "1.2.3.4:9876".parse().unwrap();
        let mut candidate = Candidate::host(socket_addr, Protocol::Udp).unwrap();
        assert_eq!(
            no_hash(candidate.to_string()),
            "candidate:--- 1 udp 2130706175 1.2.3.4 9876 typ host"
        );

        candidate.ufrag = Some("ufrag".into());
        assert_eq!(
            no_hash(candidate.to_string()),
            "candidate:--- 1 udp 2130706175 1.2.3.4 9876 typ host ufrag ufrag"
        );

        candidate.raddr = Some("5.5.5.5:5555".parse().unwrap());
        assert_eq!(
            no_hash(candidate.to_string()),
            "candidate:--- 1 udp 2130706175 1.2.3.4 9876 typ host raddr 5.5.5.5 rport 5555 ufrag ufrag"
        );

        // let base_addr = "5.6.7.8:4321".parse().unwrap();

        let candidate = Candidate::relayed(socket_addr, local_addr, Protocol::SslTcp).unwrap();
        assert_eq!(
            no_hash(candidate.to_string()),
            "candidate:--- 1 ssltcp 16776959 1.2.3.4 9876 typ relay raddr 0.0.0.0 rport 0"
        );
    }

    #[test]
    fn new_from_sdp_string() {
        let candidate = Candidate::from_sdp_string(
            "candidate:fffeff7e5e895846293d220a 1 udp 2130706175 1.2.3.4 9876 typ host ufrag myuserfrag",
        )
        .unwrap();

        assert_eq!(candidate.ufrag(), Some("myuserfrag"));
        assert_eq!(candidate.addr().to_string(), "1.2.3.4:9876");
    }

    #[test]
    fn spoofed_raddr() {
        let local_addr = "7.8.9.0:2345".parse().unwrap();
        let socket_addr = "1.2.3.4:9876".parse().unwrap();
        let base_addr = "5.6.7.8:4321".parse().unwrap();

        let host = Candidate::host(socket_addr, Protocol::Udp).unwrap();
        assert!(host.raddr().is_none());

        // We're not picky on the exact choice, but it must not be the private base
        let relay = Candidate::relayed(socket_addr, local_addr, Protocol::Udp).unwrap();
        assert!(relay.raddr().is_some());
        let srflx = Candidate::server_reflexive(socket_addr, base_addr, Protocol::Udp).unwrap();
        assert!(srflx.raddr().is_some_and(|raddr| raddr != base_addr));

        let prflx = Candidate::peer_reflexive(
            Protocol::Udp,
            socket_addr,
            base_addr,
            1000,
            None,
            "ufrag".into(),
        );
        assert!(prflx.raddr().is_none());
    }

    #[test]
    fn bad_candidate() {
        let s = "candidate:12344 bad value";
        assert!(Candidate::from_sdp_string(s).is_err());
    }

    #[test]
    fn tcp_candidates_sanity() {
        let socket_addr = "1.2.3.4:9876".parse().unwrap();
        let candidates = [
            Candidate::host(socket_addr, Protocol::Tcp).unwrap(),
            Candidate::builder()
                .tcp()
                .host(socket_addr)
                .build()
                .unwrap(),
            Candidate::builder()
                .tcp()
                .host(socket_addr)
                .tcptype(TcpType::Passive)
                .build()
                .unwrap(),
            Candidate::builder()
                .ssl_tcp()
                .host(socket_addr)
                .tcptype(TcpType::Active)
                .build()
                .unwrap(),
            Candidate::builder()
                .tls()
                .host(socket_addr)
                .tcptype(TcpType::So)
                .build()
                .unwrap(),
        ];

        assert!(!candidates[0].to_sdp_string().contains("tcptype"));
        assert!(!candidates[1].to_sdp_string().contains("tcptype"));
        assert!(candidates[2].to_sdp_string().contains("tcptype passive"));
        assert!(candidates[3].to_sdp_string().contains("tcptype active"));
        assert!(candidates[4].to_sdp_string().contains("tcptype so"));
    }

    #[test]
    fn lexical_ordering_of_sdp_is_follows_priority() {
        let mut candidates = Vec::from([
            host("1.1.1.1:0"),
            host("2.2.2.2:0"),
            srflx("3.3.3.3:0", "4.4.4.4:0"),
            srflx("5.5.5.5:0", "6.6.6.6:0"),
            relay("8.8.8.8:0", "9.9.9.9:0"),
            relay("7.7.7.7:0", "9.9.9.9:0"),
        ]);
        candidates.sort();

        assert!(candidates[0].contains("relay"));
        assert!(candidates[1].contains("relay"));
        assert!(candidates[2].contains("srflx"));
        assert!(candidates[3].contains("srflx"));
        assert!(candidates[4].contains("host"));
        assert!(candidates[5].contains("host"));
    }

    #[test]
    fn srflx_candidate_disallows_mixed_ip_versions() {
        let error = Candidate::server_reflexive(
            "10.0.0.1:1000".parse().unwrap(),
            "[::1]:1000".parse().unwrap(),
            "udp",
        )
        .unwrap_err();

        assert_eq!(
            error.to_string(),
            "ICE bad candidate: addr and base are different IP versions"
        );
    }

    fn host(socket: &str) -> String {
        Candidate::host(socket.parse().unwrap(), "udp")
            .unwrap()
            .to_sdp_string()
    }

    fn srflx(addr: &str, base: &str) -> String {
        Candidate::server_reflexive(addr.parse().unwrap(), base.parse().unwrap(), "udp")
            .unwrap()
            .to_sdp_string()
    }

    fn relay(addr: &str, local: &str) -> String {
        Candidate::relayed(addr.parse().unwrap(), local.parse().unwrap(), "udp")
            .unwrap()
            .to_sdp_string()
    }

    #[test]
    fn builder_matches_api_host() {
        let addr = "1.2.3.4:1234".parse().unwrap();

        // UDP Host
        let api = Candidate::host(addr, Protocol::Udp).unwrap();
        let builder = Candidate::builder().udp().host(addr).build().unwrap();
        assert_eq!(api, builder);

        // TCP Host
        let api = Candidate::host(addr, Protocol::Tcp).unwrap();
        let builder = Candidate::builder().tcp().host(addr).build().unwrap();
        assert_eq!(api, builder);
    }

    #[test]
    fn builder_matches_api_server_reflexive() {
        let addr = "95.1.1.1:5000".parse().unwrap();
        let base = "192.168.1.50:5000".parse().unwrap();

        let api = Candidate::server_reflexive(addr, base, Protocol::Udp).unwrap();
        let builder = Candidate::builder()
            .udp()
            .server_reflexive(addr, base)
            .build()
            .unwrap();

        assert_eq!(api, builder);
    }

    #[test]
    fn builder_matches_api_relayed() {
        let addr = "1.2.3.4:3478".parse().unwrap(); // TURN relay addr
        let local = "192.168.1.50:5000".parse().unwrap(); // Local interface

        let api = Candidate::relayed(addr, local, Protocol::Udp).unwrap();
        let builder = Candidate::builder()
            .udp()
            .relayed(addr, local)
            .build()
            .unwrap();

        assert_eq!(api, builder);
    }

    #[test]
    fn builder_consistency_errors() {
        let v4 = "1.2.3.4:1234".parse().unwrap();
        let v6 = "[2001:db8::1]:1234".parse().unwrap();

        // API returns error on mixed versions
        let api_err = Candidate::server_reflexive(v4, v6, Protocol::Udp);
        // Builder should return same error type
        let builder_err = Candidate::builder().udp().server_reflexive(v4, v6).build();

        assert!(api_err.is_err());
        assert!(builder_err.is_err());
    }
}