* Changes in IP.php:
[lhc/web/wiklou.git] / includes / IP.php
1 <?php
2 /**
3 * Functions and constants to play with IP addresses and ranges
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Ashar Voultoiz <hashar at free dot fr>, Aaron Schulz
22 */
23
24 // Some regex definition to "play" with IP address and IP address blocks
25
26 // An IP is made of 4 bytes from x00 to xFF which is d0 to d255
27 define( 'RE_IP_BYTE', '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])' );
28 define( 'RE_IP_ADD' , RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE );
29 // An IPv4 block is an IP address and a prefix (d1 to d32)
30 define( 'RE_IP_PREFIX', '(3[0-2]|[12]?\d)' );
31 define( 'RE_IP_BLOCK', RE_IP_ADD . '\/' . RE_IP_PREFIX );
32 // For IPv6 canonicalization (NOT for strict validation; these are quite lax!)
33 define( 'RE_IPV6_WORD', '([0-9A-Fa-f]{1,4})' );
34 define( 'RE_IPV6_GAP', ':(?:0+:)*(?::(?:0+:)*)?' );
35 define( 'RE_IPV6_V4_PREFIX', '0*' . RE_IPV6_GAP . '(?:ffff:)?' );
36 // An IPv6 block is an IP address and a prefix (d1 to d128)
37 define( 'RE_IPV6_PREFIX', '(12[0-8]|1[01][0-9]|[1-9]?\d)');
38 // An IPv6 address is made up of 8 octets. However, the "::" abbreviations can be used.
39 define( 'RE_IPV6_ADD',
40 '(' . // starts with "::" (includes the address "::")
41 '(::|:(:' . RE_IPV6_WORD . '){1,7})' .
42 '|' . // ends with "::" (not including the address "::")
43 RE_IPV6_WORD . '(:' . RE_IPV6_WORD . '){0,6}::' .
44 '|' . // has no "::"
45 RE_IPV6_WORD . '(:' . RE_IPV6_WORD . '){7}' .
46 '|' . // contains one "::" in the middle ("^" check always fails if no "::" found)
47 RE_IPV6_WORD . '(:(?P<abbr>(?(abbr)|:))?' . RE_IPV6_WORD . '){1,6}(?(abbr)|^)' .
48 ')'
49 );
50 define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX );
51 // This might be useful for regexps used elsewhere, matches any IPv6 or IPv6 address or network
52 define( 'IP_ADDRESS_STRING',
53 '(?:' .
54 RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)' . // IPv4
55 '|' .
56 RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)' . // IPv6
57 ')'
58 );
59
60 /**
61 * A collection of public static functions to play with IP address
62 * and IP blocks.
63 */
64 class IP {
65 /**
66 * Given a string, determine if it as valid IP.
67 * Note: Unlike isValid(), this looks for networks too.
68 * @param string $ip possible IP address
69 * @return bool
70 */
71 public static function isIPAddress( $ip ) {
72 return (bool)preg_match( '/^' . IP_ADDRESS_STRING . '$/', $ip );
73 }
74
75 /**
76 * Given a string, determine if it as valid IP in IPv6 only.
77 * Note: Unlike isValid(), this looks for networks too.
78 * @param string $ip possible IP address
79 * @return bool
80 */
81 public static function isIPv6( $ip ) {
82 return (bool)preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip );
83 }
84
85 /**
86 * Given a string, determine if it as valid IP in IPv4 only.
87 * Note: Unlike isValid(), this looks for networks too.
88 * @param string $ip possible IP address
89 * @return bool
90 */
91 public static function isIPv4( $ip ) {
92 return (bool)preg_match( '/^' . RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)$/', $ip );
93 }
94
95 /**
96 * Given an IP address in dotted-quad notation, returns an IPv6 octet.
97 * See http://www.answers.com/topic/ipv4-compatible-address
98 * IPs with the first 92 bits as zeros are reserved from IPv6
99 * @param string $ip quad-dotted IP address.
100 * @return string
101 */
102 public static function IPv4toIPv6( $ip ) {
103 if ( !$ip ) {
104 return null;
105 }
106 // Convert only if needed
107 if ( self::isIPv6( $ip ) ) {
108 return $ip;
109 }
110 // IPv4 address with CIDR
111 if ( strpos( $ip, '/' ) !== false ) {
112 $parts = explode( '/', $ip, 2 );
113 if ( count( $parts ) != 2 ) {
114 return false;
115 }
116 list( $network, $bits ) = $parts;
117 $network = self::toUnsigned( $network );
118 if ( $network !== false && is_numeric( $bits ) && $bits >= 0 && $bits <= 32 ) {
119 $bits += 96;
120 return self::toOctet( $network ) . "/$bits";
121 } else {
122 return false;
123 }
124 }
125 return self::toOctet( self::toUnsigned( $ip ) );
126 }
127
128 private static function toUnsigned6( $ip ) {
129 if ( self::isIPv6( $ip ) ) {
130 return wfBaseConvert( self::IPv6ToRawHex( $ip ), 16, 10 );
131 }
132 return false;
133 }
134
135 /**
136 * Convert an IP into a nice standard form.
137 * IPv6 addresses in octet notation are expanded to 8 octets.
138 * IPv4 addresses are just trimmed.
139 * @param string $ip IP address in quad or octet form (CIDR or not).
140 * @return string
141 */
142 public static function sanitizeIP( $ip ) {
143 $ip = trim( $ip );
144 if ( $ip === '' ) {
145 return null;
146 }
147 if ( self::isIPv4( $ip ) || !self::isIPv6( $ip ) ) {
148 return $ip; // nothing else to do for IPv4 addresses or invalid ones
149 }
150 // Remove any whitespaces, convert to upper case
151 $ip = strtoupper( $ip );
152 // Expand zero abbreviations
153 $abbrevPos = strpos( $ip, '::' );
154 if ( $abbrevPos !== false ) {
155 // We know this is valid IPv6. Find the last index of the
156 // address before any CIDR number (e.g. "a:b:c::/24").
157 $CIDRStart = strpos( $ip, "/" );
158 $addressEnd = ( $CIDRStart !== false )
159 ? $CIDRStart - 1
160 : strlen( $ip ) - 1;
161 // If the '::' is at the beginning...
162 if( $abbrevPos == 0 ) {
163 $repeat = '0:';
164 $extra = ( $ip == '::' ) ? '0' : ''; // for the address '::'
165 $pad = 9; // 7+2 (due to '::')
166 // If the '::' is at the end...
167 } elseif( $abbrevPos == ( $addressEnd - 1 ) ) {
168 $repeat = ':0';
169 $extra = '';
170 $pad = 9; // 7+2 (due to '::')
171 // If the '::' is in the middle...
172 } else {
173 $repeat = ':0';
174 $extra = ':';
175 $pad = 8; // 6+2 (due to '::')
176 }
177 $ip = str_replace( '::',
178 str_repeat( $repeat, $pad - substr_count( $ip, ':' ) ) . $extra,
179 $ip
180 );
181 }
182 // Remove leading zereos from each bloc as needed
183 $ip = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip );
184 return $ip;
185 }
186
187 /**
188 * Given an unsigned integer, returns an IPv6 address in octet notation
189 * @param string $ip_int IP address.
190 * @return string
191 */
192 public static function toOctet( $ip_int ) {
193 $ip_hex = wfBaseConvert( $ip_int, 10, 16, 32, false ); // uppercase hex
194 return self::hexToOctet( $ip_hex );
195 }
196
197 /**
198 * Convert an IPv4 or IPv6 hexadecimal representation back to readable format
199 * @param string $hex number, with "v6-" prefix if it is IPv6
200 * @return string quad-dotted (IPv4) or octet notation (IPv6)
201 */
202 public static function formatHex( $hex ) {
203 if ( substr( $hex, 0, 3 ) == 'v6-' ) { // IPv6
204 return self::hexToOctet( substr( $hex, 3 ) );
205 } else { // IPv4
206 return self::hexToQuad( $hex );
207 }
208 }
209
210 /**
211 * Converts a hexadecimal number to an IPv6 address in octet notation
212 * @param string $ip_hex pure hex (no v6- prefix)
213 * @return string (of format a:b:c:d:e:f:g:h)
214 */
215 public static function hexToOctet( $ip_hex ) {
216 // Convert to padded uppercase hex
217 $ip_hex = str_pad( strtoupper( $ip_hex ), 32, '0', STR_PAD_LEFT );
218 // Separate into 8 octets
219 $ip_oct = substr( $ip_hex, 0, 4 );
220 for ( $n = 1; $n < 8; $n++ ) {
221 $ip_oct .= ':' . substr( $ip_hex, 4 * $n, 4 );
222 }
223 // NO leading zeroes
224 $ip_oct = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip_oct );
225 return $ip_oct;
226 }
227
228 /**
229 * Converts a hexadecimal number to an IPv4 address in quad-dotted notation
230 * @param string $ip_hex pure hex
231 * @return string (of format a.b.c.d)
232 */
233 public static function hexToQuad( $ip_hex ) {
234 // Convert to padded uppercase hex
235 $ip_hex = str_pad( strtoupper( $ip_hex ), 8, '0', STR_PAD_LEFT );
236 // Separate into four quads
237 $s = '';
238 for ( $i = 0; $i < 4; $i++ ) {
239 if ( $s !== '' ) {
240 $s .= '.';
241 }
242 $s .= base_convert( substr( $ip_hex, $i * 2, 2 ), 16, 10 );
243 }
244 return $s;
245 }
246
247 /**
248 * Convert a network specification in IPv6 CIDR notation to an
249 * integer network and a number of bits
250 * @return array(string, int)
251 */
252 private static function parseCIDR6( $range ) {
253 # Explode into <expanded IP,range>
254 $parts = explode( '/', IP::sanitizeIP( $range ), 2 );
255 if ( count( $parts ) != 2 ) {
256 return array( false, false );
257 }
258 list( $network, $bits ) = $parts;
259 $network = self::IPv6ToRawHex( $network );
260 if ( $network !== false && is_numeric( $bits ) && $bits >= 0 && $bits <= 128 ) {
261 if ( $bits == 0 ) {
262 $network = "0";
263 } else {
264 # Native 32 bit functions WONT work here!!!
265 # Convert to a padded binary number
266 $network = wfBaseConvert( $network, 16, 2, 128 );
267 # Truncate the last (128-$bits) bits and replace them with zeros
268 $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
269 # Convert back to an integer
270 $network = wfBaseConvert( $network, 2, 10 );
271 }
272 } else {
273 $network = false;
274 $bits = false;
275 }
276 return array( $network, (int)$bits );
277 }
278
279 /**
280 * Given a string range in a number of formats, return the
281 * start and end of the range in hexadecimal. For IPv6.
282 *
283 * Formats are:
284 * 2001:0db8:85a3::7344/96 CIDR
285 * 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range
286 * 2001:0db8:85a3::7344/96 Single IP
287 * @return array(string, int)
288 */
289 private static function parseRange6( $range ) {
290 # Expand any IPv6 IP
291 $range = IP::sanitizeIP( $range );
292 // CIDR notation...
293 if ( strpos( $range, '/' ) !== false ) {
294 list( $network, $bits ) = self::parseCIDR6( $range );
295 if ( $network === false ) {
296 $start = $end = false;
297 } else {
298 $start = wfBaseConvert( $network, 10, 16, 32, false );
299 # Turn network to binary (again)
300 $end = wfBaseConvert( $network, 10, 2, 128 );
301 # Truncate the last (128-$bits) bits and replace them with ones
302 $end = str_pad( substr( $end, 0, $bits ), 128, 1, STR_PAD_RIGHT );
303 # Convert to hex
304 $end = wfBaseConvert( $end, 2, 16, 32, false );
305 # see toHex() comment
306 $start = "v6-$start";
307 $end = "v6-$end";
308 }
309 // Explicit range notation...
310 } elseif ( strpos( $range, '-' ) !== false ) {
311 list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) );
312 $start = self::toUnsigned6( $start );
313 $end = self::toUnsigned6( $end );
314 if ( $start > $end ) {
315 $start = $end = false;
316 } else {
317 $start = wfBaseConvert( $start, 10, 16, 32, false );
318 $end = wfBaseConvert( $end, 10, 16, 32, false );
319 }
320 # see toHex() comment
321 $start = "v6-$start";
322 $end = "v6-$end";
323 } else {
324 # Single IP
325 $start = $end = self::toHex( $range );
326 }
327 if ( $start === false || $end === false ) {
328 return array( false, false );
329 } else {
330 return array( $start, $end );
331 }
332 }
333
334 /**
335 * Validate an IP address. Ranges are NOT considered valid.
336 * @param string $ip
337 * @return boolean True if it is valid.
338 */
339 public static function isValid( $ip ) {
340 return ( preg_match( '/^' . RE_IP_ADD . '$/', $ip )
341 || preg_match( '/^' . RE_IPV6_ADD . '$/', $ip ) );
342 }
343
344 /**
345 * Validate an IP Block (valid address WITH a valid prefix).
346 * @param string $ipblock
347 * @return boolean True if it is valid.
348 */
349 public static function isValidBlock( $ipblock ) {
350 return ( preg_match( '/^' . RE_IPV6_BLOCK . '$/', $ipblock )
351 || preg_match( '/^' . RE_IPV4_BLOCK . '$/', $ipblock ) );
352 }
353
354 /**
355 * Determine if an IP address really is an IP address, and if it is public,
356 * i.e. not RFC 1918 or similar
357 * Comes from ProxyTools.php
358 * @param string $ip
359 * @return bool
360 */
361 public static function isPublic( $ip ) {
362 if ( self::isIPv6( $ip ) ) {
363 return self::isPublic6( $ip );
364 }
365 $n = self::toUnsigned( $ip );
366 if ( !$n ) {
367 return false;
368 }
369
370 // ip2long accepts incomplete addresses, as well as some addresses
371 // followed by garbage characters. Check that it's really valid.
372 if( $ip != long2ip( $n ) ) {
373 return false;
374 }
375
376 static $privateRanges = false;
377 if ( !$privateRanges ) {
378 $privateRanges = array(
379 array( '10.0.0.0', '10.255.255.255' ), # RFC 1918 (private)
380 array( '172.16.0.0', '172.31.255.255' ), # "
381 array( '192.168.0.0', '192.168.255.255' ), # "
382 array( '0.0.0.0', '0.255.255.255' ), # this network
383 array( '127.0.0.0', '127.255.255.255' ), # loopback
384 );
385 }
386
387 foreach ( $privateRanges as $r ) {
388 $start = self::toUnsigned( $r[0] );
389 $end = self::toUnsigned( $r[1] );
390 if ( $n >= $start && $n <= $end ) {
391 return false;
392 }
393 }
394 return true;
395 }
396
397 /**
398 * Determine if an IPv6 address really is an IP address, and if it is public,
399 * i.e. not RFC 4193 or similar
400 * @param string $ip
401 * @return bool
402 */
403 private static function isPublic6( $ip ) {
404 static $privateRanges = false;
405 if ( !$privateRanges ) {
406 $privateRanges = array(
407 array( 'fc::', 'fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ), # RFC 4193 (local)
408 array( '0:0:0:0:0:0:0:1', '0:0:0:0:0:0:0:1' ), # loopback
409 );
410 }
411 $n = self::toHex( $ip );
412 foreach ( $privateRanges as $r ) {
413 $start = self::toHex( $r[0] );
414 $end = self::toHex( $r[1] );
415 if ( $n >= $start && $n <= $end ) {
416 return false;
417 }
418 }
419 return true;
420 }
421
422 /**
423 * Return a zero-padded upper case hexadecimal representation of an IP address.
424 *
425 * Hexadecimal addresses are used because they can easily be extended to
426 * IPv6 support. To separate the ranges, the return value from this
427 * function for an IPv6 address will be prefixed with "v6-", a non-
428 * hexadecimal string which sorts after the IPv4 addresses.
429 *
430 * @param string $ip Quad dotted/octet IP address.
431 * @return string
432 */
433 public static function toHex( $ip ) {
434 if ( self::isIPv6( $ip ) ) {
435 $n = 'v6-' . self::IPv6ToRawHex( $ip );
436 } else {
437 $n = self::toUnsigned( $ip );
438 if ( $n !== false ) {
439 $n = wfBaseConvert( $n, 10, 16, 8, false );
440 }
441 }
442 return $n;
443 }
444
445 /**
446 * Given an IP address in dotted-quad/octet notation, returns an unsigned integer.
447 * Like ip2long() except that it actually works and has a consistent error return value.
448 * Comes from ProxyTools.php
449 * @param string $ip Quad dotted IP address.
450 * @return mixed (string/int/false)
451 */
452 public static function toUnsigned( $ip ) {
453 if ( self::isIPv6( $ip ) ) {
454 $n = wfBaseConvert( self::IPv6ToRawHex( $ip ), 16, 10 );
455 } else {
456 if ( $ip == '255.255.255.255' ) {
457 $n = -1;
458 } else {
459 $n = ip2long( $ip );
460 if ( $n == -1 || $n === false ) { # Return value on error depends on PHP version
461 $n = false;
462 }
463 }
464 if ( $n < 0 ) {
465 $n += pow( 2, 32 );
466 }
467 }
468 return $n;
469 }
470
471 /**
472 * Given an IPv6 address in octet notation, returns a pure hex string.
473 * @param string $ip octet ipv6 IP address.
474 * @return string hex (uppercase)
475 */
476 private static function IPv6ToRawHex( $ip ) {
477 $ip = self::sanitizeIP( $ip );
478 if ( !$ip ) {
479 return null;
480 }
481 $r_ip = '';
482 foreach ( explode( ':', $ip ) as $v ) {
483 $r_ip .= str_pad( $v, 4, 0, STR_PAD_LEFT );
484 }
485 return $r_ip;
486 }
487
488 /**
489 * Convert a dotted-quad IP to a signed integer
490 * @param string $ip
491 * @return mixed (string/false)
492 */
493 public static function toSigned( $ip ) {
494 if ( $ip == '255.255.255.255' ) {
495 $n = -1;
496 } else {
497 $n = ip2long( $ip );
498 if ( $n == -1 ) {
499 $n = false;
500 }
501 }
502 return $n;
503 }
504
505 /**
506 * Convert a network specification in CIDR notation to an integer network and a number of bits
507 * @param string $range (CIDR IP)
508 * @return array(int, int)
509 */
510 public static function parseCIDR( $range ) {
511 $parts = explode( '/', $range, 2 );
512 if ( count( $parts ) != 2 ) {
513 return array( false, false );
514 }
515 list( $network, $bits ) = $parts;
516 $network = self::toSigned( $network );
517 if ( $network !== false && is_numeric( $bits ) && $bits >= 0 && $bits <= 32 ) {
518 if ( $bits == 0 ) {
519 $network = 0;
520 } else {
521 $network &= ~( ( 1 << ( 32 - $bits ) ) - 1);
522 }
523 # Convert to unsigned
524 if ( $network < 0 ) {
525 $network += pow( 2, 32 );
526 }
527 } else {
528 $network = false;
529 $bits = false;
530 }
531 return array( $network, $bits );
532 }
533
534 /**
535 * Given a string range in a number of formats, return the start and end of
536 * the range in hexadecimal.
537 *
538 * Formats are:
539 * 1.2.3.4/24 CIDR
540 * 1.2.3.4 - 1.2.3.5 Explicit range
541 * 1.2.3.4 Single IP
542 *
543 * 2001:0db8:85a3::7344/96 CIDR
544 * 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range
545 * 2001:0db8:85a3::7344 Single IP
546 * @param string $range IP range
547 * @return array(string, int)
548 */
549 public static function parseRange( $range ) {
550 // Use IPv6 functions if needed
551 if ( self::isIPv6( $range ) ) {
552 return self::parseRange6( $range );
553 }
554 if ( strpos( $range, '/' ) !== false ) {
555 # CIDR
556 list( $network, $bits ) = self::parseCIDR( $range );
557 if ( $network === false ) {
558 $start = $end = false;
559 } else {
560 $start = sprintf( '%08X', $network );
561 $end = sprintf( '%08X', $network + pow( 2, ( 32 - $bits ) ) - 1 );
562 }
563 } elseif ( strpos( $range, '-' ) !== false ) {
564 # Explicit range
565 list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) );
566 if( self::isIPAddress( $start ) && self::isIPAddress( $end ) ) {
567 $start = self::toUnsigned( $start );
568 $end = self::toUnsigned( $end );
569 if ( $start > $end ) {
570 $start = $end = false;
571 } else {
572 $start = sprintf( '%08X', $start );
573 $end = sprintf( '%08X', $end );
574 }
575 } else {
576 $start = $end = false;
577 }
578 } else {
579 # Single IP
580 $start = $end = self::toHex( $range );
581 }
582 if ( $start === false || $end === false ) {
583 return array( false, false );
584 } else {
585 return array( $start, $end );
586 }
587 }
588
589 /**
590 * Determine if a given IPv4/IPv6 address is in a given CIDR network
591 * @param $addr The address to check against the given range.
592 * @param $range The range to check the given address against.
593 * @return bool Whether or not the given address is in the given range.
594 */
595 public static function isInRange( $addr, $range ) {
596 $hexIP = self::toHex( $addr );
597 list( $start, $end ) = self::parseRange( $range );
598 return ( strcmp( $hexIP, $start ) >= 0 &&
599 strcmp( $hexIP, $end ) <= 0 );
600 }
601
602 /**
603 * Convert some unusual representations of IPv4 addresses to their
604 * canonical dotted quad representation.
605 *
606 * This currently only checks a few IPV4-to-IPv6 related cases. More
607 * unusual representations may be added later.
608 *
609 * @param $addr something that might be an IP address
610 * @return valid dotted quad IPv4 address or null
611 */
612 public static function canonicalize( $addr ) {
613 if ( self::isValid( $addr ) ) {
614 return $addr;
615 }
616
617 // Turn mapped addresses from ::ce:ffff:1.2.3.4 to 1.2.3.4
618 if ( strpos( $addr, ':' ) !== false && strpos( $addr, '.' ) !== false ) {
619 $addr = substr( $addr, strrpos( $addr, ':' ) + 1 );
620 if( self::isIPv4( $addr ) ) {
621 return $addr;
622 }
623 }
624
625 // IPv6 loopback address
626 $m = array();
627 if ( preg_match( '/^0*' . RE_IPV6_GAP . '1$/', $addr, $m ) ) {
628 return '127.0.0.1';
629 }
630
631 // IPv4-mapped and IPv4-compatible IPv6 addresses
632 if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . '(' . RE_IP_ADD . ')$/i', $addr, $m ) ) {
633 return $m[1];
634 }
635 if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . RE_IPV6_WORD . ':' . RE_IPV6_WORD . '$/i', $addr, $m ) ) {
636 return long2ip( ( hexdec( $m[1] ) << 16 ) + hexdec( $m[2] ) );
637 }
638
639 return null; // give up
640 }
641 }