File: telopt.c

package info (click to toggle)
modemu 0.0.1-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 200 kB
  • ctags: 266
  • sloc: ansic: 1,860; lex: 146; makefile: 72
file content (316 lines) | stat: -rw-r--r-- 7,623 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <stdio.h>	/*stderr,(fprintf)*/
#include <sys/time.h>	/*->ttybuf.h (timeval)*/
#define TELCMDS /*to use strings defined in telnet.h*/
#define TELOPTS
#include <arpa/telnet.h>/*IAC,DO,DONT,...*/

#include "defs.h"	/*->sockbuf.h (uchar,SOCKBUFR_SIZE,TTYBUFR_SIZE)*/
#include "sockbuf.h"	/*->telopt.h (putSock1)*/
#include "telopt.h"	/*TelOptReq,TelOptEnt*/
#include "ttybuf.h"	/*(putTtyN)*/
#include "atcmd.h"	/*atcmd*/
#include "verbose.h"	/*VERB_TELOPT*/

/* telnet option negotiation module */

static TelOptStates stTabMaster[] = {
    /*[opt]		[local]		[remote]*/
    { TELOPT_BINARY,	{TOR_BETTER},	{TOR_BETTER}	},/*0*/
    { TELOPT_ECHO,	{TOR_MUSTNOT},	{TOR_BETTER}	},/*1*/
    { TELOPT_SGA,	{TOR_BETTER},	{TOR_MUST}	},/*3*/
    { TELOPT_TTYPE,	{TOR_NEUTRAL},	{TOR_MUSTNOT}	},/*24*/
    { -1,		{TOR_MUSTNOT},	{TOR_MUSTNOT}	} /* default state */
};

TelOptStates *stTab[NTELOPTS]; /* telOptInit() makes it usable */

static /*const*/ TelOptStates *defaultSt; /* used when unknown options come */


/* must call before each telnet session begins */
void
telOptReset(void)
{
    TelOptStates *tosp;

    for (tosp = stTabMaster; tosp->opt >= 0; tosp++) {
	tosp->local.state = 
	tosp->remote.state = 0; /* all options are disabled initially */
	tosp->local.pending = 
	tosp->remote.pending = 0;
    }
    telOpt.binsend =
    telOpt.binrecv =
    telOpt.sgasend = 0;
    telOpt.sentReqs = 0;
}


/* must call once before using this module */
void
telOptInit(void)
{
    TelOptStates *tosp;
    int i;

    for (tosp = stTabMaster; tosp->opt >= 0; tosp++) ;
    for (i = 0; i < NTELOPTS; i++) stTab[i] = tosp; /* default entry */
    defaultSt = tosp;
    for (tosp-- ; tosp >= stTabMaster; tosp--) {
	stTab[tosp->opt] = tosp;
    }
    telOpt.stTab = stTab;
}


static const char *
telcmdStr(int cmd)
{
    static char str[16];

#ifndef TELCMD_FIRST /*is this rule correct for all telnet.h?*/
#define TELCMD_FIRST (256 - sizeof(telcmds)/sizeof(telcmds[0]))
#endif
    if (cmd >= TELCMD_FIRST) {
	return telcmds[cmd - TELCMD_FIRST];
    } else {
	sprintf(str, "?(%d)", cmd);
	return str;
    }
}


static const char *
teloptStr(int opt)
{
    static char str[16];

    if (opt < NTELOPTS) {
	return telopts[opt];
    } else {
	sprintf(str, "?(%d)", opt);
	return str;
    }
}


void
telOptPrintCmd(const char *str, int cmd)
{
    verboseOut(VERB_TELOPT, "%s IAC %s\r\n", str, telcmdStr(cmd));
}


static void
printCmdOpt(const char *str, int cmd, int opt)
{
    verboseOut(VERB_TELOPT, "%s %s %s\r\n", str, telcmdStr(cmd), teloptStr(opt));
}


static void
setReqs(void)
{
    static TelOptReq tabP[]
	= { TOR_BETTERNOT, TOR_BETTER, TOR_MUSTNOT, TOR_MUST };
    static TelOptReq tabN[]
	= { TOR_BETTER, TOR_BETTERNOT, TOR_MUST, TOR_MUSTNOT };

    /* %Bn=m (binary mode control) */
    stTab[TELOPT_BINARY]->local.req = tabP[atcmd.pb[1]];
    stTab[TELOPT_BINARY]->remote.req = tabP[atcmd.pb[0]];
    /* %Ln (linemode control) */
    stTab[TELOPT_SGA]->remote.req = tabN[atcmd.pl];
    stTab[TELOPT_ECHO]->remote.req = tabN[atcmd.pl];
    /* %Tn (terminal-type response control) */
    stTab[TELOPT_TTYPE]->local.req = atcmd.pt.wont? TOR_MUSTNOT : TOR_NEUTRAL;
}


