On 8/3/07 9:26 AM, "Peter J. Holzer" <[EMAIL PROTECTED]> wrote:

> On 2007-08-02 17:54:01 -0400, Guy Hulbert wrote:
>> On Thu, 2007-08-02 at 23:04 +0200, Peter J. Holzer wrote:
>>> * The return value should probably be cached. Doing a DNS lookup every
>>>   time is wasteful. A connection note is probably the cleanest way to
>>>   do this.
>> 
>> Why does everyone (i've seen this on other projects) want to write code
>> to do DNS lookups ?
> 
> I don't want to write code to do DNS looks. gethostbyaddr does a DNS
> lookup (unless it gets the info from /etc/hosts, or NIS, or whatever),
> and I want to avoid to call it more often than necessary.
> 
> Therefore instead of
> 
I'm now running:


use warnings;
use strict;
use Socket;

sub hook_config {
    my ($self, $transaction, $config) = @_;
    $self->log(LOGINFO, "config_me_localaddr called with $config");
    return DECLINED unless $config eq 'me';
    return DECLINED unless ($self->connection &&
                            $self->connection->local_ip);
    my $conn_ip = $self->connection->local_ip;
    my $local_ip = $self->connection->notes('local_ip');
    my $local_name = $self->connection->notes('local_name');
    if ((! defined $local_ip) || ($local_ip ne $conn_ip)) {
        $local_name = gethostbyaddr(inet_aton($conn_ip), AF_INET);
        $self->connection->notes('local_name', $local_name);
        $self->connection->notes('local_ip', $local_ip);
        $self->log(LOGDEBUG, "local_name set to $local_name");
    }
    return (OK, $local_name);
}

It's probably not not the most efficient code, but it's reasonably straight
forward.  It might still call gethostbyaddr for each new connection but it
should only be the one call for each connection.

> 
>> Won't a local daemon (bind or djbdns) + libresolv do the caching for
>> you ?
> 
> It's still a UDP query to an external process.

I run a local resolver on each system bound to localhost, so it hits the
wire infrequently.

Reply via email to