-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathrabbit_net.erl
More file actions
362 lines (313 loc) · 13.7 KB
/
Copy pathrabbit_net.erl
File metadata and controls
362 lines (313 loc) · 13.7 KB
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
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%
-module(rabbit_net).
-include_lib("kernel/include/inet.hrl").
-include_lib("kernel/include/net_address.hrl").
-export([is_ssl/1, ssl_info/1, controlling_process/2, getstat/2,
recv/1, sync_recv/2, async_recv/3, getopts/2,
setopts/2, send/2, close/1, fast_close/1, sockname/1, peername/1,
peercert/1, connection_string/2, socket_ends/2, is_loopback/1,
tcp_host/1, unwrap_socket/1, maybe_get_proxy_socket/1,
hostname/0, getifaddrs/0, proxy_ssl_info/2,
dist_info/0]).
%%---------------------------------------------------------------------------
-export_type([socket/0, ip_port/0, hostname/0]).
-type stat_option() ::
'recv_cnt' | 'recv_max' | 'recv_avg' | 'recv_oct' | 'recv_dvi' |
'send_cnt' | 'send_max' | 'send_avg' | 'send_oct' | 'send_pend'.
-type ok_val_or_error(A) :: rabbit_types:ok_or_error2(A, any()).
-type ok_or_any_error() :: rabbit_types:ok_or_error(any()).
-type socket() :: port() | ssl:sslsocket().
-type opts() :: [{atom(), any()} |
{raw, non_neg_integer(), non_neg_integer(), binary()}].
-type hostname() :: inet:hostname().
-type ip_port() :: inet:port_number().
-type proxy_socket() :: {'rabbit_proxy_socket', ranch_transport:socket(), ranch_proxy_header:proxy_info()}.
% -type host_or_ip() :: binary() | inet:ip_address().
-spec is_ssl(socket()) -> boolean().
-spec ssl_info(socket()) -> 'nossl' | ok_val_or_error([{atom(), any()}]).
-spec proxy_ssl_info(socket(), proxy_socket() | 'undefined') -> 'nossl' | ok_val_or_error([{atom(), any()}]).
-spec controlling_process(socket(), pid()) -> ok_or_any_error().
-spec getstat(socket(), [stat_option()]) ->
ok_val_or_error([{stat_option(), integer()}]).
-spec recv(socket()) ->
{'data', [char()] | binary()} |
'closed' |
rabbit_types:error(any()) |
{'other', any()}.
-spec sync_recv(socket(), integer()) ->
rabbit_types:ok(binary()) |
rabbit_types:error(any()).
-spec async_recv(socket(), integer(), timeout()) ->
rabbit_types:ok(any()).
-spec getopts
(socket(),
[atom() |
{raw, non_neg_integer(), non_neg_integer(),
non_neg_integer() | binary()}]) ->
ok_val_or_error(opts()).
-spec setopts(socket(), opts()) -> ok_or_any_error().
-spec send(socket(), iodata()) -> ok_or_any_error().
-spec close(socket()) -> ok_or_any_error().
-spec fast_close(socket()) -> ok_or_any_error().
-spec sockname(socket()) ->
ok_val_or_error({inet:ip_address(), ip_port()}).
-spec peername(socket()) ->
ok_val_or_error({inet:ip_address(), ip_port()}).
-spec peercert(socket()) ->
'nossl' | ok_val_or_error(rabbit_cert_info:certificate()).
-spec connection_string(socket(), 'inbound' | 'outbound') ->
ok_val_or_error(string()).
% -spec socket_ends(socket() | ranch_proxy:proxy_socket() | ranch_proxy_ssl:ssl_socket(),
% 'inbound' | 'outbound') ->
% ok_val_or_error({host_or_ip(), ip_port(),
% host_or_ip(), ip_port()}).
-spec is_loopback(socket() | inet:ip_address()) -> boolean().
% -spec unwrap_socket(socket() | ranch_proxy:proxy_socket() | ranch_proxy_ssl:ssl_socket()) -> socket().
-dialyzer({nowarn_function, [socket_ends/2, unwrap_socket/1]}).
%%---------------------------------------------------------------------------
-define(SSL_CLOSE_TIMEOUT, 5000).
-define(IS_SSL(Sock), is_tuple(Sock)
andalso (element(1, Sock) =:= sslsocket)).
is_ssl(Sock) -> ?IS_SSL(Sock).
ssl_info(Sock) when ?IS_SSL(Sock) ->
ssl:connection_information(Sock);
ssl_info(_Sock) ->
nossl.
proxy_ssl_info(Sock, {rabbit_proxy_socket, _, ProxyInfo}) ->
ConnInfo = ranch_proxy_header:to_connection_info(ProxyInfo),
case lists:keymember(protocol, 1, ConnInfo) andalso
lists:keymember(selected_cipher_suite, 1, ConnInfo) of
true ->
{ok, ConnInfo};
false ->
ssl_info(Sock)
end;
proxy_ssl_info(Sock, _) ->
ssl_info(Sock).
controlling_process(Sock, Pid) when ?IS_SSL(Sock) ->
ssl:controlling_process(Sock, Pid);
controlling_process(Sock, Pid) when is_port(Sock) ->
gen_tcp:controlling_process(Sock, Pid).
getstat(Sock, Stats) when ?IS_SSL(Sock) ->
ssl:getstat(Sock, Stats);
getstat(Sock, Stats) when is_port(Sock) ->
inet:getstat(Sock, Stats);
%% Used by Proxy protocol support in plugins
getstat({rabbit_proxy_socket, Sock, _}, Stats) when ?IS_SSL(Sock) ->
ssl:getstat(Sock, Stats);
getstat({rabbit_proxy_socket, Sock, _}, Stats) when is_port(Sock) ->
inet:getstat(Sock, Stats).
recv(Sock) when ?IS_SSL(Sock) ->
recv(Sock, {ssl, ssl_closed, ssl_error});
recv(Sock) when is_port(Sock) ->
recv(Sock, {tcp, tcp_closed, tcp_error}).
recv(S, {DataTag, ClosedTag, ErrorTag}) ->
receive
{DataTag, S, Data} -> {data, Data};
{ClosedTag, S} -> closed;
{ErrorTag, S, Reason} -> {error, Reason};
Other -> {other, Other}
end.
sync_recv(Sock, Length) when ?IS_SSL(Sock) ->
ssl:recv(Sock, Length);
sync_recv(Sock, Length) ->
gen_tcp:recv(Sock, Length).
async_recv(Sock, Length, Timeout) when ?IS_SSL(Sock) ->
Pid = self(),
Ref = make_ref(),
spawn(fun () -> Pid ! {inet_async, Sock, Ref,
ssl:recv(Sock, Length, Timeout)}
end),
{ok, Ref};
async_recv(Sock, Length, infinity) when is_port(Sock) ->
prim_inet:async_recv(Sock, Length, -1);
async_recv(Sock, Length, Timeout) when is_port(Sock) ->
prim_inet:async_recv(Sock, Length, Timeout).
getopts(Sock, Options) when ?IS_SSL(Sock) ->
ssl:getopts(Sock, Options);
getopts(Sock, Options) when is_port(Sock) ->
inet:getopts(Sock, Options).
setopts(Sock, Options) when ?IS_SSL(Sock) ->
ssl:setopts(Sock, Options);
setopts(Sock, Options) when is_port(Sock) ->
inet:setopts(Sock, Options).
send(Sock, Data) when ?IS_SSL(Sock) -> ssl:send(Sock, Data);
send(Sock, Data) when is_port(Sock) -> gen_tcp:send(Sock, Data).
close(Sock) when ?IS_SSL(Sock) -> ssl:close(Sock);
close(Sock) when is_port(Sock) -> gen_tcp:close(Sock).
fast_close(Sock) when ?IS_SSL(Sock) ->
_ = ssl:close(Sock, ?SSL_CLOSE_TIMEOUT),
ok;
fast_close(Sock) when is_port(Sock) ->
catch port_close(Sock), ok.
sockname(Sock) when ?IS_SSL(Sock) -> ssl:sockname(Sock);
sockname(Sock) when is_port(Sock) -> inet:sockname(Sock).
peername(Sock) when ?IS_SSL(Sock) -> ssl:peername(Sock);
peername(Sock) when is_port(Sock) -> inet:peername(Sock).
peercert(Sock) when ?IS_SSL(Sock) -> ssl:peercert(Sock);
peercert(Sock) when is_port(Sock) -> nossl.
connection_string(Sock, Direction) ->
case socket_ends(Sock, Direction) of
{ok, {FromAddress, FromPort, ToAddress, ToPort}} ->
{ok, rabbit_misc:format(
"~ts:~tp -> ~ts:~tp",
[maybe_ntoab(FromAddress), FromPort,
maybe_ntoab(ToAddress), ToPort])};
Error ->
Error
end.
socket_ends(Sock, Direction) when ?IS_SSL(Sock);
is_port(Sock) ->
{From, To} = sock_funs(Direction),
case {From(Sock), To(Sock)} of
{{ok, {FromAddress, FromPort}}, {ok, {ToAddress, ToPort}}} ->
{ok, {rdns(FromAddress), FromPort,
rdns(ToAddress), ToPort}};
{{error, _Reason} = Error, _} ->
Error;
{_, {error, _Reason} = Error} ->
Error
end;
socket_ends({rabbit_proxy_socket, Sock, ProxyInfo}, Direction) ->
case ProxyInfo of
%% LOCAL header: we take the IP/ports from the socket.
#{command := local} ->
socket_ends(Sock, Direction);
%% PROXY header: use the IP/ports from the proxy header.
#{
src_address := FromAddress,
src_port := FromPort,
dest_address := ToAddress,
dest_port := ToPort
} ->
{ok, {rdns(FromAddress), FromPort,
rdns(ToAddress), ToPort}}
end.
maybe_ntoab(Addr) when is_tuple(Addr) -> rabbit_misc:ntoab(Addr);
maybe_ntoab(Host) -> Host.
tcp_host({0,0,0,0}) ->
hostname();
tcp_host({0,0,0,0,0,0,0,0}) ->
hostname();
tcp_host(IPAddress) ->
case inet:gethostbyaddr(IPAddress) of
{ok, #hostent{h_name = Name}} -> Name;
{error, _Reason} -> rabbit_misc:ntoa(IPAddress)
end.
hostname() ->
{ok, Hostname} = inet:gethostname(),
case inet:gethostbyname(Hostname) of
{ok, #hostent{h_name = Name}} -> Name;
{error, _Reason} -> Hostname
end.
format_nic_attribute({Key, undefined}) ->
{Key, undefined};
format_nic_attribute({Key = flags, List}) when is_list(List) ->
Val = string:join(lists:map(fun rabbit_data_coercion:to_list/1, List), ", "),
{Key, rabbit_data_coercion:to_binary(Val)};
format_nic_attribute({Key, Tuple}) when is_tuple(Tuple) and (Key =:= addr orelse
Key =:= broadaddr orelse
Key =:= netmask orelse
Key =:= dstaddr) ->
Val = inet_parse:ntoa(Tuple),
{Key, rabbit_data_coercion:to_binary(Val)};
format_nic_attribute({Key = hwaddr, List}) when is_list(List) ->
%% [140, 133, 144, 28, 241, 121] => 8C:85:90:1C:F1:79
Val = string:join(lists:map(fun(N) -> integer_to_list(N, 16) end, List), ":"),
{Key, rabbit_data_coercion:to_binary(Val)}.
getifaddrs() ->
{ok, AddrList} = inet:getifaddrs(),
Addrs0 = maps:from_list(AddrList),
maps:map(fun (_Key, Proplist) ->
lists:map(fun format_nic_attribute/1, Proplist)
end, Addrs0).
rdns(Addr) ->
case application:get_env(rabbit, reverse_dns_lookups) of
{ok, true} -> list_to_binary(tcp_host(Addr));
_ -> Addr
end.
sock_funs(inbound) -> {fun peername/1, fun sockname/1};
sock_funs(outbound) -> {fun sockname/1, fun peername/1}.
is_loopback(Sock) when is_port(Sock) ; ?IS_SSL(Sock) ->
case peername(Sock) of
{ok, {Addr, _Port}} -> is_loopback(Addr);
{error, _} -> false
end;
%% We could parse the results of inet:getifaddrs() instead. But that
%% would be more complex and less maybe Windows-compatible...
is_loopback({127,_,_,_}) -> true;
is_loopback({0,0,0,0,0,0,0,1}) -> true;
is_loopback({0,0,0,0,0,65535,AB,CD}) -> is_loopback(ipv4(AB, CD));
is_loopback(_) -> false.
ipv4(AB, CD) -> {AB bsr 8, AB band 255, CD bsr 8, CD band 255}.
unwrap_socket({rabbit_proxy_socket, Sock, _}) ->
Sock;
unwrap_socket(Sock) ->
Sock.
maybe_get_proxy_socket(Sock={rabbit_proxy_socket, _, _}) ->
Sock;
maybe_get_proxy_socket(_Sock) ->
undefined.
%% deps/prometheus/src/collectors/vm/prometheus_vm_dist_collector.erl
%% https://github.com/deadtrickster/prometheus.erl/blob/v4.8.2/src/collectors/vm/prometheus_vm_dist_collector.erl#L386-L450
dist_info() ->
{ok, NodesInfo} = net_kernel:nodes_info(),
TcpInetPorts = [P || P <- erlang:ports(),
erlang:port_info(P, name) =:= {name, "tcp_inet"}],
[dist_info(NodeInfo, TcpInetPorts) || NodeInfo <- NodesInfo].
dist_info({Node, Info}, TcpInetPorts) ->
DistPid = proplists:get_value(owner, Info),
case proplists:get_value(address, Info, #net_address{}) of
#net_address{address=undefined} ->
%% No stats available
{Node, DistPid, []};
#net_address{address=PeerAddr} ->
dist_info(Node, TcpInetPorts, DistPid, PeerAddr)
end.
dist_info(Node, TcpInetPorts, DistPid, PeerAddrArg) ->
case [P || P <- TcpInetPorts, inet:peername(P) =:= {ok, PeerAddrArg}] of
%% Note: sometimes the port closes before we get here and thus can't get stats
%% See rabbitmq/rabbitmq-server#5490
[] ->
{Node, DistPid, []};
[DistPort] ->
S = dist_port_stats(DistPort),
{Node, DistPid, S};
%% And sometimes there multiple ports, most likely right after a peer node
%% restart.
DistPorts when is_list(DistPorts) ->
ConnectedPorts = lists:filter(fun(Port) ->
case erlang:port_info(Port, connected) of
{connected, AssocPid} ->
erlang:is_process_alive(AssocPid);
undefined ->
false
end
end, DistPorts),
case ConnectedPorts of
[] ->
{Node, DistPid, []};
CPs when is_list(CPs) ->
S = dist_port_stats(hd(CPs)),
{Node, DistPid, S}
end
end.
dist_port_stats(DistPort) ->
case {socket_ends(DistPort, inbound), getstat(DistPort, [recv_oct, send_oct])} of
{{ok, {PeerAddr, PeerPort, SockAddr, SockPort}}, {ok, Stats}} ->
PeerAddrBin = rabbit_data_coercion:to_binary(maybe_ntoab(PeerAddr)),
SockAddrBin = rabbit_data_coercion:to_binary(maybe_ntoab(SockAddr)),
[{peer_addr, PeerAddrBin},
{peer_port, PeerPort},
{sock_addr, SockAddrBin},
{sock_port, SockPort},
{recv_bytes, rabbit_misc:pget(recv_oct, Stats)},
{send_bytes, rabbit_misc:pget(send_oct, Stats)}];
_ ->
[]
end.