Skip to content

Commit 95e411b

Browse files
authored
use human-readable address in error log of send_packet (#155)
1 parent 9865b33 commit 95e411b

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/service_daemon.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,11 @@ fn broadcast_dns_on_intf(out: &DnsOutgoing, intf: &IntfSock) -> Vec<u8> {
21732173

21742174
/// Sends an outgoing broadcast packet, and returns the packet bytes.
21752175
fn broadcast_on_intf<'a>(packet: &'a [u8], intf: &IntfSock) -> &'a [u8] {
2176+
if packet.len() > MAX_MSG_ABSOLUTE {
2177+
error!("Drop over-sized packet ({})", packet.len());
2178+
return &[];
2179+
}
2180+
21762181
let sock: SocketAddr = match intf.intf.addr {
21772182
if_addrs::IfAddr::V4(_) => SocketAddrV4::new(GROUP_ADDR_V4, MDNS_PORT).into(),
21782183
if_addrs::IfAddr::V6(_) => {
@@ -2182,21 +2187,17 @@ fn broadcast_on_intf<'a>(packet: &'a [u8], intf: &IntfSock) -> &'a [u8] {
21822187
}
21832188
};
21842189

2185-
if packet.len() > MAX_MSG_ABSOLUTE {
2186-
error!("Drop over-sized packet ({})", packet.len());
2187-
return &[];
2188-
}
2189-
2190-
send_packet(packet, &SockAddr::from(sock), intf);
2190+
send_packet(packet, sock, intf);
21912191
packet
21922192
}
21932193

21942194
/// Sends out `packet` to `addr` on the socket in `intf_sock`.
2195-
fn send_packet(packet: &[u8], addr: &SockAddr, intf_sock: &IntfSock) {
2196-
match intf_sock.sock.send_to(packet, addr) {
2195+
fn send_packet(packet: &[u8], addr: SocketAddr, intf_sock: &IntfSock) {
2196+
let sockaddr = SockAddr::from(addr);
2197+
match intf_sock.sock.send_to(packet, &sockaddr) {
21972198
Ok(sz) => debug!("sent out {} bytes on interface {:?}", sz, &intf_sock.intf),
21982199
Err(e) => error!(
2199-
"send to {:?} via interface {:?} failed: {}",
2200+
"Failed to send to {} via {:?}: {}",
22002201
addr, &intf_sock.intf, e
22012202
),
22022203
}
@@ -2213,12 +2214,19 @@ fn valid_instance_name(name: &str) -> bool {
22132214
mod tests {
22142215
use super::{
22152216
broadcast_dns_on_intf, my_ip_interfaces, new_socket_bind, valid_instance_name, IntfSock,
2216-
ServiceDaemon, ServiceEvent, ServiceInfo,
2217+
ServiceDaemon, ServiceEvent, ServiceInfo, GROUP_ADDR_V4, MDNS_PORT,
22172218
};
22182219
use crate::dns_parser::{
22192220
DnsOutgoing, DnsPointer, CLASS_IN, FLAGS_AA, FLAGS_QR_RESPONSE, TYPE_PTR,
22202221
};
2221-
use std::time::Duration;
2222+
use std::{net::SocketAddr, net::SocketAddrV4, time::Duration};
2223+
2224+
#[test]
2225+
fn test_socketaddr_print() {
2226+
let addr: SocketAddr = SocketAddrV4::new(GROUP_ADDR_V4, MDNS_PORT).into();
2227+
let print = format!("{}", addr);
2228+
assert_eq!(print, "224.0.0.251:5353");
2229+
}
22222230

22232231
#[test]
22242232
fn test_instance_name() {

0 commit comments

Comments
 (0)