saintmike has asked for the wisdom of the Perl Monks concerning the following question:

Hey monks,

Net::Pcap provides a function lookupnet to query a host's network interface and to return its network and and netmask setting.

However, when running

use Net::Pcap; my $err; my($address, $netmask); my $dev = Net::Pcap::lookupdev(\$err); print "dev is $dev\n"; if(Net::Pcap::lookupnet($dev, \$address, \$netmask, \$err)) { die "Failed to look up dev info on $dev"; } print "address=$address netmask=$netmask\n";
the output is something like
dev is eth0 address=-1062731776 netmask=-256
Which format is this in? I've tried inet_ntoa and friends but can't seem to convert the result to the xx.xx.xx.xx format I'm looking for. Any ideas?

Replies are listed 'Best First'.
Re: Network/Netmask returned by Net::Pcap's lookupnet
by Fletch (Bishop) on Sep 07, 2004 at 01:54 UTC
    print join( ".", unpack( "C4", pack( "N", $addr ) ) ), "/", join( ".", unpack( "C4", pack( "N", $mask ) ) );
Re: Network/Netmask returned by Net::Pcap's lookupnet
by tachyon (Chancellor) on Sep 07, 2004 at 02:24 UTC

    I've tried inet_ntoa and friends

    Presumably you have lost the Network byte packing somewhere because there is no problem converting your quoted integers into realistic dot quads.....

    my $addr = pack "N", -1062731776; my $mask = pack "N", -256; use Socket; print inet_ntoa($addr),"/",inet_ntoa($mask),"\n"; sub perl_ntoa { join ".", unpack( "C4", $_[0] ) } print perl_ntoa($addr),"/",perl_ntoa($mask),"\n"; __DATA__ 192.168.0.0/255.255.255.0 192.168.0.0/255.255.255.0

    cheers

    tachyon

Re: Network/Netmask returned by Net::Pcap's lookupnet
by PodMaster (Abbot) on Sep 07, 2004 at 01:54 UTC
    Which format is this in?
    numbers:)? What does the pcap documentation say?

    update: Taking a cue from Fletch reveals (i knew to pack, but not what format):

    C:\>perl -MSocket -le"print inet_ntoa( pack 'N', -1062731776 )" 192.168.0.0 C:\>perl -MSocket -le"print inet_ntoa( pack 'N', -256 )" 255.255.255.0

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Network/Netmask returned by Net::Pcap's lookupnet
by NetWallah (Canon) on Sep 07, 2004 at 02:03 UTC
    Here is my code for this part of the initialization of pcap:
    my %pcap_parameters = ( SNAPLEN => 124, # Num bytes to capture from packet PROMISCUOUS_MODE => 1, # Operate in promiscuous mode? TIMEOUT => 1000, # Read timeout (ms) NUMPACKETS => 500, # Pkts to read (-1 = loop forever) #FILTER => 'ip proto \icmp', # Filter string FILTER => 'udp dst port 161', # Filter string USERDATA => '', # Passed as first arg to callback fn SAVEFILE => '', # Default save file # Items below are RETURNED values from PCap calls. # Do not attempt to change them in the declaration. FILTER_HANDLE => 0, # Reference to compiled filter NETWORK_INTERFACE => '',# Network interface to open NETWORK_ADDR =>0, # Network Address (32 bit number) NETWORK_MASK =>0, # Mask (32-bit number) mode => '', # Internal variable ); ... ... #Get the name of a network device that can be used $pcap_parameters{NETWORK_INTERFACE} = Net::Pcap::lookupdev(\$err) or + die "No Network device found:$err\n"; $verbose and print "Found device \t$pcap_parameters{NETWORK_INTERFAC +E}\n"; $result = Net::Pcap::lookupnet($pcap_parameters{NETWORK_INTERFACE}, \$pcap_parameters{NETWORK_ADDR}, \$pcap_parameters{NETWORK_MASK}, \$err); $verbose and print "Found Net \tnet " . NetPacket::IP::to_dotquad($p +cap_parameters{NETWORK_ADDR}) . " mask " . NetPacket::IP::to_dotquad($pcap_parameters{NETWORK +_MASK}) . "\n"; $pcap_desc = Net::Pcap::open_live($pcap_parameters{NETWORK_INTERFACE +}, $pcap_parameters{SNAPLEN}, $pcap_parameters{PROMISCUOUS_MODE}, $pcap_parameters{TIMEOUT}, \$err) or die("Net::Pcap::open_live returned error $err\n");

        Earth first! (We'll rob the other planets later)