示例#1
0
void send_wont(int option, int init) {
    if (init) {
        if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
                my_want_state_is_wont(option))
            return;
        set_my_want_state_wont(option);
        will_wont_resp[option]++;
    }
    netoprintf((char *)wont, option);

    DIAG(TD_OPTIONS, printoption("td: send wont", option));
}
示例#2
0
void send_dont(int option, int init) {
    if (init) {
        if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
                his_want_state_is_wont(option))
            return;
        set_his_want_state_wont(option);
        do_dont_resp[option]++;
    }
    netoprintf((char *) dont, option);

    DIAG(TD_OPTIONS, printoption("td: send dont", option));
}
示例#3
0
static void
_gettermname(void)
{
    /*
     * If the client turned off the option,
     * we can't send another request, so we
     * just return.
     */
    if (his_state_is_wont(TELOPT_TTYPE))
	return;
    settimer(baseline);
    netoprintf("%c%c%c%c%c%c", IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE);
    while (sequenceIs(ttypesubopt, baseline))
	ttloop();
}
示例#4
0
/*
 * The will/wont/do/dont state machines are based on Dave Borman's
 * Telnet option processing state machine.
 *
 * These correspond to the following states:
 *	my_state = the last negotiated state
 *	want_state = what I want the state to go to
 *	want_resp = how many requests I have sent
 * All state defaults are negative, and resp defaults to 0.
 *
 * When initiating a request to change state to new_state:
 *
 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
 *	do nothing;
 * } else {
 *	want_state = new_state;
 *	send new_state;
 *	want_resp++;
 * }
 *
 * When receiving new_state:
 *
 * if (want_resp) {
 *	want_resp--;
 *	if (want_resp && (new_state == my_state))
 *		want_resp--;
 * }
 * if ((want_resp == 0) && (new_state != want_state)) {
 *	if (ok_to_switch_to new_state)
 *		want_state = new_state;
 *	else
 *		want_resp++;
 *	send want_state;
 * }
 * my_state = new_state;
 *
 * Note that new_state is implied in these functions by the function itself.
 * will and do imply positive new_state, wont and dont imply negative.
 *
 * Finally, there is one catch.  If we send a negative response to a
 * positive request, my_state will be the positive while want_state will
 * remain negative.  my_state will revert to negative when the negative
 * acknowlegment arrives from the peer.  Thus, my_state generally tells
 * us not only the last negotiated state, but also tells us what the peer
 * wants to be doing as well.  It is important to understand this difference
 * as we may wish to be processing data streams based on our desired state
 * (want_state) or based on what the peer thinks the state is (my_state).
 *
 * This all works fine because if the peer sends a positive request, the data
 * that we receive prior to negative acknowlegment will probably be affected
 * by the positive state, and we can process it as such (if we can; if we
 * can't then it really doesn't matter).  If it is that important, then the
 * peer probably should be buffering until this option state negotiation
 * is complete.
 *
 */
void send_do(int option, int init) {
    if (init) {
        if ((do_dont_resp[option] == 0 && his_state_is_will(option)) ||
                his_want_state_is_will(option))
            return;
        /*
         * Special case for TELOPT_TM:  We send a DO, but pretend
         * that we sent a DONT, so that we can send more DOs if
         * we want to.
         */
        if (option == TELOPT_TM)
            set_his_want_state_wont(option);
        else
            set_his_want_state_will(option);
        do_dont_resp[option]++;
    }
    netoprintf((char *)doopt, option);

    DIAG(TD_OPTIONS, printoption("td: send do", option));
}
示例#5
0
/*
 * clientstat
 *
 * Process linemode related requests from the client.
 * Client can request a change to only one of linemode, editmode or slc's
 * at a time, and if using kludge linemode, then only linemode may be
 * affected.
 */
