dns_win32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2008-2016 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: Pierre A. Joye <pierre@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include <windows.h>
  20. #include <Winbase.h >
  21. #include <Windns.h>
  22. #include "php_dns.h"
  23. #define PHP_DNS_NUM_TYPES 12 /* Number of DNS Types Supported by PHP currently */
  24. #define PHP_DNS_A 0x00000001
  25. #define PHP_DNS_NS 0x00000002
  26. #define PHP_DNS_CNAME 0x00000010
  27. #define PHP_DNS_SOA 0x00000020
  28. #define PHP_DNS_PTR 0x00000800
  29. #define PHP_DNS_HINFO 0x00001000
  30. #define PHP_DNS_MX 0x00004000
  31. #define PHP_DNS_TXT 0x00008000
  32. #define PHP_DNS_A6 0x01000000
  33. #define PHP_DNS_SRV 0x02000000
  34. #define PHP_DNS_NAPTR 0x04000000
  35. #define PHP_DNS_AAAA 0x08000000
  36. #define PHP_DNS_ANY 0x10000000
  37. #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)
  38. PHP_FUNCTION(dns_get_mx) /* {{{ */
  39. {
  40. char *hostname;
  41. int hostname_len;
  42. zval *mx_list, *weight_list = NULL;
  43. DNS_STATUS status; /* Return value of DnsQuery_A() function */
  44. PDNS_RECORD pResult, pRec; /* Pointer to DNS_RECORD structure */
  45. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|z", &hostname, &hostname_len, &mx_list, &weight_list) == FAILURE) {
  46. return;
  47. }
  48. status = DnsQuery_A(hostname, DNS_TYPE_MX, DNS_QUERY_STANDARD, NULL, &pResult, NULL);
  49. if (status) {
  50. RETURN_FALSE;
  51. }
  52. zval_dtor(mx_list);
  53. array_init(mx_list);
  54. if (weight_list) {
  55. zval_dtor(weight_list);
  56. array_init(weight_list);
  57. }
  58. for (pRec = pResult; pRec; pRec = pRec->pNext) {
  59. DNS_SRV_DATA *srv = &pRec->Data.Srv;
  60. if (pRec->wType != DNS_TYPE_MX) {
  61. continue;
  62. }
  63. add_next_index_string(mx_list, pRec->Data.MX.pNameExchange, 1);
  64. if (weight_list) {
  65. add_next_index_long(weight_list, srv->wPriority);
  66. }
  67. }
  68. /* Free memory allocated for DNS records. */
  69. DnsRecordListFree(pResult, DnsFreeRecordListDeep);
  70. RETURN_TRUE;
  71. }
  72. /* }}} */
  73. /* {{{ proto bool dns_check_record(string host [, string type])
  74. Check DNS records corresponding to a given Internet host name or IP address */
  75. PHP_FUNCTION(dns_check_record)
  76. {
  77. char *hostname, *rectype = NULL;
  78. int hostname_len, rectype_len = 0;
  79. int type = DNS_TYPE_MX;
  80. DNS_STATUS status; /* Return value of DnsQuery_A() function */
  81. PDNS_RECORD pResult; /* Pointer to DNS_RECORD structure */
  82. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &hostname, &hostname_len, &rectype, &rectype_len) == FAILURE) {
  83. return;
  84. }
  85. if (hostname_len == 0) {
  86. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host cannot be empty");
  87. RETURN_FALSE;
  88. }
  89. if (rectype) {
  90. if (!strcasecmp("A", rectype)) type = DNS_TYPE_A;
  91. else if (!strcasecmp("NS", rectype)) type = DNS_TYPE_NS;
  92. else if (!strcasecmp("MX", rectype)) type = DNS_TYPE_MX;
  93. else if (!strcasecmp("PTR", rectype)) type = DNS_TYPE_PTR;
  94. else if (!strcasecmp("ANY", rectype)) type = DNS_TYPE_ANY;
  95. else if (!strcasecmp("SOA", rectype)) type = DNS_TYPE_SOA;
  96. else if (!strcasecmp("TXT", rectype)) type = DNS_TYPE_TEXT;
  97. else if (!strcasecmp("CNAME", rectype)) type = DNS_TYPE_CNAME;
  98. else if (!strcasecmp("AAAA", rectype)) type = DNS_TYPE_AAAA;
  99. else if (!strcasecmp("SRV", rectype)) type = DNS_TYPE_SRV;
  100. else if (!strcasecmp("NAPTR", rectype)) type = DNS_TYPE_NAPTR;
  101. else if (!strcasecmp("A6", rectype)) type = DNS_TYPE_A6;
  102. else {
  103. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%s' not supported", rectype);
  104. RETURN_FALSE;
  105. }
  106. }
  107. status = DnsQuery_A(hostname, type, DNS_QUERY_STANDARD, NULL, &pResult, NULL);
  108. if (status) {
  109. RETURN_FALSE;
  110. }
  111. RETURN_TRUE;
  112. }
  113. /* }}} */
  114. /* {{{ php_parserr */
  115. static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, int raw, zval **subarray)
  116. {
  117. int type;
  118. u_long ttl;
  119. type = pRec->wType;
  120. ttl = pRec->dwTtl;
  121. if (type_to_fetch != DNS_TYPE_ANY && type != type_to_fetch) {
  122. return;
  123. }
  124. if (!store) {
  125. return;
  126. }
  127. ALLOC_INIT_ZVAL(*subarray);
  128. array_init(*subarray);
  129. add_assoc_string(*subarray, "host", pRec->pName, 1);
  130. add_assoc_string(*subarray, "class", "IN", 1);
  131. add_assoc_long(*subarray, "ttl", ttl);
  132. if (raw) {
  133. add_assoc_long(*subarray, "type", type);
  134. add_assoc_stringl(*subarray, "data", (char*) &pRec->Data, (uint) pRec->wDataLength, 1);
  135. return;
  136. }
  137. switch (type) {
  138. case DNS_TYPE_A: {
  139. IN_ADDR ipaddr;
  140. ipaddr.S_un.S_addr = (pRec->Data.A.IpAddress);
  141. add_assoc_string(*subarray, "type", "A", 1);
  142. add_assoc_string(*subarray, "ip", inet_ntoa(ipaddr), 1);
  143. break;
  144. }
  145. case DNS_TYPE_MX:
  146. add_assoc_string(*subarray, "type", "MX", 1);
  147. add_assoc_long(*subarray, "pri", pRec->Data.Srv.wPriority);
  148. /* no break; */
  149. case DNS_TYPE_CNAME:
  150. if (type == DNS_TYPE_CNAME) {
  151. add_assoc_string(*subarray, "type", "CNAME", 1);
  152. }
  153. /* no break; */
  154. case DNS_TYPE_NS:
  155. if (type == DNS_TYPE_NS) {
  156. add_assoc_string(*subarray, "type", "NS", 1);
  157. }
  158. /* no break; */
  159. case DNS_TYPE_PTR:
  160. if (type == DNS_TYPE_PTR) {
  161. add_assoc_string(*subarray, "type", "PTR", 1);
  162. }
  163. add_assoc_string(*subarray, "target", pRec->Data.MX.pNameExchange, 1);
  164. break;
  165. /* Not available on windows, the query is possible but there is no DNS_HINFO_DATA structure */
  166. case DNS_TYPE_HINFO:
  167. case DNS_TYPE_TEXT:
  168. {
  169. DWORD i = 0;
  170. DNS_TXT_DATA *data_txt = &pRec->Data.TXT;
  171. DWORD count = data_txt->dwStringCount;
  172. char *txt, *txt_dst;
  173. long txt_len = 0;
  174. zval *entries;
  175. add_assoc_string(*subarray, "type", "TXT", 1);
  176. ALLOC_INIT_ZVAL(entries);
  177. array_init(entries);
  178. for (i = 0; i < count; i++) {
  179. txt_len += strlen(data_txt->pStringArray[i]) + 1;
  180. }
  181. txt = ecalloc(txt_len * 2, 1);
  182. txt_dst = txt;
  183. for (i = 0; i < count; i++) {
  184. int len = strlen(data_txt->pStringArray[i]);
  185. memcpy(txt_dst, data_txt->pStringArray[i], len);
  186. add_next_index_stringl(entries, data_txt->pStringArray[i], len, 1);
  187. txt_dst += len;
  188. }
  189. add_assoc_string(*subarray, "txt", txt, 0);
  190. add_assoc_zval(*subarray, "entries", entries);
  191. }
  192. break;
  193. case DNS_TYPE_SOA:
  194. {
  195. DNS_SOA_DATA *data_soa = &pRec->Data.Soa;
  196. add_assoc_string(*subarray, "type", "SOA", 1);
  197. add_assoc_string(*subarray, "mname", data_soa->pNamePrimaryServer, 1);
  198. add_assoc_string(*subarray, "rname", data_soa->pNameAdministrator, 1);
  199. add_assoc_long(*subarray, "serial", data_soa->dwSerialNo);
  200. add_assoc_long(*subarray, "refresh", data_soa->dwRefresh);
  201. add_assoc_long(*subarray, "retry", data_soa->dwRetry);
  202. add_assoc_long(*subarray, "expire", data_soa->dwExpire);
  203. add_assoc_long(*subarray, "minimum-ttl", data_soa->dwDefaultTtl);
  204. }
  205. break;
  206. case DNS_TYPE_AAAA:
  207. {
  208. DNS_AAAA_DATA *data_aaaa = &pRec->Data.AAAA;
  209. char buf[sizeof("AAAA:AAAA:AAAA:AAAA:AAAA:AAAA:AAAA:AAAA")];
  210. char *tp = buf;
  211. int i;
  212. unsigned short out[8];
  213. int have_v6_break = 0, in_v6_break = 0;
  214. for (i = 0; i < 4; ++i) {
  215. DWORD chunk = data_aaaa->Ip6Address.IP6Dword[i];
  216. out[i * 2] = htons(LOWORD(chunk));
  217. out[i * 2 + 1] = htons(HIWORD(chunk));
  218. }
  219. for(i=0; i < 8; i++) {
  220. if (out[i] != 0) {
  221. if (tp > (u_char *)buf) {
  222. in_v6_break = 0;
  223. tp[0] = ':';
  224. tp++;
  225. }
  226. tp += sprintf((char*)tp,"%x", out[i]);
  227. } else {
  228. if (!have_v6_break) {
  229. have_v6_break = 1;
  230. in_v6_break = 1;
  231. tp[0] = ':';
  232. tp++;
  233. } else if (!in_v6_break) {
  234. tp[0] = ':';
  235. tp++;
  236. tp[0] = '0';
  237. tp++;
  238. }
  239. }
  240. }
  241. if (have_v6_break && in_v6_break) {
  242. tp[0] = ':';
  243. tp++;
  244. }
  245. tp[0] = '\0';
  246. add_assoc_string(*subarray, "type", "AAAA", 1);
  247. add_assoc_string(*subarray, "ipv6", buf, 1);
  248. }
  249. break;
  250. #if 0
  251. /* Won't be implemented. A6 is deprecated. (Pierre) */
  252. case DNS_TYPE_A6:
  253. break;
  254. #endif
  255. case DNS_TYPE_SRV:
  256. {
  257. DNS_SRV_DATA *data_srv = &pRec->Data.Srv;
  258. add_assoc_string(*subarray, "type", "SRV", 1);
  259. add_assoc_long(*subarray, "pri", data_srv->wPriority);
  260. add_assoc_long(*subarray, "weight", data_srv->wWeight);
  261. add_assoc_long(*subarray, "port", data_srv->wPort);
  262. add_assoc_string(*subarray, "target", data_srv->pNameTarget, 1);
  263. }
  264. break;
  265. #if _MSC_VER >= 1500
  266. case DNS_TYPE_NAPTR:
  267. {
  268. DNS_NAPTR_DATA * data_naptr = &pRec->Data.Naptr;
  269. add_assoc_string(*subarray, "type", "NAPTR", 1);
  270. add_assoc_long(*subarray, "order", data_naptr->wOrder);
  271. add_assoc_long(*subarray, "pref", data_naptr->wPreference);
  272. add_assoc_string(*subarray, "flags", data_naptr->pFlags, 1);
  273. add_assoc_string(*subarray, "services", data_naptr->pService, 1);
  274. add_assoc_string(*subarray, "regex", data_naptr->pRegularExpression, 1);
  275. add_assoc_string(*subarray, "replacement", data_naptr->pReplacement, 1);
  276. }
  277. break;
  278. #endif
  279. default:
  280. /* unknown type */
  281. zval_ptr_dtor(subarray);
  282. *subarray = NULL;
  283. return;
  284. }
  285. }
  286. /* }}} */
  287. /* {{{ proto array|false dns_get_record(string hostname [, int type[, array authns, array addtl]])
  288. Get any Resource Record corresponding to a given Internet host name */
  289. PHP_FUNCTION(dns_get_record)
  290. {
  291. char *hostname;
  292. int hostname_len;
  293. long type_param = PHP_DNS_ANY;
  294. zval *authns = NULL, *addtl = NULL;
  295. int type, type_to_fetch, first_query = 1, store_results = 1;
  296. zend_bool raw = 0;
  297. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz!z!b",
  298. &hostname, &hostname_len, &type_param, &authns, &addtl, &raw) == FAILURE) {
  299. return;
  300. }
  301. if (authns) {
  302. zval_dtor(authns);
  303. array_init(authns);
  304. }
  305. if (addtl) {
  306. zval_dtor(addtl);
  307. array_init(addtl);
  308. }
  309. if (!raw) {
  310. if ((type_param & ~PHP_DNS_ALL) && (type_param != PHP_DNS_ANY)) {
  311. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%ld' not supported", type_param);
  312. RETURN_FALSE;
  313. }
  314. } else {
  315. if ((type_param < 1) || (type_param > 0xFFFF)) {
  316. php_error_docref(NULL TSRMLS_CC, E_WARNING,
  317. "Numeric DNS record type must be between 1 and 65535, '%ld' given", type_param);
  318. RETURN_FALSE;
  319. }
  320. }
  321. /* Initialize the return array */
  322. array_init(return_value);
  323. if (raw) {
  324. type = -1;
  325. } else if (type_param == PHP_DNS_ANY) {
  326. type = PHP_DNS_NUM_TYPES + 1;
  327. } else {
  328. type = 0;
  329. }
  330. for ( ;
  331. type < (addtl ? (PHP_DNS_NUM_TYPES + 2) : PHP_DNS_NUM_TYPES) || first_query;
  332. type++
  333. ) {
  334. DNS_STATUS status; /* Return value of DnsQuery_A() function */
  335. PDNS_RECORD pResult, pRec; /* Pointer to DNS_RECORD structure */
  336. first_query = 0;
  337. switch (type) {
  338. case -1: /* raw */
  339. type_to_fetch = type_param;
  340. /* skip over the rest and go directly to additional records */
  341. type = PHP_DNS_NUM_TYPES - 1;
  342. break;
  343. case 0:
  344. type_to_fetch = type_param&PHP_DNS_A ? DNS_TYPE_A : 0;
  345. break;
  346. case 1:
  347. type_to_fetch = type_param&PHP_DNS_NS ? DNS_TYPE_NS : 0;
  348. break;
  349. case 2:
  350. type_to_fetch = type_param&PHP_DNS_CNAME ? DNS_TYPE_CNAME : 0;
  351. break;
  352. case 3:
  353. type_to_fetch = type_param&PHP_DNS_SOA ? DNS_TYPE_SOA : 0;
  354. break;
  355. case 4:
  356. type_to_fetch = type_param&PHP_DNS_PTR ? DNS_TYPE_PTR : 0;
  357. break;
  358. case 5:
  359. type_to_fetch = type_param&PHP_DNS_HINFO ? DNS_TYPE_HINFO : 0;
  360. break;
  361. case 6:
  362. type_to_fetch = type_param&PHP_DNS_MX ? DNS_TYPE_MX : 0;
  363. break;
  364. case 7:
  365. type_to_fetch = type_param&PHP_DNS_TXT ? DNS_TYPE_TEXT : 0;
  366. break;
  367. case 8:
  368. type_to_fetch = type_param&PHP_DNS_AAAA ? DNS_TYPE_AAAA : 0;
  369. break;
  370. case 9:
  371. type_to_fetch = type_param&PHP_DNS_SRV ? DNS_TYPE_SRV : 0;
  372. break;
  373. case 10:
  374. type_to_fetch = type_param&PHP_DNS_NAPTR ? DNS_TYPE_NAPTR : 0;
  375. break;
  376. case 11:
  377. type_to_fetch = type_param&PHP_DNS_A6 ? DNS_TYPE_A6 : 0;
  378. break;
  379. case PHP_DNS_NUM_TYPES:
  380. store_results = 0;
  381. continue;
  382. default:
  383. case (PHP_DNS_NUM_TYPES + 1):
  384. type_to_fetch = DNS_TYPE_ANY;
  385. break;
  386. }
  387. if (type_to_fetch) {
  388. status = DnsQuery_A(hostname, type_to_fetch, DNS_QUERY_STANDARD, NULL, &pResult, NULL);
  389. if (status) {
  390. if (status == DNS_INFO_NO_RECORDS || status == DNS_ERROR_RCODE_NAME_ERROR) {
  391. continue;
  392. } else {
  393. php_error_docref(NULL TSRMLS_CC, E_WARNING, "DNS Query failed");
  394. zval_dtor(return_value);
  395. RETURN_FALSE;
  396. }
  397. }
  398. for (pRec = pResult; pRec; pRec = pRec->pNext) {
  399. DNS_SRV_DATA *srv = &pRec->Data.Srv;
  400. zval *retval = NULL;
  401. if (pRec->Flags.S.Section == DnsSectionAnswer) {
  402. php_parserr(pRec, type_to_fetch, store_results, raw, &retval);
  403. if (retval != NULL && store_results) {
  404. add_next_index_zval(return_value, retval);
  405. }
  406. }
  407. if (authns && pRec->Flags.S.Section == DnsSectionAuthority) {
  408. php_parserr(pRec, type_to_fetch, 1, raw, &retval);
  409. if (retval != NULL) {
  410. add_next_index_zval(authns, retval);
  411. }
  412. }
  413. /* Stupid typo in PSDK 6.1, WinDNS.h(1258)... */
  414. #ifndef DnsSectionAdditional
  415. # ifdef DnsSectionAddtional
  416. # define DnsSectionAdditional DnsSectionAddtional
  417. # else
  418. # define DnsSectionAdditional 3
  419. # endif
  420. #endif
  421. if (addtl && pRec->Flags.S.Section == DnsSectionAdditional) {
  422. php_parserr(pRec, type_to_fetch, 1, raw, &retval);
  423. if (retval != NULL) {
  424. add_next_index_zval(addtl, retval);
  425. }
  426. }
  427. }
  428. /* Free memory allocated for DNS records. */
  429. DnsRecordListFree(pResult, DnsFreeRecordListDeep);
  430. }
  431. }
  432. }
  433. /* }}} */