home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / inetutils-1.2-src.tgz / tar.out / fsf / inetutils / telnetd / slc.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  12KB  |  496 lines

  1. /*
  2.  * Copyright (c) 1989, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)slc.c    8.2 (Berkeley) 5/30/95";
  36. #endif /* not lint */
  37.  
  38. #ifdef HAVE_CONFIG_H
  39. #include <config.h>
  40. #endif
  41.  
  42. #include "telnetd.h"
  43.  
  44. #ifdef    LINEMODE
  45. /*
  46.  * local varibles
  47.  */
  48. static unsigned char    *def_slcbuf = (unsigned char *)0;
  49. static int        def_slclen = 0;
  50. static int        slcchange;    /* change to slc is requested */
  51. static unsigned char    *slcptr;    /* pointer into slc buffer */
  52. static unsigned char    slcbuf[NSLC*6];    /* buffer for slc negotiation */
  53.  
  54. /*
  55.  * send_slc
  56.  *
  57.  * Write out the current special characters to the client.
  58.  */
  59.     void
  60. send_slc()
  61. {
  62.     register int i;
  63.  
  64.     /*
  65.      * Send out list of triplets of special characters
  66.      * to client.  We only send info on the characters
  67.      * that are currently supported.
  68.      */
  69.     for (i = 1; i <= NSLC; i++) {
  70.         if ((slctab[i].defset.flag & SLC_LEVELBITS) == SLC_NOSUPPORT)
  71.             continue;
  72.         add_slc((unsigned char)i, slctab[i].current.flag,
  73.                             slctab[i].current.val);
  74.     }
  75.  
  76. }  /* end of send_slc */
  77.  
  78. /*
  79.  * default_slc
  80.  *
  81.  * Set pty special characters to all the defaults.
  82.  */
  83.     void
  84. default_slc()
  85. {
  86.     register int i;
  87.  
  88.     for (i = 1; i <= NSLC; i++) {
  89.         slctab[i].current.val = slctab[i].defset.val;
  90.         if (slctab[i].current.val == (cc_t)(_POSIX_VDISABLE))
  91.             slctab[i].current.flag = SLC_NOSUPPORT;
  92.         else
  93.             slctab[i].current.flag = slctab[i].defset.flag;
  94.         if (slctab[i].sptr) {
  95.             *(slctab[i].sptr) = slctab[i].defset.val;
  96.         }
  97.     }
  98.     slcchange = 1;
  99.  
  100. }  /* end of default_slc */
  101. #endif    /* LINEMODE */
  102.  
  103. /*
  104.  * get_slc_defaults
  105.  *
  106.  * Initialize the slc mapping table.
  107.  */
  108.     void
  109. get_slc_defaults()
  110. {
  111.     register int i;
  112.  
  113.     init_termbuf();
  114.  
  115.     for (i = 1; i <= NSLC; i++) {
  116.         slctab[i].defset.flag = 
  117.             spcset(i, &slctab[i].defset.val, &slctab[i].sptr);
  118.         slctab[i].current.flag = SLC_NOSUPPORT; 
  119.         slctab[i].current.val = 0; 
  120.     }
  121.  
  122. }  /* end of get_slc_defaults */
  123.  
  124. #ifdef    LINEMODE
  125. /*
  126.  * add_slc
  127.  *
  128.  * Add an slc triplet to the slc buffer.
  129.  */
  130.     void
  131. add_slc(func, flag, val)
  132.     register char func, flag;
  133.     register cc_t val;
  134. {
  135.  
  136.     if ((*slcptr++ = (unsigned char)func) == 0xff)
  137.         *slcptr++ = 0xff;
  138.  
  139.     if ((*slcptr++ = (unsigned char)flag) == 0xff)
  140.         *slcptr++ = 0xff;
  141.  
  142.     if ((*slcptr++ = (unsigned char)val) == 0xff)
  143.         *slcptr++ = 0xff;
  144.  
  145. }  /* end of add_slc */
  146.  
  147. /*
  148.  * start_slc
  149.  *
  150.  * Get ready to process incoming slc's and respond to them.
  151.  *
  152.  * The parameter getit is non-zero if it is necessary to grab a copy
  153.  * of the terminal control structures.
  154.  */
  155.     void
  156. start_slc(getit)
  157.     register int getit;
  158. {
  159.  
  160.     slcchange = 0;
  161.     if (getit)
  162.         init_termbuf();
  163.     (void) sprintf((char *)slcbuf, "%c%c%c%c",
  164.                     IAC, SB, TELOPT_LINEMODE, LM_SLC);
  165.     slcptr = slcbuf + 4;
  166.  
  167. }  /* end of start_slc */
  168.  
  169. /*
  170.  * end_slc
  171.  *
  172.  * Finish up the slc negotiation.  If something to send, then send it.
  173.  */
  174.     int
  175. end_slc(bufp)
  176.     register unsigned char **bufp;
  177. {
  178.     register int len;
  179.     void netflush();
  180.  
  181.     /*
  182.      * If a change has occured, store the new terminal control
  183.      * structures back to the terminal driver.
  184.      */
  185.     if (slcchange) {
  186.         set_termbuf();
  187.     }
  188.  
  189.     /*
  190.      * If the pty state has not yet been fully processed and there is a
  191.      * deferred slc request from the client, then do not send any
  192.      * sort of slc negotiation now.  We will respond to the client's
  193.      * request very soon.
  194.      */
  195.     if (def_slcbuf && (terminit() == 0)) {
  196.         return(0);
  197.     }
  198.  
  199.     if (slcptr > (slcbuf + 4)) {
  200.         if (bufp) {
  201.             *bufp = &slcbuf[4];
  202.             return(slcptr - slcbuf - 4);
  203.         } else {
  204.             (void) sprintf((char *)slcptr, "%c%c", IAC, SE);
  205.             slcptr += 2;
  206.             len = slcptr - slcbuf;
  207.             writenet(slcbuf, len);
  208.             netflush();  /* force it out immediately */
  209.             DIAG(TD_OPTIONS, printsub('>', slcbuf+2, len-2););
  210.         }
  211.     }
  212.     return (0);
  213.  
  214. }  /* end of end_slc */
  215.  
  216. /*
  217.  * process_slc
  218.  *
  219.  * Figure out what to do about the client's slc
  220.  */
  221.     void
  222. process_slc(func, flag, val)
  223.     register unsigned char func, flag;
  224.     register cc_t val;
  225. {
  226.     register int hislevel, mylevel, ack;
  227.  
  228.     /*
  229.      * Ensure that we know something about this function
  230.      */
  231.     if (func > NSLC) {
  232.         add_slc(func, SLC_NOSUPPORT, 0);
  233.         return;
  234.     }
  235.  
  236.     /*
  237.      * Process the special case requests of 0 SLC_DEFAULT 0
  238.      * and 0 SLC_VARIABLE 0.  Be a little forgiving here, don't
  239.      * worry about whether the value is actually 0 or not.
  240.      */
  241.     if (func == 0) {
  242.         if ((flag = flag & SLC_LEVELBITS) == SLC_DEFAULT) {
  243.             default_slc();
  244.             send_slc();
  245.         } else if (flag == SLC_VARIABLE) {
  246.             send_slc();
  247.         }
  248.         return;
  249.     }
  250.  
  251.     /*
  252.      * Appears to be a function that we know something about.  So
  253.      * get on with it and see what we know.
  254.      */
  255.  
  256.     hislevel = flag & SLC_LEVELBITS;
  257.     mylevel = slctab[func].current.flag & SLC_LEVELBITS;
  258.     ack = flag & SLC_ACK;
  259.     /*
  260.      * ignore the command if:
  261.      * the function value and level are the same as what we already have;
  262.      * or the level is the same and the ack bit is set
  263.      */
  264.     if (hislevel == mylevel && (val == slctab[func].current.val || ack)) {
  265.         return;
  266.     } else if (ack) {
  267.         /*
  268.          * If we get here, we got an ack, but the levels don't match.
  269.          * This shouldn't happen.  If it does, it is probably because
  270.          * we have sent two requests to set a variable without getting
  271.          * a response between them, and this is the first response.
  272.          * So, ignore it, and wait for the next response.
  273.          */
  274.         return;
  275.     } else {
  276.         change_slc(func, flag, val);
  277.     }
  278.  
  279. }  /* end of process_slc */
  280.  
  281. /*
  282.  * change_slc
  283.  *
  284.  * Process a request to change one of our special characters.
  285.  * Compare client's request with what we are capable of supporting.
  286.  */
  287.     void
  288. change_slc(func, flag, val)
  289.     register char func, flag;
  290.     register cc_t val;
  291. {
  292.     register int hislevel, mylevel;
  293.     
  294.     hislevel = flag & SLC_LEVELBITS;
  295.     mylevel = slctab[func].defset.flag & SLC_LEVELBITS;
  296.     /*
  297.      * If client is setting a function to NOSUPPORT
  298.      * or DEFAULT, then we can easily and directly
  299.      * accomodate the request.
  300.      */
  301.     if (hislevel == SLC_NOSUPPORT) {
  302.         slctab[func].current.flag = flag;
  303.         slctab[func].current.val = (cc_t)_POSIX_VDISABLE;
  304.         flag |= SLC_ACK;
  305.         add_slc(func, flag, val);
  306.         return;
  307.     }
  308.     if (hislevel == SLC_DEFAULT) {
  309.         /*
  310.          * Special case here.  If client tells us to use
  311.          * the default on a function we don't support, then
  312.          * return NOSUPPORT instead of what we may have as a
  313.          * default level of DEFAULT.
  314.          */
  315.         if (mylevel == SLC_DEFAULT) {
  316.             slctab[func].current.flag = SLC_NOSUPPORT;
  317.         } else {
  318.             slctab[func].current.flag = slctab[func].defset.flag;
  319.         }
  320.         slctab[func].current.val = slctab[func].defset.val;
  321.         add_slc(func, slctab[func].current.flag,
  322.                         slctab[func].current.val);
  323.         return;
  324.     }
  325.  
  326.     /*
  327.      * Client wants us to change to a new value or he
  328.      * is telling us that he can't change to our value.
  329.      * Some of the slc's we support and can change,
  330.      * some we do support but can't change,
  331.      * and others we don't support at all.
  332.      * If we can change it then we have a pointer to
  333.      * the place to put the new value, so change it,
  334.      * otherwise, continue the negotiation.
  335.      */
  336.     if (slctab[func].sptr) {
  337.         /*
  338.          * We can change this one.
  339.          */
  340.         slctab[func].current.val = val;
  341.         *(slctab[func].sptr) = val;
  342.         slctab[func].current.flag = flag;
  343.         flag |= SLC_ACK;
  344.         slcchange = 1;
  345.         add_slc(func, flag, val);
  346.     } else {
  347.         /*
  348.         * It is not possible for us to support this
  349.         * request as he asks.
  350.         *
  351.         * If our level is DEFAULT, then just ack whatever was
  352.         * sent. 
  353.         *
  354.         * If he can't change and we can't change,
  355.         * then degenerate to NOSUPPORT.
  356.         *
  357.         * Otherwise we send our level back to him, (CANTCHANGE
  358.         * or NOSUPPORT) and if CANTCHANGE, send
  359.         * our value as well.
  360.         */
  361.         if (mylevel == SLC_DEFAULT) {
  362.             slctab[func].current.flag = flag;
  363.             slctab[func].current.val = val;
  364.             flag |= SLC_ACK;
  365.         } else if (hislevel == SLC_CANTCHANGE &&
  366.                     mylevel == SLC_CANTCHANGE) {
  367.             flag &= ~SLC_LEVELBITS;
  368.             flag |= SLC_NOSUPPORT;
  369.             slctab[func].current.flag = flag;
  370.         } else {
  371.             flag &= ~SLC_LEVELBITS;
  372.             flag |= mylevel;
  373.             slctab[func].current.flag = flag;
  374.             if (mylevel == SLC_CANTCHANGE) {
  375.                 slctab[func].current.val =
  376.                     slctab[func].defset.val;
  377.                 val = slctab[func].current.val;
  378.             }
  379.         }
  380.         add_slc(func, flag, val);
  381.     }
  382.  
  383. }  /* end of change_slc */
  384.  
  385. #if    defined(USE_TERMIO) && (VEOF == VMIN)
  386. cc_t oldeofc = '\004';
  387. #endif
  388.  
  389. /*
  390.  * check_slc
  391.  *
  392.  * Check the special characters in use and notify the client if any have
  393.  * changed.  Only those characters that are capable of being changed are
  394.  * likely to have changed.  If a local change occurs, kick the support level
  395.  * and flags up to the defaults.
  396.  */
  397.     void
  398. check_slc()
  399. {
  400.     register int i;
  401.  
  402.     for (i = 1; i <= NSLC; i++) {
  403. #if    defined(USE_TERMIO) && (VEOF == VMIN)
  404.         /*
  405.          * In a perfect world this would be a neat little
  406.          * function.  But in this world, we should not notify
  407.          * client of changes to the VEOF char when
  408.          * ICANON is off, because it is not representing
  409.          * a special character.
  410.          */
  411.         if (i == SLC_EOF) {
  412.             if (!tty_isediting())
  413.                 continue;
  414.             else if (slctab[i].sptr)
  415.                 oldeofc = *(slctab[i].sptr);
  416.         }
  417. #endif    /* defined(USE_TERMIO) && defined(SYSV_TERMIO) */
  418.         if (slctab[i].sptr &&
  419.                 (*(slctab[i].sptr) != slctab[i].current.val)) {
  420.             slctab[i].current.val = *(slctab[i].sptr);
  421.             if (*(slctab[i].sptr) == (cc_t)_POSIX_VDISABLE)
  422.                 slctab[i].current.flag = SLC_NOSUPPORT;
  423.             else
  424.                 slctab[i].current.flag = slctab[i].defset.flag;
  425.             add_slc((unsigned char)i, slctab[i].current.flag,
  426.                         slctab[i].current.val);
  427.         }
  428.     }
  429. }  /* check_slc */
  430.  
  431. /*
  432.  * do_opt_slc
  433.  *
  434.  * Process an slc option buffer.  Defer processing of incoming slc's
  435.  * until after the terminal state has been processed.  Save the first slc
  436.  * request that comes along, but discard all others.
  437.  *
  438.  * ptr points to the beginning of the buffer, len is the length.
  439.  */
  440.     void
  441. do_opt_slc(ptr, len)
  442.     register unsigned char *ptr;
  443.     register int len;
  444. {
  445.     register unsigned char func, flag;
  446.     cc_t val;
  447.     register unsigned char *end = ptr + len;
  448.  
  449.     if (terminit()) {  /* go ahead */
  450.         while (ptr < end) {
  451.             func = *ptr++;
  452.             if (ptr >= end) break;
  453.             flag = *ptr++;
  454.             if (ptr >= end) break;
  455.             val = (cc_t)*ptr++;
  456.  
  457.             process_slc(func, flag, val);
  458.  
  459.         }
  460.     } else {
  461.         /*
  462.          * save this slc buffer if it is the first, otherwise dump
  463.          * it.
  464.          */
  465.         if (def_slcbuf == (unsigned char *)0) {
  466.             def_slclen = len;
  467.             def_slcbuf = (unsigned char *)malloc((unsigned)len);
  468.             if (def_slcbuf == (unsigned char *)0)
  469.                 return;  /* too bad */
  470.             memmove(def_slcbuf, ptr, len);
  471.         }
  472.     }
  473.  
  474. }  /* end of do_opt_slc */
  475.  
  476. /*
  477.  * deferslc
  478.  *
  479.  * Do slc stuff that was deferred.
  480.  */
  481.     void
  482. deferslc()
  483. {
  484.     if (def_slcbuf) {
  485.         start_slc(1);
  486.         do_opt_slc(def_slcbuf, def_slclen);
  487.         (void) end_slc(0);
  488.         free(def_slcbuf);
  489.         def_slcbuf = (unsigned char *)0;
  490.         def_slclen = 0;
  491.     }
  492.  
  493. }  /* end of deferslc */
  494.  
  495. #endif    /* LINEMODE */
  496.