| 1 |
;;; |
| 2 |
;;; telnet.scm - telnet protocol implementation |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;;; RFC 854 Telnet Protocol Specification |
| 35 |
;;; RFC 855 Telnet Option Specifications |
| 36 |
;;; RFC 856 Telnet Binary Transmission |
| 37 |
;;; RFC 857 Telnet Echo Option |
| 38 |
;;; RFC 858 Telnet Suppress Go Ahead option |
| 39 |
;;; RFC 859 Telnet Status Option |
| 40 |
;;; RFC 860 Telnet Timing Mark option |
| 41 |
;;; RFC 861 Telnet Extended Options option |
| 42 |
;;; RFC 1091 Telnet Terminal Type option |
| 43 |
;;; RFC 1096 Telnet X Display location option |
| 44 |
;;; RFC 1184 Telnet Linemode option |
| 45 |
;;; RFC 1372 Telnet Remote Flow option |
| 46 |
;;; RFC 1408 Telnet Environment option |
| 47 |
;;; RFC 1571 Telnet Environment option interoperability issues |
| 48 |
;;; RFC 1572 Telnet Environment option |
| 49 |
;;; RFC 2066 Telnet Charset option |
| 50 |
;;; RFC 2941 Telnet Authentication option |
| 51 |
;;; RFC 2840 Telnet Kermit option |
| 52 |
;;; RFC 2217 Telnet Com Port option |
| 53 |
;;; RFC 1073 Telnet Window Size option |
| 54 |
;;; RFC 1079 Telnet Terminal Speed option |
| 55 |
;;; RFC 727 Telnet logout option |
| 56 |
|
| 57 |
(define-module rfc.telnet |
| 58 |
(use gauche.net) |
| 59 |
(export <telnet> |
| 60 |
)) |
| 61 |
(select-module rfc.telnet) |
| 62 |
|
| 63 |
(define *telnet-port* 23) |
| 64 |
|
| 65 |
;; Control codes (RFC 854) |
| 66 |
(define *telnet-se* 240) ;F0 |
| 67 |
(define *telnet-nop* 241) ;F1 |
| 68 |
(define *telnet-data-mark* 242) ;F2 |
| 69 |
(define *telnet-break* 243) ;F3 |
| 70 |
(define *telnet-intr* 244) ;F4 |
| 71 |
(define *telnet-abort* 245) ;F5 |
| 72 |
(define *telnet-ayt* 246) ;F6 |
| 73 |
(define *telnet-erase-char* 247) ;F7 |
| 74 |
(define *telnet-erase-line* 248) ;F8 |
| 75 |
(define *telnet-go-ahead* 249) ;F9 |
| 76 |
(define *telnet-sb* 250) ;FA |
| 77 |
(define *telnet-will* 251) ;FB |
| 78 |
(define *telnet-wont* 252) ;FC |
| 79 |
(define *telnet-do* 253) ;FD |
| 80 |
(define *telnet-dont* 254) ;FE |
| 81 |
(define *telnet-iac* 255) ;FF |
| 82 |
|
| 83 |
;; Known options |
| 84 |
(define *telnet-transmit-binary* 0) ;RFC 856 |
| 85 |
(define *telnet-echo* 1) ;RFC 857 |
| 86 |
(define *telnet-suppress-go-ahead* 3) ;RFC 858 |
| 87 |
(define *telnet-status* 5) ;RFC 859 |
| 88 |
(define *telnet-timing-mark* 6) ;RFC 860 |
| 89 |
(define *telnet-logout* 18) ;RFC 727 |
| 90 |
(define *telnet-terminal-type* 24) ;RFC 1091 |
| 91 |
(define *telnet-naws* 31) ;RFC 1073 |
| 92 |
(define *telnet-terminal-speed* 32) ;RFC 1079 |
| 93 |
(define *telnet-toggle-flow-control* 33) ;RFC 1372 |
| 94 |
(define *telnet-linemode* 34) ;RFC 1184 |
| 95 |
(define *telnet-xdisploc* 35) ;RFC 1096 |
| 96 |
(define *telnet-environ* 36) ;RFC 1408, 1571 |
| 97 |
(define *telnet-authentication* 37) ;RFC 2941 |
| 98 |
(define *telnet-new-environ* 39) ;RFC 1572 |
| 99 |
(define *telnet-charset* 42) ;RFC 2066 |
| 100 |
(define *telnet-com-port-option* 44) ;RFC 2217 |
| 101 |
(define *telnet-kermit* 47) ;RFC 2840 |
| 102 |
(define *telnet-exopl* 255) ;RFC 861 |
| 103 |
|
| 104 |
;; telnet class |
| 105 |
(define-class <telnet> () |
| 106 |
((socket :initform #f :init-keyword :socket :getter socket-of) |
| 107 |
(input :getter input-of) |
| 108 |
(output :getter output-of) |
| 109 |
)) |
| 110 |
|
| 111 |
(define-method initialize ((self <telnet>) initargs) |
| 112 |
(next-method) |
| 113 |
(let ((sock (setup-socket self initargs))) |
| 114 |
(slot-set! self 'input (socket-input-port sock :buffered? #f)) |
| 115 |
(slot-set! self 'output (socket-output-port sock :buffered? #f)) |
| 116 |
)) |
| 117 |
|
| 118 |
(define (setup-socket self initargs) |
| 119 |
(let ((sock (socket-of self))) |
| 120 |
(cond ((not sock) |
| 121 |
(let-keywords initargs ((host #f) |
| 122 |
(port *telnet-port*)) |
| 123 |
(unless host |
| 124 |
(error "either connected socket or remote host name must be given to initialize a telnet session. Given initargs is" initargs)) |
| 125 |
(let ((sock (make-client-socket 'inet host port))) |
| 126 |
(slot-set! self 'socket sock) |
| 127 |
sock))) |
| 128 |
((not (and (is-a? sock <socket>) |
| 129 |
(eq? (socket-status sock) 'connected))) |
| 130 |
(error "connected socket required to initialize a telnet session, but got" |
| 131 |
sock)) |
| 132 |
(else sock)))) |
| 133 |
|
| 134 |
;;; Client operations |
| 135 |
|
| 136 |
;; Send normal message. IAC is escaped. |
| 137 |
(define-method telnet-send-message ((self <telnet>) string) |
| 138 |
(let ((out (output-of self))) |
| 139 |
(with-input-from-string string |
| 140 |
(lambda () |
| 141 |
(let loop ((b (read-byte))) |
| 142 |
(cond ((eof-object? b)) |
| 143 |
((= b *telnet-iac*) (write-byte b out) |
| 144 |
(write-byte b out) |
| 145 |
(loop (read-byte))) |
| 146 |
(else (write-byte b out) |
| 147 |
(loop (read-byte))))))) |
| 148 |
(flush out))) |
| 149 |
|
| 150 |
;; Send Command. |
| 151 |
(define (%telnet-send-command self args) |
| 152 |
(let ((out (output-of self))) |
| 153 |
(write-byte *telnet-iac* out) |
| 154 |
(for-each (^b (write-byte b out)) args))) |
| 155 |
|
| 156 |
(define-method telnet-recv ((self <telnet>)) |
| 157 |
(let* ((inb (read-block 4000 (input-of self)))) |
| 158 |
inb)) |