#include #include #include #include #include #include #include #include #include #include char *pcap_filter; struct timeval c_time,s_time; struct timezone tz,tz1; bpf_u_int32 mask; bpf_u_int32 net; char errbuf[1024]; struct bpf_program filter; unsigned long int dby; unsigned long int tby; int nri,len; char str[250]; int q; typedef struct client { unsigned long int ipn; int mask; char ipch[20]; char cmas[5]; } clients; clients ipc[10]; void siga() { exit(1); } unsigned long int putere(int a,int b) { unsigned long int p; int j; if (b==0) return 1; p=1; for(j=1;j<=b;j++) { p=p*a; } return p; } int prel() { char *p,*w,ss[250]; int i; nri=0; struct in_addr intmp; strcpy(ss,str); w=str;p=ss; while(*w) { nri++; strtok(p,"/"); strcat(pcap_filter," or "); strcat(pcap_filter,"net "); strcat(pcap_filter,p); strcat(pcap_filter,"/"); strcpy(ipc[nri].ipch,p); while(strncmp(w,"/",1)) w++; w++; strcpy(ss,w); p=ss; strtok(p,"a"); strcpy(ipc[nri].cmas,p); strcat(pcap_filter,p); w++;w++;w++; strcpy(ss,w); p=ss; } p=pcap_filter; p++;p++;p++;p++; strcpy(pcap_filter,p); for(i=1;i<=nri;i++) { inet_aton(ipc[i].ipch,&intmp); ipc[i].ipn=intmp.s_addr; ipc[i].mask=atoi(ipc[i].cmas); /*printf("%s %s %lu\n",ipc[i].ipch,ipc[i].cmas,ipc[i].ipn); */ } printf("pf=%s==\n",pcap_filter); } void call_ip(struct iphdr *ip) { struct protoent *pp; struct in_addr ia; unsigned long int ipb,ipx; int gasit,i; ia.s_addr = ip->saddr; /* printf("\tsaddr = %X;\t(%s)\n", ip->saddr, inet_ntoa(ia));*/ i=1; gasit=0; ipx=ip->saddr; /* printf("%lu\n",ipx);*/ while(i<=nri) { ipb=ipc[i].ipn+(putere(2,32-ipc[i].mask)-1)*16777216; if (((ipx-ipc[i].ipn)%16777216==0) && (ipx<=ipb) && (ipx>=ipc[i].ipn)) { gasit=1; } i++; } if (gasit==1) tby+=len; else dby+=len; ia.s_addr = ip->daddr; /* printf("\tdaddr = %X;\t(%s)\n", ip->daddr, inet_ntoa(ia));*/ fflush(stdout); /* printf("%lu",ia.s_addr);*/ } int main(int argc, char *argv[]) { unsigned long int x; pcap_t *pcap; /* PCAP descriptor */ u_char *packet; /* Our newly captured packet */ struct pcap_pkthdr pkthdr; /* PCAP packet information structure */ char INTERFACE[20]; struct itimerval del; /* printf("Example of portable packet sniffer using Libpcap\n");*/ /* Obtain a descriptor for interface, capture no more than * 8192 octets, set interface to promiscuous mode, 1000 miliseconds * read timeout, No buffer for error messages */ strcpy(INTERFACE,"eth2"); dby=0; tby=0; signal(SIGALRM, siga); del.it_interval.tv_sec = 2; /* intervalul in s*/ del.it_interval.tv_usec = 0; del.it_value.tv_sec = 2; /*peste cate s sa plece*/ del.it_value.tv_usec = 0; setitimer(ITIMER_REAL, &del, NULL); if((pcap_filter = (char *) malloc(200)) == NULL) error("malloc error"); memset(pcap_filter, 0, 200); pcap_lookupnet(INTERFACE, &net, &mask, errbuf); pcap = pcap_open_live(INTERFACE, 8192, 0, 0, NULL); if (pcap == NULL) { perror("pcap_open_live"); exit(EXIT_FAILURE); } strcpy(str,argv[1]); prel(); if (pcap_compile(pcap, &filter, pcap_filter, 0, net) < 0) error("%s", pcap_geterr(pcap)); if (pcap_setfilter(pcap, &filter) < 0) error("%s", pcap_geterr(pcap)); /* Get the next packet from the queue */ packet = (u_char *) pcap_next(pcap, &pkthdr); packet+=14; if (packet != NULL) { /* Dump the packet in various forms */ /* printf("Dumping %i bytes:\n", pkthdr.caplen);*/ len=pkthdr.caplen; call_ip((struct iphdr *) packet); printf("\n\n"); } else { pcap_perror(pcap, "pcap_next returned NULL"); } /* s_time=time(NULL); sleep(1); f_time=time(NULL); d_time=f_time-s_time; printf("dif time=%d\n",d_time); x=0; */ gettimeofday(&s_time, &tz); q=0; while (q==0) { packet = (u_char *) pcap_next(pcap, &pkthdr); packet+=14; if (packet != NULL) { /* printf("Dumping %i bytes:\n", pkthdr.caplen);*/ len=pkthdr.caplen; call_ip((struct iphdr *) packet); } gettimeofday(&c_time, &tz1); if ((s_time.tv_sec