Skip to content

Instantly share code, notes, and snippets.

@essen
Created December 16, 2017 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save essen/599a9652e5c3db591ab2198278722a6f to your computer and use it in GitHub Desktop.
Save essen/599a9652e5c3db591ab2198278722a6f to your computer and use it in GitHub Desktop.
do_get_remote_pid_tcp(Socket) when is_port(Socket) →
do_get_remote_pid_tcp(inet:sockname(Socket));
do_get_remote_pid_tcp(SockName) →
AllPorts = [{P, erlang:port_info(P)} || P ← erlang:ports()],
[Pid] = [
proplists:get_value(connected, I)
|| {P, I} ← AllPorts,
proplists:get_value(name, I) ≡ "tcp_inet",
inet:peername(P) ≡ SockName],
Pid.
-include_lib("ssl/src/ssl_connection.hrl").
do_get_remote_pid_tls(Socket) →
%% This gives us the pid of the sslsocket process.
%% We must introspect this process in order to retrieve the connection pid.
TLSPid = do_get_remote_pid_tcp(ssl:sockname(Socket)),
{_, #state{user_application={_, UserPid}}} = sys:get_state(TLSPid),
UserPid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment