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

Added "vendor" to host response #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::Error;
#[derive(Display, Clone, Debug, PartialEq)]
pub enum Address {
IpAddr(IpAddr),
MacAddr(String),
MacAddr { addr: String, vendor: String },
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -97,8 +97,15 @@ fn parse_address_node(node: Node) -> Result<Address, Error> {
.attribute("addr")
.ok_or_else(|| Error::from("expected `addr` attribute in `address` node"))?;

let vendor = node
.attribute("vendor")
.unwrap_or_default();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this approach. If the vendor field is always present, we should just use .expect() here with the expectation that it should never fail. If the vendor field is option, it should be an Option<String>.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the vendor field isnt always present on nmap output..
image
Check this..
And for this i set the vendor field to an empty string if the field is not found..

what do you think about it?

Ric

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, Option<String> is the perfect type for that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, Option<String> is the perfect type for that.

Ok thanks, i'll try to fix it.. :)


match addrtype {
"mac" => Ok(Address::MacAddr(addr.to_string())),
"mac" => Ok(Address::MacAddr {
addr: addr.to_string(),
vendor: vendor.to_string(),
}),
_ => {
let a = addr
.parse::<IpAddr>()
Expand Down
18 changes: 15 additions & 3 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ fn host_ip_address() {
let ip_addr = host.addresses().next().unwrap();
match ip_addr {
host::Address::IpAddr(s) => assert_eq!(s, &ip),
host::Address::MacAddr(_) => assert!(false),
host::Address::MacAddr { addr, vendor } => {
assert!(!addr.is_empty()); // Verifica che l'indirizzo non sia vuoto
assert!(!vendor.is_empty()); // Verifica che il fornitore non sia vuoto
},
}
}

Expand Down Expand Up @@ -191,6 +194,7 @@ fn host_portinfo_ports() {
fn test_issue_one() {
let ip: std::net::IpAddr = "192.168.59.138".parse().unwrap();
let mac = "00:0C:29:71:23:2B".to_string();
let ven = "VMware".to_string();

let host = NMAP_ISSUE_ONE.hosts().next().unwrap();
assert!(host.addresses().count() == 2);
Expand All @@ -201,14 +205,22 @@ fn test_issue_one() {
println!("{:?}", ip_addr);
match ip_addr {
host::Address::IpAddr(s) => assert_eq!(s, &ip),
host::Address::MacAddr(_) => assert!(false),
host::Address::MacAddr { addr, vendor } => {
assert!(!addr.is_empty());
assert!(!vendor.is_empty());
}
}

let mac_addr = addresses.next().unwrap();
println!("{:?}", mac_addr);
match mac_addr {
host::Address::IpAddr(_) => assert!(false),
host::Address::MacAddr(s) => assert_eq!(s, &mac),
//host::Address::MacAddr(s) => assert_eq!(s, &mac),
host::Address::MacAddr { addr, vendor } => {
assert_eq!(addr, &mac);
assert_eq!(vendor, &ven);

}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<verbose level="0"/>
<debugging level="0"/>
<host starttime="1588318812" endtime="1588318814"><status state="up" reason="echo-reply" reason_ttl="53"/>
<address addr="45.33.32.156" addrtype="ipv4"/>
<address addr="45.33.32.156" addrtype="ipv4" vendor="VMware"/>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very clearly wrong as addrtype="ipv4". tests/issue_1.xml has a valid example of a addrtype="mac".

<hostnames>
<hostname name="scanme.nmap.org" type="user"/>
<hostname name="scanme.nmap.org" type="PTR"/>
Expand Down