Posted in

How to create ARP requests with Tins?

If you’ve ever dug into network programming, you know how critical Address Resolution Protocol (ARP) is—its job is to map a device’s IP address to a physical MAC address on a local network, and that mapping is the backbone of every Ethernet communication. As a Tins supplier, I’ve spent years walking developers, network engineers, and hobbyists through building custom network tools, and one of the most common (and most useful) tasks we get asked about is crafting ARP requests from scratch. Tins, a modern C++ network packet crafting library, makes this process way more straightforward than working with raw sockets or manual header manipulation, but there’s still a handful of best practices that can save you hours of debugging. Let’s walk through exactly how to create ARP requests with Tins, share the lessons I’ve learned supporting hundreds of teams, and break down why Tins is the go-to tool for this work. Tins

First, let’s get clear on what an ARP request actually is, because skipping the basics leads to messy code and broken network behavior. An ARP request is a broadcast packet that asks, “Who has this IP address? Tell me your MAC address.” Unlike ARP replies, which are unicast, requests go out to every device on the local subnet, so their destination MAC is always the broadcast address: FF:FF:FF:FF:FF:FF. The core structure of an ARP packet is split into two main parts: the Ethernet II header (the layer 2 wrapper that carries the ARP data) and the ARP payload itself. The ARP payload includes fields like hardware type (for Ethernet, that’s 1), protocol type (IPv4 is 0x0800), hardware address length (6 bytes for MACs), protocol address length (4 bytes for IPv4), opcode (1 for requests, 2 for replies), sender MAC, sender IP, target MAC, and target IP. Getting these fields right is non-negotiable—even a single wrong byte can make your request ignored by every device on the network.

Tins simplifies this entire process by providing pre-built classes for each network layer, so you don’t have to manually calculate offsets or handle byte order (little-endian vs big-endian) on your own. Byte order is a common pitfall for new network programmers; Tins abstracts all of that away, so you can work with native integer types instead of flipping bits manually. Let’s start with the basic steps to create an ARP request, using a real-world example that I’ve seen dozens of teams adapt for their own tools, from network scanners to device discovery utilities.

First, you need to set up your network interface. Tins uses interface names, like eth0 on Linux or en0 on macOS, and requires you to open a layer 2 socket to send packets. That’s different from most networking libraries that use layer 3 sockets, because working at layer 2 lets you craft the entire packet, including the Ethernet header and ARP payload, instead of just sending data to an IP address. To open the interface, you’ll use the Sniffer and PacketSender classes from Tins—wait, actually, for sending packets directly, the PacketSender is exactly what we need. Let’s outline the core code structure, and I’ll explain each part so it makes sense, not just like a random code snippet.

First, you include the necessary Tins headers: tins/arp.h for the ARP packet, tins/ethernetII.h for the Ethernet header, and tins/packet_sender.h to send the finished packet. Then, you define your local network details: your MAC address, your IPv4 address, and the target IP address you want to resolve. For example, if your device is on 192.168.1.0/24 and you want to find the MAC address for 192.168.1.1, those variables would look like:

// Local host details
HWAddress<Eth::addr_type> src_mac("00:11:22:33:44:55");
IPv4Address src_ip("192.168.1.100");

// Target IP we want to resolve
IPv4Address target_ip("192.168.1.1");

Next, you create the ARP packet itself. Tins’s ARP class has a constructor that lets you set all the required fields in one go, which is way easier than building each field individually. For an ARP request, the opcode is ARP::OP_REQUEST, which is Tins’s enum for the request opcode (no more memorizing that magic number 1). You set the sender hardware and protocol addresses, then the target hardware address—wait, for ARP requests, the target hardware address is usually all zeros, because we don’t know it yet, right? That’s another common mistake I see: people set the target MAC to FF:FF… or their own MAC, which breaks the request. The correct value is a zeroed MAC address, which Tins makes easy with HWAddress<Eth::addr_type>(), which creates an empty, zeroed MAC.

So the ARP packet line looks like this:

ARP arp_packet(ARP::OP_REQUEST, src_mac, src_ip, HWAddress<Eth::addr_type>(), target_ip);

