c言語指定ポート転送パケット---libpcapパケットを使用


sendPkt.c
#include <stdio.h>
#include <pcap.h>
 
int main(int argc, char **argv)
{
	system("clear");
	pcap_t *fp;
 
	char errbuf[PCAP_ERRBUF_SIZE];
	pcap_t *descr= pcap_open_offline("/home/IPS/test.pcap",errbuf);
	const u_char * packet;
 
	struct pcap_pkthdr hdr;
 
	packet = pcap_next(descr, &hdr);
	//printf("packet: %s
",packet); fflush(stdout); /* Open the output device */ if ( (fp= pcap_open_live("eth3", 100, 1,1000,errbuf ) ) == NULL) { fprintf(stderr,"
Unable to open the adapter. %s is not supported
", "eth3"); return 0; } printf("open the output device success!
"); getchar(); while (packet) { unsigned packetSize= hdr.len; printf("packeSize is :%d
",packetSize); unsigned i=0; /* for (i=0; i< packetSize;i++) { //printf("now i is %d
",i); //sleep(2); } */ //pcap_sendpacket(sp->handle.pcap, data, (int)len); pcap_sendpacket(fp, packet, packetSize); packet = pcap_next(descr, &hdr); printf("send packet over!:%s
",descr); } pcap_close(descr); return 0; }

コンパイルコマンド:gcc-o sendpkt sendPkt.c -lpcap
実際のテストでは、wiresharkでバッグをつかむのが正しいです.