Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
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;
}
Posted
Updated 16-Oct-12 23:57pm
v4

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);

	}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900