PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/php8/classes/dns.c

https://bitbucket.org/osmanov/pecl-event
C | 272 lines | 159 code | 47 blank | 66 comment | 24 complexity | e51ebf57b53d988ec5ac7a2b8f1fa06c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 8 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2020 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. | Author: Ruslan Osmanov <osmanov@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "../src/common.h"
  19. #include "../src/util.h"
  20. #include "../src/priv.h"
  21. /* {{{ proto EventDnsBase EventDnsBase::__construct(EventBase base, bool initialize);
  22. *
  23. * Returns object representing event dns base.
  24. *
  25. * If the initialize argument is true, it tries to configure the DNS base
  26. * sensibly given your operating system’s default. Otherwise, it leaves the
  27. * event dns base empty, with no nameservers or options configured. In the latter
  28. * case you should configure dns base yourself, e.g. with
  29. * EventDnsBase::parseResolvConf() */
  30. PHP_EVENT_METHOD(EventDnsBase, __construct)
  31. {
  32. php_event_base_t *base;
  33. zval *zbase;
  34. php_event_dns_base_t *dnsb;
  35. zend_bool initialize;
  36. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob",
  37. &zbase, php_event_base_ce, &initialize) == FAILURE) {
  38. return;
  39. }
  40. PHP_EVENT_REQUIRE_BASE_BY_REF(zbase);
  41. base = Z_EVENT_BASE_OBJ_P(zbase);
  42. dnsb = Z_EVENT_DNS_BASE_OBJ_P(getThis());
  43. dnsb->dns_base = evdns_base_new(base->base, initialize);
  44. }
  45. /* }}} */
  46. /* {{{ proto bool EventDnsBase::parseResolvConf(int flags, string filename);
  47. * Scans the resolv.conf formatted file stored in filename, and read in all the
  48. * options from it that are listed in flags */
  49. PHP_EVENT_METHOD(EventDnsBase, parseResolvConf)
  50. {
  51. php_event_dns_base_t *dnsb;
  52. zval *zdns_base = getThis();
  53. zend_long flags;
  54. char *filename;
  55. size_t filename_len;
  56. int ret;
  57. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls",
  58. &flags, &filename, &filename_len) == FAILURE) {
  59. return;
  60. }
  61. if (flags & ~(DNS_OPTION_NAMESERVERS | DNS_OPTION_SEARCH | DNS_OPTION_MISC
  62. | DNS_OPTIONS_ALL)) {
  63. php_error_docref(NULL, E_WARNING,
  64. "Invalid flags");
  65. RETURN_FALSE;
  66. }
  67. dnsb = Z_EVENT_DNS_BASE_OBJ_P(zdns_base);
  68. ret = evdns_base_resolv_conf_parse(dnsb->dns_base, flags, filename);
  69. if (ret) {
  70. char err[40];
  71. switch (ret) {
  72. case 1:
  73. strcpy(err, "Failed to open file");
  74. break;
  75. case 2:
  76. strcpy(err, "Failed to stat file");
  77. break;
  78. case 3:
  79. strcpy(err, "File too large");
  80. break;
  81. case 4:
  82. strcpy(err, "Out of memory");
  83. break;
  84. case 5:
  85. strcpy(err, "Short read from file");
  86. break;
  87. case 6:
  88. strcpy(err, "No nameservers listed in the file");
  89. break;
  90. }
  91. php_error_docref(NULL, E_WARNING, "%s", err);
  92. }
  93. RETVAL_TRUE;
  94. }
  95. /* }}} */
  96. /* {{{ proto bool EventDnsBase::addNameserverIp(string ip);
  97. * Adds a nameserver to an existing evdns_base. It takes the nameserver in a
  98. * text string, either as an IPv4 address, an IPv6 address, an IPv4 address
  99. * with a port (IPv4:Port), or an IPv6 address with a port ([IPv6]:Port).
  100. */
  101. PHP_EVENT_METHOD(EventDnsBase, addNameserverIp)
  102. {
  103. php_event_dns_base_t *dnsb;
  104. zval *zdns_base = getThis();
  105. char *ip;
  106. size_t ip_len;
  107. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
  108. &ip, &ip_len) == FAILURE) {
  109. return;
  110. }
  111. dnsb = Z_EVENT_DNS_BASE_OBJ_P(zdns_base);
  112. if (evdns_base_nameserver_ip_add(dnsb->dns_base, ip)) {
  113. RETURN_FALSE;
  114. }
  115. RETVAL_TRUE;
  116. }
  117. /* }}} */
  118. /* {{{ proto bool EventDnsBase::loadHosts(string hosts);
  119. * Loads a hosts file (in the same format as /etc/hosts) from hosts file
  120. */
  121. PHP_EVENT_METHOD(EventDnsBase, loadHosts)
  122. {
  123. php_event_dns_base_t *dnsb;
  124. zval *zdns_base = getThis();
  125. char *hosts;
  126. size_t hosts_len;
  127. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
  128. &hosts, &hosts_len) == FAILURE) {
  129. return;
  130. }
  131. dnsb = Z_EVENT_DNS_BASE_OBJ_P(zdns_base);
  132. if (evdns_base_load_hosts(dnsb->dns_base, hosts)) {
  133. RETURN_FALSE;
  134. }
  135. RETVAL_TRUE;
  136. }
  137. /* }}} */
  138. /* {{{ proto void EventDnsBase::clearSearch(void);
  139. * Removes all current search suffixes (as configured by the search option)
  140. * from the evdns_base; the evdns_base_search_add() function adds a suffix
  141. */
  142. PHP_EVENT_METHOD(EventDnsBase, clearSearch)
  143. {
  144. php_event_dns_base_t *dnsb;
  145. zval *zdns_base = getThis();
  146. if (zend_parse_parameters_none() == FAILURE) {
  147. return;
  148. }
  149. dnsb = Z_EVENT_DNS_BASE_OBJ_P(zdns_base);
  150. evdns_base_search_clear(dnsb->dns_base);
  151. }
  152. /* }}} */
  153. /* {{{ proto void EventDnsBase::addSearch(string domain);
  154. */
  155. PHP_EVENT_METHOD(EventDnsBase, addSearch)
  156. {
  157. php_event_dns_base_t *dnsb;
  158. zval *zdns_base = getThis();
  159. char *domain;
  160. size_t domain_len;
  161. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
  162. &domain, &domain_len) == FAILURE) {
  163. return;
  164. }
  165. dnsb = Z_EVENT_DNS_BASE_OBJ_P(zdns_base);
  166. evdns_base_search_add(dnsb->dns_base, domain);
  167. }
  168. /* }}} */
  169. /* {{{ proto void EventDnsBase::setSearchNdots(int ndots);
  170. */
  171. PHP_EVENT_METHOD(EventDnsBase, setSearchNdots)
  172. {
  173. php_event_dns_base_t *dnsb;
  174. zval *zdns_base = getThis();
  175. zend_long ndots;
  176. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l",
  177. &ndots) == FAILURE) {
  178. return;
  179. }
  180. dnsb = Z_EVENT_DNS_BASE_OBJ_P(zdns_base);
  181. evdns_base_search_ndots_set(dnsb->dns_base, ndots);
  182. }
  183. /* }}} */
  184. /* {{{ proto bool EventDnsBase::setOption(string option, string value);
  185. */
  186. PHP_EVENT_METHOD(EventDnsBase, setOption)
  187. {
  188. php_event_dns_base_t *dnsb;
  189. zval *zdns_base = getThis();
  190. char *option;
  191. size_t option_len;
  192. char *value;
  193. size_t value_len;
  194. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
  195. &option, &option_len, &value, &value_len) == FAILURE) {
  196. return;
  197. }
  198. dnsb = Z_EVENT_DNS_BASE_OBJ_P(zdns_base);
  199. if (evdns_base_set_option(dnsb->dns_base, option, value)) {
  200. RETURN_FALSE;
  201. }
  202. RETVAL_TRUE;
  203. }
  204. /* }}} */
  205. /* {{{ proto int EventDnsBase::countNameservers(void);
  206. */
  207. PHP_EVENT_METHOD(EventDnsBase, countNameservers)
  208. {
  209. php_event_dns_base_t *dnsb;
  210. zval *zdns_base = getThis();
  211. if (zend_parse_parameters_none() == FAILURE) {
  212. return;
  213. }
  214. dnsb = Z_EVENT_DNS_BASE_OBJ_P(zdns_base);
  215. RETURN_LONG(evdns_base_count_nameservers(dnsb->dns_base));
  216. }
  217. /* }}} */
  218. /*
  219. * Local variables:
  220. * tab-width: 4
  221. * c-basic-offset: 4
  222. * End:
  223. * vim600: noet sw=4 ts=4 sts=4 fdm=marker
  224. * vim<600: noet sw=4 ts=4 sts=4
  225. */