Back to index

php5  5.3.10
dns.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: The typical suspects                                        |
00016    |          Pollita <pollita@php.net>                                   |
00017    |          Marcus Boerger <helly@php.net>                              |
00018    +----------------------------------------------------------------------+
00019  */
00020 
00021 /* $Id: dns.c 321634 2012-01-01 13:15:04Z felipe $ */
00022 
00023 /* {{{ includes */
00024 #include "php.h"
00025 #include "php_network.h"
00026 
00027 #if HAVE_SYS_SOCKET_H
00028 #include <sys/socket.h>
00029 #endif
00030 
00031 #ifdef PHP_WIN32
00032 # include "win32/inet.h"
00033 # include <winsock2.h>
00034 # include <windows.h>
00035 # include <Ws2tcpip.h>
00036 #else  /* This holds good for NetWare too, both for Winsock and Berkeley sockets */
00037 #include <netinet/in.h>
00038 #if HAVE_ARPA_INET_H
00039 #include <arpa/inet.h>
00040 #endif
00041 #include <netdb.h>
00042 #ifdef _OSD_POSIX
00043 #undef STATUS
00044 #undef T_UNSPEC
00045 #endif
00046 #if HAVE_ARPA_NAMESER_H
00047 #ifdef DARWIN
00048 # define BIND_8_COMPAT 1
00049 #endif
00050 #include <arpa/nameser.h>
00051 #endif
00052 #if HAVE_RESOLV_H
00053 #include <resolv.h>
00054 #endif
00055 #ifdef HAVE_DNS_H
00056 #include <dns.h>
00057 #endif
00058 #endif
00059 
00060 /* Borrowed from SYS/SOCKET.H */
00061 #if defined(NETWARE) && defined(USE_WINSOCK)
00062 #define AF_INET 2   /* internetwork: UDP, TCP, etc. */
00063 #endif
00064 
00065 #ifndef MAXHOSTNAMELEN
00066 #define MAXHOSTNAMELEN 255
00067 #endif
00068 
00069 /* For the local hostname obtained via gethostname which is different from the
00070    dns-related MAXHOSTNAMELEN constant above */
00071 #ifndef HOST_NAME_MAX
00072 #define HOST_NAME_MAX 255
00073 #endif
00074 
00075 #include "php_dns.h"
00076 
00077 /* type compat */
00078 #ifndef DNS_T_A
00079 #define DNS_T_A             1
00080 #endif
00081 #ifndef DNS_T_NS
00082 #define DNS_T_NS     2
00083 #endif
00084 #ifndef DNS_T_CNAME
00085 #define DNS_T_CNAME  5
00086 #endif
00087 #ifndef DNS_T_SOA
00088 #define DNS_T_SOA    6
00089 #endif
00090 #ifndef DNS_T_PTR
00091 #define DNS_T_PTR    12
00092 #endif
00093 #ifndef DNS_T_HINFO
00094 #define DNS_T_HINFO  13
00095 #endif
00096 #ifndef DNS_T_MINFO
00097 #define DNS_T_MINFO  14
00098 #endif
00099 #ifndef DNS_T_MX
00100 #define DNS_T_MX     15
00101 #endif
00102 #ifndef DNS_T_TXT
00103 #define DNS_T_TXT    16
00104 #endif
00105 #ifndef DNS_T_AAAA
00106 #define DNS_T_AAAA   28
00107 #endif
00108 #ifndef DNS_T_SRV
00109 #define DNS_T_SRV    33
00110 #endif
00111 #ifndef DNS_T_NAPTR
00112 #define DNS_T_NAPTR  35
00113 #endif
00114 #ifndef DNS_T_A6
00115 #define DNS_T_A6     38
00116 #endif
00117 
00118 #ifndef DNS_T_ANY
00119 #define DNS_T_ANY    255
00120 #endif
00121 /* }}} */
00122 
00123 static char *php_gethostbyaddr(char *ip);
00124 static char *php_gethostbyname(char *name);
00125 
00126 #ifdef HAVE_GETHOSTNAME
00127 /* {{{ proto string gethostname()
00128    Get the host name of the current machine */
00129 PHP_FUNCTION(gethostname)
00130 {
00131        char buf[HOST_NAME_MAX];
00132 
00133        if (zend_parse_parameters_none() == FAILURE) {
00134               return;
00135        }
00136 
00137        if (gethostname(buf, sizeof(buf) - 1)) {
00138               php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to fetch host [%d]: %s", errno, strerror(errno));
00139               RETURN_FALSE;
00140        }
00141 
00142        RETURN_STRING(buf, 1);
00143 }
00144 /* }}} */
00145 #endif
00146 
00147 /* TODO: Reimplement the gethostby* functions using the new winxp+ API, in dns_win32.c, then
00148  we can have a dns.c, dns_unix.c and dns_win32.c instead of a messy dns.c full of #ifdef
00149 */
00150 
00151 /* {{{ proto string gethostbyaddr(string ip_address)
00152    Get the Internet host name corresponding to a given IP address */
00153 PHP_FUNCTION(gethostbyaddr)
00154 {
00155        char *addr;
00156        int addr_len;
00157        char *hostname;
00158 
00159        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &addr, &addr_len) == FAILURE) {
00160               return;
00161        }
00162 
00163        hostname = php_gethostbyaddr(addr);
00164 
00165        if (hostname == NULL) {
00166 #if HAVE_IPV6 && HAVE_INET_PTON
00167               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Address is not a valid IPv4 or IPv6 address");
00168 #else
00169               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Address is not in a.b.c.d form");
00170 #endif
00171               RETVAL_FALSE;
00172        } else {
00173               RETVAL_STRING(hostname, 0);
00174        }
00175 }
00176 /* }}} */
00177 
00178 /* {{{ php_gethostbyaddr */
00179 static char *php_gethostbyaddr(char *ip)
00180 {
00181 #if HAVE_IPV6 && HAVE_INET_PTON
00182        struct in6_addr addr6;
00183 #endif
00184        struct in_addr addr;
00185        struct hostent *hp;
00186 
00187 #if HAVE_IPV6 && HAVE_INET_PTON
00188        if (inet_pton(AF_INET6, ip, &addr6)) {
00189               hp = gethostbyaddr((char *) &addr6, sizeof(addr6), AF_INET6);
00190        } else if (inet_pton(AF_INET, ip, &addr)) {
00191               hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
00192        } else {
00193               return NULL;
00194        }
00195 #else
00196        addr.s_addr = inet_addr(ip);
00197 
00198        if (addr.s_addr == -1) {
00199               return NULL;
00200        }
00201 
00202        hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
00203 #endif
00204 
00205        if (!hp || hp->h_name == NULL || hp->h_name[0] == '\0') {
00206               return estrdup(ip);
00207        }
00208 
00209        return estrdup(hp->h_name);
00210 }
00211 /* }}} */
00212 
00213 /* {{{ proto string gethostbyname(string hostname)
00214    Get the IP address corresponding to a given Internet host name */
00215 PHP_FUNCTION(gethostbyname)
00216 {
00217        char *hostname;
00218        int hostname_len;
00219        char *addr;
00220 
00221        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &hostname, &hostname_len) == FAILURE) {
00222               return;
00223        }
00224 
00225        addr = php_gethostbyname(hostname);
00226 
00227        RETVAL_STRING(addr, 0);
00228 }
00229 /* }}} */
00230 
00231 /* {{{ proto array gethostbynamel(string hostname)
00232    Return a list of IP addresses that a given hostname resolves to. */
00233 PHP_FUNCTION(gethostbynamel)
00234 {
00235        char *hostname;
00236        int hostname_len;
00237        struct hostent *hp;
00238        struct in_addr in;
00239        int i;
00240 
00241        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &hostname, &hostname_len) == FAILURE) {
00242               return;
00243        }
00244 
00245        hp = gethostbyname(hostname);
00246        if (hp == NULL || hp->h_addr_list == NULL) {
00247               RETURN_FALSE;
00248        }
00249 
00250        array_init(return_value);
00251 
00252        for (i = 0 ; hp->h_addr_list[i] != 0 ; i++) {
00253               in = *(struct in_addr *) hp->h_addr_list[i];
00254               add_next_index_string(return_value, inet_ntoa(in), 1);
00255        }
00256 }
00257 /* }}} */
00258 
00259 /* {{{ php_gethostbyname */
00260 static char *php_gethostbyname(char *name)
00261 {
00262        struct hostent *hp;
00263        struct in_addr in;
00264 
00265        hp = gethostbyname(name);
00266 
00267        if (!hp || !*(hp->h_addr_list)) {
00268               return estrdup(name);
00269        }
00270 
00271        memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr));
00272 
00273        return estrdup(inet_ntoa(in));
00274 }
00275 /* }}} */
00276 
00277 #if HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32)
00278 # define PHP_DNS_NUM_TYPES  12     /* Number of DNS Types Supported by PHP currently */
00279 
00280 # define PHP_DNS_A      0x00000001
00281 # define PHP_DNS_NS     0x00000002
00282 # define PHP_DNS_CNAME  0x00000010
00283 # define PHP_DNS_SOA    0x00000020
00284 # define PHP_DNS_PTR    0x00000800
00285 # define PHP_DNS_HINFO  0x00001000
00286 # define PHP_DNS_MX     0x00004000
00287 # define PHP_DNS_TXT    0x00008000
00288 # define PHP_DNS_A6     0x01000000
00289 # define PHP_DNS_SRV    0x02000000
00290 # define PHP_DNS_NAPTR  0x04000000
00291 # define PHP_DNS_AAAA   0x08000000
00292 # define PHP_DNS_ANY    0x10000000
00293 # 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_DNS_A6|PHP_DNS_SRV|PHP_DNS_NAPTR|PHP_DNS_AAAA)
00294 #endif /* HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32) */
00295 
00296 /* Note: These functions are defined in ext/standard/dns_win32.c for Windows! */
00297 #if !defined(PHP_WIN32) && (HAVE_DNS_SEARCH_FUNC && !(defined(__BEOS__) || defined(NETWARE)))
00298   
00299 #ifndef HFIXEDSZ
00300 #define HFIXEDSZ        12      /* fixed data in header <arpa/nameser.h> */
00301 #endif /* HFIXEDSZ */
00302 
00303 #ifndef QFIXEDSZ
00304 #define QFIXEDSZ        4       /* fixed data in query <arpa/nameser.h> */
00305 #endif /* QFIXEDSZ */
00306 
00307 #undef MAXHOSTNAMELEN
00308 #define MAXHOSTNAMELEN  1024
00309 
00310 #ifndef MAXRESOURCERECORDS
00311 #define MAXRESOURCERECORDS  64
00312 #endif /* MAXRESOURCERECORDS */
00313 
00314 typedef union {
00315        HEADER qb1;
00316        u_char qb2[65536];
00317 } querybuf;
00318 
00319 /* just a hack to free resources allocated by glibc in __res_nsend()
00320  * See also:
00321  *   res_thread_freeres() in glibc/resolv/res_init.c
00322  *   __libc_res_nsend()   in resolv/res_send.c
00323  * */
00324 
00325 #if defined(__GLIBC__) && !defined(HAVE_DEPRECATED_DNS_FUNCS)
00326 #define php_dns_free_res(__res__) _php_dns_free_res(__res__)
00327 static void _php_dns_free_res(struct __res_state res) { /* {{{ */
00328        int ns;
00329        for (ns = 0; ns < MAXNS; ns++) {
00330               if (res._u._ext.nsaddrs[ns] != NULL) {
00331                      free (res._u._ext.nsaddrs[ns]);
00332                      res._u._ext.nsaddrs[ns] = NULL;
00333               }
00334        }
00335 } /* }}} */
00336 #else
00337 #define php_dns_free_res(__res__)
00338 #endif
00339 
00340 /* {{{ proto bool dns_check_record(string host [, string type])
00341    Check DNS records corresponding to a given Internet host name or IP address */
00342 PHP_FUNCTION(dns_check_record)
00343 {
00344 #ifndef MAXPACKET
00345 #define MAXPACKET  8192 /* max packet size used internally by BIND */
00346 #endif
00347        u_char ans[MAXPACKET];
00348        char *hostname, *rectype = NULL;
00349        int hostname_len, rectype_len = 0;
00350        int type = T_MX, i;
00351 #if defined(HAVE_DNS_SEARCH)
00352        struct sockaddr_storage from;
00353        uint32_t fromsize = sizeof(from);
00354        dns_handle_t handle;
00355 #elif defined(HAVE_RES_NSEARCH)
00356        struct __res_state state;
00357        struct __res_state *handle = &state;
00358 #endif
00359 
00360        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &hostname, &hostname_len, &rectype, &rectype_len) == FAILURE) {
00361               return;
00362        }
00363 
00364        if (hostname_len == 0) {
00365               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host cannot be empty");
00366               RETURN_FALSE;
00367        }
00368 
00369        if (rectype) {
00370               if (!strcasecmp("A",     rectype)) type = T_A;
00371               else if (!strcasecmp("NS",    rectype)) type = DNS_T_NS;
00372               else if (!strcasecmp("MX",    rectype)) type = DNS_T_MX;
00373               else if (!strcasecmp("PTR",   rectype)) type = DNS_T_PTR;
00374               else if (!strcasecmp("ANY",   rectype)) type = DNS_T_ANY;
00375               else if (!strcasecmp("SOA",   rectype)) type = DNS_T_SOA;
00376               else if (!strcasecmp("TXT",   rectype)) type = DNS_T_TXT;
00377               else if (!strcasecmp("CNAME", rectype)) type = DNS_T_CNAME;
00378               else if (!strcasecmp("AAAA",  rectype)) type = DNS_T_AAAA;
00379               else if (!strcasecmp("SRV",   rectype)) type = DNS_T_SRV;
00380               else if (!strcasecmp("NAPTR", rectype)) type = DNS_T_NAPTR;
00381               else if (!strcasecmp("A6",    rectype)) type = DNS_T_A6;
00382               else {
00383                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%s' not supported", rectype);
00384                      RETURN_FALSE;
00385               }
00386        }
00387 
00388 #if defined(HAVE_DNS_SEARCH)
00389        handle = dns_open(NULL);
00390        if (handle == NULL) {
00391               RETURN_FALSE;
00392        }
00393 #elif defined(HAVE_RES_NSEARCH)
00394     memset(&state, 0, sizeof(state));
00395     if (res_ninit(handle)) {
00396                      RETURN_FALSE;
00397        }
00398 #else
00399        res_init();
00400 #endif
00401 
00402        RETVAL_TRUE;
00403        i = php_dns_search(handle, hostname, C_IN, type, ans, sizeof(ans));
00404 
00405        if (i < 0) {
00406               RETVAL_FALSE;
00407        }
00408 
00409        php_dns_free_handle(handle);
00410 }
00411 /* }}} */
00412 
00413 #if HAVE_FULL_DNS_FUNCS
00414 
00415 /* {{{ php_parserr */
00416 static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int store, zval **subarray)
00417 {
00418        u_short type, class, dlen;
00419        u_long ttl;
00420        long n, i;
00421        u_short s;
00422        u_char *tp, *p;
00423        char name[MAXHOSTNAMELEN];
00424        int have_v6_break = 0, in_v6_break = 0;
00425 
00426        *subarray = NULL;
00427 
00428        n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, sizeof(name) - 2);
00429        if (n < 0) {
00430               return NULL;
00431        }
00432        cp += n;
00433 
00434        GETSHORT(type, cp);
00435        GETSHORT(class, cp);
00436        GETLONG(ttl, cp);
00437        GETSHORT(dlen, cp);
00438        if (type_to_fetch != T_ANY && type != type_to_fetch) {
00439               cp += dlen;
00440               return cp;
00441        }
00442 
00443        if (!store) {
00444               cp += dlen;
00445               return cp;
00446        }
00447 
00448        ALLOC_INIT_ZVAL(*subarray);
00449        array_init(*subarray);
00450 
00451        add_assoc_string(*subarray, "host", name, 1);
00452        switch (type) {
00453               case DNS_T_A:
00454                      add_assoc_string(*subarray, "type", "A", 1);
00455                      snprintf(name, sizeof(name), "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
00456                      add_assoc_string(*subarray, "ip", name, 1);
00457                      cp += dlen;
00458                      break;
00459               case DNS_T_MX:
00460                      add_assoc_string(*subarray, "type", "MX", 1);
00461                      GETSHORT(n, cp);
00462                      add_assoc_long(*subarray, "pri", n);
00463                      /* no break; */
00464               case DNS_T_CNAME:
00465                      if (type == DNS_T_CNAME) {
00466                             add_assoc_string(*subarray, "type", "CNAME", 1);
00467                      }
00468                      /* no break; */
00469               case DNS_T_NS:
00470                      if (type == DNS_T_NS) {
00471                             add_assoc_string(*subarray, "type", "NS", 1);
00472                      }
00473                      /* no break; */
00474               case DNS_T_PTR:
00475                      if (type == DNS_T_PTR) {
00476                             add_assoc_string(*subarray, "type", "PTR", 1);
00477                      }
00478                      n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
00479                      if (n < 0) {
00480                             return NULL;
00481                      }
00482                      cp += n;
00483                      add_assoc_string(*subarray, "target", name, 1);
00484                      break;
00485               case DNS_T_HINFO:
00486                      /* See RFC 1010 for values */
00487                      add_assoc_string(*subarray, "type", "HINFO", 1);
00488                      n = *cp & 0xFF;
00489                      cp++;
00490                      add_assoc_stringl(*subarray, "cpu", (char*)cp, n, 1);
00491                      cp += n;
00492                      n = *cp & 0xFF;
00493                      cp++;
00494                      add_assoc_stringl(*subarray, "os", (char*)cp, n, 1);
00495                      cp += n;
00496                      break;
00497               case DNS_T_TXT:
00498                      {
00499                             int ll = 0;
00500                             zval *entries = NULL;
00501 
00502                             add_assoc_string(*subarray, "type", "TXT", 1);
00503                             tp = emalloc(dlen + 1);
00504                             
00505                             MAKE_STD_ZVAL(entries);
00506                             array_init(entries);
00507                             
00508                             while (ll < dlen) {
00509                                    n = cp[ll];
00510                                    memcpy(tp + ll , cp + ll + 1, n);
00511                                    add_next_index_stringl(entries, cp + ll + 1, n, 1);
00512                                    ll = ll + n + 1;
00513                             }
00514                             tp[dlen] = '\0';
00515                             cp += dlen;
00516 
00517                             add_assoc_stringl(*subarray, "txt", tp, dlen - 1, 0);
00518                             add_assoc_zval(*subarray, "entries", entries);
00519                      }
00520                      break;
00521               case DNS_T_SOA:
00522                      add_assoc_string(*subarray, "type", "SOA", 1);
00523                      n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) -2);
00524                      if (n < 0) {
00525                             return NULL;
00526                      }
00527                      cp += n;
00528                      add_assoc_string(*subarray, "mname", name, 1);
00529                      n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) -2);
00530                      if (n < 0) {
00531                             return NULL;
00532                      }
00533                      cp += n;
00534                      add_assoc_string(*subarray, "rname", name, 1);
00535                      GETLONG(n, cp);
00536                      add_assoc_long(*subarray, "serial", n);
00537                      GETLONG(n, cp);
00538                      add_assoc_long(*subarray, "refresh", n);
00539                      GETLONG(n, cp);
00540                      add_assoc_long(*subarray, "retry", n);
00541                      GETLONG(n, cp);
00542                      add_assoc_long(*subarray, "expire", n);
00543                      GETLONG(n, cp);
00544                      add_assoc_long(*subarray, "minimum-ttl", n);
00545                      break;
00546               case DNS_T_AAAA:
00547                      tp = (u_char*)name;
00548                      for(i=0; i < 8; i++) {
00549                             GETSHORT(s, cp);
00550                             if (s != 0) {
00551                                    if (tp > (u_char *)name) {
00552                                           in_v6_break = 0;
00553                                           tp[0] = ':';
00554                                           tp++;
00555                                    }
00556                                    tp += sprintf((char*)tp,"%x",s);
00557                             } else {
00558                                    if (!have_v6_break) {
00559                                           have_v6_break = 1;
00560                                           in_v6_break = 1;
00561                                           tp[0] = ':';
00562                                           tp++;
00563                                    } else if (!in_v6_break) {
00564                                           tp[0] = ':';
00565                                           tp++;
00566                                           tp[0] = '0';
00567                                           tp++;
00568                                    }
00569                             }
00570                      }
00571                      if (have_v6_break && in_v6_break) {
00572                             tp[0] = ':';
00573                             tp++;
00574                      }
00575                      tp[0] = '\0';
00576                      add_assoc_string(*subarray, "type", "AAAA", 1);
00577                      add_assoc_string(*subarray, "ipv6", name, 1);
00578                      break;
00579               case DNS_T_A6:
00580                      p = cp;
00581                      add_assoc_string(*subarray, "type", "A6", 1);
00582                      n = ((int)cp[0]) & 0xFF;
00583                      cp++;
00584                      add_assoc_long(*subarray, "masklen", n);
00585                      tp = (u_char*)name;
00586                      if (n > 15) {
00587                             have_v6_break = 1;
00588                             in_v6_break = 1;
00589                             tp[0] = ':';
00590                             tp++;
00591                      }
00592                      if (n % 16 > 8) {
00593                             /* Partial short */
00594                             if (cp[0] != 0) {
00595                                    if (tp > (u_char *)name) {
00596                                           in_v6_break = 0;
00597                                           tp[0] = ':';
00598                                           tp++;
00599                                    }
00600                                    sprintf((char*)tp, "%x", cp[0] & 0xFF);
00601                             } else {
00602                                    if (!have_v6_break) {
00603                                           have_v6_break = 1;
00604                                           in_v6_break = 1;
00605                                           tp[0] = ':';
00606                                           tp++;
00607                                    } else if (!in_v6_break) {
00608                                           tp[0] = ':';
00609                                           tp++;
00610                                           tp[0] = '0';
00611                                           tp++;
00612                                    }
00613                             }
00614                             cp++;
00615                      }
00616                      for (i = (n + 8) / 16; i < 8; i++) {
00617                             GETSHORT(s, cp);
00618                             if (s != 0) {
00619                                    if (tp > (u_char *)name) {
00620                                           in_v6_break = 0;
00621                                           tp[0] = ':';
00622                                           tp++;
00623                                    }
00624                                    tp += sprintf((char*)tp,"%x",s);
00625                             } else {
00626                                    if (!have_v6_break) {
00627                                           have_v6_break = 1;
00628                                           in_v6_break = 1;
00629                                           tp[0] = ':';
00630                                           tp++;
00631                                    } else if (!in_v6_break) {
00632                                           tp[0] = ':';
00633                                           tp++;
00634                                           tp[0] = '0';
00635                                           tp++;
00636                                    }
00637                             }
00638                      }
00639                      if (have_v6_break && in_v6_break) {
00640                             tp[0] = ':';
00641                             tp++;
00642                      }
00643                      tp[0] = '\0';
00644                      add_assoc_string(*subarray, "ipv6", name, 1);
00645                      if (cp < p + dlen) {
00646                             n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
00647                             if (n < 0) {
00648                                    return NULL;
00649                             }
00650                             cp += n;
00651                             add_assoc_string(*subarray, "chain", name, 1);
00652                      }
00653                      break;
00654               case DNS_T_SRV:
00655                      add_assoc_string(*subarray, "type", "SRV", 1);
00656                      GETSHORT(n, cp);
00657                      add_assoc_long(*subarray, "pri", n);
00658                      GETSHORT(n, cp);
00659                      add_assoc_long(*subarray, "weight", n);
00660                      GETSHORT(n, cp);
00661                      add_assoc_long(*subarray, "port", n);
00662                      n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
00663                      if (n < 0) {
00664                             return NULL;
00665                      }
00666                      cp += n;
00667                      add_assoc_string(*subarray, "target", name, 1);
00668                      break;
00669               case DNS_T_NAPTR:
00670                      add_assoc_string(*subarray, "type", "NAPTR", 1);
00671                      GETSHORT(n, cp);
00672                      add_assoc_long(*subarray, "order", n);
00673                      GETSHORT(n, cp);
00674                      add_assoc_long(*subarray, "pref", n);
00675                      n = (cp[0] & 0xFF);
00676                      add_assoc_stringl(*subarray, "flags", (char*)++cp, n, 1);
00677                      cp += n;
00678                      n = (cp[0] & 0xFF);
00679                      add_assoc_stringl(*subarray, "services", (char*)++cp, n, 1);
00680                      cp += n;
00681                      n = (cp[0] & 0xFF);
00682                      add_assoc_stringl(*subarray, "regex", (char*)++cp, n, 1);
00683                      cp += n;
00684                      n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
00685                      if (n < 0) {
00686                             return NULL;
00687                      }
00688                      cp += n;
00689                      add_assoc_string(*subarray, "replacement", name, 1);
00690                      break;
00691               default:
00692                      cp += dlen;
00693        }
00694 
00695        add_assoc_string(*subarray, "class", "IN", 1);
00696        add_assoc_long(*subarray, "ttl", ttl);
00697 
00698        return cp;
00699 }
00700 /* }}} */
00701 
00702 /* {{{ proto array|false dns_get_record(string hostname [, int type[, array authns, array addtl]])
00703    Get any Resource Record corresponding to a given Internet host name */
00704 PHP_FUNCTION(dns_get_record)
00705 {
00706        char *hostname;
00707        int hostname_len;
00708        long type_param = PHP_DNS_ANY;
00709        zval *authns = NULL, *addtl = NULL;
00710        int type_to_fetch;
00711 #if defined(HAVE_DNS_SEARCH)
00712        struct sockaddr_storage from;
00713        uint32_t fromsize = sizeof(from);
00714        dns_handle_t handle;
00715 #elif defined(HAVE_RES_NSEARCH)
00716        struct __res_state state;
00717        struct __res_state *handle = &state;
00718 #endif
00719        HEADER *hp;
00720        querybuf answer;
00721        u_char *cp = NULL, *end = NULL;
00722        int n, qd, an, ns = 0, ar = 0;
00723        int type, first_query = 1, store_results = 1;
00724 
00725        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzz", &hostname, &hostname_len, &type_param, &authns, &addtl) == FAILURE) {
00726               return;
00727        }
00728 
00729        if (authns) {
00730               zval_dtor(authns);
00731               array_init(authns);
00732        }
00733        if (addtl) {
00734               zval_dtor(addtl);
00735               array_init(addtl);
00736        }
00737 
00738        if (type_param & ~PHP_DNS_ALL && type_param != PHP_DNS_ANY) {
00739               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%ld' not supported", type_param);
00740               RETURN_FALSE;
00741        }
00742 
00743        /* Initialize the return array */
00744        array_init(return_value);
00745 
00746        /* - We emulate an or'ed type mask by querying type by type. (Steps 0 - NUMTYPES-1 )
00747         *   If additional info is wanted we check again with DNS_T_ANY (step NUMTYPES / NUMTYPES+1 )
00748         *   store_results is used to skip storing the results retrieved in step
00749         *   NUMTYPES+1 when results were already fetched.
00750         * - In case of PHP_DNS_ANY we use the directly fetch DNS_T_ANY. (step NUMTYPES+1 )
00751         */
00752        for (type = (type_param == PHP_DNS_ANY ? (PHP_DNS_NUM_TYPES + 1) : 0);
00753               type < (addtl ? (PHP_DNS_NUM_TYPES + 2) : PHP_DNS_NUM_TYPES) || first_query;
00754               type++
00755        ) {
00756               first_query = 0;
00757               switch (type) {
00758                      case 0:
00759                             type_to_fetch = type_param&PHP_DNS_A     ? DNS_T_A     : 0;
00760                             break;
00761                      case 1:
00762                             type_to_fetch = type_param&PHP_DNS_NS    ? DNS_T_NS    : 0;
00763                             break;
00764                      case 2:
00765                             type_to_fetch = type_param&PHP_DNS_CNAME ? DNS_T_CNAME : 0;
00766                             break;
00767                      case 3:
00768                             type_to_fetch = type_param&PHP_DNS_SOA   ? DNS_T_SOA   : 0;
00769                             break;
00770                      case 4:
00771                             type_to_fetch = type_param&PHP_DNS_PTR   ? DNS_T_PTR   : 0;
00772                             break;
00773                      case 5:
00774                             type_to_fetch = type_param&PHP_DNS_HINFO ? DNS_T_HINFO : 0;
00775                             break;
00776                      case 6:
00777                             type_to_fetch = type_param&PHP_DNS_MX    ? DNS_T_MX    : 0;
00778                             break;
00779                      case 7:
00780                             type_to_fetch = type_param&PHP_DNS_TXT   ? DNS_T_TXT   : 0;
00781                             break;
00782                      case 8:
00783                             type_to_fetch = type_param&PHP_DNS_AAAA    ? DNS_T_AAAA  : 0;
00784                             break;
00785                      case 9:
00786                             type_to_fetch = type_param&PHP_DNS_SRV   ? DNS_T_SRV   : 0;
00787                             break;
00788                      case 10:
00789                             type_to_fetch = type_param&PHP_DNS_NAPTR ? DNS_T_NAPTR : 0;
00790                             break;
00791                      case 11:
00792                             type_to_fetch = type_param&PHP_DNS_A6      ? DNS_T_A6 : 0;
00793                             break;
00794                      case PHP_DNS_NUM_TYPES:
00795                             store_results = 0;
00796                             continue;
00797                      default:
00798                      case (PHP_DNS_NUM_TYPES + 1):
00799                             type_to_fetch = DNS_T_ANY;
00800                             break;
00801               }
00802 
00803               if (type_to_fetch) {
00804 #if defined(HAVE_DNS_SEARCH)
00805                      handle = dns_open(NULL);
00806                      if (handle == NULL) {
00807                             zval_dtor(return_value);
00808                             RETURN_FALSE;
00809                      }
00810 #elif defined(HAVE_RES_NSEARCH)
00811                   memset(&state, 0, sizeof(state));
00812                   if (res_ninit(handle)) {
00813                      zval_dtor(return_value);
00814                             RETURN_FALSE;
00815                      }
00816 #else
00817                      res_init();
00818 #endif
00819 
00820                      n = php_dns_search(handle, hostname, C_IN, type_to_fetch, answer.qb2, sizeof answer);
00821 
00822                      if (n < 0) {
00823                             php_dns_free_handle(handle);
00824                             continue;
00825                      }
00826 
00827                      cp = answer.qb2 + HFIXEDSZ;
00828                      end = answer.qb2 + n;
00829                      hp = (HEADER *)&answer;
00830                      qd = ntohs(hp->qdcount);
00831                      an = ntohs(hp->ancount);
00832                      ns = ntohs(hp->nscount);
00833                      ar = ntohs(hp->arcount);
00834 
00835                      /* Skip QD entries, they're only used by dn_expand later on */
00836                      while (qd-- > 0) {
00837                             n = dn_skipname(cp, end);
00838                             if (n < 0) {
00839                                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to parse DNS data received");
00840                                    zval_dtor(return_value);
00841                                    php_dns_free_handle(handle);
00842                                    RETURN_FALSE;
00843                             }
00844                             cp += n + QFIXEDSZ;
00845                      }
00846 
00847                      /* YAY! Our real answers! */
00848                      while (an-- && cp && cp < end) {
00849                             zval *retval;
00850 
00851                             cp = php_parserr(cp, &answer, type_to_fetch, store_results, &retval);
00852                             if (retval != NULL && store_results) {
00853                                    add_next_index_zval(return_value, retval);
00854                             }
00855                      }
00856 
00857                      if (authns || addtl) {
00858                             /* List of Authoritative Name Servers
00859                              * Process when only requesting addtl so that we can skip through the section
00860                              */
00861                             while (ns-- > 0 && cp && cp < end) {
00862                                    zval *retval = NULL;
00863 
00864                                    cp = php_parserr(cp, &answer, DNS_T_ANY, authns != NULL, &retval);
00865                                    if (retval != NULL) {
00866                                           add_next_index_zval(authns, retval);
00867                                    }
00868                             }
00869                      }
00870 
00871                      if (addtl) {
00872                             /* Additional records associated with authoritative name servers */
00873                             while (ar-- > 0 && cp && cp < end) {
00874                                    zval *retval = NULL;
00875 
00876                                    cp = php_parserr(cp, &answer, DNS_T_ANY, 1, &retval);
00877                                    if (retval != NULL) {
00878                                           add_next_index_zval(addtl, retval);
00879                                    }
00880                             }
00881                      }
00882                      php_dns_free_handle(handle);
00883               }
00884        }
00885 }
00886 /* }}} */
00887 
00888 /* {{{ proto bool dns_get_mx(string hostname, array mxhosts [, array weight])
00889    Get MX records corresponding to a given Internet host name */
00890 PHP_FUNCTION(dns_get_mx)
00891 {
00892        char *hostname;
00893        int hostname_len;
00894        zval *mx_list, *weight_list = NULL;
00895        int count, qdc;
00896        u_short type, weight;
00897        u_char ans[MAXPACKET];
00898        char buf[MAXHOSTNAMELEN];
00899        HEADER *hp;
00900        u_char *cp, *end;
00901        int i;
00902 #if defined(HAVE_DNS_SEARCH)
00903        struct sockaddr_storage from;
00904        uint32_t fromsize = sizeof(from);
00905        dns_handle_t handle;
00906 #elif defined(HAVE_RES_NSEARCH)
00907        struct __res_state state;
00908        struct __res_state *handle = &state;
00909 #endif
00910 
00911        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|z", &hostname, &hostname_len, &mx_list, &weight_list) == FAILURE) {
00912               return;
00913        }
00914 
00915        zval_dtor(mx_list);
00916        array_init(mx_list);
00917 
00918        if (weight_list) {
00919               zval_dtor(weight_list);
00920               array_init(weight_list);
00921        }
00922 
00923 #if defined(HAVE_DNS_SEARCH)
00924        handle = dns_open(NULL);
00925        if (handle == NULL) {
00926               RETURN_FALSE;
00927        }
00928 #elif defined(HAVE_RES_NSEARCH)
00929     memset(&state, 0, sizeof(state));
00930     if (res_ninit(handle)) {
00931                      RETURN_FALSE;
00932        }
00933 #else
00934        res_init();
00935 #endif
00936 
00937        i = php_dns_search(handle, hostname, C_IN, DNS_T_MX, (u_char *)&ans, sizeof(ans));
00938        if (i < 0) {
00939               RETURN_FALSE;
00940        }
00941        if (i > (int)sizeof(ans)) {
00942               i = sizeof(ans);
00943        }
00944        hp = (HEADER *)&ans;
00945        cp = (u_char *)&ans + HFIXEDSZ;
00946        end = (u_char *)&ans +i;
00947        for (qdc = ntohs((unsigned short)hp->qdcount); qdc--; cp += i + QFIXEDSZ) {
00948               if ((i = dn_skipname(cp, end)) < 0 ) {
00949                      php_dns_free_handle(handle);
00950                      RETURN_FALSE;
00951               }
00952        }
00953        count = ntohs((unsigned short)hp->ancount);
00954        while (--count >= 0 && cp < end) {
00955               if ((i = dn_skipname(cp, end)) < 0 ) {
00956                      php_dns_free_handle(handle);
00957                      RETURN_FALSE;
00958               }
00959               cp += i;
00960               GETSHORT(type, cp);
00961               cp += INT16SZ + INT32SZ;
00962               GETSHORT(i, cp);
00963               if (type != DNS_T_MX) {
00964                      cp += i;
00965                      continue;
00966               }
00967               GETSHORT(weight, cp);
00968               if ((i = dn_expand(ans, end, cp, buf, sizeof(buf)-1)) < 0) {
00969                      php_dns_free_handle(handle);
00970                      RETURN_FALSE;
00971               }
00972               cp += i;
00973               add_next_index_string(mx_list, buf, 1);
00974               if (weight_list) {
00975                      add_next_index_long(weight_list, weight);
00976               }
00977        }
00978        php_dns_free_handle(handle);
00979        RETURN_TRUE;
00980 }
00981 /* }}} */
00982 #endif /* HAVE_FULL_DNS_FUNCS */
00983 #endif /* !defined(PHP_WIN32) && (HAVE_DNS_SEARCH_FUNC && !(defined(__BEOS__) || defined(NETWARE))) */
00984 
00985 #if HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32)
00986 PHP_MINIT_FUNCTION(dns) {
00987        REGISTER_LONG_CONSTANT("DNS_A",     PHP_DNS_A,     CONST_CS | CONST_PERSISTENT);
00988        REGISTER_LONG_CONSTANT("DNS_NS",    PHP_DNS_NS,    CONST_CS | CONST_PERSISTENT);
00989        REGISTER_LONG_CONSTANT("DNS_CNAME", PHP_DNS_CNAME, CONST_CS | CONST_PERSISTENT);
00990        REGISTER_LONG_CONSTANT("DNS_SOA",   PHP_DNS_SOA,   CONST_CS | CONST_PERSISTENT);
00991        REGISTER_LONG_CONSTANT("DNS_PTR",   PHP_DNS_PTR,   CONST_CS | CONST_PERSISTENT);
00992        REGISTER_LONG_CONSTANT("DNS_HINFO", PHP_DNS_HINFO, CONST_CS | CONST_PERSISTENT);
00993        REGISTER_LONG_CONSTANT("DNS_MX",    PHP_DNS_MX,    CONST_CS | CONST_PERSISTENT);
00994        REGISTER_LONG_CONSTANT("DNS_TXT",   PHP_DNS_TXT,   CONST_CS | CONST_PERSISTENT);
00995        REGISTER_LONG_CONSTANT("DNS_SRV",   PHP_DNS_SRV,   CONST_CS | CONST_PERSISTENT);
00996        REGISTER_LONG_CONSTANT("DNS_NAPTR", PHP_DNS_NAPTR, CONST_CS | CONST_PERSISTENT);
00997        REGISTER_LONG_CONSTANT("DNS_AAAA",  PHP_DNS_AAAA,  CONST_CS | CONST_PERSISTENT);
00998        REGISTER_LONG_CONSTANT("DNS_A6",    PHP_DNS_A6,    CONST_CS | CONST_PERSISTENT);
00999        REGISTER_LONG_CONSTANT("DNS_ANY",   PHP_DNS_ANY,   CONST_CS | CONST_PERSISTENT);
01000        REGISTER_LONG_CONSTANT("DNS_ALL",   PHP_DNS_ALL,   CONST_CS | CONST_PERSISTENT);
01001        return SUCCESS;
01002 }
01003 #endif /* HAVE_FULL_DNS_FUNCS */
01004 
01005 /*
01006  * Local variables:
01007  * tab-width: 4
01008  * c-basic-offset: 4
01009  * End:
01010  * vim600: sw=4 ts=4 fdm=marker
01011  * vim<600: sw=4 ts=4
01012  */