develooper Front page | perl.beginners | Postings from December 2002

Hooking a perl script to xinetd

Thread Next
From:
Michael Weber
Date:
December 27, 2002 10:18
Subject:
Hooking a perl script to xinetd
Message ID:
se0c44fc.016@mail-2.alliednational.com
I have a script that listens on a port for a connection, receives some
data over that connection, and writes it to a file.

This works, but it takes up CPU cycles, RAM etc. when it waits.  I am
trying to get the script to use xinetd instead.  That way the tcp daemon
will launch it when needed and let it die when it's done.

What I get when xinetd runs it,however, is "bind: Address already in
use".

Here's the script.  (Fervent readers of Ellie Quigley will recognize it
right off as a modified copy of Example 18.21."

(My /etc/services file contains the entry (nukem  2222/tcp".)

Thanx!

-Michael

#!/usr/bin/perl -T

# Program name:  nukem_listner,
# opens a Rendezvous Socket on port 2222
# and waits for a client to connect.
# When the client connects, this server receives the IP
# address of an offending host, blocks it with iptables,
# and adds it to the list of blocked hosts.
#
# Usage: nukem_listner.pl IP_Address
#
use strict;
use warnings;
my($port, $AF_INET, $SOCK_STREAM, $sockaddr, # Variable declarations
                $name, $aliases, $proto, $this, $now);

($port)=@ARGV;
$port=2222;
$AF_INET=2;
$SOCK_STREAM = 1;
$sockaddr = 'S n a4 x8';
($name,$aliases,$proto)=getprotobyname('tcp');
if($port !~ /^\d+$/){
    ($name, $aliases, $port)=getservbyport($port,'tcp');
}
print "Port = $port\n";
$this = pack($sockaddr, $AF_INET, $port, "\0\0\0\0");
select(COMM_SOCK); $| = 1; select(STDOUT);

# Create R_SOCKET, the rendezvous socket descriptor
socket(R_SOCKET, $AF_INET, $SOCK_STREAM, $proto ) ||
    die "socket: $!\n";

# Bind R_SOCKET to my address, $this
bind(R_SOCKET, $this) || die "bind: $!\n";
listen(R_SOCKET, 5) || die "connect: $!\n";

# Infinite loop - wait until client connects, then serve the client
my $ip_addr = "255.255.255.255";

accept(COMM_SOCK, R_SOCKET) || die "$!\n";
read(COMM_SOCK,$ip_addr,16);
print COMM_SOCK "Got it!";

close (COMM_SOCK);

chomp $ip_addr;

open(LOGFILE, ">/var/log/nukem.log");
print LOGFILE "Got IP address of $ip_addr\n";

close(LOGFILE);



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About