36
36
Req # radius_request .cmd == 'coareq' orelse
37
37
Req # radius_request .cmd == 'discreq' )).
38
38
39
- -type nas_address () :: {string () | binary () | inet :ip_address (),
40
- eradius_server :port_number (),
39
+ -type nas_address () :: {string () | binary () | inet :ip_address (),
40
+ eradius_server :port_number (),
41
41
eradius_lib :secret ()}.
42
42
-type options () :: [{retries , pos_integer ()} |
43
43
{timeout , timeout ()} |
@@ -63,10 +63,10 @@ send_request(NAS, Request) ->
63
63
% If no answer is received within the specified timeout, the request will be sent again.
64
64
-spec send_request (nas_address (), # radius_request {}, options ()) ->
65
65
{ok , binary (), eradius_lib :authenticator ()} | {error , 'timeout' | 'socket_down' }.
66
- send_request ({Host , Port , Secret }, Request , Options )
66
+ send_request ({Host , Port , Secret }, Request , Options )
67
67
when ? GOOD_CMD (Request ) andalso is_binary (Host ) ->
68
68
send_request ({erlang :binary_to_list (Host ), Port , Secret }, Request , Options );
69
- send_request ({Host , Port , Secret }, Request , Options )
69
+ send_request ({Host , Port , Secret }, Request , Options )
70
70
when ? GOOD_CMD (Request ) andalso is_list (Host ) ->
71
71
IP = get_ip (Host ),
72
72
send_request ({IP , Port , Secret }, Request , Options );
@@ -335,10 +335,19 @@ init([]) ->
335
335
Else -> Else
336
336
end .
337
337
338
+ % % @private
339
+ inet_family_based_on_peer (_PeerSocket = {{_ , _ , _ , _ }, _port }) ->
340
+ [inet ];
341
+ inet_family_based_on_peer (_PeerSocket = {{_ , _ , _ , _ , _ , _ , _ , _ }, _port }) ->
342
+ [inet6 ];
343
+ inet_family_based_on_peer (_PeerSocket ) ->
344
+ [].
345
+
338
346
% % @private
339
347
handle_call ({wanna_send , Peer = {_PeerName , PeerSocket }, _MetricsInfo }, _From , State ) ->
340
348
{PortIdx , ReqId , NewIdCounters } = next_port_and_req_id (PeerSocket , State # state .no_ports , State # state .idcounters ),
341
- {SocketProcess , NewSockets } = find_socket_process (PortIdx , State # state .sockets , State # state .socket_ip , State # state .sup ),
349
+ InetFamily = inet_family_based_on_peer (PeerSocket ),
350
+ {SocketProcess , NewSockets } = find_socket_process (PortIdx , State # state .sockets , State # state .socket_ip , InetFamily , State # state .sup ),
342
351
IsCreated = lists :member (Peer , State # state .clients ),
343
352
NewState = case IsCreated of
344
353
false ->
@@ -512,11 +521,11 @@ next_port_and_req_id(Peer, NumberOfPorts, Counters) ->
512
521
NewCounters = Counters #{Peer => {NextPortIdx , NextReqId }},
513
522
{NextPortIdx , NextReqId , NewCounters }.
514
523
515
- find_socket_process (PortIdx , Sockets , SocketIP , Sup ) ->
524
+ find_socket_process (PortIdx , Sockets , SocketIP , Options , Sup ) ->
516
525
case array :get (PortIdx , Sockets ) of
517
526
undefined ->
518
527
Res = supervisor :start_child (Sup , {PortIdx ,
519
- {eradius_client_socket , start , [SocketIP , self (), PortIdx ]},
528
+ {eradius_client_socket , start , [SocketIP , self (), PortIdx , Options ]},
520
529
transient , brutal_kill , worker , [eradius_client_socket ]}),
521
530
Pid = case Res of
522
531
{ok , P } -> P ;
@@ -538,7 +547,7 @@ parse_ip(Address) when is_list(Address) ->
538
547
inet_parse :address (Address );
539
548
parse_ip (T = {_ , _ , _ , _ }) ->
540
549
{ok , T };
541
- parse_ip (T = {_ , _ , _ , _ , _ , _ }) ->
550
+ parse_ip (T = {_ , _ , _ , _ , _ , _ , _ , _ }) ->
542
551
{ok , T }.
543
552
544
553
init_server_status_metrics () ->
@@ -689,5 +698,14 @@ get_ip(Host) ->
689
698
{ok , # hostent {h_addrtype = inet , h_addr_list = [_ | _ ] = IPs }} ->
690
699
Index = rand :uniform (length (IPs )),
691
700
lists :nth (Index , IPs );
692
- _ -> error (badarg )
701
+ _ ->
702
+ case inet :gethostbyname (Host , inet6 ) of
703
+ {ok , # hostent {h_addrtype = inet6 , h_addr_list = [IP ]}} ->
704
+ IP ;
705
+ {ok , # hostent {h_addrtype = inet6 , h_addr_list = [_ | _ ] = IPs }} ->
706
+ Index = rand :uniform (length (IPs )),
707
+ lists :nth (Index , IPs );
708
+ _Err ->
709
+ error (badarg )
710
+ end
693
711
end .
0 commit comments