forked from NetComposer/nkpacket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnkpacket.erl
773 lines (604 loc) · 23.3 KB
/
nkpacket.erl
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2016 Carlos Gonzalez Florido. All Rights Reserved.
%%
%% This file is provided to you 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.
%%
%% -------------------------------------------------------------------
%% @doc Main management module.
%% TODO:
%% - WS Client Compression
%% - Simplify HTTP/WS listeners not using Cowboy's process but only cowlib?
-module(nkpacket).
-author('Carlos Gonzalez <[email protected]>').
-export([register_protocol/2, register_protocol/3]).
-export([get_protocol/1, get_protocol/2]).
-export([start_listener/2, get_listener/2, stop_listener/1]).
-export([get_all/0, get_all/1, get_class/0, get_class/1]).
-export([stop_all/0, stop_all/1]).
-export([send/2, send/3, connect/2]).
-export([get_listening/2, get_listening/3, is_local/1, is_local/2, is_local_ip/1]).
-export([pid/1, get_nkport/1, get_local/1, get_remote/1, get_remote_bin/1]).
-export([get_meta/1, get_user/1]).
-export([resolve/1, resolve/2, multi_resolve/1, multi_resolve/2]).
-export_type([listen_id/0, class/0, transport/0, protocol/0, nkport/0, netspec/0]).
-export_type([listener_opts/0, connect_opts/0, send_opts/0, resolve_opts/0]).
-export_type([connection/0, raw_connection/0, send_spec/0]).
-export_type([http_proto/0, incoming/0, outcoming/0, pre_send_fun/0]).
-include_lib("nklib/include/nklib.hrl").
-include("nkpacket.hrl").
%% ===================================================================
%% Types
%% ===================================================================
%% Each listening transport has an unique id, generated from transp, ip, port and path
-type listen_id() :: atom().
%% Listeners and connections have an associated class.
%% When sending a message, if a previous connection to the same remote
%% and class exists, it will be reused.
%% When starting an outgoing connection, if a suitable listening transport
%% is found with the same class, some values from listener's metadata will
%% be copied to the new connection: user, idle_timeout, host, path, ws_proto,
%% refresh_fun, tcp_packet, tls_opts
-type class() :: term().
%% Recognized transport schemes
-type transport() :: udp | tcp | tls | sctp | ws | wss | http | https.
%% A protocol is a module implementing nkpacket_protocol behaviour
-type protocol() :: module().
%% An opened port (listener or connection)
-type nkport() :: #nkport{}.
%%
-type netspec() :: {protocol(), transport(), inet:ip_address(), inet:port_number()}.
-type cowboy_opts() ::
[
{max_empty_lines, non_neg_integer()} |
{max_header_name_length, non_neg_integer()} |
{max_header_value_length, non_neg_integer()} |
{max_headers, non_neg_integer()} |
{max_keepalive, non_neg_integer()} |
{max_request_line_length, non_neg_integer()} |
{onresponse, cowboy:onresponse_fun()}
].
-type http_proto() ::
{static,
nkpacket_cowboy_static:opts()} |
{dispatch,
#{
routes => cowboy_router:routes()
}} |
{custom,
#{
env => cowboy_middleware:env(),
middlewares => [module()]
}}.
%% Options for listeners
-type listener_opts() ::
#{
% Common options
class => class(), % Class (see above)
user => term(), % User metadata
parse_syntax => map(), % Allows to update the syntax
monitor => atom() | pid(), % Connection will monitor this
idle_timeout => integer(), % MSecs, default in config
refresh_fun => fun((nkport()) -> boolean()), % Will be called on timeout
valid_schemes => [nklib:scheme()], % Fail if not valid protocol (for URIs)
% UDP options
udp_starts_tcp => boolean(), % UDP starts TCP on the same port
udp_no_connections => boolean(), % Do not create connections
udp_stun_reply => boolean(), % Do STUNs replies
udp_stun_t1 => integer(), % msecs, default 500
% SCTP Opts
sctp_out_streams => integer(), % Number of out streams
sctp_in_streams => integer(), % Max input streams
% TCP/TLS/WS/WSS options
tcp_packet => 1 | 2 | 4 | raw, %
tcp_max_connections => integer(), % Default 1024
tcp_listeners => integer(), % Default 100
?TLS_TYPES,
% WS/WSS/HTTP/HTTPS options
host => string() | binary(), % Listen only on this host
path => string() | binary(), % Listen on this path and subpaths
get_headers => boolean() | [binary()], % Get all headers or some
cowboy_opts => cowboy_opts(),
% WS/WSS
ws_proto => string() | binary(), % Listen only on this protocol
% ws_opts => map(), % See nkpacket_connection_ws
% % (i.e. #{compress=>true})
% HTTP/HTTPS
http_proto => http_proto()
}.
%% Options for connections
-type connect_opts() ::
#{
% Common options
class => class(), % Class (see above)
user => term(), % User metadata
parse_syntax => map(), % Allows to update the syntax
monitor => atom() | pid(), % Connection will monitor this
connect_timeout => integer(), % MSecs, default in config
no_dns_cache => boolean(), % Avoid DNS cache
idle_timeout => integer(), % MSecs, default in config
refresh_fun => fun((nkport()) -> boolean()), % Will be called on timeout
base_nkport => boolean()| nkport(), % Select (or disables auto) base NkPort
valid_schemes => [nklib:scheme()], % Fail if not valid protocol (for URIs)
% TCP/TLS/WS/WSS options
tcp_packet => 1 | 2 | 4 | raw,
?TLS_TYPES,
% WS/WSS
host => string() | binary(), % Host header to use
path => string() | binary(), % Path to use
ws_proto => string() | binary() % Proto to use
}.
%% Options for sending
-type send_opts() ::
connect_opts() |
#{
% Specific options
force_new => boolean(), % Forces a new connection
udp_to_tcp => boolean(), % Change to TCP for large packets
udp_max_size => pos_integer(), % Used only for this sent request
pre_send_fun => pre_send_fun()
}.
%% Options for resolving
-type resolve_opts() ::
#{
resolve_type => listen | connect
} % used to process options
| listener_opts()
| send_opts().
%% Connection specification
-type user_connection() ::
nklib:user_uri() | connection().
%% Connection specification
-type connection() ::
nklib:uri() | raw_connection().
-type raw_connection() ::
{protocol(), transport(), inet:ip_address(), inet:port_number()}.
%% Sending remote specification options
-type send_spec() ::
user_connection() | {current, raw_connection()} | {connect, raw_connection()} |
pid() | nkport().
%% Incoming data to be parsed
-type incoming() ::
binary() |
{text, binary()} | {binary, binary()} | pong | {pong, binary} | %% For WS
close.
%% Outcoming data ready to be sent
-type outcoming() ::
iolist() | binary() |
cow_ws:frame(). % Only WS
%% See send
-type pre_send_fun() ::
fun((term(), nkport()) -> term()).
%% ===================================================================
%% Public functions
%% ===================================================================
%% doc Registers a new 'default' protocol
-spec register_protocol(nklib:scheme(), nkpacket:protocol()) ->
ok.
register_protocol(Scheme, Protocol) when is_atom(Scheme), is_atom(Protocol) ->
{module, _} = code:ensure_loaded(Protocol),
nklib_config:put(nkpacket, {protocol, Scheme}, Protocol).
%% doc Registers a new protocol for an specific class
-spec register_protocol(class(), nklib:scheme(), protocol()) ->
ok.
register_protocol(Class, Scheme, Protocol) when is_atom(Scheme), is_atom(Protocol) ->
{module, _} = code:ensure_loaded(Protocol),
nklib_config:put_domain(nkpacket, Class, {protocol, Scheme}, Protocol).
%% @doc
get_protocol(Scheme) ->
nkpacket_app:get({protocol, Scheme}).
%% @doc
get_protocol(Class, Scheme) ->
nkpacket_app:get_srv(Class, {protocol, Scheme}).
%% @doc Starts a new listening transport.
-spec start_listener(user_connection(), listener_opts()) ->
{ok, listen_id()} | {error, term()}.
start_listener(UserConn, Opts) ->
case get_listener(UserConn, Opts) of
{ok, Spec} ->
case nkpacket_sup:add_listener(Spec) of
{ok, Pid} ->
{registered_name, Id} = process_info(Pid, registered_name),
{ok, Id};
{error, Error} ->
{error, Error}
end;
{error, Error} ->
{error, Error}
end.
%% @doc Gets a listening supervisor specification
-spec get_listener(user_connection(), listener_opts()) ->
{ok, supervisor:child_spec()} | {error, term()}.
get_listener({Protocol, Transp, Ip, Port}, Opts) when is_map(Opts) ->
case nkpacket_util:parse_opts(Opts) of
{ok, Opts1} ->
Opts2 = case Transp==http orelse Transp==https of
true ->
WebProto = nkpacket_util:make_web_proto(Opts1),
Opts1#{http_proto=>WebProto};
_ ->
Opts1
end,
% We cannot yet generate id, port can be 0
NkPort = #nkport{
class = maps:get(class, Opts, none),
protocol = Protocol,
transp = Transp,
listen_ip = Ip,
listen_port = Port,
meta = Opts2
},
nkpacket_transport:get_listener(NkPort);
{error, Error} ->
{error, Error}
end;
get_listener(Uri, Opts) when is_map(Opts) ->
case resolve(Uri, Opts#{resolve_type=>listen}) of
{ok, [Conn], Opts1} ->
get_listener(Conn, Opts1);
{ok, _, _} ->
{error, invalid_uri};
{error, Error} ->
{error, Error}
end.
%% @doc Stops a locally started listener (only for standard supervisor)
-spec stop_listener(nkport()|listen_id()|pid()) ->
ok | {error, term()}.
stop_listener(Id) when is_pid(Id); is_atom(Id) ->
nklib_util:call(Id, nkpacket_stop, 30000);
stop_listener(#nkport{pid=Pid}) ->
stop_listener(Pid).
%% @doc Gets all registered transports
-spec get_all() ->
[listen_id()].
get_all() ->
[Id || {{Id, _Class}, _Pid} <- nklib_proc:values(nkpacket_listeners)].
%% @doc Gets all registered transports for a class.
-spec get_all(class()) ->
[pid()].
get_all(Class) ->
[Id || {{Id, C}, _Pid} <- nklib_proc:values(nkpacket_listeners), C==Class].
%% @doc Gets all classes having registered listeners
-spec get_class() ->
#{class() => [listen_id()]}.
get_class() ->
lists:foldl(
fun({{Id, Class}, _Pid}, Acc) ->
maps:put(Class, [Id|maps:get(Class, Acc, [])], Acc)
end,
#{},
nklib_proc:values(nkpacket_listeners)).
%% @doc Gets all classes having registered listeners
-spec get_class(class()) ->
[listen_id()].
get_class(Class) ->
All = get_class(),
maps:get(Class, All, []).
%% @doc Stops all locally started listeners (only for standard supervisor)
stop_all() ->
lists:foreach(
fun(Pid) -> stop_listener(Pid) end,
get_all()).
%% @doc Stops all locally started listeners for a class (only for standard supervisor)
stop_all(Class) ->
lists:foreach(
fun(Pid) -> stop_listener(Pid) end,
get_all(Class)).
%% @doc Gets the current nkport of a listener or connection
-spec get_nkport(listen_id()|pid()) ->
{ok, nkport()} | error.
get_nkport(#nkport{}=NkPort) ->
{ok, NkPort};
get_nkport(Id) when is_pid(Id); is_atom(Id) ->
apply_nkport(Id, fun get_nkport/1).
%% @doc Gets the current port number of a listener or connection
-spec get_local(listen_id()|pid()|nkport()) ->
{ok, netspec()} | error.
get_local(#nkport{protocol=Proto, transp=Transp, local_ip=Ip, local_port=Port}) ->
{ok, {Proto, Transp, Ip, Port}};
get_local(Id) when is_pid(Id); is_atom(Id) ->
apply_nkport(Id, fun get_local/1).
%% @doc Gets the current remote peer address and port
-spec get_remote(listen_id()|pid()|nkport()) ->
{ok, netspec()} | error.
get_remote(#nkport{protocol=Proto, transp=Transp, remote_ip=Ip, remote_port=Port}) ->
{ok, {Proto, Transp, Ip, Port}};
get_remote(Id) when is_pid(Id); is_atom(Id) ->
apply_nkport(Id, fun get_remote/1).
%% @doc Gets the current remote peer address and port
-spec get_remote_bin(listen_id()|pid()|nkport()) ->
{ok, binary()} | error.
get_remote_bin(Term) ->
case get_remote(Term) of
{ok, {_Proto, Transp, Ip, Port}} ->
{ok,
<<
(nklib_util:to_binary(Transp))/binary, ":",
(nklib_util:to_host(Ip))/binary, ":",
(nklib_util:to_binary(Port))/binary
>>};
{error, Error} ->
{error, Error}
end.
%% @doc Gets the user metadata of a listener or connection
-spec get_meta(listen_id()|pid()|nkport()) ->
{ok, map()} | error.
get_meta(#nkport{meta=Meta}) ->
{ok, Meta};
get_meta(Id) when is_pid(Id); is_atom(Id) ->
apply_nkport(Id, fun get_meta/1).
%% @doc Gets the user metadata of a listener or connection
-spec get_user(listen_id()|pid()|nkport()) ->
{ok, term(), term()} | error.
get_user(#nkport{class=Class, meta=Meta}) ->
{ok, Class, maps:get(user, Meta, undefined)};
get_user(Id) when is_pid(Id); is_atom(Id) ->
apply_nkport(Id, fun get_user/1).
%% @doc Gets the current pid() of a listener or connection
-spec pid(listen_id()|pid()|nkport()) ->
pid().
pid(Id) when is_atom(Id) ->
pid(whereis(Id));
pid(Pid) when is_pid(Pid) ->
Pid;
pid(#nkport{pid=Pid}) ->
Pid.
%% @doc Sends a message to a connection.
-spec send(send_spec() | [send_spec()], term()) ->
{ok, pid()} | {error, term()}.
send(SendSpec, Msg) ->
send(SendSpec, Msg, #{}).
%% @doc Sends a message to a connection
%% If a class is included, it will try to reuse any existing connection of the same class
%% (except if force_new option is set)
-spec send(send_spec() | [send_spec()], term(), send_opts()) ->
{ok, pid()} | {error, term()}.
send(SendSpec, Msg, Opts) when is_list(SendSpec), not is_integer(hd(SendSpec)) ->
case nkpacket_util:parse_opts(Opts) of
{ok, Opts1} ->
case nkpacket_transport:send(SendSpec, Msg, Opts1) of
{ok, {Pid, _Msg1}} -> {ok, Pid};
{error, Error} -> {error, Error}
end;
{error, Error} ->
{error, Error}
end;
send(SendSpec, Msg, Opts) ->
send([SendSpec], Msg, Opts).
%% @doc Forces a new outbound connection.
-spec connect(user_connection() | [connection()], connect_opts()) ->
{ok, pid()} | {error, term()}.
connect({_, _, _, _}=Conn, Opts) when is_map(Opts) ->
connect([Conn], Opts);
connect(Conns, Opts) when is_list(Conns), not is_integer(hd(Conns)), is_map(Opts) ->
case nkpacket_util:parse_opts(Opts) of
{ok, Opts1} ->
nkpacket_transport:connect(Conns, Opts1);
{error, Error} ->
{error, Error}
end;
connect(Uri, Opts) when is_map(Opts) ->
case resolve(Uri, Opts) of
{ok, Conns, Opts1} ->
connect(Conns, Opts1);
{error, Error} ->
{error, Error}
end.
%% @private Finds a listening transport of Proto.
-spec get_listening(protocol(), transport()) ->
[nkport()].
get_listening(Protocol, Transp) ->
get_listening(Protocol, Transp, #{}).
%% @private Finds a listening transport of Proto.
-spec get_listening(protocol(), transport(), #{class=>class(), ip=>4|6|tuple()}) ->
[nkport()].
get_listening(Protocol, Transp, Opts) ->
Class = maps:get(class, Opts, none),
Tag = case maps:get(ip, Opts, 4) of
4 -> nkpacket_listen4;
6 -> nkpacket_listen6;
Ip when is_tuple(Ip), size(Ip)==4 -> nkpacket_listen4;
Ip when is_tuple(Ip), size(Ip)==8 -> nkpacket_listen6
end,
[
NkPort ||
{NkPort, _Pid}
<- nklib_proc:values({Tag, Class, Protocol, Transp})
].
%% @doc Checks if an `uri()' refers to a local started transport.
%% For ws/wss, it does not check the path
-spec is_local(nklib:uri()) ->
boolean().
is_local(Uri) ->
is_local(Uri, #{}).
%% @doc Checks if an `uri()' refers to a local started transport.
%% For ws/wss, it does not check the path
-spec is_local(nklib:uri(), #{class=>class(), no_dns_cache=>boolean()}) ->
boolean().
is_local(#uri{}=Uri, Opts) ->
case resolve(Uri, Opts) of
{ok, [{Protocol, Transp, Ip, _Port}|_]=Conns, _Opts1} ->
List = get_listening(Protocol, Transp, Opts#{ip=>Ip}),
Listen = [
{Transp, LIp, LPort} ||
#nkport{local_ip=LIp, local_port=LPort} <- List
],
LocalIps = nkpacket_config_cache:local_ips(),
is_local(Listen, Conns, LocalIps);
_ ->
false
end.
%% @private
is_local(Listen, [{Protocol, Transp, Ip, 0}|Rest], LocalIps) ->
case nkpacket_transport:get_defport(Protocol, Transp) of
{ok, Port} ->
is_local(Listen, [{Protocol, Transp, Ip, Port}|Rest], LocalIps);
error ->
is_local(Listen, Rest, LocalIps)
end;
is_local(Listen, [{_Protocol, Transp, Ip, Port}|Rest], LocalIps) ->
case lists:member(Ip, LocalIps) of
true ->
case lists:member({Transp, Ip, Port}, Listen) of
true ->
true;
false ->
case
is_tuple(Ip) andalso size(Ip)==4 andalso
lists:member({Transp, {0,0,0,0}, Port}, Listen)
of
true ->
true;
false ->
case
is_tuple(Ip) andalso size(Ip)==8 andalso
lists:member({Transp, {0,0,0,0,0,0,0,0}, Port},
Listen)
of
true -> true;
false -> is_local(Listen, Rest, LocalIps)
end
end
end;
false ->
is_local(Listen, Rest, LocalIps)
end;
is_local(_, [], _) ->
false.
%% @doc Checks if an IP is local to this node.
-spec is_local_ip(inet:ip_address()) ->
boolean().
is_local_ip({0,0,0,0}) ->
true;
is_local_ip({0,0,0,0,0,0,0,0}) ->
true;
is_local_ip(Ip) ->
lists:member(Ip, nkpacket_config_cache:local_ips()).
%% ===================================================================
%% Internal
%% ===================================================================
%% @private
-spec resolve(nklib:user_uri()) ->
{ok, [raw_connection()], map()} |
{error, term()}.
resolve(Uri) ->
resolve(Uri, #{}).
%% @private
-spec resolve(nklib:user_uri(), resolve_opts()) ->
{ok, [raw_connection()], map()} |
{error, term()}.
resolve(#uri{scheme=Scheme}=Uri, Opts) ->
#uri{domain=Host, path=Path, ext_opts=UriOpts, ext_headers=Headers} = Uri,
UriOpts1 = [{nklib_parse:unquote(K), nklib_parse:unquote(V)} || {K, V} <- UriOpts],
UriOpts2 = case Host of
<<"0.0.0.0">> -> UriOpts1;
<<"0:0:0:0:0:0:0:0">> -> UriOpts1;
<<"::0">> -> UriOpts1;
<<"all">> -> UriOpts1;
_ -> [{host, Host}|UriOpts1] % Host to listen on for WS/HTTP
end,
UriOpts3 = case Path of
<<>> -> UriOpts2;
_ -> [{path, Path}|UriOpts2] % Path to listen on for WS/HTTP
end,
UriOpts4 = case Headers of
[] -> UriOpts3;
_ -> [{user, Headers}|UriOpts3]
end,
try
UriOpts5 = case nkpacket_util:parse_uri_opts(UriOpts4, Opts) of
{ok, ParsedUriOpts} -> ParsedUriOpts;
{error, Error1} -> throw(Error1)
end,
Opts1 = case nkpacket_util:parse_opts(Opts) of
{ok, CoreOpts} -> maps:merge(UriOpts5, CoreOpts);
{error, Error2} -> throw(Error2)
end,
Opts2 = case Opts1 of
#{valid_schemes:=ValidSchemes} ->
case lists:member(Scheme, ValidSchemes) of
true -> maps:remove(valid_schemes, Opts1);
false -> throw({invalid_scheme, Scheme})
end;
_ ->
Opts1
end,
Protocol = case Opts2 of
#{class:=Class} ->
nkpacket:get_protocol(Class, Scheme);
_ ->
nkpacket:get_protocol(Scheme)
end,
case nkpacket_dns:resolve(Uri, Opts2#{protocol=>Protocol}) of
{ok, Addrs} ->
Conns = [
{Protocol, Transp, Addr, Port}
|| {Transp, Addr, Port} <- Addrs
],
{ok, Conns, maps:remove(resolve_type, Opts2)};
{error, Error} ->
{error, Error}
end
catch
throw:Throw -> {error, Throw}
end;
resolve(Uri, Opts) ->
case nklib_parse:uris(Uri) of
[Parsed] ->
resolve(Parsed, Opts);
_ ->
{error, {invalid_uri, Uri}}
end.
%% @private
-spec multi_resolve(nklib:user_uri()|[nklib:user_uri()]) ->
{ok, [{[raw_connection()], map()}]} |
{error, term()}.
multi_resolve(Uri) ->
multi_resolve(Uri, #{}).
%% @private
-spec multi_resolve(nklib:user_uri()|[nklib:user_uri()], resolve_opts()) ->
{ok, [{[raw_connection()], map()}]} |
{error, term()}.
multi_resolve([], _Opts) ->
{ok, []};
multi_resolve(List, Opts) when is_list(List), not is_integer(hd(List)) ->
multi_resolve(List, Opts, []);
multi_resolve(Other, Opts) ->
multi_resolve([Other], Opts).
%% @private
multi_resolve([], _Opts, Acc) ->
{ok, lists:reverse(Acc)};
multi_resolve([#uri{}=Uri|Rest], Opts, Acc) ->
case resolve(Uri, Opts) of
{ok, Conns, Opts1} ->
multi_resolve(Rest, Opts, [{Conns, Opts1}|Acc]);
{error, Error} ->
{error, Error}
end;
multi_resolve([Uri|Rest], Opts, Acc) ->
case nklib_parse:uris(Uri) of
error ->
{error, {invalid_uri, Uri}};
Parsed ->
multi_resolve(Parsed++Rest, Opts, Acc)
end.
%% @private
-spec apply_nkport(listen_id()|pid(), fun((nkport()) -> {ok, term()})) ->
term() | error.
apply_nkport(Id, Fun) when is_pid(Id); is_atom(Id) ->
case catch gen_server:call(Id, {nkpacket_apply_nkport, Fun}, 180000) of
{'EXIT', _} -> error;
Other -> Other
end.