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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2005-2018. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%
%%%-------------------------------------------------------------------
%%% File : tftp_lib.erl
%%% Author : Hakan Mattsson <hakan@erix.ericsson.se>
%%% Description : Option parsing, decode, encode etc.
%%%
%%% Created : 18 May 2004 by Hakan Mattsson <hakan@erix.ericsson.se>
%%%-------------------------------------------------------------------
-module(tftp_lib).
%%-------------------------------------------------------------------
%% Interface
%%-------------------------------------------------------------------
%% application internal functions
-export([
parse_config/1,
parse_config/2,
decode_msg/1,
encode_msg/1,
replace_val/3,
to_lower/1,
host_to_string/1,
add_default_callbacks/1
]).
%%-------------------------------------------------------------------
%% Defines
%%-------------------------------------------------------------------
-include("tftp.hrl").
-define(LOWER(Char),
if
Char >= $A, Char =< $Z ->
Char - ($A - $a);
true ->
Char
end).
%%-------------------------------------------------------------------
%% Config
%%-------------------------------------------------------------------
parse_config(Options) ->
parse_config(Options, #config{}).
parse_config(Options, Config) ->
do_parse_config(Options, Config).
do_parse_config([{Key, Val} | Tail], Config) when is_record(Config, config) ->
case Key of
debug ->
if
Val =:= 0; Val =:= none ->
do_parse_config(Tail, Config#config{debug_level = none});
Val =:= 1; Val =:= error ->
do_parse_config(Tail, Config#config{debug_level = error});
Val =:= 2; Val =:= warning ->
do_parse_config(Tail, Config#config{debug_level = warning});
Val =:= 3; Val =:= brief ->
do_parse_config(Tail, Config#config{debug_level = brief});
Val =:= 4; Val =:= normal ->
do_parse_config(Tail, Config#config{debug_level = normal});
Val =:= 5; Val =:= verbose ->
do_parse_config(Tail, Config#config{debug_level = verbose});
Val =:= 6; Val =:= all ->
do_parse_config(Tail, Config#config{debug_level = all});
true ->
exit({badarg, {Key, Val}})
end;
host ->
if
is_list(Val) ->
do_parse_config(Tail, Config#config{udp_host = Val});
is_tuple(Val), size(Val) =:= 4 ->
do_parse_config(Tail, Config#config{udp_host = Val});
is_tuple(Val), size(Val) =:= 8 ->
do_parse_config(Tail, Config#config{udp_host = Val});
true ->
exit({badarg, {Key, Val}})
end;
port ->
if
is_integer(Val), Val >= 0 ->
Config2 = Config#config{udp_port = Val, udp_options = Config#config.udp_options},
do_parse_config(Tail, Config2);
true ->
exit({badarg, {Key, Val}})
end;
port_policy ->
case Val of
random ->
do_parse_config(Tail, Config#config{port_policy = Val});
0 ->
do_parse_config(Tail, Config#config{port_policy = random});
MinMax when is_integer(MinMax), MinMax > 0 ->
do_parse_config(Tail, Config#config{port_policy = {range, MinMax, MinMax}});
{range, Min, Max} when Max >= Min,
is_integer(Min), Min > 0,
is_integer(Max), Max > 0 ->
do_parse_config(Tail, Config#config{port_policy = Val});
true ->
exit({badarg, {Key, Val}})
end;
udp when is_list(Val) ->
Fun =
fun({K, V}, List) when K /= active ->
replace_val(K, V, List);
(V, List) when V /= list, V /= binary ->
List ++ [V];
(V, _List) ->
exit({badarg, {udp, [V]}})
end,
UdpOptions = lists:foldl(Fun, Config#config.udp_options, Val),
do_parse_config(Tail, Config#config{udp_options = UdpOptions});
use_tsize ->
case Val of
true ->
do_parse_config(Tail, Config#config{use_tsize = Val});
false ->
do_parse_config(Tail, Config#config{use_tsize = Val});
_ ->
exit({badarg, {Key, Val}})
end;
max_tsize ->
if
Val =:= infinity ->
do_parse_config(Tail, Config#config{max_tsize = Val});
is_integer(Val), Val >= 0 ->
do_parse_config(Tail, Config#config{max_tsize = Val});
true ->
exit({badarg, {Key, Val}})
end;
max_conn ->
if
Val =:= infinity ->
do_parse_config(Tail, Config#config{max_conn = Val});
is_integer(Val), Val > 0 ->
do_parse_config(Tail, Config#config{max_conn = Val});
true ->
exit({badarg, {Key, Val}})
end;
_ when is_list(Key), is_list(Val) ->
Key2 = to_lower(Key),
Val2 = to_lower(Val),
TftpOptions = replace_val(Key2, Val2, Config#config.user_options),
do_parse_config(Tail, Config#config{user_options = TftpOptions});
reject ->
case Val of
read ->
Rejected = [Val | Config#config.rejected],
do_parse_config(Tail, Config#config{rejected = Rejected});
write ->
Rejected = [Val | Config#config.rejected],
do_parse_config(Tail, Config#config{rejected = Rejected});
_ when is_list(Val) ->
Rejected = [Val | Config#config.rejected],
do_parse_config(Tail, Config#config{rejected = Rejected});
_ ->
exit({badarg, {Key, Val}})
end;
callback ->
case Val of
{RegExp, Mod, State} when is_list(RegExp), is_atom(Mod) ->
case re:compile(RegExp) of
{ok, Internal} ->
Callback = #callback{regexp = RegExp,
internal = Internal,
module = Mod,
state = State},
Callbacks = Config#config.callbacks ++ [Callback],
do_parse_config(Tail, Config#config{callbacks = Callbacks});
{error, Reason} ->
exit({badarg, {Key, Val}, Reason})
end;
_ ->
exit({badarg, {Key, Val}})
end;
logger ->
if
is_atom(Val) ->
do_parse_config(Tail, Config#config{logger = Val});
true ->
exit({badarg, {Key, Val}})
end;
max_retries ->
if
is_integer(Val), Val > 0 ->
do_parse_config(Tail, Config#config{max_retries = Val});
true ->
exit({badarg, {Key, Val}})
end;
_ ->
exit({badarg, {Key, Val}})
end;
do_parse_config([], #config{udp_host = Host,
udp_options = UdpOptions,
user_options = UserOptions,
callbacks = Callbacks} = Config) ->
IsInet6 = lists:member(inet6, UdpOptions),
IsInet = lists:member(inet, UdpOptions),
Host2 =
if
(IsInet and not IsInet6); (not IsInet and not IsInet6) ->
case inet:getaddr(Host, inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
exit({badarg, {host, Reason}})
end;
(IsInet6 and not IsInet) ->
case inet:getaddr(Host, inet6) of
{ok, Addr} ->
Addr;
{error, Reason} ->
exit({badarg, {host, Reason}})
end;
true ->
%% Conflicting options
exit({badarg, {udp, [inet]}})
end,
UdpOptions2 = lists:reverse(UdpOptions),
TftpOptions = lists:reverse(UserOptions),
Callbacks2 = add_default_callbacks(Callbacks),
Config#config{udp_host = Host2,
udp_options = UdpOptions2,
user_options = TftpOptions,
callbacks = Callbacks2};
do_parse_config(Options, Config) when is_record(Config, config) ->
exit({badarg, Options}).
add_default_callbacks(Callbacks) ->
RegExp = "",
{ok, Internal} = re:compile(RegExp),
File = #callback{regexp = RegExp,
internal = Internal,
module = tftp_file,
state = []},
Bin = #callback{regexp = RegExp,
internal = Internal,
module = tftp_binary,
state = []},
Callbacks ++ [File, Bin].
host_to_string(Host) ->
case Host of
String when is_list(String) ->
String;
{A1, A2, A3, A4} -> % inet
lists:concat([A1, ".", A2, ".", A3, ".",A4]);
{A1, A2, A3, A4, A5, A6, A7, A8} -> % inet6
lists:concat([
int16_to_hex(A1), "::",
int16_to_hex(A2), "::",
int16_to_hex(A3), "::",
int16_to_hex(A4), "::",
int16_to_hex(A5), "::",
int16_to_hex(A6), "::",
int16_to_hex(A7), "::",
int16_to_hex(A8)
])
end.
int16_to_hex(0) ->
[$0];
int16_to_hex(I) ->
N1 = ((I bsr 8) band 16#ff),
N2 = (I band 16#ff),
[code_character(N1 div 16), code_character(N1 rem 16),
code_character(N2 div 16), code_character(N2 rem 16)].
code_character(N) when N < 10 ->
$0 + N;
code_character(N) ->
$A + (N - 10).
%%-------------------------------------------------------------------
%% Decode
%%-------------------------------------------------------------------
decode_msg(Bin) when is_binary(Bin) ->
case Bin of
<<?TFTP_OPCODE_RRQ:16/integer, Tail/binary>> ->
case decode_strings(Tail, [keep_case, lower_case]) of
[Filename, Mode | Strings] ->
Options = decode_options(Strings),
#tftp_msg_req{access = read,
filename = Filename,
mode = to_lower(Mode),
options = Options};
[_Filename | _Strings] ->
exit(#tftp_msg_error{code = undef,
text = "Missing mode"});
_ ->
exit(#tftp_msg_error{code = undef,
text = "Missing filename"})
end;
<<?TFTP_OPCODE_WRQ:16/integer, Tail/binary>> ->
case decode_strings(Tail, [keep_case, lower_case]) of
[Filename, Mode | Strings] ->
Options = decode_options(Strings),
#tftp_msg_req{access = write,
filename = Filename,
mode = to_lower(Mode),
options = Options};
[_Filename | _Strings] ->
exit(#tftp_msg_error{code = undef,
text = "Missing mode"});
_ ->
exit(#tftp_msg_error{code = undef,
text = "Missing filename"})
end;
<<?TFTP_OPCODE_DATA:16/integer, SeqNo:16/integer, Data/binary>> ->
#tftp_msg_data{block_no = SeqNo, data = Data};
<<?TFTP_OPCODE_ACK:16/integer, SeqNo:16/integer>> ->
#tftp_msg_ack{block_no = SeqNo};
<<?TFTP_OPCODE_ERROR:16/integer, ErrorCode:16/integer, Tail/binary>> ->
case decode_strings(Tail, [keep_case]) of
[ErrorText] ->
ErrorCode2 = decode_error_code(ErrorCode),
#tftp_msg_error{code = ErrorCode2,
text = ErrorText};
_ ->
exit(#tftp_msg_error{code = undef,
text = "Trailing garbage"})
end;
<<?TFTP_OPCODE_OACK:16/integer, Tail/binary>> ->
Strings = decode_strings(Tail, [lower_case]),
Options = decode_options(Strings),
#tftp_msg_oack{options = Options};
_ ->
exit(#tftp_msg_error{code = undef,
text = "Invalid syntax"})
end.
decode_strings(Bin, Cases) when is_binary(Bin), is_list(Cases) ->
do_decode_strings(Bin, Cases, []).
do_decode_strings(<<>>, _Cases, Strings) ->
lists:reverse(Strings);
do_decode_strings(Bin, [Case | Cases], Strings) ->
{String, Tail} = decode_string(Bin, Case, []),
if
Cases =:= [] ->
do_decode_strings(Tail, [Case], [String | Strings]);
true ->
do_decode_strings(Tail, Cases, [String | Strings])
end.
decode_string(<<Char:8/integer, Tail/binary>>, Case, String) ->
if
Char =:= 0 ->
{lists:reverse(String), Tail};
Case =:= keep_case ->
decode_string(Tail, Case, [Char | String]);
Case =:= lower_case ->
Char2 = ?LOWER(Char),
decode_string(Tail, Case, [Char2 | String])
end;
decode_string(<<>>, _Case, _String) ->
exit(#tftp_msg_error{code = undef, text = "Trailing null missing"}).
decode_options([Key, Value | Strings]) ->
[{to_lower(Key), Value} | decode_options(Strings)];
decode_options([]) ->
[].
decode_error_code(Int) ->
case Int of
?TFTP_ERROR_UNDEF -> undef;
?TFTP_ERROR_ENOENT -> enoent;
?TFTP_ERROR_EACCES -> eacces;
?TFTP_ERROR_ENOSPC -> enospc;
?TFTP_ERROR_BADOP -> badop;
?TFTP_ERROR_BADBLK -> badblk;
?TFTP_ERROR_EEXIST -> eexist;
?TFTP_ERROR_BADUSER -> baduser;
?TFTP_ERROR_BADOPT -> badopt;
Int when is_integer(Int), Int >= 0, Int =< 65535 -> Int;
_ -> exit(#tftp_msg_error{code = undef, text = "Error code outside range."})
end.
%%-------------------------------------------------------------------
%% Encode
%%-------------------------------------------------------------------
encode_msg(#tftp_msg_req{access = Access,
filename = Filename,
mode = Mode,
options = Options}) ->
OpCode = case Access of
read -> ?TFTP_OPCODE_RRQ;
write -> ?TFTP_OPCODE_WRQ
end,
[
<<OpCode:16/integer>>,
Filename,
0,
Mode,
0,
[[Key, 0, Val, 0] || {Key, Val} <- Options]
];
encode_msg(#tftp_msg_data{block_no = BlockNo, data = Data}) when BlockNo =< 65535 ->
[
<<?TFTP_OPCODE_DATA:16/integer, BlockNo:16/integer>>,
Data
];
encode_msg(#tftp_msg_ack{block_no = BlockNo}) when BlockNo =< 65535 ->
<<?TFTP_OPCODE_ACK:16/integer, BlockNo:16/integer>>;
encode_msg(#tftp_msg_error{code = Code, text = Text}) ->
IntCode = encode_error_code(Code),
[
<<?TFTP_OPCODE_ERROR:16/integer, IntCode:16/integer>>,
Text,
0
];
encode_msg(#tftp_msg_oack{options = Options}) ->
[
<<?TFTP_OPCODE_OACK:16/integer>>,
[[Key, 0, Val, 0] || {Key, Val} <- Options]
].
encode_error_code(Code) ->
case Code of
undef -> ?TFTP_ERROR_UNDEF;
enoent -> ?TFTP_ERROR_ENOENT;
eacces -> ?TFTP_ERROR_EACCES;
enospc -> ?TFTP_ERROR_ENOSPC;
badop -> ?TFTP_ERROR_BADOP;
badblk -> ?TFTP_ERROR_BADBLK;
eexist -> ?TFTP_ERROR_EEXIST;
baduser -> ?TFTP_ERROR_BADUSER;
badopt -> ?TFTP_ERROR_BADOPT;
Int when is_integer(Int), Int >= 0, Int =< 65535 -> Int
end.
%%-------------------------------------------------------------------
%% Miscellaneous
%%-------------------------------------------------------------------
replace_val(Key, Val, List) ->
case lists:keysearch(Key, 1, List) of
false ->
List ++ [{Key, Val}];
{value, {_, OldVal}} when OldVal =:= Val ->
List;
{value, {_, _}} ->
lists:keyreplace(Key, 1, List, {Key, Val})
end.
to_lower(Chars) ->
[?LOWER(Char) || Char <- Chars].
|