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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use priv_prelude::*;

/// An ARP packet
#[derive(Clone, PartialEq)]
pub struct ArpPacket {
    buffer: Bytes,
}

impl fmt::Debug for ArpPacket {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.fields() {
            ArpFields::Request { .. } => {
                f
                .debug_struct("ArpPacket::Request")
                .field("source_mac", &self.source_mac())
                .field("source_ip", &self.source_ip())
                .field("dest_mac", &self.dest_mac())
                .field("dest_ip", &self.dest_ip())
                .finish()
            },
            ArpFields::Response { .. } => {
                f
                .debug_struct("ArpPacket::Response")
                .field("source_mac", &self.source_mac())
                .field("source_ip", &self.source_ip())
                .field("dest_mac", &self.dest_mac())
                .field("dest_ip", &self.dest_ip())
                .finish()
            },
        }
    }
}

/// The fields of an ARP packet.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ArpFields {
    /// An ARP request
    Request {
        /// The MAC address of the sender.
        source_mac: MacAddr,
        /// The Ipv4 address of the sender.
        source_ip: Ipv4Addr,
        /// The Ipv4 address of the peer whose MAC address we are requesting.
        dest_ip: Ipv4Addr,
    },
    /// An ARP response
    Response {
        /// The MAC address of the sender. 
        source_mac: MacAddr,
        /// The Ipv4 address of the sender.
        source_ip: Ipv4Addr,
        /// The MAC address of the receiver.
        dest_mac: MacAddr,
        /// The Ipv4 address of the receiver.
        dest_ip: Ipv4Addr,
    },
}

fn set_fields(buffer: &mut [u8], fields: ArpFields) {
    buffer[0..6].clone_from_slice(&[
        0x00, 0x01,
        0x08, 0x00,
        0x06, 0x04,
    ]);
    match fields {
        ArpFields::Request {
            source_mac,
            source_ip,
            dest_ip,
        } => {
            buffer[6..8].clone_from_slice(&[0x00, 0x01]);
            buffer[8..14].clone_from_slice(source_mac.as_bytes());
            buffer[14..18].clone_from_slice(&source_ip.octets());
            buffer[18..24].clone_from_slice(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
            buffer[24..28].clone_from_slice(&dest_ip.octets());
        },
        ArpFields::Response {
            source_mac,
            source_ip,
            dest_mac,
            dest_ip,
        } => {
            buffer[6..8].clone_from_slice(&[0x00, 0x02]);
            buffer[8..14].clone_from_slice(source_mac.as_bytes());
            buffer[14..18].clone_from_slice(&source_ip.octets());
            buffer[18..24].clone_from_slice(dest_mac.as_bytes());
            buffer[24..28].clone_from_slice(&dest_ip.octets());
        },
    }
}

impl ArpPacket {
    /// Create a new `ArpPacket` given the description provided by `fields`.
    pub fn new_from_fields(fields: ArpFields) -> ArpPacket {
        let mut buffer = unsafe { BytesMut::uninit(28) };
        set_fields(&mut buffer, fields);
        ArpPacket {
            buffer: buffer.freeze(),
        }
    }

    /// Write an ARP packet described by `fields` to the given buffer.
    pub fn write_to_buffer(
        buffer: &mut [u8],
        fields: ArpFields,
    ) {
        set_fields(buffer, fields);
    }

    /// Parse an ARP packet from the given buffer.
    pub fn from_bytes(buffer: Bytes) -> ArpPacket {
        ArpPacket {
            buffer,
        }
    }

    /// Parse the ARP packet into an `ArpFields`.
    pub fn fields(&self) -> ArpFields {
        match NetworkEndian::read_u16(&self.buffer[6..8]) {
            0x0001 => ArpFields::Request {
                source_mac: self.source_mac(),
                source_ip: self.source_ip(),
                dest_ip: self.dest_ip(),
            },
            0x0002 => ArpFields::Response {
                source_mac: self.source_mac(),
                source_ip: self.source_ip(),
                dest_mac: self.dest_mac(),
                dest_ip: self.dest_ip(),
            },
            x => panic!("unexpected ARP operation type (0x{:04x})", x),
        }
    }

    /// Get the MAC address of the sender.
    pub fn source_mac(&self) -> MacAddr {
        MacAddr::from_bytes(&self.buffer[8..14])
    }

    /// Get the IP address of the sender.
    pub fn source_ip(&self) -> Ipv4Addr {
        Ipv4Addr::from(slice_assert_len!(4, &self.buffer[14..18]))
    }

    /// Get the MAC address of the destination.
    pub fn dest_mac(&self) -> MacAddr {
        MacAddr::from_bytes(&self.buffer[18..24])
    }

    /// Get the IP address of the destination.
    pub fn dest_ip(&self) -> Ipv4Addr {
        Ipv4Addr::from(slice_assert_len!(4, &self.buffer[24..28]))
    }

    /// Return the underlying byte buffer of this packet.
    pub fn as_bytes(&self) -> &Bytes {
        &self.buffer
    }
}