Then, you wrap that ARP packet in an Ethernet II header, because ARP packets can’t exist on their own—they need to be carried over Ethernet. The Ethernet header has a source MAC (your device’s MAC), a destination MAC (the broadcast address, FF:FF:FF:FF:FF:FF, which Tins has a predefined constant for: Eth::BROADCAST), and the type of payload, which is 0x0800 for IPv4, but wait—no, for ARP packets, the EtherType is 0x0806, which is ARP. Tins has a constant for that too: Eth::ARP. So building the Ethernet header is:

EthernetII eth_header = EthernetII(src_mac, Eth::BROADCAST) / arp_packet;

That / operator in Tins is called a “pdu” (protocol data unit) stack operator—you’re stacking the ARP PDU on top of the Ethernet PDU, which is exactly how network packets are structured, layer by layer. It’s a clean, intuitive way to build packets without having to track each layer manually.

Now, you need to send this packet out over your network interface. That’s where Tins’s PacketSender comes in. You initialize it with the name of your network interface, like “eth0” or “en0”, then send the packet with the send method. The full line for that is:

PacketSender sender("eth0");
sender.send(eth_header);

Wait, that’s it? That’s the core of creating and sending an ARP request with Tins. But there are a few critical additions that make this code work reliably, not just in a test environment. For example, you need to make sure you have the right permissions: on Linux, you need to run the program as root (or with CAP_NET_RAW), and on macOS, you need to grant network permissions to the binary. Also, you might want to add a timeout if you’re expecting an ARP reply, right? Because sending a request is useless if you don’t wait for the device to respond. Tins’s Sniffer class can help here—you can set up a sniffer on the same interface, filter for ARP replies targeted at your MAC, and collect the response.

Let’s adjust that example to include receiving the reply, because that’s what most people actually want to do with an ARP request. After sending the request, we’ll set up a sniffer with a filter that only captures ARP packets where the target IP is our source IP and the opcode is a reply. The Berkeley Packet Filter (BPF) filter for that would be “arp and dst host 192.168.1.100 and arp[6:2] == 2”, which translates to: capture ARP packets, destined for our IP, with an opcode of 2 (reply). Then we set a timeout of, say, 5 seconds, so the sniffer doesn’t hang forever waiting for a reply.

Adding that in, the code becomes:

// Set up sniffer to capture ARP replies
Sniffer sniffer("eth0", Sniffer::Mode::SNIFF, 0);
sniffer.set_filter("arp and dst host 192.168.1.100 and arp[6:2] == 2");

// Start sniffing, wait for 5 seconds
auto replies = sniffer.sniff<1>(5000); // 5000 ms timeout

if (replies) {
    for (auto &pkt : *replies) {
        ARP &arp = pkt.rfind_pdu<ARP>();
        std::cout << "Target MAC address: " << arp.sender_addr() << std::endl;
    }
} else {
    std::cout << "No ARP reply received within timeout" << std::endl;
}

This is the exact workflow I’ve guided developers through again and again. But let’s talk about common mistakes I see when people first start using Tins for ARP requests, because fixing these is where a lot of frustration comes from. First, mixing up the ARP opcode: if you set it to OP_REPLY instead of OP_REQUEST, you’re sending a reply, not a request, which is not what you want here. Tins makes this easy to avoid because the opcodes are clearly named, but it’s still a common typo. Second, setting the target MAC address in the ARP packet to something other than zero. I’ve seen people use the broadcast MAC or their own MAC, and that makes the ARP request unroutable on the local network. The target MAC should always be zero for requests, because you don’t know it yet—you only fill it in when you receive a reply.

Another mistake is using the wrong Ethernet type. If you set the Ethernet header’s type to Eth::IP, which is 0x0800, that means the payload is an IPv4 packet, not ARP, so every switch and router will drop the packet. Always use Eth::ARP for ARP packets, which is 0x0806. Third, interface issues: using the wrong interface name, or not granting enough permissions. I once had a customer who spent two days troubleshooting their ARP requests, only to realize they were using wlan0 (their Wi-Fi interface) instead of eth0 (wired), and Wi-Fi interfaces often block raw ARP requests for security. That’s a quick fix, but it’s easy to miss when you’re deep in code.

