summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMicael Karlberg <bmk@erlang.org>2021-07-23 09:55:34 +0200
committerMicael Karlberg <bmk@erlang.org>2021-08-30 16:41:16 +0200
commitb0bea27e7b7d0f189f4015c68c8ff2c1586a909c (patch)
tree74e99303f525a979abc7c0f135e3728b963c7c6f
parent175fb04cf23713ba5fc82a0873d9fe018c113bfe (diff)
downloaderlang-b0bea27e7b7d0f189f4015c68c8ff2c1586a909c.tar.gz
[megaco|udp-transport] UDP transport can now use inet_backend option
The option 'inet_backend' is now handled to make it possible to specifically configure the inet-backend (inet or socket). OTP-17533
-rw-r--r--lib/megaco/src/udp/megaco_udp.erl44
-rw-r--r--lib/megaco/src/udp/megaco_udp.hrl5
-rw-r--r--lib/megaco/src/udp/megaco_udp_server.erl31
3 files changed, 61 insertions, 19 deletions
diff --git a/lib/megaco/src/udp/megaco_udp.erl b/lib/megaco/src/udp/megaco_udp.erl
index 01aaa90f6c..099f4b7455 100644
--- a/lib/megaco/src/udp/megaco_udp.erl
+++ b/lib/megaco/src/udp/megaco_udp.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 1999-2016. All Rights Reserved.
+%% Copyright Ericsson AB 1999-2021. 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.
@@ -114,9 +114,15 @@ open(SupPid, Options) ->
%%------------------------------------------------------
%% Setup the socket
- IpOpts = [binary, {reuseaddr, true}, {active, once} |
- UdpRec#megaco_udp.options],
-
+ IpOpts =
+ case UdpRec#megaco_udp.inet_backend of
+ default ->
+ [];
+ IB ->
+ [{inet_backend, IB}]
+ end ++
+ [binary, {reuseaddr, true}, {active, once} |
+ UdpRec#megaco_udp.options],
case (catch gen_udp:open(UdpRec#megaco_udp.port, IpOpts)) of
{ok, Socket} ->
?udp_debug(UdpRec, "udp open", []),
@@ -258,9 +264,9 @@ close(#send_handle{socket = Socket}) ->
close(Socket);
close(Socket) ->
?udp_debug({socket, Socket}, "udp close", []),
- case erlang:port_info(Socket, connected) of
- {connected, ControlPid} ->
- megaco_udp_server:stop(ControlPid);
+ case inet:info(Socket) of
+ #{owner := ControlPid} = _Info when is_pid(ControlPid) ->
+ (catch megaco_udp_server:stop(ControlPid));
undefined ->
{error, already_closed}
end.
@@ -294,8 +300,12 @@ parse_options([{Tag, Val} | T], UdpRec, Mand) ->
parse_options(T, UdpRec#megaco_udp{receive_handle = Val}, Mand2);
module when is_atom(Val) ->
parse_options(T, UdpRec#megaco_udp{module = Val}, Mand2);
- serialize when (Val =:= true) orelse (Val =:= false) ->
+ serialize when is_boolean(Val) ->
parse_options(T, UdpRec#megaco_udp{serialize = Val}, Mand2);
+ inet_backend when (Val =:= default) orelse
+ (Val =:= inet) orelse
+ (Val =:= socket) ->
+ parse_options(T, UdpRec#megaco_udp{inet_backend = Val}, Mand2);
Bad ->
{error, {bad_option, Bad}}
end;
@@ -321,5 +331,19 @@ incNumOutOctets(SH, NumOctets) ->
incCounter(Key, Inc) ->
ets:update_counter(megaco_udp_stats, Key, Inc).
-% incNumErrors(SH) ->
-% incCounter({SH, medGwyGatewayNumErrors}, 1).
+%% incNumErrors(SH) ->
+%% incCounter({SH, medGwyGatewayNumErrors}, 1).
+
+%%-----------------------------------------------------------------
+
+%% formated_timestamp() ->
+%% format_timestamp(os:timestamp()).
+
+%% format_timestamp(TS) ->
+%% megaco:format_timestamp(TS).
+
+%% d(F) ->
+%% d(F, []).
+
+%% d(F, A) ->
+%% io:format("*** [~s] ~p " ++ F ++ "~n", [formated_timestamp(), self() | A]).
diff --git a/lib/megaco/src/udp/megaco_udp.hrl b/lib/megaco/src/udp/megaco_udp.hrl
index b5422d28c9..2560b4ad96 100644
--- a/lib/megaco/src/udp/megaco_udp.hrl
+++ b/lib/megaco/src/udp/megaco_udp.hrl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2000-2016. All Rights Reserved.
+%% Copyright Ericsson AB 2000-2021. 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.
@@ -32,7 +32,8 @@
socket,
receive_handle,
module = megaco,
- serialize = false % false: Spawn a new process for each message
+ serialize = false, % false: Spawn a new process for each message
+ inet_backend = default
}).
diff --git a/lib/megaco/src/udp/megaco_udp_server.erl b/lib/megaco/src/udp/megaco_udp_server.erl
index 1df9a76671..5abb4165ae 100644
--- a/lib/megaco/src/udp/megaco_udp_server.erl
+++ b/lib/megaco/src/udp/megaco_udp_server.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 1999-2016. All Rights Reserved.
+%% Copyright Ericsson AB 1999-2021. 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.
@@ -29,9 +29,11 @@
-behaviour(gen_server).
+
%%-----------------------------------------------------------------
%% Include files
%%-----------------------------------------------------------------
+
-include_lib("megaco/src/udp/megaco_udp.hrl").
-include_lib("megaco/src/app/megaco_internal.hrl").
@@ -39,6 +41,7 @@
%%-----------------------------------------------------------------
%% External exports
%%-----------------------------------------------------------------
+
-export([
start_link/1,
stop/1,
@@ -46,9 +49,11 @@
upgrade_receive_handle/2
]).
+
%%-----------------------------------------------------------------
%% Internal exports
%%-----------------------------------------------------------------
+
-export([
init/1,
handle_call/3,
@@ -60,6 +65,7 @@
handle_received_message/5
]).
+
%%-----------------------------------------------------------------
%% External interface functions
%%-----------------------------------------------------------------
@@ -71,6 +77,7 @@
start_link(Arg) ->
gen_server:start_link(?MODULE, Arg, []).
+
%%-----------------------------------------------------------------
%% Func: stop/1
%% Description: Stops the process that keeps track of an UDP
@@ -83,6 +90,7 @@ stop(Pid) ->
upgrade_receive_handle(Pid, NewHandle) ->
call(Pid, {upgrade_receive_handle, NewHandle}).
+
%%-----------------------------------------------------------------
%% Internal interface functions
%%-----------------------------------------------------------------
@@ -98,6 +106,7 @@ init(Arg) ->
?udp_debug(Arg, "udp server starting", [self()]),
{ok, Arg}.
+
%%-----------------------------------------------------------------
%% Func: terminate/2
%% Description: Termination function for the generic server
@@ -106,6 +115,7 @@ terminate(Reason, State) ->
?udp_debug(State, "udp server terminating", [self(), Reason]),
ok.
+
%%-----------------------------------------------------------------
%% Func: handle_call/3
%% Description: Handling call messages (really just stop and garbage)
@@ -120,6 +130,7 @@ handle_call(Req, From, UdpRec) ->
"~n~p", [From, Req]),
{reply, {error, {invalid_request, Req}}, UdpRec}.
+
%%-----------------------------------------------------------------
%% Func: handle_cast/2
%% Description: Handling cast messages (really just stop and garbage)
@@ -132,12 +143,13 @@ handle_cast(Msg, UdpRec) ->
"~n~w", [Msg]),
{noreply, UdpRec}.
+
%%-----------------------------------------------------------------
%% Func: handle_info/2
%% Description: Handling non call/cast messages. Incomming messages
%% from the socket and exit codes.
%%-----------------------------------------------------------------
-handle_info({udp, _UdpId, Ip, Port, Msg},
+handle_info({udp, _Socket, Ip, Port, Msg},
#megaco_udp{serialize = false} = UdpRec) ->
#megaco_udp{socket = Socket, module = Mod, receive_handle = RH} = UdpRec,
SH = megaco_udp:create_send_handle(Socket, Ip, Port),
@@ -150,9 +162,9 @@ handle_info({udp, _UdpId, Ip, Port, Msg},
Sz ->
receive_message(Mod, RH, SH, Sz, Msg)
end,
- inet:setopts(Socket, [{active, once}]),
+ activate(Socket),
{noreply, UdpRec};
-handle_info({udp, _UdpId, Ip, Port, Msg},
+handle_info({udp, _Socket, Ip, Port, Msg},
#megaco_udp{serialize = true} = UdpRec) ->
#megaco_udp{socket = Socket, module = Mod, receive_handle = RH} = UdpRec,
SH = megaco_udp:create_send_handle(Socket, Ip, Port),
@@ -160,7 +172,7 @@ handle_info({udp, _UdpId, Ip, Port, Msg},
incNumInMessages(SH),
incNumInOctets(SH, MsgSize),
process_received_message(Mod, RH, SH, Msg),
- inet:setopts(Socket, [{active, once}]),
+ activate(Socket),
{noreply, UdpRec};
handle_info(Info, UdpRec) ->
warning_msg("received unexpected info: "
@@ -203,6 +215,11 @@ do_stop(#megaco_udp{socket = Socket}) ->
gen_udp:close(Socket).
+-compile({inline, [activate/1]}).
+activate(Socket) ->
+ inet:setopts(Socket, [{active, once}]).
+
+
%%-----------------------------------------------------------------
%% Func: incNumInMessages/1, incNumInOctets/2, incNumErrors/1
%% Description: SNMP counter increment functions
@@ -217,8 +234,8 @@ incNumInOctets(SH, NumOctets) ->
incCounter(Key, Inc) ->
ets:update_counter(megaco_udp_stats, Key, Inc).
-% incNumErrors(SH) ->
-% incCounter({SH, medGwyGatewayNumErrors}, 1).
+%% incNumErrors(SH) ->
+%% incCounter({SH, medGwyGatewayNumErrors}, 1).
%% info_msg(F, A) ->