Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DnsTxt debug print: make its text field human-readable #247

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/dns_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#[cfg(feature = "logging")]
use crate::log::debug;
use crate::{Error, Result, ServiceInfo};
use crate::{service_info::decode_txt, Error, Result, ServiceInfo};
use if_addrs::Interface;
use std::{
any::Any,
Expand Down Expand Up @@ -482,7 +482,7 @@ impl DnsRecordExt for DnsSrv {
// marks). Everything up to the first '=' character is the key (Section
// 6.4). Everything after the first '=' character to the end of the
// string (including subsequent '=' characters, if any) is the value
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct DnsTxt {
pub(crate) record: DnsRecord,
pub(crate) text: Vec<u8>,
Expand Down Expand Up @@ -525,6 +525,17 @@ impl DnsRecordExt for DnsTxt {
}
}

impl fmt::Debug for DnsTxt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let properties = decode_txt(&self.text);
write!(
f,
"DnsTxt {{ record: {:?}, text: {:?} }}",
self.record, properties
)
}
}

/// A DNS host information record
#[derive(Debug, Clone)]
struct DnsHostInfo {
Expand Down
4 changes: 3 additions & 1 deletion src/service_info.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Define `ServiceInfo` to represent a service and its operations.

#[cfg(feature = "logging")]
use crate::log::error;
use crate::{dns_parser::split_sub_domain, Error, Result};
Expand Down Expand Up @@ -652,7 +654,7 @@ fn encode_txt<'a>(properties: impl Iterator<Item = &'a TxtProperty>) -> Vec<u8>
}

// Convert from DNS TXT record content to key/value pairs
fn decode_txt(txt: &[u8]) -> Vec<TxtProperty> {
pub(crate) fn decode_txt(txt: &[u8]) -> Vec<TxtProperty> {
let mut properties = Vec::new();
let mut offset = 0;
while offset < txt.len() {
Expand Down