PEX: Announce support and message definition
This commit is contained in:
parent
c758d0022d
commit
bbc951733f
7 changed files with 213 additions and 35 deletions
|
|
@ -1,16 +1,16 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
net::{IpAddr, Ipv4Addr, Ipv6Addr},
|
||||
net::IpAddr,
|
||||
};
|
||||
|
||||
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};
|
||||
use crate::{EXTENDED_UT_METADATA_KEY, EXTENDED_UT_PEX_KEY, MY_EXTENDED_UT_METADATA, MY_EXTENDED_UT_PEX};
|
||||
|
||||
use super::PeerExtendedMessageIds;
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Default)]
|
||||
pub struct ExtendedHandshake<ByteBuf: Eq + std::hash::Hash> {
|
||||
|
|
@ -40,6 +40,7 @@ impl ExtendedHandshake<ByteBuf<'static>> {
|
|||
pub fn new() -> Self {
|
||||
let mut features = HashMap::new();
|
||||
features.insert(ByteBuf(EXTENDED_UT_METADATA_KEY), MY_EXTENDED_UT_METADATA);
|
||||
features.insert(ByteBuf(EXTENDED_UT_PEX_KEY), MY_EXTENDED_UT_PEX);
|
||||
Self {
|
||||
m: features,
|
||||
..Default::default()
|
||||
|
|
@ -58,6 +59,17 @@ where
|
|||
pub fn ut_metadata(&self) -> Option<u8> {
|
||||
self.get_msgid(EXTENDED_UT_METADATA_KEY)
|
||||
}
|
||||
|
||||
pub fn ut_pex(&self) -> Option<u8> {
|
||||
self.get_msgid(EXTENDED_UT_PEX_KEY)
|
||||
}
|
||||
|
||||
pub fn peer_extended_messages(&self) -> PeerExtendedMessageIds {
|
||||
PeerExtendedMessageIds {
|
||||
ut_metadata: self.ut_metadata(),
|
||||
ut_pex: self.ut_pex(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<ByteBuf> CloneToOwned for ExtendedHandshake<ByteBuf>
|
||||
|
|
@ -122,18 +134,11 @@ impl<'de> Deserialize<'de> for YourIP {
|
|||
E: serde::de::Error,
|
||||
{
|
||||
if v.len() == 4 {
|
||||
return Ok(YourIP(IpAddr::V4(Ipv4Addr::new(v[0], v[1], v[2], v[3]))));
|
||||
let ip_bytes: &[u8; 4] = v[0..4].try_into().unwrap(); // Safe to unwrap as we check slice length
|
||||
return Ok(YourIP(IpAddr::from(*ip_bytes)));
|
||||
} 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..]),
|
||||
))));
|
||||
let ip_bytes: &[u8; 16] = v[0..16].try_into().unwrap(); // Safe to unwrap as we check slice length
|
||||
return Ok(YourIP(IpAddr::from(*ip_bytes)));
|
||||
}
|
||||
Err(E::custom("expected 4 or 16 byte address"))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue