1#  Copyright (C) 2024 The Android Open Source Project
2#
3#  Licensed under the Apache License, Version 2.0 (the "License");
4#  you may not use this file except in compliance with the License.
5#  You may obtain a copy of the License at
6#
7#       http://www.apache.org/licenses/LICENSE-2.0
8#
9#  Unless required by applicable law or agreed to in writing, software
10#  distributed under the License is distributed on an "AS IS" BASIS,
11#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12#  See the License for the specific language governing permissions and
13#  limitations under the License.
14from ipaddress import IPv4Address
15from socket import inet_aton
16
17ETHER_BROADCAST_MAC_ADDRESS = "FF:FF:FF:FF:FF:FF"
18ARP_REQUEST_OP = 1
19ARP_REPLY_OP = 2
20
21"""
22This variable defines a template for constructing ARP packets in hexadecimal format.
23It's used to provide the common fields for ARP packet, and replaced needed fields when constructing
24"""
25ARP_TEMPLATE = (
26    # Ether Header (14 bytes)
27    "{dst_mac}" + # DA
28    "{src_mac}" + # SA
29    "0806" + # ARP
30    # ARP Header (28 bytes)
31    "0001" + # Hardware type (Ethernet)
32    "0800" + # Protocol type (IPv4)
33    "06" + # hardware address length
34    "04" + # protocol address length
35    "{opcode}" + # opcode
36    "{sender_mac}" + # sender MAC
37    "{sender_ip}" + # sender IP
38    "{target_mac}" + # target MAC
39    "{target_ip}" # target IP
40)
41
42def construct_arp_packet(src_mac, dst_mac, src_ip, dst_ip, op) -> str:
43    """Constructs an ARP packet as a hexadecimal string.
44
45    This function creates an ARP packet by filling in the required fields
46    in a predefined ARP packet template.
47
48    Args:
49    src_mac: The MAC address of the sender. (e.g. "11:22:33:44:55:66")
50    dst_mac: The MAC address of the recipient. (e.g. "aa:bb:cc:dd:ee:ff")
51    src_ip: The IP address of the sender. (e.g. "1.1.1.1")
52    dst_ip: The IP address of the target machine. (e.g. "2.2.2.2")
53    op: The op code of the ARP packet, refer to ARP_*_OP
54
55    Returns:
56    A string representing the ARP packet in hexadecimal format.
57    """
58    # Replace the needed fields from packet template
59    arp_pkt = ARP_TEMPLATE.format(
60            dst_mac=dst_mac.replace(":",""),
61            src_mac=src_mac.replace(":",""),
62            opcode=str(op).rjust(4, "0"),
63            sender_mac=src_mac.replace(":",""),
64            sender_ip=inet_aton(src_ip).hex(),
65            target_mac=("000000000000" if op == ARP_REQUEST_OP else dst_mac.replace(":", "")),
66            target_ip=inet_aton(dst_ip).hex()
67    )
68
69    # always convert to upper case hex string
70    return arp_pkt.upper()