IO::Socket::SSL : $sock is not defined if client is not SSL (crash)

S

sgt_b2002

Working on a small utility and everything works perfectly using
IO:Socket::INET. When I move over to using IO::Socket::SSL, a certain
piece of code no longer works. Essentially, this is a "one client only"
application. If the original socket is defined, subsequent connections
are immediately closed. When using IO::Socket::INET, this works great.
Moving over to SSL, things die.

Here when a non SSL connection is established, the new $sock object is
never defined. Because it is not defined, calls to it obviously fail.
Shouldn't $tempsock be created regardless of whether or not SSL is
being used? I'm thinking that SSL_error_trap was designed to deal with
those cases.

Right now, the below code will kill the server if a non SSL client
connects. Here is the error:

Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be
16 at /usr/lib/perl5/5.8.7/i686-linux/Socket.pm line 370.

If I remove everything but the $tempsock object, I'll get a "can't call
method close on an undefined object".

Any ideas?

[code snippet]
$agent_sock = new IO::Socket::SSL(
LocalHost => $aip,
LocalPort => $aport,
Proto => 'tcp',
Listen => 1,
Reuse => 1,
SSL_cert_file => 'server.crt',
SSL_key_file => 'server.key',
SSL_error_trap => \&ssl_error,
);
$agent_sock or die "Socket error: $!";

$sel = IO::Select->new($agent_sock);

while(@ready = $sel->can_read) {

foreach $fh (@ready) {
if ($fh == $agent_sock) {
if (defined $asock) {
my $tempsock = $agent_sock->accept();
my ($tempsock,$tempaddr) = $agent_sock->accept();
my ($tempport, $tempip) = sockaddr_in($tempaddr);
my $tempipnum = inet_ntoa($tempip);
$tempsock->close;
print "Closed an unsolicited Agent connection from
".$tempipnum.':'.$tempport."\n";
last;
}
($asock, $a_addr) = $agent_sock->accept();
($a_port, $a_ip) = sockaddr_in($a_addr);
$a_ipnum = inet_ntoa($a_ip);
print "Relay Agent connecting from: $a_ipnum:$a_port\n";
$sel->add($asock);
<snip>
[/code snippet]
 
X

xhoster

Working on a small utility and everything works perfectly using
IO:Socket::INET. When I move over to using IO::Socket::SSL, a certain
piece of code no longer works. Essentially, this is a "one client only"
application.

What is a "one client only" application?
If the original socket is defined, subsequent connections
are immediately closed.

Then why even bother accepting them?
When using IO::Socket::INET, this works great.
Moving over to SSL, things die.

Here when a non SSL connection is established, the new $sock object is
never defined. Because it is not defined, calls to it obviously fail.

Then don't make calls to it when it is undefined.
Shouldn't $tempsock be created regardless of whether or not SSL is
being used?

Apparently not.
I'm thinking that SSL_error_trap was designed to deal with
those cases.

It was, but probably the way it "deals" with them is not the way you think
it does.

Since you haven't shown us the code of \&ssl_error, which is what you
pass to SSL_error_trap, how are we to know what you think it does?

Right now, the below code will kill the server if a non SSL client
connects. Here is the error:

Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be
16 at /usr/lib/perl5/5.8.7/i686-linux/Socket.pm line 370.

If I remove everything but the $tempsock object, I'll get a "can't call
method close on an undefined object".

Any ideas?

Yeah. check the success and/or failure of "accept", and act accordingly.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top