a semi-functional bit of code... lots of todos and error checking
[PHPIPManager.git] / lib / ip.php
1 <?php
2
3 function ipversion($ip)
4 {
5         $ipv4 = ereg('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$', $ip);
6         $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip);
7         
8         error_log("ipversion, $ip, $ipv4, $ipv6");
9         if($ipv4 == 1) return 4;
10         if($ipv6 == 1) return 6;
11         return 0;
12 }
13
14 // this function tries to take a short representation of a ip6 address and converts it to a long one.
15 // this is not entirely going to work.. but we'll get back to this
16 function ip6ToLongStr($ip)
17 {
18         if(ipversion($ip)!=6) return false;
19         
20         $pss = explode(":", $ip);
21         $ns = count($pss);
22         
23         $nstr = "";
24         foreach($pss as $ele) {
25                 if($nstr != "") {
26                         $nstr .= ":";
27                 }
28                 if(strlen($ele < 1)) {
29                         // this is where we pad 8-count of 0000:
30                         $pds = 8-$ns+1;
31                         for($i=0; $i < $pds; $i++) {
32                                 $nstr .= "0000";
33                         }
34                 } else if(strlen($ele) < 4) {
35                         $nstr .= str_pad($ele, 4, "0", STR_PAD_LEFT);
36                 } else {
37                         $nstr .= $ele;
38                 }
39         }
40         return $nstr;
41 }
42
43 // this function converts an ip address to a comparable integer
44 // so we can bounds-check.
45 // it returns an array of [firstnum][lastnum]
46 function ip6ToComp($ip, $mask)
47 {
48         if($ipversion($ip)!=6) return false;
49         
50         $rip = ip6ToLongStr($ip);
51         
52 }
53
54 function ip4ToComp($ip, $mask)
55 {
56         
57 }
58
59 class ip {
60         
61         static function isValid($ip, $mask = 0)
62         {
63                 
64                 $ver = ipversion($ip);
65                 
66                 if($ver == 0) return 0;
67
68                 if($ver == 6) {
69                         if($mask != 0) {
70                                 if($mask%16 != 0) {
71                                         error_log("no");
72                                         return false;
73                                 } else {
74                                         $sns = $mask / 16;
75                                         
76                                         // now check that we have $sns number of subnets specified
77                                         $pss = explode(":", $ip);
78                                         $ns = count($pss);
79                                         
80                                         // we need to specify an error we can throw back at the user
81                                         if($ns < $sns) {
82                                                 error_log("no 2, $ns, $sns, $ip");
83                                                 print_r($pss);
84                                                 return false;
85                                         }
86                                         for($i = 0; $i < $sns; $i++) {
87                                                 if(strlen($pss[$i]) < 1) {
88                                                         error_log("no 3");
89                                                         return false;
90                                                 }
91                                         }
92                                         
93                                         // we are still a valid ipv6 ip address/mask
94                                         error_log("Valid");
95                                         return true;
96                                 }
97                         }
98                 }
99                 
100                 if($ver == 4) {
101                         if($mask != 0) {
102                                 return $ip;
103                         }
104                 }
105         }
106         
107         static function truncateNetwork($ip, $mask)
108         {
109                 $ver = ipversion($ip);
110                 
111                 if($ver == 0) return false;
112                 
113                 if($ver == 6) {
114                         if($mask != 0) {
115                                 if($mask%16 != 0) {
116                                         error_log("no");
117                                         return false;
118                                 } else {
119                                         $sns = $mask / 16;
120                                         
121                                         // now check that we have $sns number of subnets specified
122                                         $pss = explode(":", $ip);
123                                         $ns = count($pss);
124                                         
125                                         // we need to specify an error we can throw back at the user
126                                         if($ns < $sns) {
127                                                 error_log("no 2, $ns, $sns, $ip");
128                                                 print_r($pss);
129                                                 return false;
130                                         }
131                                         
132                                         $slt = "";
133                                         for($i = 0; $i < $sns; $i++) {
134                                                 if($i!=0) $slt .= ":";
135                                                 if(strlen($pss[$i]) < 1) {
136                                                         error_log("no 3");
137                                                         return false;
138                                                 }
139                                                 $slt .= "".$pss[$i];
140                                         }
141                                         
142                                         // we are still a valid ipv6 ip address/mask
143                                         error_log("Valid");
144                                         return $slt;
145                                 }
146                         }
147                 }
148                 
149                 if($ver == 4) {
150                         if($mask != 0) {
151                                 return $ip;
152                         }
153                 }
154         }
155         
156         function addSupernet($name, $sn, $mask, $desc)
157         {
158                 global $db;
159                 
160                 if(ip::isValid($sn, $mask)) {
161                         $sn = ip::truncateNetwork($sn, $mask);
162                         $sql = "insert into supernet values (NULL, '$name', '$sn', '$mask', '$desc')";
163                         $db->dbobject->query($sql);
164                         return true;
165                 }
166                 return false;
167         }
168         
169         function addSubnet($name, $subnet, $mask, $desc, $super)
170         {
171                 global $db;
172                 if(ip::isValid($subnet, $mask)) {
173                         $sn = ip::truncateNetwork($sn, $mask);
174                         // ("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
175                         //if(!isSubnet($subnet, $mask, $super)) return "Is not a valid subnet"; 
176                         
177                         $sql = "insert into subnet values (NULL, '$super', '$subnet', '$mask', '$name', '$desc')";
178                         error_log("sql: $sql");
179                         $db->dbobject->query($sql);
180                         return true;
181                 }
182         }
183         
184         function isSubnet($subnet, $mask, $superid)
185         {
186                 return true;
187         }
188         
189         // this function returns true if it is NOT conflicting
190         function isConflicting($subnet, $mask, $superid)
191         {
192                 return true;
193         }
194         
195         public $supers = "";
196         public $subs = "";
197 }
198 ?>