]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Daemon.pm
daemon: introduce host_with_port for identifying sockets
[public-inbox.git] / lib / PublicInbox / Daemon.pm
index 77ab2a71c166ba39b7844c8186baeec3bf1530ab..c101ecb7ad538313324879c4986587ca30fee0d8 100644 (file)
@@ -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 () {