/* tell the peer my option-state-to-be requests */
void
telOptSendReqs(void)
{
    TelOptStates *tosp;

    setReqs();

    for (tosp = stTabMaster; tosp->opt >= 0; tosp++) {
	switch (tosp->local.req) {
	case TOR_MUSTNOT:
	case TOR_BETTERNOT:
	    if (tosp->local.state == 1) {
		putOptCmd(WONT, tosp->opt);
		printCmdOpt(">", WONT, tosp->opt);
		tosp->local.pending = 1;
	    }
	    break;
	case TOR_BETTER:
	case TOR_MUST:
	    if (tosp->local.state == 0) {
		putOptCmd(WILL, tosp->opt);
		printCmdOpt(">", WILL, tosp->opt);
		tosp->local.pending = 1;
	    }
	    break;
	default:;
	}	    
	switch (tosp->remote.req) {
	case TOR_MUSTNOT:
	case TOR_BETTERNOT:
	    if (tosp->remote.state == 1) {
		putOptCmd(DONT, tosp->opt);
		printCmdOpt(">", DONT, tosp->opt);
		tosp->remote.pending = 1;
	    }
	    break;
	case TOR_BETTER:
	case TOR_MUST:
	    if (tosp->remote.state == 0) {
		putOptCmd(DO, tosp->opt);
		printCmdOpt(">", DO, tosp->opt);
		tosp->remote.pending = 1;
	    }
	    break;
	default:;
	}	    
    }
    telOpt.sentReqs = 1;
}


/* summarize option states into flags */
static void
telOptSummarize(void)
{
    telOpt.binsend = stTab[TELOPT_BINARY]->local.state;
    telOpt.binrecv = stTab[TELOPT_BINARY]->remote.state;
    telOpt.sgasend = stTab[TELOPT_SGA]->remote.state;
}


/* telnet option request/response handling */
int
telOptHandle(int cmd, int opt)
{
    TelOptState *tostp;
    TelOptStates *tosp;
    int reqState;	/* cmd's requiring state */
    int posiResCmd;	/* positive response command for cmd */
    int negaResCmd;	/* negative response command for cmd */
    TelOptReq mustNegate;	/* must negate if req is this */
    TelOptReq betterNegate;	/* better negate if req is this */
    TelOptReq betterAssert;	/* better assert if req is this */
    TelOptReq mustAssert;	/* must assert if req is this */

    printCmdOpt("<", cmd, opt);

    tosp = (opt < NTELOPTS)? stTab[opt] : defaultSt;

    switch (cmd) {
    case WILL:
	tostp = &tosp->remote;
	reqState = 1;
	mustNegate = TOR_MUSTNOT;
	betterNegate = TOR_BETTERNOT;
	betterAssert = TOR_BETTER;
	mustAssert = TOR_MUST;
	posiResCmd = DO;
	negaResCmd = DONT;
	break;
    case WONT:
	tostp = &tosp->remote;
	reqState = 0;
	mustNegate = TOR_MUST;
	betterNegate = TOR_BETTER;
	betterAssert = TOR_BETTERNOT;
	mustAssert = TOR_MUSTNOT;
	posiResCmd = DONT;
	negaResCmd = DO;
	break;
    case DO:
	tostp = &tosp->local;
	reqState = 1;
	mustNegate = TOR_MUSTNOT;
	betterNegate = TOR_BETTERNOT;
	betterAssert = TOR_BETTER;
	mustAssert = TOR_MUST;
	posiResCmd = WILL;
	negaResCmd = WONT;
	break;
    case DONT:
	tostp = &tosp->local;
	reqState = 0;
	mustNegate = TOR_MUST;
	betterNegate = TOR_BETTER;
	betterAssert = TOR_BETTERNOT;
	mustAssert = TOR_MUSTNOT;
	posiResCmd = WONT;
	negaResCmd = WILL;
	break;
    default:
	fprintf(stderr, "bug\r\n");
	exit(1);
    }

    if (tostp->req == mustNegate || tostp->req == betterNegate) {
	if (tostp->pending) {
	    tostp->pending = 0;
	    if (tostp->req == mustNegate)
		return 1; /* requirment didn't meet */
	    if (tostp->state == !reqState) { /* this may not happen */
		tostp->state = reqState;
		putOptCmd(posiResCmd, opt); /* positive response */
		printCmdOpt(">", posiResCmd, opt);
	    }
	} else {
	    putOptCmd(negaResCmd, opt); /* negative response */
	    printCmdOpt(">", negaResCmd, opt);
	}
    } else /*if (tostp->req == betterAssert or mustAssert or TOR_NEUTRAL)*/ {
	if (tostp->pending) {
	    tostp->pending = 0;
	    /* don't response because cmd is the response of my request */
	} else {
          if (tostp->state == !reqState) { /* this may not happen */
                putOptCmd(posiResCmd, opt); /* positive response */
                printCmdOpt(">", posiResCmd, opt);
            }
	}
	tostp->state = reqState; /* {en,dis}able option as requested */
    }

    telOptSummarize();
    return 0;
}

/* send term-type subnego param */
static void
ttypeSBHandle(void)
{
    putSock1(IAC);
    putSock1(SB);
    putSock1(TELOPT_TTYPE);
    putSock1(TELQUAL_IS);
    putSockN(atcmd.pt.str, atcmd.pt.len);
    putSock1(IAC);
    putSock1(SE);
    verboseOut(VERB_TELOPT, "> SB %s IS %s SE\r\n",
	     telopts[TELOPT_TTYPE], atcmd.pt.str);
}

/* telnet option subnegotiation request handling */
int
telOptSBHandle(int opt)
{
    verboseOut(VERB_TELOPT, "< SB %s SEND SE.\r\n", telopts[opt]);

    switch (opt) {
    case TELOPT_TTYPE:
	ttypeSBHandle();
	break;
    default:
	return 1;
    }
    return 0;
}