Click to See Complete Forum and Search --> : MultiThreading in C with pcap


IPoverRadio
January 19th, 2011, 04:57 AM
HI,

I would greatly appreciate some help regarding a problem i am facing. I am using pcap libraries in C to capture packets from one interface and then send them to another interface. I am using threads to do both the tasks.

The problem that i am facing is that the receiving thread which runs the pcap_loop function , which takes a callback funtion, proceeds really fast. It takes packets and puts them into a queue. The sending thread though is very slow. The sending thread takes the packet out of the queue and the sends it on another interface. I am using the pcap_sendpacket function to send the packet. Here is a short code for the threads i am using.



Send=CreateThread(NULL,0,ThreadSend,0,0,0);
//SetThreadPriority(Send,2);
//Receive=CreateThread(NULL,0,ThreadRec,0,0,0);

Here is the receiving function,

DWORD WINAPI ThreadRec(LPVOID b)
{

pcap_loop(adhandle, 0,packet_handler, NULL);

return 0;

}
this is the call back function

void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{

int caplen=header->caplen;
int len=header->len;

rear++;
rear= (rear%QUEUESIZE);
if(rear==front)
{
printf("CIRCULAR QUEUE FULL........................................FULL FULL FULL");
return;
}
else
{

payload[rear]=(u_char*)malloc(len);

sizeindex[rear]=len;
checklist[rear]=true;//boolean variable
// //printf("Rear = %d Front = %d ",rear,front);
// }

}

this is the sending function

DWORD WINAPI ThreadSend(LPVOID a)
{
while(1){


if(front==rear)
{
//printf("CIRCULAR STACK EMPTY");
//return 0;
}
else{
front++;
front = front%QUEUESIZE;

while(checklist[front]==false){}
size=sizeindex[front];

if (pcap_sendpacket(adhandle2,payload[front], size /* size */) != 0)
{
fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(fp));
// return;
}
checklist[front]=false;
free((u_char*)payload[front]);

}

}}

If i am receiving data rate is about 40 mbps, the sending just goes uptil 3 mbps. I am using a packet generate to generate a constant data rate. The size of the queue is adjustable, i can keep it as long as i want ofcourse. Please help me out with this issue, i dont understand why the sending thread is soo slow.

Best Regards

jchian
January 22nd, 2011, 10:58 AM
I have the same problem here, create a similar (but simpler) program that only sends packets in a loop. The packets are 100 bytes in size. It can only send at a rate of 1800 packets/seconds (or 1.8Mbps). I have 100Mbps link (duplex) and my PC has duo core at 2.4GHz running windows 7 home.

Hope someone found the cause or better yet, a solution.

Thanks.

aamir121a
February 7th, 2011, 03:44 AM
if you are reviving faster than you can send , as in serial transmission , then go parallel, split the sending part into a pool of threads , see if that helps