cowboy_ssl_transport.erl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.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. %% @doc SSL transport API.
  15. %%
  16. %% Wrapper around <em>ssl</em> implementing the Cowboy transport API.
  17. %%
  18. %% This transport requires the <em>crypto</em>, <em>public_key</em>
  19. %% and <em>ssl</em> applications to be started. If they aren't started,
  20. %% it will try to start them itself before opening a port to listen.
  21. %% Applications aren't stopped when the listening socket is closed, though.
  22. %%
  23. %% @see ssl
  24. -module(cowboy_ssl_transport).
  25. -export([name/0, messages/0, listen/1, accept/2, recv/3, send/2, setopts/2,
  26. controlling_process/2, peername/1, close/1]).
  27. %% @doc Name of this transport API, <em>ssl</em>.
  28. -spec name() -> ssl.
  29. name() -> ssl.
  30. %% @doc Atoms used in the process messages sent by this API.
  31. %%
  32. %% They identify incoming data, closed connection and errors when receiving
  33. %% data in active mode.
  34. -spec messages() -> {ssl, ssl_closed, ssl_error}.
  35. messages() -> {ssl, ssl_closed, ssl_error}.
  36. %% @doc Setup a socket to listen on the given port on the local host.
  37. %%
  38. %% The available options are:
  39. %% <dl>
  40. %% <dt>port</dt><dd>Mandatory. TCP port number to open.</dd>
  41. %% <dt>backlog</dt><dd>Maximum length of the pending connections queue.
  42. %% Defaults to 1024.</dd>
  43. %% <dt>ip</dt><dd>Interface to listen on. Listen on all interfaces
  44. %% by default.</dd>
  45. %% <dt>certfile</dt><dd>Mandatory. Path to a file containing the user's
  46. %% certificate.</dd>
  47. %% <dt>keyfile</dt><dd>Mandatory. Path to the file containing the user's
  48. %% private PEM encoded key.</dd>
  49. %% <dt>cacertfile</dt><dd>Optional. Path to file containing PEM encoded
  50. %% CA certificates (trusted certificates used for verifying a peer
  51. %% certificate).</dd>
  52. %% <dt>password</dt><dd>Mandatory. String containing the user's password.
  53. %% All private keyfiles must be password protected currently.</dd>
  54. %% </dl>
  55. %%
  56. %% @see ssl:listen/2
  57. -spec listen([{port, inet:port_number()} | {certfile, string()}
  58. | {keyfile, string()} | {password, string()}
  59. | {cacertfile, string()} | {ip, inet:ip_address()}])
  60. -> {ok, ssl:sslsocket()} | {error, atom()}.
  61. listen(Opts) ->
  62. require([crypto, public_key, ssl]),
  63. {port, Port} = lists:keyfind(port, 1, Opts),
  64. Backlog = proplists:get_value(backlog, Opts, 1024),
  65. {certfile, CertFile} = lists:keyfind(certfile, 1, Opts),
  66. ListenOpts0 = [binary, {active, false},
  67. {backlog, Backlog}, {packet, raw}, {reuseaddr, true},
  68. {certfile, CertFile}],
  69. ListenOpts = lists:foldl(fun
  70. ({ip, _} = Ip, Acc) -> [Ip | Acc];
  71. ({keyfile, _} = KeyFile, Acc) -> [KeyFile | Acc];
  72. ({password, _} = Password, Acc) -> [Password | Acc];
  73. ({cacertfile, _} = CACertFile, Acc) -> [CACertFile | Acc];
  74. ({ciphers, _} = Ciphers, Acc) -> [Ciphers | Acc];
  75. (_, Acc) -> Acc
  76. end, ListenOpts0, Opts),
  77. ssl:listen(Port, ListenOpts).
  78. %% @doc Accept an incoming connection on a listen socket.
  79. %%
  80. %% Note that this function does both the transport accept and
  81. %% the SSL handshake.
  82. %%
  83. %% @see ssl:transport_accept/2
  84. %% @see ssl:ssl_accept/2
  85. -spec accept(ssl:sslsocket(), timeout())
  86. -> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}.
  87. accept(LSocket, Timeout) ->
  88. case ssl:transport_accept(LSocket, Timeout) of
  89. {ok, CSocket} ->
  90. ssl_accept(CSocket, Timeout);
  91. {error, Reason} ->
  92. {error, Reason}
  93. end.
  94. %% @doc Receive a packet from a socket in passive mode.
  95. %% @see ssl:recv/3
  96. -spec recv(ssl:sslsocket(), non_neg_integer(), timeout())
  97. -> {ok, any()} | {error, closed | atom()}.
  98. recv(Socket, Length, Timeout) ->
  99. ssl:recv(Socket, Length, Timeout).
  100. %% @doc Send a packet on a socket.
  101. %% @see ssl:send/2
  102. -spec send(ssl:sslsocket(), iolist()) -> ok | {error, atom()}.
  103. send(Socket, Packet) ->
  104. ssl:send(Socket, Packet).
  105. %% @doc Set one or more options for a socket.
  106. %% @see ssl:setopts/2
  107. -spec setopts(ssl:sslsocket(), list()) -> ok | {error, atom()}.
  108. setopts(Socket, Opts) ->
  109. ssl:setopts(Socket, Opts).
  110. %% @doc Assign a new controlling process <em>Pid</em> to <em>Socket</em>.
  111. %% @see ssl:controlling_process/2
  112. -spec controlling_process(ssl:sslsocket(), pid())
  113. -> ok | {error, closed | not_owner | atom()}.
  114. controlling_process(Socket, Pid) ->
  115. ssl:controlling_process(Socket, Pid).
  116. %% @doc Return the address and port for the other end of a connection.
  117. %% @see ssl:peername/1
  118. -spec peername(ssl:sslsocket())
  119. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  120. peername(Socket) ->
  121. ssl:peername(Socket).
  122. %% @doc Close a TCP socket.
  123. %% @see ssl:close/1
  124. -spec close(ssl:sslsocket()) -> ok.
  125. close(Socket) ->
  126. ssl:close(Socket).
  127. %% Internal.
  128. -spec require(list(module())) -> ok.
  129. require([]) ->
  130. ok;
  131. require([App|Tail]) ->
  132. case application:start(App) of
  133. ok -> ok;
  134. {error, {already_started, App}} -> ok
  135. end,
  136. require(Tail).
  137. -spec ssl_accept(ssl:sslsocket(), timeout())
  138. -> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}.
  139. ssl_accept(Socket, Timeout) ->
  140. case ssl:ssl_accept(Socket, Timeout) of
  141. ok ->
  142. {ok, Socket};
  143. {error, Reason} ->
  144. {error, Reason}
  145. end.