1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2017 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
|
17 |
|
-module(mod_global_distrib_transport). |
18 |
|
-author('konrad.zemek@erlang-solutions.com'). |
19 |
|
|
20 |
|
-record(?MODULE, { |
21 |
|
transport :: fast_tls | gen_tcp, |
22 |
|
socket :: fast_tls:tls_socket() | gen_tcp:socket() |
23 |
|
}). |
24 |
|
|
25 |
|
-type t() :: #?MODULE{}. |
26 |
|
|
27 |
|
-export([wrap/2, wrap/3, setopts/2, recv_data/2, close/1, send/2, peername/1]). |
28 |
|
-export_type([t/0]). |
29 |
|
|
30 |
|
%%-------------------------------------------------------------------- |
31 |
|
%% API |
32 |
|
%%-------------------------------------------------------------------- |
33 |
|
|
34 |
|
-spec wrap(gen_tcp:socket(), #{tls := mongoose_tls:options()}) -> {ok, t()} | {error, any()}. |
35 |
|
wrap(Socket, ConnOpts) -> |
36 |
697 |
wrap(Socket, ConnOpts, #{}). |
37 |
|
|
38 |
|
-spec wrap(gen_tcp:socket(), #{tls := mongoose_tls:options()}, ExtraOpts :: map()) -> |
39 |
|
{ok, t()} | {error, any()}. |
40 |
|
wrap(Socket, #{tls := Opts}, ExtraOpts) -> |
41 |
1373 |
PreparedOpts = mongoose_tls:prepare_options(fast_tls, maps:merge(Opts, ExtraOpts)), |
42 |
1373 |
case fast_tls:tcp_to_tls(Socket, PreparedOpts) of |
43 |
1373 |
{ok, TLSSocket} -> {ok, #?MODULE{transport = fast_tls, socket = TLSSocket}}; |
44 |
:-( |
Error -> Error |
45 |
|
end; |
46 |
|
wrap(Socket, #{}, _ExtraOpts) -> |
47 |
:-( |
{ok, #?MODULE{transport = gen_tcp, socket = Socket}}. |
48 |
|
|
49 |
|
-spec setopts(t(), Opts :: proplists:proplist()) -> ok | {error, term()}. |
50 |
|
setopts(#?MODULE{transport = gen_tcp, socket = Socket}, Opts) -> |
51 |
:-( |
inet:setopts(Socket, Opts); |
52 |
|
setopts(#?MODULE{transport = fast_tls, socket = Socket}, Opts) -> |
53 |
5968 |
fast_tls:setopts(Socket, Opts). |
54 |
|
|
55 |
|
-spec recv_data(t(), Data :: binary()) -> {ok, binary()} | {error, any()}. |
56 |
|
recv_data(#?MODULE{transport = gen_tcp}, Data) -> |
57 |
:-( |
{ok, Data}; |
58 |
|
recv_data(#?MODULE{transport = fast_tls, socket = Socket}, Data) -> |
59 |
4595 |
fast_tls:recv_data(Socket, Data). |
60 |
|
|
61 |
|
-spec close(t()) -> ok | {error, any()}. |
62 |
|
close(#?MODULE{transport = gen_tcp, socket = Socket}) -> |
63 |
:-( |
gen_tcp:close(Socket); |
64 |
|
close(#?MODULE{transport = fast_tls, socket = Socket}) -> |
65 |
1373 |
fast_tls:close(Socket). |
66 |
|
|
67 |
|
-spec send(t(), Data :: binary()) -> ok | {error, any()}. |
68 |
|
send(#?MODULE{transport = gen_tcp, socket = Socket}, Data) -> |
69 |
:-( |
gen_tcp:send(Socket, Data); |
70 |
|
send(#?MODULE{transport = fast_tls, socket = Socket}, Data) -> |
71 |
1017 |
fast_tls:send(Socket, Data). |
72 |
|
|
73 |
|
-spec peername(t()) -> {inet:ip_address(), inet:port_number()} | unknown. |
74 |
|
peername(#?MODULE{transport = gen_tcp, socket = Socket}) -> |
75 |
:-( |
normalize_peername(inet:peername(Socket)); |
76 |
|
peername(#?MODULE{transport = fast_tls, socket = Socket}) -> |
77 |
1373 |
normalize_peername(fast_tls:peername(Socket)). |
78 |
|
|
79 |
|
-spec normalize_peername({ok, {inet:ip_address(), inet:port_number()}} | any()) -> |
80 |
|
{inet:ip_address(), inet:port_number()} | unknown. |
81 |
1352 |
normalize_peername({ok, {IP, Port}}) when is_tuple(IP), is_integer(Port) -> {IP, Port}; |
82 |
21 |
normalize_peername(_Other) -> unknown. |
83 |
|
|