asd*_*bug 3 c network-programming arp packet
我想知道如何发送语言C的ARP数据包,从套接字发送和发送的函数通过TCP或UDP发送,但我没有找到如何使用ARP.
在这种情况下你必须使用原始套接字,或者你可以使用一些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);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,你必须创建所有包体,看看:http://www.cse.ohio-state.edu/cgi-bin/rfc/rfc1293.html ARP报文,并在信息的http:// EN.有关以太网帧的信息,请访问wikipedia.org/wiki/Ethernet_frame