[PHP] IPv6 lookup in prefix function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [PHP] IPv6 lookup in prefix function
# 1  
Old 05-19-2005
[PHP] IPv6 lookup in prefix function

Hi there,

Was wondering if you could help me out again. In my efforts to write a function to check whether an IPv6 address is part of an IPv6 prefix, I've come up with this:

PHP Code:
function isIPIn ($ip,$net,$mask
{
    
    
$net pack("H" strlen($net), str_replace(":"""$net));
    
$ip pack("H" strlen($ip), str_replace(":"""$ip));
    
$binnet str_pad ($net128"0""STR_PAD_LEFT");
    
$firstpart substr($binnet0$mask);
    
$binip str_pad ($ip128"0""STR_PAD_LEFT");    
    
$firstip substr ($binip0$mask);    
    return (
strcmp ($firstpart$firstip) == 0);
    

A number of things I'd like to clarify:
1. This code is based on a similar function I found dealing with IPv4 adresses.
2. I haven't taken into account the compressed format of IPv6 addresses yet. I'll get to that when I get this working. Smilie
3. To give a quick explanation of what I hope and think I'm doing in the function: I first try to convert the IPv6 hexadecimal address to binary.
4. I then fill it up to 128 bits (at least I think, but I'm actually not really sure if I need to)
5. Finally I compare chunks of the two addresses (based on the mask) to see if they match up. This actually checks whether the IPv6 address actually matches the prefix, I hope.

When I try the following:
PHP Code:
<?
$ip = "2000:0610:013A:0000:0000:0000:0000:0001";
$net = "2001:0610:013A:0000:0000:0000:0000:0000";
$mask = "48";

echo isIPIn ($ip, $net, $mask);
?>
it returns 1 (true), which it shouldn't (or should it?).
If someone could help me out here to see if my logic is right, that would be great. In any case, any help is greatly appreciated!
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

2. UNIX for Dummies Questions & Answers

Assigning ipv6 to bonding interface - getting old as well as changed ipv6 in ifconfig output

Hi, I have created a bonding bond1 interface with 6 Eth , mode=4. Recently i have changed my old ipv6 to new one and tried to restart as well as reload network service. Post which i can see old as well as changed ipv6 in ifconfig command output. Below are few files and command output for your... (1 Reply)
Discussion started by: omkar.jadhav
1 Replies

3. Shell Programming and Scripting

PHP populate variable with function

Hi, I've a php scirpt that calls in an external class. The external class, myClass, has several public and private functions. There is one public function which derives a value that I want be able to bring it to the main php scirpt. Here is the code. ....snip.... include... (1 Reply)
Discussion started by: nitin
1 Replies

4. UNIX for Dummies Questions & Answers

Split Function Prefix Numbers

Hello, Hello, I use the following command to split a file: split -Number_of_Lines Input_File MyPrefix_ output is MyPrefix_a MyPrefix_b MyPrefix_c ...... Instead, how can I get numerical values like: MyPrefix_1 MyPrefix_2 MyPrefix_3 ...... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

5. Shell Programming and Scripting

PHP function in a shell script

Hi. I am writing a shell script for Ubuntu linux. Eventually I want my script to run every 10 mins via cron. This is what I am trying to do: php file: connect to mysql database get the last row input check date against previously saved date (on last run) if date>previous return... (4 Replies)
Discussion started by: daydreamer
4 Replies

6. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies

7. Shell Programming and Scripting

PHP function kills my code?

So I'm making a page for one of my web sites that scans the visitors ports (just a few specific ones, not ALL) and tells them if they have any open. Now I made up the code and it worked great. Now instead of having the same chunk of code all over my script, I tried to make my own subroutine so I... (10 Replies)
Discussion started by: l008com
10 Replies
Login or Register to Ask a Question