I am trying to implement the ARP protocol by myself, but I'm not sure how it should behave, how I should craft an ARP request. How does it work and why it should work..

I tried to research this on the internet but nothing really helped me. I want to create an ARP_table like this:

typedef struct arp_table_entry {
    uint8_t shwaddr[ETH_ALEN]; /* sender hardware address. */
    uint8_t sipaddr[IPv4LEN];  /* sender ip address. */
    uint8_t dhwaddr[ETH_ALEN]; /* destination hardware address. */
    uint8_t dipaddr[IPv4LEN];  /* destination ip address. */
} arp_table;

But I don't know what kind of packet I should receive or what I should send or even how. Also if you could give me some information about where I could find some good definitions on: what is an interface, what is a port, what is an IP and why do we need it, things about networking in general..

  • 1
    Also, off-topic for a programming question. ARP is defined in RFC 826.
    – Zac67
    Mar 21 '20 at 11:14
  • Homework questions are also off topic here.
    – Ron Trunk
    Mar 21 '20 at 11:49

The data structure in your original post might be misnamed -- is it what you want to use for your arp table, or for composing ARP packets to transmit onto the LAN?

ARP packets have a header identifying some needed attributes: ARP packet format

  • Hardware Address Space is 1 for Ethernet.
  • Protocol Address Space is 0x0800 for IPv4 over Ethernet (the same as IPv4's ethertype.)
  • Hardware & Protocol Address Lengths are 6 and 4, respectively.
  • OpCode is 1 for an ARP who-has (request) and 2 for is-at (reply)
  • Following the above fields are the fields you identified in your C structure

If you are indeed asking about a structure for an ARP table, rather than composing ARP packets, you can look at the one on your own PC for some hints. On my Mac, we have the following fields:

  • IP address
  • MAC address
  • Interface (many devices, such as routers, have multiple ports/interfaces)
  • Age or Scheduled Expiration Time

The command-line tool represents the entries like this:

? ( at 1c:42:2c:80:d3:80 on en0 ifscope [ethernet]
? ( at 9c:ae:42:eb:80:16 on en0 ifscope [ethernet]
? ( at (incomplete) on en0 ifscope [ethernet]
? ( at d0:4d:42:3f:80:85 on en0 ifscope [ethernet]
  • Hmm, I understand my misconception now, how should I craft such an ARP request to send it?
    – C. Cristi
    Mar 21 '20 at 15:12
  • You can use a C structure like in your original post. Just add the missing fields. Mar 21 '20 at 15:34
  • Yeah but how do I really receive the packets? also why do we need the field with the target mac address if ARP is a protocol to resolve the mac address?
    – C. Cristi
    Mar 21 '20 at 15:43
  • How you receive the packets depends on your environment. The operating system implements ARP, but if you want to learn how it works, Linux has a SOCK_RAW socket which can be configured for protocol ETH_P_ARP to listen for only ARP traffic. With regards to the target MAC address, the ARP RFC will answer this question, and others. Mar 21 '20 at 16:00
  • ARP maps IPv4 addresses (protocol address) to MAC addresses (hardware address). You don't need any distinction between source and destination.
    – Zac67
    Mar 21 '20 at 16:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.