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
// Rust language amplification library providing multiple generic trait
// implementations, type wrappers, derive macros and other language enhancements
//
// Written in 2019-2020 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//     Martin Habovstiak <martin.habovstiak@gmail.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

//! Universal addresses that support IPv4, IPv6 and Tor

// TODO: Move all uniform encodings into a trait

use std::convert::TryFrom;
use std::fmt;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::str::FromStr;
#[cfg(feature = "tor")]
use torut::onion::{OnionAddressV2, OnionAddressV3, TorPublicKeyV3, TORV3_PUBLIC_KEY_LENGTH};

/// A universal address covering IPv4, IPv6 and Tor in a single byte sequence
/// of 32 bytes.
///
/// Holds either:
/// * IPv4-to-IPv6 address
/// * IPv6 address
/// * Tor address (V2 and V3)
///
/// NB: we are using `TorPublicKeyV3` instead of `OnionAddressV3`, since
/// `OnionAddressV3` keeps checksum and other information which can be
/// reconstructed from `TorPublicKeyV3`. The 2-byte checksum in `OnionAddressV3`
/// is designed for human-readable part that checks that the address was typed
/// in correctly. In computer-stored digital data it may be deterministically
/// regenerated and does not add any additional security.
///
/// For Version2 Tor support only `OnionAddressV2` handling is supported.
/// `OnionAddressV2` can only be constructed from an address string.
///
/// Tor addresses are distinguished by the fact that last 16 bits
/// must be set to 0
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(
    all(feature = "serde", feature = "serde_str_helpers"),
    derive(Serialize, Deserialize),
    serde(
        try_from = "serde_str_helpers::DeserBorrowStr",
        into = "String",
        crate = "serde_crate"
    )
)]
#[cfg_attr(
    all(feature = "serde", not(feature = "serde_str_helpers")),
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate")
)]
pub enum InetAddr {
    /// IP address of V4 standard
    IPv4(Ipv4Addr),

    /// IP address of V6 standard
    IPv6(Ipv6Addr),

    /// Tor address of V2 standard
    #[cfg(feature = "tor")]
    Tor(TorPublicKeyV3),

    /// Tor address of V3 standard
    #[cfg(feature = "tor")]
    TorV2(OnionAddressV2),
}

impl InetAddr {
    /// Length of the encoded address; equal to the maximal length of encoding
    /// for different address types
    #[cfg(feature = "tor")]
    pub const UNIFORM_ADDR_LEN: usize = TORV3_PUBLIC_KEY_LENGTH + 1; //32usize

    const IPV4_TAG: u8 = 0;
    const IPV6_TAG: u8 = 1;
    #[cfg(feature = "tor")]
    const TORV2_TAG: u8 = 2;
    #[cfg(feature = "tor")]
    const TORV3_TAG: u8 = 3;

    /// Length of the encoded address; equal to the maximal length of encoding
    /// for different address types
    #[cfg(not(feature = "tor"))]
    pub const UNIFORM_ADDR_LEN: usize = 33;
    #[inline]

    /// Returns an IPv6 address, constructed from IPv4 data; or, if Onion
    /// address is used, [`Option::None`]
    pub fn to_ipv6(&self) -> Option<Ipv6Addr> {
        match self {
            InetAddr::IPv4(ipv4_addr) => Some(ipv4_addr.to_ipv6_mapped()),
            InetAddr::IPv6(ipv6_addr) => Some(*ipv6_addr),
            #[cfg(feature = "tor")]
            _ => None,
        }
    }

    /// Returns an IPv4 address, if any, or [`Option::None`]
    pub fn to_ipv4(&self) -> Option<Ipv6Addr> {
        match self {
            InetAddr::IPv4(ipv4_addr) => Some(ipv4_addr.to_ipv6_mapped()),
            InetAddr::IPv6(ipv6_addr) => Some(*ipv6_addr),
            #[cfg(feature = "tor")]
            _ => None,
        }
    }

    /// Determines whether provided address is a Tor address. Always returns
    /// `fales` (the library is built without `tor` feature; use it to
    /// enable Tor addresses).
    #[cfg(not(feature = "tor"))]
    #[inline]
    pub fn is_tor(&self) -> bool {
        false
    }

