DNS.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /*
  3. * Module written/ported by Eric Kilfoil <eric@ypass.net>
  4. *
  5. * This is the copyright notice from the PERL Net::DNS module:
  6. *
  7. * Copyright (c) 1997-2000 Michael Fuhr. All rights reserved. This
  8. * program is free software; you can redistribute it and/or modify it
  9. * under the same terms as Perl itself.
  10. *
  11. * The majority of this is _NOT_ my code. I simply ported it from the
  12. * PERL Net::DNS module.
  13. *
  14. * The author of the Net::DNS module is Michael Fuhr <mike@fuhr.org>
  15. * http://www.fuhr.org/~mfuhr/perldns/
  16. *
  17. * I _DO_ maintain this code, and Miachael Fuhr has nothing to with the
  18. * porting of this code to PHP. Any questions directly related to this
  19. * class library should be directed to me.
  20. *
  21. * I'll be setting up a CVS repository for this class soon. The more
  22. * emails i get concerning this, the more apt i am to do it.
  23. *
  24. * License Information:
  25. *
  26. * Net_DNS: A resolver library for PHP
  27. * Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
  28. *
  29. * This library is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU Lesser General Public
  31. * License as published by the Free Software Foundation; either
  32. * version 2.1 of the License, or (at your option) any later version.
  33. *
  34. * This library is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  37. * Lesser General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU Lesser General Public
  40. * License along with this library; if not, write to the Free Software
  41. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  42. */
  43. /* Include information {{{ */
  44. $phpdns_basedir = 'Net';
  45. require_once("$phpdns_basedir/DNS/Header.php");
  46. require_once("$phpdns_basedir/DNS/Question.php");
  47. require_once("$phpdns_basedir/DNS/Packet.php");
  48. require_once("$phpdns_basedir/DNS/Resolver.php");
  49. require_once("$phpdns_basedir/DNS/RR.php");
  50. /* }}} */
  51. /* GLOBAL VARIABLE definitions {{{ */
  52. // Used by the Net_DNS_Resolver object to generate an ID
  53. mt_srand((double) microtime() * 10000);
  54. $_Net_DNS_packet_id = (int)mt_rand(0, 65535);
  55. /* }}} */
  56. /* Net_DNS object definition (incomplete) {{{ */
  57. /**
  58. * Initializes a resolver object
  59. *
  60. * Net_DNS allows you to query a nameserver for DNS lookups. It bypasses the
  61. * system resolver library entirely, which allows you to query any nameserver,
  62. * set your own values for retries, timeouts, recursion, etc.
  63. *
  64. * @author Eric Kilfoil <eric@ypass.net>
  65. * @package Net_DNS
  66. * @version 0.01alpha
  67. */
  68. class Net_DNS
  69. {
  70. /* class variable definitions {{{ */
  71. /**
  72. * A default resolver object created on instantiation
  73. *
  74. * @var object Net_DNS_Resolver
  75. */
  76. var $resolver;
  77. var $VERSION = '1.00b2'; // This should probably be a define :(
  78. var $PACKETSZ = 512;
  79. var $HFIXEDSZ = 12;
  80. var $QFIXEDSZ = 4;
  81. var $RRFIXEDSZ = 10;
  82. var $INT32SZ = 4;
  83. var $INT16SZ = 2;
  84. /* }}} */
  85. /* class constructor - Net_DNS() {{{ */
  86. /**
  87. * Initializes a resolver object
  88. *
  89. * @see Net_DNS_Resolver
  90. */
  91. function Net_DNS()
  92. {
  93. $this->resolver = new Net_DNS_Resolver();
  94. }
  95. /* }}} */
  96. /* Net_DNS::opcodesbyname() {{{ */
  97. /**
  98. * Translates opcode names to integers
  99. *
  100. * Translates the name of a DNS OPCODE into it's assigned number
  101. * listed in RFC1035, RFC1996, or RFC2136. Valid OPCODES are:
  102. * <ul>
  103. * <li>QUERY
  104. * <li>IQUERY
  105. * <li>STATUS
  106. * <li>NS_NOTIFY_OP
  107. * <li>UPDATE
  108. * <ul>
  109. *
  110. * @param string $opcode A DNS Packet OPCODE name
  111. * @return integer The integer value of an OPCODE
  112. * @see Net_DNS::opcodesbyval()
  113. */
  114. function opcodesbyname($opcode)
  115. {
  116. $op = array(
  117. 'QUERY' => 0, // RFC 1035
  118. 'IQUERY' => 1, // RFC 1035
  119. 'STATUS' => 2, // RFC 1035
  120. 'NS_NOTIFY_OP' => 4, // RFC 1996
  121. 'UPDATE' => 5, // RFC 2136
  122. );
  123. if (! strlen($op[$opcode])) {
  124. $op[$opcode] = NULL;
  125. }
  126. return($op[$opcode]);
  127. }
  128. /* }}} */
  129. /* Net_DNS::opcodesbyval() {{{*/
  130. /**
  131. * Translates opcode integers into names
  132. *
  133. * Translates the integer value of an opcode into it's name
  134. *
  135. * @param integer $opcodeval A DNS packet opcode integer
  136. * @return string The name of the OPCODE
  137. * @see Net_DNS::opcodesbyname()
  138. */
  139. function opcodesbyval($opcodeval)
  140. {
  141. $opval = array(
  142. 0 => 'QUERY',
  143. 1 => 'IQUERY',
  144. 2 => 'STATUS',
  145. 4 => 'NS_NOTIFY_OP',
  146. 5 => 'UPDATE',
  147. );
  148. if (! strlen($opval[$opcodeval])) {
  149. $opval[$opcodeval] = NULL;
  150. }
  151. return($opval[$opcodeval]);
  152. }
  153. /*}}}*/
  154. /* Net_DNS::rcodesbyname() {{{*/
  155. /**
  156. * Translates rcode names to integers
  157. *
  158. * Translates the name of a DNS RCODE (result code) into it's assigned number.
  159. * <ul>
  160. * <li>NOERROR
  161. * <li>FORMERR
  162. * <li>SERVFAIL
  163. * <li>NXDOMAIN
  164. * <li>NOTIMP
  165. * <li>REFUSED
  166. * <li>YXDOMAIN
  167. * <li>YXRRSET
  168. * <li>NXRRSET
  169. * <li>NOTAUTH
  170. * <li>NOTZONE
  171. * <ul>
  172. *
  173. * @param string $rcode A DNS Packet RCODE name
  174. * @return integer The integer value of an RCODE
  175. * @see Net_DNS::rcodesbyval()
  176. */
  177. function rcodesbyname($rcode)
  178. {
  179. $rc = array(
  180. 'NOERROR' => 0, // RFC 1035
  181. 'FORMERR' => 1, // RFC 1035
  182. 'SERVFAIL' => 2, // RFC 1035
  183. 'NXDOMAIN' => 3, // RFC 1035
  184. 'NOTIMP' => 4, // RFC 1035
  185. 'REFUSED' => 5, // RFC 1035
  186. 'YXDOMAIN' => 6, // RFC 2136
  187. 'YXRRSET' => 7, // RFC 2136
  188. 'NXRRSET' => 8, // RFC 2136
  189. 'NOTAUTH' => 9, // RFC 2136
  190. 'NOTZONE' => 10, // RFC 2136
  191. );
  192. if (! strlen($rc[$rcode])) {
  193. $rc[$rcode] = NULL;
  194. }
  195. return($rc[$rcode]);
  196. }
  197. /*}}}*/
  198. /* Net_DNS::rcodesbyval() {{{*/
  199. /**
  200. * Translates rcode integers into names
  201. *
  202. * Translates the integer value of an rcode into it's name
  203. *
  204. * @param integer $rcodeval A DNS packet rcode integer
  205. * @return string The name of the RCODE
  206. * @see Net_DNS::rcodesbyname()
  207. */
  208. function rcodesbyval($rcodeval)
  209. {
  210. $rc = array(
  211. 0 => 'NOERROR',
  212. 1 => 'FORMERR',
  213. 2 => 'SERVFAIL',
  214. 3 => 'NXDOMAIN',
  215. 4 => 'NOTIMP',
  216. 5 => 'REFUSED',
  217. 6 => 'YXDOMAIN',
  218. 7 => 'YXRRSET',
  219. 8 => 'NXRRSET',
  220. 9 => 'NOTAUTH',
  221. 10 => 'NOTZONE',
  222. );
  223. if (! strlen($rc[$rcodeval])) {
  224. $rc[$rcodeval] = NULL;
  225. }
  226. return($rc[$rcodeval]);
  227. }
  228. /*}}}*/
  229. /* Net_DNS::typesbyname() {{{*/
  230. /**
  231. * Translates RR type names into integers
  232. *
  233. * Translates a Resource Record from it's name to it's integer value.
  234. * Valid resource record types are:
  235. *
  236. * <ul>
  237. * <li>A
  238. * <li>NS
  239. * <li>MD
  240. * <li>MF
  241. * <li>CNAME
  242. * <li>SOA
  243. * <li>MB
  244. * <li>MG
  245. * <li>MR
  246. * <li>NULL
  247. * <li>WKS
  248. * <li>PTR
  249. * <li>HINFO
  250. * <li>MINFO
  251. * <li>MX
  252. * <li>TXT
  253. * <li>RP
  254. * <li>AFSDB
  255. * <li>X25
  256. * <li>ISDN
  257. * <li>RT
  258. * <li>NSAP
  259. * <li>NSAP_PTR
  260. * <li>SIG
  261. * <li>KEY
  262. * <li>PX
  263. * <li>GPOS
  264. * <li>AAAA
  265. * <li>LOC
  266. * <li>NXT
  267. * <li>EID
  268. * <li>NIMLOC
  269. * <li>SRV
  270. * <li>ATMA
  271. * <li>NAPTR
  272. * <li>TSIG
  273. * <li>UINFO
  274. * <li>UID
  275. * <li>GID
  276. * <li>UNSPEC
  277. * <li>IXFR
  278. * <li>AXFR
  279. * <li>MAILB
  280. * <li>MAILA
  281. * <li>ANY
  282. * <ul>
  283. *
  284. * @param string $rrtype A DNS packet RR type name
  285. * @return integer The integer value of an RR type
  286. * @see Net_DNS::typesbyval()
  287. */
  288. function typesbyname($rrtype)
  289. {
  290. $rc = array(
  291. 'A' => 1,
  292. 'NS' => 2,
  293. 'MD' => 3,
  294. 'MF' => 4,
  295. 'CNAME' => 5,
  296. 'SOA' => 6,
  297. 'MB' => 7,
  298. 'MG' => 8,
  299. 'MR' => 9,
  300. 'NULL' => 10,
  301. 'WKS' => 11,
  302. 'PTR' => 12,
  303. 'HINFO' => 13,
  304. 'MINFO' => 14,
  305. 'MX' => 15,
  306. 'TXT' => 16,
  307. 'RP' => 17,
  308. 'AFSDB' => 18,
  309. 'X25' => 19,
  310. 'ISDN' => 20,
  311. 'RT' => 21,
  312. 'NSAP' => 22,
  313. 'NSAP_PTR' => 23,
  314. 'SIG' => 24,
  315. 'KEY' => 25,
  316. 'PX' => 26,
  317. 'GPOS' => 27,
  318. 'AAAA' => 28,
  319. 'LOC' => 29,
  320. 'NXT' => 30,
  321. 'EID' => 31,
  322. 'NIMLOC' => 32,
  323. 'SRV' => 33,
  324. 'ATMA' => 34,
  325. 'NAPTR' => 35,
  326. 'UINFO' => 100,
  327. 'UID' => 101,
  328. 'GID' => 102,
  329. 'UNSPEC' => 103,
  330. 'TSIG' => 250,
  331. 'IXFR' => 251,
  332. 'AXFR' => 252,
  333. 'MAILB' => 253,
  334. 'MAILA' => 254,
  335. 'ANY' => 255,
  336. );
  337. if (! strlen($rc[$rrtype])) {
  338. $rc[$rrtype] = NULL;
  339. }
  340. return($rc[$rrtype]);
  341. }
  342. /*}}}*/
  343. /* Net_DNS::typesbyval() {{{*/
  344. /**
  345. * Translates RR type integers into names
  346. *
  347. * Translates the integer value of an RR type into it's name
  348. *
  349. * @param integer $rrtypeval A DNS packet RR type integer
  350. * @return string The name of the RR type
  351. * @see Net_DNS::typesbyname()
  352. */
  353. function typesbyval($rrtypeval)
  354. {
  355. $rc = array(
  356. 1 => 'A',
  357. 2 => 'NS',
  358. 3 => 'MD',
  359. 4 => 'MF',
  360. 5 => 'CNAME',
  361. 6 => 'SOA',
  362. 7 => 'MB',
  363. 8 => 'MG',
  364. 9 => 'MR',
  365. 10 => 'NULL',
  366. 11 => 'WKS',
  367. 12 => 'PTR',
  368. 13 => 'HINFO',
  369. 14 => 'MINFO',
  370. 15 => 'MX',
  371. 16 => 'TXT',
  372. 17 => 'RP',
  373. 18 => 'AFSDB',
  374. 19 => 'X25',
  375. 20 => 'ISDN',
  376. 21 => 'RT',
  377. 22 => 'NSAP',
  378. 23 => 'NSAP_PTR',
  379. 24 => 'SIG',
  380. 25 => 'KEY',
  381. 26 => 'PX',
  382. 27 => 'GPOS',
  383. 28 => 'AAAA',
  384. 29 => 'LOC',
  385. 30 => 'NXT',
  386. 31 => 'EID',
  387. 32 => 'NIMLOC',
  388. 33 => 'SRV',
  389. 34 => 'ATMA',
  390. 35 => 'NAPTR',
  391. 100 => 'UINFO',
  392. 101 => 'UID',
  393. 102 => 'GID',
  394. 103 => 'UNSPEC',
  395. 250 => 'TSIG',
  396. 251 => 'IXFR',
  397. 252 => 'AXFR',
  398. 253 => 'MAILB',
  399. 254 => 'MAILA',
  400. 255 => 'ANY',
  401. );
  402. if (! strlen($rc[$rrtypeval])) {
  403. $rc[$rrtypeval] = NULL;
  404. }
  405. return($rc[$rrtypeval]);
  406. }
  407. /*}}}*/
  408. /* Net_DNS::classesbyname() {{{*/
  409. /**
  410. * translates a DNS class from it's name to it's integer value. Valid
  411. * class names are:
  412. * <ul>
  413. * <li>IN
  414. * <li>CH
  415. * <li>HS
  416. * <li>NONE
  417. * <li>ANY
  418. * </ul>
  419. *
  420. * @param string $class A DNS packet class type
  421. * @return integer The integer value of an class type
  422. * @see Net_DNS::classesbyval()
  423. */
  424. function classesbyname($class)
  425. {
  426. $rc = array(
  427. 'IN' => 1,
  428. 'CH' => 3,
  429. 'HS' => 4,
  430. 'NONE' => 254,
  431. 'ANY' => 255
  432. );
  433. if (! isset($rc[$class])) {
  434. $rc[$class] = NULL;
  435. }
  436. return($rc[$class]);
  437. }
  438. /*}}}*/
  439. /* Net_DNS::classesbyval() {{{*/
  440. /**
  441. * Translates RR class integers into names
  442. *
  443. * Translates the integer value of an RR class into it's name
  444. *
  445. * @param integer $classval A DNS packet RR class integer
  446. * @return string The name of the RR class
  447. * @see Net_DNS::classesbyname()
  448. */
  449. function classesbyval($classval)
  450. {
  451. $rc = array(
  452. 1 => 'IN',
  453. 3 => 'CH',
  454. 4 => 'HS',
  455. 254 => 'NONE',
  456. 255 => 'ANY'
  457. );
  458. if (! strlen($rc[$classval])) {
  459. $rc[$classval] = NULL;
  460. }
  461. return($rc[$classval]);
  462. }
  463. /*}}}*/
  464. /* not completed - Net_DNS::mx() {{{*/
  465. /*}}}*/
  466. /* not completed - Net_DNS::yxrrset() {{{*/
  467. /*}}}*/
  468. /* not completed - Net_DNS::nxrrset() {{{*/
  469. /*}}}*/
  470. /* not completed - Net_DNS::yxdomain() {{{*/
  471. /*}}}*/
  472. /* not completed - Net_DNS::nxdomain() {{{*/
  473. /*}}}*/
  474. /* not completed - Net_DNS::rr_add() {{{*/
  475. /*}}}*/
  476. /* not completed - Net_DNS::rr_del() {{{*/
  477. /*}}}*/
  478. }
  479. /* }}} */
  480. /* VIM Settings {{{
  481. * Local variables:
  482. * tab-width: 4
  483. * c-basic-offset: 4
  484. * soft-stop-width: 4
  485. * c indent on
  486. * End:
  487. * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  488. * vim<600: sw=4 ts=4
  489. * }}} */
  490. ?>