From a863d82c7954d1c231d623309d69ba75160c5ef8 Mon Sep 17 00:00:00 2001 From: Hailey Somerville Date: Fri, 18 Aug 2023 19:12:35 +1000 Subject: [PATCH] set expedited forwarding on broadcast packets --- Cargo.lock | 10 ++++++++++ Cargo.toml | 2 +- src/main.rs | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 492b197..a21d653 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -436,6 +436,15 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "memoffset" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" +dependencies = [ + "autocfg", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -491,6 +500,7 @@ dependencies = [ "bitflags", "cfg-if", "libc", + "memoffset", "static_assertions", ] diff --git a/Cargo.toml b/Cargo.toml index a3a4ac2..1f16d4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" bytemuck = { version = "1.13.1", features = ["derive"] } cpal = "0.15.2" derive_more = "0.99.17" -nix = { version = "0.26.2", features = ["time"], default-features = false } +nix = { version = "0.26.2", features = ["time", "socket", "net"], default-features = false } static_assertions = "1.1.0" structopt = "0.3.26" termcolor = "1.2.0" diff --git a/src/main.rs b/src/main.rs index 8907fc7..0361e75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod status; mod resample; use std::net::{UdpSocket, Ipv4Addr, SocketAddrV4}; +use std::os::fd::AsRawFd; use std::process::ExitCode; use std::sync::{Mutex, Arc}; use std::time::Duration; @@ -12,6 +13,7 @@ use std::time::Duration; use bytemuck::Zeroable; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use cpal::{OutputCallbackInfo, StreamConfig, InputCallbackInfo, BufferSize, SupportedBufferSize}; +use nix::sys::socket::sockopt::IpTos; use structopt::StructOpt; use protocol::{TimestampMicros, AudioPacket, PacketBuffer, TimePacket, MAX_PACKET_SIZE, TimePacketPadding}; @@ -93,6 +95,12 @@ fn run_stream(opt: StreamOpt) -> Result<(), RunError> { socket.join_multicast_v4(&opt.group, bind.ip()) .map_err(RunError::JoinMulticast)?; + const IPTOS_DSCP_EF: i32 = 0xb8; + if let Err(e) = nix::sys::socket::setsockopt(socket.as_raw_fd(), IpTos, &IPTOS_DSCP_EF) { + eprintln!("warning: failed to set IPTOS_DSCP_EF (expedited forwarding) on broadcast socket: {e:?}"); + } + + // we don't need it: let _ = socket.set_multicast_loop_v4(false);