    /// Always returns [`Option::None`] (the library is built without `tor`
    /// feature; use it to enable Tor addresses).
    #[cfg(not(feature = "tor"))]
    #[inline]
    pub fn to_onion_v2(&self) -> Option<()> {
        None
    }

    /// Always returns [`Option::None`] (the library is built without `tor`
    /// feature; use it to enable Tor addresses).
    #[cfg(not(feature = "tor"))]
    #[inline]
    pub fn to_onion(&self) -> Option<()> {
        None
    }

    /// Determines whether provided address is a Tor address
    #[cfg(feature = "tor")]
    #[inline]
    pub fn is_tor(&self) -> bool {
        match self {
            InetAddr::Tor(_) => true,
            InetAddr::TorV2(_) => true,
            _ => false,
        }
    }

    /// Returns Onion v2 address, if any, or [`Option::None`]
    #[cfg(feature = "tor")]
    #[inline]
    pub fn to_onion_v2(&self) -> Option<OnionAddressV2> {
        match self {
            InetAddr::IPv4(_) | InetAddr::IPv6(_) | InetAddr::Tor(_) => None,
            InetAddr::TorV2(onion) => Some(*onion),
        }
    }

    /// Returns Onion v3 address, if any, or [`Option::None`]
    #[cfg(feature = "tor")]
    #[inline]
    pub fn to_onion(&self) -> Option<OnionAddressV3> {
        match self {
            InetAddr::IPv4(_) | InetAddr::IPv6(_) | InetAddr::TorV2(_) => None,
            InetAddr::Tor(key) => Some(OnionAddressV3::from(key)),
        }
    }

    /// Decodes byte array containing uniform encoding of some internet address,
    /// constructed with [`to_uniform_encoding()`]. If the address can't be
    /// recognized, returns [`Option::None`]
    pub fn from_uniform_encoding(data: &[u8]) -> Option<Self> {
        if data.len() != Self::UNIFORM_ADDR_LEN {
            None?
        }

        let mut slice = [0u8; Self::UNIFORM_ADDR_LEN];
        slice.clone_from_slice(data);

        match slice[0] {
            Self::IPV4_TAG => {
                let mut a = [0u8; 4];
                a.clone_from_slice(&slice[29..]);
                Some(InetAddr::IPv4(Ipv4Addr::from(a)))
            }
            Self::IPV6_TAG => {
                let mut a = [0u8; 16];
                a.clone_from_slice(&slice[17..]);
                Some(InetAddr::IPv6(Ipv6Addr::from(a)))
            }
            #[cfg(feature = "tor")]
            Self::TORV3_TAG => {
                let mut a = [0u8; TORV3_PUBLIC_KEY_LENGTH];
                a.clone_from_slice(&slice[1..]);
                TorPublicKeyV3::from_bytes(&a).map(InetAddr::Tor).ok()
            }
            _ => None,
        }
    }

    /// Encodes address into a uniform byte array for storage. Here, *uniform*
    /// means that it can contain any possible internet address and have some
    /// fixed length (equal to [`InetAddr::UNIFORM_ADDR_LEN`])
    pub fn to_uniform_encoding(&self) -> [u8; Self::UNIFORM_ADDR_LEN] {
        let mut buf = [0u8; Self::UNIFORM_ADDR_LEN];
        match self {
            InetAddr::IPv4(ipv4_addr) => {
                buf[0] = Self::IPV4_TAG;
                buf[29..].copy_from_slice(&ipv4_addr.octets())
            }
            InetAddr::IPv6(ipv6_addr) => {
                buf[0] = Self::IPV6_TAG;
                buf[17..].copy_from_slice(&ipv6_addr.octets())
            }
            #[cfg(feature = "tor")]
            InetAddr::Tor(tor_pubkey) => {
                buf[0] = Self::TORV3_TAG;
                buf[1..].copy_from_slice(&tor_pubkey.to_bytes())
            }
            #[cfg(feature = "tor")]
            InetAddr::TorV2(onion_addr) => {
                buf[0] = Self::TORV2_TAG;
                buf[23..].copy_from_slice(onion_addr.get_raw_bytes().as_ref())
            }
        }
        buf
    }
}

