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

Discussion in 'Perl Misc' started by sgt_b2002@yahoo.com, Dec 18, 2005.

  1. Guest

    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]
    , Dec 18, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Dec 19, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  2. Allen Marshall
    Replies:
    1
    Views:
    191
    Gunnar Hjalmarsson
    Jan 24, 2004
  3. Nikolaus Rath
    Replies:
    1
    Views:
    73
    Roy Smith
    Feb 17, 2014
  4. Chris Angelico
    Replies:
    0
    Views:
    72
    Chris Angelico
    Feb 17, 2014
  5. Nikolaus Rath
    Replies:
    0
    Views:
    81
    Nikolaus Rath
    Feb 18, 2014
Loading...

Share This Page