dyns 0.7.2

DNS discovery and resolver support for DHTTP applications
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
1200
1201
1202
1203
1204
use std::{
    convert::TryFrom,
    fmt::Display,
    hash::{Hash, Hasher},
    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
    path::Path,
};

use base64::Engine;
use bytes::BufMut;
use dhttp_identity::certificate::{CertificateChainKey, CertificateSequence};
use dquic::qbase::net::addr::EndpointAddr as DquicEndpointAddr;
use nom::{
    IResult, Parser,
    bytes::streaming::take,
    combinator::{flat_map, map},
    error::{ErrorKind, make_error},
    number::streaming::{be_u8, be_u16, be_u32, be_u128},
};
use rustls::{SignatureScheme, pki_types::SubjectPublicKeyInfoDer};
use snafu::{ResultExt, Snafu};

use crate::core::parser::{
    sigin,
    varint::{VarInt, WriteVarInt, be_varint},
};

#[derive(Debug, Snafu)]
#[snafu(module)]
pub enum SignEndpointError {
    #[snafu(display("failed to sign endpoint address"))]
    Sign {
        source: dhttp_identity::identity::SignError,
    },
    #[snafu(display("no supported signature scheme for endpoint address"))]
    NoSupportedScheme,
}

/// EndpointAddress record (Type E = 266)
///
/// Unified endpoint format that encodes address family, routing, clustering and NAT information
/// in a single flags byte, followed by optional fields.
///
/// ## Wire format
///
/// ```text
/// +-------+-----------------+--------------------+----------------+----------------------------+
/// | flags | sequence(varint)| addr               | load(optional) | signature (optional)       |
/// +-------+-----------------+--------------------+----------------+----------------------------+
/// | u8    | QUIC varint     | see addr layout    | f32            | scheme(u16)+len(varint)+N  |
/// +-------+-----------------+--------------------+----------------+----------------------------+
///
/// addr layout:
/// +---------+-----------------------------------------+
/// | kind    | addr fields                             |
/// +---------+-----------------------------------------+
/// | direct  | port(u16) + IP(u32/u128)                |
/// +---------+-----------------------------------------+
/// | nat     | outer_port(u16) + outer_IP(u32/u128)    |
/// |         | + agent_port(u16) + agent_IP(u32/u128)  |
/// +---------+-----------------------------------------+
/// ```
///
/// ## flags (u8) bit layout (bit 1 = MSB)
///
/// - bit 1 (0x80): FAMILY    — address family: 0=IPv4, 1=IPv6
/// - bit 2 (0x40): MAIN      — primary/backup: 1=primary, 0=backup
/// - bit 3 (0x20): CLUSTERED — multiple hosts share this name; record includes a device sequence number
/// - bit 4 (0x10): NAT       — endpoint is behind NAT; record includes the agent endpoint address
/// - bit 5 (0x08): LOAD      — record carries 1-minute load average (f32)
/// - bit 6 (0x04): reserved
/// - bit 7 (0x02): reserved
/// - bit 8 (0x01): SIGNED    — record carries a publisher key signature to prevent DNS poisoning
///
/// ## Field order
///
/// `flags` → `sequence` (if CLUSTERED) → `addr` → `agent addr` (if NAT) → `load` (if LOAD) → `signature` (if SIGNED)
///
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EndpointSignature {
    scheme: u16,
    signature: Vec<u8>,
}

#[derive(Debug, Clone)]
pub struct EndpointAddr {
    flags: u8,
    /// Certificate-chain sequence used when multiple hosts share a domain (CLUSTERED).
    /// None means no sequence number.
    sequence: Option<CertificateSequence>,
    /// 1-minute load average (present when LOAD flag is set)
    load: Option<f32>,
    signature: Option<EndpointSignature>,
    /// Primary address (the direct address, or the outer/public address for NAT)
    pub primary: SocketAddr,
    /// Agent address used for NAT traversal (present when NAT flag is set)
    pub agent: Option<SocketAddr>,
}

impl PartialEq for EndpointAddr {
    fn eq(&self, other: &Self) -> bool {
        self.flags == other.flags
            && self.sequence == other.sequence
            && self.load.map(f32::to_bits) == other.load.map(f32::to_bits)
            && self.signature == other.signature
            && self.primary == other.primary
            && self.agent == other.agent
    }
}

impl Eq for EndpointAddr {}

impl Hash for EndpointAddr {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.flags.hash(state);
        self.sequence.hash(state);
        self.load.map(f32::to_bits).hash(state);
        self.signature.hash(state);
        self.primary.hash(state);
        self.agent.hash(state);
    }
}

impl EndpointAddr {
    const FLAG_FAMILY: u8 = 0b1000_0000; // bit 1: 0=IPv4, 1=IPv6
    const FLAG_MAIN: u8 = 0b0100_0000; // bit 2: 1=primary, 0=backup
    const FLAG_CLUSTERED: u8 = 0b0010_0000; // bit 3: multiple hosts share this domain
    const FLAG_NAT: u8 = 0b0001_0000; // bit 4: endpoint is behind NAT, agent address present
    const FLAG_LOAD: u8 = 0b0000_1000; // bit 5: record carries load average
    // bit 6 (0x04): reserved
    // bit 7 (0x02): reserved
    const FLAG_SIGNED: u8 = 0b0000_0001; // bit 8: record carries publisher signature

