Hey all,
I'm trying to make a tool that communicates directly with a DNS server via TCP, sends a query to it and return all the data and store it into a string.
In most cases, it works. However, there have been times where it sees a particular character and it stops. One of the characters that it has occurred with is %0A. As it is stopping prior to the task being complete, I am unable to complete the task I wish to do.
Please note that I am using PHP 4.3.4 at present, on a Windows XP Pro machine.
The code that is most efficient for me at present is:
$in = fgets($fp, 8); // get first few bytes to check the length
$inLength = intval(str_pad(ord(substr($in, 0, 1)), 2, "0", STR_PAD_LEFT)
. str_pad(ord(substr($in, 1, 1)), 2, "0", STR_PAD_LEFT)); // get packet length (working)
$c = strlen($in); // $c = 8
while ($c < $inLength) {
$in .= fgets($fp, ($inLength - $c + 3));
$c = strlen($in);
}
I cannot say why the "$inLength - $c + 3" is required in that specific syntax. If 3 is replaced with 1, two characters disappear from the output. If it is replaced with 4, the process times out (infinite loop, I'm not too sure).
Are there any other way to capture all returned information into a string without having characters lost? This fgets() method does not seem to do the job properly. I have used fgets() in the past without a drama, but it was mainly with HTTP TCP communication. Also, keeping in mind that all communication is to be done on port 53.
I've also tried searching for 'DNS' and like terms on this forum, however found nothing relevant to the issue I'm experiencing.
If anyone has a suggestion or two, please reply with them as I will try them and return feedback regarding the results.
Thanks in advance.