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

ch8/ch8-mac: Clear Unicast/Multicast bit #101

Open
wants to merge 1 commit into
base: 1st-edition
Choose a base branch
from
Open
Changes from all commits
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
12 changes: 10 additions & 2 deletions ch8/ch8-mac/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ impl Display for MacAddress {
}
}

// MAC address format summary:
// https://en.wikipedia.org/wiki/MAC_address#Address_details
//
// 1st Octet:
// -- bit 0 (LSB): 0 = unicast / 1 = multicast
// -- bit 1: 0 = globally unique OUI / 1 = locally assigned
//
impl MacAddress {
fn new() -> MacAddress {
let mut octets: [u8; 6] = [0; 6];
rand::thread_rng().fill_bytes(&mut octets);
octets[0] |= 0b_0000_0011; // <3>
octets[0] |= 0b_0000_0010; // <3>
octets[0] &= 0b_1111_1110; // <3>
MacAddress { 0: octets }
}

Expand All @@ -32,7 +40,7 @@ impl MacAddress {
}

fn is_unicast(&self) -> bool {
(self.0[0] & 0b_0000_0001) == 0b_0000_0001
(self.0[0] & 0b_0000_0001) == 0b_0000_0000
}
}

Expand Down