    pub fn direct_v4(addr: SocketAddrV4) -> Self {
        Self {
            flags: 0, // IPv4 direct: FAMILY=0, NAT=0
            sequence: None,
            load: None,
            signature: None,
            primary: addr.into(),
            agent: None,
        }
    }

    pub fn direct_v6(addr: SocketAddrV6) -> Self {
        Self {
            flags: Self::FLAG_FAMILY, // IPv6 direct: FAMILY=1, NAT=0
            sequence: None,
            load: None,
            signature: None,
            primary: addr.into(),
            agent: None,
        }
    }

    /// Create an IPv4 endpoint that requires NAT traversal.
    /// `outer` is the public address; `agent` is the NAT helper address.
    pub fn nat_v4(outer: SocketAddrV4, agent: SocketAddrV4) -> Self {
        Self {
            flags: Self::FLAG_NAT, // IPv4 NAT: FAMILY=0, NAT=1
            sequence: None,
            load: None,
            signature: None,
            primary: outer.into(),
            agent: Some(agent.into()),
        }
    }

    /// Create an IPv6 endpoint that requires NAT traversal.
    /// `outer` is the public address; `agent` is the NAT helper address.
    pub fn nat_v6(outer: SocketAddrV6, agent: SocketAddrV6) -> Self {
        Self {
            flags: Self::FLAG_FAMILY | Self::FLAG_NAT, // IPv6 NAT: FAMILY=1, NAT=1
            sequence: None,
            load: None,
            signature: None,
            primary: outer.into(),
            agent: Some(agent.into()),
        }
    }

    /// Returns true if the address family is IPv6.
    pub fn is_ipv6(&self) -> bool {
        self.flags & Self::FLAG_FAMILY != 0
    }

    /// Returns true if NAT traversal is required (agent address is present).
    pub fn is_nat(&self) -> bool {
        self.flags & Self::FLAG_NAT != 0
    }

    /// Returns true if this domain maps to a cluster of hosts (sequence number is present).
    pub fn is_clustered(&self) -> bool {
        self.flags & Self::FLAG_CLUSTERED != 0
    }

    /// Returns true if the record carries a load average value.
    pub fn is_load(&self) -> bool {
        self.flags & Self::FLAG_LOAD != 0
    }

    pub fn set_clustered(&mut self, clustered: bool) {
        if clustered {
            self.flags |= Self::FLAG_CLUSTERED;
        } else {
            self.flags &= !Self::FLAG_CLUSTERED;
            self.sequence = None; // clear sequence number
        }
    }

    pub fn set_load(&mut self, load: Option<f32>) {
        self.load = load;
        if self.load.is_some() {
            self.flags |= Self::FLAG_LOAD;
        } else {
            self.flags &= !Self::FLAG_LOAD;
        }
    }

    pub async fn sign_with_authority(
        &mut self,
        authority: &(impl dhttp_identity::identity::LocalAuthority + ?Sized),
    ) -> Result<(), SignEndpointError> {
        self.set_signed(true);
        let data = self.signed_data();

        let scheme = authority
            .cert_chain()
            .first()
            .and_then(|_| sigin::canonical_scheme_for_spki(authority.public_key()))
            .ok_or(SignEndpointError::NoSupportedScheme)?;
        let signature = authority
            .sign(&data)
            .await
            .context(sign_endpoint_error::SignSnafu)?;

        self.signature = Some(EndpointSignature {
            scheme: u16::from(scheme),
            signature,
        });
        Ok(())
    }

    pub fn verify_signature(
        &self,
        spki: SubjectPublicKeyInfoDer<'_>,
    ) -> Result<bool, sigin::VerifyError> {
        let Some(sig) = &self.signature else {
            return Ok(false);
        };
        let data = self.signed_data();
        sigin::verify(
            spki,
            SignatureScheme::from(sig.scheme),
            &data,
            &sig.signature,
        )
    }

    pub fn verify_signature_from_der(&self, cert_der: &[u8]) -> Result<bool, sigin::VerifyError> {
        let (_, cert) = x509_parser::parse_x509_certificate(cert_der).map_err(|e| {
            sigin::VerifyError::InvalidCertificate {
                details: e.to_string(),
            }
        })?;

        let spki = SubjectPublicKeyInfoDer::from(cert.tbs_certificate.subject_pki.raw);
        self.verify_signature(spki)
    }

    pub fn verify_signature_from_pem(&self, cert_pem: &[u8]) -> Result<bool, sigin::VerifyError> {
        let mut reader = std::io::Cursor::new(cert_pem);
        if let Some(item) = rustls_pemfile::certs(&mut reader).next() {
            let cert_der = item.map_err(|e| sigin::VerifyError::InvalidPem { source: e })?;
            return self.verify_signature_from_der(&cert_der);
        }
        Err(sigin::VerifyError::InvalidCertificate {
            details: "No certificate found in PEM".to_string(),
        })
    }

    pub fn verify_signature_from_base64(
        &self,
        cert_base64: &str,
    ) -> Result<bool, sigin::VerifyError> {
        let cert_base64 = cert_base64.trim();
        let cert_der = base64::engine::general_purpose::STANDARD
            .decode(cert_base64)
            .map_err(|e| sigin::VerifyError::InvalidBase64 { source: e })?;
        self.verify_signature_from_der(&cert_der)
    }

