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

Makes Endianness derive from Debug, PartialEq, Eq, Clone and Copy. #492

Merged
merged 1 commit into from
May 8, 2017
Merged

Makes Endianness derive from Debug, PartialEq, Eq, Clone and Copy. #492

merged 1 commit into from
May 8, 2017

Conversation

ithinuel
Copy link
Contributor

@ithinuel ithinuel commented Apr 9, 2017

Enables us to also use nom to detect endianness and use it in various ways.
Debug allows us to return Endianness and compare it in assert_eq!.

named_args!(parse_header_e(e: Endianness, nsec: bool)<(Endianness, Header)>, tuple!(
    value!(e),
    do_parse!(
        major: u16!(e) >>
        minor: u16!(e) >>
        this_zone: i32!(e) >>
        sigfigs: u32!(e) >>
        snaplen: u32!(e) >>
        network: u32!(e) >>
        (Header {
            major: major,
            minor: minor,
            this_zone: this_zone,
            sigfigs: sigfigs,
            snaplen: snaplen,
            network: network,
            nano_sec: nsec
        })
    )
));

named!(parse_header<(Endianness, Header)>, switch!(be_u32,
    0xa1b2c3d4 => call!(parse_header_e, Endianness::Big, false)    | 
    0xd4c3b2a1 => call!(parse_header_e, Endianness::Little, false) 
));


#[cfg(test)]
mod tests {
    use nom;
    use super::*;
    #[test]
    fn parse_header_le_nsec() {
        let i = b"\x4d\x3c\xb2\xa1\x02\x00\x04\x00\xFF\xFF\xFF\xFF\
                  \x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x0a";
        let h = Header {
            major: 2,
            minor: 4,
            this_zone: -1,
            sigfigs: 1,
            snaplen: 1,
            network: 1,
            nano_sec: true
        };
        assert_eq!(parse_header(&i[..]), nom::IResult::Done(&[10][..], (Endianness::Little, h)));
    }
}

@ithinuel ithinuel mentioned this pull request Apr 14, 2017
59 tasks
@Geal
Copy link
Collaborator

Geal commented May 8, 2017

Thanks!

@Geal Geal merged commit d2642df into rust-bakery:master May 8, 2017
@Geal
Copy link
Collaborator

Geal commented May 8, 2017

welp, looks like I thanked you but forgot to merge it :p

@ithinuel
Copy link
Contributor Author

woops, I'm very late on this one !
pcap-rs is now on crates.io : https://crates.io/crates/pcap-rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants