root/src/noit_console_telnet.h

Revision 30700011fc4916bef71870a2bae7739745a7a2bc, 10.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

integrate a full-on telnet server... hey, why not?

  • 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 #ifdef HAVE_TERM_H
47 #include <term.h>
48 #define USE_TERMIO
49 #endif
50
51 #define LINEMODE 1
52
53 #ifndef _POSIX_VDISABLE
54 # ifdef VDISABLE
55 #  define _POSIX_VDISABLE VDISABLE
56 # else
57 #  define _POSIX_VDISABLE ((unsigned char)'\377')
58 # endif
59 #endif
60
61 /*
62  * Structures of information for each special character function.
63  */
64 typedef unsigned char cc_t;
65
66 typedef struct {
67         unsigned char   flag;           /* the flags for this function */
68         cc_t            val;            /* the value of the special character */
69 } slcent, *Slcent;
70
71 typedef struct {
72         slcent          defset;         /* the default settings */
73         slcent          current;        /* the current settings */
74         cc_t            *sptr;          /* a pointer to the char in */
75                                         /* system data structures */
76 } slcfun, *Slcfun;
77
78 #ifndef USE_TERMIO
79 struct termbuf {
80         struct sgttyb sg;
81         struct tchars tc;
82         struct ltchars ltc;
83         int state;
84         int lflags;
85 };
86 # define        cfsetospeed(tp, val)    (tp)->sg.sg_ospeed = (val)
87 # define        cfsetispeed(tp, val)    (tp)->sg.sg_ispeed = (val)
88 # define        cfgetospeed(tp)         (tp)->sg.sg_ospeed
89 # define        cfgetispeed(tp)         (tp)->sg.sg_ispeed
90 #else   /* USE_TERMIO */
91 # ifndef        TCSANOW
92 #  ifdef TCSETS
93 #   define      TCSANOW         TCSETS
94 #   define      TCSADRAIN       TCSETSW
95 #   define      tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
96 #  else
97 #   ifdef TCSETA
98 #    define     TCSANOW         TCSETA
99 #    define     TCSADRAIN       TCSETAW
100 #    define     tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
101 #   else
102 #    define     TCSANOW         TIOCSETA
103 #    define     TCSADRAIN       TIOCSETAW
104 #    define     tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
105 #   endif
106 #  endif
107 #  define       tcsetattr(f, a, t)      ioctl(f, a, t)
108 #  define       cfsetospeed(tp, val)    (tp)->c_cflag &= ~CBAUD; \
109                                         (tp)->c_cflag |= (val)
110 #  define       cfgetospeed(tp)         ((tp)->c_cflag & CBAUD)
111 #  ifdef CIBAUD
112 #   define      cfsetispeed(tp, val)    (tp)->c_cflag &= ~CIBAUD; \
113                                         (tp)->c_cflag |= ((val)<<IBSHIFT)
114 #   define      cfgetispeed(tp)         (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
115 #  else
116 #   define      cfsetispeed(tp, val)    (tp)->c_cflag &= ~CBAUD; \
117                                         (tp)->c_cflag |= (val)
118 #   define      cfgetispeed(tp)         ((tp)->c_cflag & CBAUD)
119 #  endif
120 # endif /* TCSANOW */
121 #endif  /* USE_TERMIO */
122
123 /*
124  * The following are some clocks used to decide how to interpret
125  * the relationship between various variables.
126  */
127
128 struct clocks_t {
129     int
130         system,                 /* what the current time is */
131         echotoggle,             /* last time user entered echo character */
132         modenegotiated,         /* last time operating mode negotiated */
133         didnetreceive,          /* last time we read data from network */
134         ttypesubopt,            /* ttype subopt is received */
135         tspeedsubopt,           /* tspeed subopt is received */
136         environsubopt,          /* environ subopt is received */
137         oenvironsubopt,         /* old environ subopt is received */
138         xdisplocsubopt,         /* xdisploc subopt is received */
139         baseline,               /* time started to do timed action */
140         gotDM;                  /* when did we last see a data mark */
141 };
142
143 typedef struct {
144   noit_hash_table _env;
145   unsigned char *_subbuffer;
146   unsigned char *_subpointer;
147   unsigned char *_subend;
148   unsigned char *_subsave;
149   unsigned char  _pty_buf[1024];
150   int            _pty_fill;
151   slcfun         _slctab[NSLC + 1];
152   char           _options[256];
153   char           _do_dont_resp[256];
154   char           _will_wont_resp[256];
155   int            _SYNCHing;
156   int            _flowmode;
157   int            _restartany;
158   int            _def_tspeed; /* This one */
159   int            _def_rspeed; /* and this one, need init to -1 */
160 #ifdef TIOCSWINSZ
161   int            _def_row;
162   int            _def_col;
163 #endif
164   char           _terminalname[41];
165   struct clocks_t _clocks;
166 #ifdef USE_TERMIO
167   struct termios _termbuf, _termbuf2;   /* pty control structure */
168 #else
169   struct termbuf _termbuf, _termbuf2;   /* pty control structure */
170 #endif
171 } *noit_console_telnet_closure_t;
172
173
174 /*
175  * We keep track of each side of the option negotiation.
176  */
177
178 #define MY_STATE_WILL           0x01
179 #define MY_WANT_STATE_WILL      0x02
180 #define MY_STATE_DO             0x04
181 #define MY_WANT_STATE_DO        0x08
182
183 /*
184  * Macros to check the current state of things
185  */
186
187 #define my_state_is_do(opt)             (ncct->telnet->_options[opt]&MY_STATE_DO)
188 #define my_state_is_will(opt)           (ncct->telnet->_options[opt]&MY_STATE_WILL)
189 #define my_want_state_is_do(opt)        (ncct->telnet->_options[opt]&MY_WANT_STATE_DO)
190 #define my_want_state_is_will(opt)      (ncct->telnet->_options[opt]&MY_WANT_STATE_WILL)
191
192 #define my_state_is_dont(opt)           (!my_state_is_do(opt))
193 #define my_state_is_wont(opt)           (!my_state_is_will(opt))
194 #define my_want_state_is_dont(opt)      (!my_want_state_is_do(opt))
195 #define my_want_state_is_wont(opt)      (!my_want_state_is_will(opt))
196
197 #define set_my_state_do(opt)            (ncct->telnet->_options[opt] |= MY_STATE_DO)
198 #define set_my_state_will(opt)          (ncct->telnet->_options[opt] |= MY_STATE_WILL)
199 #define set_my_want_state_do(opt)       (ncct->telnet->_options[opt] |= MY_WANT_STATE_DO)
200 #define set_my_want_state_will(opt)     (ncct->telnet->_options[opt] |= MY_WANT_STATE_WILL)
201
202 #define set_my_state_dont(opt)          (ncct->telnet->_options[opt] &= ~MY_STATE_DO)
203 #define set_my_state_wont(opt)          (ncct->telnet->_options[opt] &= ~MY_STATE_WILL)
204 #define set_my_want_state_dont(opt)     (ncct->telnet->_options[opt] &= ~MY_WANT_STATE_DO)
205 #define set_my_want_state_wont(opt)     (ncct->telnet->_options[opt] &= ~MY_WANT_STATE_WILL)
206
207 /*
208  * Tricky code here.  What we want to know is if the MY_STATE_WILL
209  * and MY_WANT_STATE_WILL bits have the same value.  Since the two
210  * bits are adjacent, a little arithmatic will show that by adding
211  * in the lower bit, the upper bit will be set if the two bits were
212  * different, and clear if they were the same.
213  */
214 #define my_will_wont_is_changing(opt) \
215                         ((ncct->telnet->_options[opt]+MY_STATE_WILL) & MY_WANT_STATE_WILL)
216
217 #define my_do_dont_is_changing(opt) \
218                         ((ncct->telnet->_options[opt]+MY_STATE_DO) & MY_WANT_STATE_DO)
219
220 /*
221  * Make everything symmetrical
222  */
223
224 #define HIS_STATE_WILL                  MY_STATE_DO
225 #define HIS_WANT_STATE_WILL             MY_WANT_STATE_DO
226 #define HIS_STATE_DO                    MY_STATE_WILL
227 #define HIS_WANT_STATE_DO               MY_WANT_STATE_WILL
228
229 #define his_state_is_do                 my_state_is_will
230 #define his_state_is_will               my_state_is_do
231 #define his_want_state_is_do            my_want_state_is_will
232 #define his_want_state_is_will          my_want_state_is_do
233
234 #define his_state_is_dont               my_state_is_wont
235 #define his_state_is_wont               my_state_is_dont
236 #define his_want_state_is_dont          my_want_state_is_wont
237 #define his_want_state_is_wont          my_want_state_is_dont
238
239 #define set_his_state_do                set_my_state_will
240 #define set_his_state_will              set_my_state_do
241 #define set_his_want_state_do           set_my_want_state_will
242 #define set_his_want_state_will         set_my_want_state_do
243
244 #define set_his_state_dont              set_my_state_wont
245 #define set_his_state_wont              set_my_state_dont
246 #define set_his_want_state_dont         set_my_want_state_wont
247 #define set_his_want_state_wont         set_my_want_state_dont
248
249 #define his_will_wont_is_changing       my_do_dont_is_changing
250 #define his_do_dont_is_changing         my_will_wont_is_changing
251
252 struct __noit_console_closure;
253 void
254   noit_console_telnet_willoption(struct __noit_console_closure *ncct,
255                                  int option);
256 void
257   noit_console_telnet_wontoption(struct __noit_console_closure *ncct,
258                                  int option);
259 void
260   noit_console_telnet_dooption(struct __noit_console_closure *ncct,
261                                int option);
262 void
263   noit_console_telnet_dontoption(struct __noit_console_closure *ncct,
264                                  int option);
265 void
266   noit_console_telnet_send_do(struct __noit_console_closure *ncct,
267                               int option, int init);
268 void
269   noit_console_telnet_send_dont(struct __noit_console_closure *ncct,
270                                 int option, int init);
271 void
272   noit_console_telnet_send_will(struct __noit_console_closure *ncct,
273                                 int option, int init);
274 void
275   noit_console_telnet_send_wont(struct __noit_console_closure *ncct,
276                                 int option, int init);
277 void
278   noit_console_telnet_doclientstat(struct __noit_console_closure *ncct);
279 void
280   noit_console_telnet_send_status(struct __noit_console_closure *ncct);
281
282 void noit_console_telnet_free(noit_console_telnet_closure_t);
283 noit_console_telnet_closure_t
284   noit_console_telnet_alloc(struct __noit_console_closure *ncct);
285 int
286   noit_console_telnet_telrcv(struct __noit_console_closure *ncct,
287                              const void *buf, int buflen);
288 void ptyflush(struct __noit_console_closure *ncct);
289
290 #endif
Note: See TracBrowser for help on using the browser.