aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorHans Nilsson <[email protected]>2013-09-25 14:56:19 +0200
committerHans Nilsson <[email protected]>2013-10-28 16:09:20 +0100
commitd54e4318a77c7a42a7b8952780b33987775c7608 (patch)
tree1d05e90565293daa09b337e18e5021f78390c5d6 /lib
parent609e1414a0685c2082d30f9d5c50ca65209a1236 (diff)
downloadotp-d54e4318a77c7a42a7b8952780b33987775c7608.tar.gz
otp-d54e4318a77c7a42a7b8952780b33987775c7608.tar.bz2
otp-d54e4318a77c7a42a7b8952780b33987775c7608.zip
ssh: Add function ssh:peername/1 with test
OTP-11345, sto575, tsk374
Diffstat (limited to 'lib')
-rw-r--r--lib/ssh/doc/src/ssh.xml14
-rw-r--r--lib/ssh/src/ssh.erl12
-rw-r--r--lib/ssh/test/ssh_basic_SUITE.erl34
3 files changed, 59 insertions, 1 deletions
diff --git a/lib/ssh/doc/src/ssh.xml b/lib/ssh/doc/src/ssh.xml
index fb58a4b014..ddfb50ebd2 100644
--- a/lib/ssh/doc/src/ssh.xml
+++ b/lib/ssh/doc/src/ssh.xml
@@ -334,6 +334,20 @@
</func>
<func>
+ <name>peername(ConnectionRef) -> {ok, {Address,Port}} | {error,Error} </name>
+ <fsummary> </fsummary>
+ <type>
+ <v> ConnectionRef = ssh_connection_ref()</v>
+ <v> Address = ip_address()</v>
+ <v> Port = integer()</v>
+ </type>
+ <desc>
+ <p>Returns the address and port for the other end of a connection.
+ </p>
+ </desc>
+ </func>
+
+ <func>
<name>shell(Host) -> </name>
<name>shell(Host, Option) -> </name>
<name>shell(Host, Port, Option) -> _</name>
diff --git a/lib/ssh/src/ssh.erl b/lib/ssh/src/ssh.erl
index 7d5478c3f6..718321ef21 100644
--- a/lib/ssh/src/ssh.erl
+++ b/lib/ssh/src/ssh.erl
@@ -28,6 +28,7 @@
-export([start/0, start/1, stop/0, connect/3, connect/4, close/1, connection_info/2,
channel_info/3,
daemon/1, daemon/2, daemon/3,
+ peername/1,
stop_listener/1, stop_listener/2, stop_daemon/1, stop_daemon/2,
shell/1, shell/2, shell/3]).
@@ -245,6 +246,17 @@ shell(Host, Port, Options) ->
end.
%%--------------------------------------------------------------------
+%% Function: peername(ConnectionRef) -> {ok, {Host,Port}}
+%% | {error,Error}
+%%
+%% Description: Returns the peer address of the connection
+%%--------------------------------------------------------------------
+peername(ConnectionRef) ->
+ [{peer, {_Name,{IP,Port}}}] =
+ ssh_connection_manager:connection_info(ConnectionRef, [peer]),
+ {ok, {IP,Port}}.
+
+%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
fix_idle_time(SshOptions) ->
diff --git a/lib/ssh/test/ssh_basic_SUITE.erl b/lib/ssh/test/ssh_basic_SUITE.erl
index 0aa60624bf..e13610bc2a 100644
--- a/lib/ssh/test/ssh_basic_SUITE.erl
+++ b/lib/ssh/test/ssh_basic_SUITE.erl
@@ -22,6 +22,7 @@
-module(ssh_basic_SUITE).
-include_lib("common_test/include/ct.hrl").
+-include_lib("kernel/include/inet.hrl").
%% Note: This directive should only be used in test suites.
-compile(export_all).
@@ -48,7 +49,7 @@ all() ->
close].
groups() ->
- [{dsa_key, [], [send, exec, exec_compressed, shell, known_hosts, idle_time, rekey, openssh_zlib_basic_test]},
+ [{dsa_key, [], [send, peername, exec, exec_compressed, shell, known_hosts, idle_time, rekey, openssh_zlib_basic_test]},
{rsa_key, [], [send, exec, exec_compressed, shell, known_hosts, idle_time, rekey, openssh_zlib_basic_test]},
{dsa_pass_key, [], [pass_phrase]},
{rsa_pass_key, [], [pass_phrase]},
@@ -473,6 +474,37 @@ send(Config) when is_list(Config) ->
%%--------------------------------------------------------------------
+peername() ->
+ [{doc, "Test ssh:peername/1"}].
+peername(Config) when is_list(Config) ->
+ process_flag(trap_exit, true),
+ SystemDir = filename:join(?config(priv_dir, Config), system),
+ UserDir = ?config(priv_dir, Config),
+
+ {Pid, Host, Port} = ssh_test_lib:daemon([{system_dir, SystemDir},
+ {user_dir, UserDir},
+ {failfun, fun ssh_test_lib:failfun/2}]),
+ ConnectionRef =
+ ssh_test_lib:connect(Host, Port, [{silently_accept_hosts, true},
+ {user_dir, UserDir},
+ {user_interaction, false}]),
+ {ok,{IP,Port}} = ssh:peername(ConnectionRef),
+ host_equal(Host,IP),
+ ssh:stop_daemon(Pid).
+
+
+host_equal(Name, IP) when is_list(Name), is_tuple(IP) ->
+ Family = if size(IP)==4 -> inet;
+ size(IP)==8 -> inet6
+ end,
+ {ok,#hostent{h_addr_list=IPs}} = inet:gethostbyname(Name,Family),
+ lists:any(fun(X) -> X==IP end, IPs);
+host_equal(IP, Name) when is_list(Name), is_tuple(IP) ->
+ host_equal(Name, IP);
+host_equal(X,Y) -> X==Y.
+
+
+%%--------------------------------------------------------------------
close() ->
[{doc, "Simulate that we try to close an already closed connection"}].
close(Config) when is_list(Config) ->