3

I run a Minecraft website and currently when using the query protocol it can't work with SRV records.

I am just wondering is there a way to get the ip and port that the SRV record points to.

E.g: mc.lunarphase.co.uk => 192.198.91.238:64759
Phyore
  • 41
  • 10

2 Answers2

5

You could use the dns_get_record.

$result = dns_get_record("_http._tcp.mxtoolbox.com", DNS_SRV);
print_r($result);

Which prints out:

Array
(
    [0] => Array
        (
            [host] => _http._tcp.mxtoolbox.com
            [class] => IN
            [ttl] => 2409
            [type] => SRV
            [pri] => 10
            [weight] => 100
            [port] => 80
            [target] => mxtoolbox.com
        )

)
jonaz
  • 3,764
  • 2
  • 20
  • 20
2

The easiest way is probably to use dig. You could use sockets directly but something like this is easier (IMHO):

function getDNS($hostname, $type='') {
        $records=`dig +noall +answer $hostname $type`;
        $records=explode("\n",$records);
        $res_hostname='';
        $port=0;

        foreach ($records as $record) {
                preg_match_all('/([^\s]+)\s*/',$record, $matches);
                if (is_array($matches) && is_array($matches[1]) && count($matches[1]) > 3) {
                        switch (strtoupper($matches[1][3])) {
                        case 'SRV':
                                if (count($matches[1]) > 6) {
                                        $port=$matches[1][6];
                                        $res_hostname=$matches[1][7];
                                }
                                break;
                        case 'A':
                        case 'CNAME':
                                if (count($matches[1]) > 3) {
                                        $res_hostname=$matches[1][4];
                                }
                                break;
                        }
                        if (!empty($res_hostname) && substr($res_hostname, -1) == '.') break; // if it's a cname, we've probably already got the ip, so keep going just in case (but we might not so don't count on it!)
                }
        }
        if (substr($res_hostname, -1) == '.') { // we have more resolving to do
                $res_hostname=getDNS(trim($res_hostname, '. '));
        }

        if (empty($res_hostname)) die('Failed to lookup IP for ' . (!empty($type) ? '(' . $type .' record) ' : '' ) . $hostname . PHP_EOL);
        if (empty($port)) return $res_hostname;
        return $res_hostname . ':' . $port;
}
$hostIPPair=getDNS('example.com', 'srv');
Chelsea Urquhart
  • 1,388
  • 1
  • 11
  • 18
  • 1
    Hmm just tried and it replied; Failed to lookup IP for (srv record) mc.lunarphase.co.uk – Phyore Jul 01 '13 at 21:59
  • The correct record for your server it would be _minecraft._tcp.mc.lunarphase.co.uk; however, I just looked up that srv record and it's pointing to 192.198.91.238. That's wrong (SRV records can't point to IPS; the correct way to do it would be to create an A record that points to that ip on a subdomain and then point the srv record to said subdomain) What I would recommend is adding an A record to mc. and pointing it to that IP and then pointing the SRV to mc.lunarphase.co.uk. – Chelsea Urquhart Jul 01 '13 at 22:11
  • The host of the servers told me to set it up that way~ It's the way SRV records are seyup for minecraft – Phyore Jul 01 '13 at 22:24
  • You were misinformed... Minecraft works with DNS the same way any other service does. – Chelsea Urquhart Jul 01 '13 at 22:41
  • I currently am using the method to connect to my server via mc.lunarphase.co.uk An A record would require me to also use the port at the end of the domain eg: mc.lunarphase.co.uk:64759 – Phyore Jul 02 '13 at 01:02
  • _minecraft._tcp.mc.lunarphase.co.uk should have an SRV record pointing to mc.lunarphase.co.uk:64759 as well as an A record pointing to 192.198.91.238 – Chelsea Urquhart Jul 02 '13 at 05:43