flowscope 0.18.0

Passive flow & session tracking for packet capture (runtime-free, cross-platform)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//! `Asset` + supporting types.

use std::net::{Ipv4Addr, Ipv6Addr};

use bitflags::bitflags;

use crate::{MacAddr, Timestamp};

/// One inventory record per MAC address. Fields are populated
/// opportunistically as parsers contribute; absent data stays
/// `None` / empty.
///
/// `#[non_exhaustive]` — fields land additively forever.
///
/// Issue #27 (0.18).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct Asset {
    /// L2 address — the inventory's primary key.
    pub mac: MacAddr,
    /// IPv4 addresses ever observed bound to `mac`. Bounded
    /// at 4 entries; the oldest is evicted when full.
    pub ipv4: Vec<Ipv4Addr>,
    /// IPv6 addresses ever observed bound to `mac`. Bounded
    /// at 4 entries; oldest-evicted-when-full.
    pub ipv6: Vec<Ipv6Addr>,
    /// Hostname — sourced from DHCP option 12 ("hostname")
    /// or LLDP / CDP "system name" TLV.
    pub hostname: Option<String>,
    /// Fully-qualified domain name — sourced from DHCP
    /// option 81 ("client FQDN"). May contain the hostname
    /// as the leftmost label.
    pub fqdn: Option<String>,
    /// Vendor / OS banner — sourced from DHCP option 60
    /// (`"MSFT 5.0"`), LLDP system-description, CDP
    /// software-version, or SSDP `SERVER`. The most
    /// discriminating fingerprint surface.
    pub vendor_banner: Option<String>,
    /// Hardware platform — sourced from LLDP / CDP "platform"
    /// TLV. Examples: `"cisco WS-C2960X-24TS-L"`,
    /// `"Cisco IP Phone 7960"`.
    pub platform: Option<String>,
    /// Capability bitmask — union over every parser's
    /// reported set.
    pub capabilities: AssetCapabilities,
    /// Per-parser fingerprints — populated when the source
    /// surfaced one. Each is `Option<String>` so a partial
    /// inventory doesn't lie about what it has.
    pub fingerprints: AssetFingerprints,
    /// Which parsers have contributed to this record.
    pub seen_via: AssetSourceSet,
    /// Most-recent observation timestamp.
    pub last_seen: Timestamp,
}

impl Asset {
    /// Construct a bare `Asset` for the given MAC. All
    /// observation fields are empty / default. Use the
    /// `from_*` adapter functions when you have a parsed
    /// message in hand.
    pub fn new(mac: MacAddr) -> Self {
        Self {
            mac,
            ipv4: Vec::new(),
            ipv6: Vec::new(),
            hostname: None,
            fqdn: None,
            vendor_banner: None,
            platform: None,
            capabilities: AssetCapabilities::empty(),
            fingerprints: AssetFingerprints::default(),
            seen_via: AssetSourceSet::empty(),
            last_seen: Timestamp::default(),
        }
    }

    /// Merge `other`'s fields into `self`. Non-empty / `Some`
    /// fields on `other` overwrite `self`. `last_seen` takes
    /// the later of the two. `seen_via` and `capabilities`
    /// bitwise-OR.
    pub fn merge(&mut self, other: &Asset) {
        // MAC mismatch is a usage error — assets are keyed by
        // MAC, so the inventory should never call merge on
        // two different MACs.
        debug_assert_eq!(self.mac, other.mac, "merge across different MAC keys");
        for ip in &other.ipv4 {
            if !self.ipv4.contains(ip) {
                push_bounded(&mut self.ipv4, *ip, MAX_IPS_PER_ASSET);
            }
        }
        for ip in &other.ipv6 {
            if !self.ipv6.contains(ip) {
                push_bounded(&mut self.ipv6, *ip, MAX_IPS_PER_ASSET);
            }
        }
        if other.hostname.is_some() {
            self.hostname = other.hostname.clone();
        }
        if other.fqdn.is_some() {
            self.fqdn = other.fqdn.clone();
        }
        if other.vendor_banner.is_some() {
            self.vendor_banner = other.vendor_banner.clone();
        }
        if other.platform.is_some() {
            self.platform = other.platform.clone();
        }
        self.capabilities |= other.capabilities;
        self.fingerprints.merge_from(&other.fingerprints);
        self.seen_via |= other.seen_via;
        if other.last_seen > self.last_seen {
            self.last_seen = other.last_seen;
        }
    }
}

