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
use std::{
    collections::HashMap,
    net::{IpAddr, Ipv4Addr, Ipv6Addr},
};

use buffers::ByteBuf;
use byteorder::ByteOrder;
use byteorder::BE;
use bytes::Bytes;
use clone_to_owned::CloneToOwned;
use serde::{Deserialize, Deserializer, Serialize};

use crate::{EXTENDED_UT_METADATA_KEY, MY_EXTENDED_UT_METADATA};

#[derive(Deserialize, Serialize, Debug, Default)]
pub struct ExtendedHandshake<ByteBuf: Eq + std::hash::Hash> {
    #[serde(bound(deserialize = "ByteBuf: From<&'de [u8]>"))]
    pub m: HashMap<ByteBuf, u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub p: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub v: Option<ByteBuf>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub yourip: Option<YourIP>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ipv6: Option<ByteBuf>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ipv4: Option<ByteBuf>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reqq: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata_size: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub complete_ago: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub upload_only: Option<u32>,
}

impl ExtendedHandshake<ByteBuf<'static>> {
    pub fn new() -> Self {
        let mut features = HashMap::new();
        features.insert(ByteBuf(EXTENDED_UT_METADATA_KEY), MY_EXTENDED_UT_METADATA);
        Self {
            m: features,
            ..Default::default()
        }
    }
}

impl<'a, ByteBuf> ExtendedHandshake<ByteBuf>
where
    ByteBuf: Eq + std::hash::Hash + std::borrow::Borrow<[u8]>,
{
    fn get_msgid(&self, msg_type: &'a [u8]) -> Option<u8> {
        self.m.get(msg_type).copied()
    }

    pub fn ut_metadata(&self) -> Option<u8> {
        self.get_msgid(EXTENDED_UT_METADATA_KEY)
    }
}

impl<ByteBuf> CloneToOwned for ExtendedHandshake<ByteBuf>
where
    ByteBuf: CloneToOwned + Eq + std::hash::Hash,
    <ByteBuf as CloneToOwned>::Target: Eq + std::hash::Hash,
{
    type Target = ExtendedHandshake<<ByteBuf as CloneToOwned>::Target>;

    fn clone_to_owned(&self, within_buffer: Option<&Bytes>) -> Self::Target {
        ExtendedHandshake {
            m: self.m.clone_to_owned(within_buffer),
            p: self.p,
            v: self.v.clone_to_owned(within_buffer),
            yourip: self.yourip,
            ipv6: self.ipv6.clone_to_owned(within_buffer),
            ipv4: self.ipv4.clone_to_owned(within_buffer),
            reqq: self.reqq,
            metadata_size: self.metadata_size,
            complete_ago: self.complete_ago,
            upload_only: self.upload_only,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct YourIP(pub IpAddr);

impl Serialize for YourIP {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self.0 {
            IpAddr::V4(ipv4) => {
                let buf = ipv4.octets();
                serializer.serialize_bytes(&buf)
            }
            IpAddr::V6(ipv6) => {
                let buf = ipv6.octets();
                serializer.serialize_bytes(&buf)
            },
        }
    }
}

impl<'de> Deserialize<'de> for YourIP {
    fn deserialize<D>(de: D) -> Result<YourIP, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct Visitor {}
        impl<'de> serde::de::Visitor<'de> for Visitor {
            type Value = YourIP;

            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(f, "expecting 4 bytes of ipv4 or 16 bytes of ipv6")
            }

            fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                if v.len() == 4 {
                    return Ok(YourIP(IpAddr::V4(Ipv4Addr::new(v[0], v[1], v[2], v[3]))));
                } else if v.len() == 16 {
                    return Ok(YourIP(IpAddr::V6(Ipv6Addr::new(
                        BE::read_u16(&v[..2]),
                        BE::read_u16(&v[2..4]),
                        BE::read_u16(&v[4..6]),
                        BE::read_u16(&v[6..8]),
                        BE::read_u16(&v[8..10]),
                        BE::read_u16(&v[10..12]),
                        BE::read_u16(&v[12..14]),
                        BE::read_u16(&v[14..]),
                    ))));
                }
                Err(E::custom("expected 4 or 16 byte address"))
            }
        }
        de.deserialize_bytes(Visitor {})
    }
}