use crate::network::types::{NetBiosInfo, NetBiosOpcode, NetBiosService};
const MIN_NBNS_SIZE: usize = 12;
const MIN_NBDGM_SIZE: usize = 14;
pub fn analyze_netbios_ns(payload: &[u8]) -> Option<NetBiosInfo> {
if payload.len() < MIN_NBNS_SIZE {
return None;
}
let flags = u16::from_be_bytes([payload[2], payload[3]]);
let opcode_value = ((flags >> 11) & 0x0F) as u8;
let is_response = (flags & 0x8000) != 0;
let opcode = if is_response {
NetBiosOpcode::Response
} else {
parse_opcode(opcode_value)
};
let name = if payload.len() > 12 {
decode_netbios_name(&payload[12..])
} else {
None
};
Some(NetBiosInfo {
service: NetBiosService::NameService,
opcode,
name,
})
}
pub fn analyze_netbios_dgm(payload: &[u8]) -> Option<NetBiosInfo> {
if payload.len() < MIN_NBDGM_SIZE {
return None;
}
let msg_type = payload[0];
let opcode = match msg_type {
0x10 => NetBiosOpcode::Query, 0x11 => NetBiosOpcode::Query, 0x12 => NetBiosOpcode::Registration, 0x13 => NetBiosOpcode::Query, 0x14 => NetBiosOpcode::Query, 0x15 => NetBiosOpcode::Response, 0x16 => NetBiosOpcode::Response, _ => NetBiosOpcode::Unknown(msg_type),
};
let name = if payload.len() > 14 {
decode_netbios_name(&payload[14..])
} else {
None
};
Some(NetBiosInfo {
service: NetBiosService::DatagramService,
opcode,
name,
})
}
fn parse_opcode(value: u8) -> NetBiosOpcode {
match value {
0 => NetBiosOpcode::Query,
5 => NetBiosOpcode::Registration,
6 => NetBiosOpcode::Release,
7 => NetBiosOpcode::Wack,
8 => NetBiosOpcode::Refresh,
other => NetBiosOpcode::Unknown(other),
}
}
fn decode_netbios_name(data: &[u8]) -> Option<String> {
if data.is_empty() {
return None;
}
let name_len = data[0] as usize;
if name_len != 32 || data.len() < 33 {
return None;
}
let mut name = String::with_capacity(16);
for i in 0..16 {
let idx = 1 + i * 2;
if idx + 1 >= data.len() {
break;
}
let hi = data[idx];
let lo = data[idx + 1];
if !(b'A'..=b'P').contains(&hi) || !(b'A'..=b'P').contains(&lo) {
return None;
}
let hi_nibble = (hi - b'A') << 4;
let lo_nibble = lo - b'A';
let c = hi_nibble | lo_nibble;
if c.is_ascii_graphic() {
name.push(c as char);
}
}
if name.is_empty() { None } else { Some(name) }
}
#[cfg(test)]
mod tests {
use super::*;
fn encode_netbios_name(name: &str) -> Vec<u8> {
let mut encoded = Vec::with_capacity(33);
encoded.push(32);
let padded: Vec<u8> = name
.bytes()
.chain(std::iter::repeat(0x20))
.take(16)
.collect();
for &b in &padded {
encoded.push(b'A' + ((b >> 4) & 0x0F));
encoded.push(b'A' + (b & 0x0F));
}
encoded
}
fn build_nbns_query(name: &str) -> Vec<u8> {
let mut packet = Vec::new();
packet.extend_from_slice(&[0x00, 0x01]);
packet.extend_from_slice(&[0x01, 0x10]);
packet.extend_from_slice(&[0x00, 0x01]);
packet.extend_from_slice(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
packet.extend_from_slice(&encode_netbios_name(name));
packet.push(0x00);
packet.extend_from_slice(&[0x00, 0x20, 0x00, 0x01]);
packet
}
fn build_nbns_response(name: &str) -> Vec<u8> {
let mut packet = Vec::new();
packet.extend_from_slice(&[0x00, 0x01]);
packet.extend_from_slice(&[0x85, 0x00]);
packet.extend_from_slice(&[0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]);
packet.extend_from_slice(&encode_netbios_name(name));
packet
}
#[test]
fn test_nbns_query() {
let packet = build_nbns_query("WORKSTATION");
let info = analyze_netbios_ns(&packet).expect("should parse");
assert_eq!(info.service, NetBiosService::NameService);
assert_eq!(info.opcode, NetBiosOpcode::Query);
assert_eq!(info.name, Some("WORKSTATION".to_string()));
}
#[test]
fn test_nbns_response() {
let packet = build_nbns_response("FILESERVER");
let info = analyze_netbios_ns(&packet).expect("should parse");
assert_eq!(info.service, NetBiosService::NameService);
assert_eq!(info.opcode, NetBiosOpcode::Response);
assert_eq!(info.name, Some("FILESERVER".to_string()));
}
#[test]
fn test_nbns_too_short() {
let packet = [0u8; 5];
assert!(analyze_netbios_ns(&packet).is_none());
}
#[test]
fn test_nbdgm_direct() {
let mut packet = vec![0u8; 20];
packet[0] = 0x10; let info = analyze_netbios_dgm(&packet).expect("should parse");
assert_eq!(info.service, NetBiosService::DatagramService);
assert_eq!(info.opcode, NetBiosOpcode::Query);
}
#[test]
fn test_nbdgm_too_short() {
let packet = [0u8; 5];
assert!(analyze_netbios_dgm(&packet).is_none());
}
#[test]
fn test_decode_netbios_name() {
let encoded = encode_netbios_name("TEST");
let name = decode_netbios_name(&encoded).expect("should decode");
assert_eq!(name, "TEST");
}
#[test]
fn test_decode_netbios_name_with_padding() {
let encoded = encode_netbios_name("PC");
let name = decode_netbios_name(&encoded).expect("should decode");
assert_eq!(name, "PC");
}
#[test]
fn test_decode_netbios_name_strips_service_type_suffix() {
let mut encoded = Vec::with_capacity(33);
encoded.push(32);
let padded = b"WORKSTATION "; for &b in padded {
encoded.push(b'A' + ((b >> 4) & 0x0F));
encoded.push(b'A' + (b & 0x0F));
}
encoded.push(b'A');
encoded.push(b'A');
let decoded = decode_netbios_name(&encoded).expect("should decode");
assert_eq!(decoded, "WORKSTATION");
}
}