void clientstat(register int code, register int parm1, register int parm2)
{
    /*
	 * Get a copy of terminal characteristics.
	 */
    init_termbuf();

    /*
	 * Process request from client. code tells what it is.
	 */
    switch (code) {
#ifdef LINEMODE
    case TELOPT_LINEMODE:
        /*
		 * Don't do anything unless client is asking us to change
		 * modes.
		 */
        uselinemode = (parm1 == WILL);
        if (uselinemode != linemode) {
#ifdef KLUDGELINEMODE
            /*
			 * If using kludge linemode, make sure that
			 * we can do what the client asks.
			 * We can not turn off linemode if alwayslinemode
			 * and the ICANON bit is set.
			 */
            if (lmodetype == KLUDGE_LINEMODE) {
                if (alwayslinemode && tty_isediting()) {
                    uselinemode = 1;
                }
            }

            /*
			 * Quit now if we can't do it.
			 */
            if (uselinemode == linemode)
                return;

            /*
			 * If using real linemode and linemode is being
			 * turned on, send along the edit mode mask.
			 */
            if (lmodetype == REAL_LINEMODE && uselinemode)
#else /* KLUDGELINEMODE */
            if (uselinemode)
#endif /* KLUDGELINEMODE */
            {
                useeditmode = 0;
                if (tty_isediting())
                    useeditmode |= MODE_EDIT;
                if (tty_istrapsig)
                    useeditmode |= MODE_TRAPSIG;
                if (tty_issofttab())
                    useeditmode |= MODE_SOFT_TAB;
                if (tty_islitecho())
                    useeditmode |= MODE_LIT_ECHO;
                (void)netoprintf("%c%c%c%c%c%c%c", IAC,
                    SB, TELOPT_LINEMODE, LM_MODE,
                    useeditmode, IAC, SE);
                editmode = useeditmode;
            }

            tty_setlinemode(uselinemode);

            linemode = uselinemode;
        }
        break;

    case LM_MODE: {
        register int ack, changed;

        /*
		 * Client has sent along a mode mask.  If it agrees with
		 * what we are currently doing, ignore it; if not, it could
		 * be viewed as a request to change.  Note that the server
		 * will change to the modes in an ack if it is different from
		 * what we currently have, but we will not ack the ack.
		 */
        useeditmode &= MODE_MASK;
        ack = (useeditmode & MODE_ACK);
        useeditmode &= ~MODE_ACK;

        if (changed = (useeditmode ^ editmode)) {
            /*
			 * This check is for a timing problem.  If the
			 * state of the tty has changed (due to the user
			 * application) we need to process that info
			 * before we write in the state contained in the
			 * ack!!!  This gets out the new MODE request,
			 * and when the ack to that command comes back
			 * we'll set it and be in the right mode.
			 */
            if (ack)
                localstat();
            if (changed & MODE_EDIT)
                tty_setedit(useeditmode & MODE_EDIT);

            if (changed & MODE_TRAPSIG)
                tty_setsig(useeditmode & MODE_TRAPSIG);

            if (changed & MODE_SOFT_TAB)
                tty_setsofttab(useeditmode & MODE_SOFT_TAB);

            if (changed & MODE_LIT_ECHO)
                tty_setlitecho(useeditmode & MODE_LIT_ECHO);

            set_termbuf();

            if (!ack) {
                (void)netoprintf("%c%c%c%c%c%c%c", IAC,
                    SB, TELOPT_LINEMODE, LM_MODE,
                    useeditmode | MODE_ACK,
                    IAC, SE);
            }

            editmode = useeditmode;
        }

        break;

    } /* end of case LM_MODE */
#endif /* LINEMODE */

    case TELOPT_NAWS:
#ifdef TIOCSWINSZ
    {
        struct winsize ws = {0};

        def_col = parm1;
        def_row = parm2;
#ifdef LINEMODE
        /*
		 * Defer changing window size until after terminal is
		 * initialized.
		 */
        if (terminit() == 0)
            return;
#endif /* LINEMODE */

        /*
		 * Change window size as requested by client.
		 */

        ws.ws_col = parm1;
        ws.ws_row = parm2;
        (void)ioctl(pty, TIOCSWINSZ, (char*)&ws);
    }
#endif /* TIOCSWINSZ */

    break;

    case TELOPT_TSPEED: {
        def_tspeed = parm1;
        def_rspeed = parm2;
#ifdef LINEMODE
        /*
		 * Defer changing the terminal speed.
		 */
        if (terminit() == 0)
            return;
#endif /* LINEMODE */
        /*
		 * Change terminal speed as requested by client.
		 * We set the receive speed first, so that if we can't
		 * store seperate receive and transmit speeds, the transmit
		 * speed will take precedence.
		 */
        tty_rspeed(parm2);
        tty_tspeed(parm1);
        set_termbuf();

        break;

    } /* end of case TELOPT_TSPEED */

    default:
        /* What? */
        break;
    } /* end of switch */

    netflush();

} /* end of clientstat */
示例#6
0
/*
 * localstat
 *
 * This function handles all management of linemode.
 *
 * Linemode allows the client to do the local editing of data
 * and send only complete lines to the server.  Linemode state is
 * based on the state of the pty driver.  If the pty is set for
 * external processing, then we can use linemode.  Further, if we
 * can use real linemode, then we can look at the edit control bits
 * in the pty to determine what editing the client should do.
 *
 * Linemode support uses the following state flags to keep track of
 * current and desired linemode state.
 *	alwayslinemode : true if -l was specified on the telnetd
 * 	command line.  It means to have linemode on as much as
 *	possible.
 *
 * 	lmodetype: signifies whether the client can
 *	handle real linemode, or if use of kludgeomatic linemode
 *	is preferred.  It will be set to one of the following:
 *		REAL_LINEMODE : use linemode option
 *		KLUDGE_LINEMODE : use kludge linemode
 *		NO_LINEMODE : client is ignorant of linemode
 *
 *	linemode, uselinemode : linemode is true if linemode
 *	is currently on, uselinemode is the state that we wish
 *	to be in.  If another function wishes to turn linemode
 *	on or off, it sets or clears uselinemode.
 *
 *	editmode, useeditmode : like linemode/uselinemode, but
 *	these contain the edit mode states (edit and trapsig).
 *
 * The state variables correspond to some of the state information
 * in the pty.
 *	linemode:
 *		In real linemode, this corresponds to whether the pty
 *		expects external processing of incoming data.
 *		In kludge linemode, this more closely corresponds to the
 *		whether normal processing is on or not.  (ICANON in
 *		system V, or COOKED mode in BSD.)
 *		If the -l option was specified (alwayslinemode), then
 *		an attempt is made to force external processing on at
 *		all times.
 *
 * The following heuristics are applied to determine linemode
 * handling within the server.
 *	1) Early on in starting up the server, an attempt is made
 *	   to negotiate the linemode option.  If this succeeds
 *	   then lmodetype is set to REAL_LINEMODE and all linemode
 *	   processing occurs in the context of the linemode option.
 *	2) If the attempt to negotiate the linemode option failed,
 *	   then we try to use kludge linemode.  We test for this
 *	   capability by sending "do Timing Mark".  If a positive
 *	   response comes back, then we assume that the client
 *	   understands kludge linemode (ech!) and the
 *	   lmodetype flag is set to KLUDGE_LINEMODE.
 *	3) Otherwise, linemode is not supported at all and
 *	   lmodetype remains set to NO_LINEMODE (which happens
 *	   to be 0 for convenience).
 *	4) At any time a command arrives that implies a higher
 *	   state of linemode support in the client, we move to that
 *	   linemode support.
 *
 * A short explanation of kludge linemode is in order here.
 *	1) The heuristic to determine support for kludge linemode
 *	   is to send a do timing mark.  We assume that a client
 *	   that supports timing marks also supports kludge linemode.
 *	   A risky proposition at best.
 *	2) Further negotiation of linemode is done by changing the
 *	   the server's state regarding SGA.  If server will SGA,
 *	   then linemode is off, if server won't SGA, then linemode
 *	   is on.
 */
