helly Mon Nov 18 21:34:14 2002 EDT Modified files: /php4/ext/standard basic_functions.c config.m4 dns.c dns.h Log: -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)
Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.543 php4/ext/standard/basic_functions.c:1.544 --- php4/ext/standard/basic_functions.c:1.543 Fri Nov 8 10:49:32 2002 +++ php4/ext/standard/basic_functions.c Mon Nov 18 21:34:13 2002 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.543 2002/11/08 15:49:32 sterling Exp $ */ +/* $Id: basic_functions.c,v 1.544 2002/11/19 02:34:13 helly Exp $ */ #include "php.h" #include "php_streams.h" @@ -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 <stdarg.h> #include <stdlib.h> @@ -434,15 +435,6 @@ 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 @@ 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) @@ -1058,6 +1065,12 @@ # if HAVE_OPENSSL_EXT 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 Index: php4/ext/standard/config.m4 diff -u php4/ext/standard/config.m4:1.47 php4/ext/standard/config.m4:1.48 --- php4/ext/standard/config.m4:1.47 Wed Oct 30 10:11:11 2002 +++ php4/ext/standard/config.m4 Mon Nov 18 21:34:13 2002 @@ -1,4 +1,4 @@ -dnl $Id: config.m4,v 1.47 2002/10/30 15:11:11 hholzgra Exp $ -*- sh -*- +dnl $Id: config.m4,v 1.48 2002/11/19 02:34:13 helly Exp $ -*- sh -*- divert(3)dnl @@ -236,6 +236,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 \ Index: php4/ext/standard/dns.c diff -u php4/ext/standard/dns.c:1.44 php4/ext/standard/dns.c:1.45 --- php4/ext/standard/dns.c:1.44 Fri Oct 18 18:08:23 2002 +++ php4/ext/standard/dns.c Mon Nov 18 21:34:13 2002 @@ -12,15 +12,15 @@ | obtain it through the world-wide-web, please send a note to | | [EMAIL PROTECTED] so we can mail you a copy immediately. | +----------------------------------------------------------------------+ - | Author: | + | Authors: The typical suspects | + | Pollita <[EMAIL PROTECTED]> | + | Marcus Boerger <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: dns.c,v 1.44 2002/10/18 22:08:23 sniper Exp $ */ - -/* {{{ includes - */ +/* $Id: dns.c,v 1.45 2002/11/19 02:34:13 helly Exp $ */ +/* {{{ includes */ #include "php.h" #if HAVE_SYS_SOCKET_H @@ -104,8 +104,7 @@ } /* }}} */ -/* {{{ php_gethostbyaddr - */ +/* {{{ php_gethostbyaddr */ static char *php_gethostbyaddr(char *ip) { #if HAVE_IPV6 && !defined(__MacOSX__) @@ -190,8 +189,7 @@ } /* }}} */ -/* {{{ php_gethostbyname - */ +/* {{{ php_gethostbyname */ static char *php_gethostbyname(char *name) { struct hostent *hp; @@ -211,9 +209,9 @@ #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 @@ } /* }}} */ +#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 <arpa/nameser.h> */ #endif /* HFIXEDSZ */ @@ -277,9 +304,303 @@ #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 @@ } /* }}} */ -#endif +#endif /* HAVE_DNS_FUNCS */ +#endif /* HAVE_RES_SEARCH && !(defined(__BEOS__)||defined(PHP_WIN32) || +defined(NETWARE)) */ + /* * Local variables: * tab-width: 4 Index: php4/ext/standard/dns.h diff -u php4/ext/standard/dns.h:1.11 php4/ext/standard/dns.h:1.12 --- php4/ext/standard/dns.h:1.11 Thu Feb 28 03:26:44 2002 +++ php4/ext/standard/dns.h Mon Nov 18 21:34:13 2002 @@ -12,22 +12,37 @@ | obtain it through the world-wide-web, please send a note to | | [EMAIL PROTECTED] so we can mail you a copy immediately. | +----------------------------------------------------------------------+ - | Author: | + | Authors: The typical suspects | + | Marcus Boerger <[EMAIL PROTECTED]> | + | Pollita <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: dns.h,v 1.11 2002/02/28 08:26:44 sebastian Exp $ */ +/* $Id: dns.h,v 1.12 2002/11/19 02:34:13 helly Exp $ */ #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
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php