#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <msql.h>

#define PING	"/bin/ping"
#define DBSPACE "PINGER"

#define	MAX	60
#define	WIDTH	64

char host[MAX][WIDTH], local[MAX][WIDTH], remote[MAX][WIDTH];
char name[MAX][WIDTH], operatori[200];
float response[MAX];
int  vreme[MAX], timeout=10, debug;
int  tajm[MAX], count[MAX], mesto[MAX];

int read_config(char *);
float ping(char *, int, int);
void strip(char*);

/*- ----------------------------------------------------------------------- -*/
/*- 									    -*/
/*- 									    -*/
/*- 									    -*/
/*- ----------------------------------------------------------------------- -*/
main (int argc, char **argv) {
int i, hosts, odg, out=0, lresponse, conn;
int sck;
float loc, rem;
char help[200];


if ((hosts=read_config("check.conf"))==0)
   exit(0);
if (debug) {
   printf("Procitana konfiguracija ... ");
   printf("Ucitani podaci za %d linja ...\n", hosts);
   }

if ((sck=msqlConnect(NULL))<0) {
   out=1;
   printf("Ne mogu da se povezem na mSQL server.\n");
   printf("%s\n", msqlErrMsg);
   }

if (msqlSelectDB(sck, DBSPACE)<0) {
   out=1;
   printf("Ne mogu da izaberem DBspace %s.\n", DBSPACE);
   printf("%s\n", msqlErrMsg);
   }


for (i=0; i<hosts-1 && i<MAX && !out; i++) {
   printf("%d, %s\n", mesto[i], name[i]);
   }


msqlClose(sck);

printf("Program je zavrsio\n");

}

/*- ----------------------------------------------------------------------- -*/
/*- 									    -*/
/*- 									    -*/
/*- 									    -*/
/*- ----------------------------------------------------------------------- -*/
void strip(char *sta) {
char what[200];
int i;

for(i=0; i<200 && i<strlen(sta) && sta[i]!=' '; i++);
strcpy(what, sta+i);
strcpy(sta, what);

for (i=strlen(sta)-1; i>0 && sta[i]!=' '; i++)
   sta[i]=0;

}

/*- ----------------------------------------------------------------------- -*/
/*- 									    -*/
/*- 									    -*/
/*- 									    -*/
/*- ----------------------------------------------------------------------- -*/
float ping(char *host, int ack, int cnt) {
FILE *prog;
char help[200], ohelp[200], j1[200], j2[200], j3[200], jk[200];
int i;
float rtn;
float a1, a2, a3;

if (host[0]==0)
   return 0;

if (debug)
   printf("---> Pingujem host %s\n", host);
   
sprintf(help, "%s -q -n -c %d %s", PING, cnt, host);

if ((prog=popen(help, "r"))==NULL) {
   printf("Ne mogu da startujem PING.\n");
   return -1;
   }

while(!feof(prog)) {
   strcpy(ohelp, help);
   fgets(help, 200, prog);
   }

pclose(prog);

rtn=0;
if (strncmp(help, "round-trip ", 11)==0) {
   sscanf(help+25, "%f/%f/%f", &a1, &a2, &a3);
   rtn=a2;
   }

return rtn;
}

