0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +00:00

Pass {ip, Ip} configuration through for TCP and SSL transports

This commit is contained in:
Hunter Morris 2011-06-21 13:39:06 +01:00
parent a93eb02646
commit 5f438561e9
2 changed files with 20 additions and 6 deletions

View file

@ -25,7 +25,8 @@ name() -> ssl.
messages() -> {ssl, ssl_closed, ssl_error}. messages() -> {ssl, ssl_closed, ssl_error}.
-spec listen([{port, inet:ip_port()} | {certfile, string()} -spec listen([{port, inet:ip_port()} | {certfile, string()}
| {keyfile, string()} | {password, string()}]) | {keyfile, string()} | {password, string()}
| {ip, inet:ip_address()}])
-> {ok, ssl:sslsocket()} | {error, atom()}. -> {ok, ssl:sslsocket()} | {error, atom()}.
listen(Opts) -> listen(Opts) ->
require([crypto, public_key, ssl]), require([crypto, public_key, ssl]),
@ -34,9 +35,15 @@ listen(Opts) ->
{certfile, CertFile} = lists:keyfind(certfile, 1, Opts), {certfile, CertFile} = lists:keyfind(certfile, 1, Opts),
{keyfile, KeyFile} = lists:keyfind(keyfile, 1, Opts), {keyfile, KeyFile} = lists:keyfind(keyfile, 1, Opts),
{password, Password} = lists:keyfind(password, 1, Opts), {password, Password} = lists:keyfind(password, 1, Opts),
ssl:listen(Port, [binary, {active, false}, ListenOpts0 = [binary, {active, false},
{backlog, Backlog}, {packet, raw}, {reuseaddr, true}, {backlog, Backlog}, {packet, raw}, {reuseaddr, true},
{certfile, CertFile}, {keyfile, KeyFile}, {password, Password}]). {certfile, CertFile}, {keyfile, KeyFile}, {password, Password}],
ListenOpts =
case lists:keyfind(ip, 1, Opts) of
false -> ListenOpts0;
Ip -> [Ip|ListenOpts0]
end,
ssl:listen(Port, ListenOpts).
-spec accept(ssl:sslsocket(), timeout()) -spec accept(ssl:sslsocket(), timeout())
-> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}. -> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}.

View file

@ -24,12 +24,19 @@ name() -> tcp.
-spec messages() -> {tcp, tcp_closed, tcp_error}. -spec messages() -> {tcp, tcp_closed, tcp_error}.
messages() -> {tcp, tcp_closed, tcp_error}. messages() -> {tcp, tcp_closed, tcp_error}.
-spec listen([{port, inet:ip_port()}]) -> {ok, inet:socket()} | {error, atom()}. -spec listen([{port, inet:ip_port()} | {ip, inet:ip_address()}])
-> {ok, inet:socket()} | {error, atom()}.
listen(Opts) -> listen(Opts) ->
{port, Port} = lists:keyfind(port, 1, Opts), {port, Port} = lists:keyfind(port, 1, Opts),
Backlog = proplists:get_value(backlog, Opts, 1024), Backlog = proplists:get_value(backlog, Opts, 1024),
gen_tcp:listen(Port, [binary, {active, false}, ListenOpts0 = [binary, {active, false},
{backlog, Backlog}, {packet, raw}, {reuseaddr, true}]). {backlog, Backlog}, {packet, raw}, {reuseaddr, true}],
ListenOpts =
case lists:keyfind(ip, 1, Opts) of
false -> ListenOpts0;
Ip -> [Ip|ListenOpts0]
end,
gen_tcp:listen(Port, ListenOpts).
-spec accept(inet:socket(), timeout()) -spec accept(inet:socket(), timeout())
-> {ok, inet:socket()} | {error, closed | timeout | atom()}. -> {ok, inet:socket()} | {error, closed | timeout | atom()}.