    pub fn verify_signature_from_file(
        &self,
        path: impl AsRef<Path>,
    ) -> Result<bool, sigin::VerifyError> {
        let contents = std::fs::read(path).map_err(|e| sigin::VerifyError::Io { source: e })?;
        // Try PEM first
        if let Ok(res) = self.verify_signature_from_pem(&contents) {
            return Ok(res);
        }
        // If PEM failed, try DER
        self.verify_signature_from_der(&contents)
    }

    pub fn is_main(&self) -> bool {
        self.flags() & Self::FLAG_MAIN == Self::FLAG_MAIN
    }

    pub fn set_main(&mut self, is_main: bool) {
        let flags = self.flags_mut();
        if is_main {
            *flags |= Self::FLAG_MAIN;
        } else {
            *flags &= !Self::FLAG_MAIN;
        }
    }

    pub fn is_signed(&self) -> bool {
        self.flags() & Self::FLAG_SIGNED == Self::FLAG_SIGNED
    }

    pub fn set_signed(&mut self, is_signed: bool) {
        let flags = self.flags_mut();
        if is_signed {
            *flags |= Self::FLAG_SIGNED;
        } else {
            *flags &= !Self::FLAG_SIGNED;
        }
    }

    pub fn encpding_size(&self) -> usize {
        let mut meta_len = 1; // flags

        // sequence is only encoded when CLUSTERED flag is set
        if let Some(seq) = &self.sequence {
            meta_len += VarInt::from_u32(seq.get()).encoding_size();
        }

        if self.load.is_some() {
            meta_len += 4; // f32
        }

        if self.is_signed()
            && let Some(sig) = &self.signature
        {
            let sig_len =
                VarInt::try_from(sig.signature.len() as u64).unwrap_or(VarInt::from_u32(0));
            meta_len += 2 + sig_len.encoding_size() + sig.signature.len();
        }

        let addr_len = match (self.is_ipv6(), self.is_nat()) {
            (false, false) => 2 + 4,      // IPv4 direct: port + ipv4
            (false, true) => (2 + 4) * 2, // IPv4 NAT: (port + ipv4) * 2
            (true, false) => 2 + 16,      // IPv6 direct: port + ipv6
            (true, true) => (2 + 16) * 2, // IPv6 NAT: (port + ipv6) * 2
        };

        meta_len + addr_len
    }

    pub fn addr(&self) -> SocketAddr {
        self.primary
    }

    pub fn agent_addr(&self) -> Option<SocketAddr> {
        self.agent
    }

    pub fn sequence(&self) -> Option<CertificateSequence> {
        self.sequence
    }

    pub fn normalized_sequence(&self) -> CertificateSequence {
        self.sequence
            .unwrap_or_else(|| CertificateSequence::from(0u8))
    }

    pub fn set_sequence(&mut self, sequence: CertificateSequence) {
        if sequence.get() > 0 {
            self.sequence = Some(sequence);
            self.set_clustered(true);
        } else {
            self.sequence = None;
            self.set_clustered(false);
        }
    }

    pub fn certificate_chain_key(&self) -> CertificateChainKey {
        if self.is_main() {
            crate::core::certificate::primary_chain_key(self.normalized_sequence())
        } else {
            crate::core::certificate::secondary_chain_key(self.normalized_sequence())
        }
    }

    pub fn load(&self) -> Option<f32> {
        self.load
    }

    fn flags(&self) -> u8 {
        self.flags
    }

    fn flags_mut(&mut self) -> &mut u8 {
        &mut self.flags
    }

    pub fn signature(&self) -> Option<&EndpointSignature> {
        self.signature.as_ref()
    }

    pub fn signature_base64(&self) -> Option<String> {
        self.signature
            .as_ref()
            .map(|sig| base64::engine::general_purpose::STANDARD.encode(&sig.signature))
    }

    fn write_base<B: BufMut>(&self, buf: &mut B) {
        buf.put_u8(self.flags);

        // Sequence is only written when CLUSTERED is set
        if let Some(seq) = &self.sequence {
            buf.put_varint(VarInt::from_u32(seq.get()));
        }

        // Write primary address
        match self.primary {
            SocketAddr::V4(addr) => buf.put_socket_addr_v4(&addr),
            SocketAddr::V6(addr) => buf.put_socket_addr_v6(&addr),
        }

        // Write agent address when NAT traversal is required
        if let Some(agent_addr) = &self.agent {
            match agent_addr {
                SocketAddr::V4(addr) => buf.put_socket_addr_v4(addr),
                SocketAddr::V6(addr) => buf.put_socket_addr_v6(addr),
            }
        }

        if let Some(load) = self.load {
            buf.put_u32(load.to_bits());
        }
    }

    fn signed_data(&self) -> Vec<u8> {
        let mut unsigned = self.clone();
        unsigned.set_signed(true);
        unsigned.signature = None;
        let mut buf = bytes::BytesMut::with_capacity(unsigned.encpding_size());
        unsigned.write_base(&mut buf);
        buf.to_vec()
    }
}

