PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/net/dns2/rr/NSAP.php

https://bitbucket.org/speedealing/speedealing
PHP | 262 lines | 98 code | 25 blank | 139 comment | 10 complexity | ee1590f7aaf44623373d8f4582c298ab MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * DNS Library for handling lookups and updates.
  5. *
  6. * PHP Version 5
  7. *
  8. * Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in
  20. * the documentation and/or other materials provided with the
  21. * distribution.
  22. *
  23. * * Neither the name of Mike Pultz nor the names of his contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * @category Networking
  41. * @package Net_DNS2
  42. * @author Mike Pultz <mike@mikepultz.com>
  43. * @copyright 2010 Mike Pultz <mike@mikepultz.com>
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id: NSAP.php 179 2012-11-23 05:49:01Z mike.pultz $
  46. * @link http://pear.php.net/package/Net_DNS2
  47. * @since File available since Release 0.6.0
  48. *
  49. */
  50. /**
  51. * NSAP Resource Record - RFC1706
  52. *
  53. * |--------------|
  54. * | <-- IDP --> |
  55. * |--------------|-------------------------------------|
  56. * | AFI | IDI | <-- DSP --> |
  57. * |-----|--------|-------------------------------------|
  58. * | 47 | 0005 | DFI | AA |Rsvd | RD |Area | ID |Sel |
  59. * |-----|--------|-----|----|-----|----|-----|----|----|
  60. * octets | 1 | 2 | 1 | 3 | 2 | 2 | 2 | 6 | 1 |
  61. * |-----|--------|-----|----|-----|----|-----|----|----|
  62. *
  63. * @category Networking
  64. * @package Net_DNS2
  65. * @author Mike Pultz <mike@mikepultz.com>
  66. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  67. * @link http://pear.php.net/package/Net_DNS2
  68. * @see Net_DNS2_RR
  69. *
  70. */
  71. class Net_DNS2_RR_NSAP extends Net_DNS2_RR
  72. {
  73. public $afi;
  74. public $idi;
  75. public $dfi;
  76. public $aa;
  77. public $rsvd;
  78. public $rd;
  79. public $area;
  80. public $id;
  81. public $sel;
  82. /**
  83. * method to return the rdata portion of the packet as a string
  84. *
  85. * @return string
  86. * @access protected
  87. *
  88. */
  89. protected function rrToString()
  90. {
  91. return $this->cleanString($this->afi) . '.' .
  92. $this->cleanString($this->idi) . '.' .
  93. $this->cleanString($this->dfi) . '.' .
  94. $this->cleanString($this->aa) . '.' .
  95. $this->cleanString($this->rsvd) . '.' .
  96. $this->cleanString($this->rd) . '.' .
  97. $this->cleanString($this->area) . '.' .
  98. $this->cleanString($this->id) . '.' .
  99. $this->sel;
  100. }
  101. /**
  102. * parses the rdata portion from a standard DNS config line
  103. *
  104. * @param array $rdata a string split line of values for the rdata
  105. *
  106. * @return boolean
  107. * @access protected
  108. *
  109. */
  110. protected function rrFromString(array $rdata)
  111. {
  112. $data = strtolower(trim(array_shift($rdata)));
  113. //
  114. // there is no real standard for format, so we can't rely on the fact that
  115. // the value will come in with periods separating the values- so strip
  116. // them out if they're included, and parse without them.
  117. //
  118. $data = str_replace(array('.', '0x'), '', $data);
  119. //
  120. // unpack it as ascii characters
  121. //
  122. $x = unpack('A2afi/A4idi/A2dfi/A6aa/A4rsvd/A4rd/A4area/A12id/A2sel', $data);
  123. //
  124. // make sure the afi value is 47
  125. //
  126. if ($x['afi'] == 47) {
  127. $this->afi = '0x' . $x['afi'];
  128. $this->idi = $x['idi'];
  129. $this->dfi = $x['dfi'];
  130. $this->aa = $x['aa'];
  131. $this->rsvd = $x['rsvd'];
  132. $this->rd = $x['rd'];
  133. $this->area = $x['area'];
  134. $this->id = $x['id'];
  135. $this->sel = $x['sel'];
  136. return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * parses the rdata of the Net_DNS2_Packet object
  142. *
  143. * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
  144. *
  145. * @return boolean
  146. * @access protected
  147. *
  148. */
  149. protected function rrSet(Net_DNS2_Packet &$packet)
  150. {
  151. if ($this->rdlength == 20) {
  152. //
  153. // get the AFI value
  154. //
  155. $this->afi = dechex(ord($this->rdata[0]));
  156. //
  157. // we only support AFI 47- there arent' any others defined.
  158. //
  159. if ($this->afi == 47) {
  160. //
  161. // unpack the rest of the values
  162. //
  163. $x = unpack(
  164. 'Cafi/nidi/Cdfi/C3aa/nrsvd/nrd/narea/Nidh/nidl/Csel',
  165. $this->rdata
  166. );
  167. $this->afi = sprintf('0x%02x', $x['afi']);
  168. $this->idi = sprintf('%04x', $x['idi']);
  169. $this->dfi = sprintf('%02x', $x['dfi']);
  170. $this->aa = sprintf(
  171. '%06x', $x['aa1'] << 16 | $x['aa2'] << 8 | $x['aa3']
  172. );
  173. $this->rsvd = sprintf('%04x', $x['rsvd']);
  174. $this->rd = sprintf('%04x', $x['rd']);
  175. $this->area = sprintf('%04x', $x['area']);
  176. $this->id = sprintf('%08x', $x['idh']) .
  177. sprintf('%04x', $x['idl']);
  178. $this->sel = sprintf('%02x', $x['sel']);
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. /**
  185. * returns the rdata portion of the DNS packet
  186. *
  187. * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
  188. * compressed names
  189. *
  190. * @return mixed either returns a binary packed
  191. * string or null on failure
  192. * @access protected
  193. *
  194. */
  195. protected function rrGet(Net_DNS2_Packet &$packet)
  196. {
  197. if ($this->afi == 0x47) {
  198. //
  199. // build the aa field
  200. //
  201. $aa = unpack('A2x/A2y/A2z', $this->aa);
  202. //
  203. // build the id field
  204. //
  205. $id = unpack('A8a/A4b', $this->id);
  206. //
  207. $data = pack(
  208. 'CnCCCCnnnNnC',
  209. hexdec($this->afi),
  210. hexdec($this->idi),
  211. hexdec($this->dfi),
  212. hexdec($aa['x']),
  213. hexdec($aa['y']),
  214. hexdec($aa['z']),
  215. hexdec($this->rsvd),
  216. hexdec($this->rd),
  217. hexdec($this->area),
  218. hexdec($id['a']),
  219. hexdec($id['b']),
  220. hexdec($this->sel)
  221. );
  222. if (strlen($data) == 20) {
  223. $packet->offset += 20;
  224. return $data;
  225. }
  226. }
  227. return null;
  228. }
  229. }
  230. /*
  231. * Local variables:
  232. * tab-width: 4
  233. * c-basic-offset: 4
  234. * c-hanging-comment-ender-p: nil
  235. * End:
  236. */
  237. ?>