sconsole.c 3.34 KB
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */

/*
 * sconsole.c:  use this instead of telnet to connect to simos slave consoles
 *
 * telnet does character echoing, line buffering, etc. that interact badly
 * with the operating system running inside of simos
 *
 * Connect stdin/out to a tcp socket connection with no line editing or anything
 */

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <sys/termios.h>
#include <stropts.h>
#include <poll.h>

#define BUFSIZ 4096
char buf[BUFSIZ];

int main(int argc, char** argv)
{
   int sock_fd, tmp;
   struct sockaddr_in server;
   struct hostent* hp;
   struct termios ios, ios_save; 

   if (argc != 3) {
      fprintf(stderr, "usage: %s machine_name port\n", argv[0]);
      return 1;
   }

   sock_fd = socket(AF_INET, SOCK_STREAM, 0);
   if (sock_fd < 0) {
      perror("opening stream socket");
      return 1;
   }

   server.sin_family = AF_INET;
   hp = gethostbyname(argv[1]);
   if (hp == 0) {
      fprintf(stderr, "%s: unknown host\n", argv[1]);
      return 1;
   }

   memcpy((char*) &server.sin_addr, (char*) hp->h_addr, (uint) hp->h_length);
   server.sin_port = (ushort) atoi(argv[2]);

   if (connect(sock_fd, (struct sockaddr*) &server, sizeof(server)) < 0) {
      perror("connecting stream socket");
      return 1;
   }

   /* Tell TCP not to delay small packets.  This greatly speeds up
      interactive response. */
   tmp = 1;
   if (setsockopt (sock_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&tmp, sizeof(tmp))<0) {
      perror("setting socket option");
      return 1;
   }

   /* Turn off all local character processing by my tty.  This
    * is the responsibility of the simos I am connected to.
    */

   if (tcgetattr(0, &ios_save) < 0) {
      perror("tcgetattr\n");
   }
   ios = ios_save;
   ios.c_iflag &= ~(ISTRIP|IGNCR|IXOFF|IXON|ICRNL);
   ios.c_oflag &= ~OPOST;
   ios.c_lflag &= ~(ISIG|ICANON|ECHO);
   ios.c_cc[VMIN] = 1;
   ios.c_cc[VTIME] = 0;
   if (tcsetattr(0, TCSANOW, &ios) < 0) {
      perror("tcsetattr\n");
   }

   /* set up for nonblocking I/O */

   if (fcntl(0, F_SETFL, FNONBLK)) {
      perror("setting noblock on stdin");
      return 1;
   }

   if (fcntl(sock_fd, F_SETFL, FNONBLK)) {
      perror("setting noblock on socket");
      return 1;
   }

   /* main loop */

   {
      struct pollfd fds[2];
      int done;

      /* we poll on stdin and on the socket */
      fds[0].fd = 0;
      fds[0].events = POLLIN;
      fds[1].fd = sock_fd;
      fds[1].events = POLLIN;
      
      while (1) {
	 fds[0].revents = 0;
	 fds[1].revents = 0;
	 poll(fds, 2, -1);

	 if (fds[0].revents & POLLIN) {
	    done = read(0, buf, BUFSIZ);
	    if (done == 0) break;
	    if (done > 0) write(sock_fd, buf, done);
	 }
	 if (fds[1].revents & POLLIN) {
	    done = read(sock_fd, buf, BUFSIZ);
	    if (done == 0) break;
	    if (done > 0) write(1, buf, done);
	 }
      }
   }

   if (tcsetattr(0, TCSAFLUSH, &ios_save) < 0) {
      perror("tcsetattr\n");
   }

   if (fcntl(0, F_SETFL, 0)) {
      perror("turning off noblock on stdin");
      return 1;
   }

   printf("\n");
   return 0;
}