pub(crate) trait WriteEndpointAddr {
    fn put_endpoint_addr(&mut self, endpoint: &EndpointAddr);
}

impl<B: BufMut> WriteEndpointAddr for B {
    fn put_endpoint_addr(&mut self, endpoint: &EndpointAddr) {
        endpoint.write_base(self);
        if endpoint.is_signed()
            && let Some(sig) = endpoint.signature()
        {
            self.put_u16(sig.scheme);
            let len = VarInt::try_from(sig.signature.len() as u64).unwrap_or(VarInt::from_u32(0));
            self.put_varint(len);
            self.put_slice(&sig.signature);
        }
    }
}

pub fn be_endpoint_addr(input: &[u8]) -> nom::IResult<&[u8], EndpointAddr> {
    let (remain, flags) = be_u8(input)?;

    let is_clustered = flags & EndpointAddr::FLAG_CLUSTERED != 0;
    let is_ipv6 = flags & EndpointAddr::FLAG_FAMILY != 0;
    let is_nat = flags & EndpointAddr::FLAG_NAT != 0;
    let has_load = flags & EndpointAddr::FLAG_LOAD != 0;

    // Sequence number is only present when CLUSTERED is set
    let (remain, sequence) = if is_clustered {
        let (remain, seq) = be_varint(remain)?;
        let sequence = match CertificateSequence::try_from(seq.into_inner()) {
            Ok(sequence) => sequence,
            Err(_error) => {
                return Err(nom::Err::Failure(make_error(remain, ErrorKind::TooLarge)));
            }
        };
        (remain, Some(sequence))
    } else {
        (remain, None)
    };

    let (remain, primary) = if is_ipv6 {
        let (remain, addr) = be_socket_addr_v6(remain)?;
        (remain, SocketAddr::V6(addr))
    } else {
        let (remain, addr) = be_socket_addr_v4(remain)?;
        (remain, SocketAddr::V4(addr))
    };

    let (remain, agent) = if is_nat {
        let agent_addr = if is_ipv6 {
            let (remain, addr) = be_socket_addr_v6(remain)?;
            (remain, SocketAddr::V6(addr))
        } else {
            let (remain, addr) = be_socket_addr_v4(remain)?;
            (remain, SocketAddr::V4(addr))
        };
        let (remain, addr) = agent_addr;
        (remain, Some(addr))
    } else {
        (remain, None)
    };

    let (remain, load) = if has_load {
        let (remain, load) = be_u32(remain)?;
        (remain, Some(f32::from_bits(load)))
    } else {
        (remain, None)
    };

    let (remain, signature) = be_endpoint_signature(remain, flags)?;

    Ok((
        remain,
        EndpointAddr {
            flags,
            sequence,
            load,
            signature,
            primary,
            agent,
        },
    ))
}

/// Parse an EndpointAddr with backward compatibility.
///
/// - If `rdlen` matches a legacy fixed length, parse as address-only and fill in default flags.
/// - Otherwise parse as the modern format (with `flags` + optional fields).
///
/// Note: Legacy and modern records differ in their leading bytes.
/// Disambiguation is done via `RDLENGTH` to avoid misinterpreting the port's high byte as `flags`.
pub(crate) fn be_endpoint_addr_compat(
    input: &[u8],
    rdlen: u16,
) -> nom::IResult<&[u8], EndpointAddr> {
    // Check for legacy fixed lengths
    let legacy_lengths = [
        6,  // IPv4 direct: port(2) + ip(4)
        12, // IPv4 NAT:    (port(2) + ip(4)) * 2
        18, // IPv6 direct: port(2) + ip(16)
        36, // IPv6 NAT:    (port(2) + ip(16)) * 2
    ];

    if legacy_lengths.contains(&(rdlen as usize)) {
        // Modern records have variable length, so a valid modern encoding can
        // legitimately be 12, 18, or 36 bytes as well. Prefer a complete
        // modern parse and only fall back to the legacy layout when it fails.
        if let Ok((remaining, endpoint)) = be_endpoint_addr(input)
            && remaining.is_empty()
        {
            return Ok((remaining, endpoint));
        }
        return be_legacy_endpoint_addr_by_length(input, rdlen);
    }

    // Modern format
    be_endpoint_addr(input)
}

/// Parse a legacy EndpointAddr record, identified by its fixed RDLENGTH.
fn be_legacy_endpoint_addr_by_length(
    input: &[u8],
    rdlen: u16,
) -> nom::IResult<&[u8], EndpointAddr> {
    match rdlen {
        6 => {
            // IPv4 direct
            let (remain, addr) = be_socket_addr_v4(input)?;
            Ok((
                remain,
                EndpointAddr {
                    flags: 0,
                    sequence: None,
                    load: None,
                    signature: None,
                    primary: addr.into(),
                    agent: None,
                },
            ))
        }
        12 => {
            // IPv4 NAT
            let (remain, primary) = be_socket_addr_v4(input)?;
            let (remain, agent) = be_socket_addr_v4(remain)?;
            Ok((
                remain,
                EndpointAddr {
                    flags: EndpointAddr::FLAG_NAT,
                    sequence: None,
                    load: None,
                    signature: None,
                    primary: primary.into(),
                    agent: Some(agent.into()),
                },
            ))
        }
        18 => {
            // IPv6 direct
            let (remain, addr) = be_socket_addr_v6(input)?;
            Ok((
                remain,
                EndpointAddr {
                    flags: EndpointAddr::FLAG_FAMILY,
                    sequence: None,
                    load: None,
                    signature: None,
                    primary: addr.into(),
                    agent: None,
                },
            ))
        }
        36 => {
            // IPv6 NAT
            let (remain, primary) = be_socket_addr_v6(input)?;
            let (remain, agent) = be_socket_addr_v6(remain)?;
            Ok((
                remain,
                EndpointAddr {
                    flags: EndpointAddr::FLAG_FAMILY | EndpointAddr::FLAG_NAT,
                    sequence: None,
                    load: None,
                    signature: None,
                    primary: primary.into(),
                    agent: Some(agent.into()),
                },
            ))
        }
        _ => Err(nom::Err::Error(nom::error::make_error(
            input,
            nom::error::ErrorKind::LengthValue,
        ))),
    }
}

