forked from sivann/itdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_net_dns2.php
More file actions
161 lines (143 loc) · 5.12 KB
/
functions_net_dns2.php
File metadata and controls
161 lines (143 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/********************************* NET_DNS2 RELATED QUERIES IN FUNCTION FORM ****************************************/
require_once 'NET/DNS2.php';
/* Return DNS suffix with dot */
function suffix_dot($suffix)
{
//Make sure hostname is not empty
if($suffix != "")
{
$correct_suffixes="";
//Explode DNS suffixes
$suffix_array = explode(',', $suffix);
// Loop thru array to make sure every suffix has a leading DOT
foreach ($suffix_array as $ss)
{
//Trim whitespace before suffix
$ss=ltrim($ss);
//Check if suffix has leading DOT
$suffix_dot = substr_compare($ss, ".", 0, 1);
//Add DOT when necessary
if ($suffix_dot != 0 )
{
$correct_suffixes .= ".".$ss.",";
}
else
{
$correct_suffixes .= $ss.",";
}
}
//Trim whitespaces and unnecessary comma at the end
$correct_suffixes=rtrim($correct_suffixes, ",");
$correct_suffixes=trim($correct_suffixes);
return $correct_suffixes;
}
}
/* Return DNS server list without unnecessary spaces and commas */
function format_dns_server_list($dns_servers)
{
//Make sure dns server list is not empty
if($dns_servers != "")
{
$correct_dns_servers="";
//Explode DNS servers
$dns_servers_array = explode(',', $dns_servers);
// Loop thru array to make sure every suffix has a leading DOT
foreach ($dns_servers_array as $dss)
{
//Trim whitespace before suffix
$dss=ltrim($dss);
//Add to list of correctly formatted dns servers
$correct_dns_servers .= $dss.",";
}
//Trim whitespaces and unnecessary comma at the end
$correct_dns_servers=rtrim($correct_dns_servers, ",");
$correct_dns_servers=trim($correct_dns_servers);
return $correct_dns_servers;
}
}
/* Return IPv4 addresses for hostname of item */
function getipv4fromdns($hostname)
{
//Make sure hostname is not empty
if($hostname != "")
{
//Get DNS Servers
$dns_servers = dns_servers();
//Get DNS suffixes
$suffix = dns_suffix();
//Make sure DNS server list and suffix list are not empty
if(!empty($dns_servers))
{
if(!empty($suffix))
{
//Explode DNS suffixes
$suffix_array = explode(',', $suffix);
//Build array for hostnames with every suffix and without suffix
foreach ($suffix_array as $suffix)
{
$hostname_array .= $hostname.$suffix.",";
}
}
$hostname_array .= $hostname;
//Explode DNS Servers
$dns_servers_array = explode(',', $dns_servers);
//NET DNS2 Resolver with 1 second timeout (default = 5)
$r = new Net_DNS2_Resolver(array('nameservers' => $dns_servers_array));
$r->timeout = "1";
//Loop thru array to make sure every hostname+suffix combination from the array is checked against every DNS Server until (hopefully) a valid response was received
$success=false;
$hostname_array = explode(',', $hostname_array);
foreach ($hostname_array as $hostname)
{
//Try DNS query and catch exceptions from NET_DNS2
$ipv4 = "";
try {
$result = $r->query($hostname);
$success=true;
break;
}
catch (Net_DNS2_Exception $e) {
$ipv4[0] = "DNS Verification failed! Error: ".$e->getMessage();
}
}
// Loop thru result array to get IP Addresses from result when DNS query succeded
if($success)
{
foreach ($result->answer as $rr)
{
if (($rr->type == 'A') && ($r->isIPv4($rr->address)))
{
$ipv4 = $rr->address.",".$ipv4;
}
}
//Trim whitespaces and commas
$ipv4 = trim($ipv4);
$ipv4 = rtrim($ipv4,",");
//Sort IP Addresses
$ipv4_array = explode(',', $ipv4);
natsort($ipv4_array);
$ipv4 = implode(', ', $ipv4_array);
//Trim whitespaces and commas again
$ipv4 = trim($ipv4);
$ipv4 = rtrim($ipv4,",");
}
else
{
$ipv4[0] = "Error! Hostname is empty, unreadable or unresolveable!";
//Give back an error due to hostname error
}
}
else
{
$ipv4[0] = "Error! No DNS Servers specified under settings!";
//Give back an error due to missing dns server setting
}
}
else
{
$ipv4[0] = "Error! Hostname is empty, unreadable or unresolveable!";
//Give back an error due to hostname error
}
return $ipv4;
}