develooper Front page | perl.poe | Postings from April 2008

removing blocking network code

From:
Kirill Miazine
Date:
April 12, 2008 09:39
Subject:
removing blocking network code
Dear POEts,

I used to have a non-POE module that worked as an authentication server
saslauthd. A while ago I converted it to POE, but there're still traces
from the old days that I'd like to get rid of. As I'm not very much
familiar with creating non-blocking servers that process binary data,
I hope I can get some help here.

Below is the code. _start is a state handler that received the network
handle as it's argument. Ideally I'd like to see state handlers that
read SASL strings and write response back to the client. I've done that
when working with simple line-oriented protocols, but dealing with
binary data confuses me.

Thanks in advance for any advice!

<ccode>
sub _start {
     my ($kernel, $heap) = @_[KERNEL, HEAP];
     my $handle = $_[ARG0];

     $handle->blocking(1); # XXX fix later
     my $username = _sasl_string($handle);
     my $password = _sasl_string($handle);
     my $service = _sasl_string($handle);
     my $realm = _sasl_string($handle);

     my $domain = $realm || 'krot.org';
     if (!$realm and $username =~ /[@%]/) {
         ($username, $domain) = split /[@%]/, $username, 2;
     }

     my $user = krot_auth("$username\@$domain", $password, $service);
     return defined $user ? _sasl_allow($handle) : _sasl_deny($handle);
}

sub _sasl_string {
     my $sock = shift;
     my $buf;
     $sock->read($buf, 2);
     my $size = unpack('n', $buf);
     $sock->read($buf, $size);
     return unpack("A$size", $buf);
}

sub _sasl_allow {
     my $sock = shift;
     $sock->print(pack('nA3', 2, "OK\0"));
     $sock->close();
}

sub _sasl_deny {
     my $sock = shift;
     $sock->print(pack('nA3', 2, "NO\0"));
     $sock->close();
}
</code>

-- 
#!/usr/bin/perl -w
print(&{sub{eval(qq(q(@_)))}}((join(''=>map{ord=~m(1(06|12))?uc:lc}($[=>
map{chr}(97..122))[map{int}grep{length}split(/(\d\d)/,'102119200114152'.
q(008051816051812080103110518))]))=~m(^(Just)(.+)(Perl)(.+)(?#:-)$)),$/)




Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About