Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Net::IP convert ip to int

by Anonymous Monk
on Jun 23, 2012 at 06:19 UTC ( #977941=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I've the code below to try and get the integer value of an IP.

my $netip = new Net::IP ($ip) or die (Net::IP::Error()); my $intip = $netip->intip(); # output $intip # bless( { 'value' => [ 5050079, 291 ], 'sign' => '+' }, 'Math::BigInt +' );

What does the second number 291 represent? I've run the code with different IPs and it seems similar ips have slightly different first integer values but the same second value.

Comment on Net::IP convert ip to int
Download Code
Re: Net::IP convert ip to int
by Anonymous Monk on Jun 23, 2012 at 06:44 UTC

    What does the second number 291 represent?

    It doesn't matter, you're only interested in the stringified version (no bless stuff), so use

    print ("Int : ".$ip->intip()."\n");
    To see the integer
Re: Net::IP convert ip to int
by Khen1950fx (Canon) on Jun 23, 2012 at 10:27 UTC
    The value of 291 seems awfully high. Most of the ip's that I work with have 0, 1, or 2, sometimes more. They refer to the number of prefixes, "/nn", for point-to-point networks. The + sign means that it has been signed with an extra 32-bit integer, again, for point-to-point broadcast purposes. It may make more sense if we look at the whole bundle of methods.
    #!/usr/bin/perl -l use strict; use warnings; use Net::IP qw/:PROC/; use Data::Dumper::Concise; my $netip = new Net::IP ('157.166.255.18') or die (Net::IP::Error()); print Dumper( "IP : " . $netip->ip(), "Sho : " . $netip->short(), "Bin : " . $netip->binip(), "Int : " . $netip->intip(), "Mask : " . $netip->mask(), "Last : " . $netip->last_ip(), "Len : " . $netip->prefixlen(), "Size : " . $netip->size(), "Type : " . $netip->iptype(), "Rev : " . $netip->reverse_ip(), "Pre : " . $netip->is_prefix(), ); print Dumper( $netip );
    You'll be able to get more information from a post by the author: Managing your IP space with Perl.
Re: Net::IP convert ip to int
by Anonymous Monk on Jun 23, 2012 at 14:30 UTC

    Thanks for your kind replies.

    Actually, what I wanted to do was to be able to tell if two ore more IPs coming from the same user belong to the same IP range (not sure what the technical name is). That information will indicate to me that at least this user is who he is (not someone from a different IP who has stolen his connection session).

    Is there a way to do that? Is Net::IP the right module to use?

      That information will indicate to me that at least this user is who he is (not someone from a different IP who has stolen his connection session).

      That is an unreliable technique -- there is no guarantee, if the users IP changes, that his new IP will be from the same block

      As another monk told you, assuming the user alsways gets an IP from the same block is a dangerous road. It wont work for most DSL links and certainly not for mobile devices like tablets.

      Even assuming the user stays in the same building (for example a company), when unplugging or plugging an ethernet cable, many devices automatically switch from WLAN to LAN or vice versa. And, on a reasonably big network, these might be in different IP ranges.

      Also, you can not assume that it's "safe" if the IP changed but stayed in the same IP range. Many script kiddies are just that: Young people, often living or working in a small or large group together. You know, pupils, students, apprentice PerlMonks, and so on. And often enough, they are very... enthusiastic... about their skills while still learning the more intricate details of right and wrong... ;-)

      On the other hand, multiple users may share a single IP adress because they are behind a NAT or using a SSH connection or other tunnel to the same server. Or, for HTTP, they might use the same proxy.

      Your method also ultimatively will fail rather sooner than later from a simple, mathematical standpoint: We are running out of IPv4 adresses and more and more networks start to enable IPv6. For this, you need another set of calculations.

      Finally, you can not even rely on an unchanged IP. With access to the users local network, it's usually not that hard to knock his/her computer of the net and re-use the IP for ones own purposes for a short time. That's why cryptography was invented. You know, SSL and stuff.

      (Not that anything i just said really matters for the "wrong" kind of users. I know a number people who run a pirated, unpatched Version of XP. Without AntiVirus of course. But with IE6 that came pre-installed. Only two of them have learned their lesson in the last year, both got their online banking access hijacked.)

      "You have reached the Monastery. All our helpdesk monks are busy at the moment. Please press "1" to instantly donate 10 currency units for a good cause or press "2" to hang up. Or you can dial "12" to get connected directly to second level support."

        In the old days of the WWW, AOL users were forced to use HTTP proxies provided by AOL. The setup had the annoying feature of using a different proxy for each request, so that the requests from a single user appeared to come from a large set of very different IP addresses, while each single IP address was used by a large set of users. Websites that assumed constant client IP addresses were unusable, websites that assumed one user per client IP address had huge security issues. I don't know if AOL still uses this setup, things have changed a lot since then. But as far as I know, this setup does not violate a single RFC, and websites that can't handle this setup are broken, period.

        HTTP Proxies have become more and more popular, especially in corporate environments; mainly for security and for filtering / censoring unwanted content (mainly malware and porn sites). Many mobile internet providers offer a low cost connection that uses private IP addresses for the mobile device and a forced, transparent HTTP proxy to reduce the data volume in their mobile networks. But unlike AOL, they seem to use only a single proxy, or at least try not to change the proxy during a dialup session.

        Big internet providers have several IPv4 address pools, simply because they had to request a new block from time to time. Those pools are not continuous, but fragmented. Dial-up (and DSL) clients get a random IP address from the pool, and with each new dial-up, the IP address may change wildly. Some providers use several small regional pools with perhaps two, three or four IP ranges, some have use a large common pool for the entire state with a large number of IP ranges.

        At least in Germany, DSL connections are disconnected after 24 hours, and usually, the IP address changes after the reconnect. Fixed IPv4 addresses are available for extra money.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://977941]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-09-02 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (20 votes), past polls