简体   繁体   中英

Send a ARP packet with C

我想知道如何发送语言C的ARP数据包,从套接字发送和发送的函数通过TCP或UDP发送,但我没有找到如何使用ARP。

In this case you have to use Raw Sockets, or you can use some lib to make easier this, a recommended lib to that is libpcap, below I wrote a simple example to send a packet 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);

In this case you have to create all packet body, look at: http://www.cse.ohio-state.edu/cgi-bin/rfc/rfc1293.html for information about ARP packet and at http://en.wikipedia.org/wiki/Ethernet_frame for info about ethernet frame

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM