summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Protocol/Dns.php
blob: 3d422d729d8829218b9c061ef040953a4587ddd5 (plain)
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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Protocol;

/**
 * Discover dns records using regular or reverse lookup
 */
class Dns
{
    /**
     * Discover all service records on a given domain
     *
     * @param string    $domain     The domain to search
     * @param string    $service    The type of the service, like for example 'ldaps' or 'ldap'
     * @param string    $protocol   The transport protocol used by the service, defaults to 'tcp'
     *
     * @return array                An array of all found service records
     */
    public static function getSrvRecords($domain, $service, $protocol = 'tcp')
    {
        $records = dns_get_record('_' . $service . '._' . $protocol . '.' . $domain, DNS_SRV);
        return $records === false ? array() : $records;
    }

    /**
     * Get all ldap records for the given domain
     *
     * @param   string  $query    The domain to query
     * @param   int     $type     The type of DNS-entry to fetch, see
     *                            http://www.php.net/manual/de/function.dns-get-record.php for available types
     *
     * @return  array|null        An array of record entries
     */
    public static function records($query, $type = DNS_ANY)
    {
        return dns_get_record($query, $type);
    }

    /**
     * Reverse lookup all host names available on the given ip address
     *
     * @param   string  $ipAddress
     * @param   int     $type
     *
     * @return array|null
     */
    public static function ptr($ipAddress, $type = DNS_ANY)
    {
        $host = gethostbyaddr($ipAddress);
        if ($host === false || $host === $ipAddress) {
            // malformed input or no host found
            return null;
        }
        return self::records($host, $type);
    }

    /**
     * Get the IPv4 address of the given hostname.
     *
     * @param   $hostname       The hostname to resolve
     *
     * @return  string|null     The IPv4 address of the given hostname or null, when no entry exists.
     */
    public static function ipv4($hostname)
    {
        $records = dns_get_record($hostname, DNS_A);
        if ($records !== false && count($records) > 0) {
            return $records[0]['ip'];
        }
        return null;
    }

    /**
     * Get the IPv6 address of the given hostname.
     *
     * @param   $hostname       The hostname to resolve
     *
     * @return  string|null     The IPv6 address of the given hostname or null, when no entry exists.
     */
    public static function ipv6($hostname)
    {
        $records = dns_get_record($hostname, DNS_AAAA);
        if ($records !== false && count($records) > 0) {
            return $records[0]['ip'];
        }
        return null;
    }
}