in reply to unsigned int in the host format and raw socket

In Enumerating IP Addresses in a Subnet, I used:
sub parse_ipv4 { local *_ = \(@_ ? $_[0] : $_); return unpack('N', pack('C4', split(/\./))); }

Update: What follows is an alternative implementation that accepts domain names ("www.perlmonks.org") as well as IP addresses:

use Socket (); sub parse_ipv4 { local *_ = \(@_ ? $_[0] : $_); my $packed = Socket::inet_aton($_); return unless defined $packed; return unpack('N', $packed); }
print(parse_ipv4('202.227.44.16'), "\n"); # 3403885584

Replies are listed 'Best First'.
Re^2: unsigned int in the host format and raw socket
by doctor_moron (Scribe) on Jun 30, 2005 at 18:32 UTC
    Thanks for the quickest reply

    Yes it's work, but please look at this code (i got this code from Net::Raw Example)

    $| = 1; require 'getopts.pl'; use Net::RawIP; Getopts('t:'); die "Usage $0 -t <target>" unless $opt_t; srand(time); $i = 996; $data .= chr(int rand(255)),$i-- while($i); $icmp = new Net::RawIP({ ip => { ihl => 6, tot_len => 1024, id => 1, ttl => 255, frag_off => 0, daddr => $opt_t }, icmp => { id => 2650, data => $data } }); for(;;){ $j++; $icmp->set({ ip => { saddr => 17000000 + int rand 4261000000 }, icmp => { type => int rand(14), code => int rand(10), sequence => int rand(255) } }); $icmp->send; print "b00m " unless $j%1000; }
    Have a look in looping area, we got :
    ip => { saddr => 17000000 + int rand 4261000000 }

    The author give $addr with random int number without doing packing or unpack, so i make my own conclusion that we can give $addr with int number (like code above) as long as it's valid ip addy, is it true?

    i m trying hard to explain my problem in proper english i hope you can understand

    thanks, zak

      An IP (v4) address is any number between 0 and 4294967295 inclusively. Different interfaces expect the address to be in different formats (reprentations). "aaa.bbb.ccc.ddd" is just one format. Net::RawIP works with a number of formats, including numerical (0 to 4294967295) and dotted ('aaa.bbb.ccc.ddd').

      For example, two following two lines are identical:

      ip => { saddr => '202.227.44.16' } ip => { saddr => 3403885584 }

      I hope that answers your question; I don't quite understand it.

        Eureka...Yes! this is what i need, u got me, thank you