问题
I have to make a request to a nameserver. the socketpart is working like a charm, but to create the package I have some problems.
$domainname = "google.nl";
$hexdomain = ascii2he($domainname);
$package = "\x01\x01\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0b".$hexodmain."\x00\x00\xff\x00\x01";
this should be the package i send to the nameserver but the package is not correct. what is the right way to create $package
回答1:
First, the name you pass to the nameserver is not dot-separated, but every part of the name is transmitted separately.
Second, you do not send the data converted to hex, but send them directly. The hex (\x01\x01
) is just the representation.
So you would encode your google.nl
in the form "\x06google\x02nl\x00"
, as each of the name parts is preceded by its length, and the last one is succeeded by a \x00
meaning the empty string - which in turn denotes the end of the names chain.
So in order to remain variable, you should split your domain name into its components and precede each of them with the corresponding length byte.
Something like
function domain2dns($domain)
{
$split = explode(".", $domain);
$target = ""; // cumulate here
foreach ($split as $part) {
// For every $part, prepend one byte denoting its length.
// strlen($part) is its length which is supposed to be put into one character.
$target .= chr(strlen($part)).$part;
}
return $target . "\x00";
}
might be useful to do
$domainname = "google.nl";
$dnsdomain = domain2dns($domainname);
$package = "\x01\x01\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00" . $dnsdomain . "\x00\xff\x00\x01";
来源:https://stackoverflow.com/questions/13606868/create-nameserver-hex-header