impl Default for InetAddr {
    #[inline]
    fn default() -> Self {
        InetAddr::IPv4(Ipv4Addr::from(0))
    }
}

impl fmt::Display for InetAddr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            InetAddr::IPv4(addr) => write!(f, "{}", addr),
            InetAddr::IPv6(addr) => write!(f, "{}", addr),
            #[cfg(feature = "tor")]
            InetAddr::Tor(addr) => write!(f, "{}", addr),
            #[cfg(feature = "tor")]
            InetAddr::TorV2(addr) => write!(f, "{}", addr),
        }
    }
}

#[cfg(feature = "tor")]
impl TryFrom<InetAddr> for IpAddr {
    type Error = String;
    #[inline]
    fn try_from(addr: InetAddr) -> Result<Self, Self::Error> {
        Ok(match addr {
            InetAddr::IPv4(addr) => IpAddr::V4(addr),
            InetAddr::IPv6(addr) => IpAddr::V6(addr),
            #[cfg(feature = "tor")]
            InetAddr::Tor(_) => Err(String::from("IpAddr can't be used to store Tor v3 address"))?,
            #[cfg(feature = "tor")]
            InetAddr::TorV2(_) => {
                Err(String::from("IpAddr can't be used to store Tor v2 address"))?
            }
        })
    }
}

#[cfg(not(feature = "tor"))]
impl From<InetAddr> for IpAddr {
    #[inline]
    fn from(addr: InetAddr) -> Self {
        match addr {
            InetAddr::IPv4(addr) => IpAddr::V4(addr),
            InetAddr::IPv6(addr) => IpAddr::V6(addr),
        }
    }
}

impl From<IpAddr> for InetAddr {
    #[inline]
    fn from(value: IpAddr) -> Self {
        match value {
            IpAddr::V4(v4) => InetAddr::from(v4),
            IpAddr::V6(v6) => InetAddr::from(v6),
        }
    }
}

impl From<Ipv4Addr> for InetAddr {
    #[inline]
    fn from(addr: Ipv4Addr) -> Self {
        InetAddr::IPv4(addr)
    }
}

impl From<Ipv6Addr> for InetAddr {
    #[inline]
    fn from(addr: Ipv6Addr) -> Self {
        InetAddr::IPv6(addr)
    }
}

#[cfg(feature = "tor")]
impl From<TorPublicKeyV3> for InetAddr {
    #[inline]
    fn from(value: TorPublicKeyV3) -> Self {
        InetAddr::Tor(value)
    }
}

#[cfg(feature = "tor")]
impl From<OnionAddressV3> for InetAddr {
    #[inline]
    fn from(addr: OnionAddressV3) -> Self {
        InetAddr::Tor(addr.get_public_key())
    }
}

#[cfg(feature = "tor")]
impl From<OnionAddressV2> for InetAddr {
    #[inline]
    fn from(addr: OnionAddressV2) -> Self {
        InetAddr::TorV2(addr)
    }
}

#[cfg(all(feature = "stringly_conversions", feature = "serde_str_helpers"))]
impl_try_from_stringly_standard!(InetAddr);
#[cfg(all(feature = "stringly_conversions", feature = "serde_str_helpers"))]
impl_into_stringly_standard!(InetAddr);

impl FromStr for InetAddr {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        #[cfg(feature = "tor")]
        match (
            IpAddr::from_str(s),
            OnionAddressV3::from_str(s),
            OnionAddressV2::from_str(s),
        ) {
            (Ok(_), Ok(_), _) | (Ok(_), _, Ok(_)) | (_, Ok(_), Ok(_)) => {
                Err(format!("Confusing result of parsing {}", s))
            }
            (Ok(ip_addr), _, _) => Ok(Self::from(ip_addr)),
            (_, Ok(onionv3), _) => Ok(Self::from(onionv3)),
            (_, _, Ok(onionv2)) => Ok(Self::from(onionv2)),
            _ => Err(String::from("Wrong onion address")),
        }

