Send an ARP packet using C

I would like to know how to send an ARP packet in C, functions sent and sent from sockets are sent via TCP or UDP, but I have not found how to use ARP.

+3
source share
1 answer

In this case, you need to use Raw Sockets, or you can use some lib to simplify this, the recommended lib for this is libpcap, below I wrote a simple example for sending a package using libpcap.

const int packet_size = 40; // change the value for real size of your packet
char *packet = (char *) malloc(packet_size)
pcap_t *handle = NULL;
char errbuf[PCAP_ERRBUF_SIZE], *device = NULL;
if ((device = pcap_lookupdev(errbuf)) == NULL) {
  fprintf(stderr, "Error lookup device", device, errbuf);
  exit(1);
}
if ((handle = pcap_open_live(device, BUFSIZ, 1, 0, errbuf)) == NULL) {
  fprintf(stderr, "ERRO: %s\n", errbuf);
  exit(1);
}
// here you have to write your packet bit by bit at packet
int result = pcap_inject(handle, packet, packet_size);

, http://www.cse.ohio-state.edu/cgi-bin/rfc/rfc1293.html ARP http://en.wikipedia.org/wiki/Ethernet_frame ethernet

+5

All Articles