Rev 2 | Rev 13 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2 | Rev 5 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * IP resolution functions |
4 | * IP resolution functions |
5 | * Copyright 2012 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2012 Daniel Marschall, ViaThinkSoft |
6 | * Version 2012-02-02 |
6 | * Version 2012-02-02 |
7 | * |
7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | * you may not use this file except in compliance with the License. |
9 | * you may not use this file except in compliance with the License. |
10 | * You may obtain a copy of the License at |
10 | * You may obtain a copy of the License at |
11 | * |
11 | * |
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | * |
13 | * |
14 | * Unless required by applicable law or agreed to in writing, software |
14 | * Unless required by applicable law or agreed to in writing, software |
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | * See the License for the specific language governing permissions and |
17 | * See the License for the specific language governing permissions and |
18 | * limitations under the License. |
18 | * limitations under the License. |
19 | */ |
19 | */ |
20 | 20 | ||
21 | /* -- Testcases -- |
21 | /* -- Testcases -- |
22 | print_r(gethostbynamel6('example.com')); |
22 | print_r(gethostbynamel6('example.com')); |
23 | print_r(gethostbynamel6('ipv6.google.com')); |
23 | print_r(gethostbynamel6('ipv6.google.com')); |
24 | print_r(gethostbynamel6('google.de')); |
24 | print_r(gethostbynamel6('google.de')); |
25 | print_r(gethostbynamel6('ipv6.google.de')); |
25 | print_r(gethostbynamel6('ipv6.google.de')); |
26 | print_r(gethostbynamel6('abc')); |
26 | print_r(gethostbynamel6('abc')); |
27 | print_r(gethostbynamel6('111.111.111.111')); |
27 | print_r(gethostbynamel6('111.111.111.111')); |
28 | print_r(gethostbynamel6('2620::2d0:200:0:0:0:10')); |
28 | print_r(gethostbynamel6('2620::2d0:200:0:0:0:10')); |
29 | */ |
29 | */ |
30 | 30 | ||
31 | function resolveip($host) { |
31 | function resolveip($host) { |
32 | return gethostbynamel6($host); |
32 | return gethostbynamel6($host); |
33 | } |
33 | } |
34 | 34 | ||
35 | # http://www.php.net/manual/en/function.gethostbyname.php#70936 |
35 | # http://www.php.net/manual/en/function.gethostbyname.php#70936 |
36 | # Modified by ViaThinkSoft |
36 | # Modified by ViaThinkSoft |
37 | 37 | ||
38 | # VTS-Modified: try_a default false -> true |
38 | # VTS-Modified: try_a default false -> true |
39 | function gethostbyname6($host, $try_a = /* false */ true) { |
39 | function gethostbyname6($host, $try_a = /* false */ true) { |
40 | // get AAAA record for $host |
40 | // get AAAA record for $host |
41 | // if $try_a is true, if AAAA fails, it tries for A |
41 | // if $try_a is true, if AAAA fails, it tries for A |
42 | // the first match found is returned |
42 | // the first match found is returned |
43 | // otherwise returns false |
43 | // otherwise returns false |
44 | 44 | ||
45 | $dns = gethostbynamel6($host, $try_a); |
45 | $dns = gethostbynamel6($host, $try_a); |
46 | if ($dns == false) { |
46 | if ($dns == false) { |
47 | return false; |
47 | return false; |
48 | } else { |
48 | } else { |
49 | return $dns[0]; |
49 | return $dns[0]; |
50 | } |
50 | } |
51 | } |
51 | } |
52 | 52 | ||
53 | # VTS-Modified: try_a default false -> true |
53 | # VTS-Modified: try_a default false -> true |
54 | function gethostbynamel6($host, $try_a = /* false */ true) { |
54 | function gethostbynamel6($host, $try_a = /* false */ true) { |
55 | # Added by VTS |
55 | # Added by VTS |
56 | $ipfilter = filter_var($host,FILTER_VALIDATE_IP); |
56 | $ipfilter = filter_var($host,FILTER_VALIDATE_IP); |
57 | if ($ipfilter != '') return array($ipfilter); |
57 | if ($ipfilter != '') return array($ipfilter); |
58 | 58 | ||
59 | // get AAAA records for $host, |
59 | // get AAAA records for $host, |
60 | // if $try_a is true, if AAAA fails, it tries for A |
60 | // if $try_a is true, if AAAA fails, it tries for A |
61 | // results are returned in an array of ips found matching type |
61 | // results are returned in an array of ips found matching type |
62 | // otherwise returns false |
62 | // otherwise returns false |
63 | 63 | ||
64 | $dns6 = dns_get_record($host, DNS_AAAA); |
64 | $dns6 = dns_get_record($host, DNS_AAAA); |
65 | if ($try_a == true) { |
65 | if ($try_a == true) { |
66 | $dns4 = dns_get_record($host, DNS_A); |
66 | $dns4 = dns_get_record($host, DNS_A); |
67 | $dns = array_merge($dns4, $dns6); |
67 | $dns = array_merge($dns4, $dns6); |
68 | } else { |
68 | } else { |
69 | $dns = $dns6; |
69 | $dns = $dns6; |
70 | } |
70 | } |
71 | $ip6 = array(); |
71 | $ip6 = array(); |
72 | $ip4 = array(); |
72 | $ip4 = array(); |
73 | foreach ($dns as $record) { |
73 | foreach ($dns as $record) { |
74 | if ($record["type"] == "A") { |
74 | if ($record["type"] == "A") { |
75 | $ip4[] = $record["ip"]; |
75 | $ip4[] = $record["ip"]; |
76 | } |
76 | } |
77 | if ($record["type"] == "AAAA") { |
77 | if ($record["type"] == "AAAA") { |
78 | $ip6[] = $record["ipv6"]; |
78 | $ip6[] = $record["ipv6"]; |
79 | } |
79 | } |
80 | } |
80 | } |
81 | 81 | ||
82 | # VTS-Modified: IP4+IP6 merged |
82 | # VTS-Modified: IP4+IP6 merged |
83 | $merged = array_merge($ip4, $ip6); |
83 | $merged = array_merge($ip4, $ip6); |
84 | if (count($merged) < 1) { |
84 | if (count($merged) < 1) { |
85 | return false; |
85 | return false; |
86 | } else { |
86 | } else { |
87 | return $merged; |
87 | return $merged; |
88 | } |
88 | } |
89 | 89 | ||
90 | if (count($ip6) < 1) { |
90 | if (count($ip6) < 1) { |
91 | if ($try_a == true) { |
91 | if ($try_a == true) { |
92 | if (count($ip4) < 1) { |
92 | if (count($ip4) < 1) { |
93 | return false; |
93 | return false; |
94 | } else { |
94 | } else { |
95 | return $ip4; |
95 | return $ip4; |
96 | } |
96 | } |
97 | } else { |
97 | } else { |
98 | return false; |
98 | return false; |
99 | } |
99 | } |
100 | } else { |
100 | } else { |
101 | return $ip6; |
101 | return $ip6; |
102 | } |
102 | } |
103 | } |
103 | } |
104 | 104 | ||
105 | ?> |
105 | ?> |
106 | 106 |