        #[cfg(not(feature = "tor"))]
        match IpAddr::from_str(s) {
            Ok(ip_addr) => Ok(InetAddr::from(ip_addr)),
            _ => Err(String::from(
                "Tor addresses are not supported; consider compiling with 'tor' feature",
            )),
        }
    }
}

impl TryFrom<Vec<u8>> for InetAddr {
    type Error = String;
    #[inline]
    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        InetAddr::try_from(&value[..])
    }
}

// Yes, I checked that onion addresses don't need to optimize ownership of input
// String.
#[cfg(feature = "parse_arg")]
impl parse_arg::ParseArgFromStr for InetAddr {
    fn describe_type<W: std::fmt::Write>(mut writer: W) -> std::fmt::Result {
        #[cfg(not(feature = "tor"))]
        {
            write!(writer, "IPv4 or IPv6 address")
        }
        #[cfg(feature = "tor")]
        {
            write!(writer, "IPv4, IPv6, or Tor (onion) address")
        }
    }
}

impl TryFrom<&[u8]> for InetAddr {
    type Error = String;
    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        match value.len() {
            4 => {
                let mut buf = [0u8; 4];
                buf.clone_from_slice(value);
                Ok(InetAddr::from(buf))
            }
            16 => {
                let mut buf = [0u8; 16];
                buf.clone_from_slice(value);
                Ok(InetAddr::from(buf))
            }
            #[cfg(feature = "tor")]
            32 => {
                let mut buf = [0u8; 32];
                buf.clone_from_slice(value);
                InetAddr::try_from(buf)
            }
            _ => Err(String::from(
                "Unsupported length of the byte string to read `InetAddr` from",
            )),
        }
    }
}

impl From<[u8; 4]> for InetAddr {
    #[inline]
    fn from(value: [u8; 4]) -> Self {
        InetAddr::from(Ipv4Addr::from(value))
    }
}

impl From<[u8; 16]> for InetAddr {
    #[inline]
    fn from(value: [u8; 16]) -> Self {
        InetAddr::from(Ipv6Addr::from(value))
    }
}

impl From<[u16; 8]> for InetAddr {
    #[inline]
    fn from(value: [u16; 8]) -> Self {
        InetAddr::from(Ipv6Addr::from(value))
    }
}

#[cfg(feature = "tor")]
impl TryFrom<[u8; TORV3_PUBLIC_KEY_LENGTH]> for InetAddr {
    type Error = String;
    #[inline]
    fn try_from(value: [u8; TORV3_PUBLIC_KEY_LENGTH]) -> Result<Self, Self::Error> {
        let mut buf = [3u8; Self::UNIFORM_ADDR_LEN];
        buf[1..].copy_from_slice(&value);
        Self::from_uniform_encoding(&buf).ok_or(s!("Wrong `InetAddr` binary encoding"))
    }
}

/// Transport protocols that may be part of `TransportAddr`
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate")
)]
#[non_exhaustive]
#[repr(u8)]
pub enum Transport {
    /// Normal TCP
    TCP = 1,

    /// Normal UDP
    UDP = 2,

    /// Multipath TCP version
    MTCP = 3,

    /// More efficient UDP version under developent by Google and consortium of
    /// other internet companies
    QUIC = 4,
    /* There are other rarely used protocols. Do not see any reason to add
     * them to the LNP/BP stack for now, but it may appear in the future,
     * so keeping them for referencing purposes: */
    /*
    UDPLite,
    SCTP,
    DCCP,
    RUDP,
    */
}

impl Transport {
    /// Decodes byte containing uniform encoding of some transport id,
    /// constructed with [`to_uniform_encoding()`]. If the address can't be
    /// recognized, returns [`Option::None`]
    #[inline]
    pub fn from_uniform_encoding(data: u8) -> Option<Self> {
        use Transport::*;
        Some(match data {
            a if a == TCP as u8 => TCP,
            a if a == UDP as u8 => UDP,
            a if a == MTCP as u8 => MTCP,
            a if a == QUIC as u8 => QUIC,
            _ => None?,
        })
    }

    /// Encodes transport as a single byte
    #[inline]
    pub fn to_uniform_encoding(&self) -> u8 {
        *self as u8
    }
}

impl Default for Transport {
    #[inline]
    fn default() -> Self {
        Transport::TCP
    }
}

