use crate::payloads::connectiontype::ConnectionType;
use crate::payloads::Ipv8Payload;
use crate::serialization::bits::Bits;
use crate::serialization::rawend::RawEnd;
use serde;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use crate::networking::address::Address;
#[derive(Debug, PartialEq)]
pub struct IntroductionResponsePayload {
pub destination_address: Address,
pub source_lan_address: Address,
pub source_wan_address: Address,
pub lan_introduction_address: Address,
pub wan_introduction_address: Address,
pub tunnel: bool,
pub connection_type: ConnectionType,
pub identifier: u16,
pub extra_bytes: RawEnd,
}
impl Ipv8Payload for IntroductionResponsePayload {
}
impl Serialize for IntroductionResponsePayload {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let conntype = self.connection_type.encode();
let mut state = serializer.serialize_struct("IntroductionResponsePayload", 6)?;
state.serialize_field("destination_address", &self.destination_address)?;
state.serialize_field("source_lan_address", &self.source_lan_address)?;
state.serialize_field("source_wan_address", &self.source_wan_address)?;
state.serialize_field("lan_introduction_address", &self.lan_introduction_address)?;
state.serialize_field("wan_introduction_address", &self.wan_introduction_address)?;
state.serialize_field(
"advice",
&Bits::from_bools((
conntype.0, conntype.1, false, false, false, false, false, false,
)),
)?;
state.serialize_field("identifier", &self.identifier)?;
state.serialize_field("extra_bytes", &self.extra_bytes)?;
state.end()
}
}
#[derive(Debug, PartialEq, serde::Deserialize)]
struct IntroductionResponsePayloadPattern(
Address,
Address,
Address,
Address,
Address,
Bits,
u16,
RawEnd,
);
impl<'de> Deserialize<'de> for IntroductionResponsePayload {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let payload_temporary = IntroductionResponsePayloadPattern::deserialize(deserializer)?;
Ok(IntroductionResponsePayload {
destination_address: payload_temporary.0,
source_lan_address: payload_temporary.1,
source_wan_address: payload_temporary.2,
lan_introduction_address: payload_temporary.3,
wan_introduction_address: payload_temporary.4,
tunnel: false,
connection_type: ConnectionType::decode((
payload_temporary.5.bit0,
payload_temporary.5.bit1,
)),
identifier: payload_temporary.6,
extra_bytes: payload_temporary.7,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::serialization::Packet;
use std::net::{Ipv4Addr, IpAddr, SocketAddr};
#[test]
fn integration_test_creation() {
let i = IntroductionResponsePayload {
destination_address: Address(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
8000,
)),
source_lan_address: Address(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(42, 42, 42, 42)),
8000,
)),
source_wan_address: Address(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0)),
8000,
)),
lan_introduction_address: Address(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(43, 43, 43, 43)),
8000,
)),
wan_introduction_address: Address(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(4, 44, 44, 44)),
8000,
)),
tunnel: true,
connection_type: ConnectionType::decode((true, true)),
identifier: 42,
extra_bytes: RawEnd(vec![43, 44]),
};
let mut packet = Packet::new(create_test_header!()).unwrap();
packet.add(&i).unwrap();
assert_eq!(
packet,
Packet(vec![
0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 127, 0, 0, 1,
31, 64, 42, 42, 42, 42, 31, 64, 255, 255, 255, 0, 31, 64, 43, 43, 43, 43, 31, 64,
4, 44, 44, 44, 31, 64, 3, 0, 42, 43, 44
])
);
assert_eq!(
IntroductionResponsePayload {
destination_address: Address(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
8000
)),
source_lan_address: Address(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(42, 42, 42, 42)),
8000
)),
source_wan_address: Address(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0)),
8000
)),
lan_introduction_address: Address(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(43, 43, 43, 43)),
8000
)),
wan_introduction_address: Address(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(4, 44, 44, 44)),
8000
)),
tunnel: false,
connection_type: ConnectionType::decode((true, true)),
identifier: 42,
extra_bytes: RawEnd(vec![43, 44]),
},
packet
.start_deserialize()
.skip_header()
.unwrap()
.next_payload()
.unwrap()
);
}
}