user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/5] daemon: cleanup internal data structures
Date: Mon,  8 Aug 2022 23:53:09 +0000	[thread overview]
Message-ID: <20220808235311.2006279-4-e@80x24.org> (raw)
In-Reply-To: <20220808235311.2006279-1-e@80x24.org>

This avoids dangling {''} entries in $xnetd and
%tls_opt hashes.  Furthermore, we can safely undef
%tls_opt once it's associated with each $xnetd object.
---
 lib/PublicInbox/Daemon.pm | 66 ++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 35 deletions(-)

diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index bb140640..16bae231 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -179,10 +179,7 @@ EOF
 		die "--pid-file cannot end with '.oldbin'\n";
 	}
 	@listeners = inherit($listener_names);
-
-	# allow socket-activation users to set certs once and not
-	# have to configure each socket:
-	my @inherited_names = keys(%$listener_names) if defined($default_cert);
+	my @inherited_names = keys(%$listener_names);
 
 	# ignore daemonize when inheriting
 	$daemonize = undef if scalar @listeners;
@@ -191,20 +188,18 @@ EOF
 		$default_listen // die "no listeners specified\n";
 		push @cfg_listen, $default_listen
 	}
-
+	my ($default_scheme) = (($default_listen // '') =~ m!\A([^:]+)://!);
 	foreach my $l (@cfg_listen) {
 		my $orig = $l;
 		my ($scheme, $port, $opt);
-
 		$l =~ s!\A([a-z0-9]+)://!! and $scheme = $1;
-		(!$scheme && ($default_listen // '') =~ m!\A([^:]+)://!) and
-			$scheme = $1;
+		$scheme //= $default_scheme;
 		if ($l =~ /\A(?:\[[^\]]+\]|[^:]+):([0-9]+)/) {
 			$port = $1 + 0;
 			$scheme //= $KNOWN_TLS{$port} // $KNOWN_STARTTLS{$port};
 		}
-		$scheme or die "unable to determine URL scheme of $orig\n";
-		if (!defined($port) && index($l, '/') != 0) { # unix socket
+		$scheme // die "unable to determine URL scheme of $orig\n";
+		if (!defined($port) && index($l, '/') != 0) { # AF_UNIX socket
 			$port = $SCHEME2PORT{$scheme} //
 				die "no port in listen=$orig\n";
 			$l =~ s!\A([^/]+)!$1:$port! or
@@ -263,21 +258,28 @@ EOF
 
 	# cert/key options in @cfg_listen takes precedence when inheriting,
 	# but map well-known inherited ports if --listen isn't specified
-	# at all
-	for my $sockname (@inherited_names) {
-		$sockname =~ /:([0-9]+)\z/ or next;
-		if (my $scheme = $KNOWN_TLS{$1}) {
-			$xnetd->{$sockname} //= load_mod($scheme);
-			$tls_opt{"$scheme://$sockname"} ||= accept_tls_opt('');
-		} elsif (($scheme = $KNOWN_STARTTLS{$1})) {
-			$xnetd->{$sockname} //= load_mod($scheme);
-			$tls_opt{"$scheme://$sockname"} ||= accept_tls_opt('');
-			$tls_opt{''} ||= accept_tls_opt('');
+	# at all.  This allows socket-activation users to set certs once
+	# and not have to configure each socket:
+	if (defined $default_cert) {
+		my ($stls) = (($default_scheme // '') =~ /\A(pop3|nntp|imap)/);
+		for my $x (@inherited_names) {
+			$x =~ /:([0-9]+)\z/ or next; # no TLS for AF_UNIX
+			if (my $scheme = $KNOWN_TLS{$1}) {
+				$xnetd->{$x} //= load_mod($scheme);
+				$tls_opt{"$scheme://$x"} ||= accept_tls_opt('');
+			} elsif (($scheme = $KNOWN_STARTTLS{$1})) {
+				$xnetd->{$x} //= load_mod($scheme);
+				$tls_opt{"$scheme://$x"} ||= accept_tls_opt('');
+			} elsif (defined $stls) {
+				$tls_opt{"$stls://$x"} ||= accept_tls_opt('');
+			}
+		}
+	}
+	if (defined $default_scheme) {
+		for my $x (@inherited_names) {
+			$xnetd->{$x} //= load_mod($default_scheme);
 		}
 	}
-	my @d;
-	while (my ($k, $v) = each %tls_opt) { push(@d, $k) if !defined($v) }
-	delete @tls_opt{@d};
 	die "No listeners bound\n" unless @listeners;
 }
 
@@ -671,14 +673,14 @@ sub daemon_loop ($) {
 	};
 	my %post_accept;
 	while (my ($k, $ctx_opt) = each %tls_opt) {
-		my $l = $k;
-		$l =~ s!\A([^:]+)://!!;
-		my $scheme = $1 // '';
-		my $xn = $xnetd->{$l} // $xnetd->{''};
+		$ctx_opt // next;
+		my ($scheme, $l) = split(m!://!, $k, 2);
+		my $xn = $xnetd->{$l} // die "BUG: no xnetd for $k";
 		$xn->{tlsd}->{ssl_ctx_opt} //= $ctx_opt;
 		$scheme =~ m!\A(?:https|imaps|nntps|pop3s)! and
 			$post_accept{$l} = tls_cb(@$xn{qw(post_accept tlsd)});
 	}
+	undef %tls_opt;
 	my $sig = {
 		HUP => $refresh,
 		INT => \&worker_quit,
@@ -706,7 +708,7 @@ sub daemon_loop ($) {
 	@listeners = map {;
 		my $l = sockname($_);
 		my $tls_cb = $post_accept{$l};
-		my $xn = $xnetd->{$l} // $xnetd->{''};
+		my $xn = $xnetd->{$l} // die "BUG: no xnetd for $l";
 
 		# NNTPS, HTTPS, HTTP, IMAPS and POP3S are client-first traffic
 		# IMAP, NNTP and POP3 are server-first
@@ -720,13 +722,7 @@ sub daemon_loop ($) {
 
 sub run {
 	my ($default_listen) = @_;
-	my $xnetd = {};
-	if ($default_listen) {
-		$default_listen =~ /\A(http|imap|nntp|pop3)/ or
-			die "BUG: $default_listen";
-		$xnetd->{''} = load_mod($1);
-	}
-	daemon_prepare($default_listen, $xnetd);
+	daemon_prepare($default_listen, my $xnetd = {});
 	my $for_destroy = daemonize();
 
 	# localize GCF2C for tests:

  parent reply	other threads:[~2022-08-08 23:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-08 23:53 [PATCH 0/5] daemon/netd-related improvements Eric Wong
2022-08-08 23:53 ` [PATCH 1/5] daemon: use default address + well-known ports for scheme Eric Wong
2022-08-08 23:53 ` [PATCH 2/5] daemon: use per-listener SIG{__WARN__} callbacks Eric Wong
2022-08-08 23:53 ` Eric Wong [this message]
2022-08-08 23:53 ` [PATCH 4/5] imap: mailboxes list across listeners Eric Wong
2022-08-08 23:53 ` [PATCH 5/5] imapd|nntpd: drop ->base_url preload Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220808235311.2006279-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).