Hoa central
Public Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Hoa\Dns\Resolver Class Reference
Inheritance diagram for Hoa\Dns\Resolver:

Public Member Functions

static __construct (Socket\Server $server)
 
 on ($listenerId, $callable)
 
 run ()
 

Protected Attributes

 $_on = null
 
 $_server = null
 

Static Protected Attributes

static $_types
 
static $_classes
 

Detailed Description

Class .

Provide a DNS resolution server. Please, see RFC6195, RFC1035 and RFC1034 for an overview.

Definition at line 51 of file Resolver.php.

Constructor & Destructor Documentation

static Hoa\Dns\Resolver::__construct ( Socket\Server  $server)

Construct the DNS server.

Parameters
\Hoa\Socket\Server$serverServer.
Returns
void

Definition at line 163 of file Resolver.php.

164  {
165  if ('udp' != $server->getSocket()->getTransport()) {
166  throw new Exception(
167  'Server must listen on UDP transport; given %s.',
168  0,
169  strtoupper($server->getSocket()->getTransport())
170  );
171  }
172 
173  set_time_limit(0);
174 
175  $this->_server = $server;
176  $this->_on = new Core\Event\Listener($this, ['query']);
177 
178  return;
179  }

Member Function Documentation

Hoa\Dns\Resolver::on (   $listenerId,
  $callable 
)

Attach a callable to this listenable object.

Parameters
string$listenerIdListener ID.
mixed$callableCallable.
Returns
Exceptions

Implements Hoa\Core\Event\Listenable.

Definition at line 189 of file Resolver.php.

190  {
191  $this->_on->attach($listenerId, $callable);
192 
193  return $this;
194  }
Hoa\Dns\Resolver::run ( )

Run the server.

Returns
void

Definition at line 201 of file Resolver.php.

202  {
203  $this->_server->considerRemoteAddress(true);
204  $this->_server->connectAndWait();
205 
206  while (true) {
207  $buffer = $this->_server->read(1024);
208 
209  if (empty($buffer)) {
210  continue;
211  }
212 
213  // Skip header.
214  $handle = substr($buffer, 12);
215  $domain = null;
216 
217  // QNAME.
218  for ($i = 0, $m = strlen($handle); $i < $m; ++$i) {
219  if (0 === $length = ord($handle[$i])) {
220  break;
221  }
222 
223  if (null !== $domain) {
224  $domain .= '.';
225  }
226 
227  $domain .= substr($handle, $i + 1, $length);
228  $i += $length;
229  }
230 
231  // QTYPE.
232  $i += 2;
233  $qtype = (int) (string) ord($handle[$i]) +
234  (int) (string) ord($handle[$i + 1]);
235  $type = array_search($qtype, static::$_types) ?: $qtype;
236 
237  // QCLASS.
238  $i += 2;
239  $qclass = (int) (string) ord($handle[$i]);
240  $class = array_search($qclass, static::$_classes) ?: $qclass;
241 
242  $ips = $this->_on->fire('query', new Core\Event\Bucket([
243  'domain' => $domain,
244  'type' => $type,
245  'class' => $class
246  ]));
247  $ip = null;
248 
249  if (false === $ips[0]) {
250  $this->_server->writeAll(
251  // Header.
252 
253  // ID.
254  $buffer[0] .
255  $buffer[1] .
256  pack(
257  'C',
258  1 << 7 // QR, 1 = response.
259  // OpCode, 0 = QUERY.
260  // AA, 0
261  // TC, 0
262  | 1 // RD
263  ) .
264  pack(
265  'C',
266  0 // RA, Z, AD, CD.
267  | 3 // NXDOMAIN.
268  ) .
269  // QDCOUNT.
270  pack('n', 0) .
271  // ANCOUNT.
272  pack('n', 0) .
273  // NSCOUNT.
274  pack('n', 0) .
275  // ARCOUNT.
276  pack('n', 0)
277  );
278 
279  continue;
280  }
281 
282  foreach (explode('.', $ips[0]) as $foo) {
283  $ip .= pack('C', $foo);
284  }
285 
286  $this->_server->writeAll(
287  // Header.
288 
289  // ID.
290  $buffer[0] .
291  $buffer[1] .
292  pack(
293  'C',
294  1 << 7 // QR, 1 = response.
295  // OpCode, 0 = QUERY.
296  // AA, 0
297  // TC, 0
298  | 1 // RD
299  ) .
300  pack(
301  'C',
302  0 // RA, Z, AD, CD.
303  ) .
304  // QDCOUNT.
305  $buffer[4] .
306  $buffer[5] .
307  // ANCOUNT.
308  pack('n', 1) .
309  // NSCOUNT.
310  pack('n', 0) .
311  // ARCOUNT.
312  pack('n', 0) .
313 
314  // Question.
315 
316  $handle .
317  pack('CC', 192, 12) .
318 
319  // Answer.
320 
321  // TYPE.
322  pack('n', $qtype) .
323  // CLASS.
324  pack('n', $qclass) .
325  // TTL.
326  pack('N', 60) .
327  // RDLENGTH.
328  pack('n', 4) .
329  // RDATA.
330  $ip
331  );
332  }
333 
334  $this->_server->disconnect();
335 
336  return;
337  }

Member Data Documentation

Hoa\Dns\Resolver::$_classes
staticprotected
Initial value:
= [
'in' => 1

Definition at line 146 of file Resolver.php.

Hoa\Dns\Resolver::$_on = null
protected

Definition at line 58 of file Resolver.php.

Hoa\Dns\Resolver::$_server = null
protected

Definition at line 65 of file Resolver.php.

Hoa\Dns\Resolver::$_types
staticprotected
Initial value:
= [
'invalid' => 0

Definition at line 74 of file Resolver.php.


The documentation for this class was generated from the following file: