summaryrefslogtreecommitdiffabout
path: root/options.cpp
blob: 171ae4a253a686204d979052445917b029241228 (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

enum	_TOption {
	toBinaryTransmission = 0,
	toEcho = 1,						// Done.
	toReconnection = 2,
	toSuppressGA = 3,				// Eaten
	toApproxMsgSizeNegotiation = 4,
	toStatus = 5,					// Half-implemented
	toTimingMark = 6,
	toRemoteControlledTransAndEcho = 7,
	toOutputLineWidth = 8,
	toOutputPageSize = 9,
	toOutputCRDisposition = 10,
	toOutputHTabstops = 11,
	toOutputHTabDisposition = 12,
	toOutputFFDisposition = 13,
	toOutputVTabstops = 14,
	toOutputVTabDisposition = 15,
	toOutputLFDisposition = 16,
	toExtendedASCII = 17,
	toLogout = 18,
	toByteMacro = 19,
	toDET = 20,
	toSUPDUP = 22,	// ???
	toSUPDUPOutput = 22,
	toSendLocation = 23,
	toTerminalType =24,				// Done.
	toEndOfRecord = 25,
	toTACACSUserId = 26,
	toOutputMarking = 27,
	toTerminalLocationNumber = 28,
	toTelnet3270Regime = 29,
	toX3PAD = 30,
	toNAWS = 31,					// Done.
	toTerminalSpeed = 32,			// No need to implement, really.
	toRemoteFlowControl = 33,		// !!!
	toLineMode = 34,
	toXDisplayLocation = 35,		// No need to implement until we
									// do our own X-Server.
	toEnvironmentOption = 36,
	toAuthenticationOption = 37,
	toEncryptionOption = 38,
	toNewEnviron = 39,				// Need to implement
	toTN3270E = 40,
	toExtendedOptionsList = 255
};

struct	TOption	{
	enum _state {
		stateNone=0, stateNo, stateYes, stateWantNo, stateWantYes
	};
	UINT m_StateU:3;	// State - US
	UINT m_StateH:3;	// State - HIM
	int m_Q:1;
	int m_Supported:1;
	UINT m_SBCluster;
	UINT m_AllocatedSB;
	UINT m_StoredSB;
	LPBYTE m_SB;

	BOOL (*m_OnDo)();
	BOOL (*m_OnDont)();
	BOOL (*m_OnWill)();
	BOOL (*m_OnWont)();
	BOOL (*m_OnSB)(LPBYTE data,UINT size);
	BOOL (*m_OnInit)();

	BOOL OnDo() { return m_OnDo?(*m_OnDo)():FALSE; }
	BOOL OnDont() { return m_OnDont?(*m_OnDont)():FALSE; }
	BOOL OnWill() { return m_OnWill?(*m_OnWill)():FALSE; }
	BOOL OnWont() { return m_OnWont?(*m_OnWont)():FALSE; }
	BOOL OnSB(LPBYTE data,UINT size) { return m_OnSB?(*m_OnSB)(data,size):FALSE; }
	BOOL OnInit() { return m_OnInit?(*m_OnInit)():FALSE; }

	BOOL StoreSByte(BYTE c) {
		if(m_AllocatedSB>=m_StoredSB){
			ASSERT(m_SBCluster);
		LPBYTE nsb = new BYTE[m_AllocatedSB+m_SBCluster];
			ASSERT(nsb);
			if(m_StoredSB){
				ASSERT(m_SB);
				memmove(nsb,m_SB,m_StoredSB);
			}
			if(m_SB)
				delete m_SB;
			m_SB = nsb;
		}
		ASSERT(m_SB);
		m_SB[m_StoredSB++]=c;
		return TRUE;
	}
	BOOL CleanSB() {
		if(m_SB)
			delete m_SB;
		m_SB = NULL;
		m_AllocatedSB=m_StoredSB=0;
		return TRUE;
	}

}	Options[256];


BOOL AskWill(BYTE o);
BOOL AskWont(BYTE o);
BOOL AskDo(BYTE o);
BOOL AskDont(BYTE o);

#include "NAWS.cpp"
#include "terminal.cpp"
#include "status.cpp"
#include "NEW-ENVIRON.cpp"
#include "TIMING-MARK.cpp"

BOOL On_ACK()	{ return TRUE; }


void InitOptionsTable()
{
	memset(Options,0,sizeof(Options));
	// Echo
	Options[toEcho].m_OnDo = On_ACK;
	Options[toEcho].m_OnDont = On_ACK;
	Options[toEcho].m_OnWill = On_ACK;
	Options[toEcho].m_OnWont = On_ACK;
	// Suppress Go-Ahead
	Options[toSuppressGA].m_OnWill = On_ACK;
	Options[toSuppressGA].m_OnDo = On_ACK;
	// Status
	Options[toStatus].m_OnWill = statusOnWill;
	Options[toStatus].m_OnDo = statusOnDo;
	Options[toStatus].m_OnSB = statusOnSB;
	// Terminal Type;
	Options[toTerminalType].m_OnDo = terminaltypeOnDo;
	Options[toTerminalType].m_OnSB = terminaltypeOnSB;
	Options[toTerminalType].m_OnInit = terminaltypeOnInit;
	Options[toTerminalType].m_SBCluster = 32;
	// NAWS
	Options[toNAWS].m_OnDo = nawsOnDo;
	Options[toNAWS].m_SBCluster = 16;
	// New Environment
	Options[toNewEnviron].m_OnDo = newenvironOnDo;
	Options[toNewEnviron].m_OnDont = newenvironOnDont;
	Options[toNewEnviron].m_OnSB = newenvironOnSB;
	Options[toNewEnviron].m_SBCluster = 32;
	// Timing Mark
	Options[toTimingMark].m_OnDo = tmOnDo;
	Options[toTimingMark].m_OnWill = tmOnWill;

	for(int tmp=0;tmp<(sizeof(Options)/sizeof(*Options));tmp++){
	TOption& to = Options[tmp];
		to.OnInit();
		if(to.m_OnDo || to.m_OnDont || to.m_OnWill || to.m_OnWont
		   || to.m_OnSB || to.m_OnInit)
			to.m_Supported=TRUE;
		if(!to.m_SBCluster)
			to.m_SBCluster=128;
	}
}

void ProcessDO(BYTE c)
{
TOption& to = Options[c];
	// Ignore this junk if we're already in desired mode of operation
	if(to.m_StateU==TOption::stateYes){
		connState = cstateNone;
		return;
	}
	TRACE1("He want us to DO %d\n",(WORD)c);
	if(!to.m_Supported){
		TRACE1("Unsupported option = %d\n",(WORD)c);
		// Option is not suported - send WONT and switch to stateNo unless
		// we've already denied this option.
		if(to.m_StateU == TOption::stateNone){
			VERIFY(ShowUnwill(c));
			to.m_StateU = TOption::stateNo;
		}
	}else{
		// Okay, if we do -  we do and tell him so, unless we asked for it.
		// Otherwise - Tell him we're not about to.
		if(to.OnDo()){
			if(to.m_StateU!=TOption::stateWantYes)
				VERIFY(ShowWill(c));
			to.m_StateU = TOption::stateYes;
		}else{
			if(to.m_StateU!=TOption::stateWantNo)
				VERIFY(ShowUnwill(c));
			to.m_StateU = TOption::stateNo;
		}
	}
	connState = cstateNone;
}

void ProcessWILL(BYTE c)
{
TOption& to = Options[c];
	// Ignore this junk if we consider him already in this mode
	if(to.m_StateH==TOption::stateYes){
		connState = cstateNone;
		return;
	}
	TRACE1("He WILL %d\n",(WORD)c);
	if(!to.m_Supported){
		TRACE1("Unsupported option = %d\n",(WORD)c);
		// Since we don't expect remote end to use this option - beg him
		// not to go for it unless we think we already did.
		if(to.m_StateH == TOption::stateNone){
			VERIFY(BegDont(c));
			to.m_StateH = TOption::stateNo;
		}
	}else{
		if(to.OnWill()){
			// We've accepted this - tell him to go ahead if we thought he's
			// in opposite state and adjust our vision/
			if(to.m_StateH!=TOption::stateWantYes)
				VERIFY(BegDo(c));
			to.m_StateH = TOption::stateYes;
		}else{
			if(to.m_StateH!=TOption::stateWantNo)
				VERIFY(BegDont(c));
			to.m_StateH = TOption::stateNo;
		}
	}
	connState = cstateNone;
}

void ProcessWONT(BYTE c)
{
TOption& to = Options[c];
	// Ignore this junk if we consider him already in this mode
	if(to.m_StateH==TOption::stateNo){
		connState = cstateNone;
		return;
	}
	TRACE1("He WONT %d\n",(WORD)c);
	if(!to.m_Supported){
		TRACE1("Unsupported option = %d\n",(WORD)c);
		// Since we don't expect remote end to use this option - beg him
		// not to go for it unless we think we already did.
		if(to.m_StateH == TOption::stateNone){
			VERIFY(BegDont(c));
			to.m_StateH = TOption::stateNo;
		}
	}else{
		if(to.OnWont()){
			// We've accepted this - tell him to go ahead if we thought he's
			// in opposite state and adjust our vision/
			if(to.m_StateH!=TOption::stateWantNo)
				VERIFY(BegDont(c));
			to.m_StateH = TOption::stateNo;
		}else{
			if(to.m_StateH!=TOption::stateWantYes)
				VERIFY(BegDo(c));
			to.m_StateH = TOption::stateYes;
		}
	}
	connState = cstateNone;
}

void ProcessDONT(BYTE c)
{
TOption& to = Options[c];
	// Ignore this junk if we consider him already in this mode
	if(to.m_StateU==TOption::stateNo){
		connState = cstateNone;
		return;
	}
	TRACE1("He want us to DONT %d\n",(WORD)c);
	if(!to.m_Supported){
		TRACE1("DONT for unsupported option = %d\n",(WORD)c);
		// Since we don't expect remote end to use this option - beg him
		// not to go for it unless we think we already did.
		if(to.m_StateU == TOption::stateNone){
			VERIFY(ShowUnwill(c));
			to.m_StateU = TOption::stateNo;
		}
	}else{
		if(to.OnDont()){
			if(to.m_StateU!=TOption::stateWantNo)
				VERIFY(ShowUnwill(c));
			to.m_StateU = TOption::stateNo;
		}else{
			if(to.m_StateU!=TOption::stateWantYes)
				VERIFY(ShowWill(c));
			to.m_StateU = TOption::stateYes;
		}
	}
	connState = cstateNone;
}

void ProcessSBData(BYTE c)
{
	switch(connState){
	case cstateSBData:
		if(c==tnIAC){
			connState=cstateSBDataIAC;
			return;
		}
		break;
	case cstateSBDataIAC:
		if(c==tnSE){
			// Just let option handler process the data
		TOption& to = Options[negOption];
			to.OnSB(to.m_SB,to.m_StoredSB);
			to.CleanSB();
			connState = cstateNone;
			return;
		}
		break;
	default:
		ASSERT(FALSE);
		break;
	}
TOption& to = Options[negOption];
	to.StoreSByte(c);
}

BOOL AskWill(BYTE o)
{
TOption& to = Options[o];
	if(to.m_StateU==TOption::stateYes || to.m_StateU==TOption::stateWantYes)
		return TRUE;
#ifdef _DEBUG
	if(to.m_StateU==TOption::stateWantNo)
		TRACE1("NEED TO QUEUE %d\n",(WORD)o);
#endif
	VERIFY(ShowWill(o));
	to.m_StateU=TOption::stateWantYes;
	return TRUE;
}

BOOL AskUnwill(BYTE o)
{
TOption& to = Options[o];
	if(to.m_StateU==TOption::stateNo || to.m_StateU==TOption::stateWantNo)
		return TRUE;
#ifdef _DEBUG
	if(to.m_StateU==TOption::stateWantNo)
		TRACE1("NEED TO QUEUE %d\n",(WORD)o);
#endif
	VERIFY(ShowUnwill(o));
	to.m_StateU=TOption::stateWantNo;
	return TRUE;
}

BOOL AskDo(BYTE o)
{
TOption& to = Options[o];
	if(to.m_StateH==TOption::stateYes || to.m_StateH==TOption::stateWantYes)
		return TRUE;
#ifdef _DEBUG
	if(to.m_StateH==TOption::stateWantNo)
		TRACE1("NEED TO QUEUE %d\n",(WORD)o);
#endif
	VERIFY(BegDo(o));
	to.m_StateH=TOption::stateWantYes;
	return TRUE;
}

BOOL AskDont(BYTE o)
{
TOption& to = Options[o];
	if(to.m_StateH==TOption::stateNo || to.m_StateH==TOption::stateWantNo)
		return TRUE;
#ifdef _DEBUG
	if(to.m_StateH==TOption::stateWantYes)
		TRACE1("NEED TO QUEUE %d\n",(WORD)o);
#endif
	VERIFY(BegDont(o));
	to.m_StateH=TOption::stateWantNo;
	return TRUE;
}