/// Maximum number of IPs (v4 or v6) we'll remember per asset.
/// Devices roaming between IPs on the same MAC accumulate
/// state; bound the growth.
const MAX_IPS_PER_ASSET: usize = 4;

fn push_bounded<T: Clone>(v: &mut Vec<T>, item: T, cap: usize) {
    if v.len() >= cap {
        v.remove(0); // oldest goes first
    }
    v.push(item);
}

bitflags! {
    /// Device-capability bitmask. Combines the
    /// LLDP / CDP / SSDP capability vocabularies into a
    /// unified set.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
    pub struct AssetCapabilities: u32 {
        /// Layer 2 bridge / switch.
        const BRIDGE          = 1 << 0;
        /// Layer 3 router.
        const ROUTER          = 1 << 1;
        /// Multi-port switch (covers Suricata-style
        /// SWITCH capability).
        const SWITCH          = 1 << 2;
        /// IEEE 802.11 access point.
        const WLAN_AP         = 1 << 3;
        /// VoIP phone / IP phone.
        const PHONE           = 1 << 4;
        /// IGMP-capable.
        const IGMP            = 1 << 5;
        /// Repeater / hub.
        const REPEATER        = 1 << 6;
        /// DOCSIS cable modem.
        const DOCSIS_CABLE    = 1 << 7;
        /// Source-route bridge.
        const SOURCE_BRIDGE   = 1 << 8;
        /// Station-only (host, not infrastructure).
        const HOST            = 1 << 9;
        /// Remotely managed device.
        const REMOTELY_MANAGED = 1 << 10;
        /// UPnP / DLNA media device — IoT / consumer.
        const UPNP            = 1 << 11;
        /// 802.1Q customer / service VLAN tagging.
        const C_VLAN          = 1 << 12;
        const S_VLAN          = 1 << 13;
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for AssetCapabilities {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_u32(self.bits())
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for AssetCapabilities {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let bits = <u32 as serde::Deserialize>::deserialize(deserializer)?;
        Ok(AssetCapabilities::from_bits_truncate(bits))
    }
}

bitflags! {
    /// Which parsers have contributed to this asset record.
    /// Useful for both ranking confidence and for surfacing
    /// per-protocol freshness in dashboards.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
    pub struct AssetSourceSet: u32 {
        const ARP   = 1 << 0;
        const NDP   = 1 << 1;
        const DHCP  = 1 << 2;
        const LLDP  = 1 << 3;
        const CDP   = 1 << 4;
        const SSDP  = 1 << 5;
        const MDNS  = 1 << 6;
        const NBNS  = 1 << 7;
        /// Reserved for future parsers — SNMP-asset-discovery, etc.
        const OTHER = 1 << 31;
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for AssetSourceSet {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_u32(self.bits())
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for AssetSourceSet {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let bits = <u32 as serde::Deserialize>::deserialize(deserializer)?;
        Ok(AssetSourceSet::from_bits_truncate(bits))
    }
}

/// Per-parser fingerprints surfaced for this asset. Each is
/// `Option<String>` (rather than e.g. `Vec`) — devices have
/// at most one of each kind, and historical drift across
/// observations is recorded by overwrite (the most-recent
/// fingerprint wins).
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct AssetFingerprints {
    /// DHCP option-55 + option-60 (Fingerbank-style)
    /// fingerprint. Sourced from
    /// [`crate::dhcp::DhcpMessage::fingerprint`].
    pub dhcp: Option<String>,
    /// p0f-style TCP/IP fingerprint signature. Sourced from
    /// [`crate::tcp_fingerprint::TcpFingerprint::to_p0f_signature`].
    /// Consumers populate this themselves when they hold a
    /// TCP-fingerprint capture.
    pub p0f: Option<String>,
    /// HASSH client SSH fingerprint. From
    /// [`crate::ssh::SshKexInit::hassh`] (`from_client = true`).
    pub hassh: Option<String>,
    /// JA3 TLS-client fingerprint.
    pub ja3: Option<String>,
    /// JA4 TLS-client fingerprint.
    pub ja4: Option<String>,
}

impl AssetFingerprints {
    fn merge_from(&mut self, other: &AssetFingerprints) {
        if other.dhcp.is_some() {
            self.dhcp = other.dhcp.clone();
        }
        if other.p0f.is_some() {
            self.p0f = other.p0f.clone();
        }
        if other.hassh.is_some() {
            self.hassh = other.hassh.clone();
        }
        if other.ja3.is_some() {
            self.ja3 = other.ja3.clone();
        }
        if other.ja4.is_some() {
            self.ja4 = other.ja4.clone();
        }
    }
}

// ─── Per-parser adapter functions ────────────────────────────
//
// Each adapter takes a single parser-emitted message and
// returns a fresh `Asset` populated with whatever fields the
// message contributed. The caller absorbs it into an
// `Inventory` via `Inventory::absorb`, which merges by MAC.

#[cfg(feature = "arp")]
impl Asset {
    /// Build an `Asset` from one parsed ARP message. Uses
    /// `arp.sender` as the keying MAC and `arp.sender_ip` as
    /// the bound IPv4. The target side is intentionally NOT
    /// recorded — for ARP requests we don't know the target's
    /// MAC, and for replies the ARP-spoof case would lie.
    pub fn from_arp(arp: &crate::arp::ArpMessage) -> Self {
        let mut a = Self::new(arp.sender);
        a.ipv4.push(arp.sender_ip);
        a.capabilities |= AssetCapabilities::HOST;
        a.seen_via |= AssetSourceSet::ARP;
        a
    }
}

#[cfg(feature = "ndp")]
impl Asset {
    /// Build an `Asset` from one NDP NS / NA message. Requires
    /// the message to carry a Link-Layer Address option;
    /// returns `None` when the lladdr is absent (NS without
    /// SLLA / NA without TLLA).
    pub fn from_ndp(ndp: &crate::ndp::NdpMessage) -> Option<Self> {
        let mac = ndp.lladdr?;
        let mut a = Self::new(mac);
        a.ipv6.push(ndp.target);
        a.capabilities |= AssetCapabilities::HOST;
        a.seen_via |= AssetSourceSet::NDP;
        Some(a)
    }
}

#[cfg(feature = "dhcp")]
impl Asset {
    /// Build an `Asset` from one parsed DHCP message. Returns
    /// `None` when the message has no client-MAC (non-
    /// Ethernet htype/hlen — uncommon in practice). Pulls:
    ///
    /// - `client_mac` (chaddr) — primary key.
    /// - `ciaddr` and `yiaddr` — IPv4 bindings if non-zero.
    /// - `hostname` (opt 12) and `client_fqdn` (opt 81).
    /// - `vendor_class` (opt 60) → vendor_banner.
    /// - Fingerbank `fingerprint()` → fingerprints.dhcp.
    pub fn from_dhcp(dhcp: &crate::dhcp::DhcpMessage) -> Option<Self> {
        let mac = dhcp.client_mac?;
        let mut a = Self::new(mac);
        if !dhcp.ciaddr.is_unspecified() {
            a.ipv4.push(dhcp.ciaddr);
        }
        if !dhcp.yiaddr.is_unspecified() && !a.ipv4.contains(&dhcp.yiaddr) {
            a.ipv4.push(dhcp.yiaddr);
        }
        a.hostname = dhcp.hostname.clone();
        a.fqdn = dhcp.client_fqdn.clone();
        a.vendor_banner = dhcp.vendor_class.clone();
        a.fingerprints.dhcp = dhcp.fingerprint();
        a.capabilities |= AssetCapabilities::HOST;
        a.seen_via |= AssetSourceSet::DHCP;
        Some(a)
    }
}

#[cfg(feature = "lldp")]
impl Asset {
    /// Build an `Asset` from one parsed LLDP message. The
    /// keying MAC comes from the Chassis-ID TLV when it's a
    /// MAC subtype; falls back to `None` for non-MAC chassis
    /// IDs (interface-name / network-address / locally-
    /// assigned). Pulls system-name → hostname,
    /// system-description → vendor_banner, capabilities,
    /// management-addresses → ipv4/ipv6.
    pub fn from_lldp(lldp: &crate::lldp::LldpMessage) -> Option<Self> {
        let mac = match &lldp.chassis_id {
            crate::lldp::ChassisId::MacAddress(m) => *m,
            _ => return None,
        };
        let mut a = Self::new(mac);
        if let Some(name) = &lldp.system_name
            && let Ok(s) = std::str::from_utf8(name)
        {
            a.hostname = Some(s.to_string());
        }
        if let Some(desc) = &lldp.system_description
            && let Ok(s) = std::str::from_utf8(desc)
        {
            a.vendor_banner = Some(s.to_string());
        }
        if let Some(caps) = &lldp.capabilities {
            a.capabilities |= lldp_caps_to_asset(caps.system);
        }
        for mgmt in &lldp.management_addresses {
            if let Some(std::net::IpAddr::V4(v4)) = mgmt.ip {
                if !a.ipv4.contains(&v4) {
                    push_bounded(&mut a.ipv4, v4, MAX_IPS_PER_ASSET);
                }
            } else if let Some(std::net::IpAddr::V6(v6)) = mgmt.ip
                && !a.ipv6.contains(&v6)
            {
                push_bounded(&mut a.ipv6, v6, MAX_IPS_PER_ASSET);
            }
        }
        a.seen_via |= AssetSourceSet::LLDP;
        Some(a)
    }
}

#[cfg(feature = "cdp")]
impl Asset {
    /// Build an `Asset` from one parsed CDP message. CDP
    /// doesn't carry a MAC in its TLVs (the SNAP source MAC
    /// is the device); the caller must provide
    /// `source_mac`.
    pub fn from_cdp(cdp: &crate::cdp::CdpMessage, source_mac: MacAddr) -> Self {
        let mut a = Self::new(source_mac);
        if let Some(devid) = &cdp.device_id
            && let Ok(s) = std::str::from_utf8(devid)
        {
            a.hostname = Some(s.to_string());
        }
        if let Some(sw) = &cdp.software_version
            && let Ok(s) = std::str::from_utf8(sw)
        {
            a.vendor_banner = Some(s.to_string());
        }
        if let Some(plat) = &cdp.platform
            && let Ok(s) = std::str::from_utf8(plat)
        {
            a.platform = Some(s.to_string());
        }
        if let Some(caps) = cdp.capabilities {
            a.capabilities |= cdp_caps_to_asset(caps);
        }
        for addr in cdp.management_addresses.iter().chain(cdp.addresses.iter()) {
            match addr.ip {
                Some(std::net::IpAddr::V4(v4)) if !a.ipv4.contains(&v4) => {
                    push_bounded(&mut a.ipv4, v4, MAX_IPS_PER_ASSET);
                }
                Some(std::net::IpAddr::V6(v6)) if !a.ipv6.contains(&v6) => {
                    push_bounded(&mut a.ipv6, v6, MAX_IPS_PER_ASSET);
                }
                _ => {}
            }
        }
        a.seen_via |= AssetSourceSet::CDP;
        a
    }
}

#[cfg(feature = "ssdp")]
impl Asset {
    /// Build an `Asset` from one parsed SSDP message. SSDP
    /// payloads carry no MAC and no IP (the LOCATION URL has
    /// one inside the URL but parsing it is fragile); the
    /// caller provides `source_mac`.
    pub fn from_ssdp(ssdp: &crate::ssdp::SsdpMessage, source_mac: MacAddr) -> Self {
        let mut a = Self::new(source_mac);
        a.vendor_banner = ssdp.server.clone();
        a.capabilities |= AssetCapabilities::UPNP | AssetCapabilities::HOST;
        a.seen_via |= AssetSourceSet::SSDP;
        a
    }
}

#[cfg(feature = "mdns")]
impl Asset {
    /// Build an `Asset` from one parsed mDNS [`crate::dns::DnsResponse`].
    /// mDNS payloads carry no MAC; the caller must provide
    /// `source_mac` (typically the Ethernet source of the
    /// frame that carried the response). Returns `None` when
    /// the response contains nothing inventory-relevant —
    /// no `.local` A/AAAA bindings and no service-discovery
    /// PTRs.
    ///
    /// Pulls:
    ///
    /// - A / AAAA records whose name ends in `.local` →
    ///   `hostname` (set from the leftmost label of the first
    ///   matching record) + ipv4/ipv6 bindings.
    /// - Service-discovery PTR records (RFC 6763) →
    ///   `AssetCapabilities::UPNP | HOST`. The full
    ///   service vocabulary stays on the parsed `ServiceRecord`s
    ///   themselves; the inventory just records the device
    ///   class.
    pub fn from_mdns(resp: &crate::dns::DnsResponse, source_mac: MacAddr) -> Option<Self> {
        let mut a = Self::new(source_mac);
        let mut populated = false;

        // Walk every answer + additional looking for A / AAAA
        // bindings on `.local` names — those are the host's
        // direct address publications.
        let all_records = resp.answers.iter().chain(resp.additionals.iter());
        for rr in all_records {
            // DNS is case-insensitive (RFC 1035 §2.3.3) so
            // the suffix match needs to be too; but hostnames
            // are operationally display-relevant, so keep the
            // original case for the extracted value.
            let original = rr.name.strip_suffix('.').unwrap_or(&rr.name);
            let lower = original.to_ascii_lowercase();
            let Some(_) = lower.strip_suffix(".local") else {
                continue;
            };
            let hostname_orig = &original[..original.len() - ".local".len()];
            // hostname_label may itself contain dots (sub-
            // domain.local); take everything before any dot
            // as the leftmost label.
            let leftmost = hostname_orig.split('.').next().unwrap_or(hostname_orig);
            match &rr.data {
                crate::dns::DnsRdata::A(v4) => {
                    if !a.ipv4.contains(v4) {
                        push_bounded(&mut a.ipv4, *v4, MAX_IPS_PER_ASSET);
                    }
                    if a.hostname.is_none() && !leftmost.is_empty() {
                        a.hostname = Some(leftmost.to_string());
                    }
                    populated = true;
                }
                crate::dns::DnsRdata::AAAA(v6) => {
                    if !a.ipv6.contains(v6) {
                        push_bounded(&mut a.ipv6, *v6, MAX_IPS_PER_ASSET);
                    }
                    if a.hostname.is_none() && !leftmost.is_empty() {
                        a.hostname = Some(leftmost.to_string());
                    }
                    populated = true;
                }
                _ => {}
            }
        }

        // Walk service-discovery PTR records — every one
        // implies UPNP-class IoT capability.
        let services = crate::mdns::extract_services(resp);
        if !services.is_empty() {
            a.capabilities |= AssetCapabilities::UPNP | AssetCapabilities::HOST;
            populated = true;
        } else if populated {
            // Pure A/AAAA contribution — still a host.
            a.capabilities |= AssetCapabilities::HOST;
        }

        if populated {
            a.seen_via |= AssetSourceSet::MDNS;
            Some(a)
        } else {
            None
        }
    }
}

#[cfg(feature = "netbios-ns")]
impl Asset {
    /// Build an `Asset` from one parsed NetBIOS Name Service
    /// message. NBNS payloads carry no L2 — the caller must
    /// supply `source_mac` (the Ethernet source of the frame
    /// that carried the datagram). Returns `None` when the
    /// message has neither a `queried_name` nor any
    /// `answer_addresses` — nothing inventory-relevant.
    ///
    /// Pulls:
    ///
    /// - `queried_name` → `hostname` (the decoded NetBIOS
    ///   name, suffix-stripped).
    /// - Every address in `answer_addresses` → `ipv4`
    ///   bindings.
    /// - `AssetCapabilities::HOST` (NBNS is a station
    ///   protocol).
    /// - `AssetSourceSet::NBNS`.
    pub fn from_netbios_ns(
        nb: &crate::netbios_ns::NbnsMessage,
        source_mac: MacAddr,
    ) -> Option<Self> {
        if nb.queried_name.is_none() && nb.answer_addresses.is_empty() {
            return None;
        }
        let mut a = Self::new(source_mac);
        if let Some(name) = nb.queried_name.as_deref() {
            a.hostname = Some(name.to_string());
        }
        for v4 in nb.answer_addresses.iter() {
            if !a.ipv4.contains(v4) {
                push_bounded(&mut a.ipv4, *v4, MAX_IPS_PER_ASSET);
            }
        }
        a.capabilities |= AssetCapabilities::HOST;
        a.seen_via |= AssetSourceSet::NBNS;
        Some(a)
    }
}

#[cfg(feature = "lldp")]
fn lldp_caps_to_asset(c: crate::lldp::CapabilityBits) -> AssetCapabilities {
    use crate::lldp::CapabilityBits as L;
    let mut out = AssetCapabilities::empty();
    if c.contains(L::BRIDGE) {
        out |= AssetCapabilities::BRIDGE;
    }
    if c.contains(L::ROUTER) {
        out |= AssetCapabilities::ROUTER;
    }
    if c.contains(L::WLAN_AP) {
        out |= AssetCapabilities::WLAN_AP;
    }
    if c.contains(L::TELEPHONE) {
        out |= AssetCapabilities::PHONE;
    }
    if c.contains(L::REPEATER) {
        out |= AssetCapabilities::REPEATER;
    }
    if c.contains(L::DOCSIS_CABLE) {
        out |= AssetCapabilities::DOCSIS_CABLE;
    }
    if c.contains(L::STATION_ONLY) {
        out |= AssetCapabilities::HOST;
    }
    if c.contains(L::C_VLAN) {
        out |= AssetCapabilities::C_VLAN;
    }
    if c.contains(L::S_VLAN) {
        out |= AssetCapabilities::S_VLAN;
    }
    out
}

#[cfg(feature = "cdp")]
fn cdp_caps_to_asset(c: crate::cdp::CdpCapabilities) -> AssetCapabilities {
    use crate::cdp::CdpCapabilities as C;
    let mut out = AssetCapabilities::empty();
    if c.contains(C::ROUTER) {
        out |= AssetCapabilities::ROUTER;
    }
    if c.contains(C::BRIDGE) {
        out |= AssetCapabilities::BRIDGE;
    }
    if c.contains(C::SOURCE_BRIDGE) {
        out |= AssetCapabilities::SOURCE_BRIDGE;
    }
    if c.contains(C::SWITCH) {
        out |= AssetCapabilities::SWITCH;
    }
    if c.contains(C::HOST) {
        out |= AssetCapabilities::HOST;
    }
    if c.contains(C::IGMP) {
        out |= AssetCapabilities::IGMP;
    }
    if c.contains(C::REPEATER) {
        out |= AssetCapabilities::REPEATER;
    }
    if c.contains(C::PHONE) {
        out |= AssetCapabilities::PHONE;
    }
    if c.contains(C::REMOTELY_MANAGED) {
        out |= AssetCapabilities::REMOTELY_MANAGED;
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bare_asset_starts_empty() {
        let a = Asset::new(MacAddr([1, 2, 3, 4, 5, 6]));
        assert_eq!(a.mac, MacAddr([1, 2, 3, 4, 5, 6]));
        assert!(a.ipv4.is_empty());
        assert!(a.ipv6.is_empty());
        assert_eq!(a.seen_via, AssetSourceSet::empty());
    }

    #[test]
    fn merge_unions_capabilities_and_seen_via() {
        let mac = MacAddr([0xaa; 6]);
        let mut a = Asset::new(mac);
        a.capabilities |= AssetCapabilities::HOST;
        a.seen_via |= AssetSourceSet::DHCP;
        let mut b = Asset::new(mac);
        b.capabilities |= AssetCapabilities::ROUTER;
        b.seen_via |= AssetSourceSet::LLDP;
        a.merge(&b);
        assert!(a.capabilities.contains(AssetCapabilities::HOST));
        assert!(a.capabilities.contains(AssetCapabilities::ROUTER));
        assert!(a.seen_via.contains(AssetSourceSet::DHCP));
        assert!(a.seen_via.contains(AssetSourceSet::LLDP));
    }

    #[test]
    fn merge_dedupes_ips_and_caps_at_bound() {
        let mac = MacAddr([0xaa; 6]);
        let mut a = Asset::new(mac);
        a.ipv4 = vec![
            Ipv4Addr::new(10, 0, 0, 1),
            Ipv4Addr::new(10, 0, 0, 2),
            Ipv4Addr::new(10, 0, 0, 3),
        ];
        let mut b = Asset::new(mac);
        b.ipv4 = vec![
            Ipv4Addr::new(10, 0, 0, 2), // dup
            Ipv4Addr::new(10, 0, 0, 4),
            Ipv4Addr::new(10, 0, 0, 5), // pushes past MAX
        ];
        a.merge(&b);
        // De-duped, bounded at MAX_IPS_PER_ASSET = 4.
        assert_eq!(a.ipv4.len(), MAX_IPS_PER_ASSET);
        assert!(
            !a.ipv4.contains(&Ipv4Addr::new(10, 0, 0, 1)),
            "oldest should have been evicted to make room"
        );
        assert!(a.ipv4.contains(&Ipv4Addr::new(10, 0, 0, 5)));
    }

    #[test]
    fn fingerprints_overwrite_on_merge() {
        let mac = MacAddr([0xaa; 6]);
        let mut a = Asset::new(mac);
        a.fingerprints.dhcp = Some("OLD".into());
        let mut b = Asset::new(mac);
        b.fingerprints.dhcp = Some("NEW".into());
        a.merge(&b);
        assert_eq!(a.fingerprints.dhcp.as_deref(), Some("NEW"));
    }

    #[test]
    fn fingerprints_keep_when_other_is_none() {
        let mac = MacAddr([0xaa; 6]);
        let mut a = Asset::new(mac);
        a.fingerprints.dhcp = Some("ORIGINAL".into());
        let b = Asset::new(mac);
        a.merge(&b);
        assert_eq!(a.fingerprints.dhcp.as_deref(), Some("ORIGINAL"));
    }
}