/* gcc -Wall -pedantic af.c -lpcap -o af */ #include "af.h" u_int16_t handle_ethernet(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* packet); u_char* handle_IP(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* packet); void errsys(char msg[50]) { fprintf(stdout,"%s",msg); } unsigned long int putere(int a,int b) { unsigned long int p; int j; p=a; for(j=1;jlen; u_int hlen,off,version; unsigned long int ipb,ipx; int len; int is,id,i,gasit; /* jump pass the ethernet header */ ip = (struct my_ip*)(packet + sizeof(struct ether_header)); length -= sizeof(struct ether_header); /* check to see we have a packet of valid length */ if (length < sizeof(struct my_ip)) { printf("truncated ip %d",length); return NULL; } len = ntohs(ip->ip_len); hlen = IP_HL(ip); /* header length */ version = IP_V(ip);/* ip version */ /* if(((ip->ip_src.s_addr)!=locip) && ((ip->ip_dst.s_addr)!=locip))*/ /* Aici se pune numaratoarea */ gasit=0;i=1; while ((gasit==0)&&(i<=nripc)) { if(ip->ip_src.s_addr==ipc[i].ipn) { ipc[i].tupb+=len; gasit=1; } else { if (ip->ip_dst.s_addr==ipc[i].ipn) { ipc[i].tdownb+=len; gasit=1; } else { ipx=ip->ip_src.s_addr; ipb=ipc[i].subrn+(putere(2,32-ipc[i].nm)-1)*16777216; if (((ipx-ipc[i].subrn)%16777216==0) && (ipx<=ipb)) { ipc[i].tupb+=len; gasit=1; } else { ipx=ip->ip_dst.s_addr; ipb=ipc[i].subrn+(putere(2,32-ipc[i].nm)-1)*16777216; if (((ipx-ipc[i].subrn)%16777216==0) && (ipx<=ipb)) { ipc[i].tdownb+=len; gasit=1; } } } } i++; } } /* handle ethernet packets, much of this code gleaned from * print-ether.c from tcpdump source */ u_int16_t handle_ethernet(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* packet) { u_int caplen = pkthdr->caplen; /* u_int length = pkthdr->len;*/ struct ether_header *eptr; /* net/ethernet.h */ u_short ether_type; if (caplen < ETHER_HDRLEN) { fprintf(stdout,"Packet length less than ethernet header length\n"); return -1; } /* lets start with the ether header... */ eptr = (struct ether_header *) packet; ether_type = ntohs(eptr->ether_type); /* Lets print SOURCE DEST TYPE LENGTH */ /*fprintf(stdout,"ETH: "); fprintf(stdout,"%s ",ether_ntoa((struct ether_addr*)eptr->ether_shost)); fprintf(stdout,"%s ",ether_ntoa((struct ether_addr*)eptr->ether_dhost)); */ /* check to see if we have an ip packet */ if (ether_type == ETHERTYPE_IP) { /* fprintf(stdout,"(IP)");*/ }else if (ether_type == ETHERTYPE_ARP) { /* fprintf(stdout,"(ARP)");*/ }else if (eptr->ether_type == ETHERTYPE_REVARP) { /* fprintf(stdout,"(RARP)");*/ }else { /* fprintf(stdout,"(?)");*/ } /* fprintf(stdout," %d\n",length);*/ return ether_type; } void siga() { int j; prel(); if(contor%3==0) { init_db(); load_db(); } pthread_create (&thread_id, NULL, &save_data, NULL); } void sigt(int signal) { /* save_data_zi(); save_data_luna(); save_data_an(); _exit(3);*/ } int main(int argc,char **argv) { char *dev; char errbuf[PCAP_ERRBUF_SIZE]; pcap_t* descr; struct bpf_program fp; /* hold compiled program */ bpf_u_int32 maskp; /* subnet mask */ bpf_u_int32 netp; /* ip */ u_char* args = NULL; int k; struct in_addr adrc; struct itimerval del; pid_t pid, sid; /* locip=375591690;*/ /* Options must be passed in as a string because I am lazy */ /* if(argc < 2){ fprintf(stdout,"Usage: %s numpackets \"options\"\n",argv[0]); return 0; } */ /* grab a device to peak into... */ dev = pcap_lookupdev(errbuf); strcpy(dev,DEVICE); /* printf("device=%s\n",dev);* if(dev == NULL) { printf("%s\n",errbuf); exit(1); } /* ask pcap for the network address and mask of the device */ pcap_lookupnet(dev,&netp,&maskp,errbuf); /* open device for reading. NOTE: defaulting to * promiscuous mode*/ descr = pcap_open_live(dev,BUFSIZ,1,-1,errbuf); if(descr == NULL) { printf("pcap_open_live(): %s\n",errbuf); exit(1); } if(argc > 2) { /* Lets try and compile the program.. non-optimized */ if(pcap_compile(descr,&fp,argv[2],0,netp) == -1) { fprintf(stderr,"Error calling pcap_compile\n"); exit(1); } /* set the compiled program as the filter */ if(pcap_setfilter(descr,&fp) == -1) { fprintf(stderr,"Error setting filter\n"); exit(1); } } /* fprintf(stdout,"IP src, Ip dst, len, proto\n" );*/ /* ... and loop */ init_db(); /* pid=fork(); if (pid<0) { exit(EXIT_FAILURE); } if (pid>0) { exit(EXIT_SUCCESS); } umask(0); sid=setsid(); if (sid < 0) { exit(EXIT_FAILURE); } if ((chdir("/")) < 0) { exit(EXIT_FAILURE); } close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); */ /* atoi(argv[1])*/ printf("%s",argv[1]); /* for(k=1;k<=nripc;k++) { printf("down=%lu up=%lu",ipc[k].tdownb,ipc[k].tupb); printf("--------------------\n"); } printf("++++\n"); prel(); for(k=1;k<=xnr;k++) { printf("down=%lu up=%lu",xipc[k].tdownb,xipc[k].tupb); printf("--------------------\n"); } */ /* save_data_zi(); save_data_luna(); save_data_an(); fprintf(stdout,"\nfinished%d\n",init_db()); */ return 0; }