impl FromStr for Transport {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s.to_lowercase().as_str() {
            "tcp" => Transport::TCP,
            "udp" => Transport::UDP,
            "mtcp" => Transport::MTCP,
            "quic" => Transport::QUIC,
            _ => Err(String::from("Unknown transport"))?,
        })
    }
}

impl fmt::Display for Transport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Transport::TCP => "tcp",
                Transport::UDP => "udp",
                Transport::MTCP => "mtcp",
                Transport::QUIC => "quic",
            }
        )
    }
}

/// Internet socket address, which consists of [`InetAddr`] IP or Tor address
/// and a port number (without protocol specification, i.e. TCP/UDP etc). If you
/// need to include transport-level protocol information into the socket
/// details, pls check [`InetSocketAddrExt`]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[cfg_attr(
    all(feature = "serde", feature = "serde_str_helpers"),
    derive(Serialize, Deserialize),
    serde(
        try_from = "serde_str_helpers::DeserBorrowStr",
        into = "String",
        crate = "serde_crate"
    )
)]
#[cfg_attr(
    all(feature = "serde", not(feature = "serde_str_helpers")),
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate")
)]
pub struct InetSocketAddr {
    /// Address part of the socket
    pub address: InetAddr,

    /// Port of the socket
    pub port: u16,
}

impl InetSocketAddr {
    /// Constructs new socket address from an internet address and a port
    /// information
    #[inline]
    pub fn new(address: InetAddr, port: u16) -> Self {
        Self { address, port }
    }

    /// Determines whether provided address is a Tor address
    #[inline]
    pub fn is_tor(&self) -> bool {
        self.address.is_tor()
    }
}

impl InetSocketAddr {
    /// Length of the encoded address; equal to the maximal length of encoding
    /// for different address types
    pub const UNIFORM_ADDR_LEN: usize = InetAddr::UNIFORM_ADDR_LEN + 2;

    /// Decodes byte array containing uniform encoding of some socket address,
    /// constructed with [`to_uniform_encoding()`]. If the address can't be
    /// recognized, returns [`Option::None`]
    #[inline]
    pub fn from_uniform_encoding(data: &[u8]) -> Option<Self> {
        if data.len() != Self::UNIFORM_ADDR_LEN {
            None?
        }

        Some(Self {
            address: {
                let mut buf = [0u8; InetAddr::UNIFORM_ADDR_LEN];
                buf.clone_from_slice(&data[..InetAddr::UNIFORM_ADDR_LEN]);
                InetAddr::from_uniform_encoding(&buf)?
            },
            port: {
                let mut buf = [0u8; 2];
                buf.clone_from_slice(&data[InetAddr::UNIFORM_ADDR_LEN..]);
                u16::from_be_bytes(buf)
            },
        })
    }

    /// Encodes address into a uniform byte array for storage. Here, *uniform*
    /// means that it can contain any possible internet address and have some
    /// fixed length (equal to [`InetSocketAddr::UNIFORM_ADDR_LEN`])
    #[inline]
    pub fn to_uniform_encoding(&self) -> [u8; Self::UNIFORM_ADDR_LEN] {
        let mut buf = [0u8; Self::UNIFORM_ADDR_LEN];
        buf[..InetAddr::UNIFORM_ADDR_LEN].copy_from_slice(&self.address.to_uniform_encoding());
        buf[InetAddr::UNIFORM_ADDR_LEN..].copy_from_slice(&self.port.to_be_bytes());
        buf
    }
}

impl fmt::Display for InetSocketAddr {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.address, self.port)
    }
}

impl FromStr for InetSocketAddr {
    type Err = String;

