/*
 * 
 *
 * Author:	John D. Carlson
 *		Martin Hunt
 *		Networks and Distributed Computing
 *		Computing & Communications
 *		University of Washington
 *		Administration Building, AG-44
 *		Seattle, WA  98195
 *		Internet: johnc@cac.washington.edu
 *
 *
 * Copyright 1992 by the University of Washington
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appears in all copies and that both the
 * above copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the University of Washington not be
 * used in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  This software is made
 * available "as is", and
 * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
 * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
 * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
 * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */
#ifndef lint
static char rcsid[] =
    "@(#) $Header: /usr/staff/martinh/tcpview/RCS/telnet.c,v 1.1 1993/04/22 20:36:38 martinh Exp $ (UW)";
#endif

#include <stdio.h>
#include <sys/types.h>

int PrintFrames = 0;  /* print headers ? */
int EndSub=1;  /* begin/end subnegotiation */
int TType=0;  /* request and response for terminal types */

#ifdef __STDC__
int getbyte(void);
u_short getword(void);
u_long getlword(void);
void processIAC(void);
void checksmallopts(void);
#else
int getbyte();
u_short getword();
u_long getlword();
void processIAC();
void checksmallopts();
#endif

main(argc, argv)
int argc;
char *argv[];
{
  int c, i=0;
  int type;
  int length;

  if( argc > 1 )
    if( *argv[1]=='-' && argv[1][1]=='f' )
      PrintFrames=1;

  while( (c = getbyte()) != EOF) {
    if( c=='\n' ) {
      putchar('\n');
      c = getbyte();
      if( c=='\r' )
	continue;
    } else if( c=='\r' ) {
      putchar('\n');
      c = getbyte();
      if( c=='\n')
	continue;
    } 

    if( c=='\t' )
      putchar(c);
    else if (c == 255) {
	putchar('\n');
	processIAC();
      }
    else { /* not IAC or subnegotiation must be c > 31 && c < 128 */
      if (0 < c && c < 128)
	putchar(c);
      else
	fprintf(stdout,"<%d>",c);
    }
  }
}

/* handle IAC stuff */
void processIAC()
{
int c;

  c=getbyte();
  switch (c) {
  case 250: /* SB */
    printf("<Start Subnegotiation 855>");
    EndSub=0;
    while (!EndSub) { /* keep looking until we find an end to subnegotiation */
      checksmallopts();
    }
    break;
  case 251: /* WILL */
    printf("<WILL 854>");
    checksmallopts();
    break;
  case 252: /* WONT */
    printf("<WONT 854>");
    checksmallopts();
    break;
  case 253: /* DO */
    printf("<DO 854>");
    checksmallopts();
    break;
  case 254: /* DONT */
    printf("<DONT 854>");
    checksmallopts();
    break;
  default:  /* don't know what it is so indicate command */
    if (c) /* if not null ie c == 0, then print it */
      putchar(c);
  }
}


/* need to handle option which are larger than 31 */
void checksmallopts()
{
int c;

  c=getbyte();
  switch (c) {
  case 0:
    if (TType && !EndSub) {
      printf("<IS>");
      TType=0;
    }
    else
      printf("<Transmit Binary: 856>");
    break;
  case 1:
    if (TType && !EndSub) {
      printf("<SEND>");
      TType=0;
    }
    else
      printf("<Echo: 857>");
    break;
  case 2:
    printf("<Reconnection>");
    break;
  case 3:  
    printf("<Suppress Go Ahead: 858>");
    break;
  case 4:
    printf("<Approx Msg Size Negotiation>");
    break;
  case 5:
    printf("<Request Status: 859>");
    break;
  case 6:
    printf("<Request Timing Mark for synchronization: 860>");
    break;
  case 7:
    printf("<Remote Controlled Trans and Echo: 726>");
    break;
  case 8:
    printf("<Output Line Width>");
    break;
  case 9:
    printf("<Output Page Size>");
    break;
  case 10:
    printf("<Output CR Disposition: 652>");
    break;
  case 11:
    printf("<Output HT Stops: 653>");
    break;
  case 12:
    printf("<Output HT Disposition: 654>");
    break;
  case 13:
    printf("<Output FF Disposition: 655>");
    break;
  case 14:
    printf("<Output VT Stops: 656>");
    break;
  case 15:
    printf("<Output VT Disposition: 657>");
    break;
  case 16:
    printf("<Output LF Disposition: 658>");
    break;
  case 17:
    printf("<Extended ASCII: 698>");
    break;
  case 18:
    printf("<Logout: 727>");
    break;
  case 19:
    printf("<Byte Macro: 735>");
    break;
  case 20:
    printf("<Data Entry Terminal: 1043>");
    break;
  case 21:
    printf("<SUPDUP: 734>");
    break;
  case 22:
    printf("<SUPDUP Output: 749>");
    break;
  case 23:
    printf("<Send Location: 779>");
    break;
  case 24:
    printf("<Terminal Type: 1091>");
    TType=1;
    break;
  case 25:
    printf("<Terminiate data sent with EOR: 885>");
    break;
  case 26:
    printf("<TACACS User Identification: 927>");
    break;
  case 27:
    printf("<Output Marking: 933>");
    break;
  case 28:
    printf("<Terminal Location Number: 946>");
    break;
  case 29:
    printf("<Telnet 3270 Regime: 1041>");
    break;
  case 30:
    printf("<X.3 PAD: 1053>");
    break;
  case 31:
    printf("<Negotiate Window Size: 1073>");
    break;
  case 32:
    printf("<Terminal Speed: 1079>");
    break;
  case 33:
    printf("<Remote Flow Control: 1080>");
    break;
  case 34:
    printf("<Line Mode: 1184>");
    break;
  case 35:
    printf("<X Display Location 1096>");
    break;
  case 36:
    printf("<Environment Option>");
    break;
  case 37:
    printf("<Authentication Option>");
    break;
  case 38:
    printf("<Encryption Option>");
    break;
  case 239:
    printf("<Terminiate data sent with EOR: 885>");
    break;
  case 240: /* SE */
    printf("<End Subnegotiation 855>");
    EndSub=1;
    break;
  case 241:
    printf("<NOOP>");
    break;
  case 242:
    printf("<DMARK data stream protion of SYNCH: 854>");
    break;
  case 243:
    printf("<Break signal: 854>");
    break;
  case 244:
    printf("<Interrupt Process: 854>");
    break;
  case 245:
    printf("<Abort Output: 854>");
    break;
  case 246:
    printf("<Are You There: 854>");
    break;
  case 247:
    printf("<Erase Character: 854>");
    break;
  case 248:
    printf("<Erase Line: 854>");
    break;
  case 249:
    printf("<Go Ahead: 858>");
    break;
  default:
    if (!EndSub && c != 255) /* print char if in subnegot and not an IAC */
      putchar(c);
    else if (EndSub && c != 255) /* print # if not in subnegot and not IAC */
      fprintf(stdout,"<%d>",c);
  }
if (EndSub)  /* insert a newline unless in the midst of subnegotiation */
  putchar('\n');
}


/* some basic I/O functions */

int getbyte()
{
  u_char b;
  if(hexread(&b,1))
    return( (int)b );
  else 
    return( EOF );
}

u_short getword()
{
  unsigned short buf;
  if( hexread(&buf,2) )
    return( ntohs(buf) );
  else
    return( EOF );
}

u_long getlword()
{
  unsigned int buf;
  if( hexread(&buf,4) )
    return( ntohl(buf) );
  else
    return( EOF );
}




syntax highlighted by Code2HTML, v. 0.9.1