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