    #[allow(unreachable_code)]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Ok(socket_addr) = SocketAddrV6::from_str(s) {
            return Ok(Self::new((*socket_addr.ip()).into(), socket_addr.port()));
        } else if let Ok(socket_addr) = SocketAddrV4::from_str(s) {
            return Ok(Self::new((*socket_addr.ip()).into(), socket_addr.port()));
        } else {
            #[cfg(not(feature = "tor"))]
            return Err(format!(
                "Can't parse internet address {}. Tor addresses are not supported",
                s
            ));
        }

        let mut vals = s.split(':');
        let err_msg =
            String::from("Wrong format of socket address string; use <inet_address>[:<port>]");
        let em = |_| String::from(err_msg.clone());
        let emi = |_| String::from(err_msg.clone());
        match (vals.next(), vals.next(), vals.next()) {
            (Some(addr), Some(port), None) => Ok(Self {
                address: addr.parse().map_err(em)?,
                port: port.parse().map_err(emi)?,
            }),
            (Some(addr), None, _) => Ok(Self {
                address: addr.parse().map_err(em)?,
                port: 0,
            }),
            _ => Err(err_msg),
        }
    }
}

#[cfg(feature = "tor")]
impl TryFrom<InetSocketAddr> for SocketAddr {
    type Error = String;
    #[inline]
    fn try_from(socket_addr: InetSocketAddr) -> Result<Self, Self::Error> {
        Ok(Self::new(
            IpAddr::try_from(socket_addr.address)?,
            socket_addr.port,
        ))
    }
}

#[cfg(not(feature = "tor"))]
impl From<InetSocketAddr> for SocketAddr {
    #[inline]
    fn from(socket_addr: InetSocketAddr) -> Self {
        Self::new(IpAddr::from(socket_addr.address), socket_addr.port)
    }
}

impl From<SocketAddr> for InetSocketAddr {
    #[inline]
    fn from(addr: SocketAddr) -> Self {
        Self::new(addr.ip().into(), addr.port())
    }
}

impl From<SocketAddrV4> for InetSocketAddr {
    #[inline]
    fn from(addr: SocketAddrV4) -> Self {
        Self::new((*addr.ip()).into(), addr.port())
    }
}

impl From<SocketAddrV6> for InetSocketAddr {
    #[inline]
    fn from(addr: SocketAddrV6) -> Self {
        Self::new((*addr.ip()).into(), addr.port())
    }
}

/// Internet socket address of [`InetSocketAddr`] type, extended with a
/// transport-level protocol information (see [`Transport`])
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[cfg_attr(
    all(feature = "serde", feature = "serde_str_helpers"),
    derive(Serialize, Deserialize),
    serde(
        try_from = "serde_str_helpers::DeserBorrowStr",
        into = "String",
        crate = "serde_crate"
    )
)]
#[cfg_attr(
    all(feature = "serde", not(feature = "serde_str_helpers")),
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate")
)]
pub struct InetSocketAddrExt(
    /// Transport-level protocol details (like TCP, UDP etc)
    pub Transport,
    /// Details of the socket address, i.e internet address and port
    /// information
    pub InetSocketAddr,
);

impl InetSocketAddrExt {
    /// Length of the encoded address; equal to the maximal length of encoding
    /// for different address types
    pub const UNIFORM_ADDR_LEN: usize = InetSocketAddr::UNIFORM_ADDR_LEN + 1;

    /// Constructs [`InetSocketAddrExt`] for a given internet address and TCP
    /// port
    #[inline]
    pub fn tcp(address: InetAddr, port: u16) -> Self {
        Self(Transport::TCP, InetSocketAddr::new(address, port))
    }

    /// Constructs [`InetSocketAddrExt`] for a given internet address and UDP
    /// port
    #[inline]
    pub fn udp(address: InetAddr, port: u16) -> Self {
        Self(Transport::UDP, InetSocketAddr::new(address, port))
    }

    /// Decodes byte array containing uniform encoding of some socket address,
    /// constructed with [`to_uniform_encoding()`]. If the address can't be
    /// recognized, returns [`Option::None`]
    #[inline]
    pub fn from_uniform_encoding(data: &[u8]) -> Option<Self> {
        if data.len() != Self::UNIFORM_ADDR_LEN {
            None?
        }
        let mut buf = [0u8; InetSocketAddr::UNIFORM_ADDR_LEN];
        buf.copy_from_slice(&data[1..]);
        Some(Self(
            Transport::from_uniform_encoding(data[0])?,
            InetSocketAddr::from_uniform_encoding(&buf)?,
        ))
    }

