root/src/noit_console_telnet.h

Revision 065b0349f122c8e5f44538bed5636ea8b49362b6, 10.7 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

compile clean on OpenBSD

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2007
5  *      OmnitTI Computer Consulting, Inc.  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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      @(#)defs.h      8.1 (Berkeley) 6/4/93
36  */
37
38 #ifndef _NOIT_CONSOLE_TELNET_H
39 #define _NOIT_CONSOLE_TELNET_H
40
41 #include "noit_defines.h"
42 #include "utils/noit_hash.h"
43
44 #include <arpa/telnet.h>
45 #include <sys/ioctl.h>
46 #include <sys/ioctl_compat.h>
47 #ifdef HAVE_TERMIOS_H
48 #include <termios.h>
49 #endif
50 #ifdef HAVE_TERM_H
51 #include <term.h>
52 #endif
53
54 #define LINEMODE 1
55
56 #ifndef _POSIX_VDISABLE
57 # ifdef VDISABLE
58 #  define _POSIX_VDISABLE VDISABLE
59 # else
60 #  define _POSIX_VDISABLE ((unsigned char)'\377')
61 # endif
62 #endif
63
64 /*
65  * Structures of information for each special character function.
66  */
67
68 typedef struct {
69         unsigned char   flag;           /* the flags for this function */
70         unsigned char   val;            /* the value of the special character */
71 } slcent, *Slcent;
72
73 typedef struct {
74         slcent          defset;         /* the default settings */
75         slcent          current;        /* the current settings */
76         unsigned char   *sptr;          /* a pointer to the char in */
77                                         /* system data structures */
78 } slcfun, *Slcfun;
79
80 #ifndef USE_TERMIO
81 struct termbuf {
82         struct sgttyb sg;
83         struct tchars tc;
84         struct ltchars ltc;
85         int state;
86         int lflags;
87 };
88 # define        cfsetospeed(tp, val)    (tp)->sg.sg_ospeed = (val)
89 # define        cfsetispeed(tp, val)    (tp)->sg.sg_ispeed = (val)
90 # define        cfgetospeed(tp)         (tp)->sg.sg_ospeed
91 # define        cfgetispeed(tp)         (tp)->sg.sg_ispeed
92 #else   /* USE_TERMIO */
93 # ifndef        TCSANOW
94 #  ifdef TCSETS
95 #   define      TCSANOW         TCSETS
96 #   define      TCSADRAIN       TCSETSW
97 #   define      tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
98 #  else
99 #   ifdef TCSETA
100 #    define     TCSANOW         TCSETA
101 #    define     TCSADRAIN       TCSETAW
102 #    define     tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
103 #   else
104 #    define     TCSANOW         TIOCSETA
105 #    define     TCSADRAIN       TIOCSETAW
106 #    define     tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
107 #   endif
108 #  endif
109 #  define       tcsetattr(f, a, t)      ioctl(f, a, t)
110 #  define       cfsetospeed(tp, val)    (tp)->c_cflag &= ~CBAUD; \
111                                         (tp)->c_cflag |= (val)
112 #  define       cfgetospeed(tp)         ((tp)->c_cflag & CBAUD)
113 #  ifdef CIBAUD
114 #   define      cfsetispeed(tp, val)    (tp)->c_cflag &= ~CIBAUD; \
115                                         (tp)->c_cflag |= ((val)<<IBSHIFT)
116 #   define      cfgetispeed(tp)         (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
117 #  else
118 #   define      cfsetispeed(tp, val)    (tp)->c_cflag &= ~CBAUD; \
119                                         (tp)->c_cflag |= (val)
120 #   define      cfgetispeed(tp)         ((tp)->c_cflag & CBAUD)
121 #  endif
122 # endif /* TCSANOW */
123 #endif  /* USE_TERMIO */
124
125 /*
126  * The following are some clocks used to decide how to interpret
127  * the relationship between various variables.
128  */
129
130 struct clocks_t {
131     int
132         system,                 /* what the current time is */
133         echotoggle,             /* last time user entered echo character */
134         modenegotiated,         /* last time operating mode negotiated */
135         didnetreceive,          /* last time we read data from network */
136         ttypesubopt,            /* ttype subopt is received */
137         tspeedsubopt,           /* tspeed subopt is received */
138         environsubopt,          /* environ subopt is received */
139         oenvironsubopt,         /* old environ subopt is received */
140         xdisplocsubopt,         /* xdisploc subopt is received */
141         baseline,               /* time started to do timed action */
142         gotDM;                  /* when did we last see a data mark */
143 };
144
145 typedef struct {
146   noit_hash_table _env;
147   unsigned char *_subbuffer;
148   unsigned char *_subpointer;
149   unsigned char *_subend;
150   unsigned char *_subsave;
151   unsigned char  _pty_buf[1024];
152   int            _pty_fill;
153   slcfun         _slctab[NSLC + 1];
154   char           _options[256];
155   char           _do_dont_resp[256];
156   char           _will_wont_resp[256];
157   int            _SYNCHing;
158   int            _flowmode;
159   int            _restartany;
160   int            __terminit;
161   int            _linemode;
162   int            _uselinemode;
163   int            _alwayslinemode;
164   int            _editmode;
165   int            _useeditmode;
166   unsigned char *_def_slcbuf;
167   int            _def_slclen;
168   int            _slcchange;    /* change to slc is requested */
169   unsigned char *_slcptr;       /* pointer into slc buffer */
170   unsigned char  _slcbuf[NSLC*6];       /* buffer for slc negotiation */
171
172   int            _def_tspeed; /* This one */
173   int            _def_rspeed; /* and this one, need init to -1 */
174 #if     !defined(LINEMODE) || !defined(KLUDGELINEMODE)
175   int            _turn_on_sga;
176 #endif
177 #ifdef TIOCSWINSZ
178   int            _def_row;
179   int            _def_col;
180 #endif
181   char           _terminalname[41];
182   struct clocks_t _clocks;
183 #ifdef USE_TERMIO
184   struct termios _termbuf, _termbuf2;   /* pty control structure */
185 #else
186   struct termbuf _termbuf, _termbuf2;   /* pty control structure */
187 #endif
188 } *noit_console_telnet_closure_t;
189
190
191 /*
192  * We keep track of each side of the option negotiation.
193  */
194
195 #define MY_STATE_WILL           0x01
196 #define MY_WANT_STATE_WILL      0x02
197 #define MY_STATE_DO             0x04
198 #define MY_WANT_STATE_DO        0x08
199
200 /*
201  * Macros to check the current state of things
202  */
203
204 #define my_state_is_do(opt)             (ncct->telnet->_options[opt]&MY_STATE_DO)
205 #define my_state_is_will(opt)           (ncct->telnet->_options[opt]&MY_STATE_WILL)
206 #define my_want_state_is_do(opt)        (ncct->telnet->_options[opt]&MY_WANT_STATE_DO)
207 #define my_want_state_is_will(opt)      (ncct->telnet->_options[opt]&MY_WANT_STATE_WILL)
208
209 #define my_state_is_dont(opt)           (!my_state_is_do(opt))
210 #define my_state_is_wont(opt)           (!my_state_is_will(opt))
211 #define my_want_state_is_dont(opt)      (!my_want_state_is_do(opt))
212 #define my_want_state_is_wont(opt)      (!my_want_state_is_will(opt))
213
214 #define set_my_state_do(opt)            (ncct->telnet->_options[opt] |= MY_STATE_DO)
215 #define set_my_state_will(opt)          (ncct->telnet->_options[opt] |= MY_STATE_WILL)
216 #define set_my_want_state_do(opt)       (ncct->telnet->_options[opt] |= MY_WANT_STATE_DO)
217 #define set_my_want_state_will(opt)     (ncct->telnet->_options[opt] |= MY_WANT_STATE_WILL)
218
219 #define set_my_state_dont(opt)          (ncct->telnet->_options[opt] &= ~MY_STATE_DO)
220 #define set_my_state_wont(opt)          (ncct->telnet->_options[opt] &= ~MY_STATE_WILL)
221 #define set_my_want_state_dont(opt)     (ncct->telnet->_options[opt] &= ~MY_WANT_STATE_DO)
222 #define set_my_want_state_wont(opt)     (ncct->telnet->_options[opt] &= ~MY_WANT_STATE_WILL)
223
224 /*
225  * Tricky code here.  What we want to know is if the MY_STATE_WILL
226  * and MY_WANT_STATE_WILL bits have the same value.  Since the two
227  * bits are adjacent, a little arithmatic will show that by adding
228  * in the lower bit, the upper bit will be set if the two bits were
229  * different, and clear if they were the same.
230  */
231 #define my_will_wont_is_changing(opt) \
232                         ((ncct->telnet->_options[opt]+MY_STATE_WILL) & MY_WANT_STATE_WILL)
233
234 #define my_do_dont_is_changing(opt) \
235                         ((ncct->telnet->_options[opt]+MY_STATE_DO) & MY_WANT_STATE_DO)
236
237 /*
238  * Make everything symmetrical
239  */
240
241 #define HIS_STATE_WILL                  MY_STATE_DO
242 #define HIS_WANT_STATE_WILL             MY_WANT_STATE_DO
243 #define HIS_STATE_DO                    MY_STATE_WILL
244 #define HIS_WANT_STATE_DO               MY_WANT_STATE_WILL
245
246 #define his_state_is_do                 my_state_is_will
247 #define his_state_is_will               my_state_is_do
248 #define his_want_state_is_do            my_want_state_is_will
249 #define his_want_state_is_will          my_want_state_is_do
250
251 #define his_state_is_dont               my_state_is_wont
252 #define his_state_is_wont               my_state_is_dont
253 #define his_want_state_is_dont          my_want_state_is_wont
254 #define his_want_state_is_wont          my_want_state_is_dont
255
256 #define set_his_state_do                set_my_state_will
257 #define set_his_state_will              set_my_state_do
258 #define set_his_want_state_do           set_my_want_state_will
259 #define set_his_want_state_will         set_my_want_state_do
260
261 #define set_his_state_dont              set_my_state_wont
262 #define set_his_state_wont              set_my_state_dont
263 #define set_his_want_state_dont         set_my_want_state_wont
264 #define set_his_want_state_wont         set_my_want_state_dont
265
266 #define his_will_wont_is_changing       my_do_dont_is_changing
267 #define his_do_dont_is_changing         my_will_wont_is_changing
268
269 struct __noit_console_closure;
270 void
271   noit_console_telnet_willoption(struct __noit_console_closure *ncct,
272                                  int option);
273 void
274   noit_console_telnet_wontoption(struct __noit_console_closure *ncct,
275                                  int option);
276 void
277   noit_console_telnet_dooption(struct __noit_console_closure *ncct,
278                                int option);
279 void
280   noit_console_telnet_dontoption(struct __noit_console_closure *ncct,
281                                  int option);
282 void
283   noit_console_telnet_send_do(struct __noit_console_closure *ncct,
284                               int option, int init);
285 void
286   noit_console_telnet_send_dont(struct __noit_console_closure *ncct,
287                                 int option, int init);
288 void
289   noit_console_telnet_send_will(struct __noit_console_closure *ncct,
290                                 int option, int init);
291 void
292   noit_console_telnet_send_wont(struct __noit_console_closure *ncct,
293                                 int option, int init);
294 void
295   noit_console_telnet_doclientstat(struct __noit_console_closure *ncct);
296 void
297   noit_console_telnet_send_status(struct __noit_console_closure *ncct);
298
299 void noit_console_telnet_free(noit_console_telnet_closure_t);
300 noit_console_telnet_closure_t
301   noit_console_telnet_alloc(struct __noit_console_closure *ncct);
302 int
303   noit_console_telnet_telrcv(struct __noit_console_closure *ncct,
304                              const void *buf, int buflen);
305 void ptyflush(struct __noit_console_closure *ncct);
306
307 #endif
Note: See TracBrowser for help on using the browser.