winpcap学习笔记--(Sending Packet)

感觉winpcap的发包的函数做得有点呵呵啊,没啥用的感觉。。。如果有大神能解答程序中注释中的问题小弟感激不敬!!!

//这些包都发送到哪儿去了???

#define WIN32
#define HAVE_REMOTE

#include <stdio.h>
#include <stdlib.h>
#include "pcap.h"

void usage();

void main(int argc,char **argv){
	pcap_t *indesc,*outdesc;
	char errbuf[PCAP_ERRBUF_SIZE];
	char source[PCAP_BUF_SIZE];
	FILE *capfile;
	int caplen,sync;
	u_int res;
	//pcap_send_queue
	/*
	A queue of raw packets that will be sent to the network with pcap_sendqueue_transmit(). More...

	#include <Win32-Extensions.h>

	Data Fields

	u_int 	maxlen			//Maximum size of the the queue, in bytes. This variable contains the size of the buffer field. 
	u_int 	len				//Current size of the queue, in bytes. 
	char * 	buffer			//Buffer containing the packets to be sent. 
	*/
	pcap_send_queue *squeue;
	struct pcap_pkthdr *pktheader;
	const u_char *pktdata;
	float cpu_time;
	u_int npacks=0;
	//errno_t
	/*
	errno_t是一种数据类型,实际上是一个整形,代表错误号码。

	比如0就代表没有错误,1就找不到文件等。
	*/
	errno_t fopen_error;
	/*Check the validity of the command line*/
	if(argc<=2 || argc>=5){
		usage();
		return;
	}

	/*Retrieve the length of the capture file*/
	fopen_error=fopen_s(&capfile,argv[1],"rb");
	if(fopen_error!=0){
		printf("Error opening the file, errno %d.\n", fopen_error);
		return;
	}
	//fseek
	/*
	int fseek(FILE *stream, long offset, int fromwhere);
	函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere为基准,偏移offset(指针偏移量)个字节的位置,函数返回0。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置,函数返回一个非0值。
	实验得出,超出文件末尾位置,还是返回0。往回偏移超出首位置,返回-1,且指向一个-1的位置,请小心使用。
	fseek函数和lseek函数类似,但lseek返回的是一个off_t数值,而fseek返回的是一个整型。
	*/
	fseek(capfile,0,SEEK_END);
	//ftell
	/*
	函数 ftell 用于得到文件位置指针当前位置相对于文件首的偏移字节数。在随机方式存取文件时,由于文件位置频繁的前后移动,程序不容易确定文件的当前位置。
	*/
	//pcap_file_header
	/*
	Header of a libpcap dump file. More...

	Data Fields

	bpf_u_int32 	magic
	u_short 	version_major		//Libpcap major version. 
	u_short 	version_minor		//Libpcap minor version. 
	bpf_int32 	thiszone			//gmt to local correction 
	bpf_u_int32 	sigfigs			//accuracy of timestamps 
	bpf_u_int32 	snaplen			//max length saved portion of each pkt 
	bpf_u_int32 	linktype		//data link type (LINKTYPE_*) 
	*/
	caplen=ftell(capfile)-sizeof(struct pcap_file_header);
	fclose(capfile);

	/*Check if the timestamps must be respected*/
	if(argc==4 && argv[3][0] == 's'){
		sync=TRUE;
	}
	else{
		sync=FALSE;
	}
	/*Open the capture*/
	/*Create the source string according to the new WinPcap syntax*/
	if ( pcap_createsrcstr( source,         // variable that will keep the source string
							PCAP_SRC_FILE,  // we want to open a file
							NULL,           // remote host
							NULL,           // port on the remote host
							argv[1],        // name of the file we want to open
							errbuf          // error buffer
							) != 0)
	{
		fprintf(stderr,"\nError creating a source string\n");
		return;
	}

	/*Open the capture file*/
	if((indesc=pcap_open(source,65536,PCAP_OPENFLAG_PROMISCUOUS,1000,NULL,errbuf))==NULL){
		fprintf(stderr,"\nUnable to open the file %s.\n", source);
		return;
	}

	/*Open the output adapter*/
	if((outdesc=pcap_open(argv[2],100,PCAP_OPENFLAG_PROMISCUOUS,100,NULL,errbuf))==NULL){
		fprintf(stderr,"\nUnable to open the file %s.\n", source);
		return;
	}

	/*Allocate a send queue*/
	squeue=pcap_sendqueue_alloc(caplen);

	/*Fill the queue with the packets from the file*/
	while((res=pcap_next_ex(indesc,&pktheader,&pktdata))==1){
		if(pcap_sendqueue_queue(squeue,pktheader,pktdata)==-1){
			printf("Warning:Packet buffer too small,not all the packets will be sent\n");
			break;
		}

		npacks++;
	}

	if(res==-1){
		printf("Corrupted input file.\n");
		pcap_sendqueue_destroy(squeue);
		return;
	}

	/*Transmit the queue*/
	cpu_time=(float)clock();
	//pcap_sendqueue_transmit
	/*
	u_int pcap_sendqueue_transmit	(	
		pcap_t * 	p,
		pcap_send_queue * 	queue,
		int 	sync	 
	)	
	Send a queue of raw packets to the network.

	This function transmits the content of a queue to the wire. 
	p is a pointer to the adapter on which the packets will be sent, queue points to a pcap_send_queue structure containing the packets to send (see pcap_sendqueue_alloc() and pcap_sendqueue_queue()), 
	sync determines if the send operation must be synchronized: if it is non-zero, the packets are sent respecting the timestamps, otherwise they are sent as fast as possible.

	The return value is the amount of bytes actually sent. If it is smaller than the size parameter, an error occurred during the send. The error can be caused by a driver/adapter problem or by an inconsistent/bogus send queue.
	*/
	if((res=pcap_sendqueue_transmit(outdesc,squeue,sync))<squeue->len){
		printf("An error occurred sending the packets: %s. Only %d bytes were sent\n", pcap_geterr(outdesc), res);
	}

	cpu_time=(clock()-cpu_time)/CLK_TCK;

	printf ("\n\nElapsed time: %5.3f\n", cpu_time);
	printf ("\nTotal packets generated = %d", npacks);
	printf ("\nAverage packets per second = %d", (int)((double)npacks/cpu_time));
	printf ("\n");

	/*free the send queue*/
	pcap_sendqueue_destroy(squeue);

	/*Close the input file*/
	    /* Close the input file */
    pcap_close(indesc);

    /* 
     * lose the output adapter 
     * IMPORTANT: remember to close the adapter, otherwise there will be no guarantee that all the 
     * packets will be sent!
     */
    pcap_close(outdesc);
}
void usage()
{

	printf("\nSendcap, sends a libpcap/tcpdump capture file to the net. Copyright (C) 2002 Loris Degioanni.\n");
	printf("\nUsage:\n");
	printf("\t sendcap file_name adapter [s]\n");
	printf("\nParameters:\n");
	printf("\nfile_name: the name of the dump file that will be sent to the network\n");
	printf("\nadapter: the device to use. Use \"WinDump -D\" for a list of valid devices\n");
	printf("\ns: if present, forces the packets to be sent synchronously, i.e. respecting the timestamps in the dump file. This option will work only under Windows NTx.\n\n");

	exit(0);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值