43
44
parse_config(Options) ->
44
45
parse_config(Options, #config{}).
46
parse_config([{Key, Val} | Tail], Config) when record(Config, config) ->
47
parse_config(Options, Config) ->
48
do_parse_config(Options, Config).
50
do_parse_config([{Key, Val} | Tail], Config) when record(Config, config) ->
51
parse_config(Tail, Config#config{debug_level = Val});
55
do_parse_config(Tail, Config#config{debug_level = Val});
57
do_parse_config(Tail, Config#config{debug_level = Val});
53
parse_config(Tail, Config#config{debug_level = Val});
59
do_parse_config(Tail, Config#config{debug_level = Val});
55
parse_config(Tail, Config#config{debug_level = Val});
61
do_parse_config(Tail, Config#config{debug_level = Val});
57
parse_config(Tail, Config#config{debug_level = Val});
63
do_parse_config(Tail, Config#config{debug_level = Val});
59
parse_config(Tail, Config#config{debug_level = Val});
65
do_parse_config(Tail, Config#config{debug_level = Val});
61
67
exit({badarg, {Key, Val}})
66
parse_config(Tail, Config#config{udp_host = Val});
72
do_parse_config(Tail, Config#config{udp_host = Val});
73
tuple(Val), size(Val) == 4 ->
74
do_parse_config(Tail, Config#config{udp_host = Val});
75
tuple(Val), size(Val) == 8 ->
76
do_parse_config(Tail, Config#config{udp_host = Val});
68
78
exit({badarg, {Key, Val}})
72
82
integer(Val), Val >= 0 ->
73
parse_config(Tail, Config#config{udp_port = Val});
83
Config2 = Config#config{udp_port = Val, udp_options = Config#config.udp_options},
84
do_parse_config(Tail, Config2);
75
86
exit({badarg, {Key, Val}})
80
parse_config(Tail, Config#config{port_policy = Val});
91
do_parse_config(Tail, Config#config{port_policy = Val});
82
parse_config(Tail, Config#config{port_policy = random});
93
do_parse_config(Tail, Config#config{port_policy = random});
83
94
MinMax when integer(MinMax), MinMax > 0 ->
84
parse_config(Tail, Config#config{port_policy = {range, MinMax, MinMax}});
95
do_parse_config(Tail, Config#config{port_policy = {range, MinMax, MinMax}});
85
96
{range, Min, Max} when Max >= Min,
86
97
integer(Min), Min > 0,
87
98
integer(Max), Max > 0 ->
88
parse_config(Tail, Config#config{port_policy = Val});
99
do_parse_config(Tail, Config#config{port_policy = Val});
90
101
exit({badarg, {Key, Val}})
99
110
exit({badarg, {udp, [V]}})
101
112
UdpOptions = lists:foldl(Fun, Config#config.udp_options, Val),
102
parse_config(Tail, Config#config{udp_options = UdpOptions});
113
do_parse_config(Tail, Config#config{udp_options = UdpOptions});
106
parse_config(Tail, Config#config{use_tsize = Val});
117
do_parse_config(Tail, Config#config{use_tsize = Val});
108
parse_config(Tail, Config#config{use_tsize = Val});
119
do_parse_config(Tail, Config#config{use_tsize = Val});
110
121
exit({badarg, {Key, Val}})
114
125
Val == infinity ->
115
parse_config(Tail, Config#config{max_tsize = Val});
126
do_parse_config(Tail, Config#config{max_tsize = Val});
116
127
integer(Val), Val >= 0 ->
117
parse_config(Tail, Config#config{max_tsize = Val});
128
do_parse_config(Tail, Config#config{max_tsize = Val});
119
130
exit({badarg, {Key, Val}})
123
134
Val == infinity ->
124
parse_config(Tail, Config#config{max_conn = Val});
135
do_parse_config(Tail, Config#config{max_conn = Val});
125
136
integer(Val), Val > 0 ->
126
parse_config(Tail, Config#config{max_conn = Val});
137
do_parse_config(Tail, Config#config{max_conn = Val});
128
139
exit({badarg, {Key, Val}})
131
142
Key2 = to_lower(Key),
132
143
Val2 = to_lower(Val),
133
144
TftpOptions = replace_val(Key2, Val2, Config#config.user_options),
134
parse_config(Tail, Config#config{user_options = TftpOptions});
145
do_parse_config(Tail, Config#config{user_options = TftpOptions});
138
149
Rejected = [Val | Config#config.rejected],
139
parse_config(Tail, Config#config{rejected = Rejected});
150
do_parse_config(Tail, Config#config{rejected = Rejected});
141
152
Rejected = [Val | Config#config.rejected],
142
parse_config(Tail, Config#config{rejected = Rejected});
153
do_parse_config(Tail, Config#config{rejected = Rejected});
143
154
_ when list(Val) ->
144
155
Rejected = [Val | Config#config.rejected],
145
parse_config(Tail, Config#config{rejected = Rejected});
156
do_parse_config(Tail, Config#config{rejected = Rejected});
147
158
exit({badarg, {Key, Val}})
167
178
exit({badarg, {Key, Val}})
169
parse_config([], Config) when record(Config, config) ->
170
UdpOptions = lists:reverse(Config#config.udp_options),
180
do_parse_config([], Config) when record(Config, config) ->
181
UdpOptions = Config#config.udp_options,
182
IsInet6 = lists:member(inet6, UdpOptions),
183
IsInet = lists:member(inet, UdpOptions),
184
Host = Config#config.udp_host,
187
(IsInet and not IsInet6); (not IsInet and not IsInet6) ->
188
case inet:getaddr(Host, inet) of
192
exit({badarg, {host, Reason}})
194
(IsInet6 and not IsInet) ->
195
case inet:getaddr(Host, inet6) of
199
exit({badarg, {host, Reason}})
202
%% Conflicting options
203
exit({badarg, {udp, [inet]}})
205
UdpOptions2 = lists:reverse(UdpOptions),
171
206
TftpOptions = lists:reverse(Config#config.user_options),
172
Config#config{udp_options = UdpOptions, user_options = TftpOptions};
173
parse_config(Options, Config) when record(Config, config) ->
207
Config#config{udp_host = Host2, udp_options = UdpOptions2, user_options = TftpOptions};
208
do_parse_config(Options, Config) when record(Config, config) ->
174
209
exit({badarg, Options}).
211
host_to_string(Host) ->
213
String when list(String) ->
215
{A1, A2, A3, A4} -> % inet
216
lists:concat([A1, ".", A2, ".", A3, ".",A4]);
217
{A1, A2, A3, A4, A5, A6, A7, A8} -> % inet6
219
int16_to_hex(A1), "::",
220
int16_to_hex(A2), "::",
221
int16_to_hex(A3), "::",
222
int16_to_hex(A4), "::",
223
int16_to_hex(A5), "::",
224
int16_to_hex(A6), "::",
225
int16_to_hex(A7), "::",
233
N1 = ((I bsr 8) band 16#ff),
235
[code_character(N1 div 16), code_character(N1 rem 16),
236
code_character(N2 div 16), code_character(N2 rem 16)].
238
code_character(N) when N < 10 ->
176
243
%%-------------------------------------------------------------------
178
245
%%-------------------------------------------------------------------