fix php-5.3 incomatibility and several typos
[m6w6/ext-ares] / ares.c
diff --git a/ares.c b/ares.c
index 567039793af691b7fe44b2f3c6062c98e8856edc..7ace77565c39e3868d077b4611e16992785767b2 100644 (file)
--- a/ares.c
+++ b/ares.c
@@ -1,19 +1,13 @@
 /*
-  +----------------------------------------------------------------------+
-  | PHP Version 5                                                        |
-  +----------------------------------------------------------------------+
-  | Copyright (c) 1997-2006 The PHP Group                                |
-  +----------------------------------------------------------------------+
-  | This source file is subject to version 3.01 of the PHP license,      |
-  | that is bundled with this package in the file LICENSE, and is        |
-  | available through the world-wide-web at the following url:           |
-  | http://www.php.net/license/3_01.txt                                  |
-  | If you did not receive a copy of the PHP license and are unable to   |
-  | obtain it through the world-wide-web, please send a note to          |
-  | license@php.net so we can mail you a copy immediately.               |
-  +----------------------------------------------------------------------+
-  | Author:                                                              |
-  +----------------------------------------------------------------------+
+    +--------------------------------------------------------------------+
+    | PECL :: ares                                                       |
+    +--------------------------------------------------------------------+
+    | Redistribution and use in source and binary forms, with or without |
+    | modification, are permitted provided that the conditions mentioned |
+    | in the accompanying LICENSE file are met.                          |
+    +--------------------------------------------------------------------+
+    | Copyright (c) 2006, Michael Wallner <mike@php.net>                 |
+    +--------------------------------------------------------------------+
 */
 
 /* $Id$ */
 #define local inline
 
 #ifndef ZEND_ENGINE_2
-#      define zend_is_callable(a,b,c) 1
+#      define IS_CALLABLE(a,b,c) 1
+#      ifndef ZTS
+#              undef TSRMLS_SET_CTX
+#              define TSRMLS_SET_CTX
+#              undef TSRMLS_FETCH_FROM_CTX
+#              define TSRMLS_FETCH_FROM_CTX
+#      endif
+#endif
+#if (PHP_MAJOR_VERSION > 5) || ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION >= 3))
+#      define ADDREF(z) Z_ADDREF_P(z)
+#      define IS_CALLABLE(a,b,c) zend_is_callable((a), (b), (c) TSRMLS_CC)
+#else
+#      define ADDREF(z) ZVAL_ADDREF(z)
+#      define IS_CALLABLE(a,b,c) zend_is_callable((a), (b), (c))
 #endif
 
 #define PHP_ARES_LE_NAME "AsyncResolver"
 static int le_ares;
 static int le_ares_query;
 
-#define PHP_ARES_ERROR(err) \
+#ifdef HAVE_OLD_ARES_STRERROR
+#      define PHP_ARES_ERROR(err) { \
+       char *__tmp = NULL; \
+       php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ares_strerror(err, &__tmp)); \
+       if (__tmp) ares_free_errmem(__tmp); \
+}
+#else
+#      define PHP_ARES_ERROR(err) \
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ares_strerror(err))
+#endif
+
 #define RETURN_ARES_ERROR(err) \
        PHP_ARES_ERROR(err); \
        RETURN_FALSE
@@ -76,7 +92,7 @@ typedef struct _php_ares {
        ares_channel channel;
        php_ares_options options;
        zend_llist queries;
-       TSRMLS_D;
+       void ***tsrm_ls;
        unsigned in_callback:1;
        unsigned reserved:31;
 } php_ares;
