用Libpcap发送数据包 [英] Send packet with Libpcap

查看:154
本文介绍了用Libpcap发送数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
谁能帮我.. !!

我试图将数据包发送到LAN,但不幸的是发现了一些问题.
我想知道我们如何增加延迟以发送数据包.
除了Libpcap之外,还有其他替代方法可以在LAN端口上发送数据包吗?

建议,不胜感激.

干杯...

Hi Guys,
Can anyone please help me..!!

I was trying to send packets on to the LAN,but unfortunately was finding some probs.
I wanted to know how we add delay in order to send the packet.
Is there any other alternate way to send packets on the LAN port rather than Libpcap.

Suggestion most appreciated.

Cheers...

#include <iostream>
#include <fstream>
//#include 
#include <pcap.h>

int main(int argc, char **argv)
{
	pcap_t *fp;
	using namespace std;

	char errbuf[PCAP_ERRBUF_SIZE];
	pcap_t *descr= pcap_open_offline("/root/Desktop/raju.pcap",errbuf);
	const u_char * packet;

	struct pcap_pkthdr hdr;

	packet = pcap_next(descr, &hdr);
	cout<< packet <<endl;
	fflush(stdout);
	cout<<"hello gupta"<<endl;
	fflush(stdout);
		if ( (fp= pcap_open_live(argv[1], 100, 1,1000,errbuf ) ) == NULL)
		{
		fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]);
		return 0;
		}
			while (packet)
			{
				unsigned packetSize= hdr.len;
				cout <<"packet Size = " <<packetSize<<endl;
				for (unsigned i=0; i< packetSize;i++)
				{
					cout<<packet[i];
					usleep(Second3);
				}

				packet = pcap_next(descr, &hdr);
				cout <<"\n"<<endl;
			}
		pcap_close(descr);
return 0;
}

推荐答案

访问:

http://forums.devshed. com/c-programming-42/how-to-send-a-tcp-packet-using-libpcap-594845.html [
visit :

http://forums.devshed.com/c-programming-42/how-to-send-a-tcp-packet-using-libpcap-594845.html[^]


void playThread::run()
{
QMutex waitMutex;
printf("\n ************** Play Thread Started **************\n");
   while(1)
	{ 
printf("\n ************** Play Thread Started **************\n");
// pause the simulator
if (m_bToSuspend) {
waitMutex.lock();
m_waitCondt.wait(&waitMutex);
waitMutex.unlock();
}

    pcap_t *fp;
    struct pcap_pkthdr hdr;
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t *descr= pcap_open_offline(D_T_Buff,errbuf);
    const u_char * packet;
printf("D_T_Buff %s\n",D_T_Buff);
		devss = pcap_lookupdev(errbuf);
		if (devss == NULL) {
			fprintf(stderr, "Couldn''t find default device: %s\n", errbuf);
			//return(2);
		}		


    packet = pcap_next(descr, &hdr);
			//cout<<"hhhhhhhh" << packet <<endl;
    if ( pcap_open_live(devss, 100, 1,1000,errbuf ) == NULL)
    {
//        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", );
  //      return 0;
    }
    struct timespec *req=new timespec;
    struct timespec *rem=new timespec;
    while (packet)
	{
		unsigned packetSize= hdr.len;
		unsigned long packetTime=	req->tv_sec  = hdr.ts.tv_sec;
		packetTime=	req->tv_nsec = hdr.ts.tv_usec*1000L;
//		cout <<"packet Size =" <<packetSize <<endl;
//		cout <<"packet Time =" <<packetTime<<endl;
//		cout << pcap_sendpacket(fp, packet, packetSize /* size */)<< endl;

        for (unsigned i=0; i < packetSize ;i++)
		{
//                        cout<<packet[i];
                }
		packet = pcap_next(descr, &hdr);
		unsigned long packetTime1=	rem->tv_sec  = hdr.ts.tv_sec;
		packetTime1=	rem->tv_nsec = hdr.ts.tv_usec*1000L;
//		cout <<"packet Time1 =" <<packetTime1<<endl;
		unsigned long delay = (packetTime1 - packetTime)/1000;
		printf("Delay %ld",delay);
//		cout <<"delay =" <<delay<<endl;
		usleep(delay);
//		cout <<"\n"<<endl;
		}
pcap_close(descr);

	}

}


这篇关于用Libpcap发送数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