Connect to a server's IPv4 address first, as vanilla does
Vanilla resolves a server with InetAddress.getByName, and Java's default (java.net.preferIPv6Addresses=false) puts IPv4 addresses first. Horizon tried addresses in the system resolver's order, which on macOS puts IPv6 first: a server whose IPv6 address resets the connection showed "Connection reset by peer (os error 54)" where vanilla joined. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
use crate::{Error, Result, buf::Reader, crypto::Cfb8, frame};
|
use crate::{Error, Result, buf::Reader, crypto::Cfb8, frame};
|
||||||
use std::{
|
use std::{
|
||||||
io::{Read, Write},
|
io::{Read, Write},
|
||||||
net::{Shutdown, TcpStream, ToSocketAddrs},
|
net::{Shutdown, SocketAddr, TcpStream, ToSocketAddrs},
|
||||||
sync::{
|
sync::{
|
||||||
Arc, Condvar, Mutex,
|
Arc, Condvar, Mutex,
|
||||||
atomic::{AtomicI64, Ordering},
|
atomic::{AtomicI64, Ordering},
|
||||||
@@ -180,6 +180,13 @@ pub struct ConnWriter {
|
|||||||
pub threshold: Option<usize>,
|
pub threshold: Option<usize>,
|
||||||
counters: Arc<Counters>,
|
counters: Arc<Counters>,
|
||||||
}
|
}
|
||||||
|
/// InetAddress.getByName with Java's default `java.net.preferIPv6Addresses=false`: IPv4 addresses
|
||||||
|
/// first, whatever order the system resolver gives (macOS puts IPv6 first). A server whose IPv6
|
||||||
|
/// address doesn't answer the game still lets vanilla in over IPv4.
|
||||||
|
fn ipv4_first(mut addrs: Vec<SocketAddr>) -> Vec<SocketAddr> {
|
||||||
|
addrs.sort_by_key(SocketAddr::is_ipv6);
|
||||||
|
addrs
|
||||||
|
}
|
||||||
/// Both halves before the split.
|
/// Both halves before the split.
|
||||||
pub struct Connection {
|
pub struct Connection {
|
||||||
pub reader: ConnReader,
|
pub reader: ConnReader,
|
||||||
@@ -187,10 +194,12 @@ pub struct Connection {
|
|||||||
}
|
}
|
||||||
impl Connection {
|
impl Connection {
|
||||||
pub fn connect(host: &str, port: u16, timeout: Duration) -> Result<Self> {
|
pub fn connect(host: &str, port: u16, timeout: Duration) -> Result<Self> {
|
||||||
let addrs: Vec<_> = (host, port)
|
let addrs = ipv4_first(
|
||||||
.to_socket_addrs()
|
(host, port)
|
||||||
.map_err(|e| Error::new(format!("Unknown host {host}: {e}")))?
|
.to_socket_addrs()
|
||||||
.collect();
|
.map_err(|e| Error::new(format!("Unknown host {host}: {e}")))?
|
||||||
|
.collect(),
|
||||||
|
);
|
||||||
let mut last = Error::new(format!("Unknown host {host}"));
|
let mut last = Error::new(format!("Unknown host {host}"));
|
||||||
for addr in addrs {
|
for addr in addrs {
|
||||||
match TcpStream::connect_timeout(&addr, timeout) {
|
match TcpStream::connect_timeout(&addr, timeout) {
|
||||||
@@ -315,6 +324,26 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ipv4_addresses_come_first_in_resolver_order() {
|
||||||
|
let a = |s: &str| s.parse::<SocketAddr>().unwrap();
|
||||||
|
let resolved = vec![
|
||||||
|
a("[2001:db8::1]:25565"),
|
||||||
|
a("192.0.2.1:25565"),
|
||||||
|
a("[2001:db8::2]:25565"),
|
||||||
|
a("10.0.0.1:25565"),
|
||||||
|
];
|
||||||
|
assert_eq!(
|
||||||
|
ipv4_first(resolved),
|
||||||
|
[
|
||||||
|
a("192.0.2.1:25565"),
|
||||||
|
a("10.0.0.1:25565"),
|
||||||
|
a("[2001:db8::1]:25565"),
|
||||||
|
a("[2001:db8::2]:25565"),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn loopback_with_compression_and_encryption() {
|
fn loopback_with_compression_and_encryption() {
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user