Welcome Guest, Not a member yet? Register   Sign In
Supporting IP6 when fetching and storing IP addresses
#1

[eluser]Xeoncross[/eluser]
I'm not sure how long we have until we make the jump to IP6, but the available IP4 addresses are supposed to be used up by 2011. So it make since that our apps should be ready to handle this new address types that our servers are already supporting.

For those of you unfamiler with IP6, it looks like this:
Code:
3ffe:1900:4545:3:200:f8ff:fe21:67cf

And can be abreivated in many ways:
Code:
2001:0db8:0000:0000:0000:0000:1428:57ab
2001:0db8:0000:0000:0000::1428:57ab
2001:0db8:0:0:0:0:1428:57ab
2001:0db8:0:0::1428:57ab
2001:0db8::1428:57ab
2001:db8::1428:57ab

Anyway, the current method we are using for IP checking is found on line 343 of the Input Class.
Code:
function ip_address()
    {
        if ($this->ip_address !== FALSE)
        {
            return $this->ip_address;
        }
        
        if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR'))
        {
            $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY);
            $proxies = is_array($proxies) ? $proxies : array($proxies);

            $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
        }
        elseif ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP'))
        {
            $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif ($this->server('REMOTE_ADDR'))
        {
            $this->ip_address = $_SERVER['REMOTE_ADDR'];
        }
        elseif ($this->server('HTTP_CLIENT_IP'))
        {
            $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif ($this->server('HTTP_X_FORWARDED_FOR'))
        {
            $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }

        if ($this->ip_address === FALSE)
        {
            $this->ip_address = '0.0.0.0';
            return $this->ip_address;
        }

        if (strstr($this->ip_address, ','))
        {
            $x = explode(',', $this->ip_address);
            $this->ip_address = trim(end($x));
        }

        if ( ! $this->valid_ip($this->ip_address))
        {
            $this->ip_address = '0.0.0.0';
        }

        return $this->ip_address;
    }

The problem is that is still the old IP4 format. Thanks to the inet_ntop() and inet_pton() we can convert either format into something that is easier to store. Here are some php functions for anyone interested.

So when is CodeIgniter going to support IP6 and what hurdles are there?
#2

[eluser]bretticus[/eluser]
I think because the current 2 billion Internet users at home with IPv4 addresses and NAT routers won't start complaining unless their ISP is forced to give them a IPv6 address (which will be awhile still) CI will probably handle it then.

Good to get us to start thinking though. I hope they come up with a better spec though. I don't like the idea of an IP address tied to my NIC (if that's true.)
#3

[eluser]Xeoncross[/eluser]
Oh, perhaps my understanding of the issue is incorrect. I thought that the transition was starting now with some servers changing to the IPv6 format while still accepting IPv4. You make it sound like everyone must make the jump on the same day.
#4

[eluser]bretticus[/eluser]
[quote author="Xeoncross" date="1252378983"]You make it sound like everyone must make the jump on the same day.[/quote]

It may be problematic. But mostly for businesses that do not have enough IPv4 addresses to support their growing customer base. The truth of the matter is, no one is going to worry about it until we run out (at least not consumers viewing websites.) When we have consumers that cannot view web pages because they have an IPv6 address, then we will have more software supporting IPv6. Sad but true...
#5

[eluser]bretticus[/eluser]
I would also bet that cheap ISPs to save money might even implement NAT in their network. I wouldn't use that ISP, but there's a lot of people who wouldn't understand the difference.
#6

[eluser]Xeoncross[/eluser]
I really don't like this whole idea of waiting until the day that it happens to make the switch. There must be some php best-practices or example code that is out there now...
#7

[eluser]bretticus[/eluser]
Funny I got bit by this today.

I use a mac to develop with (I'm somewhat new to using a mac too.) For some reason, my local address is ::1 instead of 127.0.0.1. I have a little debug helper in a certain project that checks to see if my ip is a debugger ip or not. well, I stuck in ::1 to my list, but the valid_ip method was returning false and ultimately 0.0.0.0 for me. So I rewrote valid_ip().

NOTE: This is not a fix for CI. It relies upon the filter_var() function which is PHP5 => 5.2.0. Also, if filter_var is not available, it goes back to ipv4 checkign only. I needed this to be quick (deadlines!)

However, I thought I'd pass it along as it may help you out (like me.)

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Input extends CI_Input
{
    
    /** Updated version suggested by Geert De Deckere (modified by Brett Millett)
    *
    * @access    public
    * @param    string
    * @return    string
    */

    function valid_ip($ip)
    {
            if ( function_exists('filter_var') ) { //PHP 5 >= 5.2.0 only.
                //check ipv4
                if( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== FALSE ) {
                    return TRUE;
                } elseif ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== FALSE ) {
                    return TRUE;
                } else {
                    return FALSE;
                }
            } else {
                // ipv4 only. todo implement ipv6 pattern matching
                $ip_segments = explode('.', $ip);

                // Always 4 segments needed
                if (count($ip_segments) != 4)
                {
                    return FALSE;
                }
                // IP can not start with 0
                if ($ip_segments[0][0] == '0')
                {
                    return FALSE;
                }
                // Check each segment
                foreach ($ip_segments as $segment)
                {
                    // IP segments must be digits and can not be
                    // longer than 3 digits or greater then 255
                    if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
                    {
                        return FALSE;
                    }
                }
            }

            return TRUE;
    }

}
?>
#8

[eluser]brianw1975[/eluser]
Since I don't use a Mac, let me ask this: can you force it to have 127.0.0.1 in your hosts file?
#9

[eluser]bretticus[/eluser]
No, it's not DNS. my loopback interface is bound to both 127.0.0.1 and ::1
Code:
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host

Unfortunately, for some reason Apache on my system returns ::1 for $_SERVER['REMOTE_ADDR'].




Theme © iAndrew 2016 - Forum software by © MyBB