/******************************************************** 
 *                 SWD.MOD.037/101 
 * 
 * File:           ubeam.c 
 *
 * P.N.:           ESWP-S744A-DRV 
 * 
 * Rel.:           2.0.0 
 *
 * Product:        DY4 SVME744 AIX Device Driver TECS Interface 
 *
 * Property:       Ele.Si.A. s.r.l. - All rights reserved 
 *
 * Author:         Alberto Neri 
 * 
 * Creation Date:  Feb. 06, 1996 
 *
 * Description: 
 * 	Program to test UDP/IP socket
 * 
 * Revisions History: 
 *
 *      2.0.0  - S. Palma - Dec. 14, 1999
 *      Modified style of the file as described in
 *      the procedure SWD.PRC.0014.
 *
 ********************************************************/

/*@****************************************************** 
 *	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>
#ifdef LYNXOS
#include <bsd/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#endif   
#define NUM_CAR 4096
#define PORT 12345
unsigned int tvsub(struct timeval *, 
                   struct timeval *);
int read_timeo(int fd, int usec);
/*@******************************************************
 * Function: main 
 * 
 * Description: 
 * 
 * INPUT: 
 * 
 * OUTPUT: 
 *
 * RETURN VALUE: 
 * 
 ********************************************************/ 
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;
  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;
  if (argc < 5) {
    printf("Usage: %s <local_hostname> <remote hostname> <num of iterations> <packet len> [<port>]\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 */      	

  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]);
  }
    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);
  
  for (i=0; i<NUM_CAR; i++) str_out[i] = (i % 10) + '0';
  
  for (j = 0; j < iter; j++) {
    //    str_out[0]=j;
    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++;
    timeo.tv_sec=1;
    timeo.tv_usec=0; 
    namelen = sizeof(struct sockaddr);
    /*	if(read_timeo(s, 5000) == 0)
	{
	printf(".");
	fflush(stdout);
	lost++;
	j--;
	}
	else
	{
	if ((num_car_letti = recvfrom(s, str_in, NUM_CAR, 
	0, &rem_addr, &namelen))  == -1 ) {
	
	perror("ubeam: recvfrom failed: ");
	//	      exit(1);
	
	}
	recvd++;
	
	
    */  
    
    
#if 0
    printf("\nReceived %d characters ", num_car_letti);
    for (i=0; i<num_car_letti; i++) printf("%c", str_in[i]);  
    printf("\n");
    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", *((unsigned short*)&(src_addr.sa_data[0])));
    printf("     family     0x%04x \n",src_addr.sa_family);
    printf("\n\n");
#endif 
    sleep(1);
  }
  /* end for */
  close(s);
  
} /* end of main */
