Skip to content

Commit

Permalink
DnsTxt debug print: make its text field human-readable (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
keepsimple1 authored Aug 22, 2024
1 parent 5567c1f commit 3292110
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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

0 comments on commit 3292110

Please sign in to comment.