Home | History | Annotate | Line # | Download | only in telnetd
      1 /*	$NetBSD: defs.h,v 1.16 2007/02/21 21:14:07 hubertf Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	from: @(#)defs.h	8.1 (Berkeley) 6/4/93
     32  */
     33 
     34 /*
     35  * Telnet server defines
     36  */
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 
     40 #if defined(PRINTOPTIONS) && defined(DIAGNOSTICS)
     41 #define TELOPTS
     42 #define TELCMDS
     43 #define	SLC_NAMES
     44 #endif
     45 
     46 #include <sys/socket.h>
     47 #include <sys/wait.h>
     48 #include <fcntl.h>
     49 #include <sys/file.h>
     50 #include <sys/stat.h>
     51 #include <sys/time.h>
     52 #include <sys/poll.h>
     53 #include <sys/ioctl.h>
     54 
     55 #include <netinet/in.h>
     56 
     57 #include <arpa/telnet.h>
     58 
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <signal.h>
     62 #include <errno.h>
     63 #include <netdb.h>
     64 #include <syslog.h>
     65 #include <string.h>
     66 #include <termios.h>
     67 #include <time.h>
     68 #include <unistd.h>
     69 
     70 /*
     71  * I/O data buffers defines
     72  */
     73 #define	NETSLOP	64
     74 
     75 #define	NIACCUM(c)	do { \
     76 			    *netip++ = c; \
     77 			    ncc++; \
     78 			} while (0)
     79 
     80 /* clock manipulations */
     81 #define	settimer(x)	(clocks.x = ++clocks.system)
     82 #define	sequenceIs(x,y)	(clocks.x < clocks.y)
     83 
     84 /*
     85  * Linemode support states, in decreasing order of importance
     86  */
     87 #define REAL_LINEMODE	0x04
     88 #define KLUDGE_OK	0x03
     89 #define	NO_AUTOKLUDGE	0x02
     90 #define KLUDGE_LINEMODE	0x01
     91 #define NO_LINEMODE	0x00
     92 
     93 /*
     94  * Structures of information for each special character function.
     95  */
     96 typedef struct {
     97 	unsigned char	flag;		/* the flags for this function */
     98 	cc_t		val;		/* the value of the special character */
     99 } slcent, *Slcent;
    100 
    101 typedef struct {
    102 	slcent		defset;		/* the default settings */
    103 	slcent		current;	/* the current settings */
    104 	cc_t		*sptr;		/* a pointer to the char in */
    105 					/* system data structures */
    106 } slcfun, *Slcfun;
    107 
    108 #ifdef DIAGNOSTICS
    109 /*
    110  * Diagnostics capabilities
    111  */
    112 #define	TD_REPORT	0x01	/* Report operations to client */
    113 #define TD_EXERCISE	0x02	/* Exercise client's implementation */
    114 #define TD_NETDATA	0x04	/* Display received data stream */
    115 #define TD_PTYDATA	0x08	/* Display data passed to pty */
    116 #define	TD_OPTIONS	0x10	/* Report just telnet options */
    117 #endif /* DIAGNOSTICS */
    118 
    119 /*
    120  * We keep track of each side of the option negotiation.
    121  */
    122 
    123 #define	MY_STATE_WILL		0x01
    124 #define	MY_WANT_STATE_WILL	0x02
    125 #define	MY_STATE_DO		0x04
    126 #define	MY_WANT_STATE_DO	0x08
    127 
    128 /*
    129  * Macros to check the current state of things
    130  */
    131 
    132 #define	my_state_is_do(opt)		(options[opt]&MY_STATE_DO)
    133 #define	my_state_is_will(opt)		(options[opt]&MY_STATE_WILL)
    134 #define my_want_state_is_do(opt)	(options[opt]&MY_WANT_STATE_DO)
    135 #define my_want_state_is_will(opt)	(options[opt]&MY_WANT_STATE_WILL)
    136 
    137 #define	my_state_is_dont(opt)		(!my_state_is_do(opt))
    138 #define	my_state_is_wont(opt)		(!my_state_is_will(opt))
    139 #define my_want_state_is_dont(opt)	(!my_want_state_is_do(opt))
    140 #define my_want_state_is_wont(opt)	(!my_want_state_is_will(opt))
    141 
    142 #define	set_my_state_do(opt)		(options[opt] |= MY_STATE_DO)
    143 #define	set_my_state_will(opt)		(options[opt] |= MY_STATE_WILL)
    144 #define	set_my_want_state_do(opt)	(options[opt] |= MY_WANT_STATE_DO)
    145 #define	set_my_want_state_will(opt)	(options[opt] |= MY_WANT_STATE_WILL)
    146 
    147 #define	set_my_state_dont(opt)		(options[opt] &= ~MY_STATE_DO)
    148 #define	set_my_state_wont(opt)		(options[opt] &= ~MY_STATE_WILL)
    149 #define	set_my_want_state_dont(opt)	(options[opt] &= ~MY_WANT_STATE_DO)
    150 #define	set_my_want_state_wont(opt)	(options[opt] &= ~MY_WANT_STATE_WILL)
    151 
    152 /*
    153  * Tricky code here.  What we want to know is if the MY_STATE_WILL
    154  * and MY_WANT_STATE_WILL bits have the same value.  Since the two
    155  * bits are adjacent, a little arithmetic will show that by adding
    156  * in the lower bit, the upper bit will be set if the two bits were
    157  * different, and clear if they were the same.
    158  */
    159 #define my_will_wont_is_changing(opt) \
    160 			((options[opt]+MY_STATE_WILL) & MY_WANT_STATE_WILL)
    161 
    162 #define my_do_dont_is_changing(opt) \
    163 			((options[opt]+MY_STATE_DO) & MY_WANT_STATE_DO)
    164 
    165 /*
    166  * Make everything symmetrical
    167  */
    168 
    169 #define	HIS_STATE_WILL			MY_STATE_DO
    170 #define	HIS_WANT_STATE_WILL		MY_WANT_STATE_DO
    171 #define HIS_STATE_DO			MY_STATE_WILL
    172 #define HIS_WANT_STATE_DO		MY_WANT_STATE_WILL
    173 
    174 #define	his_state_is_do			my_state_is_will
    175 #define	his_state_is_will		my_state_is_do
    176 #define his_want_state_is_do		my_want_state_is_will
    177 #define his_want_state_is_will		my_want_state_is_do
    178 
    179 #define	his_state_is_dont		my_state_is_wont
    180 #define	his_state_is_wont		my_state_is_dont
    181 #define his_want_state_is_dont		my_want_state_is_wont
    182 #define his_want_state_is_wont		my_want_state_is_dont
    183 
    184 #define	set_his_state_do		set_my_state_will
    185 #define	set_his_state_will		set_my_state_do
    186 #define	set_his_want_state_do		set_my_want_state_will
    187 #define	set_his_want_state_will		set_my_want_state_do
    188 
    189 #define	set_his_state_dont		set_my_state_wont
    190 #define	set_his_state_wont		set_my_state_dont
    191 #define	set_his_want_state_dont		set_my_want_state_wont
    192 #define	set_his_want_state_wont		set_my_want_state_dont
    193 
    194 #define his_will_wont_is_changing	my_do_dont_is_changing
    195 #define his_do_dont_is_changing		my_will_wont_is_changing
    196 
    197 /*
    198  * Initialization buffer for tty device [16 characters long]
    199  */
    200 #define NULL16STR	"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
    201