/*- ----------------------------------------------------------------------- -*/
/*- 									    -*/
/*- 									    -*/
/*- 									    -*/
/*- ----------------------------------------------------------------------- -*/
int read_config(char *conf) {
FILE *infile;
int  tekuci, lch=0, i, bgn, line, out=0;
int defack=0, deftimeout=1, defcount=5;
char help[200], cmd[MAX], rest[200], net[100];

if ((infile=fopen(conf, "r"))==NULL) {
   printf("Ne mogu da otvorim konfiguracioni fajl '%s'\n", conf);
   return 0;
   }

tekuci=0; line=0;
while(!feof(infile)) {
   fgets(help, 200, infile);
   line++;
   cmd[0]=0; rest[0]=0;
   lch=0; bgn=0;
   if (help[strlen(help)-1]=='\n') 
      help[strlen(help)-1]=0;
   for(i=0; help[i]!=0 && i<100; i++) 
     if (help[i]=='*') {
        help[i]=0;
        }
   
   sscanf(help, "%s %s", cmd, rest);
   for(i=0; cmd[i]!=0 && i<20; i++) 
      cmd[i]=toupper(cmd[i]);

   if (help[0]==0 || cmd[0]==0);
   else if (strcmp(cmd, "VERBOSE")==0) {
	   debug=1;
           if (debug)
              printf("Debuging je podesen: %d\n", debug);
           }
   else if (strcmp(cmd, "OPERATOR")==0) {
           strcpy(operatori, rest);
           if (debug)
              printf("Definisani OPERATORI: %s\n", operatori);
           }
   else if (strcmp(cmd, "DEFINE")==0) {
           strcpy(name[tekuci], rest);
           tajm[tekuci]=deftimeout;
           vreme[tekuci]=defack;
           host[tekuci][0]=0;
           local[tekuci][0]=0;
           remote[tekuci][0]=0;
           count[tekuci]=defcount;
           if (debug)
              printf("Definisana veza \"%s\"\n", name[tekuci]);
           }
   else if (strcmp(cmd, "MESTO")==0) {
           mesto[tekuci]=atoi(rest);
           if (debug)
              printf("---> Mesto linije: %d\n", mesto[tekuci]);
           }
   else if (strcmp(cmd, "LOCAL")==0) {
           if (rest[0]=='.') {
              sprintf(help, "%s%s", net, rest);
              strcpy(rest, help);
              }               
           strcpy(local[tekuci], rest);
           if (debug)
              printf("---> Lokalni router: %s\n", local[tekuci]);
           }
   else if (strcmp(cmd, "REMOTE")==0) {
           if (rest[0]=='.') {
              sprintf(help, "%s%s", net, rest);
              strcpy(rest, help);
              }               
           strcpy(remote[tekuci], rest);
           if (debug)
              printf("---> Udaljeni router: %s\n", remote[tekuci]);
           }
   else if (strcmp(cmd, "ADDRESS")==0) {
           if (rest[0]=='.') {
              sprintf(help, "%s%s", net, rest);
              strcpy(rest, help);
              }               
           strcpy(host[tekuci], rest);
           if (debug)
              printf("---> Udaljeni racunar: %s\n", host[tekuci]);
           }
   else if (strcmp(cmd, "ACK")==0) {
           vreme[tekuci]=atoi(rest);
           if (debug)
              printf("---> Vreme odgovora: %d\n", vreme[tekuci]);           
           }
   else if (strcmp(cmd, "TIMEOUT")==0) {
           tajm[tekuci]=atoi(rest);
           if (debug)
              printf("---> Vreme odgovora: %d\n", tajm[tekuci]);
           }
   else if (strcmp(cmd, "COUNT")==0) {
           count[tekuci]=atoi(rest);
           if (debug)
              printf("---> Broj PING paketa: %d\n", count[tekuci]);
           }
   else if (strcmp(cmd, "DEFSTATUS")==0) {
           response[tekuci]=atoi(rest);
           if (debug)
              printf("DEFAULT stanje: %d\n", response[tekuci]);
           }
   else if (strcmp(cmd, "DEFCOUNT")==0) {
           defcount=atoi(rest);
           if (debug)
              printf("DEFAULT broj paketa: %d\n", defcount);
           }
   else if (strcmp(cmd, "DEFACK")==0) {
           defack=atoi(rest);
           if (debug)
              printf("DEFAULT ACK: %s\n", rest);
           }
   else if (strcmp(cmd, "DEFTIMEOUT")==0) {
           deftimeout=atoi(rest);
           if (debug)
              printf("DEFAULT TIMEOUT: %d\n", deftimeout);
           }
   else if (strcmp(cmd, "END")==0) {
           tekuci++;
           strcpy(name[tekuci], "Unknown");
           }
   else if (strcmp(cmd, "WAKEUP")==0) {
           timeout=atoi(rest);
 	   if (debug)
 	      printf("Vreme \"SPAVANJA\" podeseno na %d\n", timeout);
           }
   else if (strcmp(cmd, "NET")==0) {
           strcpy(net, rest);
 	   if (debug)
 	      printf("Lokalna mreza je %s\n", net);
           }
   else {
      printf("Greska u konfiguracionom fajlu, linija %d\n", line);
      printf("---> %s\n", help); 
      out=1;
      }

   }

fclose(infile);

if (out)
   return 0;

return tekuci;
}
