1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2014 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: The typical suspects                                        |
16   |          Pollita <pollita@php.net>                                   |
17   |          Marcus Boerger <helly@php.net>                              |
18   +----------------------------------------------------------------------+
19 */
20
21/* $Id$ */
22
23/* {{{ includes */
24#include "php.h"
25#include "php_network.h"
26
27#if HAVE_SYS_SOCKET_H
28#include <sys/socket.h>
29#endif
30
31#ifdef PHP_WIN32
32# include "win32/inet.h"
33# include <winsock2.h>
34# include <windows.h>
35# include <Ws2tcpip.h>
36#else   /* This holds good for NetWare too, both for Winsock and Berkeley sockets */
37#include <netinet/in.h>
38#if HAVE_ARPA_INET_H
39#include <arpa/inet.h>
40#endif
41#include <netdb.h>
42#ifdef _OSD_POSIX
43#undef STATUS
44#undef T_UNSPEC
45#endif
46#if HAVE_ARPA_NAMESER_H
47#ifdef DARWIN
48# define BIND_8_COMPAT 1
49#endif
50#include <arpa/nameser.h>
51#endif
52#if HAVE_RESOLV_H
53#include <resolv.h>
54#endif
55#ifdef HAVE_DNS_H
56#include <dns.h>
57#endif
58#endif
59
60/* Borrowed from SYS/SOCKET.H */
61#if defined(NETWARE) && defined(USE_WINSOCK)
62#define AF_INET 2   /* internetwork: UDP, TCP, etc. */
63#endif
64
65#ifndef MAXHOSTNAMELEN
66#define MAXHOSTNAMELEN 255
67#endif
68
69/* For the local hostname obtained via gethostname which is different from the
70   dns-related MAXHOSTNAMELEN constant above */
71#ifndef HOST_NAME_MAX
72#define HOST_NAME_MAX 255
73#endif
74
75#include "php_dns.h"
76
77/* type compat */
78#ifndef DNS_T_A
79#define DNS_T_A     1
80#endif
81#ifndef DNS_T_NS
82#define DNS_T_NS    2
83#endif
84#ifndef DNS_T_CNAME
85#define DNS_T_CNAME 5
86#endif
87#ifndef DNS_T_SOA
88#define DNS_T_SOA   6
89#endif
90#ifndef DNS_T_PTR
91#define DNS_T_PTR   12
92#endif
93#ifndef DNS_T_HINFO
94#define DNS_T_HINFO 13
95#endif
96#ifndef DNS_T_MINFO
97#define DNS_T_MINFO 14
98#endif
99#ifndef DNS_T_MX
100#define DNS_T_MX    15
101#endif
102#ifndef DNS_T_TXT
103#define DNS_T_TXT   16
104#endif
105#ifndef DNS_T_AAAA
106#define DNS_T_AAAA  28
107#endif
108#ifndef DNS_T_SRV
109#define DNS_T_SRV   33
110#endif
111#ifndef DNS_T_NAPTR
112#define DNS_T_NAPTR 35
113#endif
114#ifndef DNS_T_A6
115#define DNS_T_A6    38
116#endif
117
118#ifndef DNS_T_ANY
119#define DNS_T_ANY   255
120#endif
121/* }}} */
122
123static char *php_gethostbyaddr(char *ip);
124static char *php_gethostbyname(char *name);
125
126#ifdef HAVE_GETHOSTNAME
127/* {{{ proto string gethostname()
128   Get the host name of the current machine */
129PHP_FUNCTION(gethostname)
130{
131    char buf[HOST_NAME_MAX];
132
133    if (zend_parse_parameters_none() == FAILURE) {
134        return;
135    }
136
137    if (gethostname(buf, sizeof(buf) - 1)) {
138        php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to fetch host [%d]: %s", errno, strerror(errno));
139        RETURN_FALSE;
140    }
141
142    RETURN_STRING(buf, 1);
143}
144/* }}} */
145#endif
146
147/* TODO: Reimplement the gethostby* functions using the new winxp+ API, in dns_win32.c, then
148 we can have a dns.c, dns_unix.c and dns_win32.c instead of a messy dns.c full of #ifdef
149*/
150
151/* {{{ proto string gethostbyaddr(string ip_address)
152   Get the Internet host name corresponding to a given IP address */
153PHP_FUNCTION(gethostbyaddr)
154{
155    char *addr;
156    int addr_len;
157    char *hostname;
158
159    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &addr, &addr_len) == FAILURE) {
160        return;
161    }
162
163    hostname = php_gethostbyaddr(addr);
164
165    if (hostname == NULL) {
166#if HAVE_IPV6 && HAVE_INET_PTON
167        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Address is not a valid IPv4 or IPv6 address");
168#else
169        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Address is not in a.b.c.d form");
170#endif
171        RETVAL_FALSE;
172    } else {
173        RETVAL_STRING(hostname, 0);
174    }
175}
176/* }}} */
177
178/* {{{ php_gethostbyaddr */
179static char *php_gethostbyaddr(char *ip)
180{
181#if HAVE_IPV6 && HAVE_INET_PTON
182    struct in6_addr addr6;
183#endif
184    struct in_addr addr;
185    struct hostent *hp;
186
187#if HAVE_IPV6 && HAVE_INET_PTON
188    if (inet_pton(AF_INET6, ip, &addr6)) {
189        hp = gethostbyaddr((char *) &addr6, sizeof(addr6), AF_INET6);
190    } else if (inet_pton(AF_INET, ip, &addr)) {
191        hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
192    } else {
193        return NULL;
194    }
195#else
196    addr.s_addr = inet_addr(ip);
197
198    if (addr.s_addr == -1) {
199        return NULL;
200    }
201
202    hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
203#endif
204
205    if (!hp || hp->h_name == NULL || hp->h_name[0] == '\0') {
206        return estrdup(ip);
207    }
208
209    return estrdup(hp->h_name);
210}
211/* }}} */
212
213/* {{{ proto string gethostbyname(string hostname)
214   Get the IP address corresponding to a given Internet host name */
215PHP_FUNCTION(gethostbyname)
216{
217    char *hostname;
218    int hostname_len;
219    char *addr;
220
221    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &hostname, &hostname_len) == FAILURE) {
222        return;
223    }
224
225    addr = php_gethostbyname(hostname);
226
227    RETVAL_STRING(addr, 0);
228}
229/* }}} */
230
231/* {{{ proto array gethostbynamel(string hostname)
232   Return a list of IP addresses that a given hostname resolves to. */
233PHP_FUNCTION(gethostbynamel)
234{
235    char *hostname;
236    int hostname_len;
237    struct hostent *hp;
238    struct in_addr in;
239    int i;
240
241    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &hostname, &hostname_len) == FAILURE) {
242        return;
243    }
244
245    hp = gethostbyname(hostname);
246    if (hp == NULL || hp->h_addr_list == NULL) {
247        RETURN_FALSE;
248    }
249
250    array_init(return_value);
251
252    for (i = 0 ; hp->h_addr_list[i] != 0 ; i++) {
253        in = *(struct in_addr *) hp->h_addr_list[i];
254        add_next_index_string(return_value, inet_ntoa(in), 1);
255    }
256}
257/* }}} */
258
259/* {{{ php_gethostbyname */
260static char *php_gethostbyname(char *name)
261{
262    struct hostent *hp;
263    struct in_addr in;
264
265    hp = gethostbyname(name);
266
267    if (!hp || !*(hp->h_addr_list)) {
268        return estrdup(name);
269    }
270
271    memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr));
272
273    return estrdup(inet_ntoa(in));
274}
275/* }}} */
276
277#if HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32)
278# define PHP_DNS_NUM_TYPES  12  /* Number of DNS Types Supported by PHP currently */
279
280# define PHP_DNS_A      0x00000001
281# define PHP_DNS_NS     0x00000002
282# define PHP_DNS_CNAME  0x00000010
283# define PHP_DNS_SOA    0x00000020
284# define PHP_DNS_PTR    0x00000800
285# define PHP_DNS_HINFO  0x00001000
286# define PHP_DNS_MX     0x00004000
287# define PHP_DNS_TXT    0x00008000
288# define PHP_DNS_A6     0x01000000
289# define PHP_DNS_SRV    0x02000000
290# define PHP_DNS_NAPTR  0x04000000
291# define PHP_DNS_AAAA   0x08000000
292# define PHP_DNS_ANY    0x10000000
293# 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)
294#endif /* HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32) */
295
296/* Note: These functions are defined in ext/standard/dns_win32.c for Windows! */
297#if !defined(PHP_WIN32) && (HAVE_DNS_SEARCH_FUNC && !(defined(__BEOS__) || defined(NETWARE)))
298
299#ifndef HFIXEDSZ
300#define HFIXEDSZ        12      /* fixed data in header <arpa/nameser.h> */
301#endif /* HFIXEDSZ */
302
303#ifndef QFIXEDSZ
304#define QFIXEDSZ        4       /* fixed data in query <arpa/nameser.h> */
305#endif /* QFIXEDSZ */
306
307#undef MAXHOSTNAMELEN
308#define MAXHOSTNAMELEN  1024
309
310#ifndef MAXRESOURCERECORDS
311#define MAXRESOURCERECORDS  64
312#endif /* MAXRESOURCERECORDS */
313
314typedef union {
315    HEADER qb1;
316    u_char qb2[65536];
317} querybuf;
318
319/* just a hack to free resources allocated by glibc in __res_nsend()
320 * See also:
321 *   res_thread_freeres() in glibc/resolv/res_init.c
322 *   __libc_res_nsend()   in resolv/res_send.c
323 * */
324
325#if defined(__GLIBC__) && !defined(HAVE_DEPRECATED_DNS_FUNCS)
326#define php_dns_free_res(__res__) _php_dns_free_res(__res__)
327static void _php_dns_free_res(struct __res_state res) { /* {{{ */
328    int ns;
329    for (ns = 0; ns < MAXNS; ns++) {
330        if (res._u._ext.nsaddrs[ns] != NULL) {
331            free (res._u._ext.nsaddrs[ns]);
332            res._u._ext.nsaddrs[ns] = NULL;
333        }
334    }
335} /* }}} */
336#else
337#define php_dns_free_res(__res__)
338#endif
339
340/* {{{ proto bool dns_check_record(string host [, string type])
341   Check DNS records corresponding to a given Internet host name or IP address */
342PHP_FUNCTION(dns_check_record)
343{
344#ifndef MAXPACKET
345#define MAXPACKET  8192 /* max packet size used internally by BIND */
346#endif
347    u_char ans[MAXPACKET];
348    char *hostname, *rectype = NULL;
349    int hostname_len, rectype_len = 0;
350    int type = T_MX, i;
351#if defined(HAVE_DNS_SEARCH)
352    struct sockaddr_storage from;
353    uint32_t fromsize = sizeof(from);
354    dns_handle_t handle;
355#elif defined(HAVE_RES_NSEARCH)
356    struct __res_state state;
357    struct __res_state *handle = &state;
358#endif
359
360    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &hostname, &hostname_len, &rectype, &rectype_len) == FAILURE) {
361        return;
362    }
363
364    if (hostname_len == 0) {
365        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host cannot be empty");
366        RETURN_FALSE;
367    }
368
369    if (rectype) {
370        if (!strcasecmp("A",     rectype)) type = T_A;
371        else if (!strcasecmp("NS",    rectype)) type = DNS_T_NS;
372        else if (!strcasecmp("MX",    rectype)) type = DNS_T_MX;
373        else if (!strcasecmp("PTR",   rectype)) type = DNS_T_PTR;
374        else if (!strcasecmp("ANY",   rectype)) type = DNS_T_ANY;
375        else if (!strcasecmp("SOA",   rectype)) type = DNS_T_SOA;
376        else if (!strcasecmp("TXT",   rectype)) type = DNS_T_TXT;
377        else if (!strcasecmp("CNAME", rectype)) type = DNS_T_CNAME;
378        else if (!strcasecmp("AAAA",  rectype)) type = DNS_T_AAAA;
379        else if (!strcasecmp("SRV",   rectype)) type = DNS_T_SRV;
380        else if (!strcasecmp("NAPTR", rectype)) type = DNS_T_NAPTR;
381        else if (!strcasecmp("A6",    rectype)) type = DNS_T_A6;
382        else {
383            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%s' not supported", rectype);
384            RETURN_FALSE;
385        }
386    }
387
388#if defined(HAVE_DNS_SEARCH)
389    handle = dns_open(NULL);
390    if (handle == NULL) {
391        RETURN_FALSE;
392    }
393#elif defined(HAVE_RES_NSEARCH)
394    memset(&state, 0, sizeof(state));
395    if (res_ninit(handle)) {
396            RETURN_FALSE;
397    }
398#else
399    res_init();
400#endif
401
402    RETVAL_TRUE;
403    i = php_dns_search(handle, hostname, C_IN, type, ans, sizeof(ans));
404
405    if (i < 0) {
406        RETVAL_FALSE;
407    }
408
409    php_dns_free_handle(handle);
410}
411/* }}} */
412
413#if HAVE_FULL_DNS_FUNCS
414
415#define CHECKCP(n) do { \
416    if (cp + n > end) { \
417        return NULL; \
418    } \
419} while (0)
420
421/* {{{ php_parserr */
422static u_char *php_parserr(u_char *cp, u_char *end, querybuf *answer, int type_to_fetch, int store, int raw, zval **subarray)
423{
424    u_short type, class, dlen;
425    u_long ttl;
426    long n, i;
427    u_short s;
428    u_char *tp, *p;
429    char name[MAXHOSTNAMELEN];
430    int have_v6_break = 0, in_v6_break = 0;
431
432    *subarray = NULL;
433
434    n = dn_expand(answer->qb2, end, cp, name, sizeof(name) - 2);
435    if (n < 0) {
436        return NULL;
437    }
438    cp += n;
439
440    CHECKCP(10);
441    GETSHORT(type, cp);
442    GETSHORT(class, cp);
443    GETLONG(ttl, cp);
444    GETSHORT(dlen, cp);
445    CHECKCP(dlen);
446    if (type_to_fetch != T_ANY && type != type_to_fetch) {
447        cp += dlen;
448        return cp;
449    }
450
451    if (!store) {
452        cp += dlen;
453        return cp;
454    }
455
456    ALLOC_INIT_ZVAL(*subarray);
457    array_init(*subarray);
458
459    add_assoc_string(*subarray, "host", name, 1);
460    add_assoc_string(*subarray, "class", "IN", 1);
461    add_assoc_long(*subarray, "ttl", ttl);
462
463    if (raw) {
464        add_assoc_long(*subarray, "type", type);
465        add_assoc_stringl(*subarray, "data", (char*) cp, (uint) dlen, 1);
466        cp += dlen;
467        return cp;
468    }
469
470    switch (type) {
471        case DNS_T_A:
472            CHECKCP(4);
473            add_assoc_string(*subarray, "type", "A", 1);
474            snprintf(name, sizeof(name), "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
475            add_assoc_string(*subarray, "ip", name, 1);
476            cp += dlen;
477            break;
478        case DNS_T_MX:
479            CHECKCP(2);
480            add_assoc_string(*subarray, "type", "MX", 1);
481            GETSHORT(n, cp);
482            add_assoc_long(*subarray, "pri", n);
483            /* no break; */
484        case DNS_T_CNAME:
485            if (type == DNS_T_CNAME) {
486                add_assoc_string(*subarray, "type", "CNAME", 1);
487            }
488            /* no break; */
489        case DNS_T_NS:
490            if (type == DNS_T_NS) {
491                add_assoc_string(*subarray, "type", "NS", 1);
492            }
493            /* no break; */
494        case DNS_T_PTR:
495            if (type == DNS_T_PTR) {
496                add_assoc_string(*subarray, "type", "PTR", 1);
497            }
498            n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
499            if (n < 0) {
500                return NULL;
501            }
502            cp += n;
503            add_assoc_string(*subarray, "target", name, 1);
504            break;
505        case DNS_T_HINFO:
506            /* See RFC 1010 for values */
507            add_assoc_string(*subarray, "type", "HINFO", 1);
508            CHECKCP(1);
509            n = *cp & 0xFF;
510            cp++;
511            CHECKCP(n);
512            add_assoc_stringl(*subarray, "cpu", (char*)cp, n, 1);
513            cp += n;
514            CHECKCP(1);
515            n = *cp & 0xFF;
516            cp++;
517            CHECKCP(n);
518            add_assoc_stringl(*subarray, "os", (char*)cp, n, 1);
519            cp += n;
520            break;
521        case DNS_T_TXT:
522            {
523                int l1 = 0, l2 = 0;
524                zval *entries = NULL;
525
526                add_assoc_string(*subarray, "type", "TXT", 1);
527                tp = emalloc(dlen + 1);
528
529                MAKE_STD_ZVAL(entries);
530                array_init(entries);
531
532                while (l1 < dlen) {
533                    n = cp[l1];
534                    if ((l1 + n) >= dlen) {
535                        // Invalid chunk length, truncate
536                        n = dlen - (l1 + 1);
537                    }
538                    if (n) {
539                        memcpy(tp + l2 , cp + l1 + 1, n);
540                        add_next_index_stringl(entries, cp + l1 + 1, n, 1);
541                    }
542                    l1 = l1 + n + 1;
543                    l2 = l2 + n;
544                }
545                tp[l2] = '\0';
546                cp += dlen;
547
548                add_assoc_stringl(*subarray, "txt", tp, l2, 0);
549                add_assoc_zval(*subarray, "entries", entries);
550            }
551            break;
552        case DNS_T_SOA:
553            add_assoc_string(*subarray, "type", "SOA", 1);
554            n = dn_expand(answer->qb2, end, cp, name, (sizeof name) -2);
555            if (n < 0) {
556                return NULL;
557            }
558            cp += n;
559            add_assoc_string(*subarray, "mname", name, 1);
560            n = dn_expand(answer->qb2, end, cp, name, (sizeof name) -2);
561            if (n < 0) {
562                return NULL;
563            }
564            cp += n;
565            add_assoc_string(*subarray, "rname", name, 1);
566            CHECKCP(5*4);
567            GETLONG(n, cp);
568            add_assoc_long(*subarray, "serial", n);
569            GETLONG(n, cp);
570            add_assoc_long(*subarray, "refresh", n);
571            GETLONG(n, cp);
572            add_assoc_long(*subarray, "retry", n);
573            GETLONG(n, cp);
574            add_assoc_long(*subarray, "expire", n);
575            GETLONG(n, cp);
576            add_assoc_long(*subarray, "minimum-ttl", n);
577            break;
578        case DNS_T_AAAA:
579            tp = (u_char*)name;
580            CHECKCP(8*2);
581            for(i=0; i < 8; i++) {
582                GETSHORT(s, cp);
583                if (s != 0) {
584                    if (tp > (u_char *)name) {
585                        in_v6_break = 0;
586                        tp[0] = ':';
587                        tp++;
588                    }
589                    tp += sprintf((char*)tp,"%x",s);
590                } else {
591                    if (!have_v6_break) {
592                        have_v6_break = 1;
593                        in_v6_break = 1;
594                        tp[0] = ':';
595                        tp++;
596                    } else if (!in_v6_break) {
597                        tp[0] = ':';
598                        tp++;
599                        tp[0] = '0';
600                        tp++;
601                    }
602                }
603            }
604            if (have_v6_break && in_v6_break) {
605                tp[0] = ':';
606                tp++;
607            }
608            tp[0] = '\0';
609            add_assoc_string(*subarray, "type", "AAAA", 1);
610            add_assoc_string(*subarray, "ipv6", name, 1);
611            break;
612        case DNS_T_A6:
613            p = cp;
614            add_assoc_string(*subarray, "type", "A6", 1);
615            CHECKCP(1);
616            n = ((int)cp[0]) & 0xFF;
617            cp++;
618            add_assoc_long(*subarray, "masklen", n);
619            tp = (u_char*)name;
620            if (n > 15) {
621                have_v6_break = 1;
622                in_v6_break = 1;
623                tp[0] = ':';
624                tp++;
625            }
626            if (n % 16 > 8) {
627                /* Partial short */
628                if (cp[0] != 0) {
629                    if (tp > (u_char *)name) {
630                        in_v6_break = 0;
631                        tp[0] = ':';
632                        tp++;
633                    }
634                    sprintf((char*)tp, "%x", cp[0] & 0xFF);
635                } else {
636                    if (!have_v6_break) {
637                        have_v6_break = 1;
638                        in_v6_break = 1;
639                        tp[0] = ':';
640                        tp++;
641                    } else if (!in_v6_break) {
642                        tp[0] = ':';
643                        tp++;
644                        tp[0] = '0';
645                        tp++;
646                    }
647                }
648                cp++;
649            }
650            for (i = (n + 8) / 16; i < 8; i++) {
651                CHECKCP(2);
652                GETSHORT(s, cp);
653                if (s != 0) {
654                    if (tp > (u_char *)name) {
655                        in_v6_break = 0;
656                        tp[0] = ':';
657                        tp++;
658                    }
659                    tp += sprintf((char*)tp,"%x",s);
660                } else {
661                    if (!have_v6_break) {
662                        have_v6_break = 1;
663                        in_v6_break = 1;
664                        tp[0] = ':';
665                        tp++;
666                    } else if (!in_v6_break) {
667                        tp[0] = ':';
668                        tp++;
669                        tp[0] = '0';
670                        tp++;
671                    }
672                }
673            }
674            if (have_v6_break && in_v6_break) {
675                tp[0] = ':';
676                tp++;
677            }
678            tp[0] = '\0';
679            add_assoc_string(*subarray, "ipv6", name, 1);
680            if (cp < p + dlen) {
681                n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
682                if (n < 0) {
683                    return NULL;
684                }
685                cp += n;
686                add_assoc_string(*subarray, "chain", name, 1);
687            }
688            break;
689        case DNS_T_SRV:
690            CHECKCP(3*2);
691            add_assoc_string(*subarray, "type", "SRV", 1);
692            GETSHORT(n, cp);
693            add_assoc_long(*subarray, "pri", n);
694            GETSHORT(n, cp);
695            add_assoc_long(*subarray, "weight", n);
696            GETSHORT(n, cp);
697            add_assoc_long(*subarray, "port", n);
698            n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
699            if (n < 0) {
700                return NULL;
701            }
702            cp += n;
703            add_assoc_string(*subarray, "target", name, 1);
704            break;
705        case DNS_T_NAPTR:
706            CHECKCP(2*2);
707            add_assoc_string(*subarray, "type", "NAPTR", 1);
708            GETSHORT(n, cp);
709            add_assoc_long(*subarray, "order", n);
710            GETSHORT(n, cp);
711            add_assoc_long(*subarray, "pref", n);
712
713            CHECKCP(1);
714            n = (cp[0] & 0xFF);
715            cp++;
716            CHECKCP(n);
717            add_assoc_stringl(*subarray, "flags", (char*)cp, n, 1);
718            cp += n;
719
720            CHECKCP(1);
721            n = (cp[0] & 0xFF);
722            cp++;
723            CHECKCP(n);
724            add_assoc_stringl(*subarray, "services", (char*)cp, n, 1);
725            cp += n;
726
727            CHECKCP(1);
728            n = (cp[0] & 0xFF);
729            cp++;
730            CHECKCP(n);
731            add_assoc_stringl(*subarray, "regex", (char*)cp, n, 1);
732            cp += n;
733
734            n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
735            if (n < 0) {
736                return NULL;
737            }
738            cp += n;
739            add_assoc_string(*subarray, "replacement", name, 1);
740            break;
741        default:
742            zval_ptr_dtor(subarray);
743            *subarray = NULL;
744            cp += dlen;
745            break;
746    }
747
748    return cp;
749}
750/* }}} */
751
752/* {{{ proto array|false dns_get_record(string hostname [, int type[, array authns, array addtl]])
753   Get any Resource Record corresponding to a given Internet host name */
754PHP_FUNCTION(dns_get_record)
755{
756    char *hostname;
757    int hostname_len;
758    long type_param = PHP_DNS_ANY;
759    zval *authns = NULL, *addtl = NULL;
760    int type_to_fetch;
761#if defined(HAVE_DNS_SEARCH)
762    struct sockaddr_storage from;
763    uint32_t fromsize = sizeof(from);
764    dns_handle_t handle;
765#elif defined(HAVE_RES_NSEARCH)
766    struct __res_state state;
767    struct __res_state *handle = &state;
768#endif
769    HEADER *hp;
770    querybuf answer;
771    u_char *cp = NULL, *end = NULL;
772    int n, qd, an, ns = 0, ar = 0;
773    int type, first_query = 1, store_results = 1;
774    zend_bool raw = 0;
775
776    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz!z!b",
777            &hostname, &hostname_len, &type_param, &authns, &addtl, &raw) == FAILURE) {
778        return;
779    }
780
781    if (authns) {
782        zval_dtor(authns);
783        array_init(authns);
784    }
785    if (addtl) {
786        zval_dtor(addtl);
787        array_init(addtl);
788    }
789
790    if (!raw) {
791        if ((type_param & ~PHP_DNS_ALL) && (type_param != PHP_DNS_ANY)) {
792            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%ld' not supported", type_param);
793            RETURN_FALSE;
794        }
795    } else {
796        if ((type_param < 1) || (type_param > 0xFFFF)) {
797            php_error_docref(NULL TSRMLS_CC, E_WARNING,
798                "Numeric DNS record type must be between 1 and 65535, '%ld' given", type_param);
799            RETURN_FALSE;
800        }
801    }
802
803    /* Initialize the return array */
804    array_init(return_value);
805
806    /* - We emulate an or'ed type mask by querying type by type. (Steps 0 - NUMTYPES-1 )
807     *   If additional info is wanted we check again with DNS_T_ANY (step NUMTYPES / NUMTYPES+1 )
808     *   store_results is used to skip storing the results retrieved in step
809     *   NUMTYPES+1 when results were already fetched.
810     * - In case of PHP_DNS_ANY we use the directly fetch DNS_T_ANY. (step NUMTYPES+1 )
811     * - In case of raw mode, we query only the requestd type instead of looping type by type
812     *   before going with the additional info stuff.
813     */
814
815    if (raw) {
816        type = -1;
817    } else if (type_param == PHP_DNS_ANY) {
818        type = PHP_DNS_NUM_TYPES + 1;
819    } else {
820        type = 0;
821    }
822
823    for ( ;
824        type < (addtl ? (PHP_DNS_NUM_TYPES + 2) : PHP_DNS_NUM_TYPES) || first_query;
825        type++
826    ) {
827        first_query = 0;
828        switch (type) {
829            case -1: /* raw */
830                type_to_fetch = type_param;
831                /* skip over the rest and go directly to additional records */
832                type = PHP_DNS_NUM_TYPES - 1;
833                break;
834            case 0:
835                type_to_fetch = type_param&PHP_DNS_A     ? DNS_T_A     : 0;
836                break;
837            case 1:
838                type_to_fetch = type_param&PHP_DNS_NS    ? DNS_T_NS    : 0;
839                break;
840            case 2:
841                type_to_fetch = type_param&PHP_DNS_CNAME ? DNS_T_CNAME : 0;
842                break;
843            case 3:
844                type_to_fetch = type_param&PHP_DNS_SOA   ? DNS_T_SOA   : 0;
845                break;
846            case 4:
847                type_to_fetch = type_param&PHP_DNS_PTR   ? DNS_T_PTR   : 0;
848                break;
849            case 5:
850                type_to_fetch = type_param&PHP_DNS_HINFO ? DNS_T_HINFO : 0;
851                break;
852            case 6:
853                type_to_fetch = type_param&PHP_DNS_MX    ? DNS_T_MX    : 0;
854                break;
855            case 7:
856                type_to_fetch = type_param&PHP_DNS_TXT   ? DNS_T_TXT   : 0;
857                break;
858            case 8:
859                type_to_fetch = type_param&PHP_DNS_AAAA  ? DNS_T_AAAA  : 0;
860                break;
861            case 9:
862                type_to_fetch = type_param&PHP_DNS_SRV   ? DNS_T_SRV   : 0;
863                break;
864            case 10:
865                type_to_fetch = type_param&PHP_DNS_NAPTR ? DNS_T_NAPTR : 0;
866                break;
867            case 11:
868                type_to_fetch = type_param&PHP_DNS_A6    ? DNS_T_A6 : 0;
869                break;
870            case PHP_DNS_NUM_TYPES:
871                store_results = 0;
872                continue;
873            default:
874            case (PHP_DNS_NUM_TYPES + 1):
875                type_to_fetch = DNS_T_ANY;
876                break;
877        }
878
879        if (type_to_fetch) {
880#if defined(HAVE_DNS_SEARCH)
881            handle = dns_open(NULL);
882            if (handle == NULL) {
883                zval_dtor(return_value);
884                RETURN_FALSE;
885            }
886#elif defined(HAVE_RES_NSEARCH)
887            memset(&state, 0, sizeof(state));
888            if (res_ninit(handle)) {
889                zval_dtor(return_value);
890                RETURN_FALSE;
891            }
892#else
893            res_init();
894#endif
895
896            n = php_dns_search(handle, hostname, C_IN, type_to_fetch, answer.qb2, sizeof answer);
897
898            if (n < 0) {
899                php_dns_free_handle(handle);
900                switch (h_errno) {
901                    case NO_DATA:
902                    case HOST_NOT_FOUND:
903                        continue;
904
905                    case NO_RECOVERY:
906                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "An unexpected server failure occurred.");
907                        break;
908
909                    case TRY_AGAIN:
910                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "A temporary server error occurred.");
911                        break;
912
913                    default:
914                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "DNS Query failed");
915                }
916                zval_dtor(return_value);
917                RETURN_FALSE;
918            }
919
920            cp = answer.qb2 + HFIXEDSZ;
921            end = answer.qb2 + n;
922            hp = (HEADER *)&answer;
923            qd = ntohs(hp->qdcount);
924            an = ntohs(hp->ancount);
925            ns = ntohs(hp->nscount);
926            ar = ntohs(hp->arcount);
927
928            /* Skip QD entries, they're only used by dn_expand later on */
929            while (qd-- > 0) {
930                n = dn_skipname(cp, end);
931                if (n < 0) {
932                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to parse DNS data received");
933                    zval_dtor(return_value);
934                    php_dns_free_handle(handle);
935                    RETURN_FALSE;
936                }
937                cp += n + QFIXEDSZ;
938            }
939
940            /* YAY! Our real answers! */
941            while (an-- && cp && cp < end) {
942                zval *retval;
943
944                cp = php_parserr(cp, end, &answer, type_to_fetch, store_results, raw, &retval);
945                if (retval != NULL && store_results) {
946                    add_next_index_zval(return_value, retval);
947                }
948            }
949
950            if (authns || addtl) {
951                /* List of Authoritative Name Servers
952                 * Process when only requesting addtl so that we can skip through the section
953                 */
954                while (ns-- > 0 && cp && cp < end) {
955                    zval *retval = NULL;
956
957                    cp = php_parserr(cp, end, &answer, DNS_T_ANY, authns != NULL, raw, &retval);
958                    if (retval != NULL) {
959                        add_next_index_zval(authns, retval);
960                    }
961                }
962            }
963
964            if (addtl) {
965                /* Additional records associated with authoritative name servers */
966                while (ar-- > 0 && cp && cp < end) {
967                    zval *retval = NULL;
968
969                    cp = php_parserr(cp, end, &answer, DNS_T_ANY, 1, raw, &retval);
970                    if (retval != NULL) {
971                        add_next_index_zval(addtl, retval);
972                    }
973                }
974            }
975            php_dns_free_handle(handle);
976        }
977    }
978}
979/* }}} */
980
981/* {{{ proto bool dns_get_mx(string hostname, array mxhosts [, array weight])
982   Get MX records corresponding to a given Internet host name */
983PHP_FUNCTION(dns_get_mx)
984{
985    char *hostname;
986    int hostname_len;
987    zval *mx_list, *weight_list = NULL;
988    int count, qdc;
989    u_short type, weight;
990    u_char ans[MAXPACKET];
991    char buf[MAXHOSTNAMELEN];
992    HEADER *hp;
993    u_char *cp, *end;
994    int i;
995#if defined(HAVE_DNS_SEARCH)
996    struct sockaddr_storage from;
997    uint32_t fromsize = sizeof(from);
998    dns_handle_t handle;
999#elif defined(HAVE_RES_NSEARCH)
1000    struct __res_state state;
1001    struct __res_state *handle = &state;
1002#endif
1003
1004    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|z", &hostname, &hostname_len, &mx_list, &weight_list) == FAILURE) {
1005        return;
1006    }
1007
1008    zval_dtor(mx_list);
1009    array_init(mx_list);
1010
1011    if (weight_list) {
1012        zval_dtor(weight_list);
1013        array_init(weight_list);
1014    }
1015
1016#if defined(HAVE_DNS_SEARCH)
1017    handle = dns_open(NULL);
1018    if (handle == NULL) {
1019        RETURN_FALSE;
1020    }
1021#elif defined(HAVE_RES_NSEARCH)
1022    memset(&state, 0, sizeof(state));
1023    if (res_ninit(handle)) {
1024            RETURN_FALSE;
1025    }
1026#else
1027    res_init();
1028#endif
1029
1030    i = php_dns_search(handle, hostname, C_IN, DNS_T_MX, (u_char *)&ans, sizeof(ans));
1031    if (i < 0) {
1032        RETURN_FALSE;
1033    }
1034    if (i > (int)sizeof(ans)) {
1035        i = sizeof(ans);
1036    }
1037    hp = (HEADER *)&ans;
1038    cp = (u_char *)&ans + HFIXEDSZ;
1039    end = (u_char *)&ans +i;
1040    for (qdc = ntohs((unsigned short)hp->qdcount); qdc--; cp += i + QFIXEDSZ) {
1041        if ((i = dn_skipname(cp, end)) < 0 ) {
1042            php_dns_free_handle(handle);
1043            RETURN_FALSE;
1044        }
1045    }
1046    count = ntohs((unsigned short)hp->ancount);
1047    while (--count >= 0 && cp < end) {
1048        if ((i = dn_skipname(cp, end)) < 0 ) {
1049            php_dns_free_handle(handle);
1050            RETURN_FALSE;
1051        }
1052        cp += i;
1053        GETSHORT(type, cp);
1054        cp += INT16SZ + INT32SZ;
1055        GETSHORT(i, cp);
1056        if (type != DNS_T_MX) {
1057            cp += i;
1058            continue;
1059        }
1060        GETSHORT(weight, cp);
1061        if ((i = dn_expand(ans, end, cp, buf, sizeof(buf)-1)) < 0) {
1062            php_dns_free_handle(handle);
1063            RETURN_FALSE;
1064        }
1065        cp += i;
1066        add_next_index_string(mx_list, buf, 1);
1067        if (weight_list) {
1068            add_next_index_long(weight_list, weight);
1069        }
1070    }
1071    php_dns_free_handle(handle);
1072    RETURN_TRUE;
1073}
1074/* }}} */
1075#endif /* HAVE_FULL_DNS_FUNCS */
1076#endif /* !defined(PHP_WIN32) && (HAVE_DNS_SEARCH_FUNC && !(defined(__BEOS__) || defined(NETWARE))) */
1077
1078#if HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32)
1079PHP_MINIT_FUNCTION(dns) {
1080    REGISTER_LONG_CONSTANT("DNS_A",     PHP_DNS_A,     CONST_CS | CONST_PERSISTENT);
1081    REGISTER_LONG_CONSTANT("DNS_NS",    PHP_DNS_NS,    CONST_CS | CONST_PERSISTENT);
1082    REGISTER_LONG_CONSTANT("DNS_CNAME", PHP_DNS_CNAME, CONST_CS | CONST_PERSISTENT);
1083    REGISTER_LONG_CONSTANT("DNS_SOA",   PHP_DNS_SOA,   CONST_CS | CONST_PERSISTENT);
1084    REGISTER_LONG_CONSTANT("DNS_PTR",   PHP_DNS_PTR,   CONST_CS | CONST_PERSISTENT);
1085    REGISTER_LONG_CONSTANT("DNS_HINFO", PHP_DNS_HINFO, CONST_CS | CONST_PERSISTENT);
1086    REGISTER_LONG_CONSTANT("DNS_MX",    PHP_DNS_MX,    CONST_CS | CONST_PERSISTENT);
1087    REGISTER_LONG_CONSTANT("DNS_TXT",   PHP_DNS_TXT,   CONST_CS | CONST_PERSISTENT);
1088    REGISTER_LONG_CONSTANT("DNS_SRV",   PHP_DNS_SRV,   CONST_CS | CONST_PERSISTENT);
1089    REGISTER_LONG_CONSTANT("DNS_NAPTR", PHP_DNS_NAPTR, CONST_CS | CONST_PERSISTENT);
1090    REGISTER_LONG_CONSTANT("DNS_AAAA",  PHP_DNS_AAAA,  CONST_CS | CONST_PERSISTENT);
1091    REGISTER_LONG_CONSTANT("DNS_A6",    PHP_DNS_A6,    CONST_CS | CONST_PERSISTENT);
1092    REGISTER_LONG_CONSTANT("DNS_ANY",   PHP_DNS_ANY,   CONST_CS | CONST_PERSISTENT);
1093    REGISTER_LONG_CONSTANT("DNS_ALL",   PHP_DNS_ALL,   CONST_CS | CONST_PERSISTENT);
1094    return SUCCESS;
1095}
1096#endif /* HAVE_FULL_DNS_FUNCS */
1097
1098/*
1099 * Local variables:
1100 * tab-width: 4
1101 * c-basic-offset: 4
1102 * End:
1103 * vim600: sw=4 ts=4 fdm=marker
1104 * vim<600: sw=4 ts=4
1105 */
1106