dns_get_record or why PHP sucks

Hey,
I guess I have found a bug. In ./includes/common.inc.php on the function gethostbynamel6.
The problem is this one:
*query the A record:* var_dump(dns_get_record($host, DNS_A));
array(1) { [0]=> array(5) { ["host"]=> string(14) "host.name.tdl" ["class"]=> string(2) "IN" ["ttl"]=> int(0) ["type"]=> string(1) "A" ["ip"]=> string(12) "192.168.17.2" } }
*query the AAAA record:* var_dump(dns_get_record($host, DNS_AAAA));
PHP Warning:dns_get_record():DNS Queryfailed infile.php on line 4 bool(false)
*query both together*
var_dump(dns_get_record($host, DNS_A + DNS_AAAA));
PHP Warning:dns_get_record():DNS Queryfailed infile.php on line 4 bool(false)
As you can see if the DNS server doesn't have a AAAA record it fails, this cause for me the error that I couldn't add a new device to the observium. I build a prototype code which could be a solution for this problem but since I'm not a good php programmer I'm sure there is a smarter way for this.
$A_record = dns_get_record($host, DNS_A); $AAAA_record = dns_get_record($host, DNS_AAAA);
if($A_record != false && $AAAA_record != false) { $record = array_merge($A_record, $AAAA_record); } else if($A_record == false) { $record = $AAAA_record; } else if($AAAA_record == false) { $record = $A_record; } else { #show some error }
Since I did all the research it should be fairly easy to fix this, so I hope someone will do it. If you need anything more to fix it please ask.
A few things I noticed in the debugging process and totally unrelated to the problem: - the name gethostbyname6 is a bit confusing since it does both I would rename it to gethostbyname - same goes for $try_a I personally would pick a more descriptive name - "Could not resolve $host" in ./includes/functions.inc.php and ./includes/functions.inc_d.php, since It took me some time to figure out what this means I would add something like "Could not resolve $host with your DNS or your Hosts file"
But all in all it's a nice code base for a php project, well done!
For the case I'm wrong please tell me why.
l33tname

Hi,
thanks for the info. I think this applies only to systems where IPv6 stack fully disabled. We have many systems where IPv6 is not used and all hostnames anyway resolved.
But I'm a little fixed the code to avoid this situation in r6357.
On 01.04.2015 20:08, Sir l33tname wrote:
Hey,
I guess I have found a bug. In ./includes/common.inc.php on the function gethostbynamel6.
The problem is this one:
*query the A record:* var_dump(dns_get_record($host, DNS_A));
array(1) { [0]=> array(5) { ["host"]=> string(14) "host.name.tdl" ["class"]=> string(2) "IN" ["ttl"]=> int(0) ["type"]=> string(1) "A" ["ip"]=> string(12) "192.168.17.2" } }
*query the AAAA record:* var_dump(dns_get_record($host, DNS_AAAA));
PHP Warning:dns_get_record():DNS Queryfailed infile.php on line 4 bool(false)
*query both together*
var_dump(dns_get_record($host, DNS_A + DNS_AAAA));
PHP Warning:dns_get_record():DNS Queryfailed infile.php on line 4 bool(false)
As you can see if the DNS server doesn't have a AAAA record it fails, this cause for me the error that I couldn't add a new device to the observium. I build a prototype code which could be a solution for this problem but since I'm not a good php programmer I'm sure there is a smarter way for this.
$A_record = dns_get_record($host, DNS_A); $AAAA_record = dns_get_record($host, DNS_AAAA);
if($A_record != false && $AAAA_record != false) { $record = array_merge($A_record, $AAAA_record); } else if($A_record == false) { $record = $AAAA_record; } else if($AAAA_record == false) { $record = $A_record; } else { #show some error }
Since I did all the research it should be fairly easy to fix this, so I hope someone will do it. If you need anything more to fix it please ask.
A few things I noticed in the debugging process and totally unrelated to the problem:
- the name gethostbyname6 is a bit confusing since it does both I would
rename it to gethostbyname
- same goes for $try_a I personally would pick a more descriptive name
- "Could not resolve $host" in ./includes/functions.inc.php and
./includes/functions.inc_d.php, since It took me some time to figure out what this means I would add something like "Could not resolve $host with your DNS or your Hosts file"
But all in all it's a nice code base for a php project, well done!
For the case I'm wrong please tell me why.
l33tname
observium mailing list observium@observium.org http://postman.memetic.org/cgi-bin/mailman/listinfo/observium

Hey,
Thanks for your fast response and fix.
where IPv6 stack fully disabled.
I guess as long as the DNS returns something for the AAAA record it's okey, but if not then this error applies.
On 01/04/15 19:52, Mike Stupalov wrote:
Hi,
thanks for the info. I think this applies only to systems where IPv6 stack fully disabled. We have many systems where IPv6 is not used and all hostnames anyway resolved.
But I'm a little fixed the code to avoid this situation in r6357.
On 01.04.2015 20:08, Sir l33tname wrote:
Hey,
I guess I have found a bug. In ./includes/common.inc.php on the function gethostbynamel6.
The problem is this one:
*query the A record:* var_dump(dns_get_record($host, DNS_A));
array(1) { [0]=> array(5) { ["host"]=> string(14) "host.name.tdl" ["class"]=> string(2) "IN" ["ttl"]=> int(0) ["type"]=> string(1) "A" ["ip"]=> string(12) "192.168.17.2" } }
*query the AAAA record:* var_dump(dns_get_record($host, DNS_AAAA));
PHP Warning:dns_get_record():DNS Queryfailed infile.php on line 4 bool(false)
*query both together*
var_dump(dns_get_record($host, DNS_A + DNS_AAAA));
PHP Warning:dns_get_record():DNS Queryfailed infile.php on line 4 bool(false)
As you can see if the DNS server doesn't have a AAAA record it fails, this cause for me the error that I couldn't add a new device to the observium. I build a prototype code which could be a solution for this problem but since I'm not a good php programmer I'm sure there is a smarter way for this.
$A_record = dns_get_record($host, DNS_A); $AAAA_record = dns_get_record($host, DNS_AAAA);
if($A_record != false && $AAAA_record != false) { $record = array_merge($A_record, $AAAA_record); } else if($A_record == false) { $record = $AAAA_record; } else if($AAAA_record == false) { $record = $A_record; } else { #show some error }
Since I did all the research it should be fairly easy to fix this, so I hope someone will do it. If you need anything more to fix it please ask.
A few things I noticed in the debugging process and totally unrelated to the problem:
- the name gethostbyname6 is a bit confusing since it does both I would
rename it to gethostbyname
- same goes for $try_a I personally would pick a more descriptive name
- "Could not resolve $host" in ./includes/functions.inc.php and
./includes/functions.inc_d.php, since It took me some time to figure out what this means I would add something like "Could not resolve $host with your DNS or your Hosts file"
But all in all it's a nice code base for a php project, well done!
For the case I'm wrong please tell me why.
l33tname
observium mailing list observium@observium.org http://postman.memetic.org/cgi-bin/mailman/listinfo/observium
participants (2)
-
Mike Stupalov
-
Sir l33tname