From 3ca6344e57de58719554f45fc5889ded774b265c Mon Sep 17 00:00:00 2001 From: Marcus Boerger Date: Tue, 19 Nov 2002 02:34:13 +0000 Subject: [PATCH] -rename checkdnsrr to dns_check_record, keep old name as alias -rename getmxrr to dns_get_mx, keep old name as alias -added dns_get_record @Added dns_get_record() which allows to retrieve DNS information about @a host. (Marcus, Pollita) --- ext/standard/basic_functions.c | 31 ++- ext/standard/config.m4 | 9 + ext/standard/dns.c | 349 +++++++++++++++++++++++++++++++-- ext/standard/dns.h | 21 +- 4 files changed, 385 insertions(+), 25 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 93d9385d1e..dde522db33 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -31,6 +31,7 @@ #include "ext/standard/info.h" #include "ext/session/php_session.h" #include "zend_operators.h" +#include "ext/standard/dns.h" #include #include @@ -434,15 +435,6 @@ function_entry basic_functions[] = { PHP_FE(getprotobynumber, NULL) #endif - PHP_FE(gethostbyaddr, NULL) - PHP_FE(gethostbyname, NULL) - PHP_FE(gethostbynamel, NULL) - -#if HAVE_RES_SEARCH && !(defined(__BEOS__) || defined(PHP_WIN32) || defined(NETWARE)) - PHP_FE(checkdnsrr, NULL) - PHP_FE(getmxrr,second_and_third_args_force_ref) -#endif - PHP_FE(getmyuid, NULL) PHP_FE(getmygid, NULL) PHP_FE(getmypid, NULL) @@ -576,6 +568,21 @@ function_entry basic_functions[] = { PHP_FE(is_uploaded_file, NULL) PHP_FE(move_uploaded_file, NULL) + /* functions from dns.c */ + PHP_FE(gethostbyaddr, NULL) + PHP_FE(gethostbyname, NULL) + PHP_FE(gethostbynamel, NULL) + +#if HAVE_RES_SEARCH && !(defined(__BEOS__) || defined(PHP_WIN32) || defined(NETWARE)) + PHP_FE(dns_check_record, NULL) + PHP_FALIAS(checkdnsrr, dns_check_record, NULL) +# if HAVE_DNS_FUNCS + PHP_FE(dns_get_record, third_and_rest_force_ref) + PHP_FE(dns_get_mx, second_and_third_args_force_ref) + PHP_FALIAS(getmxrr, dns_get_mx, NULL) +# endif +#endif + /* functions from type.c */ PHP_FE(intval, NULL) PHP_FE(floatval, NULL) @@ -1059,6 +1066,12 @@ PHP_MINIT_FUNCTION(basic) php_register_url_stream_wrapper("https", &php_stream_http_wrapper TSRMLS_CC); php_register_url_stream_wrapper("ftps", &php_stream_ftp_wrapper TSRMLS_CC); # endif +#endif + +#if HAVE_RES_SEARCH && !(defined(__BEOS__)||defined(PHP_WIN32) || defined(NETWARE)) +# if HAVE_DNS_FUNCS + PHP_MINIT(dns) (INIT_FUNC_ARGS_PASSTHRU); +# endif #endif return SUCCESS; diff --git a/ext/standard/config.m4 b/ext/standard/config.m4 index 9d4fc7b985..cf0f0c2eeb 100644 --- a/ext/standard/config.m4 +++ b/ext/standard/config.m4 @@ -237,6 +237,15 @@ if test "$PHP_SAPI" = "cgi"; then AC_DEFINE(ENABLE_CHROOT_FUNC, 1, [Whether to enable chroot() function]) fi +dnl +dnl Detect library functions needed by php dns_xxx functions +dnl ext/standard/dns.h will collect these in a single define: HAVE_DNS_FUNCS +dnl +PHP_CHECK_FUNC(res_nmkquery, resolv, bind, socket) +PHP_CHECK_FUNC(res_nsend, resolv, bind, socket) +PHP_CHECK_FUNC(dn_expand, resolv, bind, socket) +dnl already done PHP_CHECK_FUNC(dn_skipname, resolv, bind, socket) + PHP_NEW_EXTENSION(standard, array.c base64.c basic_functions.c browscap.c crc32.c crypt.c \ cyr_convert.c datetime.c dir.c dl.c dns.c exec.c file.c filestat.c \ flock_compat.c formatted_print.c fsock.c head.c html.c image.c \ diff --git a/ext/standard/dns.c b/ext/standard/dns.c index 901c6c6322..cb68897944 100644 --- a/ext/standard/dns.c +++ b/ext/standard/dns.c @@ -12,15 +12,15 @@ | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ - | Author: | + | Authors: The typical suspects | + | Pollita | + | Marcus Boerger | +----------------------------------------------------------------------+ */ /* $Id$ */ -/* {{{ includes - */ - +/* {{{ includes */ #include "php.h" #if HAVE_SYS_SOCKET_H @@ -104,8 +104,7 @@ PHP_FUNCTION(gethostbyaddr) } /* }}} */ -/* {{{ php_gethostbyaddr - */ +/* {{{ php_gethostbyaddr */ static char *php_gethostbyaddr(char *ip) { #if HAVE_IPV6 && !defined(__MacOSX__) @@ -190,8 +189,7 @@ PHP_FUNCTION(gethostbynamel) } /* }}} */ -/* {{{ php_gethostbyname - */ +/* {{{ php_gethostbyname */ static char *php_gethostbyname(char *name) { struct hostent *hp; @@ -211,9 +209,9 @@ static char *php_gethostbyname(char *name) #if HAVE_RES_SEARCH && !(defined(__BEOS__)||defined(PHP_WIN32) || defined(NETWARE)) -/* {{{ proto int checkdnsrr(string host [, string type]) +/* {{{ proto int dns_check_recored(string host [, string type]) Check DNS records corresponding to a given Internet host name or IP address */ -PHP_FUNCTION(checkdnsrr) +PHP_FUNCTION(dns_check_record) { zval **arg1, **arg2; int type, i; @@ -265,6 +263,35 @@ PHP_FUNCTION(checkdnsrr) } /* }}} */ +#if HAVE_DNS_FUNCS + +/* PHP_DNS_xx = 1<<(T_xx-1) */ +#define PHP_DNS_A 0x00000001 +#define PHP_DNS_NS 0x00000002 +#define PHP_DNS_CNAME 0x00000020 +#define PHP_DNS_SOA 0x00000040 +#define PHP_DNS_PTR 0x00000800 +#define PHP_DNS_HINFO 0x00001000 +#define PHP_DNS_MX 0x00004000 +#define PHP_DNS_TXT 0x00008000 + +#define PHP_DNS_ANY 0x10000000 +#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_MINIT_FUNCTION(dns) { + REGISTER_LONG_CONSTANT("DNS_A", PHP_DNS_A, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("DNS_NS", PHP_DNS_NS, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("DNS_CNAME", PHP_DNS_CNAME, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("DNS_SOA", PHP_DNS_SOA, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("DNS_PTR", PHP_DNS_PTR, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("DNS_HINFO", PHP_DNS_HINFO, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("DNS_MX", PHP_DNS_MX, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("DNS_TXT", PHP_DNS_TXT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("DNS_ANY", PHP_DNS_ANY, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("DNS_ALL", PHP_DNS_ALL, CONST_CS | CONST_PERSISTENT); + return SUCCESS; +} + #ifndef HFIXEDSZ #define HFIXEDSZ 12 /* fixed data in header */ #endif /* HFIXEDSZ */ @@ -277,9 +304,303 @@ PHP_FUNCTION(checkdnsrr) #define MAXHOSTNAMELEN 256 #endif /* MAXHOSTNAMELEN */ -/* {{{ proto int getmxrr(string hostname, array mxhosts [, array weight]) +#ifndef MAXRESOURCERECORDS +#define MAXRESOURCERECORDS 64 +#endif /* MAXRESOURCERECORDS */ + +typedef union { + HEADER qb1; + u_char qb2[65536]; +} querybuf; + +/* {{{ php_parserr */ +static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int store, zval **subarray) { + u_short type, class, dlen; + u_long ttl; + long n, i; + char name[MAXHOSTNAMELEN]; + + n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof(name)) - 2); + if (n < 0) { + return NULL; + } + cp += n; + + GETSHORT(type, cp); + GETSHORT(class, cp); + GETLONG(ttl, cp); + GETSHORT(dlen, cp); + if (type_to_fetch != T_ANY && type != type_to_fetch) { + /* Should never actually occour */ + return NULL; + } + + if (!store) { + *subarray = NULL; + cp += dlen; + return cp; + } + + MAKE_STD_ZVAL(*subarray); + if (array_init(*subarray) != SUCCESS) { + return NULL; + } + + add_assoc_string(*subarray, "host", name, 1); + switch (type) { + case T_A: + add_assoc_string(*subarray, "type", "A", 1); + sprintf(name, "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]); + add_assoc_string(*subarray, "ip", name, 1); + cp += dlen; + break; + case T_MX: + add_assoc_string(*subarray, "type", "MX", 1); + GETSHORT(n, cp); + add_assoc_long(*subarray, "pri", n); + /* no break; */ + case T_CNAME: + if (type == T_CNAME) + add_assoc_string(*subarray, "type", "CNAME", 1); + /* no break; */ + case T_NS: + if (type == T_NS) + add_assoc_string(*subarray, "type", "NS", 1); + /* no break; */ + case T_PTR: + if (type == T_PTR) + add_assoc_string(*subarray, "type", "PTR", 1); + n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2); + if (n < 0) { + return NULL; + } + cp += n; + add_assoc_string(*subarray, "target", name, 1); + break; + case T_HINFO: + /* See RFC 1010 for values */ + add_assoc_string(*subarray, "type", "HINFO", 1); + GETSHORT(n, cp); + add_assoc_long(*subarray, "cpu", n); + GETSHORT(n, cp); + add_assoc_long(*subarray, "os", n); + break; + case T_TXT: + add_assoc_string(*subarray, "type", "TXT", 1); + n = cp[0]; + for(i=1; i<=n; i++) + name[i-1] = cp[i]; + name[i-1] = '\0'; + cp += dlen; + add_assoc_string(*subarray, "txt", name, 1); + break; + case T_SOA: + add_assoc_string(*subarray, "type", "SOA", 1); + n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) -2); + if (n < 0) { + return NULL; + } + cp += n; + add_assoc_string(*subarray, "mname", name, 1); + n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) -2); + if (n < 0) { + return NULL; + } + cp += n; + add_assoc_string(*subarray, "rname", name, 1); + GETLONG(n, cp); + add_assoc_long(*subarray, "serial", n); + GETLONG(n, cp); + add_assoc_long(*subarray, "refresh", n); + GETLONG(n, cp); + add_assoc_long(*subarray, "retry", n); + GETLONG(n, cp); + add_assoc_long(*subarray, "expire", n); + GETLONG(n, cp); + add_assoc_long(*subarray, "minimum-ttl", n); + break; + default: + cp += dlen; + } + + add_assoc_string(*subarray, "class", "IN", 1); + add_assoc_long(*subarray, "ttl", ttl); + + return cp; +} +/* }}} */ + +/* {{{ proto array|false dns_get_record(string hostname [, int type[, array authns, array addtl]]) + Get any Resource Record corresponding to a given Internet host name */ +PHP_FUNCTION(dns_get_record) +{ + zval *subarray[MAXRESOURCERECORDS]; + pval *addtl, *host, *authns, *fetch_type; + int addtl_recs = 0; + int type_to_fetch, type_param = PHP_DNS_ANY; + int current_subarray = 0; + struct __res_state res; + HEADER *hp; + querybuf buf, answer, *ans; + u_char *cp = NULL, *end = NULL; + long n, qd, an, ns = 0, ar = 0; + int type, first_query = 1, store_results = 1; + + switch (ZEND_NUM_ARGS()) { + case 1: + if (zend_get_parameters(ht, 1, &host) == FAILURE) { + WRONG_PARAM_COUNT; + } + break; + case 2: + if (zend_get_parameters(ht, 2, &host, &fetch_type) == FAILURE) { + WRONG_PARAM_COUNT; + } + type_param = Z_LVAL_P(fetch_type); + break; + case 4: + if (zend_get_parameters(ht, 4, &host, &fetch_type, &authns, &addtl) == FAILURE) { + WRONG_PARAM_COUNT; + } + type_param = Z_LVAL_P(fetch_type); + pval_destructor(authns); + addtl_recs = 1; /* We want the additional Records */ + if (array_init(authns) != SUCCESS) { + RETURN_FALSE; + } + pval_destructor(addtl); + if (array_init(addtl) != SUCCESS) { + RETURN_FALSE; + } + break; + default: + WRONG_PARAM_COUNT; + } + + convert_to_string(host); + + if (type_param&~PHP_DNS_ALL && type_param!=PHP_DNS_ANY) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%d' not supported", type_param); + RETURN_FALSE; + } + + /* Initialize the return array */ + if (array_init(return_value) != SUCCESS) { + RETURN_FALSE; + } + + /* - We emulate an or'ed type mask by querying type by type. (Steps 0 - 7) + * If additional info is wanted we check again with T_ANY (step 8/9) + * store_results is used to skip storing the results retrieved in step + * 9 when results were already fetched. + * - In case of PHP_DNS_ANY we use the directly fetch T_ANY. (step 9) + */ + for(type = (type_param==PHP_DNS_ANY ? 9 : 0); type < (addtl_recs ? 10 : 8) || first_query; type++) + { + first_query = 0; + switch(type) { + case 0: + type_to_fetch = type_param&PHP_DNS_A ? T_A : 0; + break; + case 1: + type_to_fetch = type_param&PHP_DNS_NS ? T_NS : 0; + break; + case 2: + type_to_fetch = type_param&PHP_DNS_CNAME ? T_CNAME : 0; + break; + case 3: + type_to_fetch = type_param&PHP_DNS_SOA ? T_SOA : 0; + break; + case 4: + type_to_fetch = type_param&PHP_DNS_PTR ? T_PTR : 0; + break; + case 5: + type_to_fetch = type_param&PHP_DNS_HINFO ? T_HINFO : 0; + break; + case 6: + type_to_fetch = type_param&PHP_DNS_MX ? T_MX : 0; + break; + case 7: + type_to_fetch = type_param&PHP_DNS_TXT ? T_TXT : 0; + break; + case 8: + store_results = 0; + continue; + default: + case 9: + type_to_fetch = T_ANY; + break; + } + if (type_to_fetch) { + res_ninit(&res); + res.retrans = 5; + res.options &= ~RES_DEFNAMES; + + n = res_nmkquery(&res, QUERY, Z_STRVAL_P(host), C_IN, type_to_fetch, NULL, 0, NULL, buf.qb2, sizeof buf); + if (n<0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "res_nmkquery() failed"); + zval_ptr_dtor(&return_value); + RETURN_FALSE; + } + n = res_nsend(&res, buf.qb2, n, answer.qb2, sizeof answer); + if (n<0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "res_nsend() failed"); + zval_ptr_dtor(&return_value); + RETURN_FALSE; + } + + cp = answer.qb2 + HFIXEDSZ; + end = answer.qb2 + n; + ans = &answer; + hp = (HEADER *)ans; + qd = ntohs(hp->qdcount); + an = ntohs(hp->ancount); + ns = ntohs(hp->nscount); + ar = ntohs(hp->arcount); + + /* Skip QD entries, they're only used by dn_expand later on */ + while (qd-- > 0) { + n = dn_skipname(cp, end); + if (n < 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to parse DNS data received"); + zval_ptr_dtor(&return_value); + RETURN_FALSE; + } + cp += n + QFIXEDSZ; + } + + /* YAY! Our real answers! */ + while (an-- && cp && cp < end) { + cp = php_parserr(cp, &answer, type_to_fetch, store_results, &subarray[current_subarray]); + if (subarray[current_subarray] != NULL && store_results) + zend_hash_next_index_insert(HASH_OF(return_value), (void *)&subarray[current_subarray], sizeof(zval *), NULL); + current_subarray++; + } + } + } + + if (addtl_recs) { + /* List of Authoritative Name Servers */ + while (ns-- > 0 && cp && cp < end) { + cp = php_parserr(cp, &answer, T_ANY, 1, &subarray[current_subarray]); + if (subarray[current_subarray] != NULL) + zend_hash_next_index_insert(HASH_OF(authns), (void *)&subarray[current_subarray], sizeof(zval *), NULL); + current_subarray++; + } + /* Additional records associated with authoritative name servers */ + while (ar-- > 0 && cp && cp < end) { + cp = php_parserr(cp, &answer, T_ANY, 1, &subarray[current_subarray]); + if (subarray[current_subarray] != NULL) + zend_hash_next_index_insert(HASH_OF(addtl), (void *)&subarray[current_subarray], sizeof(zval *), NULL); + current_subarray++; + } + } +} +/* }}} */ + +/* {{{ proto bool dns_get_mx(string hostname, array mxhosts [, array weight]) Get MX records corresponding to a given Internet host name */ -PHP_FUNCTION(getmxrr) +PHP_FUNCTION(dns_get_mx) { pval *host, *mx_list, *weight_list; int need_weight = 0; @@ -362,7 +683,9 @@ PHP_FUNCTION(getmxrr) } /* }}} */ -#endif +#endif /* HAVE_DNS_FUNCS */ +#endif /* HAVE_RES_SEARCH && !(defined(__BEOS__)||defined(PHP_WIN32) || defined(NETWARE)) */ + /* * Local variables: * tab-width: 4 diff --git a/ext/standard/dns.h b/ext/standard/dns.h index e3f1cc461a..3d6ce8d70e 100644 --- a/ext/standard/dns.h +++ b/ext/standard/dns.h @@ -12,7 +12,9 @@ | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ - | Author: | + | Authors: The typical suspects | + | Marcus Boerger | + | Pollita | +----------------------------------------------------------------------+ */ @@ -21,13 +23,26 @@ #ifndef DNS_H #define DNS_H +#if HAVE_RES_NMKQUERY & HAVE_RES_NSEND & HAVE_DN_EXPAND & HAVE_DN_SKIPNAME +#define HAVE_DNS_FUNCS 1 +#endif + PHP_FUNCTION(gethostbyaddr); PHP_FUNCTION(gethostbyname); PHP_FUNCTION(gethostbynamel); #if HAVE_RES_SEARCH && !(defined(__BEOS__)||defined(PHP_WIN32)) -PHP_FUNCTION(checkdnsrr); -PHP_FUNCTION(getmxrr); + +PHP_FUNCTION(dns_check_record); + +# if HAVE_DNS_FUNCS + +PHP_FUNCTION(dns_get_record); +PHP_FUNCTION(dns_get_mx); + +PHP_MINIT_FUNCTION(dns); + +# endif #endif #ifndef INT16SZ -- 2.40.0