> Hi everyone,
>
> I have a need not fullfilled by the current short set of dns functions
> (no way to retrieve aliases), so I wrote a 'gethostent' function, which
> accepts either hostname or ip, and returns a hash equivelant of the C
> hostent struct. patch is attached, if no objections in the next couple
> hours I'll commit it.
>
I have one or two objections of the implementation kind, but in general
I think the function is a good idea:
1) you shouldn't change the return value of php_gethostbyaddr(), or
rather, you shouldn't change it in this way. When we want to start
supporting ipv6 seemlessly, we can't return a hostent.
2) some of your macro's (*cough* pval_destructor *cough*) are phpv3
macro's, please update these to the proper macro's (actually in this
case, their is no reason to destroy the return_value before array_init).
3) use sizeof("blah") not strlen("blah") + 1, yes, I'm being picky. But
sizeof is compile time, and therefore will save a strlen().
-Sterling
> Shane
>
> ? dns.diff
> Index: basic_functions.c
> ===================================================================
> RCS file: /repository/php4/ext/standard/basic_functions.c,v
> retrieving revision 1.470
> diff -d -u -r1.470 basic_functions.c
> --- basic_functions.c 16 Apr 2002 22:14:19 -0000 1.470
> +++ basic_functions.c 18 Apr 2002 20:50:42 -0000
> @@ -406,6 +406,7 @@
> PHP_FE(gethostbyaddr,
> NULL)
> PHP_FE(gethostbyname,
> NULL)
> PHP_FE(gethostbynamel,
> NULL)
> + PHP_FE(gethostent,
> NULL)
>
> #if HAVE_RES_SEARCH && !(defined(__BEOS__) || defined(PHP_WIN32))
> PHP_FE(checkdnsrr,
> NULL)
> Index: dns.c
> ===================================================================
> RCS file: /repository/php4/ext/standard/dns.c,v
> retrieving revision 1.38
> diff -d -u -r1.38 dns.c
> --- dns.c 28 Feb 2002 08:26:44 -0000 1.38
> +++ dns.c 18 Apr 2002 20:50:42 -0000
> @@ -63,7 +63,7 @@
> #include "dns.h"
> /* }}} */
>
> -static char *php_gethostbyaddr(char *ip);
> +struct hostent *php_gethostbyaddr(char *ip);
> static char *php_gethostbyname(char *name);
>
> /* {{{ proto string gethostbyaddr(string ip_address)
> @@ -71,7 +71,8 @@
> PHP_FUNCTION(gethostbyaddr)
> {
> zval **arg;
> - char *addr;
> + char *addr, *ip;
> + struct hostent *hp;
>
> if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
> ZEND_WRONG_PARAM_COUNT();
> @@ -79,7 +80,14 @@
>
> convert_to_string_ex(arg);
>
> - addr = php_gethostbyaddr(Z_STRVAL_PP(arg));
> + ip = Z_STRVAL_PP(arg);
> + hp = php_gethostbyaddr(ip);
> + if (!hp) {
> + addr = estrdup(ip);
> + } else {
> + addr = estrdup(hp->h_name);
> + }
> +
>
> if(addr == NULL) {
> #if HAVE_IPV6 && !defined(__MacOSX__)
> @@ -96,9 +104,80 @@
> }
> /* }}} */
>
> +
> +/* {{{ proto string gethostent(string ip_address|host)
> + Get the Internet hostent structure corresponding to a given IP address or host
>name */
> +PHP_FUNCTION(gethostent)
> +{
> + zval **arg, *hostent_alias=NULL, *hostent_addr_list=NULL;
> + char *ip;
> + struct hostent *hp;
> + struct in_addr in;
> + int i;
> +
> + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
> + ZEND_WRONG_PARAM_COUNT();
> + }
> +
> + convert_to_string_ex(arg);
> +
> + /* start with clean arrays */
> + pval_destructor(return_value);
> + if ( array_init(return_value) == FAILURE ) {
> + RETURN_FALSE;
> + }
> +
> + ip = Z_STRVAL_PP(arg);
> + hp = php_gethostbyaddr(ip);
> + if (!hp) {
> + /* not an ip address, try it as a hostname */
> + hp = gethostbyname(ip);
> + }
> +
> + if (!hp) {
> + RETVAL_FALSE;
> + }
> +
> + MAKE_STD_ZVAL(hostent_alias);
> + MAKE_STD_ZVAL(hostent_addr_list);
> + if ( array_init(hostent_alias) == FAILURE ||
> + array_init(hostent_addr_list) == FAILURE) {
> + RETURN_FALSE;
> + }
> +
> + add_assoc_string(return_value, "name", hp->h_name, 1);
> + add_assoc_long(return_value, "addrtype", hp->h_addrtype);
> + add_assoc_long(return_value, "length", hp->h_length);
> + /* make a sublist of aliases */
> + if (hp->h_aliases[0]) {
> + for (i = 0 ; hp->h_aliases[i] != 0 ; i++) {
> + add_next_index_string(hostent_alias, hp->h_aliases[i], 1);
> + }
> + zend_hash_update(return_value->value.ht, "aliases", strlen("aliases")
>+ 1, (void *)&hostent_alias, sizeof(zval *), NULL);
> + } else {
> + add_assoc_null(return_value, "aliases");
> + }
> + if (hp->h_addr_list[0]) {
> + for (i = 0 ; hp->h_addr_list[i] != 0 ; i++) {
> + in = *(struct in_addr *) hp->h_addr_list[i];
> + add_next_index_string(hostent_addr_list, inet_ntoa(in), 1);
> + if (i ==0) {
> + add_assoc_string(return_value, "addr", inet_ntoa(in),
>1);
> + }
> + }
> + zend_hash_update(return_value->value.ht, "addr_list",
>strlen("addr_list") + 1, (void *)&hostent_addr_list, sizeof(zval *), NULL);
> + } else {
> + add_assoc_null(return_value, "addr");
> + add_assoc_null(return_value, "addr_list");
> + }
> +}
> +/* }}} */
> +
> +
> +
> /* {{{ php_gethostbyaddr
> */
> -static char *php_gethostbyaddr(char *ip)
> +struct hostent *php_gethostbyaddr(char *ip)
> {
> #if HAVE_IPV6 && !defined(__MacOSX__)
> /* MacOSX at this time has support for IPv6, but not inet_pton()
> @@ -128,11 +207,7 @@
> hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
> #endif
>
> - if (!hp) {
> - return estrdup(ip);
> - }
> -
> - return estrdup(hp->h_name);
> + return hp;
> }
> /* }}} */
>
> Index: dns.h
> ===================================================================
> RCS file: /repository/php4/ext/standard/dns.h,v
> retrieving revision 1.11
> diff -d -u -r1.11 dns.h
> --- dns.h 28 Feb 2002 08:26:44 -0000 1.11
> +++ dns.h 18 Apr 2002 20:50:42 -0000
> @@ -24,6 +24,7 @@
> PHP_FUNCTION(gethostbyaddr);
> PHP_FUNCTION(gethostbyname);
> PHP_FUNCTION(gethostbynamel);
> +PHP_FUNCTION(gethostent);
>
> #if HAVE_RES_SEARCH && !(defined(__BEOS__)||defined(PHP_WIN32))
> PHP_FUNCTION(checkdnsrr);
>
>
> --
> PHP Development Mailing List <http://www.php.net/>
> To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php