Quote:
Originally Posted by xpi0t0s
Where is main?
Creation of predefined size Link list specific code
Code:
int counter=0;
emptylist=NULL;
pktlist=NULL;
senderlist=NULL;
// snode=NULL;
while(counter <= 100000)
{
// printf("main ... 0.2");
enode=(struct CaptureBuffer *)malloc(sizeof(struct CaptureBuffer));
// printf("sizeof %d enode=%d\n",counter,sizeof(*enode));
// printf("address of %d node is %d\n",counter,enode);
counter++;
memset(enode,0,4136);
if(emptylist == NULL)
{ printf("main ... 0.3");
senderlist=shead=snode=pnode=emptylist=ehead=ecurr=enode;
}
else
{// printf("main ... 0.4");
// enode->prev=ecurr;
ecurr->next=enode;
ecurr=ecurr->next;
enode->next= ehead;
//printf("main...0.5");
}
}
printf("main... 0.5");
etail=ecurr;
etail->next=ehead;
ehead->prev=etail;
Full code of main
Code:
#include"capture.h"
#include<pthread.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<features.h>
#include<linux/if_packet.h>
#include<linux/if_ether.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<net/if.h>
#include<net/if_arp.h>
#include<linux/ip.h>
#include<linux/tcp.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<netinet/in.h>
int filter_flag=0;
struct sockaddr_ll destAddr;
//destAddr=(unsigned char)malloc(sizeof(8));
unsigned char proto[2];
unsigned char* data;
int data_len;
typedef unsigned char MacAddress[MAC_ADDR_LEN];
extern int errno;
MacAddress localMac;
MacAddress destMac;
int semaphore;
union semun arg;
wPos=0;
rPos=-3;
int main()
{
printf("main ... 0");
printf(" Allocating memory ...please wait\n");
// node=(struct CaptureBuffer *)malloc(1000000*sizeof(struct CaptureBuffer *));
printf("sizeof structis %d\n",sizeof(struct CaptureBuffer));//4136 bytes
printf("main ... 0.1");
/*Allocating memory for eth0->eth1 traffic*/
int counter=0;
emptylist=NULL;
pktlist=NULL;
senderlist=NULL;
// snode=NULL;
while(counter <= 100000)
{
// printf("main ... 0.2");
enode=(struct CaptureBuffer *)malloc(sizeof(struct CaptureBuffer));
// printf("sizeof %d enode=%d\n",counter,sizeof(*enode));
// printf("address of %d node is %d\n",counter,enode);
counter++;
memset(enode,0,4136);
if(emptylist == NULL)
{ printf("main ... 0.3");
senderlist=shead=snode=pnode=emptylist=ehead=ecurr=enode;
}
else
{// printf("main ... 0.4");
// enode->prev=ecurr;
ecurr->next=enode;
ecurr=ecurr->next;
enode->next= ehead;
//printf("main...0.5");
}
}
printf("main... 0.5");
etail=ecurr;
etail->next=ehead;
ehead->prev=etail;
printf("main ... 1");
/* Allocating memeory for the eth1->eth0 traffic*/
int counter2 =0;
emptylist2=NULL;
pktlist2=NULL;
senderlist2=NULL;
// snode=NULL;
printf("main ... 1.1");
// printf("1");
while(counter2 <= 100000)
{
// printf("2");
enode2=(struct CaptureBuffer2 *)malloc(sizeof(struct CaptureBuffer2));
// printf("sizeof %d enode=%d\n",counter,sizeof(*enode));
counter2++;
memset(enode2,0,4136);
if(emptylist2 == NULL)
{ printf("main ... 1.2");
// printf("3");
senderlist2=shead2=snode2=pnode2=emptylist2=ehead2=ecurr2=enode2;
//emptylist->next=NULL;
//ecurr->prev=NULL;
}
else
{// printf("main ... 1.3");
// printf("4");
ecurr2->next2=enode2;
ecurr2=ecurr2->next2;
enode2->next2= ehead2;
}
}
printf("main ... 1.4");
etail2=ecurr2;
etail2->next2=ehead2;
ehead2->prev2=etail2;
// printf("5");
printf("main ... 2");
printf("memory allocated !!!\n");
int i;
for(i=1;i<=3600;i++)
{
Shaper[i]=i;
//Shaper[i]=i%100;
}
for(i=0;i<3600;i++)
{
// printf("%d",Shaper[i]);
}
time_t rawtime;
struct tm *timeinfo;
char buffer[80],hr[10],min[10],sec[10];
int str;
int cnt=0;
printf("main ... 3");
/*
Pthread_t cap1;
pthread_t sendPID;
pthread_t cap2;
pthread_t sendPID2;
*/
capProcess myCap;
sendProcess mySend;
capProcess myCap2;
sendProcess mySend2;
key_t key=1234;
int pid;
// int semid;
int nsems =0;
int nsops=2;
int semflg= IPC_CREAT | 0666;
struct sembuf *sops = (struct sembuf *)malloc(2*sizeof(struct sembuf));
/*
if((semaphore=semget(key,nsems,semflg) == -1))
{
perror("semget:semget fialed\n");
exit(1);
}else
{
fprintf(stderr, "semget:semget succeeded: semaphore=%d\n",semaphore);
}
*/
int sd;
sd=CreateRawSocket(ETH_P_ALL);
printf(">>>sd is %d\n",sd);
printf("Socket created successfully to capture packets from eth0 \n");
BindRawSocketToInterface("eth0",sd,ETH_P_ALL);
printf("socket is bind to eth0 interface \n");
int Fd;
Fd=CreateRawSocket(ETH_P_ALL);
printf(">>>Fd is %d\n",Fd);
printf("Socket created successfully to send packets to eth1 \n");
BindRawSocketToInterface("eth1",Fd,ETH_P_ALL);
printf("socket for sender is bind to eth1 interface \n");
myCap.sd=sd;
myCap.nic="eth0";
myCap.semaphore=semaphore;
myCap.pktCnt=0;
printf("main ... 4");
if(pthread_create(&cap1,NULL,&capture,&myCap))
{
fprintf(stderr,"Error creating RAW Capture thread \n");
abort();
}
printf("Raw capture1 thread created \n");
printf("main...4.1");
mySend.Fd=Fd;
mySend.nic="eth1";
mySend.semaphore=semaphore;
printf("main....4.2");
if(pthread_create(&sendPID,NULL,&sender,&mySend))
{
fprintf(stderr,"Error creating sender thread \n");
abort();
}
printf("Sender1 thread created \n");
printf("main ... 5");
pthread_join(cap1,NULL);
printf("CAP1 thread joined");
pthread_join(sendPID,NULL);
printf("SENDER1 JOIN successful \n");
//-----------------------------------------
/*
myCap2.sd=Fd;
myCap2.nic="eth1";
myCap2.semaphore=semaphore;
myCap2.pktCnt=0;
if(pthread_create(&cap2,NULL,&capture1,&myCap2))
{
fprintf(stderr,"Error creating RAW Capture thread \n");
abort();
}
printf("Raw capture2 thread created \n");
mySend2.Fd=sd;
mySend2.nic="eth0";
mySend2.semaphore=semaphore;
if(pthread_create(&sendPID2,NULL,&sender1,&mySend2))
{
fprintf(stderr,"Error creating sender thread \n");
abort();
}
printf("Sender2 thread created \n");
printf("main ... 6");
pthread_join(cap2,NULL);
printf("CAP2 thread joined");
pthread_join(sendPID2,NULL);
printf("SENDER2 join successful \n");
*/
printf("main ... 7");
}
int CreateRawSocket(int protocol_to_sniff)
{
int rawso;
if((rawso = socket(PF_PACKET,SOCK_RAW,htons(protocol_to_sniff))) == -1)
{
perror("Error creating raw sockets \n");
exit(-1);
}
return rawso;
}
int BindRawSocketToInterface(char *device,int rawsock,int protocol)
{
struct sockaddr_ll sll;
struct ifreq ifr;
bzero(&sll,sizeof(sll));
bzero(&ifr,sizeof(ifr));
strncpy((char *)ifr.ifr_name,device,IFNAMSIZ);
if((ioctl(rawsock,SIOCGIFINDEX,&ifr)) == -1)
{
printf("Error getting interface index \n");
exit(-1);
}
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifr.ifr_ifindex;
sll.sll_protocol = htons(protocol);
if((bind(rawsock, (struct sockaddr *)&sll,sizeof(sll))) == -1)
{
perror("Error binding raw sotcket to interface \n");
exit(-1);
}
return 1;
}