I did:
-change php_error to php_error_docref
-added some error messages which should be rewritten again
i only wanted to point us to the problems.
-collected the autoconf information into one single define which
can be tested
-ws fixes
-minor fixes
-made the old functions alias names to the new dns_xxx functions
For me this stuff works now but i would expect dns_get_error(name,DNS_ANY)
to return all possible records. But when i use the call with DNS_ANY i receive
the NS records. To receive the A and MX entries i have to call the function
with the DNS_A and DNS_MX respectively. See example below
Any comments?
marcus
[marcus@zaphod php4-HEAD]$ php -r 'var_dump(dns_get_record("marcus-boerger.de",DNS_ANY));'
array(3) {
[0]=>
array(5) {
["host"]=>
string(17) "marcus-boerger.de"
["type"]=>
string(2) "NS"
["target"]=>
string(14) "dns.aixtra.net"
["class"]=>
string(2) "IN"
["ttl"]=>
int(86400)
}
[1]=>
array(5) {
["host"]=>
string(17) "marcus-boerger.de"
["type"]=>
string(2) "NS"
["target"]=>
string(20) "ns2.pop-hannover.net"
["class"]=>
string(2) "IN"
["ttl"]=>
int(86400)
}
[2]=>
array(5) {
["host"]=>
string(17) "marcus-boerger.de"
["type"]=>
string(2) "NS"
["target"]=>
string(21) "magrathea.aachalon.de"
["class"]=>
string(2) "IN"
["ttl"]=>
int(86400)
}
}
[marcus@zaphod php4-HEAD]$ php -r 'var_dump(dns_get_record("marcus-boerger.de",DNS_A));'
array(1) {
[0]=>
array(5) {
["host"]=>
string(17) "marcus-boerger.de"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "212.42.230.204"
["class"]=>
string(2) "IN"
["ttl"]=>
int(86400)
}
}
At 22:56 17.11.2002, Pollita wrote:
This is to update configure.in so that configure checks for the existence of res_nmkquery, res_nsend, and dn_expand which are needed by dns_getrecord()..... which is appearantly going to be dns_get_record now....Speaking of which I've also attached dns_get_record.patch.txt to update my previous patch which introduced the function as dns_getrecord Pasting as text as well because of recent not-always-attaching issues in my mail client. (....)
cvs -z3 -q diff basic_functions.c config.m4 dns.c dns.h (in directory
S:\php4-HEAD\ext\standard)
Index: basic_functions.c
===================================================================
RCS file: /repository/php4/ext/standard/basic_functions.c,v
retrieving revision 1.543
diff -u -r1.543 basic_functions.c
--- basic_functions.c 8 Nov 2002 15:49:32 -0000 1.543
+++ basic_functions.c 17 Nov 2002 23:25:02 -0000
@@ -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.543 2002/11/16 13:18:21 pollita 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>
@@ -439,8 +440,13 @@
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)
+ 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
PHP_FE(getmyuid,
NULL)
@@ -1058,6 +1064,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: config.m4
===================================================================
RCS file: /repository/php4/ext/standard/config.m4,v
retrieving revision 1.47
diff -u -r1.47 config.m4
--- config.m4 30 Oct 2002 15:11:11 -0000 1.47
+++ config.m4 17 Nov 2002 23:25:03 -0000
@@ -237,6 +237,11 @@
AC_DEFINE(ENABLE_CHROOT_FUNC, 1, [Whether to enable chroot() function])
fi
+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 \
Index: dns.c
===================================================================
RCS file: /repository/php4/ext/standard/dns.c,v
retrieving revision 1.44
diff -u -r1.44 dns.c
--- dns.c 18 Oct 2002 22:08:23 -0000 1.44
+++ dns.c 17 Nov 2002 23:25:03 -0000
@@ -12,15 +12,14 @@
| 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]> |
+----------------------------------------------------------------------+
*/
-/* $Id: dns.c,v 1.44 2002/10/18 22:08:23 sniper Exp $ */
-
-/* {{{ includes
- */
+/* $Id: dns.c,v 1.44 2002/11/16 17:43:41 pollita Exp $ */
+/* {{{ includes */
#include "php.h"
#if HAVE_SYS_SOCKET_H
@@ -104,8 +103,7 @@
}
/* }}} */
-/* {{{ php_gethostbyaddr
- */
+/* {{{ php_gethostbyaddr */
static char *php_gethostbyaddr(char *ip)
{
#if HAVE_IPV6 && !defined(__MacOSX__)
@@ -190,8 +188,7 @@
}
/* }}} */
-/* {{{ php_gethostbyname
- */
+/* {{{ php_gethostbyname */
static char *php_gethostbyname(char *name)
{
struct hostent *hp;
@@ -211,9 +208,9 @@
#if HAVE_RES_SEARCH && !(defined(__BEOS__)||defined(PHP_WIN32) || defined(NETWARE))
-/* {{{ proto int checkdnsrr(string host [, string type])
+/* {{{ proto int dns_checkd_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 +262,21 @@
}
/* }}} */
+#if HAVE_DNS_FUNCS
+
+PHP_MINIT_FUNCTION(dns) {
+ REGISTER_LONG_CONSTANT("DNS_ANY", T_ANY, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("DNS_A", T_A, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("DNS_MX", T_MX, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("DNS_CNAME", T_CNAME, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("DNS_NS", T_NS, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("DNS_TXT", T_TXT, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("DNS_PTR", T_PTR, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("DNS_SOA", T_SOA, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("DNS_HINFO", T_HINFO, CONST_CS | CONST_PERSISTENT);
+ return SUCCESS;
+}
+
#ifndef HFIXEDSZ
#define HFIXEDSZ 12 /* fixed data in header <arpa/nameser.h> */
#endif /* HFIXEDSZ */
@@ -277,9 +289,254 @@
#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, 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 */
+ cp += dlen;
+ return NULL;
+ }
+
+ 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);
+ case T_CNAME:
+ if (type == T_CNAME)
+ add_assoc_string(*subarray, "type", "CNAME", 1);
+ case T_NS:
+ if (type == T_NS)
+ add_assoc_string(*subarray, "type", "NS", 1);
+ 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 */
+ 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 = T_ANY;
+ int current_subarray = 0;
+ struct __res_state res;
+ HEADER *hp;
+ querybuf buf, answer, *ans;
+ u_char *cp, *end;
+ long n, qd, an, ns, ar;
+
+ 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_to_fetch = Z_LVAL_P(fetch_type);
+ if ( type_to_fetch != T_ANY && type_to_fetch != T_A &&
+ type_to_fetch != T_NS && type_to_fetch != T_MX &&
+ type_to_fetch != T_TXT && type_to_fetch != T_CNAME &&
+ type_to_fetch != T_SOA && type_to_fetch != T_PTR &&
+ type_to_fetch != T_HINFO ) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%d'
+not supported", type_to_fetch);
+ RETURN_FALSE;
+ }
+ break;
+ case 4:
+ if (zend_get_parameters(ht, 4, &host, &fetch_type, &authns,
+&addtl) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ 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;
+ }
+ type_to_fetch = Z_LVAL_P(fetch_type);
+ if ( type_to_fetch != T_ANY && type_to_fetch != T_A &&
+ type_to_fetch != T_NS && type_to_fetch != T_MX &&
+ type_to_fetch != T_TXT && type_to_fetch != T_CNAME &&
+ type_to_fetch != T_SOA && type_to_fetch != T_PTR &&
+ type_to_fetch != T_HINFO ) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%d'
+not supported", type_to_fetch);
+ RETURN_FALSE;
+ }
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ }
+
+ 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");
+ 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");
+ 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, "dn_skipname()
+failed");
+ RETURN_FALSE;
+ }
+ cp += n + QFIXEDSZ;
+ }
+
+ /* Initialize the return array */
+ if (array_init(return_value) != SUCCESS) {
+ RETURN_FALSE;
+ }
+
+ /* YAY! Our real answers! */
+ while (an-- && cp && cp < end) {
+ cp = php_parserr(cp, &answer, type_to_fetch,
+&subarray[current_subarray]);
+ if (subarray[current_subarray] != NULL)
+ 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,
+&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,
+&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 +619,9 @@
}
/* }}} */
-#endif
+#endif /* HAVE_DNS_FUNCS */
+#endif /* HAVE_RES_SEARCH && !(defined(__BEOS__)||defined(PHP_WIN32) ||
+defined(NETWARE)) */
+
/*
* Local variables:
* tab-width: 4
Index: dns.h
===================================================================
RCS file: /repository/php4/ext/standard/dns.h,v
retrieving revision 1.11
diff -u -r1.11 dns.h
--- dns.h 28 Feb 2002 08:26:44 -0000 1.11
+++ dns.h 17 Nov 2002 23:25:03 -0000
@@ -16,18 +16,31 @@
+----------------------------------------------------------------------+
*/
-/* $Id: dns.h,v 1.11 2002/02/28 08:26:44 sebastian Exp $ */
+/* $Id: dns.h,v 1.12 2002/11/16 13:38:33 pollita 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 Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php