fn be_endpoint_signature(input: &[u8], flags: u8) -> IResult<&[u8], Option<EndpointSignature>> {
    if (flags & EndpointAddr::FLAG_SIGNED) != EndpointAddr::FLAG_SIGNED {
        if !input.is_empty() {
            return Err(nom::Err::Error(make_error(input, ErrorKind::Eof)));
        }
        return Ok((input, None));
    }

    let (remain, scheme_u16) = be_u16(input)?;
    let (remain, sig_len) = be_varint(remain)?;
    let sig_len = usize::try_from(sig_len.into_inner())
        .map_err(|_| nom::Err::Error(make_error(remain, ErrorKind::TooLarge)))?;
    let (remain, sig) = take(sig_len)(remain)?;
    Ok((
        remain,
        Some(EndpointSignature {
            scheme: scheme_u16,
            signature: sig.to_vec(),
        }),
    ))
}

pub trait WriteSocketAddr {
    fn put_socket_addr_v4(&mut self, addr: &SocketAddrV4);

    fn put_socket_addr_v6(&mut self, addr: &SocketAddrV6);

    fn put_socket_addr(&mut self, addr: &SocketAddr) {
        match addr {
            SocketAddr::V4(v4) => self.put_socket_addr_v4(v4),
            SocketAddr::V6(v6) => self.put_socket_addr_v6(v6),
        }
    }
}

impl<T: BufMut> WriteSocketAddr for T {
    fn put_socket_addr_v4(&mut self, addr: &SocketAddrV4) {
        self.put_u16(addr.port());
        self.put_u32(u32::from(*addr.ip()));
    }

    fn put_socket_addr_v6(&mut self, addr: &SocketAddrV6) {
        self.put_u16(addr.port());
        self.put_u128(u128::from(*addr.ip()));
    }
}

pub fn be_socket_addr_v4(input: &[u8]) -> IResult<&[u8], SocketAddrV4> {
    flat_map(be_u16, |port| {
        map(be_ipv4_addr, move |ip| SocketAddrV4::new(ip, port))
    })
    .parse(input)
}

pub fn be_socket_addr_v6(input: &[u8]) -> IResult<&[u8], SocketAddrV6> {
    flat_map(be_u16, |port| {
        map(be_ipv6_addr, move |ip| SocketAddrV6::new(ip, port, 0, 0))
    })
    .parse(input)
}

pub fn be_ipv4_addr(input: &[u8]) -> IResult<&[u8], Ipv4Addr> {
    map(be_u32, Ipv4Addr::from).parse(input)
}

pub fn be_ipv6_addr(input: &[u8]) -> IResult<&[u8], Ipv6Addr> {
    map(be_u128, Ipv6Addr::from).parse(input)
}

pub fn be_ip_addr(is_v6: bool) -> impl Fn(&[u8]) -> IResult<&[u8], IpAddr> {
    move |input| match is_v6 {
        true => map(be_u128, |ip| IpAddr::V6(Ipv6Addr::from(ip))).parse(input),
        false => map(be_u32, |ip| IpAddr::V4(Ipv4Addr::from(ip))).parse(input),
    }
}

impl Display for EndpointAddr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(agent_addr) = &self.agent {
            write!(f, "{}-{agent_addr}", self.primary)
        } else {
            write!(f, "{}", self.primary)
        }
    }
}

impl TryFrom<DquicEndpointAddr> for EndpointAddr {
    type Error = ();

    fn try_from(value: DquicEndpointAddr) -> Result<Self, Self::Error> {
        match value {
            DquicEndpointAddr::Direct {
                addr: SocketAddr::V4(addr),
            } => Ok(Self::direct_v4(addr)),
            DquicEndpointAddr::Direct {
                addr: SocketAddr::V6(addr),
            } => Ok(Self::direct_v6(addr)),
            DquicEndpointAddr::Mediate {
                agent: SocketAddr::V4(agent),
                outer: SocketAddr::V4(outer),
            } => Ok(Self::nat_v4(outer, agent)),
            DquicEndpointAddr::Mediate {
                agent: SocketAddr::V6(agent),
                outer: SocketAddr::V6(outer),
            } => Ok(Self::nat_v6(outer, agent)),
            _ => Err(()),
        }
    }
}

