<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># -*- mode: perl -*-
# ============================================================================

package Net::SNMP::Transport::IPv4;

# $Id: IPv4.pm,v 1.1 2009/09/09 15:08:31 dtown Rel $

# Base object for the IPv4 Transport Domains.

# Copyright (c) 2008-2009 David M. Town &lt;dtown@cpan.org&gt;
# All rights reserved.

# This program is free software; you may redistribute it and/or modify it
# under the same terms as the Perl 5 programming language system itself.

# ============================================================================

use strict;

use Net::SNMP::Transport;

use IO::Socket qw(
   INADDR_ANY INADDR_LOOPBACK inet_aton PF_INET sockaddr_in inet_ntoa
);

## Version of the Net::SNMP::Transport::IPv4 module

our $VERSION = v1.0.0;

# [private methods] ----------------------------------------------------------

sub _socket_create
{
   my ($this) = @_;

   return IO::Socket-&gt;new()-&gt;socket($this-&gt;_protocol_family(),
                                    $this-&gt;_protocol_type(),
                                    $this-&gt;_protocol());
}

sub _protocol_family
{
   return PF_INET;
}

sub _addr_any
{
   return INADDR_ANY;
}

sub _addr_loopback
{
   return INADDR_LOOPBACK;
}

sub _hostname_resolve
{
   my ($this, $host, $nh) = @_;

   $nh-&gt;{addr} = undef;

   # See if the the service/port was included in the address.

   my $serv = ($host =~ s/:([\w\(\)\/]+)$//) ? $1 : undef;

   if (defined($serv) &amp;&amp; (!defined $this-&gt;_service_resolve($serv, $nh))) {
      return $this-&gt;_error('Failed to resolve the %s service', $this-&gt;type());
   }

   # Resolve the address.

   if (!defined ($nh-&gt;{addr} = inet_aton($_[1] = $host))) {
      return $this-&gt;_error(
         q{Unable to resolve the %s address "%s"}, $this-&gt;type(), $host
      );
   }

   return $nh-&gt;{addr};
}

sub _name_pack
{
   return sockaddr_in($_[1]-&gt;{port}, $_[1]-&gt;{addr});
}

sub _address
{
   return inet_ntoa($_[0]-&gt;_addr($_[1]));
}

sub _addr
{
   return (sockaddr_in($_[1]))[1];
}

sub _port
{
   return (sockaddr_in($_[1]))[0];
}

sub _taddress
{
   return sprintf '%s:%d', $_[0]-&gt;_address($_[1]), $_[0]-&gt;_port($_[1]);
}

sub _taddr
{
   return $_[0]-&gt;_addr($_[1]) . pack 'n', $_[0]-&gt;_port($_[1]);
}

# ============================================================================
1; # [end Net::SNMP::Transport::IPv4]

</pre></body></html>