From b38de6f02fa04e36b881d2aad9c7f792beb0b6a1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 3 Mar 2016 05:14:31 +0000 Subject: daemon: introduce host_with_port for identifying sockets This allows us to share more code between daemons and avoids having to make additional syscalls for preparing REMOTE_HOST and REMOTE_PORT in the PSGI env in -httpd. This will also make supporting HTTP (and NNTP) over Unix sockets easier in a future commit. --- lib/PublicInbox/Daemon.pm | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'lib/PublicInbox/Daemon.pm') diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm index 77ab2a71..c101ecb7 100644 --- a/lib/PublicInbox/Daemon.pm +++ b/lib/PublicInbox/Daemon.pm @@ -157,20 +157,23 @@ sub reopen_logs { sub sockname ($) { my ($s) = @_; - my $n = getsockname($s) or return; - my ($port, $addr); - if (length($n) >= 28) { + my $addr = getsockname($s) or return; + my ($host, $port) = host_with_port($addr); + "$host:$port"; +} + +sub host_with_port ($) { + my ($addr) = @_; + my ($port, $host); + if (length($addr) >= 28) { require Socket6; - ($port, $addr) = Socket6::unpack_sockaddr_in6($n); - } else { - ($port, $addr) = Socket::sockaddr_in($n); - } - if (length($addr) == 4) { - $n = Socket::inet_ntoa($addr) + ($port, $host) = Socket6::unpack_sockaddr_in6($addr); + $host = '['.Socket6::inet_ntop(Socket6::AF_INET6(), $host).']'; } else { - $n = '['.Socket6::inet_ntop(Socket6::AF_INET6(), $addr).']'; + ($port, $host) = Socket::sockaddr_in($addr); + $host = Socket::inet_ntoa($host); } - $n .= ":$port"; + ($host, $port); } sub inherit () { -- cgit v1.2.3-24-ge0c7