Captive-Portal
view release on metacpan or search on metacpan
lib/Captive/Portal/Role/Utils.pm view on Meta::CPAN
package Captive::Portal::Role::Utils;
use strict;
use warnings;
=head1 NAME
Captive::Portal::Role::Utils - common utils for Captive::Portal
=cut
our $VERSION = '4.10';
use Log::Log4perl qw(:easy);
use Spawn::Safe qw(spawn_safe);
use Try::Tiny;
use Socket qw(inet_ntoa);
use Net::hostent;
use Template::Exception;
use Role::Basic;
requires qw(cfg);
=head1 DESCRIPTION
Utility roles needed by other modules. All roles die on error.
=head1 ROLES
=over 4
=item $capo->find_mac($ip)
Returns the corresponding MAC address for given IP address from /proc/net/arp on success or undef on failure.
=cut
sub find_mac {
my $self = shift;
my $lookup_ip = shift
or LOGDIE("missing parameter 'ip'");
if ( $self->cfg->{MOCK_MAC} ) {
DEBUG 'using mocked MAC address';
return 'DE:AD:BE:EF:DE:AD';
}
DEBUG 'open /proc/net/arp';
open ARP, '<', '/proc/net/arp'
or LOGDIE "Couldn't open /proc/net/arp: $!\n";
my @proc_net_arp = <ARP>
or LOGDIE "Couldn't read /proc/net/arp: $!\n";
# regex for ipv4 address
my $ipv4_rx = qr/\d{1,3} \. \d{1,3} \. \d{1,3} \. \d{1,3}/x;
# regex for MAC address matching
my $hex_digit_rx = qr/[A-F,a-f,0-9]/;
my $mac_rx = qr/(?:$hex_digit_rx{2}:){5} $hex_digit_rx{2}/x;
my $arp_tbl = {};
foreach my $line (@proc_net_arp) {
# 10.10.1.2 0x1 0x2 00:00:01:02:03:04 * eth0
my ( $ip, $mac ) = (
$line =~ m/
^
($ipv4_rx) # IP-addr
\s+ 0x\d+ \s+ 0x2 \s+
($mac_rx) # MAC-addr
\s+ .*
/x
);
( run in 2.751 seconds using v1.01-cache-2.11-cpan-5b529ec07f3 )