    /// Encodes address into a uniform byte array for storage. Here, *uniform*
    /// means that it can contain any possible internet address and have some
    /// fixed length (equal to [`InetSocketAddrExt::UNIFORM_ADDR_LEN`])
    #[inline]
    pub fn to_uniform_encoding(&self) -> [u8; Self::UNIFORM_ADDR_LEN] {
        let mut buf = [0u8; Self::UNIFORM_ADDR_LEN];
        buf[..1].copy_from_slice(&[self.0.to_uniform_encoding()]);
        buf[1..].copy_from_slice(&self.1.to_uniform_encoding());
        buf
    }
}

impl fmt::Display for InetSocketAddrExt {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}://{}", self.0, self.1)
    }
}

impl FromStr for InetSocketAddrExt {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut vals = s.split("://");
        let err_msg = String::from("Wrong format of extended socket address string; use <transport>://<inet_address>[:<port>]");
        let em = |_| String::from(err_msg.clone());
        if let (Some(transport), Some(addr), None) = (vals.next(), vals.next(), vals.next()) {
            Ok(Self(
                transport.parse().map_err(em)?,
                addr.parse().map_err(em)?,
            ))
        } else {
            Err(err_msg)
        }
    }
}

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

    // TODO: Add tests for Tor

    #[test]
    fn test_inet_addr() {
        let ip4a = "127.0.0.1".parse().unwrap();
        let ip6a = "::1".parse().unwrap();

        let ip4 = InetAddr::IPv4(ip4a);
        let ip6 = InetAddr::IPv6(ip6a);
        assert_eq!(
            ip4.to_ipv6().unwrap(),
            Ipv6Addr::from_str("::ffff:127.0.0.1").unwrap()
        );
        assert_eq!(ip6.to_ipv6().unwrap(), ip6a);
        assert_eq!(InetAddr::from(IpAddr::V4(ip4a)), ip4);
        assert_eq!(InetAddr::from(IpAddr::V6(ip6a)), ip6);
        assert_eq!(InetAddr::from(ip4a), ip4);
        assert_eq!(InetAddr::from(ip6a), ip6);

        assert_eq!(InetAddr::default(), InetAddr::from_str("0.0.0.0").unwrap());

        #[cfg(feature = "tor")]
        assert_eq!(IpAddr::try_from(ip4.clone()).unwrap(), IpAddr::V4(ip4a));
        #[cfg(feature = "tor")]
        assert_eq!(IpAddr::try_from(ip6.clone()).unwrap(), IpAddr::V6(ip6a));

        #[cfg(not(feature = "tor"))]
        assert_eq!(IpAddr::from(ip4.clone()), IpAddr::V4(ip4a));
        #[cfg(not(feature = "tor"))]
        assert_eq!(IpAddr::from(ip6.clone()), IpAddr::V6(ip6a));

        assert_eq!(InetAddr::from_str("127.0.0.1").unwrap(), ip4);
        assert_eq!(InetAddr::from_str("::1").unwrap(), ip6);
        assert_eq!(format!("{}", ip4), "127.0.0.1");
        assert_eq!(format!("{}", ip6), "::1");

        assert!(!ip4.is_tor());
        assert!(!ip6.is_tor());

        let uenc4 = ip4.to_uniform_encoding();
        assert_eq!(InetAddr::from_uniform_encoding(&uenc4).unwrap(), ip4);
        let uenc6 = ip6.to_uniform_encoding();
        assert_ne!(uenc4.to_vec(), uenc6.to_vec());
        assert_eq!(InetAddr::from_uniform_encoding(&uenc6).unwrap(), ip6);
    }

    #[test]
    fn test_transport() {
        assert_eq!(format!("{}", Transport::TCP), "tcp");
        assert_eq!(format!("{}", Transport::UDP), "udp");
        assert_eq!(format!("{}", Transport::QUIC), "quic");
        assert_eq!(format!("{}", Transport::MTCP), "mtcp");

        assert_eq!(Transport::from_str("tcp").unwrap(), Transport::TCP);
        assert_eq!(Transport::from_str("Tcp").unwrap(), Transport::TCP);
        assert_eq!(Transport::from_str("TCP").unwrap(), Transport::TCP);
        assert_eq!(Transport::from_str("udp").unwrap(), Transport::UDP);
        assert_eq!(Transport::from_str("quic").unwrap(), Transport::QUIC);
        assert_eq!(Transport::from_str("mtcp").unwrap(), Transport::MTCP);
        assert!(Transport::from_str("xtp").is_err());
    }

    #[test]
    fn test_inet_socket_addr() {
        let ip4a = "127.0.0.1".parse().unwrap();
        let ip6a = "::1".parse().unwrap();
        let socket4a = "127.0.0.1:6865".parse().unwrap();
        let socket6a = "[::1]:6865".parse().unwrap();

        let ip4 = InetSocketAddr::new(ip4a, 6865);
        let ip6 = InetSocketAddr::new(ip6a, 6865);
        assert_eq!(InetSocketAddr::from(SocketAddr::V4(socket4a)), ip4);
        assert_eq!(InetSocketAddr::from(SocketAddr::V6(socket6a)), ip6);
        assert_eq!(InetSocketAddr::from(socket4a), ip4);
        assert_eq!(InetSocketAddr::from(socket6a), ip6);

        assert_eq!(
            InetSocketAddr::default(),
            InetSocketAddr::from_str("0.0.0.0:0").unwrap()
        );

        #[cfg(feature = "tor")]
        assert_eq!(
            SocketAddr::try_from(ip4.clone()).unwrap(),
            SocketAddr::V4(socket4a)
        );
        #[cfg(feature = "tor")]
        assert_eq!(
            SocketAddr::try_from(ip6.clone()).unwrap(),
            SocketAddr::V6(socket6a)
        );

        #[cfg(not(feature = "tor"))]
        assert_eq!(SocketAddr::from(ip4.clone()), SocketAddr::V4(socket4a));
        #[cfg(not(feature = "tor"))]
        assert_eq!(SocketAddr::from(ip6.clone()), SocketAddr::V6(socket6a));

        assert_eq!(InetSocketAddr::from_str("127.0.0.1:6865").unwrap(), ip4);
        assert_eq!(InetSocketAddr::from_str("[::1]:6865").unwrap(), ip6);
        assert_eq!(format!("{}", ip4), "127.0.0.1:6865");
        assert_eq!(format!("{}", ip6), "::1:6865");

        assert!(!ip4.is_tor());
        assert!(!ip6.is_tor());

        let uenc4 = ip4.to_uniform_encoding();
        assert_eq!(InetSocketAddr::from_uniform_encoding(&uenc4).unwrap(), ip4);
        let uenc6 = ip6.to_uniform_encoding();
        assert_ne!(uenc4.to_vec(), uenc6.to_vec());
        assert_eq!(InetSocketAddr::from_uniform_encoding(&uenc6).unwrap(), ip6);
    }

    #[test]
    fn test_inet_socket_addr_ext() {
        let ip4a = "127.0.0.1".parse().unwrap();
        let ip6a = "::1".parse().unwrap();

        let ip4 = InetSocketAddrExt::tcp(ip4a, 6865);
        let ip6 = InetSocketAddrExt::udp(ip6a, 6865);

        assert_eq!(
            InetSocketAddrExt::default(),
            InetSocketAddrExt::from_str("tcp://0.0.0.0:0").unwrap()
        );

        #[cfg(feature = "tor")]
        assert_eq!(
            InetSocketAddrExt::from_str("tcp://127.0.0.1:6865").unwrap(),
            ip4
        );
        #[cfg(feature = "tor")]
        assert_eq!(
            InetSocketAddrExt::from_str("udp://[::1]:6865").unwrap(),
            ip6
        );
        assert_eq!(format!("{}", ip4), "tcp://127.0.0.1:6865");
        assert_eq!(format!("{}", ip6), "udp://::1:6865");

        let uenc4 = ip4.to_uniform_encoding();
        assert_eq!(
            InetSocketAddrExt::from_uniform_encoding(&uenc4).unwrap(),
            ip4
        );
        let uenc6 = ip6.to_uniform_encoding();
        assert_ne!(uenc4.to_vec(), uenc6.to_vec());
        assert_eq!(
            InetSocketAddrExt::from_uniform_encoding(&uenc6).unwrap(),
            ip6
        );
    }
}