Skip to main content

cmtraceopen_parser/parser/
dns_debug.rs

1//! Windows DNS Server debug log parser.
2//!
3//! Parses PACKET summary lines from DNS debug logs (`dns.log`).
4//! These text files start with a ~29-line header, followed by PACKET lines
5//! and optional multi-line detail sections.
6//!
7//! Example PACKET line:
8//! ```text
9//! 4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Rcv 127.0.0.1       d07e   Q [0001   D   NOERROR] SOA    (4)home(4)gell(3)one(0)
10//! ```
11
12use super::dns_types;
13use super::timestamped::DateOrder;
14use crate::models::log_entry::{LogEntry, LogFormat};
15use regex::Regex;
16use std::sync::OnceLock;
17
18/// Regex for PACKET summary lines in DNS debug logs.
19///
20/// Groups:
21///  1: timestamp string (everything before the thread hex)
22///  2: thread hex (e.g. "0294")
23///  3: memory address
24///  4: protocol (UDP/TCP)
25///  5: direction (Snd/Rcv)
26///  6: remote IP
27///  7: transaction ID hex
28///  8: R flag (present on responses, absent on queries)
29///  9: opcode (Q=query, N=notify, U=update, ?)
30/// 10: flags+rcode bracket contents (e.g. "0001   D   NOERROR")
31/// 11: query type (A, AAAA, SOA, etc.)
32/// 12: query name in wire format
33fn packet_line_re() -> &'static Regex {
34    static CELL: OnceLock<Regex> = OnceLock::new();
35    CELL.get_or_init(|| {
36        Regex::new(
37            r"^(.+?)\s+([0-9A-Fa-f]{4})\s+PACKET\s+([0-9A-Fa-f]+)\s+(UDP|TCP)\s+(Snd|Rcv)\s+(\S+)\s+([0-9A-Fa-f]{4})\s+(R\s+|)([QNU?])\s+\[([^\]]+)\]\s+(\S+)\s+(.*)$"
38        )
39        .expect("packet_line_re: DNS debug PACKET line pattern must compile")
40    })
41}
42
43/// Regex to extract port from detail section: `Remote addr X.X.X.X, port NNNNN`
44fn detail_port_re() -> &'static Regex {
45    static CELL: OnceLock<Regex> = OnceLock::new();
46    CELL.get_or_init(|| {
47        Regex::new(r"Remote addr\s+\S+,\s+port\s+(\d+)")
48            .expect("detail_port_re: DNS detail port extraction pattern must compile")
49    })
50}
51
52/// Fast pre-check: returns true if this line looks like a DNS debug PACKET line.
53pub fn matches_dns_debug_record(line: &str) -> bool {
54    // Fast string check before running the regex
55    if !line.contains("PACKET") {
56        return false;
57    }
58    packet_line_re().is_match(line)
59}
60
61/// Parse DNS debug log lines into `LogEntry` values.
62///
63/// Uses `LogicalRecord` framing: a PACKET line starts a new record,
64/// subsequent non-PACKET lines (detail sections) append to it.
65/// Header lines (before the first PACKET) and blank lines are skipped.
66pub fn parse_lines(lines: &[&str], file_path: &str, date_order: DateOrder) -> (Vec<LogEntry>, u32) {
67    let mut entries = Vec::with_capacity(lines.len() / 2);
68    let mut parse_errors: u32 = 0;
69    let mut next_id: u64 = 0;
70    let mut pending: Option<PendingEntry> = None;
71
72    for (index, line) in lines.iter().enumerate() {
73        let line_number = (index + 1) as u32;
74        let trimmed = line.trim();
75
76        if trimmed.is_empty() {
77            continue;
78        }
79
80        // Try to parse as a PACKET line
81        if let Some(entry) = parse_packet_line(trimmed, file_path, date_order) {
82            // Flush any pending entry
83            flush_pending(&mut entries, &mut pending, &mut next_id);
84            pending = Some(PendingEntry {
85                entry,
86                start_line: line_number,
87            });
88            continue;
89        }
90
91        // Line contains "PACKET" but failed structured parsing — count as a parse error
92        if trimmed.contains("PACKET") {
93            parse_errors += 1;
94        }
95
96        // Not a PACKET line — if we have a pending entry, this is a detail line
97        if let Some(ref mut pending_entry) = pending {
98            // Check for port in detail section
99            if let Some(caps) = detail_port_re().captures(trimmed) {
100                if let Some(port_str) = caps.get(1) {
101                    let port = port_str.as_str();
102                    // Append port to source_ip
103                    if let Some(ref ip) = pending_entry.entry.source_ip {
104                        if !ip.contains(':') {
105                            pending_entry.entry.source_ip = Some(format!("{}:{}", ip, port));
106                        }
107                    }
108                }
109            }
110
111            // Append detail text to message
112            if !pending_entry.entry.message.is_empty() {
113                pending_entry.entry.message.push('\n');
114            }
115            pending_entry.entry.message.push_str(trimmed);
116        }
117        // If no pending entry, this is a header line — skip it
118    }
119
120    // Flush final pending entry
121    flush_pending(&mut entries, &mut pending, &mut next_id);
122
123    (entries, parse_errors)
124}
125
126struct PendingEntry {
127    entry: LogEntry,
128    start_line: u32,
129}
130
131fn flush_pending(
132    entries: &mut Vec<LogEntry>,
133    pending: &mut Option<PendingEntry>,
134    next_id: &mut u64,
135) {
136    if let Some(mut pending_entry) = pending.take() {
137        pending_entry.entry.id = *next_id;
138        pending_entry.entry.line_number = pending_entry.start_line;
139        entries.push(pending_entry.entry);
140        *next_id += 1;
141    }
142}
143
144/// Parse a single PACKET line into a LogEntry.
145fn parse_packet_line(line: &str, file_path: &str, date_order: DateOrder) -> Option<LogEntry> {
146    let caps = packet_line_re().captures(line)?;
147
148    let timestamp_str = caps.get(1)?.as_str().trim();
149    let thread_hex = caps.get(2)?.as_str();
150    // _memory_addr = caps.get(3)
151    let protocol = caps.get(4)?.as_str();
152    let direction = caps.get(5)?.as_str();
153    let remote_ip = caps.get(6)?.as_str().trim();
154    // _txn_id = caps.get(7)
155    // _r_flag = caps.get(8) — present on responses
156    // _opcode = caps.get(9)
157    let flags_rcode = caps.get(10)?.as_str();
158    let query_type_str = caps.get(11)?.as_str().trim();
159    let query_name_raw = caps.get(12)?.as_str().trim();
160
161    // Parse thread hex to decimal
162    let thread_val = u32::from_str_radix(thread_hex, 16).unwrap_or(0);
163    let thread_display = format!("{} (0x{})", thread_val, thread_hex);
164
165    // Parse flags and rcode from bracket contents
166    // Format: "HHHH CCCC RCODE" with variable whitespace
167    // Examples: "0001   D   NOERROR", "8085 A DR  NOERROR", "02a8      SERVFAIL"
168    let (flags_hex, rcode) = parse_flags_rcode(flags_rcode);
169
170    // Decode query name
171    let query_name = dns_types::decode_query_name(query_name_raw);
172
173    // Map rcode to severity
174    let severity = dns_types::rcode_to_severity(&rcode);
175
176    // Parse timestamp
177    let (timestamp, timestamp_display) = parse_dns_timestamp(timestamp_str, date_order);
178
179    // Build message: "[Rcv] [UDP] home.gell.one (A) → NOERROR"
180    let message = format!(
181        "[{}] [{}] {} ({}) \u{2192} {}",
182        direction, protocol, query_name, query_type_str, rcode
183    );
184
185    Some(LogEntry {
186        id: 0,
187        line_number: 0,
188        message,
189        component: None,
190        timestamp,
191        timestamp_display,
192        severity,
193        thread: Some(thread_val),
194        thread_display: Some(thread_display),
195        source_file: None,
196        format: LogFormat::DnsDebug,
197        file_path: file_path.to_string(),
198        timezone_offset: None,
199        error_code_spans: Vec::new(),
200        ip_address: None,
201        host_name: None,
202        mac_address: None,
203        result_code: None,
204        gle_code: None,
205        setup_phase: None,
206        operation_name: None,
207        http_method: None,
208        uri_stem: None,
209        uri_query: None,
210        status_code: None,
211        sub_status: None,
212        time_taken_ms: None,
213        client_ip: None,
214        server_ip: None,
215        user_agent: None,
216        server_port: None,
217        username: None,
218        win32_status: None,
219        query_name: if query_name.is_empty() {
220            None
221        } else {
222            Some(query_name)
223        },
224        query_type: Some(query_type_str.to_string()),
225        response_code: Some(rcode),
226        dns_direction: Some(direction.to_string()),
227        dns_protocol: Some(protocol.to_string()),
228        source_ip: Some(remote_ip.to_string()),
229        dns_flags: Some(format!("0x{}", flags_hex)),
230        dns_event_id: None,
231        zone_name: None,
232        entry_kind: None,
233        whatif: None,
234        section_name: None,
235        section_color: None,
236        iteration: None,
237        tags: None,
238    })
239}
240
241/// Parse the flags+rcode section from inside brackets.
242///
243/// The format is: `HHHH [flags] RCODE` with variable spacing.
244/// Examples:
245///   "0001   D   NOERROR"
246///   "8085 A DR  NOERROR"
247///   "8385 A DR NXDOMAIN"
248///   "02a8      SERVFAIL"
249///   "0028       NOERROR"
250///   "8081   DR  NOERROR"
251fn parse_flags_rcode(s: &str) -> (String, String) {
252    let parts: Vec<&str> = s.split_whitespace().collect();
253    if parts.is_empty() {
254        return (String::new(), "UNKNOWN".to_string());
255    }
256
257    let flags_hex = parts[0].to_string();
258    // The RCODE is always the last token
259    let rcode = parts.last().unwrap_or(&"UNKNOWN").to_string();
260
261    (flags_hex, rcode)
262}
263
264/// Parse DNS debug log timestamps.
265///
266/// Three locale variants are handled:
267///   US:  M/d/yyyy h:mm:ss AM/PM  (12-hour)
268///   EU:  dd/MM/yyyy HH:mm:ss     (24-hour)
269///   ISO: yyyyMMdd HH:mm:ss
270///
271/// The `date_order` parameter resolves MM/DD vs DD/MM ambiguity for slash dates.
272fn parse_dns_timestamp(s: &str, date_order: DateOrder) -> (Option<i64>, Option<String>) {
273    // Try to split into date part and time part
274    // US format has AM/PM at the end, so we need to handle that
275
276    let trimmed = s.trim();
277
278    // Check for ISO format: yyyyMMdd HH:mm:ss
279    // Strict validation: positions 0..8 must all be digits and position 8 must be a space.
280    if trimmed.len() >= 15 {
281        let bytes = trimmed.as_bytes();
282        if bytes[0..8].iter().all(|b| b.is_ascii_digit()) && bytes[8] == b' ' {
283            return parse_iso_timestamp(trimmed);
284        }
285    }
286
287    // Slash-delimited date: M/d/yyyy or dd/MM/yyyy
288    if let Some(space_pos) = trimmed.find(' ') {
289        let date_part = &trimmed[..space_pos];
290        let time_part = &trimmed[space_pos + 1..];
291
292        let date_fields: Vec<&str> = date_part.split('/').collect();
293        if date_fields.len() != 3 {
294            return (None, Some(trimmed.to_string()));
295        }
296
297        let field1: u32 = date_fields[0].parse().unwrap_or(0);
298        let field2: u32 = date_fields[1].parse().unwrap_or(0);
299        let year: i32 = date_fields[2].parse().unwrap_or(0);
300
301        let (month, day) = match date_order {
302            DateOrder::MonthFirst => (field1, field2),
303            DateOrder::DayFirst => (field2, field1),
304        };
305
306        // Parse time: "3:29:17 PM" or "15:29:17"
307        let (hour, minute, second) = parse_time_component(time_part);
308
309        let timestamp = chrono::NaiveDate::from_ymd_opt(year, month, day)
310            .and_then(|d| d.and_hms_opt(hour, minute, second))
311            .map(|dt| dt.and_utc().timestamp_millis());
312
313        let display = Some(format!(
314            "{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
315            year, month, day, hour, minute, second
316        ));
317
318        return (timestamp, display);
319    }
320
321    (None, Some(trimmed.to_string()))
322}
323
324/// Parse ISO timestamp: yyyyMMdd HH:mm:ss
325fn parse_iso_timestamp(s: &str) -> (Option<i64>, Option<String>) {
326    if s.len() < 15 {
327        return (None, Some(s.to_string()));
328    }
329
330    let year: i32 = s[0..4].parse().unwrap_or(0);
331    let month: u32 = s[4..6].parse().unwrap_or(0);
332    let day: u32 = s[6..8].parse().unwrap_or(0);
333
334    // Skip space at index 8
335    let time_str = &s[9..];
336    let time_parts: Vec<&str> = time_str.split(':').collect();
337    if time_parts.len() < 3 {
338        return (None, Some(s.to_string()));
339    }
340
341    let hour: u32 = time_parts[0].parse().unwrap_or(0);
342    let minute: u32 = time_parts[1].parse().unwrap_or(0);
343    let second: u32 = time_parts[2].parse().unwrap_or(0);
344
345    let timestamp = chrono::NaiveDate::from_ymd_opt(year, month, day)
346        .and_then(|d| d.and_hms_opt(hour, minute, second))
347        .map(|dt| dt.and_utc().timestamp_millis());
348
349    let display = Some(format!(
350        "{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
351        year, month, day, hour, minute, second
352    ));
353
354    (timestamp, display)
355}
356
357/// Parse a time component that may be 12-hour (with AM/PM) or 24-hour.
358fn parse_time_component(s: &str) -> (u32, u32, u32) {
359    let upper = s.trim().to_uppercase();
360    let is_pm = upper.contains("PM");
361    let is_am = upper.contains("AM");
362
363    // Strip AM/PM
364    let time_only = upper.replace("AM", "").replace("PM", "").trim().to_string();
365
366    let parts: Vec<&str> = time_only.split(':').collect();
367    if parts.len() < 3 {
368        return (0, 0, 0);
369    }
370
371    let mut hour: u32 = parts[0].trim().parse().unwrap_or(0);
372    let minute: u32 = parts[1].trim().parse().unwrap_or(0);
373    let second: u32 = parts[2].trim().parse().unwrap_or(0);
374
375    if is_pm || is_am {
376        // 12-hour format conversion
377        if is_pm && hour != 12 {
378            hour += 12;
379        } else if is_am && hour == 12 {
380            hour = 0;
381        }
382    }
383
384    (hour, minute, second)
385}
386
387#[cfg(test)]
388mod tests {
389    use super::*;
390    use crate::models::log_entry::Severity;
391
392    #[test]
393    fn test_matches_packet_line() {
394        let line = "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Rcv 127.0.0.1       d07e   Q [0001   D   NOERROR] SOA    (4)home(4)gell(3)one(0)";
395        assert!(matches_dns_debug_record(line));
396    }
397
398    #[test]
399    fn test_does_not_match_header() {
400        assert!(!matches_dns_debug_record(
401            "DNS Server log file creation at 4/11/2026 3:29:00 PM"
402        ));
403        assert!(!matches_dns_debug_record("Log file wrap:"));
404        assert!(!matches_dns_debug_record(
405            "Message logging key (for packets - other items use a subset):"
406        ));
407    }
408
409    #[test]
410    fn test_does_not_match_detail_line() {
411        assert!(!matches_dns_debug_record("  Socket = 884"));
412        assert!(!matches_dns_debug_record(
413            "  Remote addr 127.0.0.1, port 54159"
414        ));
415        assert!(!matches_dns_debug_record(
416            "UDP question info at 000002DAEC36D650"
417        ));
418    }
419
420    #[test]
421    fn test_parse_basic_query_response_pair() {
422        let lines = [
423            "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Rcv 127.0.0.1       d07e   Q [0001   D   NOERROR] SOA    (4)home(4)gell(3)one(0)",
424            "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Snd 127.0.0.1       d07e R Q [8085 A DR  NOERROR] SOA    (4)home(4)gell(3)one(0)",
425        ];
426        let line_refs: Vec<&str> = lines.iter().map(|s| s.as_ref()).collect();
427        let (entries, errors) = parse_lines(&line_refs, "dns.log", DateOrder::MonthFirst);
428
429        assert_eq!(errors, 0);
430        assert_eq!(entries.len(), 2);
431
432        // First entry: query (Rcv)
433        assert_eq!(entries[0].dns_direction.as_deref(), Some("Rcv"));
434        assert_eq!(entries[0].dns_protocol.as_deref(), Some("UDP"));
435        assert_eq!(entries[0].query_name.as_deref(), Some("home.gell.one"));
436        assert_eq!(entries[0].query_type.as_deref(), Some("SOA"));
437        assert_eq!(entries[0].response_code.as_deref(), Some("NOERROR"));
438        assert_eq!(entries[0].severity, Severity::Info);
439        assert_eq!(entries[0].format, LogFormat::DnsDebug);
440        assert!(entries[0].message.contains("[Rcv]"));
441        assert!(entries[0].message.contains("[UDP]"));
442        assert!(entries[0].message.contains("home.gell.one"));
443        assert!(entries[0].message.contains("\u{2192}"));
444        assert!(entries[0].message.contains("NOERROR"));
445
446        // Second entry: response (Snd)
447        assert_eq!(entries[1].dns_direction.as_deref(), Some("Snd"));
448        assert_eq!(entries[1].dns_protocol.as_deref(), Some("UDP"));
449        assert_eq!(entries[1].query_name.as_deref(), Some("home.gell.one"));
450    }
451
452    #[test]
453    fn test_parse_with_detail_section_extracts_port() {
454        let lines = [
455            "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Rcv 127.0.0.1       d07e   Q [0001   D   NOERROR] SOA    (4)home(4)gell(3)one(0)",
456            "UDP question info at 000002DAEC36D650",
457            "  Socket = 884",
458            "  Remote addr 127.0.0.1, port 54159",
459            "  Time Query=1714823, Queued=0, Expire=0",
460        ];
461        let line_refs: Vec<&str> = lines.iter().map(|s| s.as_ref()).collect();
462        let (entries, errors) = parse_lines(&line_refs, "dns.log", DateOrder::MonthFirst);
463
464        assert_eq!(errors, 0);
465        assert_eq!(entries.len(), 1);
466        assert_eq!(entries[0].source_ip.as_deref(), Some("127.0.0.1:54159"));
467    }
468
469    #[test]
470    fn test_parse_severity_mapping() {
471        let lines = [
472            "4/11/2026 8:34:00 PM 0294 PACKET  000002DAEF3AFDC0 UDP Snd 127.0.0.1       3c8a R Q [8385 A DR NXDOMAIN] A      (4)HOME(4)home(4)gell(3)one(0)",
473            "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC3680D0 UDP Snd 192.168.2.9     7822 R U [02a8      SERVFAIL] SOA    (4)home(4)gell(3)one(0)",
474        ];
475        let line_refs: Vec<&str> = lines.iter().map(|s| s.as_ref()).collect();
476        let (entries, _) = parse_lines(&line_refs, "dns.log", DateOrder::MonthFirst);
477
478        assert_eq!(entries[0].severity, Severity::Warning); // NXDOMAIN
479        assert_eq!(entries[1].severity, Severity::Error); // SERVFAIL
480    }
481
482    #[test]
483    fn test_parse_skips_header() {
484        let lines = [
485            "DNS Server log file creation at 4/11/2026 3:29:00 PM",
486            "Log file wrap:",
487            "Message logging key (for packets - other items use a subset):",
488            "",
489            "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Rcv 127.0.0.1       d07e   Q [0001   D   NOERROR] SOA    (4)home(4)gell(3)one(0)",
490        ];
491        let line_refs: Vec<&str> = lines.iter().map(|s| s.as_ref()).collect();
492        let (entries, errors) = parse_lines(&line_refs, "dns.log", DateOrder::MonthFirst);
493
494        assert_eq!(errors, 0);
495        assert_eq!(entries.len(), 1);
496        assert_eq!(entries[0].query_name.as_deref(), Some("home.gell.one"));
497    }
498
499    #[test]
500    fn test_parse_thread_display() {
501        let lines = [
502            "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Rcv 127.0.0.1       d07e   Q [0001   D   NOERROR] SOA    (4)home(4)gell(3)one(0)",
503        ];
504        let line_refs: Vec<&str> = lines.iter().map(|s| s.as_ref()).collect();
505        let (entries, _) = parse_lines(&line_refs, "dns.log", DateOrder::MonthFirst);
506
507        assert_eq!(entries[0].thread, Some(660));
508        assert_eq!(entries[0].thread_display.as_deref(), Some("660 (0x0294)"));
509    }
510
511    #[test]
512    fn test_parse_us_timestamp() {
513        let lines = [
514            "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Rcv 127.0.0.1       d07e   Q [0001   D   NOERROR] SOA    (4)home(4)gell(3)one(0)",
515        ];
516        let line_refs: Vec<&str> = lines.iter().map(|s| s.as_ref()).collect();
517        let (entries, _) = parse_lines(&line_refs, "dns.log", DateOrder::MonthFirst);
518
519        assert_eq!(
520            entries[0].timestamp_display.as_deref(),
521            Some("2026-04-11 15:29:17")
522        );
523    }
524
525    #[test]
526    fn test_parse_dynamic_update_opcode() {
527        let lines = [
528            "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC3680D0 UDP Rcv 192.168.2.9     7822   U [0028       NOERROR] SOA    (4)home(4)gell(3)one(0)",
529        ];
530        let line_refs: Vec<&str> = lines.iter().map(|s| s.as_ref()).collect();
531        let (entries, errors) = parse_lines(&line_refs, "dns.log", DateOrder::MonthFirst);
532
533        assert_eq!(errors, 0);
534        assert_eq!(entries.len(), 1);
535        assert_eq!(entries[0].dns_direction.as_deref(), Some("Rcv"));
536        assert_eq!(entries[0].response_code.as_deref(), Some("NOERROR"));
537    }
538
539    #[test]
540    fn test_parse_root_query() {
541        let lines = [
542            "4/11/2026 8:33:19 PM 0294 PACKET  000002DAECB28D10 UDP Snd 192.168.2.9     131a R Q [8081   DR  NOERROR] NS     (0)",
543        ];
544        let line_refs: Vec<&str> = lines.iter().map(|s| s.as_ref()).collect();
545        let (entries, errors) = parse_lines(&line_refs, "dns.log", DateOrder::MonthFirst);
546
547        assert_eq!(errors, 0);
548        assert_eq!(entries.len(), 1);
549        assert_eq!(entries[0].query_name.as_deref(), Some("."));
550    }
551}