impl TryFrom<EndpointAddr> for DquicEndpointAddr {
    type Error = ();

    fn try_from(value: EndpointAddr) -> Result<Self, Self::Error> {
        if let Some(agent_addr) = value.agent {
            match (value.primary, agent_addr) {
                (SocketAddr::V4(outer), SocketAddr::V4(agent)) => Ok(DquicEndpointAddr::Mediate {
                    outer: SocketAddr::V4(outer),
                    agent: SocketAddr::V4(agent),
                }),
                (SocketAddr::V6(outer), SocketAddr::V6(agent)) => Ok(DquicEndpointAddr::Mediate {
                    outer: SocketAddr::V6(outer),
                    agent: SocketAddr::V6(agent),
                }),
                _ => Err(()),
            }
        } else {
            match value.primary {
                SocketAddr::V4(addr) => Ok(DquicEndpointAddr::Direct {
                    addr: SocketAddr::V4(addr),
                }),
                SocketAddr::V6(addr) => Ok(DquicEndpointAddr::Direct {
                    addr: SocketAddr::V6(addr),
                }),
            }
        }
    }
}

pub async fn sign_endponit_address(
    server_id: u8,
    authority: Option<&(impl dhttp_identity::identity::LocalAuthority + ?Sized)>,
    endpoint: DquicEndpointAddr,
) -> Option<EndpointAddr> {
    let mut ep: EndpointAddr = endpoint.try_into().ok()?;
    ep.set_main(server_id == 0);
    ep.set_sequence(CertificateSequence::from(server_id));
    if let Some(authority) = authority {
        let _ = ep.sign_with_authority(authority).await;
    }
    Some(ep)
}

#[cfg(test)]
mod tests {
    use std::{
        net::{Ipv4Addr, Ipv6Addr},
        sync::Arc,
    };

    use bytes::BytesMut;
    use futures::future::BoxFuture;
    use ring::signature::KeyPair;
    use rustls::sign::{Signer, SigningKey};

    use super::*;

    fn v4_outer() -> SocketAddrV4 {
        SocketAddrV4::new(Ipv4Addr::new(203, 0, 113, 10), 4433)
    }

    #[test]
    fn endpoint_certificate_chain_key_normalizes_missing_sequence() {
        let mut endpoint = EndpointAddr::direct_v4(v4_outer());
        endpoint.set_main(true);

        let key = endpoint.certificate_chain_key();

        assert_eq!(key.usage().kind_flag(), "0");
        assert_eq!(key.sequence().get(), 0);
    }

    #[test]
    fn endpoint_certificate_chain_key_uses_present_sequence() {
        let mut endpoint = EndpointAddr::direct_v4(v4_outer());
        endpoint.set_main(false);
        endpoint.set_sequence(
            dhttp_identity::certificate::CertificateSequence::try_from(7u32).unwrap(),
        );

        let key = endpoint.certificate_chain_key();

        assert_eq!(key.usage().kind_flag(), "1");
        assert_eq!(key.sequence().get(), 7);
    }

    #[test]
    fn endpoint_parser_rejects_over_range_certificate_sequence() {
        let sequence = crate::core::parser::varint::VarInt::from_u64(
            dhttp_identity::certificate::CertificateSequence::MAX as u64 + 1,
        )
        .unwrap();
        let mut packet = BytesMut::new();
        packet.put_u8(EndpointAddr::FLAG_MAIN | EndpointAddr::FLAG_CLUSTERED);
        packet.put_varint(sequence);
        packet.put_u16(v4_outer().port());
        packet.put_slice(&v4_outer().ip().octets());

        assert!(be_endpoint_addr(&packet).is_err());
    }

    #[test]
    fn legacy_endpoint_v4_direct_without_meta() {
        let port = 5353u16;
        let ip = Ipv4Addr::new(10, 0, 0, 1);
        let mut buf = BytesMut::new();
        buf.extend_from_slice(&port.to_be_bytes());
        buf.extend_from_slice(&u32::from(ip).to_be_bytes());
        let (remain, decoded) = be_endpoint_addr_compat(&buf, 6).unwrap();
        assert!(remain.is_empty());
        assert_eq!(
            decoded,
            EndpointAddr::direct_v4(SocketAddrV4::new(ip, port))
        );
    }

    #[test]
    fn legacy_endpoint_v4_nat_without_meta() {
        let outer = SocketAddrV4::new(Ipv4Addr::new(10, 0, 0, 1), 1000);
        let agent = SocketAddrV4::new(Ipv4Addr::new(10, 0, 0, 2), 2000);
        let mut buf = BytesMut::new();
        buf.extend_from_slice(&outer.port().to_be_bytes());
        buf.extend_from_slice(&u32::from(*outer.ip()).to_be_bytes());
        buf.extend_from_slice(&agent.port().to_be_bytes());
        buf.extend_from_slice(&u32::from(*agent.ip()).to_be_bytes());
        let (remain, decoded) = be_endpoint_addr_compat(&buf, 12).unwrap();
        assert!(remain.is_empty());
        assert_eq!(decoded, EndpointAddr::nat_v4(outer, agent));
    }

    #[test]
    fn flag_bit_ops_work() {
        let addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 5353);
        let mut ep = EndpointAddr {
            flags: 0b0011_0000,
            sequence: None,
            load: None,
            signature: None,
            primary: addr.into(),
            agent: None,
        };

