For Programmers: Free Programming Magazines  


Home > Archive > PERL POE > April 2008 > removing blocking network code









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author removing blocking network code
Kirill Miazine

2008-04-12, 7:53 pm

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)(.+)(?#:-)$)),$/)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com