Skip to content

Commit

Permalink
[backend] BSServer, bs_worker: support binding to a specific address
Browse files Browse the repository at this point in the history
  • Loading branch information
mlschroe committed Jan 24, 2023
1 parent 32b14f2 commit a546194
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
31 changes: 17 additions & 14 deletions src/backend/BSServer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ sub deamonize {
my $ai_addrconfig = eval { Socket::AI_ADDRCONFIG() } || 0;

sub serveropen {
my ($port, $user, $group, $family, $bindaddr) = @_;
# creates master socket
# 512 connections in the queue maximum
# $port:
Expand All @@ -90,7 +91,6 @@ sub serveropen {
# other string - tcp socket on $port (assumes it is a number)
# $user, $group:
# if defined, try to set appropriate UID, EUID, GID, EGID ( $<, $>, $(, $) )
my ($port, $user, $group, $family) = @_;
# check if $user and $group exist on this system
my $tcpproto = getprotobyname('tcp');
my @ports;
Expand All @@ -100,35 +100,38 @@ sub serveropen {
@ports = split(',', $port, 2);
}

if (!defined($family) && $bindaddr) {
$family = AF_INET6 if $bindaddr =~ /:/;
$family = AF_INET if $bindaddr =~ /^\d+\.\d+\.\d+\.\d+$/;
}

# check if we should do ipv6
if (!defined($family) && $ai_addrconfig && grep {ref($_) || !/^&/} @ports) {
my ($err, @ai) = Socket::getaddrinfo("", 0, { 'socktype' => SOCK_STREAM, 'flags' => $ai_addrconfig });
$family = AF_INET6 if grep {$_->{'family'} == AF_INET6} @ai;
}
my $sa;
if ($bindaddr) {
$sa = Socket::inet_pton($family, $bindaddr);
die("bad bind address '$bindaddr'\n") unless $sa;
}
my @sock;
for $port (@ports) {
my $s;
if (!ref($port) && $port =~ /^&/) {
open($s, "<$port") || die("socket open: $!\n");
} elsif ($family && $family == AF_INET6) {
$sa ||= Socket::IN6ADDR_ANY();
socket($s , PF_INET6, SOCK_STREAM, $tcpproto) || die "socket: $!\n";
setsockopt($s, SOL_SOCKET, SO_REUSEADDR, pack("l",1));
if (ref($port)) {
bind($s, sockaddr_in6(0, Socket::IN6ADDR_ANY())) || die "bind: $!\n";
($$port) = sockaddr_in6(getsockname($s));
} else {
bind($s, sockaddr_in6($port, Socket::IN6ADDR_ANY())) || die "bind: $!\n";
}
bind($s, sockaddr_in6(ref($port) ? 0 : $port, $sa)) || die "bind: $!\n";
($$port) = sockaddr_in6(getsockname($s)) if ref($port);
} else {
$sa ||= Socket::INADDR_ANY();
socket($s , PF_INET, SOCK_STREAM, $tcpproto) || die "socket: $!\n";
setsockopt($s, SOL_SOCKET, SO_REUSEADDR, pack("l",1));
if (ref($port)) {
bind($s, sockaddr_in(0, INADDR_ANY)) || die "bind: $!\n";
($$port) = sockaddr_in(getsockname($s));
} else {
bind($s, sockaddr_in($port, INADDR_ANY)) || die "bind: $!\n";
}
bind($s, sockaddr_in(ref($port) ? 0 : $port, $sa)) || die "bind: $!\n";
($$port) = sockaddr_in(getsockname($s)) if ref($port);
}
listen($s , 512) || die "listen: $!\n";
push @sock, $s;
Expand Down
10 changes: 6 additions & 4 deletions src/backend/BSStdServer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ sub isrunning {
return 1 unless $conf; # can't check
# hmm, might want to use a lock instead...
eval {
BSServer::serveropen($conf->{'port'});
BSServer::serveropen($conf->{'port'}, undef, undef, $conf->{'socketfamily'}, $conf->{'bindaddress'});
BSServer::serverclose();
};
return $@ && "$@" =~ /bind:/ ? 1 : 0;
Expand Down Expand Up @@ -403,6 +403,7 @@ sub server {
$conf->{'ssl_keyfile'} ||= $BSConfig::ssl_keyfile if $BSConfig::ssl_keyfile;
$conf->{'ssl_certfile'} ||= $BSConfig::ssl_certfile if $BSConfig::ssl_certfile;
$conf->{'ssl_verify'} ||= $BSConfig::ssl_verify if $BSConfig::ssl_verify;
$conf->{'bindaddress'} ||= $BSConfig::global_bindaddress if $BSConfig::global_bindaddress;
BSDispatch::compile($conf);
}
if ($aconf) {
Expand Down Expand Up @@ -465,7 +466,7 @@ sub server {
$port2 = "&=$ports[1]" if $port2 && defined $ports[1];
POSIX::close($ports[1]) if !$port2 && defined $ports[1];
}
BSServer::serveropen($port2 ? "$port,$port2" : $port, $BSConfig::bsuser, $BSConfig::bsgroup, $conf->{'socketfamily'});
BSServer::serveropen($port2 ? "$port,$port2" : $port, $BSConfig::bsuser, $BSConfig::bsgroup, $conf->{'socketfamily'}, $conf->{'bindaddress'});
}
if ($conf && $aconf) {
$conf->{'ajaxsocketpath'} = $aconf->{'socketpath'};
Expand Down Expand Up @@ -498,10 +499,11 @@ sub server {
# intialize xml converter to speed things up
XMLin(['startup' => '_content'], '<startup>x</startup>');

my $bind = $conf->{'bindaddress'} ? " address $conf->{'bindaddress'}" : '';
if ($conf->{'port2'}) {
BSServer::msg("$name started on ports $conf->{port} and $conf->{port2}");
BSServer::msg("$name started on ports $conf->{port} and $conf->{port2}$bind");
} else {
BSServer::msg("$name started on port $conf->{port}");
BSServer::msg("$name started on port $conf->{port}$bind");
}
if ($conf->{'memoize'}) {
$memoize_fn = $conf->{'memoize'};
Expand Down
8 changes: 7 additions & 1 deletion src/backend/bs_worker
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ my $binsufsre = join('|', map {"\Q$_\E"} @binsufs);
my $buildroot;
my $port;
my $proto;
my $bindaddress;
my $statedir;
my $hostarch;
my $vm = '';
Expand Down Expand Up @@ -415,6 +416,11 @@ while (@ARGV) {
$port = shift @ARGV;
next;
}
if ($ARGV[0] eq '--bindaddress') {
shift @ARGV;
$bindaddress = shift @ARGV;
next;
}
if ($ARGV[0] eq '--proto') {
shift @ARGV;
$proto = shift @ARGV;
Expand Down Expand Up @@ -4367,7 +4373,7 @@ unlink("$buildroot/.build.log");
commitstate({'state' => 'idle'});

# start server process...
BSServer::serveropen($port ? $port : \$port);
BSServer::serveropen($port ? $port : \$port, undef, undef, undef, $bindaddress);

print "worker started on port $port code $workercode build $buildcode\n";
mkdir($buildroot) unless -d $buildroot;
Expand Down

0 comments on commit a546194

Please sign in to comment.