/******************************************************** 
 *                 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<net/if.h>
#include <sys/time.h>
#define NUM_CAR 4096
#define PORT  12345

#ifdef LYNXOS

#endif //LYNXOS

int read_timeo(int fd, int usec);
/*@******************************************************
 * 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 src_addr;
  struct sockaddr sockname;
  struct hostent * loc_host;
  int i,j;
  int namelen;
  int num_car_letti;
  int  val;
  struct ifreq testioctl;
  u_short port;
  struct timeval timeo;

  int lost;
  if(argc <2)
    {
      printf("usage %s <local_IP> [<port>]\n", argv[0]);
      return -1;
    }

  if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
	perror("ubeam: socket call failed: ");
	exit(1);
  } /* end if */

 loc_host = gethostbyname(argv[1]);
  //loc_host = gethostbyaddr(argv[1], strlen(argv[1]), AF_INET);
  if (loc_host == (struct hostent *)NULL) {
    perror("ubeam: error in gethostbyname: ");
    exit(1);
  } /* end if */
  
  loc_addr.sin_family = AF_INET;
/*********************************************/
  if(argc == 3)
    port = (u_short)atoi(argv[2]);
  else
    port = PORT;

  loc_addr.sin_port = htons(port);
  loc_addr.sin_addr.s_addr = INADDR_ANY;
/*********************************************/
  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 */
  /*  
  if ((val =ioctl(s,0x89f0+2, &testioctl)) != 0)
  {
  	perror("ioctl failed");
  	printf(" val=%d", val); 
  } */
  printf("\n test ioctl = %d\n", testioctl.ifr_data);

  for (i=0; i<NUM_CAR; i++) str_out[i] = (i % 10) + '0';

  while (1) {

        namelen = sizeof(struct sockaddr);
	if(read_timeo(s, 5000) == 0)
	  {
	    printf(".");
	    lost++;
	  }
        if ((num_car_letti = recvfrom(s, str_in, NUM_CAR, 
                                      0, &src_addr, &namelen)) == -1) {
	    perror("ubeam: recvfrom failed: ");
	    exit(1);
        }
	puts(str_in);
#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        
      
 
  } /* end for */

  close(s);

} /* end of main */

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));
} 
