//  LTelnet       Linux Telnet Client  


/*  Ltelnet.c  is a Linux Telnet Client written in C
    Copyright (C) 2002   Nicola Piazzolla
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  
   LICENSE: GNU/GPL (http://www.gnu.org/licenses/gpl.html)


 						Written by  kc  

Readme :  gcc ltelnet.c -o ltelnet 



*/




#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>


#define HELP		 'h'
#define VERSION		 'v'


#define  _DEFAULT_PORT   23

#define MAX_LINES 1000

int check (int sock);		// Function : Send & receive 

int connects (char *hostname, int PORT);	// Connect to the server.



int
main (int argc, char **argv)
{

  char char_opt[] = { HELP, VERSION };

  int ch;
  extern int optind;

  if (argc == 1)
    {
      printf ("Type %s -h\n", argv[0]);
    }

  while ((ch = getopt (argc, argv, char_opt)) != -1)
    {

      switch (ch)
	{





	case VERSION:
	  printf ("Ltelnet is a Linux Client Telnet\n");
	  printf ("Version : 1.4.5\n");
	  printf ("Written by Nicola Piazzolla\n");
	  printf ("Reports bugs at kcc@users.sourceforge.net\n");

	  break;





	case HELP:

	  printf ("Ltelnet is a Linux Telnet Client\n");

	  printf ("Usage : Type ' %s  host [ port ]'\n", argv[0]);
	  printf ("Options :\n");
	  printf (" -h  : This Help\n");
	  printf (" -v  : Version\n");


	  break;



	}

    }

  if (optind < argc)
    {


      if (argc == 2)
	{

	  connects (argv[optind], _DEFAULT_PORT);
	}

      else if (argc == 3)
	{

	  connects (argv[optind], atoi (argv[2]));

	}
    }





  return 0;
}


int
connects (char *hostname, int PORT)
{

  struct sockaddr_in server;
  struct hostent *he;
  int tcp_socket;
  int fd;


  he = gethostbyname (hostname);

  if (!he)
    {
      fprintf (stderr, "Host unknown\n");
      return -1;
    }

  server.sin_port = htons (PORT);
  server.sin_family = AF_INET;

  memcpy (&server.sin_addr.s_addr, he->h_addr, he->h_length);

  tcp_socket = socket (AF_INET, SOCK_STREAM, 0);

  fd = connect (tcp_socket, (struct sockaddr *) &server, sizeof (server));



  if (fd == 0)
    {
      printf ("Login..\n");


      if (check (tcp_socket) != -1)
	fprintf (stderr, "Error\n");


    }
  else

    printf ("Impossible to connect\n");

  return -1;
}









int
check (int sock)
{

  pid_t pid;
  char reading[MAX_LINES];
  int fd, fd1;
  char writing[MAX_LINES];

  pid = fork ();

  if (pid == 0)
    {

      while (1)
	{
	  memset (reading, '\0', sizeof (reading));	// Son  :)
	  fd = read (sock, reading, MAX_LINES);

	  if (fd == 0)
	    exit (-1);


	  printf ("%s", reading);
	}
    }


  else
    {
      while (1)
	{			// The father  :) 


	  fgets (writing, MAX_LINES, stdin);

	  fd1 = write (sock, writing, strlen (writing));

	}
    }

  close (sock);


  return -1;

}
