net-telnet-rfc2217 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/net/telnet/rfc2217.rb +41 -3
- data/lib/net/telnet/rfc2217/telnet.rb +64 -62
- data/lib/net/telnet/rfc2217/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0b5b2b5e014b1fc9906ae0be1e7d33fb5d3971cce0b4eda74332e6a1ee24a9e
|
4
|
+
data.tar.gz: b56a17ffee69e60c4c8f805fd5fb16a0195a95719ed3b93a4fc5417c1dbb0e2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b4b82e214841d59828c733e9c15a65d2e623ee807f42df97353054564cb6bd7991f6900182ecd35cedebf83876c9388ecdce8574639a86b0e4d237e3e8f0a96
|
7
|
+
data.tar.gz: 35da8ce422ca87cfc75ad2c4461582d9ec54f8335f74bb381fe178d12890bae4f5034e7338488187c07cdfb6a900e2eb89f95909c7cdd536a33581062b0c5861
|
data/lib/net/telnet/rfc2217.rb
CHANGED
@@ -8,6 +8,10 @@ Net::Telnet.prepend(Net::Telnet::RFC2217::TelnetExtensions)
|
|
8
8
|
module Net
|
9
9
|
class Telnet
|
10
10
|
class RFC2217
|
11
|
+
class WaitReadable < RuntimeError
|
12
|
+
include ::IO::WaitReadable
|
13
|
+
end
|
14
|
+
|
11
15
|
attr_reader :telnet
|
12
16
|
|
13
17
|
COM_PORT_OPTION = 44.chr
|
@@ -81,7 +85,9 @@ module Net
|
|
81
85
|
@modem_params['baud'] = 115200 unless @modem_params.key?('baud')
|
82
86
|
@modem_params['data_bits'] = 8 unless @modem_params.key?('data_bits')
|
83
87
|
@modem_params['stop_bits'] = 1 unless @modem_params.key?('stop_bits')
|
84
|
-
@modem_params
|
88
|
+
unless @modem_params.key?('parity')
|
89
|
+
@modem_params['parity'] = (data_bits == 8 ? NONE : EVEN)
|
90
|
+
end
|
85
91
|
write_modem_params if telnet
|
86
92
|
end
|
87
93
|
|
@@ -116,7 +122,7 @@ module Net
|
|
116
122
|
def readpartial(length, outbuf = '')
|
117
123
|
loop do
|
118
124
|
# 0 is special and means 'read at least one control sequence'
|
119
|
-
break if length != 0
|
125
|
+
break if length != 0 || @buffer.length != 0
|
120
126
|
|
121
127
|
data = sock.sysread([length - @buffer.length, 64 * 1024].max)
|
122
128
|
|
@@ -134,7 +140,9 @@ module Net
|
|
134
140
|
true
|
135
141
|
elsif DONT[0] == control[0] && COM_PORT_OPTION == control[1]
|
136
142
|
raise "Serial port control not supported"
|
137
|
-
|
143
|
+
elsif (WILL[0] == control[0] || DONT[0] == control[0]) && OPT_ECHO == control[1]
|
144
|
+
# just ignore echo requests
|
145
|
+
true
|
138
146
|
else
|
139
147
|
false
|
140
148
|
end
|
@@ -149,6 +157,32 @@ module Net
|
|
149
157
|
outbuf
|
150
158
|
end
|
151
159
|
|
160
|
+
def read_nonblock(length, outbuf = '', options = {})
|
161
|
+
if outbuf == ({ exception: false })
|
162
|
+
options = outbuf
|
163
|
+
outbuf = ''
|
164
|
+
end
|
165
|
+
loop do
|
166
|
+
result = wait_readable(0)
|
167
|
+
if result == nil
|
168
|
+
raise WaitReadable unless options[:exception] == false
|
169
|
+
return :wait_readable
|
170
|
+
end
|
171
|
+
# we have to try to consume control characters first
|
172
|
+
readpartial(0)
|
173
|
+
# and then only do a real read if we have something
|
174
|
+
return readpartial(length, outbuf) unless @buffer.empty?
|
175
|
+
# otherwise loop and see if there's more there
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def wait_readable(timeout = nil)
|
180
|
+
return true unless @buffer.empty?
|
181
|
+
result = sock.wait_readable(timeout)
|
182
|
+
result = self if result == sock
|
183
|
+
result
|
184
|
+
end
|
185
|
+
|
152
186
|
def ungetbyte(b)
|
153
187
|
@buffer.insert(0, b.chr)
|
154
188
|
end
|
@@ -162,6 +196,10 @@ module Net
|
|
162
196
|
telnet.write(string)
|
163
197
|
end
|
164
198
|
|
199
|
+
def flush
|
200
|
+
sock.flush
|
201
|
+
end
|
202
|
+
|
165
203
|
def close
|
166
204
|
telnet.close
|
167
205
|
end
|
@@ -1,78 +1,80 @@
|
|
1
1
|
module Net
|
2
2
|
class Telnet
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
class RFC2217
|
4
|
+
module TelnetExtensions
|
5
|
+
def preprocess(string)
|
6
|
+
# combine CR+NULL into CR
|
7
|
+
string = string.gsub(/#{CR}#{NULL}/no, CR) if @options["Telnetmode"]
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
# combine EOL into "\n"
|
10
|
+
string = string.gsub(/#{EOL}/no, "\n") unless @options["Binmode"]
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
# remove NULL
|
13
|
+
string = string.gsub(/#{NULL}/no, '') unless @options["Binmode"]
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
string.gsub(/#{IAC}(
|
16
|
+
[#{IAC}#{AO}#{AYT}#{DM}#{IP}#{NOP}]|
|
17
|
+
[#{DO}#{DONT}#{WILL}#{WONT}]
|
18
|
+
[#{OPT_BINARY}-#{OPT_NEW_ENVIRON}#{44.chr}#{OPT_EXOPL}]|
|
19
|
+
#{SB}[^#{IAC}]*#{IAC}#{SE}
|
20
|
+
)/xno) do
|
21
|
+
if block_given? && IAC != $1
|
22
|
+
# already handled; don't handle it ourselves
|
23
|
+
next '' if yield $1
|
24
|
+
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
26
|
+
if IAC == $1 # handle escaped IAC characters
|
27
|
+
IAC
|
28
|
+
elsif AYT == $1 # respond to "IAC AYT" (are you there)
|
29
|
+
self.write("nobody here but us pigeons" + EOL)
|
30
|
+
''
|
31
|
+
elsif DO[0] == $1[0] # respond to "IAC DO x"
|
32
|
+
if OPT_BINARY[0] == $1[1]
|
33
|
+
unless @telnet_option["BINARY"] == true
|
34
|
+
@telnet_option["BINARY"] = true
|
35
|
+
self.write(IAC + WILL + OPT_BINARY)
|
36
|
+
end
|
37
|
+
else
|
38
|
+
self.write(IAC + WONT + $1[1..1])
|
35
39
|
end
|
36
|
-
|
40
|
+
''
|
41
|
+
elsif DONT[0] == $1[0] # respond to "IAC DON'T x" with "IAC WON'T x"
|
37
42
|
self.write(IAC + WONT + $1[1..1])
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
self.write(IAC + DO + OPT_SGA)
|
43
|
+
''
|
44
|
+
elsif WILL[0] == $1[0] # respond to "IAC WILL x"
|
45
|
+
if OPT_BINARY[0] == $1[1]
|
46
|
+
self.write(IAC + DO + OPT_BINARY)
|
47
|
+
elsif OPT_ECHO[0] == $1[1]
|
48
|
+
self.write(IAC + DO + OPT_ECHO)
|
49
|
+
elsif OPT_SGA[0] == $1[1]
|
50
|
+
unless @telnet_option["SGA"]
|
51
|
+
@telnet_option["SGA"] = true
|
52
|
+
self.write(IAC + DO + OPT_SGA)
|
53
|
+
end
|
54
|
+
else
|
55
|
+
self.write(IAC + DONT + $1[1..1])
|
52
56
|
end
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
self.write(IAC + DONT +
|
57
|
+
''
|
58
|
+
elsif WONT[0] == $1[0] # respond to "IAC WON'T x"
|
59
|
+
if OPT_ECHO[0] == $1[1]
|
60
|
+
self.write(IAC + DONT + OPT_ECHO)
|
61
|
+
elsif OPT_SGA[0] == $1[1]
|
62
|
+
unless @telnet_option["SGA"] == false
|
63
|
+
@telnet_option["SGA"] = false
|
64
|
+
self.write(IAC + DONT + OPT_SGA)
|
65
|
+
end
|
66
|
+
else
|
67
|
+
self.write(IAC + DONT + $1[1..1])
|
64
68
|
end
|
69
|
+
''
|
70
|
+
elsif SB[0] == $1[0] # yield sub option
|
71
|
+
''
|
65
72
|
else
|
66
|
-
|
73
|
+
''
|
67
74
|
end
|
68
|
-
''
|
69
|
-
elsif SB[0] == $1[0] # yield sub option
|
70
|
-
''
|
71
|
-
else
|
72
|
-
''
|
73
75
|
end
|
74
|
-
end
|
75
|
-
end
|
76
|
+
end # preprocess
|
77
|
+
end
|
76
78
|
end
|
77
79
|
end
|
78
80
|
end
|