-
Notifications
You must be signed in to change notification settings - Fork 0
/
telnet.lisp
314 lines (239 loc) · 9.63 KB
/
telnet.lisp
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
;;;; -*- Mode: Lisp -*-
;;;; telnet.lisp
;;;;
;;;; Version with LW networking.
(in-package "CL3270")
;;; Constants.
;;; Should use my own DEFENUM.
(eval-when (:load-toplevel :compile-toplevel :execute)
(progn
(deftype telnet-code ()
'(mod 256))
(defvar *telnet-code-names* ; Maybe make it into a... A-List!!!
(make-array 256
:element-type 'symbol
:initial-element nil))
(defmacro def-telnet-code (name code &optional (doc "Not documented."))
`(progn
(declaim (type telnet-code ,name))
(setf (aref *telnet-code-names* ,code) ',name)
(defconstant ,name ,code ,doc))
))
)
(defun telnet-code-name (code)
(or (aref *telnet-code-names* code)
code))
;;; Start simple with Matthew R. Wilson's setup.
(def-telnet-code +binary+ 0 "Binary.")
(def-telnet-code +send+ 1 "Send.")
(def-telnet-code +terminal-type+ 24 "Terminal type.")
(def-telnet-code +eor-option+ 25 "EOR option.")
(def-telnet-code +eor+ 239 "EOR.")
;;; Telnet "commands", always "prefixed" by the "Interpret As Command"
;;; (IAC) sequence.
;;; Cfr., RFC 854
(def-telnet-code +se+ 240
"End of subnegotiation parameters.")
(def-telnet-code +nop+ 241
"No operation.")
(def-telnet-code +dm+ 242
"Data Mark.
The data stream portion of a Synch.
This should always be accompanied
by a TCP Urgent notification.")
(def-telnet-code +break+ 243
"Break.
NVT character BRK.")
(def-telnet-code +ip+ 244
"Interrupt Process.
The function IP.")
(def-telnet-code +ao+ 245
"Abort Output.
The function AO.")
(def-telnet-code +ayt+ 246
"Are You There.
The function AYT.")
(def-telnet-code +ec+ 247
"Erase Character.
The function EC.")
(def-telnet-code +el+ 248
"Erase Line.
The function EL.")
(def-telnet-code +ga+ 249
"Go Ahead.
The function GA.")
(def-telnet-code +sb+ 250
"Subnegotiation next.
Indicates that what follows is
subnegotiation of the indicated
option.")
(def-telnet-code +will+ 251
"WILL (option code).
Indicates the desire to begin
performing, or confirmation that
you are now performing, the
indicated option.")
(def-telnet-code +wont+ 252
"WON'T (option code).
Indicates the refusal to perform,
or continue performing, the
indicated option.")
(def-telnet-code +do+ 253
"DO (option code).
Indicates the request that the
other party perform, or
confirmation that you are expecting
the other party to perform, the
indicated option.")
(def-telnet-code +dont+ 254
"DON'T (option code).
Indicates the demand that the
other party stop performing,
or confirmation that you are no
longer expecting the other party
to perform, the indicated option.")
(def-telnet-code +IAC+ 255
"Data Byte 255.")
(defun negotiate-telnet (c &aux (ss (usocket:socket-stream c)))
"Negotiate TN3270 or telnet connection options.
NEGOTIATE-TELNET will naively (e.g. not checking client responses)
negotiate the options necessary for TN3270 on a new telnet connection,
C."
;; (declare (type usocket:stream-server-usocket c))
(write-sequence (vector +iac+ +do+ +terminal-type+) ss)
(write-sequence (vector +iac+ +sb+ +terminal-type+ +send+ +iac+ +se+) ss)
(write-sequence (vector +iac+ +do+ +eor-option+) ss)
(write-sequence (vector +iac+ +do+ +binary+) ss)
(write-sequence (vector +iac+ +will+ +eor-option+ +iac+ +will+ +binary+) ss)
(flush-connection c 5)
nil
)
(defun unnegotiate-telnet (c timeout &aux (ss (usocket:socket-stream c)))
"Un-negotiate a TN3270 or telnet connection options.
UNNEGOTIATE-TELNET will naively (e.g. not checking client responses)
attempt to restore the telnet options state to what it was before
NEGOTIATE-TELNET was called."
;; (declare (type usocket:stream-server-usocket c))
(write-sequence (vector +iac+ +wont+ +eor-option+ +iac+ +wont+ +binary+) ss)
(write-sequence (vector +iac+ +dont+ +binary+) ss)
(write-sequence (vector +iac+ +dont+ +eor-option+) ss)
(write-sequence (vector +iac+ +dont+ +terminal-type+) ss)
(flush-connection c timeout)
nil
)
(defun flush-connection (c timeout
&aux
;; (ss (usocket:socket-stream c))
(buffer (make-buffer :capacity 1024))
)
"Flush the connection C (with TIMEOUT).
FLUSH-CONNECTION discards all bytes that it can read from conn,
allowing up to the duration TIMEOUT for the first byte to be read."
(loop
(multiple-value-bind (ready time-remaining)
(usocket:wait-for-input c :timeout timeout :ready-only t)
(dbgmsg ">>> Conn ready ~S ~S ~S ~S~%" c ready timeout time-remaining)
(let ((ready-conn (first ready)))
(cond ((and ready time-remaining) ; No timeout, no error.
(usocket:with-mapped-conditions (ready-conn)
(unwind-protect
(let ((n-read (read-sequence buffer (usocket:socket-stream ready-conn)))) ; There is only C.
(dbgmsg
">>> ~D bytes read while flushing connection ~S ~S.~%"
n-read
ready-conn
timeout
))
(when (= timeout time-remaining)
(setf timeout (/ time-remaining 2.0)))
(dbgmsg ">>> Timeout now ~S~%" timeout)
))
)
(time-remaining ; And error occurred.
(dbgmsg ">>> Error while flushing.~%")
(return-from flush-connection t)
)
(ready ; Timeout, but somehow we may read.
(usocket:with-mapped-conditions (ready-conn)
(unwind-protect
(let ((n-read (read-sequence buffer (usocket:socket-stream ready-conn)))) ; There is only C.
(dbgmsg
">>> ~D bytes read while flushing connection.~%"
n-read))
(return-from flush-connection nil)
))
)
(t ; Error and timeout
(return-from flush-connection t))
)))
))
;;; telnet-read
(defun telnet-read (c pass-eor &aux (ss (usocket:socket-stream c)))
"Return the next byte of data from the connection C.
While reading, TELNET-READ filters out all telnet commands. If passEOR
is true, then telnetRead will return upon encountering the telnet End
of Record command, setting isEor to true. When isEor is true, the
value of b is meaningless and must be ignored (valid will be false).
When valid is true, the value in byte b is a real value read from the
connection; when value is false, do not use the value in b. (For
example, a valid byte AND error can be returned in the same call.)"
(let ((state :normal))
(declare (type (member :normal :command :subneg) state))
(dbgmsg ">>> Telnet read~%")
(handler-case
(loop for b of-type unsigned-byte = (read-byte ss)
do (dbgmsg ">>> TR: read (~A) ~2,'0x ~S~%" state b b)
;; I always got a new byte (I hope).
do (case state
(:normal
(cond ((= b +iac+)
(setf state :command)
(dbgmsg ">>> Entering telnet command state ~2,'0X +IAC+~%" b))
(t
(return-from telnet-read
(values b t nil nil)))
))
(:command
(cond ((= b #xFF)
(dbgmsg ">>> Leaving telnet command state; escaped #xFF ~A~%"
(telnet-code-name b))
(return-from telnet-read
(values b t nil nil)))
((= b +sb+)
(setf state :subneg)
(dbgmsg ">>> Entering telnet command ~
subnegotiation state +SB+~%"))
((and pass-eor (= b +eor+))
(dbgmsg ">>> Leaving telnet command ~
state; returning EOR~%")
(return-from telnet-read
(values 0 nil t nil)))
(t
(setf state :normal)
(dbgmsg ">>> Leaving telnet command ~
state; command was ~2,'0x ~A~%"
b (telnet-code-name b)
))
))
(:subneg
(cond ((= b +se+)
(setf state :normal)
(dbgmsg ">>> Leaving telnet command ~
subnegotiation state~%"))
(t
;; Remain in subnegotiation consuming bytes
;; until we get +se+.
(dbgmsg ">>> Consumed telnet subnegotiation ~
byte: ~2,'0X ~A~%"
b (telnet-code-name b)
))))
)
) ; LOOP
(end-of-file (eofc)
(return-from telnet-read
(values 0 nil nil eofc)))
(error (e)
(return-from telnet-read
(values 0 nil nil e)))
)))
;;;; end of file -- telnet.lisp