aboutsummaryrefslogblamecommitdiff
path: root/tst/net.c
blob: eec6788b627fd70e34d809fe05576b8466099e4c (plain) (tree)






































































































































































































































































                                                                               
#include <stdio.h>
#include <stdlib.h>

#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>

#include <errno.h>

#include <string.h>
#include <strings.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <Ecore_Config.h>

#define NB_MAX 10
#define ___NICK "ddero"

#define _EOF "\r\n"

typedef struct
{
   char     *host;
   int       port;

} t_servinfo;

char     *
format_send(char *msg)
{
   char     *tosend;

   /*unsigned int i; */

   tosend = malloc((strlen(msg) + 2) * sizeof(char));
   if (tosend == NULL)
   {
      fprintf(stderr, "Memory fault\n");
      exit(-2);
   }

   strcpy(tosend, msg);
   strcat(tosend, _EOF);

  /*
  for (i = 0 ; i < strlen(msg) + 2 ; i++)
	{
		fprintf (stderr, "[%d] %X %c\n", i, tosend[i], tosend[i]);
	}
  */
   return (tosend);
}

unsigned short
sendmsg_len(char *msg)
{
   return (strlen(msg) + 2);
}

void
sendtoserv(int fd, char *msg)
{
   printf("send: %s\n", msg);
   if ((send(fd, format_send(msg), sendmsg_len(msg), 0)) == -1)
   {
      perror("send");
      exit(-3);
   }
}

/*char*	dir_from_path (char* path)
{
	char*		dir;
	unsigned int	i;

	for ( i = strlen (path) -1 ; i != 0 ; i-- )
	{
		if ( path[i] == '/')
			break;
	}

	dir = strndup (path, i);
	dir [i] = '\0';

	printf ("dir: %s\n", dir);
	
	return (dir);
}*/

void
loadconfig(t_servinfo * s_info)
{
   if (ecore_config_init("ebic") != ECORE_CONFIG_ERR_SUCC)
   {
      printf("Cannot init Ecore_Config");
      exit(-1);
   }

   ecore_config_string_default("/config/server/host", "beber.melee");
   ecore_config_int_default("/config/server/port", 6667);

   ecore_config_load();

   s_info->host = ecore_config_string_get("/config/server/host");
   s_info->port = ecore_config_int_get("/config/server/port");

   ecore_config_shutdown();
   ecore_config_save();

   printf("sinfo->host : %s\n", s_info->host);
   printf("sinfo->port : %d\n", s_info->port);
}

int
get_max(int *tab)
{
   int       i, max;

   max = -1;

   for (i = 0; tab[i]; i++)
   {
      if (tab[i] > max)
	 max = tab[i];
   }

   return (max);
}

int
main(int argc, char **argv)
{
   int       sock;
   struct sockaddr_in dest_host;
   struct hostent *hostent;

   struct timeval wait_time;

   int       count;
   int       std;
   char      buff[NB_MAX];
   char     *tmp;

   int       r_sel;

   fd_set    fds_r;
   fd_set    fds_w;

   int       i = 0;

   t_servinfo serveur;

   loadconfig(&serveur);

   if (argc < 3)
   {
      fprintf(stderr, "Loading from conf ...\n");
   }
   else
   {
      printf("connecting to %s:%s\n", argv[1], argv[2]);
   }

/*	serveur.host = argv[1];
	serveur.port = atoi (argv[2]); */

   printf("host: %s\n", serveur.host);
   printf("port: %d\n", serveur.port);

   printf("gethostbyname (%s)\n", serveur.host);
   if ((hostent = gethostbyname(serveur.host)) == NULL)
   {
      fprintf(stderr, "%s could not be found\n", serveur.host);
      exit(-1);
   }

   memset(&dest_host, 0, sizeof(struct sockaddr_in));

   dest_host.sin_family = AF_INET;
   dest_host.sin_addr.s_addr = ((struct in_addr *)hostent->h_addr)->s_addr;
   dest_host.sin_port = htons((short)serveur.port);

   printf("socket\n");
   if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
   {
      perror("socket");
      exit(-1);
   }

   printf("connect\n");
   if ((connect(sock, (struct sockaddr *)&dest_host, sizeof(dest_host))) == -1)
   {
      perror("connect");
      exit(-1);
   }

   std = open("/dev/input", O_RDONLY);
   perror("open");

   printf("%d\n", std);

   FD_ZERO(&fds_r);
   FD_ZERO(&fds_w);
   FD_SET(std, &fds_r);
   FD_SET(sock, &fds_r);
   FD_SET(std, &fds_w);
   FD_SET(sock, &fds_w);

//      wait_time.tv_sec        = 1000000 / 1000000;*/          /* Nb sec */
//      wait_time.tv_usec       = 1000000 % 1000000;            /* Nb µsec */

   do
   {
      memset(buff, 0, NB_MAX - 1);

      r_sel = select(FD_SETSIZE, &fds_r, &fds_w, NULL, 0);

      if (FD_ISSET(std, &fds_r))
	 printf("fds_r: clavier\n");
      if (FD_ISSET(sock, &fds_r))
	 printf("fds_r: reseau\n");
      if (FD_ISSET(std, &fds_w))
	 printf("fds_w: clavier\n");
      if (FD_ISSET(sock, &fds_w))
	 printf("fds_w: reseau\n");

      count = recv(sock, buff, NB_MAX - 1, 0);
      printf("%d: %s", i, buff);
      if (i == 1)
	 sendtoserv(sock, "NICK " ___NICK);
      if (i == 2)
	 sendtoserv(sock, "USER beber beber guybrush.melee :Ronnie Reagan");
      if (i == 3)
	 sendtoserv(sock, "OPER beber beber");
      if (i == 3)
	 sendtoserv(sock, "JOIN #opers");

      if (i > 45)
      {
	 tmp =
	    malloc(sizeof(char) * (strlen(buff) + strlen("PRIVMSG #opers :")));
	 sprintf(tmp, "%s %s", "PRIVMSG #opers :", buff);
	 sendtoserv(sock, tmp);
	 free(tmp);
      }

      //sleep (1);
      i++;
   }
   while (count != 0);

   close(sock);

   return (0);
}