        assert!(!ep.is_main());
        assert!(!ep.is_signed());

        ep.set_main(true);
        assert!(ep.is_main());
        assert_eq!(ep.flags, 0b0111_0000);

        ep.set_signed(true);
        assert!(ep.is_signed());
        assert_eq!(ep.flags, 0b0111_0001);

        ep.set_main(false);
        assert!(!ep.is_main());
        assert!(ep.is_signed());
        assert_eq!(ep.flags, 0b0011_0001);

        ep.set_signed(false);
        assert!(!ep.is_signed());
        assert_eq!(ep.flags, 0b0011_0000);
    }

    #[test]
    fn varint_roundtrip_and_len() {
        fn roundtrip(v: u64) {
            let v = VarInt::from_u64(v).unwrap();
            let mut buf = BytesMut::new();
            buf.put_varint(v);
            assert_eq!(buf.len(), v.encoding_size());
            let (remain, decoded) = be_varint(&buf).unwrap();
            assert!(remain.is_empty());
            assert_eq!(decoded, v);
        }

        for v in [
            0u64,
            1,
            63,
            64,
            16383,
            16384,
            (1 << 30) - 1,
            1 << 30,
            (1 << 62) - 1,
        ] {
            roundtrip(v);
        }
    }

    #[test]
    fn varint_rejects_overflow_and_incomplete() {
        assert!(VarInt::from_u64((1 << 62) + 1).is_err());

        let incomplete = [0b01_000000u8];
        match be_varint(&incomplete) {
            Err(nom::Err::Incomplete(_)) => {}
            other => panic!("expected Incomplete, got {other:?}"),
        }
    }

    #[test]
    fn endpoint_encode_decode_roundtrip() {
        let v4_outer = SocketAddrV4::new(Ipv4Addr::new(10, 0, 0, 1), 1000);
        let v4_agent = SocketAddrV4::new(Ipv4Addr::new(10, 0, 0, 2), 2000);
        let v6_outer = SocketAddrV6::new(Ipv6Addr::LOCALHOST, 3000, 0, 0);
        let v6_agent = SocketAddrV6::new(Ipv6Addr::LOCALHOST, 4000, 0, 0);

        let mut with_load = EndpointAddr::direct_v4(v4_outer);
        with_load.set_load(Some(0.42_f32));

        let cases = vec![
            // IPv4 direct, MAIN + CLUSTERED flags
            EndpointAddr {
                flags: EndpointAddr::FLAG_MAIN | EndpointAddr::FLAG_CLUSTERED,
                sequence: Some(CertificateSequence::from(0u8)),
                load: None,
                signature: None,
                primary: v4_outer.into(),
                agent: None,
            },
            // IPv4 NAT, CLUSTERED flag
            EndpointAddr {
                flags: EndpointAddr::FLAG_NAT | EndpointAddr::FLAG_CLUSTERED,
                sequence: Some(CertificateSequence::try_from(127u32).unwrap()),
                load: None,
                signature: None,
                primary: v4_outer.into(),
                agent: Some(v4_agent.into()),
            },
            // IPv6 direct, MAIN + CLUSTERED flags
            EndpointAddr {
                flags: EndpointAddr::FLAG_FAMILY
                    | EndpointAddr::FLAG_MAIN
                    | EndpointAddr::FLAG_CLUSTERED,
                sequence: Some(CertificateSequence::try_from(128u32).unwrap()),
                load: None,
                signature: None,
                primary: v6_outer.into(),
                agent: None,
            },
            // IPv6 NAT, CLUSTERED flag
            EndpointAddr {
                flags: EndpointAddr::FLAG_FAMILY
                    | EndpointAddr::FLAG_NAT
                    | EndpointAddr::FLAG_CLUSTERED,
                sequence: Some(CertificateSequence::try_from(16_384u32).unwrap()),
                load: None,
                signature: None,
                primary: v6_outer.into(),
                agent: Some(v6_agent.into()),
            },
            // IPv4 direct with LOAD
            with_load,
        ];

        for ep in cases {
            let mut buf = BytesMut::new();
            buf.put_endpoint_addr(&ep);
            assert_eq!(buf.len(), ep.encpding_size());

            let (remain, decoded) = be_endpoint_addr(&buf).unwrap();
            assert!(remain.is_empty());
            assert_eq!(decoded, ep);
        }
    }

    #[test]
    fn compat_parser_does_not_misclassify_modern_lengths_as_legacy() {
        let mut direct = EndpointAddr::direct_v4("203.0.113.10:4433".parse().unwrap());
        direct.set_main(true);
        direct.set_sequence(CertificateSequence::try_from(10u32).unwrap());
        direct.set_load(Some(1.0));

        let mut nat = EndpointAddr::nat_v4(
            "198.51.100.10:4433".parse().unwrap(),
            "192.0.2.10:4433".parse().unwrap(),
        );
        nat.set_main(true);
        nat.set_sequence(CertificateSequence::from(1u8));
        nat.set_load(Some(2.0));

        for endpoint in [direct, nat] {
            let mut buf = BytesMut::new();
            buf.put_endpoint_addr(&endpoint);
            assert!([12, 18].contains(&buf.len()));

            let (remaining, decoded) =
                be_endpoint_addr_compat(&buf, u16::try_from(buf.len()).unwrap()).unwrap();
            assert!(remaining.is_empty());
            assert_eq!(decoded, endpoint);
        }
    }

    #[test]
    fn endpoint_signature_roundtrip_and_verify() {
        #[derive(Debug)]
        struct Ed25519Key {
            keypair: Arc<ring::signature::Ed25519KeyPair>,
            cert_chain: Vec<rustls::pki_types::CertificateDer<'static>>,
        }

        #[derive(Debug)]
        struct Ed25519Signer(Arc<ring::signature::Ed25519KeyPair>);

        impl Signer for Ed25519Signer {
            fn sign(&self, message: &[u8]) -> Result<Vec<u8>, rustls::Error> {
                Ok(self.0.sign(message).as_ref().to_vec())
            }

            fn scheme(&self) -> SignatureScheme {
                SignatureScheme::ED25519
            }
        }

        impl SigningKey for Ed25519Key {
            fn choose_scheme(&self, offered: &[SignatureScheme]) -> Option<Box<dyn Signer>> {
                offered
                    .contains(&SignatureScheme::ED25519)
                    .then(|| Box::new(Ed25519Signer(self.keypair.clone())) as Box<dyn Signer>)
            }

            fn algorithm(&self) -> rustls::SignatureAlgorithm {
                rustls::SignatureAlgorithm::ED25519
            }
        }

        impl dhttp_identity::identity::LocalAuthority for Ed25519Key {
            fn name(&self) -> &str {
                "authority.example"
            }

            fn cert_chain(&self) -> &[rustls::pki_types::CertificateDer<'static>] {
                &self.cert_chain
            }

            fn sign(
                &self,
                data: &[u8],
            ) -> BoxFuture<'_, Result<Vec<u8>, dhttp_identity::identity::SignError>> {
                let result = dhttp_identity::identity::sign_with_key(self, data);
                Box::pin(std::future::ready(result))
            }
        }

        let rng = ring::rand::SystemRandom::new();
        let pkcs8 = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
        let keypair =
            Arc::new(ring::signature::Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap());
        let mut spki = Vec::with_capacity(44);
        spki.extend_from_slice(&[
            0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
        ]);
        spki.extend_from_slice(keypair.public_key().as_ref());
        let key = Ed25519Key {
            keypair: keypair.clone(),
            cert_chain: vec![rustls::pki_types::CertificateDer::from(spki.clone())],
        };

        let addr = SocketAddrV4::new(Ipv4Addr::new(10, 0, 0, 1), 5353);
        let mut ep = EndpointAddr::direct_v4(addr);
        ep.set_main(true);
        futures::executor::block_on(ep.sign_with_authority(&key)).unwrap();

        let mut buf = BytesMut::new();
        buf.put_endpoint_addr(&ep);
        assert_eq!(buf.len(), ep.encpding_size());

        let (remain, decoded) = be_endpoint_addr(&buf).unwrap();
        assert!(remain.is_empty());
        assert!(decoded.is_signed());
        assert!(decoded.signature().is_some());
        assert!(
            decoded
                .verify_signature(SubjectPublicKeyInfoDer::from(spki.as_slice()))
                .unwrap()
        );

        let mut tampered = decoded.clone();
        tampered.set_main(false);
        assert!(
            !tampered
                .verify_signature(SubjectPublicKeyInfoDer::from(spki.as_slice()))
                .unwrap()
        );
    }

    #[test]
    fn sign_with_authority_uses_canonical_scheme_from_public_key() {
        #[derive(Debug)]
        struct Ed25519Authority {
            cert_chain: Vec<rustls::pki_types::CertificateDer<'static>>,
        }

        impl dhttp_identity::identity::LocalAuthority for Ed25519Authority {
            fn name(&self) -> &str {
                "authority.example"
            }

            fn cert_chain(&self) -> &[rustls::pki_types::CertificateDer<'static>] {
                &self.cert_chain
            }

            fn sign(
                &self,
                _data: &[u8],
            ) -> BoxFuture<'_, Result<Vec<u8>, dhttp_identity::identity::SignError>> {
                Box::pin(async move { Ok(vec![1, 2, 3]) })
            }
        }

        let cert_chain = vec![rustls::pki_types::CertificateDer::from(vec![
            0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        ])];
        let mut ep = EndpointAddr::direct_v4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 5353));
        futures::executor::block_on(ep.sign_with_authority(&Ed25519Authority { cert_chain }))
            .unwrap();

        let signature = ep.signature().unwrap();
        assert_eq!(
            SignatureScheme::from(signature.scheme),
            SignatureScheme::ED25519
        );
        assert_eq!(signature.signature, vec![1, 2, 3]);
    }

    #[test]
    fn optional_fields_flags_follow_values() {
        let addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 5353);
        let mut ep = EndpointAddr::direct_v4(addr);

        assert!(!ep.is_load());

        ep.set_load(Some(0.5_f32));

        assert!(ep.is_load());
        assert_eq!(ep.load(), Some(0.5_f32));

        ep.set_load(None);

        assert!(!ep.is_load());
        assert_eq!(ep.load(), None);
    }
}