WIP: udp: add ipv6 support

Returning IPv6 peers doesn't really work with UDP. It is not supported by
https://libtorrent.org/udp_tracker_protocol.html. There is a suggestion in
https://web.archive.org/web/20170503181830/http://opentracker.blog.h3q.com/2007/12/28/the-ipv6-situation/
of using action number 4 and returning IPv6 octets just like for IPv4
addresses. Clients seem not to support it very well, but due to a lack of
alternative solutions, it is implemented here
This commit is contained in:
Joakim Frostegård 2020-07-31 05:37:58 +02:00
parent bdb6aced1c
commit a3a1d1606b
8 changed files with 230 additions and 111 deletions

View file

@ -1,6 +1,6 @@
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
use std::io::{Cursor, ErrorKind};
use std::net::SocketAddr;
use std::net::{SocketAddr, IpAddr};
use std::time::Duration;
use std::vec::Drain;
@ -215,7 +215,9 @@ fn send_responses(
for (response, src) in response_iterator {
cursor.set_position(0);
response_to_bytes(&mut cursor, response, IpVersion::IPv4).unwrap();
let ip_version = ip_version_from_ip(src.ip());
response_to_bytes(&mut cursor, response, ip_version).unwrap();
let amt = cursor.position() as usize;
@ -240,4 +242,18 @@ fn send_responses(
state.statistics.bytes_sent
.fetch_add(bytes_sent, Ordering::SeqCst);
}
}
fn ip_version_from_ip(ip: IpAddr) -> IpVersion {
match ip {
IpAddr::V4(_) => IpVersion::IPv4,
IpAddr::V6(ip) => {
if let [0, 0, 0, 0, 0, 0xffff, ..] = ip.segments(){
IpVersion::IPv4
} else {
IpVersion::IPv6
}
}
}
}