diff options
author | Micael Karlberg <bmk@erlang.org> | 2021-07-27 18:13:09 +0200 |
---|---|---|
committer | Micael Karlberg <bmk@erlang.org> | 2021-08-30 16:41:16 +0200 |
commit | 09692df5b4e1cd627f5c51c61fc2cf88eb2edfe8 (patch) | |
tree | 0f6157cf9156b323d3aa329b955fcc992cd77067 | |
parent | 84a96fd56691a2aae1e2b5eac2b5cce3b7f34a4f (diff) | |
download | erlang-09692df5b4e1cd627f5c51c61fc2cf88eb2edfe8.tar.gz |
[megaco|tcp-transport] TCP 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).
Also found what must be a very old bug in the tcp-transport
handling of transports (linkdb). It was supposed to be a list
but was initially just the atom 'undefined'. Result improper
list. Which may not have been an issue in practice if you only
used one transport...
OTP-17533
-rw-r--r-- | lib/megaco/src/tcp/megaco_tcp.erl | 93 | ||||
-rw-r--r-- | lib/megaco/src/tcp/megaco_tcp.hrl | 9 | ||||
-rw-r--r-- | lib/megaco/src/tcp/megaco_tcp_accept.erl | 22 |
3 files changed, 100 insertions, 24 deletions
diff --git a/lib/megaco/src/tcp/megaco_tcp.erl b/lib/megaco/src/tcp/megaco_tcp.erl index 1735439501..460e679efc 100644 --- a/lib/megaco/src/tcp/megaco_tcp.erl +++ b/lib/megaco/src/tcp/megaco_tcp.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. @@ -66,6 +66,8 @@ reset_stats/0, reset_stats/1 ]). +%% -export([tcp_sockets/0]). + %%----------------------------------------------------------------- %% Internal exports @@ -177,11 +179,18 @@ connect(SupPid, Parameters) -> ?d1("connect -> options parsed: " "~n Rec: ~p", [Rec]), - #megaco_tcp{host = Host, - port = Port, - options = Options} = Rec, + #megaco_tcp{host = Host, + port = Port, + options = Options, + inet_backend = IB} = Rec, - IpOpt = [binary, {packet, tpkt}, {active, once} | Options], + IpOpt = + case IB of + default -> + []; + _ -> + [{inet_backend, IB}] + end ++ [binary, {packet, tpkt}, {active, once} | Options], %%------------------------------------------------------ %% Connect the other side @@ -376,7 +385,7 @@ create_snmp_counters(Socket, [Counter|Counters]) -> %%----------------------------------------------------------------- init({SupPid, _}) -> process_flag(trap_exit, true), - {ok, #state{supervisor_pid = SupPid}}. + {ok, #state{supervisor_pid = SupPid, linkdb = []}}. %%----------------------------------------------------------------- %% Func: terminate/1 @@ -409,7 +418,11 @@ start_tcp_listener(P, State) -> {error, Reason} -> ?d1("start_tcp_listener -> setup failed" "~n Reason: ~p", [Reason]), - {reply, {error, {could_not_start_listener, Reason}}, State} + DB = State#state.linkdb, + DBStatus = [{LPid, {LRec, LSock}, inet:info(LSock)} || + {LPid, {LRec, LSock}} <- DB], + Reply = {error, {could_not_start_listener, Reason, DBStatus}}, + {reply, Reply, State} end. @@ -480,10 +493,18 @@ setup(SupPid, Options) -> %%------------------------------------------------------ %% Setup the listen socket - IpOpts = [binary, {packet, tpkt}, {active, once}, - {reuseaddr, true} | TcpRec#megaco_tcp.options], - case catch gen_tcp:listen(TcpRec#megaco_tcp.port, IpOpts) of - {ok, Listen} -> + IpOpts = + case TcpRec#megaco_tcp.inet_backend of + default -> + []; + IB -> + [{inet_backend, IB}] + end ++ + [binary, {packet, tpkt}, {active, once}, + {reuseaddr, true} | TcpRec#megaco_tcp.options], + Port = TcpRec#megaco_tcp.port, + case catch gen_tcp:listen(Port, IpOpts) of + {ok, LSock} -> ?d1("setup -> listen ok" "~n Listen: ~p", [Listen]), @@ -491,14 +512,13 @@ setup(SupPid, Options) -> %%----------------------------------------------- %% Startup the accept process that will wait for %% connect attempts - case start_accept(SupPid, TcpRec, Listen) of + case start_accept(SupPid, TcpRec, LSock) of {ok, Pid} -> - ?d1("setup -> accept process started" "~n Pid: ~p", [Pid]), - ?tcp_debug(TcpRec, "tcp listen setup", []), - {ok, Pid, {TcpRec, Listen}}; + {ok, Pid, {TcpRec, LSock}}; + {error, _Reason} = Error -> ?d1("setup -> failed starting accept process" "~n Error: ~p", [Error]), @@ -506,16 +526,17 @@ setup(SupPid, Options) -> [Error]), Error end; + {error, Reason} -> ?d1("setup -> listen failed" "~n Reason: ~p", [Reason]), - Error = {error, {gen_tcp_listen, Reason}}, + Error = {error, {gen_tcp_listen, Reason, [Port, IpOpts]}}, ?tcp_debug(TcpRec, "tcp listen setup failed", [Error]), Error; {'EXIT', _Reason} = Exit -> ?d1("setup -> listen exited" "~n Exit: ~p", [Exit]), - Error = {error, {gen_tcp_listen, Exit}}, + Error = {error, {gen_tcp_listen, Exit, [Port, IpOpts]}}, ?tcp_debug(TcpRec, "tcp listen setup failed", [Error]), Error end; @@ -642,6 +663,10 @@ parse_options([{Tag, Val} | T], TcpRec, Mand) -> parse_options(T, TcpRec#megaco_tcp{module = Val}, Mand2); serialize when (Val =:= true) orelse (Val =:= false) -> parse_options(T, TcpRec#megaco_tcp{serialize = Val}, Mand2); + inet_backend when (Val =:= default) orelse + (Val =:= inet) orelse + (Val =:= socket) -> + parse_options(T, TcpRec#megaco_tcp{inet_backend = Val}, Mand2); Bad -> ?d1("parse_options -> bad option: " "~n Tag: ~p", [Tag]), @@ -691,3 +716,37 @@ warning_msg(F, A) -> call(Pid, Req) -> gen_server:call(Pid, Req, infinity). + + +%%----------------------------------------------------------------- + +%% formated_timestamp() -> +%% format_timestamp(os:timestamp()). + +%% format_timestamp(TS) -> +%% megaco:format_timestamp(TS). + +%% d(F) -> +%% d(F, []). + +%% d(F, A) -> +%% io:format("*** [~s] ~p ~w " ++ F ++ "~n", +%% [formated_timestamp(), self(), ?MODULE | A]). + + +%%----------------------------------------------------------------- + +%% tcp_sockets() -> +%% port_list("tcp_inet") ++ gen_tcp_socket:which_sockets(). + + +%% %% Return all ports having the name 'Name' +%% port_list(Name) -> +%% lists:filter( +%% fun(Port) -> +%% case erlang:port_info(Port, name) of +%% {name, Name} -> true; +%% _ -> false +%% end +%% end, erlang:ports()). + diff --git a/lib/megaco/src/tcp/megaco_tcp.hrl b/lib/megaco/src/tcp/megaco_tcp.hrl index 0c01f36b55..6d138eb2eb 100644 --- a/lib/megaco/src/tcp/megaco_tcp.hrl +++ b/lib/megaco/src/tcp/megaco_tcp.hrl @@ -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. @@ -34,12 +34,13 @@ proxy_pid, 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 }). --define(GC_MSG_LIMIT,1000). --define(HEAP_SIZE(S),5000 + 2*(S)). +-define(GC_MSG_LIMIT, 1000). +-define(HEAP_SIZE(S), 5000 + 2*(S)). %%---------------------------------------------------------------------- diff --git a/lib/megaco/src/tcp/megaco_tcp_accept.erl b/lib/megaco/src/tcp/megaco_tcp_accept.erl index 5fcff200aa..da163dcc10 100644 --- a/lib/megaco/src/tcp/megaco_tcp_accept.erl +++ b/lib/megaco/src/tcp/megaco_tcp_accept.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. @@ -81,8 +81,7 @@ do_accept(Tcp, Sup, Fd) -> {ok, S} -> ?d1("do_accept -> accepted: " "~n S: ~p", [S]), - case megaco_tcp:start_connection(Sup, - Tcp#megaco_tcp{socket = S}) of + case megaco_tcp:start_connection(Sup, Tcp#megaco_tcp{socket = S}) of {ok, Pid} -> ?d1("do_accept -> connection started" "~n Pid: ~p", [Pid]), @@ -122,3 +121,20 @@ tcp_clear(Socket) -> after 0 -> ok end. + + + +%%----------------------------------------------------------------- + +%% formated_timestamp() -> +%% format_timestamp(os:timestamp()). + +%% format_timestamp(TS) -> +%% megaco:format_timestamp(TS). + +%% d(F) -> +%% d(F, []). + +%% d(F, A) -> +%% io:format("*** [~s] ~p ~w " ++ F ++ "~n", +%% [formated_timestamp(), self(), ?MODULE | A]). |