int main(int argc, char *argv[]) { printf("test1\n"); test1(); printf("test2\n"); test2(); printf("speed1\n"); speed1(); printf("done\n"); return 0; }
void agent::new_speed(agent* birds, int size, int pos, objet* obj, int nb_obj, double* vpred, int nb_pred) { double* v1=speed1(birds, size, pos); double* v2=speed2(birds, size, pos); double* v3=speed3(birds, size, pos, obj, nb_obj); double* v4=speed4(vpred, nb_pred); speed[0]=speed[0]+TIME*(GAMMA1*v1[0]+GAMMA2*v2[0]+GAMMA3*v3[0]+GAMMA4*v4[0]); speed[1]=speed[1]+TIME*(GAMMA1*v1[1]+GAMMA2*v2[1]+GAMMA3*v3[1]+GAMMA4*v4[1]); //norm max = VMAX double norm=sqrt(speed[0]*speed[0]+speed[1]*speed[1]); if(norm>VMAX) { double max=(norm/VMAX); speed[0]=speed[0]/max; speed[1]=speed[1]/max; } }
int main(int argc, char* argv[]) { char *dev; /* name of the device to use */ char *net; /* dot notation of the network address */ char *mask;/* dot notation of the network mask */ int ret; /* return code */ //const u_char *packet; pcap_t* descr; /*you can man it*/ char errbuf[PCAP_ERRBUF_SIZE]; bpf_u_int32 netp; /* ip */ bpf_u_int32 maskp;/* subnet mask */ struct in_addr addr; /* ask pcap to find a valid device for use to sniff on */ dev = pcap_lookupdev(errbuf); /* error checking */ if(dev == NULL) { printf("%s\n",errbuf); exit(1); } /* print out device name */ printf("DEV: %s\n",dev); /* ask pcap for the network address and mask of the device */ /* ret = pcap_lookupnet(dev,&netp,&maskp,errbuf); if(ret == -1) { printf("%s\n",errbuf); exit(1); } */ /* get the network address in a human readable form */ // addr.s_addr = netp; // net = inet_ntoa(addr); /* if(net == NULL)// thanks Scott :-P { perror("inet_ntoa"); exit(1); } printf("NET: %s\n",net); // do the same as above for the device's mask addr.s_addr = maskp; mask = inet_ntoa(addr); if(mask == NULL) { perror("inet_ntoa"); exit(1); }*/ signal(SIGINT, sigproc); signal(SIGTERM, sigproc); signal(SIGINT, sigproc); printf("MASK: %s\n",mask); size_mac=sizeof(struct ether_header); size_ip=sizeof(struct ip); //char *filename="/dev/shm/get.pcap"; //char *filename="./get.pcap"; //char *filename="/run/shm/http.pcap"; initBCBuff(); /* pthread_t my_thread; if(pthread_create(&my_thread, NULL, threadclass, NULL)!=0) { msg("Ecreate thread error\n"); exit(0); } */ /// msg("kkkkkkkkkkkkk\n"); int i; #if 1 #define MAX_FILE 4 char filenameA[MAX_FILE+1][200]={{"/run/shm/http.pcap"},{"/run/shm/ftp.pcap"},{"/run/shm/pop3.pcap"},{"/run/shm/smtp.pcap"},{""}}; //char filenameA[MAX_FILE+1][200]={{"/run/shm/http.pcap"},{""}}; //char *filename="./exam-for/ftp.pcap"; char *filename; printf("EIinit hb\n"); hb=init_HB(HB_MAX); init_free_link(FREE_NODE); init_BC();//the cache for the pro classificationd printf("EIinit bc\n"); init_patterns(); msg("hhhhhh\n"); DELAY_INI(); NS_TIME_START(time); int num=0; int kk=0; again: filename= filenameA[num]; // msg("kk=%d\n",kk); num=(num+1)%MAX_FILE; if(kk==100000) goto endk; ++kk; descr =pcap_open_offline(filename, errbuf); if(descr == NULL) { printf("pcap_open_live(): %s\n",errbuf); exit(1); } ///////////////////////////// DELAY(DELAY_NS); pcap_loop(descr,-1,my_callback,NULL); //printf("llllllllllllll\n"); pcap_close(descr); goto again; endk: exitflag=1; //sem_post(&bin_sem); NS_TIME_END(time); msg("\n===================%d\n\n",kk); for(i=0;i<PRO_MAX+2;++i) { printf("%s:%lld\n",pro_map[i],pronum[i]); } printf("losepacket=%lld\n",losepacket); //speed(NS_GET_TIMEP(time),count+sum,firstlen); speed1(NS_GET_TIMEP(time),packet_num,packet_len); speed2(NS_GET_TIMEP(time),packet_num,packet_len,100000,DELAY_NS); #else int fd=open(filename,O_RDONLY); if(fd<=0) perror("open error\n"); //unsigned char buf struct pcap_pkthdr head; u_char buf[3000]; int headlen=sizeof(struct pcap_file_header); int phead=sizeof(struct pcap_pkthdr); //while(read(fd,&head,sizeof())) NS_TIME_START(time); printf("EIinit hb\n"); hb=init_HB(HB_MAX); init_free_link(FREE_NODE); init_BC();//the cache for the pro classificationd printf("EIinit bc\n"); init_patterns(); //msg("EISpppppppppp"); while(1) { lseek(fd,headlen,SEEK_SET); //read(fd,buf,headlen); while(read(fd,&head,phead)) { //printf("lllll\n"); read(fd,buf,head.caplen); my_callback(NULL,&head,buf); //printf("%d\n",head.caplen); } } #endif //pthread_join(my_thread, NULL); ///////////////////////////////////////////////////// return 0; }
void my_callback(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char* packet) { packet_num++; packet_len+=pkthdr->caplen; static int count = 0; //static int nn=0; static int i; static unsigned short eth_type; static int vlan_flag=0; //sem_getvalue(&shmp[i]->sem,&semnum); //printf("sem:%d\n",semnum); //usleep(1000); static int semnum; // sem_getvalue(&bin_sem,&semnum); // printf("sem:%d\n",semnum); //printf("mmmmmmmmmmmmmm\n"); if(exitflag) { /*for(i=0;i<snortnum;++i) { memcpy(shmp[i]->data[shmp[i]->tail],"########",strlen("########")); shmp[i]->tail=(shmp[i]->tail+1)%shmp[i]->looplen; my_lock_release(shmp[i]); } sleep(4); for(i=0;i<snortnum;++i) { destroy_loop(shmp[i]); DeleteShm(shmid[i]); }*/ for(i=0;i<PRO_MAX+2;++i) { printf("%s:%lld\n",pro_map[i],pronum[i]); } printf("losepacket=%lld\n",losepacket); //sem_post(&bin_sem); NS_TIME_END(time); speed1(NS_GET_TIMEP(time),packet_num,packet_len); printf("count=%d,\nfind_pro=%lld\n",count,find_pro); printf("exit\n"); del_HB(&hb); acsmFree (acsm); // exitflag=0; exit(0); } //return; mac=(struct ether_header*)packet; eth_type=ntohs(mac->ether_type); if((eth_type==0x8100)) { vlan_flag=1; //msg("W:****0X%04X\n",eth_type); eth_type=(packet[16])*256+packet[17]; } else vlan_flag=0; // msg("W:0X%04X\n",eth_type); if((eth_type!=0x0800))//不是ip数据报 return; if(vlan_flag) ip=(struct ip*)(packet+size_mac+4); else ip=(struct ip*)(packet+size_mac); /*char ipdotdecs[20]={0}; char ipdotdecc[20]={0}; inet_ntop(AF_INET,(void*)&(ip->ip_src),ipdotdecs,16); inet_ntop(AF_INET,(void*)&(ip->ip_dst),ipdotdecc,16);*/ //printf("%s-->%s: len:%d\n",ipdotdecs,ipdotdecc,pkthdr->caplen); if((ip->ip_p==6))//tcp { // msg("EIStcp\n"); //tcp=(struct fniff_tcp*)(packet+size_mac+size_ip); tcp=(struct fniff_tcp*)((char*)ip+size_ip); sd.b_ip=(ip->ip_src.s_addr); sd.l_ip=(ip->ip_dst.s_addr); if(sd.b_ip>sd.l_ip) { sd.b_port=ntohs(tcp->th_sport); sd.l_port=ntohs(tcp->th_dport); } else { sd.b_ip^=sd.l_ip; sd.l_ip^=sd.b_ip; sd.b_ip^=sd.l_ip; sd.b_port=ntohs(tcp->th_dport); sd.l_port=ntohs(tcp->th_sport); } hash=hash_HB(sd.b_ip,sd.b_port,sd.l_ip,sd.l_port); tcplen=ntohs(ip->ip_len)-(ip->ip_hl*4)-(tcp->th_off*4); // msg("EIStcp11111111111\n"); // printf("ntohs(ip->ip_len)=%d\n",ntohs(ip->ip_len)+14); // packet.tcp_URG=tcp->th_flags&TH_URG; ack=tcp->th_flags&TH_ACK; // packet.tcp_PSH=tcp->th_flags&TH_PUSH; rst=tcp->th_flags&TH_RST; syn=tcp->th_flags&TH_SYN; fin=tcp->th_flags&TH_FIN; datalen=pkthdr->caplen; ptcp=(unsigned char*)tcp+(tcp->th_off*4); temp=find_node(hb[hash].virtual_sn,&sd); if(temp==NULL&&syn&&!ack&&tcplen==0)//not find { //msg("E no\n"); SN* q=get_node(); q->sdipport=sd; q->state=1; insert_node(&(hb[hash].virtual_sn),q); hb[hash].virtual_sn_num++; //msg("**********=%ld\n",hb[hash].virtual_sn_num); #if 0 if(sd.b_port==21||sd.l_port==21) { q->state=10; pronum[FTP]++; } else if(sd.b_port==80||sd.l_port==80) { q->state=10; pronum[HTTP]++; } memcpy(fortest,packet,pkthdr->caplen); #endif } else if(temp!=NULL) { // printf("state:%d\n",temp->state); if((temp->state==1)&&syn&&ack&&(tcplen==0)) { //msg("W:my ooooooooooooooooooo\n"); temp->state=2; } else if(temp->state==2&&ack&&!syn&&tcplen==0) { temp->state=3; //msg("W:its ===============================static\n"); //msg("W:my hash:%u\n",hash); } else if(temp->state>=3&&temp->state<9) { //if(tcplen==0) // return; //msg("W:my hash:%u\n",hash); //msg("+++++\n"); //msg("ttttttttttttt\n"); p=get_BC_node(); //msg("mmmmmmmmm\n"); if(p==NULL) {msg("EISget bc node error\n");exit(0);} p->datalen=pkthdr->caplen; p->tcplen=tcplen; //msg("tcplen=%d,pkthdr->caplen=%d\n",tcplen,pkthdr->caplen); if(tcplen<0) { msg("EIS tcp<0\n"); exit(0); } p->next=NULL; memcpy(p->buf,packet,pkthdr->caplen); p->ptcp=(unsigned char*)(p->buf)+(tcp->th_off*4)+((unsigned char*)tcp-(unsigned char*)mac);//ptcp; temp->tcp_content_len+=tcplen; if(temp->bc_head==NULL) { temp->bc_head=temp->bc_tail=p; } else { temp->bc_tail->next=p; temp->bc_tail=p; } temp->state++; if((temp->state==9)||rst||fin||(temp->tcp_content_len>150)) { //msg("EIS static\n"); #if 0 p=temp->bc_head; while(p!=NULL) { if(p->tcplen!=0) acsmSearch(acsm,p->ptcp,p->tcplen,PrintMatch); p=p->next; } #else acSearch(acsm,temp->bc_head); acSearch(acsm,temp->bc_head); #endif i=getSummary(acsm->acsmPatterns,feature_num); pronum[i]++; temp->proto=i; if(rst||fin) { temp->state=10; resume_BC_node(temp->bc_head); resume_node(temp); hb[hash].virtual_sn_num--; //msg("*********=%ld\n",hb[hash].virtual_sn_num); if(hb[hash].virtual_sn_num==0) hb[hash].virtual_sn=NULL; return; } temp->state=10; resume_BC_node(temp->bc_head); temp->bc_head=NULL; temp->bc_tail=NULL; } } else if(temp->state>=10) { if(rst||fin) { //resume_node(temp); move_node(&(hb[hash].virtual_sn),temp); hb[hash].virtual_sn_num--; //msg("**************=%ld\n",hb[hash].virtual_sn_num); if(hb[hash].virtual_sn_num==0) hb[hash].virtual_sn=NULL; return; } } else { msg("ggggggggggg\n"); } } }//tcp else if(ip->ip_p==1)//icmp { //printf("2222\n"); //static char pro_map[PRO_MAX+2][20]={"HTTP","FTP","POP3","SMTP","UNKOWN","UDP","ICMP"}; pronum[PRO_MAX+1]++; } else if(ip->ip_p==17)//udp { //printf("1111111\n"); pronum[PRO_MAX]++; } else { printf("no\n"); } }