void localstat()
{
    void netflush();
    int need_will_echo = 0;

    /*
	 * Check for state of BINARY options.
	 */
    if (tty_isbinaryin()) {
        if (his_want_state_is_wont(TELOPT_BINARY))
            send_do(TELOPT_BINARY, 1);
    }
    else {
        if (his_want_state_is_will(TELOPT_BINARY))
            send_dont(TELOPT_BINARY, 1);
    }

    if (tty_isbinaryout()) {
        if (my_want_state_is_wont(TELOPT_BINARY))
            send_will(TELOPT_BINARY, 1);
    }
    else {
        if (my_want_state_is_will(TELOPT_BINARY))
            send_wont(TELOPT_BINARY, 1);
    }

    /*
	 * Check for changes to flow control if client supports it.
	 */
    if (his_state_is_will(TELOPT_LFLOW)) {
        if (tty_flowmode() != flowmode) {
            flowmode = tty_flowmode();
            (void)netoprintf("%c%c%c%c%c%c", IAC, SB,
                TELOPT_LFLOW, flowmode, IAC, SE);
        }
    }

    /*
	 * Check linemode on/off state
	 */
    uselinemode = tty_linemode();

    /*
	 * If alwayslinemode is on, and pty is changing to turn it off, then
	 * force linemode back on.
	 */
    if (alwayslinemode && linemode && !uselinemode) {
        uselinemode = 1;
        tty_setlinemode(uselinemode);
    }

    /*
	 * Do echo mode handling as soon as we know what the
	 * linemode is going to be.
	 * If the pty has echo turned off, then tell the client that
	 * the server will echo.  If echo is on, then the server
	 * will echo if in character mode, but in linemode the
	 * client should do local echoing.  The state machine will
	 * not send anything if it is unnecessary, so don't worry
	 * about that here.
	 *
	 * If we need to send the WILL ECHO (because echo is off),
	 * then delay that until after we have changed the MODE.
	 * This way, when the user is turning off both editing
	 * and echo, the client will get editing turned off first.
	 * This keeps the client from going into encryption mode
	 * and then right back out if it is doing auto-encryption
	 * when passwords are being typed.
	 */
    if (uselinemode) {
        if (tty_isecho())
            send_wont(TELOPT_ECHO, 1);
        else
            need_will_echo = 1;
    }

    /*
	 * If linemode is being turned off, send appropriate
	 * command and then we're all done.
	 */
    if (!uselinemode && linemode) {
#ifdef KLUDGELINEMODE
        if (lmodetype == REAL_LINEMODE) {
#endif /* KLUDGELINEMODE */
            send_dont(TELOPT_LINEMODE, 1);
#ifdef KLUDGELINEMODE
        }
        else if (lmodetype == KLUDGE_LINEMODE)
            send_will(TELOPT_SGA, 1);
#endif /* KLUDGELINEMODE */
        send_will(TELOPT_ECHO, 1);
        linemode = uselinemode;
        goto done;
    }

#ifdef KLUDGELINEMODE
    /*
	 * If using real linemode check edit modes for possible later use.
	 * If we are in kludge linemode, do the SGA negotiation.
	 */
    if (lmodetype == REAL_LINEMODE) {
#endif /* KLUDGELINEMODE */
        useeditmode = 0;
        if (tty_isediting())
            useeditmode |= MODE_EDIT;
        if (tty_istrapsig())
            useeditmode |= MODE_TRAPSIG;
        if (tty_issofttab())
            useeditmode |= MODE_SOFT_TAB;
        if (tty_islitecho())
            useeditmode |= MODE_LIT_ECHO;
#ifdef KLUDGELINEMODE
    }
    else if (lmodetype == KLUDGE_LINEMODE) {
        if (tty_isediting() && uselinemode)
            send_wont(TELOPT_SGA, 1);
        else
            send_will(TELOPT_SGA, 1);
    }
#endif /* KLUDGELINEMODE */

    /*
	 * Negotiate linemode on if pty state has changed to turn it on.
	 * Send appropriate command and send along edit mode, then all done.
	 */
    if (uselinemode && !linemode) {
#ifdef KLUDGELINEMODE
        if (lmodetype == KLUDGE_LINEMODE) {
            send_wont(TELOPT_SGA, 1);
        }
        else if (lmodetype == REAL_LINEMODE) {
#endif /* KLUDGELINEMODE */
            send_do(TELOPT_LINEMODE, 1);
            /* send along edit modes */
            (void)netoprintf("%c%c%c%c%c%c%c", IAC, SB,
                TELOPT_LINEMODE, LM_MODE, useeditmode,
                IAC, SE);
            editmode = useeditmode;
#ifdef KLUDGELINEMODE
        }
#endif /* KLUDGELINEMODE */
        linemode = uselinemode;
        goto done;
    }

#ifdef KLUDGELINEMODE
    /*
	 * None of what follows is of any value if not using
	 * real linemode.
	 */
    if (lmodetype < REAL_LINEMODE)
        goto done;
#endif /* KLUDGELINEMODE */

    if (linemode && his_state_is_will(TELOPT_LINEMODE)) {
        /*
		 * If edit mode changed, send edit mode.
		 */
        if (useeditmode != editmode) {
            /*
			 * Send along appropriate edit mode mask.
			 */
            (void)netoprintf("%c%c%c%c%c%c%c", IAC, SB,
                TELOPT_LINEMODE, LM_MODE, useeditmode,
                IAC, SE);
            editmode = useeditmode;
        }

        /*
		 * Check for changes to special characters in use.
		 */
        start_slc(0);
        check_slc();
        (void)end_slc(0);
    }

done:
    if (need_will_echo)
        send_will(TELOPT_ECHO, 1);
    /*
	 * Some things should be deferred until after the pty state has
	 * been set by the local process.  Do those things that have been
	 * deferred now.  This only happens once.
	 */
    if (_terminit == 0) {
        _terminit = 1;
        defer_terminit();
    }

    netflush();
    set_termbuf();
    return;

} /* end of localstat */
示例#7
0
/*
 * Main loop.  Select from pty and network, and
 * hand data to telnet receiver finite state machine.
 */
