PageRenderTime 47ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/standard/dns.c

http://github.com/infusion/PHP
C | 1000 lines | 819 code | 95 blank | 86 comment | 182 complexity | acd22b8323850e26f18647476845f873 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: The typical suspects |
  16. | Pollita <pollita@php.net> |
  17. | Marcus Boerger <helly@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id: dns.c 306939 2011-01-01 02:19:59Z felipe $ */
  21. /* {{{ includes */
  22. #include "php.h"
  23. #include "php_network.h"
  24. #if HAVE_SYS_SOCKET_H
  25. #include <sys/socket.h>
  26. #endif
  27. #ifdef PHP_WIN32
  28. # include "win32/inet.h"
  29. # include <winsock2.h>
  30. # include <windows.h>
  31. # include <Ws2tcpip.h>
  32. #else /* This holds good for NetWare too, both for Winsock and Berkeley sockets */
  33. #include <netinet/in.h>
  34. #if HAVE_ARPA_INET_H
  35. #include <arpa/inet.h>
  36. #endif
  37. #include <netdb.h>
  38. #ifdef _OSD_POSIX
  39. #undef STATUS
  40. #undef T_UNSPEC
  41. #endif
  42. #if HAVE_ARPA_NAMESER_H
  43. #ifdef DARWIN
  44. # define BIND_8_COMPAT 1
  45. #endif
  46. #include <arpa/nameser.h>
  47. #endif
  48. #if HAVE_RESOLV_H
  49. #include <resolv.h>
  50. #endif
  51. #ifdef HAVE_DNS_H
  52. #include <dns.h>
  53. #endif
  54. #endif
  55. /* Borrowed from SYS/SOCKET.H */
  56. #if defined(NETWARE) && defined(USE_WINSOCK)
  57. #define AF_INET 2 /* internetwork: UDP, TCP, etc. */
  58. #endif
  59. #include "php_dns.h"
  60. /* type compat */
  61. #ifndef DNS_T_A
  62. #define DNS_T_A 1
  63. #endif
  64. #ifndef DNS_T_NS
  65. #define DNS_T_NS 2
  66. #endif
  67. #ifndef DNS_T_CNAME
  68. #define DNS_T_CNAME 5
  69. #endif
  70. #ifndef DNS_T_SOA
  71. #define DNS_T_SOA 6
  72. #endif
  73. #ifndef DNS_T_PTR
  74. #define DNS_T_PTR 12
  75. #endif
  76. #ifndef DNS_T_HINFO
  77. #define DNS_T_HINFO 13
  78. #endif
  79. #ifndef DNS_T_MINFO
  80. #define DNS_T_MINFO 14
  81. #endif
  82. #ifndef DNS_T_MX
  83. #define DNS_T_MX 15
  84. #endif
  85. #ifndef DNS_T_TXT
  86. #define DNS_T_TXT 16
  87. #endif
  88. #ifndef DNS_T_AAAA
  89. #define DNS_T_AAAA 28
  90. #endif
  91. #ifndef DNS_T_SRV
  92. #define DNS_T_SRV 33
  93. #endif
  94. #ifndef DNS_T_NAPTR
  95. #define DNS_T_NAPTR 35
  96. #endif
  97. #ifndef DNS_T_A6
  98. #define DNS_T_A6 38
  99. #endif
  100. #ifndef DNS_T_ANY
  101. #define DNS_T_ANY 255
  102. #endif
  103. /* }}} */
  104. static char *php_gethostbyaddr(char *ip);
  105. static char *php_gethostbyname(char *name);
  106. #ifdef HAVE_GETHOSTNAME
  107. /* {{{ proto string gethostname()
  108. Get the host name of the current machine */
  109. PHP_FUNCTION(gethostname)
  110. {
  111. char buf[4096];
  112. if (zend_parse_parameters_none() == FAILURE) {
  113. return;
  114. }
  115. if (gethostname(buf, sizeof(buf) - 1)) {
  116. php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to fetch host [%d]: %s", errno, strerror(errno));
  117. RETURN_FALSE;
  118. }
  119. RETURN_STRING(buf, 1);
  120. }
  121. /* }}} */
  122. #endif
  123. /* TODO: Reimplement the gethostby* functions using the new winxp+ API, in dns_win32.c, then
  124. we can have a dns.c, dns_unix.c and dns_win32.c instead of a messy dns.c full of #ifdef
  125. */
  126. /* {{{ proto string gethostbyaddr(string ip_address)
  127. Get the Internet host name corresponding to a given IP address */
  128. PHP_FUNCTION(gethostbyaddr)
  129. {
  130. char *addr;
  131. int addr_len;
  132. char *hostname;
  133. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &addr, &addr_len) == FAILURE) {
  134. return;
  135. }
  136. hostname = php_gethostbyaddr(addr);
  137. if (hostname == NULL) {
  138. #if HAVE_IPV6 && HAVE_INET_PTON
  139. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Address is not a valid IPv4 or IPv6 address");
  140. #else
  141. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Address is not in a.b.c.d form");
  142. #endif
  143. RETVAL_FALSE;
  144. } else {
  145. RETVAL_STRING(hostname, 0);
  146. }
  147. }
  148. /* }}} */
  149. /* {{{ php_gethostbyaddr */
  150. static char *php_gethostbyaddr(char *ip)
  151. {
  152. #if HAVE_IPV6 && HAVE_INET_PTON
  153. struct in6_addr addr6;
  154. #endif
  155. struct in_addr addr;
  156. struct hostent *hp;
  157. #if HAVE_IPV6 && HAVE_INET_PTON
  158. if (inet_pton(AF_INET6, ip, &addr6)) {
  159. hp = gethostbyaddr((char *) &addr6, sizeof(addr6), AF_INET6);
  160. } else if (inet_pton(AF_INET, ip, &addr)) {
  161. hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
  162. } else {
  163. return NULL;
  164. }
  165. #else
  166. addr.s_addr = inet_addr(ip);
  167. if (addr.s_addr == -1) {
  168. return NULL;
  169. }
  170. hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
  171. #endif
  172. if (!hp || hp->h_name == NULL || hp->h_name[0] == '\0') {
  173. return estrdup(ip);
  174. }
  175. return estrdup(hp->h_name);
  176. }
  177. /* }}} */
  178. /* {{{ proto string gethostbyname(string hostname)
  179. Get the IP address corresponding to a given Internet host name */
  180. PHP_FUNCTION(gethostbyname)
  181. {
  182. char *hostname;
  183. int hostname_len;
  184. char *addr;
  185. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &hostname, &hostname_len) == FAILURE) {
  186. return;
  187. }
  188. addr = php_gethostbyname(hostname);
  189. RETVAL_STRING(addr, 0);
  190. }
  191. /* }}} */
  192. /* {{{ proto array gethostbynamel(string hostname)
  193. Return a list of IP addresses that a given hostname resolves to. */
  194. PHP_FUNCTION(gethostbynamel)
  195. {
  196. char *hostname;
  197. int hostname_len;
  198. struct hostent *hp;
  199. struct in_addr in;
  200. int i;
  201. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &hostname, &hostname_len) == FAILURE) {
  202. return;
  203. }
  204. hp = gethostbyname(hostname);
  205. if (hp == NULL || hp->h_addr_list == NULL) {
  206. RETURN_FALSE;
  207. }
  208. array_init(return_value);
  209. for (i = 0 ; hp->h_addr_list[i] != 0 ; i++) {
  210. in = *(struct in_addr *) hp->h_addr_list[i];
  211. add_next_index_string(return_value, inet_ntoa(in), 1);
  212. }
  213. }
  214. /* }}} */
  215. /* {{{ php_gethostbyname */
  216. static char *php_gethostbyname(char *name)
  217. {
  218. struct hostent *hp;
  219. struct in_addr in;
  220. hp = gethostbyname(name);
  221. if (!hp || !*(hp->h_addr_list)) {
  222. return estrdup(name);
  223. }
  224. memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr));
  225. return estrdup(inet_ntoa(in));
  226. }
  227. /* }}} */
  228. #if HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32)
  229. # define PHP_DNS_NUM_TYPES 12 /* Number of DNS Types Supported by PHP currently */
  230. # define PHP_DNS_A 0x00000001
  231. # define PHP_DNS_NS 0x00000002
  232. # define PHP_DNS_CNAME 0x00000010
  233. # define PHP_DNS_SOA 0x00000020
  234. # define PHP_DNS_PTR 0x00000800
  235. # define PHP_DNS_HINFO 0x00001000
  236. # define PHP_DNS_MX 0x00004000
  237. # define PHP_DNS_TXT 0x00008000
  238. # define PHP_DNS_A6 0x01000000
  239. # define PHP_DNS_SRV 0x02000000
  240. # define PHP_DNS_NAPTR 0x04000000
  241. # define PHP_DNS_AAAA 0x08000000
  242. # define PHP_DNS_ANY 0x10000000
  243. # define PHP_DNS_ALL (PHP_DNS_A|PHP_DNS_NS|PHP_DNS_CNAME|PHP_DNS_SOA|PHP_DNS_PTR|PHP_DNS_HINFO|PHP_DNS_MX|PHP_DNS_TXT|PHP_DNS_A6|PHP_DNS_SRV|PHP_DNS_NAPTR|PHP_DNS_AAAA)
  244. #endif /* HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32) */
  245. /* Note: These functions are defined in ext/standard/dns_win32.c for Windows! */
  246. #if !defined(PHP_WIN32) && (HAVE_DNS_SEARCH_FUNC && !(defined(__BEOS__) || defined(NETWARE)))
  247. #ifndef HFIXEDSZ
  248. #define HFIXEDSZ 12 /* fixed data in header <arpa/nameser.h> */
  249. #endif /* HFIXEDSZ */
  250. #ifndef QFIXEDSZ
  251. #define QFIXEDSZ 4 /* fixed data in query <arpa/nameser.h> */
  252. #endif /* QFIXEDSZ */
  253. #undef MAXHOSTNAMELEN
  254. #define MAXHOSTNAMELEN 1024
  255. #ifndef MAXRESOURCERECORDS
  256. #define MAXRESOURCERECORDS 64
  257. #endif /* MAXRESOURCERECORDS */
  258. typedef union {
  259. HEADER qb1;
  260. u_char qb2[65536];
  261. } querybuf;
  262. /* just a hack to free resources allocated by glibc in __res_nsend()
  263. * See also:
  264. * res_thread_freeres() in glibc/resolv/res_init.c
  265. * __libc_res_nsend() in resolv/res_send.c
  266. * */
  267. #if defined(__GLIBC__) && !defined(HAVE_DEPRECATED_DNS_FUNCS)
  268. #define php_dns_free_res(__res__) _php_dns_free_res(__res__)
  269. static void _php_dns_free_res(struct __res_state res) { /* {{{ */
  270. int ns;
  271. for (ns = 0; ns < MAXNS; ns++) {
  272. if (res._u._ext.nsaddrs[ns] != NULL) {
  273. free (res._u._ext.nsaddrs[ns]);
  274. res._u._ext.nsaddrs[ns] = NULL;
  275. }
  276. }
  277. } /* }}} */
  278. #else
  279. #define php_dns_free_res(__res__)
  280. #endif
  281. /* {{{ proto bool dns_check_record(string host [, string type])
  282. Check DNS records corresponding to a given Internet host name or IP address */
  283. PHP_FUNCTION(dns_check_record)
  284. {
  285. #ifndef MAXPACKET
  286. #define MAXPACKET 8192 /* max packet size used internally by BIND */
  287. #endif
  288. u_char ans[MAXPACKET];
  289. char *hostname, *rectype = NULL;
  290. int hostname_len, rectype_len = 0;
  291. int type = T_MX, i;
  292. #if defined(HAVE_DNS_SEARCH)
  293. struct sockaddr_storage from;
  294. uint32_t fromsize = sizeof(from);
  295. dns_handle_t handle;
  296. #elif defined(HAVE_RES_NSEARCH)
  297. struct __res_state state;
  298. struct __res_state *handle = &state;
  299. #endif
  300. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &hostname, &hostname_len, &rectype, &rectype_len) == FAILURE) {
  301. return;
  302. }
  303. if (hostname_len == 0) {
  304. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host cannot be empty");
  305. RETURN_FALSE;
  306. }
  307. if (rectype) {
  308. if (!strcasecmp("A", rectype)) type = T_A;
  309. else if (!strcasecmp("NS", rectype)) type = DNS_T_NS;
  310. else if (!strcasecmp("MX", rectype)) type = DNS_T_MX;
  311. else if (!strcasecmp("PTR", rectype)) type = DNS_T_PTR;
  312. else if (!strcasecmp("ANY", rectype)) type = DNS_T_ANY;
  313. else if (!strcasecmp("SOA", rectype)) type = DNS_T_SOA;
  314. else if (!strcasecmp("TXT", rectype)) type = DNS_T_TXT;
  315. else if (!strcasecmp("CNAME", rectype)) type = DNS_T_CNAME;
  316. else if (!strcasecmp("AAAA", rectype)) type = DNS_T_AAAA;
  317. else if (!strcasecmp("SRV", rectype)) type = DNS_T_SRV;
  318. else if (!strcasecmp("NAPTR", rectype)) type = DNS_T_NAPTR;
  319. else if (!strcasecmp("A6", rectype)) type = DNS_T_A6;
  320. else {
  321. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%s' not supported", rectype);
  322. RETURN_FALSE;
  323. }
  324. }
  325. #if defined(HAVE_DNS_SEARCH)
  326. handle = dns_open(NULL);
  327. if (handle == NULL) {
  328. RETURN_FALSE;
  329. }
  330. #elif defined(HAVE_RES_NSEARCH)
  331. memset(&state, 0, sizeof(state));
  332. if (res_ninit(handle)) {
  333. RETURN_FALSE;
  334. }
  335. #else
  336. res_init();
  337. #endif
  338. RETVAL_TRUE;
  339. i = php_dns_search(handle, hostname, C_IN, type, ans, sizeof(ans));
  340. if (i < 0) {
  341. RETVAL_FALSE;
  342. }
  343. php_dns_free_handle(handle);
  344. }
  345. /* }}} */
  346. #if HAVE_FULL_DNS_FUNCS
  347. /* {{{ php_parserr */
  348. static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int store, zval **subarray)
  349. {
  350. u_short type, class, dlen;
  351. u_long ttl;
  352. long n, i;
  353. u_short s;
  354. u_char *tp, *p;
  355. char name[MAXHOSTNAMELEN];
  356. int have_v6_break = 0, in_v6_break = 0;
  357. *subarray = NULL;
  358. n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, sizeof(name) - 2);
  359. if (n < 0) {
  360. return NULL;
  361. }
  362. cp += n;
  363. GETSHORT(type, cp);
  364. GETSHORT(class, cp);
  365. GETLONG(ttl, cp);
  366. GETSHORT(dlen, cp);
  367. if (type_to_fetch != T_ANY && type != type_to_fetch) {
  368. cp += dlen;
  369. return cp;
  370. }
  371. if (!store) {
  372. cp += dlen;
  373. return cp;
  374. }
  375. ALLOC_INIT_ZVAL(*subarray);
  376. array_init(*subarray);
  377. add_assoc_string(*subarray, "host", name, 1);
  378. switch (type) {
  379. case DNS_T_A:
  380. add_assoc_string(*subarray, "type", "A", 1);
  381. snprintf(name, sizeof(name), "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
  382. add_assoc_string(*subarray, "ip", name, 1);
  383. cp += dlen;
  384. break;
  385. case DNS_T_MX:
  386. add_assoc_string(*subarray, "type", "MX", 1);
  387. GETSHORT(n, cp);
  388. add_assoc_long(*subarray, "pri", n);
  389. /* no break; */
  390. case DNS_T_CNAME:
  391. if (type == DNS_T_CNAME) {
  392. add_assoc_string(*subarray, "type", "CNAME", 1);
  393. }
  394. /* no break; */
  395. case DNS_T_NS:
  396. if (type == DNS_T_NS) {
  397. add_assoc_string(*subarray, "type", "NS", 1);
  398. }
  399. /* no break; */
  400. case DNS_T_PTR:
  401. if (type == DNS_T_PTR) {
  402. add_assoc_string(*subarray, "type", "PTR", 1);
  403. }
  404. n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
  405. if (n < 0) {
  406. return NULL;
  407. }
  408. cp += n;
  409. add_assoc_string(*subarray, "target", name, 1);
  410. break;
  411. case DNS_T_HINFO:
  412. /* See RFC 1010 for values */
  413. add_assoc_string(*subarray, "type", "HINFO", 1);
  414. n = *cp & 0xFF;
  415. cp++;
  416. add_assoc_stringl(*subarray, "cpu", (char*)cp, n, 1);
  417. cp += n;
  418. n = *cp & 0xFF;
  419. cp++;
  420. add_assoc_stringl(*subarray, "os", (char*)cp, n, 1);
  421. cp += n;
  422. break;
  423. case DNS_T_TXT:
  424. {
  425. int ll = 0;
  426. zval *entries = NULL;
  427. add_assoc_string(*subarray, "type", "TXT", 1);
  428. tp = emalloc(dlen + 1);
  429. MAKE_STD_ZVAL(entries);
  430. array_init(entries);
  431. while (ll < dlen) {
  432. n = cp[ll];
  433. memcpy(tp + ll , cp + ll + 1, n);
  434. add_next_index_stringl(entries, cp + ll + 1, n, 1);
  435. ll = ll + n + 1;
  436. }
  437. tp[dlen] = '\0';
  438. cp += dlen;
  439. add_assoc_stringl(*subarray, "txt", tp, dlen - 1, 0);
  440. add_assoc_zval(*subarray, "entries", entries);
  441. }
  442. break;
  443. case DNS_T_SOA:
  444. add_assoc_string(*subarray, "type", "SOA", 1);
  445. n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) -2);
  446. if (n < 0) {
  447. return NULL;
  448. }
  449. cp += n;
  450. add_assoc_string(*subarray, "mname", name, 1);
  451. n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) -2);
  452. if (n < 0) {
  453. return NULL;
  454. }
  455. cp += n;
  456. add_assoc_string(*subarray, "rname", name, 1);
  457. GETLONG(n, cp);
  458. add_assoc_long(*subarray, "serial", n);
  459. GETLONG(n, cp);
  460. add_assoc_long(*subarray, "refresh", n);
  461. GETLONG(n, cp);
  462. add_assoc_long(*subarray, "retry", n);
  463. GETLONG(n, cp);
  464. add_assoc_long(*subarray, "expire", n);
  465. GETLONG(n, cp);
  466. add_assoc_long(*subarray, "minimum-ttl", n);
  467. break;
  468. case DNS_T_AAAA:
  469. tp = (u_char*)name;
  470. for(i=0; i < 8; i++) {
  471. GETSHORT(s, cp);
  472. if (s != 0) {
  473. if (tp > (u_char *)name) {
  474. in_v6_break = 0;
  475. tp[0] = ':';
  476. tp++;
  477. }
  478. tp += sprintf((char*)tp,"%x",s);
  479. } else {
  480. if (!have_v6_break) {
  481. have_v6_break = 1;
  482. in_v6_break = 1;
  483. tp[0] = ':';
  484. tp++;
  485. } else if (!in_v6_break) {
  486. tp[0] = ':';
  487. tp++;
  488. tp[0] = '0';
  489. tp++;
  490. }
  491. }
  492. }
  493. if (have_v6_break && in_v6_break) {
  494. tp[0] = ':';
  495. tp++;
  496. }
  497. tp[0] = '\0';
  498. add_assoc_string(*subarray, "type", "AAAA", 1);
  499. add_assoc_string(*subarray, "ipv6", name, 1);
  500. break;
  501. case DNS_T_A6:
  502. p = cp;
  503. add_assoc_string(*subarray, "type", "A6", 1);
  504. n = ((int)cp[0]) & 0xFF;
  505. cp++;
  506. add_assoc_long(*subarray, "masklen", n);
  507. tp = (u_char*)name;
  508. if (n > 15) {
  509. have_v6_break = 1;
  510. in_v6_break = 1;
  511. tp[0] = ':';
  512. tp++;
  513. }
  514. if (n % 16 > 8) {
  515. /* Partial short */
  516. if (cp[0] != 0) {
  517. if (tp > (u_char *)name) {
  518. in_v6_break = 0;
  519. tp[0] = ':';
  520. tp++;
  521. }
  522. sprintf((char*)tp, "%x", cp[0] & 0xFF);
  523. } else {
  524. if (!have_v6_break) {
  525. have_v6_break = 1;
  526. in_v6_break = 1;
  527. tp[0] = ':';
  528. tp++;
  529. } else if (!in_v6_break) {
  530. tp[0] = ':';
  531. tp++;
  532. tp[0] = '0';
  533. tp++;
  534. }
  535. }
  536. cp++;
  537. }
  538. for (i = (n + 8) / 16; i < 8; i++) {
  539. GETSHORT(s, cp);
  540. if (s != 0) {
  541. if (tp > (u_char *)name) {
  542. in_v6_break = 0;
  543. tp[0] = ':';
  544. tp++;
  545. }
  546. tp += sprintf((char*)tp,"%x",s);
  547. } else {
  548. if (!have_v6_break) {
  549. have_v6_break = 1;
  550. in_v6_break = 1;
  551. tp[0] = ':';
  552. tp++;
  553. } else if (!in_v6_break) {
  554. tp[0] = ':';
  555. tp++;
  556. tp[0] = '0';
  557. tp++;
  558. }
  559. }
  560. }
  561. if (have_v6_break && in_v6_break) {
  562. tp[0] = ':';
  563. tp++;
  564. }
  565. tp[0] = '\0';
  566. add_assoc_string(*subarray, "ipv6", name, 1);
  567. if (cp < p + dlen) {
  568. n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
  569. if (n < 0) {
  570. return NULL;
  571. }
  572. cp += n;
  573. add_assoc_string(*subarray, "chain", name, 1);
  574. }
  575. break;
  576. case DNS_T_SRV:
  577. add_assoc_string(*subarray, "type", "SRV", 1);
  578. GETSHORT(n, cp);
  579. add_assoc_long(*subarray, "pri", n);
  580. GETSHORT(n, cp);
  581. add_assoc_long(*subarray, "weight", n);
  582. GETSHORT(n, cp);
  583. add_assoc_long(*subarray, "port", n);
  584. n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
  585. if (n < 0) {
  586. return NULL;
  587. }
  588. cp += n;
  589. add_assoc_string(*subarray, "target", name, 1);
  590. break;
  591. case DNS_T_NAPTR:
  592. add_assoc_string(*subarray, "type", "NAPTR", 1);
  593. GETSHORT(n, cp);
  594. add_assoc_long(*subarray, "order", n);
  595. GETSHORT(n, cp);
  596. add_assoc_long(*subarray, "pref", n);
  597. n = (cp[0] & 0xFF);
  598. add_assoc_stringl(*subarray, "flags", (char*)++cp, n, 1);
  599. cp += n;
  600. n = (cp[0] & 0xFF);
  601. add_assoc_stringl(*subarray, "services", (char*)++cp, n, 1);
  602. cp += n;
  603. n = (cp[0] & 0xFF);
  604. add_assoc_stringl(*subarray, "regex", (char*)++cp, n, 1);
  605. cp += n;
  606. n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
  607. if (n < 0) {
  608. return NULL;
  609. }
  610. cp += n;
  611. add_assoc_string(*subarray, "replacement", name, 1);
  612. break;
  613. default:
  614. cp += dlen;
  615. }
  616. add_assoc_string(*subarray, "class", "IN", 1);
  617. add_assoc_long(*subarray, "ttl", ttl);
  618. return cp;
  619. }
  620. /* }}} */
  621. /* {{{ proto array|false dns_get_record(string hostname [, int type[, array authns, array addtl]])
  622. Get any Resource Record corresponding to a given Internet host name */
  623. PHP_FUNCTION(dns_get_record)
  624. {
  625. char *hostname;
  626. int hostname_len;
  627. long type_param = PHP_DNS_ANY;
  628. zval *authns = NULL, *addtl = NULL;
  629. int type_to_fetch;
  630. #if defined(HAVE_DNS_SEARCH)
  631. struct sockaddr_storage from;
  632. uint32_t fromsize = sizeof(from);
  633. dns_handle_t handle;
  634. #elif defined(HAVE_RES_NSEARCH)
  635. struct __res_state state;
  636. struct __res_state *handle = &state;
  637. #endif
  638. HEADER *hp;
  639. querybuf answer;
  640. u_char *cp = NULL, *end = NULL;
  641. int n, qd, an, ns = 0, ar = 0;
  642. int type, first_query = 1, store_results = 1;
  643. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzz", &hostname, &hostname_len, &type_param, &authns, &addtl) == FAILURE) {
  644. return;
  645. }
  646. if (authns) {
  647. zval_dtor(authns);
  648. array_init(authns);
  649. }
  650. if (addtl) {
  651. zval_dtor(addtl);
  652. array_init(addtl);
  653. }
  654. if (type_param & ~PHP_DNS_ALL && type_param != PHP_DNS_ANY) {
  655. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%ld' not supported", type_param);
  656. RETURN_FALSE;
  657. }
  658. /* Initialize the return array */
  659. array_init(return_value);
  660. /* - We emulate an or'ed type mask by querying type by type. (Steps 0 - NUMTYPES-1 )
  661. * If additional info is wanted we check again with DNS_T_ANY (step NUMTYPES / NUMTYPES+1 )
  662. * store_results is used to skip storing the results retrieved in step
  663. * NUMTYPES+1 when results were already fetched.
  664. * - In case of PHP_DNS_ANY we use the directly fetch DNS_T_ANY. (step NUMTYPES+1 )
  665. */
  666. for (type = (type_param == PHP_DNS_ANY ? (PHP_DNS_NUM_TYPES + 1) : 0);
  667. type < (addtl ? (PHP_DNS_NUM_TYPES + 2) : PHP_DNS_NUM_TYPES) || first_query;
  668. type++
  669. ) {
  670. first_query = 0;
  671. switch (type) {
  672. case 0:
  673. type_to_fetch = type_param&PHP_DNS_A ? DNS_T_A : 0;
  674. break;
  675. case 1:
  676. type_to_fetch = type_param&PHP_DNS_NS ? DNS_T_NS : 0;
  677. break;
  678. case 2:
  679. type_to_fetch = type_param&PHP_DNS_CNAME ? DNS_T_CNAME : 0;
  680. break;
  681. case 3:
  682. type_to_fetch = type_param&PHP_DNS_SOA ? DNS_T_SOA : 0;
  683. break;
  684. case 4:
  685. type_to_fetch = type_param&PHP_DNS_PTR ? DNS_T_PTR : 0;
  686. break;
  687. case 5:
  688. type_to_fetch = type_param&PHP_DNS_HINFO ? DNS_T_HINFO : 0;
  689. break;
  690. case 6:
  691. type_to_fetch = type_param&PHP_DNS_MX ? DNS_T_MX : 0;
  692. break;
  693. case 7:
  694. type_to_fetch = type_param&PHP_DNS_TXT ? DNS_T_TXT : 0;
  695. break;
  696. case 8:
  697. type_to_fetch = type_param&PHP_DNS_AAAA ? DNS_T_AAAA : 0;
  698. break;
  699. case 9:
  700. type_to_fetch = type_param&PHP_DNS_SRV ? DNS_T_SRV : 0;
  701. break;
  702. case 10:
  703. type_to_fetch = type_param&PHP_DNS_NAPTR ? DNS_T_NAPTR : 0;
  704. break;
  705. case 11:
  706. type_to_fetch = type_param&PHP_DNS_A6 ? DNS_T_A6 : 0;
  707. break;
  708. case PHP_DNS_NUM_TYPES:
  709. store_results = 0;
  710. continue;
  711. default:
  712. case (PHP_DNS_NUM_TYPES + 1):
  713. type_to_fetch = DNS_T_ANY;
  714. break;
  715. }
  716. if (type_to_fetch) {
  717. #if defined(HAVE_DNS_SEARCH)
  718. handle = dns_open(NULL);
  719. if (handle == NULL) {
  720. RETURN_FALSE;
  721. }
  722. #elif defined(HAVE_RES_NSEARCH)
  723. memset(&state, 0, sizeof(state));
  724. if (res_ninit(handle)) {
  725. RETURN_FALSE;
  726. }
  727. #else
  728. res_init();
  729. #endif
  730. n = php_dns_search(handle, hostname, C_IN, type_to_fetch, answer.qb2, sizeof answer);
  731. if (n < 0) {
  732. php_dns_free_handle(handle);
  733. continue;
  734. }
  735. cp = answer.qb2 + HFIXEDSZ;
  736. end = answer.qb2 + n;
  737. hp = (HEADER *)&answer;
  738. qd = ntohs(hp->qdcount);
  739. an = ntohs(hp->ancount);
  740. ns = ntohs(hp->nscount);
  741. ar = ntohs(hp->arcount);
  742. /* Skip QD entries, they're only used by dn_expand later on */
  743. while (qd-- > 0) {
  744. n = dn_skipname(cp, end);
  745. if (n < 0) {
  746. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to parse DNS data received");
  747. zval_dtor(return_value);
  748. php_dns_free_handle(handle);
  749. RETURN_FALSE;
  750. }
  751. cp += n + QFIXEDSZ;
  752. }
  753. /* YAY! Our real answers! */
  754. while (an-- && cp && cp < end) {
  755. zval *retval;
  756. cp = php_parserr(cp, &answer, type_to_fetch, store_results, &retval);
  757. if (retval != NULL && store_results) {
  758. add_next_index_zval(return_value, retval);
  759. }
  760. }
  761. if (authns || addtl) {
  762. /* List of Authoritative Name Servers
  763. * Process when only requesting addtl so that we can skip through the section
  764. */
  765. while (ns-- > 0 && cp && cp < end) {
  766. zval *retval = NULL;
  767. cp = php_parserr(cp, &answer, DNS_T_ANY, authns != NULL, &retval);
  768. if (retval != NULL) {
  769. add_next_index_zval(authns, retval);
  770. }
  771. }
  772. }
  773. if (addtl) {
  774. /* Additional records associated with authoritative name servers */
  775. while (ar-- > 0 && cp && cp < end) {
  776. zval *retval = NULL;
  777. cp = php_parserr(cp, &answer, DNS_T_ANY, 1, &retval);
  778. if (retval != NULL) {
  779. add_next_index_zval(addtl, retval);
  780. }
  781. }
  782. }
  783. php_dns_free_handle(handle);
  784. }
  785. }
  786. }
  787. /* }}} */
  788. /* {{{ proto bool dns_get_mx(string hostname, array mxhosts [, array weight])
  789. Get MX records corresponding to a given Internet host name */
  790. PHP_FUNCTION(dns_get_mx)
  791. {
  792. char *hostname;
  793. int hostname_len;
  794. zval *mx_list, *weight_list = NULL;
  795. int count, qdc;
  796. u_short type, weight;
  797. u_char ans[MAXPACKET];
  798. char buf[MAXHOSTNAMELEN];
  799. HEADER *hp;
  800. u_char *cp, *end;
  801. int i;
  802. #if defined(HAVE_DNS_SEARCH)
  803. struct sockaddr_storage from;
  804. uint32_t fromsize = sizeof(from);
  805. dns_handle_t handle;
  806. #elif defined(HAVE_RES_NSEARCH)
  807. struct __res_state state;
  808. struct __res_state *handle = &state;
  809. #endif
  810. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|z", &hostname, &hostname_len, &mx_list, &weight_list) == FAILURE) {
  811. return;
  812. }
  813. zval_dtor(mx_list);
  814. array_init(mx_list);
  815. if (weight_list) {
  816. zval_dtor(weight_list);
  817. array_init(weight_list);
  818. }
  819. #if defined(HAVE_DNS_SEARCH)
  820. handle = dns_open(NULL);
  821. if (handle == NULL) {
  822. RETURN_FALSE;
  823. }
  824. #elif defined(HAVE_RES_NSEARCH)
  825. memset(&state, 0, sizeof(state));
  826. if (res_ninit(handle)) {
  827. RETURN_FALSE;
  828. }
  829. #else
  830. res_init();
  831. #endif
  832. i = php_dns_search(handle, hostname, C_IN, DNS_T_MX, (u_char *)&ans, sizeof(ans));
  833. if (i < 0) {
  834. RETURN_FALSE;
  835. }
  836. if (i > (int)sizeof(ans)) {
  837. i = sizeof(ans);
  838. }
  839. hp = (HEADER *)&ans;
  840. cp = (u_char *)&ans + HFIXEDSZ;
  841. end = (u_char *)&ans +i;
  842. for (qdc = ntohs((unsigned short)hp->qdcount); qdc--; cp += i + QFIXEDSZ) {
  843. if ((i = dn_skipname(cp, end)) < 0 ) {
  844. php_dns_free_handle(handle);
  845. RETURN_FALSE;
  846. }
  847. }
  848. count = ntohs((unsigned short)hp->ancount);
  849. while (--count >= 0 && cp < end) {
  850. if ((i = dn_skipname(cp, end)) < 0 ) {
  851. php_dns_free_handle(handle);
  852. RETURN_FALSE;
  853. }
  854. cp += i;
  855. GETSHORT(type, cp);
  856. cp += INT16SZ + INT32SZ;
  857. GETSHORT(i, cp);
  858. if (type != DNS_T_MX) {
  859. cp += i;
  860. continue;
  861. }
  862. GETSHORT(weight, cp);
  863. if ((i = dn_expand(ans, end, cp, buf, sizeof(buf)-1)) < 0) {
  864. php_dns_free_handle(handle);
  865. RETURN_FALSE;
  866. }
  867. cp += i;
  868. add_next_index_string(mx_list, buf, 1);
  869. if (weight_list) {
  870. add_next_index_long(weight_list, weight);
  871. }
  872. }
  873. php_dns_free_handle(handle);
  874. RETURN_TRUE;
  875. }
  876. /* }}} */
  877. #endif /* HAVE_FULL_DNS_FUNCS */
  878. #endif /* !defined(PHP_WIN32) && (HAVE_DNS_SEARCH_FUNC && !(defined(__BEOS__) || defined(NETWARE))) */
  879. #if HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32)
  880. PHP_MINIT_FUNCTION(dns) {
  881. REGISTER_LONG_CONSTANT("DNS_A", PHP_DNS_A, CONST_CS | CONST_PERSISTENT);
  882. REGISTER_LONG_CONSTANT("DNS_NS", PHP_DNS_NS, CONST_CS | CONST_PERSISTENT);
  883. REGISTER_LONG_CONSTANT("DNS_CNAME", PHP_DNS_CNAME, CONST_CS | CONST_PERSISTENT);
  884. REGISTER_LONG_CONSTANT("DNS_SOA", PHP_DNS_SOA, CONST_CS | CONST_PERSISTENT);
  885. REGISTER_LONG_CONSTANT("DNS_PTR", PHP_DNS_PTR, CONST_CS | CONST_PERSISTENT);
  886. REGISTER_LONG_CONSTANT("DNS_HINFO", PHP_DNS_HINFO, CONST_CS | CONST_PERSISTENT);
  887. REGISTER_LONG_CONSTANT("DNS_MX", PHP_DNS_MX, CONST_CS | CONST_PERSISTENT);
  888. REGISTER_LONG_CONSTANT("DNS_TXT", PHP_DNS_TXT, CONST_CS | CONST_PERSISTENT);
  889. REGISTER_LONG_CONSTANT("DNS_SRV", PHP_DNS_SRV, CONST_CS | CONST_PERSISTENT);
  890. REGISTER_LONG_CONSTANT("DNS_NAPTR", PHP_DNS_NAPTR, CONST_CS | CONST_PERSISTENT);
  891. REGISTER_LONG_CONSTANT("DNS_AAAA", PHP_DNS_AAAA, CONST_CS | CONST_PERSISTENT);
  892. REGISTER_LONG_CONSTANT("DNS_A6", PHP_DNS_A6, CONST_CS | CONST_PERSISTENT);
  893. REGISTER_LONG_CONSTANT("DNS_ANY", PHP_DNS_ANY, CONST_CS | CONST_PERSISTENT);
  894. REGISTER_LONG_CONSTANT("DNS_ALL", PHP_DNS_ALL, CONST_CS | CONST_PERSISTENT);
  895. return SUCCESS;
  896. }
  897. #endif /* HAVE_FULL_DNS_FUNCS */
  898. /*
  899. * Local variables:
  900. * tab-width: 4
  901. * c-basic-offset: 4
  902. * End:
  903. * vim600: sw=4 ts=4 fdm=marker
  904. * vim<600: sw=4 ts=4
  905. */