Now, why use Tins for this instead of other tools like libpcap directly? Let’s be clear: libpcap is powerful, but it requires manual handling of every header, byte order, and field, which is error-prone. Tins wraps libpcap and other packet capture libraries in a C++ API that’s designed to be intuitive for network programmers, whether you’re a seasoned engineer or a hobbyist building your first network tool. For ARP requests specifically, Tins eliminates the need to calculate header offsets, handle hardware protocol types, or worry about endianness—all of that is abstracted away, so you can focus on what you’re trying to build, not the nitty-gritty of packet formatting.

As a Tins supplier, I also want to mention the support resources that come with using Tins, because building tools that work reliably requires more than just a library. We provide documentation tailored to ARP and other common protocols, example projects that include working ARP scanners, and troubleshooting guides for the most common issues I mentioned earlier. We’ve also built pre-built modules for common use cases, like ARP spoofing detectors (though we always emphasize ethical use) and device discovery tools, so teams don’t have to start from scratch when building their own ARP-based applications.

If you’re looking to scale this work beyond a simple request-reply—say, building an ARP cache poisoner, a network scanner that sweeps an entire subnet, or a tool that monitors ARP activity for security—Tins has you covered. The API supports customizing every field in the ARP packet, including setting custom hardware types, protocol types, or even adding padding if needed (though most modern networks handle that automatically). It also works with both IPv4 and IPv6, though ARP is specific to IPv4—for IPv6, you’d use Neighbor Discovery Protocol (NDP), which Tins also supports, but that’s a topic for another blog.

Let’s also talk about testing your ARP requests. Once you’ve built your code, you can use tools like Wireshark to verify that your packets are being sent correctly. Open Wireshark, select your network interface, and set a filter for “arp”, then run your program. You should see a broadcast ARP request with your source IP and MAC, and the target IP you specified, with the target MAC field as zero. If you don’t see that, there’s a mistake in your packet construction—common issues like wrong interface name, wrong opcode, or wrong Ethernet type. Wireshark is an invaluable tool here, and I recommend every developer working with ARP keep it open while testing their code.

Now, let’s address the elephant in the room: is creating ARP requests with Tins legal and ethical? ARP is a core part of network communication, and legitimate uses like network administration, device discovery, and security monitoring are completely fine. However, using ARP requests for malicious purposes, like network scanning without permission, ARP spoofing to intercept traffic, or any other activity that violates network policies or laws, is not allowed. As a Tins supplier, we fully support ethical network development, and we provide resources to help teams build tools that are used for good, not harm. If you’re building a tool that interacts with network devices, always get explicit permission from the network owner, and follow all relevant laws and regulations.

If you’ve made it this far, you probably have an idea for a project that uses ARP requests—whether it’s a home network scanner, a tool to troubleshoot connectivity issues, or a security monitoring utility. Tins makes building that tool straightforward, with a clean API that reduces boilerplate code and common errors. Our team has worked with hundreds of developers, from startups to enterprise networks, to implement Tins-based network tools, and we can help you every step of the way—from getting started with basic ARP packets to building complex, production-ready applications.

Tinplate Sheet If you’re ready to implement ARP requests with Tins for your next project, or if you have questions about protocol support, custom modules, or troubleshooting, get in touch with our team to discuss your needs. We offer flexible licensing options, dedicated support for development projects, and customized solutions tailored to your specific network use case. Don’t waste time debugging manual packet construction—use Tins to build reliable, efficient network tools that work the way you need them to.

References

  1. Tins Network Programming Library: Official Documentation
  2. TCP/IP Illustrated, Volume 1: The Protocols (W. Richard Stevens)
  3. Berkeley Packet Filter (BPF) Specification, RFC 1700
  4. Address Resolution Protocol (ARP) Standard, RFC 826

Zhangjiagang Great Robust Packing Material Co., Ltd.
Zhangjiagang Great Robust Packing Material Co., Ltd. is one of the most professional tins manufacturers and suppliers in China. We are committed to providing high quality customized products with low price. Please rest assured to wholesale bulk durable tins from our factory. Contact us for pricelist.
Address: No. 16, Xinzha Middle Road, Yangshe Town, Zhangjiagang City, Jiangsu Province
E-mail: robustpacking@vip.163.com
WebSite: https://www.robustpack.com/