PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/code/classes/Daemon/DNSd/Type/RFC2671.class.php

https://github.com/blekkzor/pinetd2
PHP | 79 lines | 56 code | 15 blank | 8 comment | 11 complexity | 289d24fdf1560d3c8e3d452f3ebdc273 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * Implementation of EDNS0
  4. *
  5. * http://en.wikipedia.org/wiki/EDNS
  6. * http://tools.ietf.org/html/rfc2671
  7. */
  8. namespace Daemon\DNSd\Type;
  9. class RFC2671 extends Base {
  10. const TYPE_OPT = 41;
  11. private $flags = array(
  12. 0 => 'do', // DNSSEC enabled
  13. );
  14. protected function decodeFlags($flags) {
  15. $res = array();
  16. for($i = 0; $i < 16; $i++) {
  17. if (isset($this->flags[$i])) {
  18. $name = $this->flags[$i];
  19. } else {
  20. $name = 'bit' . $i;
  21. }
  22. $res[$name] = (bool)($flags >> (15-$i) & 0x1);
  23. }
  24. return $res;
  25. }
  26. protected function encodeFlags($flags) {
  27. $val = 0;
  28. foreach($this->flags as $bit => $name) {
  29. if (!$flags[$name]) continue;
  30. $val |= 1 << (15-$bit);
  31. }
  32. for($i = 0; $i < 16; $i++) {
  33. if (!$flags['bit'.$i]) continue;
  34. $val |= 1 << (15-$i);
  35. }
  36. return $val;
  37. }
  38. public function decode($val, array $context) {
  39. if ($this->type != 41) {
  40. $this->value = $val;
  41. return false;
  42. }
  43. $edns0 = array(
  44. 'ext_code' => $context['ttl'] >> 24 & 0xff,
  45. 'udp_payload_size' => $context['class'], // max size for reply, according to sender
  46. 'version' => $context['ttl'] >> 16 & 0xff,
  47. 'flags' => $this->decodeFlags($context['ttl'] & 0xffff),
  48. );
  49. // TODO: handle bad version and send error reply if not 0 (field $edns0['version'])
  50. $this->value = $edns0;
  51. // TODO: define this ($this->value) as "packet's edns0 data" by reference
  52. return true;
  53. }
  54. public function encode($val = NULL, $offset = NULL) {
  55. if (is_null($val)) $val = $this->value;
  56. if ($this->type != 41) return $val;
  57. $res = array(
  58. 'class' => $val['udp_payload_size'],
  59. 'ttl' => (($val['ext_code'] & 0xff) << 24) | (($val['version'] & 0xff) << 16) | ($this->encodeFlags($val['flags']) & 0xffff),
  60. 'data' => '', // TODO: encode data
  61. );
  62. return $res;
  63. }
  64. }