S
shrike
Howdy,
I've written a TCP proxy in Perl. It is non-forking and restarts a
listening socket on the original port after a disconnection. From the
CLI it runs correctly. listen->connect->disconnect->listen.
But when I try to daemonize:
&Proc:
aemon::Init ;
The first socket will come up, but after closing it, I can not build a
socket again on the same port.
Anybody know how to fix this?
It dies at "The socket will not listen.", but only when running
daemonized.
I've included copies of my listen, accept and disconnect functions.
These use AUTOLOADed set/gets, so these examples won't build unless you
use Class::GAPI or some equivilant. I'm also using IO:
oll instead of
select. In a nutshell, $self->foo("bar") is the same as $self->{'foo'}
= "bar" ; Probably won't help much, but there it is.
-Thanks
-Matt
package Proxy ;
use IO:
oll qw(POLLIN POLLOUT POLLHUP POLLNVAL POLLERR) ;
use Time::HiRes qw(usleep) ;
use Socket qw(inet_aton inet_ntoa); # string to bin, bin to string
use IO::Socket::INET ;
use Class::GAPI ;
our @ISA = qw(Class::GAPI) ;
# Listen
#################
sub _createsock_listen {
my $self = shift ;
my $foo = IO::Socket::INET->new(Listen => 1,
LocalPort => $self->{'l'},
Reuse => 1,
Blocking => 0,
Proto => 'tcp')
|| die "The socket will not listen." ;
$self->_LISTEN($foo) ;
warn("Listening for a client on $foo") if ($self->{'W'}) ;
}
#Accept
#################
sub _wait_accept { # Listen to Connect
my $self = shift ;
my $waiting = 1 ;
my $foo ;
while ($waiting) {
usleep $self->{'_listendelay'} ;
next unless $foo = $self->_LISTEN->accept ;
$waiting = 0 ;
next ;
}
$foo->blocking(0) ;
$self->_handles->mask($foo => POLLIN|POLLOUT) ;
$self->_IN($foo);
warn ("Client connection accepted on $foo") if ($self->{'W'})
;
}
# disconnect
##################
sub _disco { # Reset Everything but the running flag
my $self = shift ;
$self->CONNECTED(0) ; # stop transmitting
$self->AUTHENTICATED(0) ; # kick the user
$self->_delshims() ; # Waste any shim hooks
$self->_handles->mask($self->_IN() => 0) ; # Tell poll to drop
the handles
$self->_handles->mask($self->_OUT() => 0) ; #
close($self->{'_IN'}) ; # Then close them
close($self->{'_OUT'}) ; #
close($self->{'_LISTEN'}) ; #
warn "Disconnection complete" ; #
}
1;
I've written a TCP proxy in Perl. It is non-forking and restarts a
listening socket on the original port after a disconnection. From the
CLI it runs correctly. listen->connect->disconnect->listen.
But when I try to daemonize:
&Proc:
The first socket will come up, but after closing it, I can not build a
socket again on the same port.
Anybody know how to fix this?
It dies at "The socket will not listen.", but only when running
daemonized.
I've included copies of my listen, accept and disconnect functions.
These use AUTOLOADed set/gets, so these examples won't build unless you
use Class::GAPI or some equivilant. I'm also using IO:
select. In a nutshell, $self->foo("bar") is the same as $self->{'foo'}
= "bar" ; Probably won't help much, but there it is.
-Thanks
-Matt
package Proxy ;
use IO:
use Time::HiRes qw(usleep) ;
use Socket qw(inet_aton inet_ntoa); # string to bin, bin to string
use IO::Socket::INET ;
use Class::GAPI ;
our @ISA = qw(Class::GAPI) ;
# Listen
#################
sub _createsock_listen {
my $self = shift ;
my $foo = IO::Socket::INET->new(Listen => 1,
LocalPort => $self->{'l'},
Reuse => 1,
Blocking => 0,
Proto => 'tcp')
|| die "The socket will not listen." ;
$self->_LISTEN($foo) ;
warn("Listening for a client on $foo") if ($self->{'W'}) ;
}
#Accept
#################
sub _wait_accept { # Listen to Connect
my $self = shift ;
my $waiting = 1 ;
my $foo ;
while ($waiting) {
usleep $self->{'_listendelay'} ;
next unless $foo = $self->_LISTEN->accept ;
$waiting = 0 ;
next ;
}
$foo->blocking(0) ;
$self->_handles->mask($foo => POLLIN|POLLOUT) ;
$self->_IN($foo);
warn ("Client connection accepted on $foo") if ($self->{'W'})
;
}
# disconnect
##################
sub _disco { # Reset Everything but the running flag
my $self = shift ;
$self->CONNECTED(0) ; # stop transmitting
$self->AUTHENTICATED(0) ; # kick the user
$self->_delshims() ; # Waste any shim hooks
$self->_handles->mask($self->_IN() => 0) ; # Tell poll to drop
the handles
$self->_handles->mask($self->_OUT() => 0) ; #
close($self->{'_IN'}) ; # Then close them
close($self->{'_OUT'}) ; #
close($self->{'_LISTEN'}) ; #
warn "Disconnection complete" ; #
}
1;