void telnet(int f, int p)
{
    int on = 1;
    char *HE;
    const char *IM;

    /*
     * Initialize the slc mapping table.
     */
    get_slc_defaults();

    /*
     * Do some tests where it is desireable to wait for a response.
     * Rather than doing them slowly, one at a time, do them all
     * at once.
     */
    if (my_state_is_wont(TELOPT_SGA))
	send_will(TELOPT_SGA, 1);
    /*
     * Is the client side a 4.2 (NOT 4.3) system?  We need to know this
     * because 4.2 clients are unable to deal with TCP urgent data.
     *
     * To find out, we send out a "DO ECHO".  If the remote system
     * answers "WILL ECHO" it is probably a 4.2 client, and we note
     * that fact ("WILL ECHO" ==> that the client will echo what
     * WE, the server, sends it; it does NOT mean that the client will
     * echo the terminal input).
     */
    send_do(TELOPT_ECHO, 1);
    
#ifdef	LINEMODE
    if (his_state_is_wont(TELOPT_LINEMODE)) {
	/*
	 * Query the peer for linemode support by trying to negotiate
	 * the linemode option.
	 */
	linemode = 0;
	editmode = 0;
	send_do(TELOPT_LINEMODE, 1);  /* send do linemode */
    }
#endif	/* LINEMODE */

    /*
     * Send along a couple of other options that we wish to negotiate.
     */
    send_do(TELOPT_NAWS, 1);
    send_will(TELOPT_STATUS, 1);
    flowmode = 1;  /* default flow control state */
    send_do(TELOPT_LFLOW, 1);
    
    /*
     * Spin, waiting for a response from the DO ECHO.  However,
     * some REALLY DUMB telnets out there might not respond
     * to the DO ECHO.  So, we spin looking for NAWS, (most dumb
     * telnets so far seem to respond with WONT for a DO that
     * they don't understand...) because by the time we get the
     * response, it will already have processed the DO ECHO.
     * Kludge upon kludge.
     */
    while (his_will_wont_is_changing(TELOPT_NAWS)) {
	ttloop();
    }
    
    /*
     * But...
     * The client might have sent a WILL NAWS as part of its
     * startup code; if so, we'll be here before we get the
     * response to the DO ECHO.  We'll make the assumption
     * that any implementation that understands about NAWS
     * is a modern enough implementation that it will respond
     * to our DO ECHO request; hence we'll do another spin
     * waiting for the ECHO option to settle down, which is
     * what we wanted to do in the first place...
     */
    if (his_want_state_is_will(TELOPT_ECHO) &&
	his_state_is_will(TELOPT_NAWS)) {
	while (his_will_wont_is_changing(TELOPT_ECHO))
	    ttloop();
    }
    /*
     * On the off chance that the telnet client is broken and does not
     * respond to the DO ECHO we sent, (after all, we did send the
     * DO NAWS negotiation after the DO ECHO, and we won't get here
     * until a response to the DO NAWS comes back) simulate the
     * receipt of a will echo.  This will also send a WONT ECHO
     * to the client, since we assume that the client failed to
     * respond because it believes that it is already in DO ECHO
     * mode, which we do not want.
     */
    if (his_want_state_is_will(TELOPT_ECHO)) {
	DIAG(TD_OPTIONS, netoprintf("td: simulating recv\r\n"););
	willoption(TELOPT_ECHO);
    }
示例#8
0
static
int
getterminaltype(char *name)
{
    int retval = -1;
    (void)name;

    settimer(baseline);
#if defined(AUTHENTICATE)
    /*
     * Handle the Authentication option before we do anything else.
     */
    send_do(TELOPT_AUTHENTICATION, 1);
    while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
	ttloop();
    if (his_state_is_will(TELOPT_AUTHENTICATION)) {
	retval = auth_wait(name);
    }
#endif

#if	defined(ENCRYPT)
    send_will(TELOPT_ENCRYPT, 1);
#endif
    send_do(TELOPT_TTYPE, 1);
    send_do(TELOPT_TSPEED, 1);
    send_do(TELOPT_XDISPLOC, 1);
    send_do(TELOPT_ENVIRON, 1);
    while (
#if	defined(ENCRYPT)
	   his_do_dont_is_changing(TELOPT_ENCRYPT) ||
#endif
	   his_will_wont_is_changing(TELOPT_TTYPE) ||
	   his_will_wont_is_changing(TELOPT_TSPEED) ||
	   his_will_wont_is_changing(TELOPT_XDISPLOC) ||
	   his_will_wont_is_changing(TELOPT_ENVIRON)) {
	ttloop();
    }
#if	defined(ENCRYPT)
    /*
     * Wait for the negotiation of what type of encryption we can
     * send with.  If autoencrypt is not set, this will just return.
     */
    if (his_state_is_will(TELOPT_ENCRYPT)) {
	encrypt_wait();
    }
#endif
    if (his_state_is_will(TELOPT_TSPEED)) {
	netoprintf("%c%c%c%c%c%c", 
		   IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE);
    }
    if (his_state_is_will(TELOPT_XDISPLOC)) {
	netoprintf("%c%c%c%c%c%c", 
		   IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE);
    }
    if (his_state_is_will(TELOPT_ENVIRON)) {
	netoprintf("%c%c%c%c%c%c", 
		   IAC, SB, TELOPT_ENVIRON, TELQUAL_SEND, IAC, SE);
    }
    if (his_state_is_will(TELOPT_TTYPE)) {
       netoprintf("%c%c%c%c%c%c", 
		  IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE);
    }
    if (his_state_is_will(TELOPT_TSPEED)) {
	while (sequenceIs(tspeedsubopt, baseline))
	    ttloop();
    }
    if (his_state_is_will(TELOPT_XDISPLOC)) {
	while (sequenceIs(xdisplocsubopt, baseline))
	    ttloop();
    }
    if (his_state_is_will(TELOPT_ENVIRON)) {
	while (sequenceIs(environsubopt, baseline))
	    ttloop();
    }
    if (his_state_is_will(TELOPT_TTYPE)) {
	char first[256], last[256];

	while (sequenceIs(ttypesubopt, baseline))
	    ttloop();

	/*
	 * If the other side has already disabled the option, then
	 * we have to just go with what we (might) have already gotten.
	 */
	if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
	    /*
	     * Due to state.c, terminaltype points to a static char[41].
	     * Therefore, this assert cannot fail, and therefore, strings
	     * arising from "terminaltype" can be safely strcpy'd into
	     * first[] or last[].
	     */
	    assert(strlen(terminaltype) < sizeof(first));

	    strcpy(first, terminaltype);

	    for(;;) {
		/*
		 * Save the unknown name, and request the next name.
		 */
		strcpy(last, terminaltype);

		_gettermname();
		assert(strlen(terminaltype) < sizeof(first));

		if (terminaltypeok(terminaltype))
		    break;

		if (!strcmp(last, terminaltype) ||
		    his_state_is_wont(TELOPT_TTYPE)) {
		    /*
		     * We've hit the end.  If this is the same as
		     * the first name, just go with it.
		     */
		    if (!strcmp(first, terminaltype))
			break;
		    /*
		     * Get the terminal name one more time, so that
		     * RFC1091 compliant telnets will cycle back to
		     * the start of the list.
		     */
		     _gettermname();
		    assert(strlen(terminaltype) < sizeof(first));

		    if (strcmp(first, terminaltype)) {
			/*
			 * first[] came from terminaltype, so it must fit
			 * back in.
			 */
			strcpy(terminaltype, first);
		    }
		    break;
		}
	    }
	}
    }
    return(retval);
}  /* end of getterminaltype */