+if (!$use_inetd) {
+ &log("Binding to port 79") if (defined($opts{v}));
+ my $server = IO::Socket::INET->new(Proto => 'tcp',
+ LocalPort => 'finger(79)',
+ Listen => SOMAXCONN,
+ Reuse => 1);
+
+ die "Cannot listen on finger port" unless $server;
+ &log("[Server listening for connections]");
+
+ my ($pid, $client, $hostinfo);
+
+ while ($client = $server->accept()) {
+ &log("Forking to handle client request") if (defined($opts{v}));
+ next if $pid = fork; # parent
+ die "fork: $!" unless defined $pid;
+
+ # child
+ $client->autoflush(1);
+ my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
+ &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
+ my $query = &readdata($client);
+ &ProcessQuery($client, $query) if (defined($query));
+ $client->close;
+ exit;
+ } continue {
+ $client->close;
+ }
+} else { # inetd
+ &log("inetd mode");
+ my $sockaddr = getpeername(STDIN);
+ my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
+ &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
+ my $query = &readdata(\*STDIN);
+ &ProcessQuery(\*STDOUT, $query) if (defined($query));
+ exit;
+}
+
+$ldap->unbind;