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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2003-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%---------------------------------------------------------------
%% Basic negotiated options with Telnet (RFC 854)
%%
%% Side A request: I WILL set option Opt.
%% Side B answer: DO go ahead, or no, DON'T set it.
%%
%% Side A request: Please DO set this option.
%% Side B answer: Ok I WILL, or no, I WON'T set it.
%%
%% "Enable option" requests may be rejected.
%% "Disable option" requests must not.
%%---------------------------------------------------------------
-module(ct_telnet_client).
%% -define(debug, true).
-export([open/2, open/3, open/4, open/5, close/1]).
-export([send_data/2, send_data/3, get_data/1]).
-define(TELNET_PORT, 23).
-define(OPEN_TIMEOUT,10000).
-define(IDLE_TIMEOUT,10000).
%% telnet control characters
-define(SE, 240).
-define(NOP, 241).
-define(DM, 242).
-define(BRK, 243).
-define(IP, 244).
-define(AO, 245).
-define(AYT, 246).
-define(EC, 247).
-define(EL, 248).
-define(GA, 249).
-define(SB, 250).
-define(WILL, 251).
-define(WONT, 252).
-define(DO, 253).
-define(DONT, 254).
-define(IAC, 255).
%% telnet options
-define(BINARY, 0).
-define(ECHO, 1).
-define(SUPPRESS_GO_AHEAD, 3).
-define(TERMINAL_TYPE, 24).
-define(WINDOW_SIZE, 31).
-record(state,{conn_name, get_data, keep_alive=true, log_pos=1}).
open(Server, ConnName) ->
open(Server, ?TELNET_PORT, ?OPEN_TIMEOUT, true, ConnName).
open(Server, Port, ConnName) ->
open(Server, Port, ?OPEN_TIMEOUT, true, ConnName).
open(Server, Port, Timeout, ConnName) ->
open(Server, Port, Timeout, true, ConnName).
open(Server, Port, Timeout, KeepAlive, ConnName) ->
Self = self(),
Pid = spawn(fun() ->
init(Self, Server, Port, Timeout,
KeepAlive, ConnName)
end),
receive
{open,Pid} ->
{ok,Pid};
{Error,Pid} ->
Error
end.
close(Pid) ->
Pid ! {close,self()},
receive closed -> ok
after 5000 -> ok
end.
send_data(Pid, Data) ->
send_data(Pid, Data, true).
send_data(Pid, Data, true) ->
send_data(Pid, Data++"\n", false);
send_data(Pid, Data, false) ->
Pid ! {send_data, Data},
ok.
get_data(Pid) ->
Pid ! {get_data,self()},
receive
{data,Data} ->
{ok,Data}
end.
%%%-----------------------------------------------------------------
%%% Internal functions
init(Parent, Server, Port, Timeout, KeepAlive, ConnName) ->
case gen_tcp:connect(Server, Port, [list,{packet,0}], Timeout) of
{ok,Sock} ->
dbg("~p connected to: ~p (port: ~w, keep_alive: ~w)\n",
[ConnName,Server,Port,KeepAlive]),
send([?IAC,?DO,?SUPPRESS_GO_AHEAD], Sock, ConnName),
Parent ! {open,self()},
loop(#state{conn_name=ConnName, get_data=10, keep_alive=KeepAlive},
Sock, []),
gen_tcp:close(Sock);
Error ->
Parent ! {Error,self()}
end.
loop(State, Sock, Acc) ->
receive
{tcp_closed,_} ->
dbg("Connection closed\n", []),
Data = lists:reverse(lists:append(Acc)),
dbg("Printing queued messages: ~tp",[Data]),
ct_telnet:log(State#state.conn_name,
general_io, "~ts",
[lists:sublist(Data,
State#state.log_pos,
length(Data))]),
receive
{get_data,Pid} ->
Pid ! closed
after 100 ->
ok
end;
{tcp,_,Msg0} ->
dbg("tcp msg: ~tp~n",[Msg0]),
Msg = check_msg(Sock,Msg0,[]),
loop(State, Sock, [Msg | Acc]);
{send_data,Data} ->
send(Data, Sock, State#state.conn_name),
loop(State, Sock, Acc);
{get_data,Pid} ->
NewState =
case Acc of
[] ->
dbg("get_data nodata\n",[]),
erlang:send_after(100,self(),{get_data_delayed,Pid}),
if State#state.keep_alive == true ->
State#state{get_data=State#state.get_data - 1};
State#state.keep_alive == false ->
State
end;
_ ->
Data = lists:reverse(lists:append(Acc)),
Len = length(Data),
dbg("get_data ~tp\n",[Data]),
ct_telnet:log(State#state.conn_name,
general_io, "~ts",
[lists:sublist(Data,
State#state.log_pos,
Len)]),
Pid ! {data,Data},
State#state{log_pos = 1}
end,
loop(NewState, Sock, []);
{get_data_delayed,Pid} ->
NewState =
case State of
#state{keep_alive = true, get_data = 0} ->
if Acc == [] -> send([?IAC,?NOP], Sock,
State#state.conn_name);
true -> ok
end,
State#state{get_data=10};
_ ->
State
end,
{NewAcc,Pos} =
case erlang:is_process_alive(Pid) of
true when Acc /= [] ->
Data = lists:reverse(lists:append(Acc)),
Len = length(Data),
dbg("get_data_delayed ~tp\n",[Data]),
ct_telnet:log(State#state.conn_name,
general_io, "~ts",
[lists:sublist(Data,
State#state.log_pos,
Len)]),
Pid ! {data,Data},
{[],1};
true when Acc == [] ->
dbg("get_data_delayed nodata\n",[]),
Pid ! {data,[]},
{[],1};
false ->
{Acc,NewState#state.log_pos}
end,
loop(NewState#state{log_pos=Pos}, Sock, NewAcc);
{close,Pid} ->
dbg("Closing connection\n", []),
if Acc == [] ->
ok;
true ->
Data = lists:reverse(lists:append(Acc)),
dbg("Printing queued messages: ~tp",[Data]),
ct_telnet:log(State#state.conn_name,
general_io, "~ts",
[lists:sublist(Data,
State#state.log_pos,
length(Data))])
end,
gen_tcp:close(Sock),
Pid ! closed
after wait(State#state.keep_alive,?IDLE_TIMEOUT) ->
Data = lists:reverse(lists:append(Acc)),
case Data of
[] ->
send([?IAC,?NOP], Sock, State#state.conn_name),
loop(State, Sock, Acc);
_ when State#state.log_pos == length(Data)+1 ->
loop(State, Sock, Acc);
_ ->
dbg("Idle timeout, printing ~tp\n",[Data]),
Len = length(Data),
ct_telnet:log(State#state.conn_name,
general_io, "~ts",
[lists:sublist(Data,
State#state.log_pos,
Len)]),
loop(State#state{log_pos = Len+1}, Sock, Acc)
end
end.
wait(true, Time) -> Time;
wait(false, _) -> infinity.
send(Data, Sock, ConnName) ->
case Data of
[?IAC|_] = Cmd ->
cmd_dbg("Sending",Cmd),
try io_lib:format("[~w] ~w", [?MODULE,Data]) of
Str ->
ct_telnet:log(ConnName, general_io, Str, [])
catch
_:_ -> ok
end;
_ ->
dbg("Sending: ~tp\n", [Data]),
try io_lib:format("[~w] ~ts", [?MODULE,Data]) of
Str ->
ct_telnet:log(ConnName, general_io, Str, [])
catch
_:_ -> ok
end
end,
gen_tcp:send(Sock, Data),
ok.
%% [IAC,IAC] = buffer data value 255
check_msg(Sock, [?IAC,?IAC | T], Acc) ->
check_msg(Sock, T, [?IAC|Acc]);
%% respond to a command
check_msg(Sock, [?IAC | Cs], Acc) ->
case get_cmd(Cs) of
{Cmd,Cs1} ->
cmd_dbg("Got",Cmd),
respond_cmd(Cmd, Sock),
check_msg(Sock, Cs1, Acc);
error ->
Acc
end;
%% buffer a data value
check_msg(Sock, [H|T], Acc) ->
check_msg(Sock, T, [H|Acc]);
check_msg(_Sock, [], Acc) ->
Acc.
%% Positive responses (WILL and DO).
respond_cmd([?WILL,?ECHO], Sock) ->
R = [?IAC,?DO,?ECHO],
cmd_dbg("Responding",R),
gen_tcp:send(Sock, R);
respond_cmd([?DO,?ECHO], Sock) ->
R = [?IAC,?WILL,?ECHO],
cmd_dbg("Responding",R),
gen_tcp:send(Sock, R);
%% Answers from server
respond_cmd([?WILL,?SUPPRESS_GO_AHEAD], _Sock) ->
dbg("Server will suppress-go-ahead\n", []);
respond_cmd([?WONT,?SUPPRESS_GO_AHEAD], _Sock) ->
dbg("Warning! Server won't suppress-go-ahead\n", []);
respond_cmd([?DONT | _Opt], _Sock) -> % server ack?
ok;
respond_cmd([?WONT | _Opt], _Sock) -> % server ack?
ok;
%% Negative responses (WON'T and DON'T). These are default!
respond_cmd([?WILL,Opt], Sock) ->
R = [?IAC,?DONT,Opt],
cmd_dbg("Responding",R),
gen_tcp:send(Sock, R);
respond_cmd([?DO | Opt], Sock) ->
R = [?IAC,?WONT | Opt],
cmd_dbg("Responding",R),
gen_tcp:send(Sock, R);
%% Commands without options (which we ignore)
respond_cmd(?NOP, _Sock) ->
ok;
%% Unexpected messages.
respond_cmd([Cmd | Opt], _Sock) when Cmd >= 240, Cmd =< 255 ->
dbg("Received cmd: ~w. Ignored!\n", [[Cmd | Opt]]);
respond_cmd([Cmd | Opt], _Sock) ->
dbg("WARNING: Received unknown cmd: ~w. Ignored!\n", [[Cmd | Opt]]).
get_cmd([Cmd | Rest]) when Cmd == ?SB ->
get_subcmd(Rest, []);
get_cmd([Cmd | Rest]) when Cmd >= 240, Cmd =< 249 ->
{?NOP, Rest};
get_cmd([Cmd,Opt | Rest]) when Cmd >= 251, Cmd =< 254 ->
{[Cmd,Opt], Rest};
get_cmd(_Other) ->
error.
get_subcmd([?SE | Rest], Acc) ->
{[?SE | lists:reverse(Acc)], Rest};
get_subcmd([Opt | Rest], Acc) ->
get_subcmd(Rest, [Opt | Acc]).
-ifdef(debug).
dbg(Str,Args) ->
TS = timestamp(),
io:format("[~p ct_telnet_client, ~s]\n" ++ Str,[self(),TS|Args]).
cmd_dbg(Prefix,Cmd) ->
case Cmd of
[?IAC|Cmd1] ->
cmd_dbg(Prefix,Cmd1);
[Ctrl|Opts] ->
CtrlStr =
case Ctrl of
?DO -> "DO";
?DONT -> "DONT";
?WILL -> "WILL";
?WONT -> "WONT";
?NOP -> "NOP";
_ -> "CMD"
end,
Opts1 =
case Opts of
[Opt] -> Opt;
_ -> Opts
end,
dbg("~ts: ~ts(~w): ~w\n", [Prefix,CtrlStr,Ctrl,Opts1]);
Any ->
dbg("Unexpected in cmd_dbg:~n~w~n",[Any])
end.
timestamp() ->
{MS,S,US} = os:timestamp(),
{{Year,Month,Day}, {Hour,Min,Sec}} =
calendar:now_to_local_time({MS,S,US}),
MilliSec = trunc(US/1000),
lists:flatten(io_lib:format("~4.10.0B-~2.10.0B-~2.10.0B "
"~2.10.0B:~2.10.0B:~2.10.0B.~3.10.0B",
[Year,Month,Day,Hour,Min,Sec,MilliSec])).
-else.
dbg(_Str,_Args) ->
ok.
cmd_dbg(_Prefix,_Cmd) ->
ok.
-endif.
|