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
mod association_request;
mod association_response;
mod authentication;
mod beacon;
mod builder;
mod deauthentication;
mod disassociate;
mod probe_request;
mod probe_response;
mod tagged_parameters;

pub use self::{
    association_request::*, association_response::*, authentication::*, beacon::*, builder::*,
    deauthentication::*, disassociate::*, probe_request::*, probe_response::*,
    tagged_parameters::*,
};
use super::*;
use std::borrow::Cow;

pub struct ManagementFrame<'a> {
    bytes: Cow<'a, [u8]>,
}

pub enum ManagementFrameLayer<'a> {
    Beacon(BeaconFrame<'a>),
    ProbeRequest(ProbeRequestFrame<'a>),
    ProbeResponse(ProbeResponseFrame<'a>),
    Authentication(AuthenticationFrame<'a>),
    Deauthentication(DeauthenticationFrame<'a>),
    Disassociate(DisassociateFrame<'a>),
    AssociationRequest(AssociationRequestFrame<'a>),
    AssociationResponse(AssociationResponseFrame<'a>),
}

impl<'a> ManagementFrame<'a> {
    pub fn new<T: Into<Cow<'a, [u8]>>>(bytes: T) -> Self {
        Self {
            bytes: bytes.into(),
        }
    }

    pub fn next_layer(&self) -> Option<ManagementFrameLayer<'_>> {
        match self.subtype() {
            FrameSubtype::Management(subtype) => match subtype {
                ManagementSubtype::Beacon => {
                    Some(ManagementFrameLayer::Beacon(BeaconFrame::new(self.bytes())))
                }
                ManagementSubtype::ProbeRequest => Some(ManagementFrameLayer::ProbeRequest(
                    ProbeRequestFrame::new(self.bytes()),
                )),
                ManagementSubtype::ProbeResponse => Some(ManagementFrameLayer::ProbeResponse(
                    ProbeResponseFrame::new(self.bytes()),
                )),
                ManagementSubtype::Authentication => Some(ManagementFrameLayer::Authentication(
                    AuthenticationFrame::new(self.bytes()),
                )),
                ManagementSubtype::Deauthentication => {
                    Some(ManagementFrameLayer::Deauthentication(
                        DeauthenticationFrame::new(self.bytes()),
                    ))
                }
                ManagementSubtype::Disassociate => Some(ManagementFrameLayer::Disassociate(
                    DisassociateFrame::new(self.bytes()),
                )),
                ManagementSubtype::AssociationRequest => {
                    Some(ManagementFrameLayer::AssociationRequest(
                        AssociationRequestFrame::new(self.bytes()),
                    ))
                }
                ManagementSubtype::AssociationResponse => {
                    Some(ManagementFrameLayer::AssociationResponse(
                        AssociationResponseFrame::new(self.bytes()),
                    ))
                }
                _ => None,
            },
            _ => unreachable!(),
        }
    }
}

impl FrameTrait for ManagementFrame<'_> {
    fn bytes(&self) -> &[u8] {
        self.bytes.as_ref()
    }
}
impl FragmentSequenceTrait for ManagementFrame<'_> {}
impl ManagementFrameTrait for ManagementFrame<'_> {}

pub trait ManagementFrameTrait: FrameTrait + FragmentSequenceTrait {
    fn addr2(&self) -> MacAddress {
        MacAddress::from_bytes(&self.bytes()[10..16]).unwrap()
    }
    fn addr3(&self) -> MacAddress {
        MacAddress::from_bytes(&self.bytes()[16..22]).unwrap()
    }

    /// Transmitter Address
    /// Who this packet came from wirelessly.
    fn transmitter_address(&self) -> Option<MacAddress> {
        Some(self.addr2())
    }

    /// Source Address
    /// Who the packet came from.
    fn source_address(&self) -> Option<MacAddress> {
        self.transmitter_address()
    }

    /// Basic Service Set Address (BSSID)
    /// Basic Service Set ID for Multicast.
    fn bssid_address(&self) -> Option<MacAddress> {
        Some(self.addr3())
    }

    /// Station Address
    fn station_address(&self) -> Option<MacAddress> {
        None
    }
}