mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
new lan discovery https://github.com/rustdesk/rustdesk/issues/261
This commit is contained in:
@@ -4,7 +4,7 @@ use bytes::{Bytes, BytesMut};
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use protobuf::Message;
|
||||
use socket2::{Domain, Protocol, Socket, Type};
|
||||
use std::net::{SocketAddr, SocketAddrV4};
|
||||
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
|
||||
use tokio::net::{ToSocketAddrs, UdpSocket};
|
||||
use tokio_socks::{udp::Socks5UdpFramed, IntoTargetAddr, TargetAddr, ToProxyAddrs};
|
||||
use tokio_util::{codec::BytesCodec, udp::UdpFramed};
|
||||
@@ -145,13 +145,26 @@ impl FramedSocket {
|
||||
|
||||
// const DEFAULT_MULTICAST: &str = "239.255.42.98";
|
||||
|
||||
pub fn bind_multicast(addr: &SocketAddrV4, multi_addr: &SocketAddrV4) -> ResultType<FramedSocket> {
|
||||
assert!(multi_addr.ip().is_multicast(), "Must be multcast address");
|
||||
pub fn bind_multicast(maddr: Option<SocketAddrV4>) -> ResultType<FramedSocket> {
|
||||
// todo: https://github.com/bltavares/multicast-socket
|
||||
// 0.0.0.0 bind to default interface, if there are two interfaces, there will be problem.
|
||||
let socket = Socket::new(Domain::ipv4(), Type::dgram(), Some(Protocol::udp()))?;
|
||||
socket.set_reuse_address(true)?;
|
||||
socket.bind(&socket2::SockAddr::from(*addr))?;
|
||||
socket.set_multicast_loop_v4(true)?;
|
||||
socket.join_multicast_v4(multi_addr.ip(), addr.ip())?;
|
||||
// somehow without this, timer.tick() under tokio::select! does not work
|
||||
socket.set_read_timeout(Some(std::time::Duration::from_millis(100)))?;
|
||||
if let Some(maddr) = maddr {
|
||||
assert!(maddr.ip().is_multicast(), "Must be multcast address");
|
||||
let addr = SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0).into(), maddr.port());
|
||||
socket.join_multicast_v4(maddr.ip(), addr.ip())?;
|
||||
socket.set_multicast_loop_v4(true)?;
|
||||
socket.bind(&socket2::SockAddr::from(addr))?;
|
||||
} else {
|
||||
socket.set_multicast_if_v4(&Ipv4Addr::new(0, 0, 0, 0))?;
|
||||
socket.bind(&socket2::SockAddr::from(SocketAddr::new(
|
||||
Ipv4Addr::new(0, 0, 0, 0).into(),
|
||||
0,
|
||||
)))?;
|
||||
}
|
||||
Ok(FramedSocket::Direct(UdpFramed::new(
|
||||
UdpSocket::from_std(socket.into_udp_socket())?,
|
||||
BytesCodec::new(),
|
||||
|
||||
Reference in New Issue
Block a user