The following is a description of a protocol I designed for an job candidate examination:
   Receives lines from a client:
      iam <id>
      need-done <args>
   In response to a need-done, sends to a client that has given
   an "iam" message:
      dothis <args>
   Server ignores any other lines
It's basically a trivial job dispatch protocol where clients register to do work with a central server and are also able to request jobs. Designwise, it's pretty much all that the described chat server would do.

The exam question asked them to describe the protocol given the following code and then identify at least three security holes (with proposed attacks) in the protocol as well as explain how you'd fix the security problems. Which is why it was written so badly. Sorry...

use Socket; socket S, PF_INET, SOCK_STREAM, getprotobyname('tcp') or die "$!"; setsockopt S, &SOL_SOCKET, &SO_REUSEADDR, 1 or die "$!"; bind S, sockaddr_in( 23456, INADDR_ANY ) or die "$!"; listen S, 5 or die "$!"; $SIG{__DIE__} = sub { close S; }; $rin = ""; vec( $rin, fileno(S), 1 ) = 1; while( 1 ) { next unless select( $rout = $rin, undef, undef, undef ) > 0; if( vec( $rout, fileno(S), 1 ) ) { local *H; $p = accept( H, S ); select( (select(H), $| = 1)[0]); push @known, *H; vec( $rin, fileno(H), 1 ) = 1; } foreach my $fd (@known) { if( vec( $rout, fileno($fd), 1 ) ) { $s = readline $fd; push @able, $fd if $s =~ /^iam\s+(.+)\s*$/io; if( @able and $s =~ /^need-done\s+(.+)\s*$/io ) { print { $able[(unpack "%32C*",$1) % @able] } "dothis ", $1 +, "\n"; } } } }
For some reason, only one candidate even attempted the question.

That aside, it's certainly possible to write a chat server in perl. It's not threaded, but I don't see why you'd really want a threaded server when so much state has to be shared between all the processes and the overhead of serving requests is so low.

c.


In reply to Re: Chat server impossible with Perl? by beauregard
in thread Chat server impossible with Perl? by bronto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.