#!/usr/bin/perl -wT
use
Sys::Syslog
qw(openlog syslog setlogsock closelog)
;
my
$daemon_name
=
'aserver'
;
my
$port
= 1234;
setlogsock(
'unix'
);
openlog(
$daemon_name
,
'pid'
,
'local2'
);
socket
(SERVER, AF_INET, SOCK_STREAM,
getprotobyname
(
'tcp'
));
setsockopt
(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
my
$addr
= sockaddr_in(
$port
, INADDR_ANY);
bind
(SERVER,
$addr
);
listen
(SERVER, SOMAXCONN);
while
(
accept
(CLIENT, SERVER)) {
my
(
$port
,
$client_addr
) = sockaddr_in(
getpeername
(CLIENT));
my
$req
= request_init(RQ_DAEMON,
$daemon_name
, RQ_FILE,
fileno
(CLIENT));
fromhost(
$req
);
if
(hosts_access(
$req
)) {
syslog(
"local2|info"
,
"connection from %s allowed"
, inet_ntoa(
$client_addr
));
print
CLIENT
"Hi there\n"
;
close
(CLIENT);
}
else
{
close
(CLIENT);
syslog(
"local2|warning"
,
"access from %s denied"
, inet_ntoa(
$client_addr
));
}
}
close
(SERVER);
closelog;