- fix byte order of ports
[m6w6/ext-ares] / ares.c
diff --git a/ares.c b/ares.c
index 567039793af691b7fe44b2f3c6062c98e8856edc..89f4c61444d81779eeac074ad84d4c2fc36b9c5b 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$ */
 
 #ifndef ZEND_ENGINE_2
 #      define zend_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
 
 #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 +85,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 +109,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;
@@ -418,11 +427,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 +456,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)) {
@@ -776,12 +785,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 +818,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);
@@ -912,7 +933,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)
 {
@@ -943,7 +964,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 +983,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)
 {
@@ -990,9 +1011,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 +1021,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 +1090,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;
@@ -1404,7 +1432,7 @@ static PHP_FUNCTION(ares_expand_string)
                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))) {
+       if (ARES_SUCCESS != (err = ares_expand_string((const unsigned char *) buf_str, (const unsigned char *) buf_str, buf_len, (unsigned char **) &exp_str, &exp_len))) {
                RETURN_ARES_ERROR(err);
        }
        RETVAL_STRINGL(exp_str, exp_len, 1);
@@ -1480,15 +1508,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