ranch_tcp.erl (7781B)
1 %% Copyright (c) 2011-2018, Loïc Hoguin <essen@ninenines.eu> 2 %% 3 %% Permission to use, copy, modify, and/or distribute this software for any 4 %% purpose with or without fee is hereby granted, provided that the above 5 %% copyright notice and this permission notice appear in all copies. 6 %% 7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 15 -module(ranch_tcp). 16 -behaviour(ranch_transport). 17 18 -export([name/0]). 19 -export([secure/0]). 20 -export([messages/0]). 21 -export([listen/1]). 22 -export([disallowed_listen_options/0]). 23 -export([accept/2]). 24 -export([accept_ack/2]). 25 -export([handshake/3]). 26 -export([connect/3]). 27 -export([connect/4]). 28 -export([recv/3]). 29 -export([recv_proxy_header/2]). 30 -export([send/2]). 31 -export([sendfile/2]). 32 -export([sendfile/4]). 33 -export([sendfile/5]). 34 -export([setopts/2]). 35 -export([getopts/2]). 36 -export([getstat/1]). 37 -export([getstat/2]). 38 -export([controlling_process/2]). 39 -export([peername/1]). 40 -export([sockname/1]). 41 -export([shutdown/2]). 42 -export([close/1]). 43 44 -type opt() :: {backlog, non_neg_integer()} 45 | {buffer, non_neg_integer()} 46 | {delay_send, boolean()} 47 | {dontroute, boolean()} 48 | {exit_on_close, boolean()} 49 | {fd, non_neg_integer()} 50 | {high_msgq_watermark, non_neg_integer()} 51 | {high_watermark, non_neg_integer()} 52 | inet 53 | inet6 54 | {ip, inet:ip_address()} 55 | {ipv6_v6only, boolean()} 56 | {keepalive, boolean()} 57 | {linger, {boolean(), non_neg_integer()}} 58 | {low_msgq_watermark, non_neg_integer()} 59 | {low_watermark, non_neg_integer()} 60 | {nodelay, boolean()} 61 | {port, inet:port_number()} 62 | {priority, integer()} 63 | {raw, non_neg_integer(), non_neg_integer(), binary()} 64 | {recbuf, non_neg_integer()} 65 | {send_timeout, timeout()} 66 | {send_timeout_close, boolean()} 67 | {sndbuf, non_neg_integer()} 68 | {tos, integer()}. 69 -export_type([opt/0]). 70 71 -type opts() :: [opt()]. 72 -export_type([opts/0]). 73 74 name() -> tcp. 75 76 -spec secure() -> boolean(). 77 secure() -> 78 false. 79 80 messages() -> {tcp, tcp_closed, tcp_error}. 81 82 -spec listen(opts()) -> {ok, inet:socket()} | {error, atom()}. 83 listen(Opts) -> 84 Opts2 = ranch:set_option_default(Opts, backlog, 1024), 85 Opts3 = ranch:set_option_default(Opts2, nodelay, true), 86 Opts4 = ranch:set_option_default(Opts3, send_timeout, 30000), 87 Opts5 = ranch:set_option_default(Opts4, send_timeout_close, true), 88 %% We set the port to 0 because it is given in the Opts directly. 89 %% The port in the options takes precedence over the one in the 90 %% first argument. 91 gen_tcp:listen(0, ranch:filter_options(Opts5, disallowed_listen_options(), 92 [binary, {active, false}, {packet, raw}, {reuseaddr, true}])). 93 94 %% 'binary' and 'list' are disallowed but they are handled 95 %% specifically as they do not have 2-tuple equivalents. 96 disallowed_listen_options() -> 97 [active, header, mode, packet, packet_size, line_delimiter, reuseaddr]. 98 99 -spec accept(inet:socket(), timeout()) 100 -> {ok, inet:socket()} | {error, closed | timeout | atom()}. 101 accept(LSocket, Timeout) -> 102 gen_tcp:accept(LSocket, Timeout). 103 104 -spec accept_ack(inet:socket(), timeout()) -> ok. 105 accept_ack(CSocket, Timeout) -> 106 {ok, _} = handshake(CSocket, [], Timeout), 107 ok. 108 109 -spec handshake(inet:socket(), opts(), timeout()) -> {ok, inet:socket()}. 110 handshake(CSocket, _, _) -> 111 {ok, CSocket}. 112 113 %% @todo Probably filter Opts? 114 -spec connect(inet:ip_address() | inet:hostname(), 115 inet:port_number(), any()) 116 -> {ok, inet:socket()} | {error, atom()}. 117 connect(Host, Port, Opts) when is_integer(Port) -> 118 gen_tcp:connect(Host, Port, 119 Opts ++ [binary, {active, false}, {packet, raw}]). 120 121 %% @todo Probably filter Opts? 122 -spec connect(inet:ip_address() | inet:hostname(), 123 inet:port_number(), any(), timeout()) 124 -> {ok, inet:socket()} | {error, atom()}. 125 connect(Host, Port, Opts, Timeout) when is_integer(Port) -> 126 gen_tcp:connect(Host, Port, 127 Opts ++ [binary, {active, false}, {packet, raw}], 128 Timeout). 129 130 -spec recv(inet:socket(), non_neg_integer(), timeout()) 131 -> {ok, any()} | {error, closed | atom()}. 132 recv(Socket, Length, Timeout) -> 133 gen_tcp:recv(Socket, Length, Timeout). 134 135 -spec recv_proxy_header(inet:socket(), timeout()) 136 -> {ok, ranch_proxy_header:proxy_info()} 137 | {error, closed | atom()} 138 | {error, protocol_error, atom()}. 139 recv_proxy_header(Socket, Timeout) -> 140 case recv(Socket, 0, Timeout) of 141 {ok, Data} -> 142 case ranch_proxy_header:parse(Data) of 143 {ok, ProxyInfo, <<>>} -> 144 {ok, ProxyInfo}; 145 {ok, ProxyInfo, Rest} -> 146 case gen_tcp:unrecv(Socket, Rest) of 147 ok -> 148 {ok, ProxyInfo}; 149 Error -> 150 Error 151 end; 152 {error, HumanReadable} -> 153 {error, protocol_error, HumanReadable} 154 end; 155 Error -> 156 Error 157 end. 158 159 -spec send(inet:socket(), iodata()) -> ok | {error, atom()}. 160 send(Socket, Packet) -> 161 gen_tcp:send(Socket, Packet). 162 163 -spec sendfile(inet:socket(), file:name_all() | file:fd()) 164 -> {ok, non_neg_integer()} | {error, atom()}. 165 sendfile(Socket, Filename) -> 166 sendfile(Socket, Filename, 0, 0, []). 167 168 -spec sendfile(inet:socket(), file:name_all() | file:fd(), non_neg_integer(), 169 non_neg_integer()) 170 -> {ok, non_neg_integer()} | {error, atom()}. 171 sendfile(Socket, File, Offset, Bytes) -> 172 sendfile(Socket, File, Offset, Bytes, []). 173 174 -spec sendfile(inet:socket(), file:name_all() | file:fd(), non_neg_integer(), 175 non_neg_integer(), [{chunk_size, non_neg_integer()}]) 176 -> {ok, non_neg_integer()} | {error, atom()}. 177 sendfile(Socket, Filename, Offset, Bytes, Opts) 178 when is_list(Filename) orelse is_atom(Filename) 179 orelse is_binary(Filename) -> 180 case file:open(Filename, [read, raw, binary]) of 181 {ok, RawFile} -> 182 try sendfile(Socket, RawFile, Offset, Bytes, Opts) of 183 Result -> Result 184 after 185 ok = file:close(RawFile) 186 end; 187 {error, _} = Error -> 188 Error 189 end; 190 sendfile(Socket, RawFile, Offset, Bytes, Opts) -> 191 Opts2 = case Opts of 192 [] -> [{chunk_size, 16#1FFF}]; 193 _ -> Opts 194 end, 195 try file:sendfile(RawFile, Socket, Offset, Bytes, Opts2) of 196 Result -> Result 197 catch 198 error:{badmatch, {error, enotconn}} -> 199 %% file:sendfile/5 might fail by throwing a 200 %% {badmatch, {error, enotconn}}. This is because its 201 %% implementation fails with a badmatch in 202 %% prim_file:sendfile/10 if the socket is not connected. 203 {error, closed} 204 end. 205 206 %% @todo Probably filter Opts? 207 -spec setopts(inet:socket(), list()) -> ok | {error, atom()}. 208 setopts(Socket, Opts) -> 209 inet:setopts(Socket, Opts). 210 211 -spec getopts(inet:socket(), [atom()]) -> {ok, list()} | {error, atom()}. 212 getopts(Socket, Opts) -> 213 inet:getopts(Socket, Opts). 214 215 -spec getstat(inet:socket()) -> {ok, list()} | {error, atom()}. 216 getstat(Socket) -> 217 inet:getstat(Socket). 218 219 -spec getstat(inet:socket(), [atom()]) -> {ok, list()} | {error, atom()}. 220 getstat(Socket, OptionNames) -> 221 inet:getstat(Socket, OptionNames). 222 223 -spec controlling_process(inet:socket(), pid()) 224 -> ok | {error, closed | not_owner | atom()}. 225 controlling_process(Socket, Pid) -> 226 gen_tcp:controlling_process(Socket, Pid). 227 228 -spec peername(inet:socket()) 229 -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}. 230 peername(Socket) -> 231 inet:peername(Socket). 232 233 -spec sockname(inet:socket()) 234 -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}. 235 sockname(Socket) -> 236 inet:sockname(Socket). 237 238 -spec shutdown(inet:socket(), read | write | read_write) 239 -> ok | {error, atom()}. 240 shutdown(Socket, How) -> 241 gen_tcp:shutdown(Socket, How). 242 243 -spec close(inet:socket()) -> ok. 244 close(Socket) -> 245 gen_tcp:close(Socket).