使用C发送ARP数据包

时间:2014-02-15 21:42:20

标签: c network-programming packet arp

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

1 个答案:

答案 0 :(得分:5)

在这种情况下你必须使用Raw Sockets,或者你可以使用一些lib来使这更容易,推荐的lib就是libpcap,下面我写了一个使用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获取有关以太网帧的信息