/******************************************************** 
 * 
 * File:           SendUDP.c 
 *
 * P.N.:            
 * 
 * Rel.:           2.0.0 
 *
 * Product:        UDP Latency test for loop controls 
 *
 * Property:       UNINA - All rights reserved 
 *
 * Author:         Fabio Garufi 
 * 
 * Creation Date:  Mar. 16, 2005 
 *
 * Description: 
 * 	Program to test UDP/IP socket round trip Send part
 * 
 * Revisions History: 
 *
 ********************************************************/

/*@****************************************************** 
 *	INCLUDE SECTION
 ********************************************************/
#include <sys/types.h>
#include <fcntl.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>
#ifdef LYNXOS
#include <bsd/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>

#ifdef vmpc
#include  <cetiatimer.h>
#endif //vmpc
#endif   //LYNXOS

#define NUM_CAR 4096
#define PORT 12345
unsigned int tvsub(struct timeval *, 
                   struct timeval *);
int read_timeo(int fd, int usec);

typedef struct
{
	int id;
	struct timeval timev;
} data_t;

/*@******************************************************
 * Function: main 
 * 
 * Description: 
 * 
 * INPUT: 
 * 
 * OUTPUT: 
 *
 * RETURN VALUE: 
 * 
 ********************************************************/ 
int main(argc, argv)
int     argc;
char ** argv;
{
  int                  s, fd;
  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;
  data_t      	       data;
  data_t      	     * pdata=&data;
  int                  i,
                       j;
  int                  namelen;
  int                  num_car_letti;
  struct timeval       start;
  struct timeval       end;
  struct timeval       tot_start;
  struct timeval       tot_end;
  unsigned int         max = 0;
  unsigned int         min = 0xFFFFFFFF;
  unsigned int         average = 0;
  unsigned int         curr;
  int                  iter;
  int                  lost =0;
  int                  sent=0, recvd=0;
  char               * host;
  unsigned int         delta_t;
  float                through, msgsec;
  unsigned long        addr_tmp;
  int val, len;
  u_short port;
  struct timeval timeo;
#ifdef vmpc
    FILE *fdt;
    struct itimerval interval;
#endif

  if (argc < 5) {
    printf("Usage: %s <local_hostname> <remote hostname> <num of iterations> <packet len> [<port>] [<interval>]\n", argv[0]);
    exit(0);	
  } /* end if */

  if (sscanf(argv[3], "%d", &iter) != 1 || 
      iter <= 0) {
    printf("Usage: %s incorrect\n", argv[3]);
    exit(0);	  	
  } /* end if */      	
#ifdef vmpc  
    if ((fd=open("/dev/tim0",O_RDWR)) < 0) {
           printf("Open failed\n");
            return(-1);
    }
//    fdt = fopen("./testtimer.txt","w"); 
#endif

  if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
	perror("ubeam: 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"));
   loc_host = gethostbyname(argv[1]);
  if (loc_host == (struct hostent *)NULL) {
    perror("ubeam: error in gethostbyname: ");
    exit(1);
  } /* end if */
  
  loc_addr.sin_family = AF_INET;
  if(argc >= 6){
    port = (u_short)atoi(argv[5]);
    if(port == 0) port = PORT; 
  }
    else
      port = PORT;
  loc_addr.sin_port = htons(port);
  loc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  inet_pton(AF_INET, argv[1], & loc_addr.sin_addr);
  /*    if (bind(s, (struct sockaddr*)&loc_addr, sizeof(struct sockaddr_in)) < 0) {
    printf("errno = %d \n", errno);
	perror("ubeam: bind failed: ");
	exit(1);
	}*/ /* end if */
  
  rem_addr.sin_port   = htons(port);  
  host = argv[2];
  if (atoi(host) > 0 )  {
    /* Numeric */
    rem_addr.sin_family      = AF_INET;
    rem_addr.sin_addr.s_addr = inet_addr(argv[2]);
  } 
  else {
    if ((rem_host = gethostbyname(argv[2])) == NULL) {
      printf("%s: host %s unknown \n", argv[0], argv[2]);
      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;
  } /* end if */
  printf("ip_address = 0x%x (",ntohl(rem_addr.sin_addr.s_addr));
 /*
  printf("%d."  , (rem_addr.sin_addr.s_addr >> 24) & 0x000000FF);
  printf("%d."  , (rem_addr.sin_addr.s_addr >> 16) & 0x000000FF);
  printf("%d."  , (rem_addr.sin_addr.s_addr >> 8) & 0x000000FF);
  printf("%d)\n", rem_addr.sin_addr.s_addr & 0x000000FF);
  printf("      port       = %d \n",ntohs(rem_addr.sin_port));
  printf("      family     = 0x%x \n",rem_addr.sin_family);
 */
  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);
 
  len =  sizeof(timeo);

#ifdef vmpc
  if(argc == 7)
    {
  	interval.it_value.tv_usec = atoi(argv[6]);
  	interval.it_value.tv_sec = 0;
  	interval.it_interval.tv_sec = 0;
  	interval.it_interval.tv_usec = atoi(argv[6]);
	printf("interval %d us\n", interval.it_value.tv_usec);
    }
  else
  {
  	interval.it_value.tv_sec = 0;
  	interval.it_value.tv_usec = 500;
  	interval.it_interval.tv_sec = 0;
  	interval.it_interval.tv_usec = 500;
  }
  ioctl(fd,TIMER_INIT,&interval); 
#endif
   for (i=0; i<NUM_CAR; i++) str_out[i] = (i % 10) + '0';
   gettimeofday(&tot_start, 0);
   data.timev.tv_sec = tot_start.tv_sec;
   data.timev.tv_usec = tot_start.tv_usec;
   data.id = 0;
   memcpy(str_out, pdata, sizeof(data_t));
    
  if (sendto(s, str_out, atoi(argv[4]), 0, (struct sockaddr*)&rem_addr, 
	     sizeof(struct sockaddr_in)) < 0) {
    perror("ubeam 0: sendto failed: ");
    exit(1);
  }  
  for (j = 0; j < iter; j++) {
        gettimeofday(&start, 0);
	data.timev.tv_sec = start.tv_sec;
	data.timev.tv_usec = start.tv_usec;
	data.id = j+1;
	memcpy(str_out, pdata, sizeof(data_t));
//	printf("%d  \n", *(int *)str_out);
        if (sendto(s, str_out, atoi(argv[4]), 0, (struct sockaddr*)&rem_addr, 
                   sizeof(struct sockaddr_in)) < 0) {
          perror("ubeam: sendto failed: ");
          exit(1);
        }
	sent++;

#ifdef vmpc
	  ioctl(fd,TIMER_WAIT);
#endif 
  } /* end for */
 
close(s);
#ifdef vmpc  
  close(fd);
//  fclose(fdt);
#endif

} /* end of main */

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 */



int read_timeo(int fd, int usec)
{
  fd_set rset;
  struct timeval timeo;
  FD_ZERO(&rset);
  FD_SET(fd, &rset);
  timeo.tv_sec = 0;
  timeo.tv_usec = usec;
  return(select(fd+1, &rset, NULL, NULL, &timeo));
} 

