2014-02-15 41 views
3

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

回答

5

在这种情况下,您必须使用Raw套接字,或者您可以使用一些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有关以太网帧信息