@@ -100,8 +116,8 @@ typedef union _php_ares_query_packet_data {
        struct {
                char *name;
                int name_len;
-               int type;
-               int dnsclass;
+               long type;
+               long dnsclass;
        } search;
        struct {
                char *name;
@@ -271,7 +287,7 @@ local php_ares_query *php_ares_query_ctor(php_ares_query *query, php_ares_query_
        query->error = -1;
        
        if (callback) {
-               ZVAL_ADDREF(callback);
+               ADDREF(callback);
                query->callback = callback;
        }
        
@@ -418,11 +434,11 @@ local php_ares_options *php_ares_options_ctor(php_ares_options *options, HashTab
                }
                if ((SUCCESS == zend_hash_find(ht, "udp_port", sizeof("udp_port"), (void *) &opt)) && (Z_TYPE_PP(opt) == IS_LONG)) {
                        options->flags |= ARES_OPT_UDP_PORT;
-                       options->strct.udp_port = Z_LVAL_PP(opt);
+                       options->strct.udp_port = htons((unsigned short) Z_LVAL_PP(opt));
                }
                if ((SUCCESS == zend_hash_find(ht, "tcp_port", sizeof("tcp_port"), (void *) &opt)) && (Z_TYPE_PP(opt) == IS_LONG)) {
                        options->flags |= ARES_OPT_TCP_PORT;
-                       options->strct.tcp_port = Z_LVAL_PP(opt);
+                       options->strct.tcp_port = htons((unsigned short) Z_LVAL_PP(opt));
                }
                if ((SUCCESS == zend_hash_find(ht, "servers", sizeof("servers"), (void *) &opt)) && (Z_TYPE_PP(opt) == IS_ARRAY) && (i = zend_hash_num_elements(Z_ARRVAL_PP(opt)))) {
                        options->strct.servers = ecalloc(i, sizeof(struct in_addr));
@@ -447,7 +463,7 @@ local php_ares_options *php_ares_options_ctor(php_ares_options *options, HashTab
                                }
                        }
                        if (options->strct.ndomains) {
-                               options->flags |= ARES_OPT_SERVERS;
+                               options->flags |= ARES_OPT_DOMAINS;
                        }
                }
                if ((SUCCESS == zend_hash_find(ht, "lookups", sizeof("lookups"), (void *) &opt)) && (Z_TYPE_PP(opt) == IS_STRING)) {
@@ -490,7 +506,7 @@ local void php_ares_options_free(php_ares_options **options) /* {{{ */
 /* }}} */
 
 /* {{{ callbacks */
-static void php_ares_callback_func(void *aq, int status, unsigned char *abuf, int alen)
+static void php_ares_callback_func_old(void *aq, int status, unsigned char *abuf, int alen)
 {
        php_ares_query *q = (php_ares_query *) aq;
        zval *params[3], *retval;
@@ -525,7 +541,7 @@ static void php_ares_callback_func(void *aq, int status, unsigned char *abuf, in
        }
 }
 
-static void php_ares_host_callback_func(void *aq, int status, struct hostent *hostent)
+static void php_ares_host_callback_func_old(void *aq, int status, struct hostent *hostent)
 {
        php_ares_query *q = (php_ares_query *) aq;
        zval *params[3], *retval;
@@ -561,7 +577,7 @@ static void php_ares_host_callback_func(void *aq, int status, struct hostent *ho
 }
 
 #ifdef HAVE_ARES_GETNAMEINFO
-static void php_ares_nameinfo_callback_func(void *aq, int status, char *node, char *service)
+static void php_ares_nameinfo_callback_func_old(void *aq, int status, char *node, char *service)
 {
        php_ares_query *q = (php_ares_query *) aq;
        zval *params[4], *retval;
@@ -609,6 +625,35 @@ static void php_ares_nameinfo_callback_func(void *aq, int status, char *node, ch
        }
 }
 #endif
+
+#if PHP_ARES_NEW_CALLBACK_API
+#      define php_ares_callback_func php_ares_callback_func_new
+static void php_ares_callback_func_new(void *aq, int status, int timeouts, unsigned char *abuf, int alen)
+{
+       php_ares_callback_func_old(aq, status, abuf, alen);
+}
+
+#      define php_ares_host_callback_func php_ares_host_callback_func_new
+static void php_ares_host_callback_func_new(void *aq, int status, int timeouts, struct hostent *hostent)
+{
+       php_ares_host_callback_func_old(aq, status, hostent);
+}
+
+#      ifdef HAVE_ARES_GETNAMEINFO
+#              define php_ares_nameinfo_callback_func php_ares_nameinfo_callback_func_new
+static void php_ares_nameinfo_callback_func_new(void *aq, int status, int timeouts, char *node, char *service)
+{
+       php_ares_nameinfo_callback_func_old(aq, status, node, service);
+}
+#      endif
+
+#else
+#      define php_ares_callback_func php_ares_callback_func_old
+#      define php_ares_host_callback_func php_ares_host_callback_func_old
+#      ifdef HAVE_ARES_GETNAMEINFO
+#              define php_ares_nameinfo_callback_func php_ares_nameinfo_callback_func_old
+#      endif
+#endif
 /* }}} */
 
 local struct timeval *php_ares_timeout(php_ares *ares, long max_timeout, struct timeval *tv_buf) /* {{{ */
@@ -776,12 +821,24 @@ static PHP_FUNCTION(ares_destroy)
 static PHP_FUNCTION(ares_strerror)
 {
        long err;
+#ifdef HAVE_OLD_ARES_STRERROR
+       char *__tmp = NULL;
+       const char *__err;
+#endif
        
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &err)) {
                RETURN_FALSE;
        }
        
+#ifdef HAVE_OLD_ARES_STRERROR
+       __err = ares_strerror(err, &__tmp);
+       RETVAL_STRING(estrdup(__err), 0);
+       if (__tmp) {
+               ares_free_errmem(__tmp);
+       }
+#else
        RETURN_STRING(estrdup(ares_strerror(err)), 0);
+#endif
 }
 /* }}} */
 
@@ -797,7 +854,7 @@ static PHP_FUNCTION(ares_mkquery)
                RETURN_FALSE;
        }
        
-       if (ARES_SUCCESS != (err = ares_mkquery(name_str, dnsclass, type, id, rd, &query_str, &query_len))) {
+       if (ARES_SUCCESS != (err = ares_mkquery(name_str, dnsclass, type, id, rd, (unsigned char **) &query_str, &query_len))) {
                RETURN_ARES_ERROR(err);
        }
        RETVAL_STRINGL(query_str, query_len, 1);
@@ -821,7 +878,7 @@ static PHP_FUNCTION(ares_search)
        }
        ZEND_FETCH_RESOURCE(ares, php_ares *, &rsrc, -1, PHP_ARES_LE_NAME, le_ares);
        
-       if (cb && !zend_is_callable(cb, 0, NULL)) {
+       if (cb && !IS_CALLABLE(cb, 0, NULL)) {
                RETURN_ARES_CB_ERROR("second");
        }
        
@@ -848,7 +905,7 @@ static PHP_FUNCTION(ares_query)
        }
        ZEND_FETCH_RESOURCE(ares, php_ares *, &rsrc, -1, PHP_ARES_LE_NAME, le_ares);
        
-       if (cb && !zend_is_callable(cb, 0, NULL)) {
+       if (cb && !IS_CALLABLE(cb, 0, NULL)) {
                RETURN_ARES_CB_ERROR("second");
        }
                
@@ -874,7 +931,7 @@ static PHP_FUNCTION(ares_send)
        }
        ZEND_FETCH_RESOURCE(ares, php_ares *, &rsrc, -1, PHP_ARES_LE_NAME, le_ares);
        
-       if (cb && !zend_is_callable(cb, 0, NULL)) {
+       if (cb && !IS_CALLABLE(cb, 0, NULL)) {
                RETURN_ARES_CB_ERROR("second");
        }
        
@@ -901,7 +958,7 @@ static PHP_FUNCTION(ares_gethostbyname)
        }
        ZEND_FETCH_RESOURCE(ares, php_ares *, &rsrc, -1, PHP_ARES_LE_NAME, le_ares);
        
-       if (cb && !zend_is_callable(cb, 0, NULL)) {
+       if (cb && !IS_CALLABLE(cb, 0, NULL)) {
                RETURN_ARES_CB_ERROR("second");
        }
        
@@ -912,7 +969,7 @@ static PHP_FUNCTION(ares_gethostbyname)
 }
 /* }}} */
 
-/* {{{ proto resource ares_gethostbyaddr(resuorce ares, mixed callback, string address[, int family = AF_INET])
+/* {{{ proto resource ares_gethostbyaddr(resuorce ares, mixed callback, string address[, int family = ARES_AF_INET])
        Get host by address */
 static PHP_FUNCTION(ares_gethostbyaddr)
 {
@@ -930,7 +987,7 @@ static PHP_FUNCTION(ares_gethostbyaddr)
        }
        ZEND_FETCH_RESOURCE(ares, php_ares *, &rsrc, -1, PHP_ARES_LE_NAME, le_ares);
        
-       if (cb && !zend_is_callable(cb, 0, NULL)) {
+       if (cb && !IS_CALLABLE(cb, 0, NULL)) {
                PHP_ARES_CB_ERROR("second");
                RETURN_FALSE;
        }
@@ -943,7 +1000,7 @@ static PHP_FUNCTION(ares_gethostbyaddr)
                        sa = emalloc(sa_len = sizeof(struct in6_addr));
                        break;
                default:
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter family is neither AF_INET nor AF_INET6");
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter family is neither ARES_AF_INET nor ARES_AF_INET6");
                        RETURN_FALSE;
                        break;
        }
@@ -962,7 +1019,7 @@ static PHP_FUNCTION(ares_gethostbyaddr)
 /* }}} */
 
 #ifdef HAVE_ARES_GETNAMEINFO
-/* {{{ proto resource ares_getnameinfo(resource ares, mixed callback, int flags, string addr[, int family[, int port]])
+/* {{{ proto resource ares_getnameinfo(resource ares, mixed callback, int flags, string addr[, int family = ARES_AF_INET[, int port = 0]])
        Get name info */
 static PHP_FUNCTION(ares_getnameinfo)
 {
@@ -982,7 +1039,7 @@ static PHP_FUNCTION(ares_getnameinfo)
        }
        ZEND_FETCH_RESOURCE(ares, php_ares *, &rsrc, -1, PHP_ARES_LE_NAME, le_ares);
        
-       if (cb && !zend_is_callable(cb, 0, NULL)) {
+       if (cb && !IS_CALLABLE(cb, 0, NULL)) {
                PHP_ARES_CB_ERROR("second");
                RETURN_FALSE;
        }
@@ -990,9 +1047,9 @@ static PHP_FUNCTION(ares_getnameinfo)
        RETVAL_TRUE;
        switch (family) {
                case AF_INET:
-                       in = emalloc(sa_len = sizeof(struct sockaddr_in));
+                       in = ecalloc(1, sa_len = sizeof(struct sockaddr_in));
                        in->sin_family = AF_INET;
-                       in->sin_port = port;
+                       in->sin_port = htons((unsigned short) port);
                        if (1 > inet_pton(in->sin_family, addr, &in->sin_addr)) {
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "inet_pton('%s') failed", addr);
                                RETVAL_FALSE;
@@ -1000,9 +1057,9 @@ static PHP_FUNCTION(ares_getnameinfo)
                        sa = (struct sockaddr *) in;
                        break;
                case AF_INET6:
-                       in6 = emalloc(sa_len = sizeof(struct sockaddr_in6));
+                       in6 = ecalloc(1, sa_len = sizeof(struct sockaddr_in6));
                        in6->sin6_family = AF_INET6;
-                       in6->sin6_port = port;
+                       in6->sin6_port = htons((unsigned short) port);
                        if (1 > inet_pton(in6->sin6_family, addr, &in6->sin6_addr)) {
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "inet_pton('%s') failed", addr);
                                RETVAL_FALSE;
@@ -1069,7 +1126,14 @@ static PHP_FUNCTION(ares_result)
                        break;
                default:
                        if (zerror) {
+#ifdef HAVE_OLD_ARES_STRERROR
+                               char *__tmp = NULL;
+                               const char *__err = ares_strerror(query->error, &__tmp);
+                               ZVAL_STRING(zerror, estrdup(__err), 0);
+                               if (__tmp) ares_free_errmem(__tmp);
+#else
                                ZVAL_STRING(zerror, estrdup(ares_strerror(query->error)), 0);
+#endif
                        }
                        RETVAL_FALSE;
                        break;
@@ -1302,117 +1366,6 @@ static PHP_FUNCTION(ares_fds)
 }
 /* }}} */
 
-
-/* {{{ proto array ares_parse_a_reply(string reply)
-       Parse an A reply */
-static PHP_FUNCTION(ares_parse_a_reply)
-{
-       char *buf;
-       int len, err;
-       struct hostent *hostent;
-       
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &len)) {
-               RETURN_FALSE;
-       }
-       
-       if (ARES_SUCCESS != (err = ares_parse_a_reply((const unsigned char *) buf, len, &hostent))) {
-               RETURN_ARES_ERROR(err);
-       }
-       
-       object_init(return_value);
-       php_ares_hostent_to_struct(hostent, HASH_OF(return_value));
-       ares_free_hostent(hostent);
-}
-/* }}} */
-
-#ifdef HAVE_ARES_PARSE_AAAA_REPLY
-/* {{{ proto array ares_parse_aaaa_reply(string reply)
-       Parse an AAAA reply */
-static PHP_FUNCTION(ares_parse_aaaa_reply)
-{
-       char *buf;
-       int len, err;
-       struct hostent *hostent;
-       
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &len)) {
-               RETURN_FALSE;
-       }
-       
-       if (ARES_SUCCESS != (err = ares_parse_aaaa_reply((const unsigned char *) buf, len, &hostent))) {
-               RETURN_ARES_ERROR(err);
-       }
-       
-       object_init(return_value);
-       php_ares_hostent_to_struct(hostent, HASH_OF(return_value));
-       ares_free_hostent(hostent);
-}
-/* }}} */
-#endif
-
-/* {{{ proto array ares_parse_ptr_reply(string reply)
-       Parse a PTR reply */
-static PHP_FUNCTION(ares_parse_ptr_reply)
-{
-       char *buf;
-       int len, err;
-       struct hostent *hostent;
-       
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &len)) {
-               RETURN_FALSE;
-       }
-       
-       if (ARES_SUCCESS != (err = ares_parse_ptr_reply((const unsigned char *) buf, len, NULL, 0, 0, &hostent))) {
-               RETURN_ARES_ERROR(err);
-       }
-       
-       object_init(return_value);
-       php_ares_hostent_to_struct(hostent, HASH_OF(return_value));
-       ares_free_hostent(hostent);
-}
-/* }}} */
-
-/* {{{ proto string ares_expand_name(string name)
-       Expand a DNS encoded name into a human readable dotted string */
-static PHP_FUNCTION(ares_expand_name)
-{
-       char *name_str, *exp_str;
-       int name_len,err;
-       PHP_ARES_EXPAND_LEN_TYPE exp_len;
-       
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name_str, &name_len)) {
-               RETURN_FALSE;
-       }
-       
-       if (ARES_SUCCESS != (err = ares_expand_name((const unsigned char *) name_str, (const unsigned char *) name_str, name_len, &exp_str, &exp_len))) {
-               RETURN_ARES_ERROR(err);
-       }
-       RETVAL_STRINGL(exp_str, exp_len, 1);
-       ares_free_string(exp_str);
-}
-/* }}} */
-
-#ifdef HAVE_ARES_EXPAND_STRING
-/* {{{ proto string ares_expand_string(string buf)
-       Expand a DNS encoded string into a human readable */
-static PHP_FUNCTION(ares_expand_string)
-{
-       char *buf_str, *exp_str;
-       int buf_len, err;
-       PHP_ARES_EXPAND_LEN_TYPE exp_len;
-       
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf_str, &buf_len)) {
-               RETURN_FALSE;
-       }
-       
-       if (ARES_SUCCESS != (err = ares_expand_string((const unsigned char *) buf_str, (const unsigned char *) buf_str, buf_len, &exp_str, &exp_len))) {
-               RETURN_ARES_ERROR(err);
-       }
-       RETVAL_STRINGL(exp_str, exp_len, 1);
-       ares_free_string(exp_str);
-}
-/* }}} */
-#endif
-
 static ZEND_RSRC_DTOR_FUNC(php_ares_le_dtor)
 {
        php_ares *ares = (php_ares *) rsrc->ptr;
@@ -1480,15 +1433,11 @@ static PHP_MINIT_FUNCTION(ares)
        REGISTER_LONG_CONSTANT("ARES_FLAG_NOALIASES", ARES_FLAG_NOALIASES, CONST_PERSISTENT|CONST_CS);
        REGISTER_LONG_CONSTANT("ARES_FLAG_NOCHECKRESP", ARES_FLAG_NOCHECKRESP, CONST_PERSISTENT|CONST_CS);
        
-       REGISTER_LONG_CONSTANT("ARES_OPT_FLAGS", ARES_OPT_FLAGS, CONST_PERSISTENT|CONST_CS);
-       REGISTER_LONG_CONSTANT("ARES_OPT_TIMEOUT", ARES_OPT_TIMEOUT, CONST_PERSISTENT|CONST_CS);
-       REGISTER_LONG_CONSTANT("ARES_OPT_TRIES", ARES_OPT_TRIES, CONST_PERSISTENT|CONST_CS);
-       REGISTER_LONG_CONSTANT("ARES_OPT_NDOTS", ARES_OPT_NDOTS, CONST_PERSISTENT|CONST_CS);
-       REGISTER_LONG_CONSTANT("ARES_OPT_UDP_PORT", ARES_OPT_UDP_PORT, CONST_PERSISTENT|CONST_CS);
-       REGISTER_LONG_CONSTANT("ARES_OPT_TCP_PORT", ARES_OPT_TCP_PORT, CONST_PERSISTENT|CONST_CS);
-       REGISTER_LONG_CONSTANT("ARES_OPT_SERVERS", ARES_OPT_SERVERS, CONST_PERSISTENT|CONST_CS);
-       REGISTER_LONG_CONSTANT("ARES_OPT_DOMAINS", ARES_OPT_DOMAINS, CONST_PERSISTENT|CONST_CS);
-       REGISTER_LONG_CONSTANT("ARES_OPT_LOOKUPS", ARES_OPT_LOOKUPS, CONST_PERSISTENT|CONST_CS);
+       /*
+        * Address Family Constants
+        */
+       REGISTER_LONG_CONSTANT("ARES_AF_INET", AF_INET, CONST_PERSISTENT|CONST_CS);
+       REGISTER_LONG_CONSTANT("ARES_AF_INET6", AF_INET6, CONST_PERSISTENT|CONST_CS);
        
        /*
         * Name Info constants
@@ -1710,6 +1659,7 @@ static PHP_MINFO_FUNCTION(ares)
 {
        php_info_print_table_start();
        php_info_print_table_header(2, "AsyncResolver support", "enabled");
+       php_info_print_table_row(2, "Version", PHP_ARES_VERSION);
        php_info_print_table_end();
        
        php_info_print_table_start();
@@ -1783,15 +1733,6 @@ zend_function_entry ares_functions[] = {
        PHP_FE(ares_select, ai_ares_select)
        PHP_FE(ares_fds, ai_ares_fds)
        PHP_FE(ares_timeout, NULL)
-       PHP_FE(ares_parse_a_reply, NULL)
-#ifdef HAVE_ARES_PARSE_AAAA_REPLY
-       PHP_FE(ares_parse_aaaa_reply, NULL)
-#endif
-       PHP_FE(ares_parse_ptr_reply, NULL)
-       PHP_FE(ares_expand_name, NULL)
-#ifdef HAVE_ARES_EXPAND_STRING
-       PHP_FE(ares_expand_string, NULL)
-#endif
        {NULL, NULL, NULL}
 };
 /* }}} */
@@ -1806,7 +1747,7 @@ zend_module_entry ares_module_entry = {
        NULL,
        NULL,
        PHP_MINFO(ares),
-       NO_VERSION_YET,
+       PHP_ARES_VERSION,
        STANDARD_MODULE_PROPERTIES
 };
 /* }}} */