/* Program to measure packet loss between two hosts Copyright (C) 2008 Kasper Dupont This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include #ifndef HAVE_NETINET_IN_H #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #define SAMPLES 16 #define DELAY 15 static inline void errstop(const char *s) { perror(s); exit(1); } int main(int argc, char ** argv) { struct sockaddr_in sa; socklen_t sal; struct hostent *host; uint16_t local_port=0, remote_port=0; int sock_fd; int index=0; int counters[SAMPLES]; int lastprinted=SAMPLES-1; int nexttolastprinted=-1; int total_counter=0; if (argc == 4) { local_port=atoi(argv[1]); remote_port=atoi(argv[3]); } if ((local_port == 0) || (remote_port == 0)) { fprintf(stderr,"Usage: %s \n",argv[0]); return 1; } host=gethostbyname(argv[2]); if (!host) { perror(argv[2]); return 1; } sock_fd=socket(PF_INET,SOCK_DGRAM,0); if (sock_fd == -1) errstop("scoket"); sa.sin_family = AF_INET; sa.sin_port = htons(local_port); sa.sin_addr.s_addr=0; if (bind(sock_fd,(struct sockaddr*)&sa,sizeof(sa))) errstop("bind"); sa.sin_family = AF_INET; sa.sin_port = htons(remote_port); sa.sin_addr.s_addr=*(unsigned long int *)(host->h_addr); sal=sizeof(sa); fcntl(sock_fd,F_SETFL,O_NONBLOCK); while(1) { int foobuf,counter,i; ++index; counters[index%SAMPLES]=0; sendto(sock_fd,"",1,0,&sa,sal); sleep(DELAY); while (recvfrom(sock_fd,&foobuf,2,0,&sa,&sal) == 1) { ++total_counter; ++counters[index%SAMPLES]; } counter=0; for (i=0;i SAMPLES) && (counter != lastprinted)) { if ((counter > lastprinted + 1) || (lastprinted > counter + 1) || (lastprinted == 0)) { lastprinted=counter; nexttolastprinted=-1; } else if (counter !=nexttolastprinted) { nexttolastprinted=lastprinted; lastprinted=counter; } if (counter == lastprinted) { char *s,*c; time_t t=time(NULL); s=ctime(&t); c=strchr(s,'\n'); if (c) *c=0; printf("%s - Received total: %d - last %d minutes: %d\n", s,total_counter,(SAMPLES*DELAY)/60,counter); fflush(stdout); lastprinted=counter; } } } }