
/*@****************************************************** 
 *	INCLUDE SECTION
 ********************************************************/
#include <sys/types.h>
#ifdef LINUX
#include <linux/socket.h>
#else
#include <socket.h>
#endif
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
  
#include "cmd.h"
 
char buffer[1024];

unsigned int tvsub(struct timeval *,  struct timeval *);

main(argc, argv)
     int     argc;
     char ** argv;
{
  int                  s;
  char                 str_in[NUM_CAR];
  char                 str_out[NUM_CAR];
  unsigned int         dest_ip_addr;
  struct sockaddr_in   loc_addr;
  struct sockaddr_in   rem_addr;
  struct sockaddr      src_addr;
  struct sockaddr      sockname;
  struct hostent     * loc_host;
  struct hostent     * rem_host;
  int                  i,
    j;
  int                  namelen;
  int                  num_car_letti;
  unsigned int         max = 0;
  unsigned int         min = 0xFFFFFFFF;
  char               * host;
  unsigned long        addr_tmp;
  int val, len;
  struct cmd_t cmd;
  char *base, *p ;
  int size;
  struct timeval DTIUtime, MyTime; 

  if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    perror("sendcmd: socket call failed: ");
    exit(1);
  } /* end if */
  val=1; len =sizeof (val);
#ifdef LYNXOS 
  if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, len)==-1)
#else
  if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, &len)==-1)
#endif
    {
      perror("setsockopt error");
      exit(1);
    }
  loc_host = gethostbyname(getenv("HOSTNAME"));
  if (loc_host == (struct hostent *)NULL) {
    perror(": error in gethostbyname: ");
    exit(1);
  } /* end if */
  
  loc_addr.sin_family = AF_INET;

  loc_addr.sin_port = htons(12866);
  loc_addr.sin_addr.s_addr = htonl(INADDR_ANY);

  if (bind(s, (struct sockaddr*)&loc_addr, sizeof(struct sockaddr_in)) < 0) {
    printf("errno = %d \n", errno);
    perror("sendcmd: bind failed: ");
    exit(1);
  } /* end if */
  
  rem_addr.sin_port   = htons(12866);  
  host = argv[1];
  if (atoi(host) > 0 )  {
    /* Numeric */
    rem_addr.sin_family      = AF_INET;
    rem_addr.sin_addr.s_addr = inet_addr(host); //FG
  } 
  else {
    if ((rem_host = gethostbyname(host)) == NULL) {
      printf("%s: host %s unknown \n", argv[0], argv[1]);
      exit(1);			
    } /* end if */ 				
    
    rem_addr.sin_family = rem_host->h_addrtype;;
    bcopy(rem_host->h_addr, (char *)&addr_tmp, rem_host->h_length);

    rem_addr.sin_addr.s_addr = addr_tmp; //FG
  } /* end if */
  
  printf("ip_address = 0x%x (",rem_addr.sin_addr.s_addr);

  printf("%s)\n"  , inet_ntoa(rem_addr.sin_addr));
  printf("      port       = %d \n",ntohs(rem_addr.sin_port));
  printf("      family     = 0x%x \n",rem_addr.sin_family);

  /* End Connection Part */
  cmd.cmd = GETTIME;
  cmd.par_nr=0;
  memcpy(str_out, &cmd,2) ;
  size = sizeof(cmd);
  /*  if(argc >3 )
      {
      int nsize =0;
      for(i = 0; i<cmd.par_nr; i++)
      {
      int psize = strlen(cmd.params[i]); 
      memcpy(str_out+2*sizeof(char)+nsize, cmd.params[i], psize+1 );
      nsize += strlen(cmd.params[i]); 
      }

      size += nsize;
      }
  */
  if (sendto(s, str_out, size+1,
	     0, (struct sockaddr*)&rem_addr, 
	     sizeof(struct sockaddr_in)) < 0) {
    perror("sendcmd: sendto failed: ");
    exit(1);
  }
  gettimeofday(&MyTime, 0);
  
  namelen = sizeof(struct sockaddr);
        
  /* Return Receive */
 
  if ((num_car_letti = recvfrom(s, str_in, NUM_CAR, 
				0, &src_addr, &namelen)) == -1) {
    perror("sendcmd: recvfrom failed: ");
    exit(1);
  }
  base = str_in;
  size =0;

  printf("\nReceived %d characters ", num_car_letti);


  printf("from ip_address ");
  
  for (i=0; i<4; i++) { /* Quattro caratteri del IP address */
    printf("%d", *((unsigned char*)&(src_addr.sa_data[i+2])));
    if (i<3) printf(".");
  } /* end for */
  printf("\n");
  printf("     port %d\n", ntohs(*((unsigned short*)&(src_addr.sa_data[0]))));
  printf("     family     0x%04x \n",src_addr.sa_family);
  printf("\n\n");
  puts(str_in);
  DTIUtime.tv_sec = atol(&str_in[0]); 
  p = &str_in[0];
  i=0;
  while(!isspace(*p)){
    p++;
    i++;
  }
  DTIUtime.tv_usec = atol(&str_in[i]); 
  printf("DTIU sec = %d usec =%d\n",  DTIUtime.tv_sec,  DTIUtime.tv_usec);
  printf("LOCAL sec = %d usec =%d\n",  MyTime.tv_sec,  MyTime.tv_usec);
  printf("Difference (us) %d\n", tvsub(&MyTime, &DTIUtime));
}



unsigned int tvsub(struct timeval * t1, 
                   struct timeval * t0)
{
  unsigned int res;
  unsigned int aux;
  
  aux = t1->tv_sec - t0->tv_sec;
  if (aux > 0) {
  /* res = (aux - 1) * 1000000 + t1->tv_usec + 1000000 - t0->tv_usec; */
    res = aux * 1000000 + t1->tv_usec - t0->tv_usec;
  }
  else {
    res = t1->tv_usec - t0->tv_usec;
  } /* end if */	
  
  return(res);              
} /* end of tvsub */





