1999-04-17 00:37:12 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 15:32:16 +00:00
|
|
|
| PHP Version 4 |
|
1999-04-17 00:37:12 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2002-12-31 16:08:15 +00:00
|
|
|
| Copyright (c) 1997-2003 The PHP Group |
|
1999-04-17 00:37:12 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2000-05-18 15:34:45 +00:00
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
1999-07-16 13:13:16 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
2000-05-18 15:34:45 +00:00
|
|
|
| http://www.php.net/license/2_02.txt. |
|
1999-07-16 13:13:16 +00:00
|
|
|
| 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. |
|
1999-04-17 00:37:12 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2002-11-19 02:34:13 +00:00
|
|
|
| Authors: The typical suspects |
|
|
|
|
| Pollita <pollita@php.net> |
|
|
|
|
| Marcus Boerger <helly@php.net> |
|
1999-04-17 00:37:12 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2002-11-19 02:34:13 +00:00
|
|
|
/* {{{ includes */
|
1999-04-17 00:37:12 +00:00
|
|
|
#include "php.h"
|
2002-10-18 22:08:23 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
#if HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
2002-10-18 22:08:23 +00:00
|
|
|
|
2000-02-11 15:59:30 +00:00
|
|
|
#ifdef PHP_WIN32
|
2001-03-11 03:49:21 +00:00
|
|
|
#if HAVE_LIBBIND
|
1999-04-17 00:37:12 +00:00
|
|
|
#ifndef WINNT
|
|
|
|
#define WINNT 1
|
|
|
|
#endif
|
|
|
|
/* located in www.php.net/extra/bindlib.zip */
|
2002-10-18 22:08:23 +00:00
|
|
|
#if HAVE_ARPA_INET_H
|
1999-04-17 00:37:12 +00:00
|
|
|
#include "arpa/inet.h"
|
2001-01-21 17:29:15 +00:00
|
|
|
#endif
|
1999-04-17 00:37:12 +00:00
|
|
|
#include "netdb.h"
|
2001-01-21 17:29:15 +00:00
|
|
|
#if HAVE_ARPA_NAMESERV_H
|
1999-04-17 00:37:12 +00:00
|
|
|
#include "arpa/nameser.h"
|
2001-01-21 17:29:15 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_RESOLV_H
|
1999-04-17 00:37:12 +00:00
|
|
|
#include "resolv.h"
|
|
|
|
#endif
|
2002-10-18 22:08:23 +00:00
|
|
|
#endif /* HAVE_LIBBIND */
|
1999-04-17 00:37:12 +00:00
|
|
|
#include <winsock.h>
|
2002-09-05 14:25:07 +00:00
|
|
|
#else /* This holds good for NetWare too, both for Winsock and Berkeley sockets */
|
1999-04-17 00:37:12 +00:00
|
|
|
#include <netinet/in.h>
|
2001-02-06 00:07:21 +00:00
|
|
|
#if HAVE_ARPA_INET_H
|
1999-04-17 00:37:12 +00:00
|
|
|
#include <arpa/inet.h>
|
2001-02-06 00:07:21 +00:00
|
|
|
#endif
|
1999-04-17 00:37:12 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#ifdef _OSD_POSIX
|
|
|
|
#undef STATUS
|
|
|
|
#undef T_UNSPEC
|
|
|
|
#endif
|
2001-02-06 00:07:21 +00:00
|
|
|
#if HAVE_ARPA_NAMESER_H
|
1999-04-17 00:37:12 +00:00
|
|
|
#include <arpa/nameser.h>
|
2001-02-06 00:07:21 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_RESOLV_H
|
1999-04-17 00:37:12 +00:00
|
|
|
#include <resolv.h>
|
|
|
|
#endif
|
2001-02-06 00:07:21 +00:00
|
|
|
#endif
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2002-09-05 14:25:07 +00:00
|
|
|
/* Borrowed from SYS/SOCKET.H */
|
|
|
|
#if defined(NETWARE) && defined(USE_WINSOCK)
|
|
|
|
#define AF_INET 2 /* internetwork: UDP, TCP, etc. */
|
|
|
|
#endif
|
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
#include "dns.h"
|
2001-06-06 13:06:12 +00:00
|
|
|
/* }}} */
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2001-08-10 02:19:05 +00:00
|
|
|
static char *php_gethostbyaddr(char *ip);
|
|
|
|
static char *php_gethostbyname(char *name);
|
1999-04-17 00:37:12 +00:00
|
|
|
|
|
|
|
/* {{{ proto string gethostbyaddr(string ip_address)
|
|
|
|
Get the Internet host name corresponding to a given IP address */
|
1999-05-16 11:19:26 +00:00
|
|
|
PHP_FUNCTION(gethostbyaddr)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
2001-08-10 02:19:05 +00:00
|
|
|
zval **arg;
|
|
|
|
char *addr;
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2000-06-05 19:47:54 +00:00
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
2001-08-10 02:19:05 +00:00
|
|
|
ZEND_WRONG_PARAM_COUNT();
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
2001-08-10 02:19:05 +00:00
|
|
|
|
1999-09-25 12:09:50 +00:00
|
|
|
convert_to_string_ex(arg);
|
2001-08-10 02:19:05 +00:00
|
|
|
|
|
|
|
addr = php_gethostbyaddr(Z_STRVAL_PP(arg));
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2001-08-10 02:19:05 +00:00
|
|
|
if(addr == NULL) {
|
2001-12-27 15:42:05 +00:00
|
|
|
#if HAVE_IPV6 && !defined(__MacOSX__)
|
|
|
|
/* MacOSX at this time has support for IPv6, but not inet_pton()
|
|
|
|
* so disabling IPv6 until further notice. MacOSX 10.1.2 (kalowsky) */
|
2002-08-24 01:19:28 +00:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Address is not a valid IPv4 or IPv6 address");
|
2001-09-19 18:08:15 +00:00
|
|
|
#else
|
2002-08-24 01:19:28 +00:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Address is not in a.b.c.d form");
|
2001-09-19 18:08:15 +00:00
|
|
|
#endif
|
2001-08-10 02:19:05 +00:00
|
|
|
RETVAL_FALSE;
|
|
|
|
} else {
|
|
|
|
RETVAL_STRING(addr, 0);
|
|
|
|
}
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-11-19 02:34:13 +00:00
|
|
|
/* {{{ php_gethostbyaddr */
|
2001-08-10 02:19:05 +00:00
|
|
|
static char *php_gethostbyaddr(char *ip)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
2001-12-27 15:42:05 +00:00
|
|
|
#if HAVE_IPV6 && !defined(__MacOSX__)
|
|
|
|
/* MacOSX at this time has support for IPv6, but not inet_pton()
|
|
|
|
* so disabling IPv6 until further notice. MacOSX 10.1.2 (kalowsky) */
|
2001-09-19 18:08:15 +00:00
|
|
|
struct in6_addr addr6;
|
|
|
|
#endif
|
2000-05-17 16:08:42 +00:00
|
|
|
struct in_addr addr;
|
1999-04-17 00:37:12 +00:00
|
|
|
struct hostent *hp;
|
|
|
|
|
2001-12-27 15:42:05 +00:00
|
|
|
#if HAVE_IPV6 && !defined(__MacOSX__)
|
|
|
|
/* MacOSX at this time has support for IPv6, but not inet_pton()
|
|
|
|
* so disabling IPv6 until further notice. MacOSX 10.1.2 (kalowsky) */
|
2001-09-19 18:08:15 +00:00
|
|
|
if (inet_pton(AF_INET6, ip, &addr6)) {
|
|
|
|
hp = gethostbyaddr((char *) &addr6, sizeof(addr6), AF_INET6);
|
|
|
|
} else if (inet_pton(AF_INET, ip, &addr)) {
|
|
|
|
hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else
|
2000-05-17 16:08:42 +00:00
|
|
|
addr.s_addr = inet_addr(ip);
|
2001-08-10 02:19:05 +00:00
|
|
|
|
2000-05-17 16:08:42 +00:00
|
|
|
if (addr.s_addr == -1) {
|
2001-08-10 02:19:05 +00:00
|
|
|
return NULL;
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
2001-08-10 02:19:05 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
|
2001-09-19 18:08:15 +00:00
|
|
|
#endif
|
2001-08-10 02:19:05 +00:00
|
|
|
|
2002-08-29 15:58:33 +00:00
|
|
|
if (!hp || hp->h_name == NULL || hp->h_name[0] == '\0') {
|
1999-04-17 00:37:12 +00:00
|
|
|
return estrdup(ip);
|
|
|
|
}
|
2001-08-10 02:19:05 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
return estrdup(hp->h_name);
|
|
|
|
}
|
2001-06-06 13:06:12 +00:00
|
|
|
/* }}} */
|
1999-04-17 00:37:12 +00:00
|
|
|
|
|
|
|
/* {{{ proto string gethostbyname(string hostname)
|
|
|
|
Get the IP address corresponding to a given Internet host name */
|
1999-05-16 11:19:26 +00:00
|
|
|
PHP_FUNCTION(gethostbyname)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
2001-08-10 02:19:05 +00:00
|
|
|
zval **arg;
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2000-06-05 19:47:54 +00:00
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
2001-08-10 02:19:05 +00:00
|
|
|
ZEND_WRONG_PARAM_COUNT();
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
2001-08-10 02:19:05 +00:00
|
|
|
|
1999-09-25 12:09:50 +00:00
|
|
|
convert_to_string_ex(arg);
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2000-11-27 13:31:21 +00:00
|
|
|
RETVAL_STRING(php_gethostbyname(Z_STRVAL_PP(arg)), 0);
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto array gethostbynamel(string hostname)
|
|
|
|
Return a list of IP addresses that a given hostname resolves to. */
|
1999-05-16 11:19:26 +00:00
|
|
|
PHP_FUNCTION(gethostbynamel)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
2001-08-10 02:19:05 +00:00
|
|
|
zval **arg;
|
1999-04-17 00:37:12 +00:00
|
|
|
struct hostent *hp;
|
|
|
|
struct in_addr in;
|
|
|
|
int i;
|
|
|
|
|
2000-06-05 19:47:54 +00:00
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
2001-08-10 02:19:05 +00:00
|
|
|
ZEND_WRONG_PARAM_COUNT();
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
1999-09-25 12:09:50 +00:00
|
|
|
convert_to_string_ex(arg);
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2002-12-05 22:28:02 +00:00
|
|
|
array_init(return_value);
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2000-11-27 13:31:21 +00:00
|
|
|
hp = gethostbyname(Z_STRVAL_PP(arg));
|
1999-04-17 00:37:12 +00:00
|
|
|
if (hp == NULL || hp->h_addr_list == NULL) {
|
2001-08-10 02:19:05 +00:00
|
|
|
RETURN_FALSE;
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0 ; hp->h_addr_list[i] != 0 ; i++) {
|
1999-09-25 12:09:50 +00:00
|
|
|
in = *(struct in_addr *) hp->h_addr_list[i];
|
1999-04-17 00:37:12 +00:00
|
|
|
add_next_index_string(return_value, inet_ntoa(in), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-11-19 02:34:13 +00:00
|
|
|
/* {{{ php_gethostbyname */
|
2001-08-10 02:19:05 +00:00
|
|
|
static char *php_gethostbyname(char *name)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
|
|
|
struct hostent *hp;
|
|
|
|
struct in_addr in;
|
|
|
|
|
|
|
|
hp = gethostbyname(name);
|
2001-08-10 02:19:05 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
if (!hp || !hp->h_addr_list) {
|
|
|
|
return estrdup(name);
|
|
|
|
}
|
2001-08-10 02:19:05 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr));
|
2001-08-10 02:19:05 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
return estrdup(inet_ntoa(in));
|
|
|
|
}
|
2001-06-06 13:06:12 +00:00
|
|
|
/* }}} */
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2002-09-05 14:25:07 +00:00
|
|
|
#if HAVE_RES_SEARCH && !(defined(__BEOS__)||defined(PHP_WIN32) || defined(NETWARE))
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2002-11-19 02:34:13 +00:00
|
|
|
/* {{{ proto int dns_check_recored(string host [, string type])
|
1999-04-17 00:37:12 +00:00
|
|
|
Check DNS records corresponding to a given Internet host name or IP address */
|
2002-11-19 02:34:13 +00:00
|
|
|
PHP_FUNCTION(dns_check_record)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
2001-08-11 17:03:37 +00:00
|
|
|
zval **arg1, **arg2;
|
|
|
|
int type, i;
|
1999-04-17 00:37:12 +00:00
|
|
|
#ifndef MAXPACKET
|
|
|
|
#define MAXPACKET 8192 /* max packet size used internally by BIND */
|
|
|
|
#endif
|
|
|
|
u_char ans[MAXPACKET];
|
|
|
|
|
2000-06-05 19:47:54 +00:00
|
|
|
switch (ZEND_NUM_ARGS()) {
|
2002-10-18 22:08:23 +00:00
|
|
|
case 1:
|
|
|
|
if (zend_get_parameters_ex(1, &arg1) == FAILURE) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
type = T_MX;
|
|
|
|
convert_to_string_ex(arg1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
if (zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
convert_to_string_ex(arg1);
|
|
|
|
convert_to_string_ex(arg2);
|
|
|
|
|
|
|
|
if (!strcasecmp("A", Z_STRVAL_PP(arg2))) type = T_A;
|
|
|
|
else if (!strcasecmp("NS", Z_STRVAL_PP(arg2))) type = T_NS;
|
|
|
|
else if (!strcasecmp("MX", Z_STRVAL_PP(arg2))) type = T_MX;
|
|
|
|
else if (!strcasecmp("PTR", Z_STRVAL_PP(arg2))) type = T_PTR;
|
|
|
|
else if (!strcasecmp("ANY", Z_STRVAL_PP(arg2))) type = T_ANY;
|
|
|
|
else if (!strcasecmp("SOA", Z_STRVAL_PP(arg2))) type = T_SOA;
|
|
|
|
else if (!strcasecmp("CNAME", Z_STRVAL_PP(arg2))) type = T_CNAME;
|
2002-12-09 11:50:15 +00:00
|
|
|
#ifdef T_AAAA
|
2002-12-08 08:14:43 +00:00
|
|
|
else if (!strcasecmp("AAAA", Z_STRVAL_PP(arg2))) type = T_AAAA;
|
2002-12-09 11:50:15 +00:00
|
|
|
#endif
|
2002-10-18 22:08:23 +00:00
|
|
|
else {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%s' not supported", Z_STRVAL_PP(arg2));
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-04-17 00:37:12 +00:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2002-10-18 22:08:23 +00:00
|
|
|
|
2001-08-11 17:03:37 +00:00
|
|
|
i = res_search(Z_STRVAL_PP(arg1), C_IN, type, ans, sizeof(ans));
|
2002-10-18 22:08:23 +00:00
|
|
|
|
|
|
|
if (i < 0) {
|
1999-04-17 00:37:12 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2002-10-18 22:08:23 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-11-19 02:34:13 +00:00
|
|
|
#if HAVE_DNS_FUNCS
|
|
|
|
|
|
|
|
/* PHP_DNS_xx = 1<<(T_xx-1) */
|
|
|
|
#define PHP_DNS_A 0x00000001
|
|
|
|
#define PHP_DNS_NS 0x00000002
|
2002-12-20 18:34:56 +00:00
|
|
|
#define PHP_DNS_CNAME 0x00000010
|
|
|
|
#define PHP_DNS_SOA 0x00000020
|
2002-11-19 02:34:13 +00:00
|
|
|
#define PHP_DNS_PTR 0x00000800
|
|
|
|
#define PHP_DNS_HINFO 0x00001000
|
|
|
|
#define PHP_DNS_MX 0x00004000
|
|
|
|
#define PHP_DNS_TXT 0x00008000
|
2002-12-08 08:14:43 +00:00
|
|
|
#define PHP_DNS_AAAA 0x08000000
|
2002-11-19 02:34:13 +00:00
|
|
|
#define PHP_DNS_ANY 0x10000000
|
2002-12-08 08:14:43 +00:00
|
|
|
#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_AAAA)
|
2002-11-19 02:34:13 +00:00
|
|
|
|
|
|
|
PHP_MINIT_FUNCTION(dns) {
|
|
|
|
REGISTER_LONG_CONSTANT("DNS_A", PHP_DNS_A, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("DNS_NS", PHP_DNS_NS, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("DNS_CNAME", PHP_DNS_CNAME, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("DNS_SOA", PHP_DNS_SOA, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("DNS_PTR", PHP_DNS_PTR, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("DNS_HINFO", PHP_DNS_HINFO, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("DNS_MX", PHP_DNS_MX, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("DNS_TXT", PHP_DNS_TXT, CONST_CS | CONST_PERSISTENT);
|
2002-12-08 08:14:43 +00:00
|
|
|
REGISTER_LONG_CONSTANT("DNS_AAAA", PHP_DNS_AAAA, CONST_CS | CONST_PERSISTENT);
|
2002-11-19 02:34:13 +00:00
|
|
|
REGISTER_LONG_CONSTANT("DNS_ANY", PHP_DNS_ANY, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("DNS_ALL", PHP_DNS_ALL, CONST_CS | CONST_PERSISTENT);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
#ifndef HFIXEDSZ
|
|
|
|
#define HFIXEDSZ 12 /* fixed data in header <arpa/nameser.h> */
|
|
|
|
#endif /* HFIXEDSZ */
|
|
|
|
|
|
|
|
#ifndef QFIXEDSZ
|
|
|
|
#define QFIXEDSZ 4 /* fixed data in query <arpa/nameser.h> */
|
|
|
|
#endif /* QFIXEDSZ */
|
|
|
|
|
|
|
|
#ifndef MAXHOSTNAMELEN
|
|
|
|
#define MAXHOSTNAMELEN 256
|
|
|
|
#endif /* MAXHOSTNAMELEN */
|
|
|
|
|
2002-11-19 02:34:13 +00:00
|
|
|
#ifndef MAXRESOURCERECORDS
|
|
|
|
#define MAXRESOURCERECORDS 64
|
|
|
|
#endif /* MAXRESOURCERECORDS */
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
HEADER qb1;
|
|
|
|
u_char qb2[65536];
|
|
|
|
} querybuf;
|
|
|
|
|
|
|
|
/* {{{ php_parserr */
|
|
|
|
static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int store, zval **subarray) {
|
|
|
|
u_short type, class, dlen;
|
|
|
|
u_long ttl;
|
|
|
|
long n, i;
|
2002-12-08 08:14:43 +00:00
|
|
|
u_short s;
|
|
|
|
u_char *tp;
|
2002-11-19 02:34:13 +00:00
|
|
|
char name[MAXHOSTNAMELEN];
|
|
|
|
|
|
|
|
n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof(name)) - 2);
|
|
|
|
if (n < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cp += n;
|
|
|
|
|
|
|
|
GETSHORT(type, cp);
|
|
|
|
GETSHORT(class, cp);
|
|
|
|
GETLONG(ttl, cp);
|
|
|
|
GETSHORT(dlen, cp);
|
|
|
|
if (type_to_fetch != T_ANY && type != type_to_fetch) {
|
2002-11-23 01:09:07 +00:00
|
|
|
*subarray = NULL;
|
|
|
|
cp += dlen;
|
|
|
|
return cp;
|
2002-11-19 02:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!store) {
|
|
|
|
*subarray = NULL;
|
|
|
|
cp += dlen;
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(*subarray);
|
2002-12-05 22:28:02 +00:00
|
|
|
array_init(*subarray);
|
2002-11-19 02:34:13 +00:00
|
|
|
|
|
|
|
add_assoc_string(*subarray, "host", name, 1);
|
|
|
|
switch (type) {
|
|
|
|
case T_A:
|
|
|
|
add_assoc_string(*subarray, "type", "A", 1);
|
|
|
|
sprintf(name, "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
|
|
|
|
add_assoc_string(*subarray, "ip", name, 1);
|
|
|
|
cp += dlen;
|
|
|
|
break;
|
|
|
|
case T_MX:
|
|
|
|
add_assoc_string(*subarray, "type", "MX", 1);
|
|
|
|
GETSHORT(n, cp);
|
|
|
|
add_assoc_long(*subarray, "pri", n);
|
|
|
|
/* no break; */
|
|
|
|
case T_CNAME:
|
|
|
|
if (type == T_CNAME)
|
|
|
|
add_assoc_string(*subarray, "type", "CNAME", 1);
|
|
|
|
/* no break; */
|
|
|
|
case T_NS:
|
|
|
|
if (type == T_NS)
|
|
|
|
add_assoc_string(*subarray, "type", "NS", 1);
|
|
|
|
/* no break; */
|
|
|
|
case T_PTR:
|
|
|
|
if (type == T_PTR)
|
|
|
|
add_assoc_string(*subarray, "type", "PTR", 1);
|
|
|
|
n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
|
|
|
|
if (n < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cp += n;
|
|
|
|
add_assoc_string(*subarray, "target", name, 1);
|
|
|
|
break;
|
|
|
|
case T_HINFO:
|
|
|
|
/* See RFC 1010 for values */
|
|
|
|
add_assoc_string(*subarray, "type", "HINFO", 1);
|
|
|
|
GETSHORT(n, cp);
|
|
|
|
add_assoc_long(*subarray, "cpu", n);
|
|
|
|
GETSHORT(n, cp);
|
|
|
|
add_assoc_long(*subarray, "os", n);
|
|
|
|
break;
|
|
|
|
case T_TXT:
|
|
|
|
add_assoc_string(*subarray, "type", "TXT", 1);
|
|
|
|
n = cp[0];
|
|
|
|
for(i=1; i<=n; i++)
|
|
|
|
name[i-1] = cp[i];
|
|
|
|
name[i-1] = '\0';
|
|
|
|
cp += dlen;
|
|
|
|
add_assoc_string(*subarray, "txt", name, 1);
|
|
|
|
break;
|
|
|
|
case T_SOA:
|
|
|
|
add_assoc_string(*subarray, "type", "SOA", 1);
|
|
|
|
n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) -2);
|
|
|
|
if (n < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cp += n;
|
|
|
|
add_assoc_string(*subarray, "mname", name, 1);
|
|
|
|
n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) -2);
|
|
|
|
if (n < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cp += n;
|
|
|
|
add_assoc_string(*subarray, "rname", name, 1);
|
|
|
|
GETLONG(n, cp);
|
|
|
|
add_assoc_long(*subarray, "serial", n);
|
|
|
|
GETLONG(n, cp);
|
|
|
|
add_assoc_long(*subarray, "refresh", n);
|
|
|
|
GETLONG(n, cp);
|
|
|
|
add_assoc_long(*subarray, "retry", n);
|
|
|
|
GETLONG(n, cp);
|
|
|
|
add_assoc_long(*subarray, "expire", n);
|
|
|
|
GETLONG(n, cp);
|
|
|
|
add_assoc_long(*subarray, "minimum-ttl", n);
|
|
|
|
break;
|
2002-12-09 11:50:15 +00:00
|
|
|
|
|
|
|
#ifdef T_AAAA
|
2002-12-08 08:14:43 +00:00
|
|
|
case T_AAAA:
|
|
|
|
tp = name;
|
|
|
|
for(i=0; i < 8; i++) {
|
|
|
|
GETSHORT(s, cp);
|
|
|
|
if (s > 0) {
|
|
|
|
if (tp > (u_char *)name) {
|
|
|
|
tp[0] = ':';
|
|
|
|
tp++;
|
|
|
|
}
|
|
|
|
sprintf(tp,"%x",s);
|
|
|
|
tp += strlen(tp);
|
|
|
|
} else if (s == 0) {
|
|
|
|
if ((tp > (u_char *)name) && (tp[-1] != ':')) {
|
|
|
|
tp[0] = ':';
|
|
|
|
tp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((tp > (u_char *)name) && (tp[-1] == ':'))
|
|
|
|
tp[-1] = '\0';
|
|
|
|
tp[0] = '\0';
|
|
|
|
add_assoc_string(*subarray, "type", "AAAA", 1);
|
|
|
|
add_assoc_string(*subarray, "ipv6", name, 1);
|
|
|
|
break;
|
2002-12-09 11:50:15 +00:00
|
|
|
#endif
|
|
|
|
|
2002-11-19 02:34:13 +00:00
|
|
|
default:
|
|
|
|
cp += dlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_assoc_string(*subarray, "class", "IN", 1);
|
|
|
|
add_assoc_long(*subarray, "ttl", ttl);
|
|
|
|
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto array|false dns_get_record(string hostname [, int type[, array authns, array addtl]])
|
|
|
|
Get any Resource Record corresponding to a given Internet host name */
|
|
|
|
PHP_FUNCTION(dns_get_record)
|
|
|
|
{
|
|
|
|
zval *subarray[MAXRESOURCERECORDS];
|
|
|
|
pval *addtl, *host, *authns, *fetch_type;
|
|
|
|
int addtl_recs = 0;
|
|
|
|
int type_to_fetch, type_param = PHP_DNS_ANY;
|
|
|
|
int current_subarray = 0;
|
|
|
|
struct __res_state res;
|
|
|
|
HEADER *hp;
|
|
|
|
querybuf buf, answer, *ans;
|
|
|
|
u_char *cp = NULL, *end = NULL;
|
|
|
|
long n, qd, an, ns = 0, ar = 0;
|
|
|
|
int type, first_query = 1, store_results = 1;
|
|
|
|
|
|
|
|
switch (ZEND_NUM_ARGS()) {
|
|
|
|
case 1:
|
|
|
|
if (zend_get_parameters(ht, 1, &host) == FAILURE) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (zend_get_parameters(ht, 2, &host, &fetch_type) == FAILURE) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
type_param = Z_LVAL_P(fetch_type);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
if (zend_get_parameters(ht, 4, &host, &fetch_type, &authns, &addtl) == FAILURE) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
type_param = Z_LVAL_P(fetch_type);
|
|
|
|
pval_destructor(authns);
|
|
|
|
addtl_recs = 1; /* We want the additional Records */
|
2002-12-05 22:28:02 +00:00
|
|
|
array_init(authns);
|
2002-11-19 02:34:13 +00:00
|
|
|
pval_destructor(addtl);
|
2002-12-05 22:28:02 +00:00
|
|
|
array_init(addtl);
|
2002-11-19 02:34:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
convert_to_string(host);
|
|
|
|
|
|
|
|
if (type_param&~PHP_DNS_ALL && type_param!=PHP_DNS_ANY) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%d' not supported", type_param);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the return array */
|
2002-12-05 22:28:02 +00:00
|
|
|
array_init(return_value);
|
2002-11-19 02:34:13 +00:00
|
|
|
|
2002-12-08 08:14:43 +00:00
|
|
|
/* - We emulate an or'ed type mask by querying type by type. (Steps 0 - 8)
|
|
|
|
* If additional info is wanted we check again with T_ANY (step 9/10)
|
2002-11-19 02:34:13 +00:00
|
|
|
* store_results is used to skip storing the results retrieved in step
|
2002-12-08 08:14:43 +00:00
|
|
|
* 10 when results were already fetched.
|
|
|
|
* - In case of PHP_DNS_ANY we use the directly fetch T_ANY. (step 10)
|
2002-11-19 02:34:13 +00:00
|
|
|
*/
|
2002-12-08 08:14:43 +00:00
|
|
|
for(type = (type_param==PHP_DNS_ANY ? 10 : 0); type < (addtl_recs ? 11 : 9) || first_query; type++)
|
2002-11-19 02:34:13 +00:00
|
|
|
{
|
|
|
|
first_query = 0;
|
|
|
|
switch(type) {
|
|
|
|
case 0:
|
|
|
|
type_to_fetch = type_param&PHP_DNS_A ? T_A : 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
type_to_fetch = type_param&PHP_DNS_NS ? T_NS : 0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
type_to_fetch = type_param&PHP_DNS_CNAME ? T_CNAME : 0;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
type_to_fetch = type_param&PHP_DNS_SOA ? T_SOA : 0;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
type_to_fetch = type_param&PHP_DNS_PTR ? T_PTR : 0;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
type_to_fetch = type_param&PHP_DNS_HINFO ? T_HINFO : 0;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
type_to_fetch = type_param&PHP_DNS_MX ? T_MX : 0;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
type_to_fetch = type_param&PHP_DNS_TXT ? T_TXT : 0;
|
|
|
|
break;
|
|
|
|
case 8:
|
2002-12-30 11:59:22 +00:00
|
|
|
#ifdef T_AAAA
|
2002-12-08 08:14:43 +00:00
|
|
|
type_to_fetch = type_param&PHP_DNS_AAAA ? T_AAAA : 0;
|
|
|
|
break;
|
2002-12-30 11:59:22 +00:00
|
|
|
#else
|
|
|
|
continue;
|
2002-12-09 11:50:15 +00:00
|
|
|
#endif
|
2002-12-08 08:14:43 +00:00
|
|
|
case 9:
|
2002-11-19 02:34:13 +00:00
|
|
|
store_results = 0;
|
|
|
|
continue;
|
|
|
|
default:
|
2002-12-08 08:14:43 +00:00
|
|
|
case 10:
|
2002-11-19 02:34:13 +00:00
|
|
|
type_to_fetch = T_ANY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (type_to_fetch) {
|
|
|
|
res_ninit(&res);
|
|
|
|
res.retrans = 5;
|
|
|
|
res.options &= ~RES_DEFNAMES;
|
|
|
|
|
|
|
|
n = res_nmkquery(&res, QUERY, Z_STRVAL_P(host), C_IN, type_to_fetch, NULL, 0, NULL, buf.qb2, sizeof buf);
|
|
|
|
if (n<0) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "res_nmkquery() failed");
|
2002-12-08 09:54:30 +00:00
|
|
|
zval_dtor(return_value);
|
2002-11-19 02:34:13 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
n = res_nsend(&res, buf.qb2, n, answer.qb2, sizeof answer);
|
|
|
|
if (n<0) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "res_nsend() failed");
|
2002-12-08 09:54:30 +00:00
|
|
|
zval_dtor(return_value);
|
2002-11-19 02:34:13 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp = answer.qb2 + HFIXEDSZ;
|
|
|
|
end = answer.qb2 + n;
|
|
|
|
ans = &answer;
|
|
|
|
hp = (HEADER *)ans;
|
|
|
|
qd = ntohs(hp->qdcount);
|
|
|
|
an = ntohs(hp->ancount);
|
|
|
|
ns = ntohs(hp->nscount);
|
|
|
|
ar = ntohs(hp->arcount);
|
|
|
|
|
|
|
|
/* Skip QD entries, they're only used by dn_expand later on */
|
|
|
|
while (qd-- > 0) {
|
|
|
|
n = dn_skipname(cp, end);
|
|
|
|
if (n < 0) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to parse DNS data received");
|
2002-12-08 09:54:30 +00:00
|
|
|
zval_dtor(return_value);
|
2002-11-19 02:34:13 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
cp += n + QFIXEDSZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* YAY! Our real answers! */
|
|
|
|
while (an-- && cp && cp < end) {
|
|
|
|
cp = php_parserr(cp, &answer, type_to_fetch, store_results, &subarray[current_subarray]);
|
|
|
|
if (subarray[current_subarray] != NULL && store_results)
|
|
|
|
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&subarray[current_subarray], sizeof(zval *), NULL);
|
|
|
|
current_subarray++;
|
|
|
|
}
|
2002-11-23 01:09:07 +00:00
|
|
|
res_nclose(&res);
|
2002-11-19 02:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addtl_recs) {
|
|
|
|
/* List of Authoritative Name Servers */
|
|
|
|
while (ns-- > 0 && cp && cp < end) {
|
|
|
|
cp = php_parserr(cp, &answer, T_ANY, 1, &subarray[current_subarray]);
|
|
|
|
if (subarray[current_subarray] != NULL)
|
|
|
|
zend_hash_next_index_insert(HASH_OF(authns), (void *)&subarray[current_subarray], sizeof(zval *), NULL);
|
|
|
|
current_subarray++;
|
|
|
|
}
|
|
|
|
/* Additional records associated with authoritative name servers */
|
|
|
|
while (ar-- > 0 && cp && cp < end) {
|
|
|
|
cp = php_parserr(cp, &answer, T_ANY, 1, &subarray[current_subarray]);
|
|
|
|
if (subarray[current_subarray] != NULL)
|
|
|
|
zend_hash_next_index_insert(HASH_OF(addtl), (void *)&subarray[current_subarray], sizeof(zval *), NULL);
|
|
|
|
current_subarray++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto bool dns_get_mx(string hostname, array mxhosts [, array weight])
|
1999-04-17 00:37:12 +00:00
|
|
|
Get MX records corresponding to a given Internet host name */
|
2002-11-19 02:34:13 +00:00
|
|
|
PHP_FUNCTION(dns_get_mx)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
|
|
|
pval *host, *mx_list, *weight_list;
|
|
|
|
int need_weight = 0;
|
2001-08-11 17:03:37 +00:00
|
|
|
int count, qdc;
|
|
|
|
u_short type, weight;
|
1999-04-17 00:37:12 +00:00
|
|
|
u_char ans[MAXPACKET];
|
|
|
|
char buf[MAXHOSTNAMELEN];
|
|
|
|
HEADER *hp;
|
2001-08-11 17:03:37 +00:00
|
|
|
u_char *cp, *end;
|
1999-04-17 00:37:12 +00:00
|
|
|
int i;
|
|
|
|
|
2000-06-05 19:47:54 +00:00
|
|
|
switch(ZEND_NUM_ARGS()) {
|
2002-10-18 22:08:23 +00:00
|
|
|
case 2:
|
|
|
|
if (zend_get_parameters(ht, 2, &host, &mx_list) == FAILURE) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
if (zend_get_parameters(ht, 3, &host, &mx_list, &weight_list) == FAILURE) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
need_weight = 1;
|
|
|
|
pval_destructor(weight_list); /* start with clean array */
|
2002-12-05 22:28:02 +00:00
|
|
|
array_init(weight_list);
|
2002-10-18 22:08:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-04-17 00:37:12 +00:00
|
|
|
WRONG_PARAM_COUNT;
|
2002-10-18 22:08:23 +00:00
|
|
|
}
|
1999-04-17 00:37:12 +00:00
|
|
|
|
2002-10-18 22:08:23 +00:00
|
|
|
convert_to_string(host);
|
|
|
|
pval_destructor(mx_list); /* start with clean array */
|
2002-12-05 22:28:02 +00:00
|
|
|
array_init(mx_list);
|
1999-04-17 00:37:12 +00:00
|
|
|
|
|
|
|
/* Go! */
|
2001-08-11 17:03:37 +00:00
|
|
|
i = res_search(Z_STRVAL_P(host), C_IN, T_MX, (u_char *)&ans, sizeof(ans));
|
2002-10-18 22:08:23 +00:00
|
|
|
if (i < 0) {
|
1999-04-17 00:37:12 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2002-10-18 22:08:23 +00:00
|
|
|
if (i > sizeof(ans)) {
|
|
|
|
i = sizeof(ans);
|
|
|
|
}
|
1999-04-17 00:37:12 +00:00
|
|
|
hp = (HEADER *)&ans;
|
|
|
|
cp = (u_char *)&ans + HFIXEDSZ;
|
|
|
|
end = (u_char *)&ans +i;
|
2002-10-18 22:08:23 +00:00
|
|
|
for (qdc = ntohs((unsigned short)hp->qdcount); qdc--; cp += i + QFIXEDSZ) {
|
|
|
|
if ((i = dn_skipname(cp, end)) < 0 ) {
|
1999-04-17 00:37:12 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count = ntohs((unsigned short)hp->ancount);
|
2002-10-18 22:08:23 +00:00
|
|
|
while (--count >= 0 && cp < end) {
|
|
|
|
if ((i = dn_skipname(cp, end)) < 0 ) {
|
1999-04-17 00:37:12 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
cp += i;
|
2001-08-11 17:03:37 +00:00
|
|
|
GETSHORT(type, cp);
|
1999-04-17 00:37:12 +00:00
|
|
|
cp += INT16SZ + INT32SZ;
|
2001-08-11 17:03:37 +00:00
|
|
|
GETSHORT(i, cp);
|
2002-10-18 22:08:23 +00:00
|
|
|
if (type != T_MX) {
|
1999-04-17 00:37:12 +00:00
|
|
|
cp += i;
|
|
|
|
continue;
|
|
|
|
}
|
2001-08-11 17:03:37 +00:00
|
|
|
GETSHORT(weight, cp);
|
2002-10-18 22:08:23 +00:00
|
|
|
if ((i = dn_expand(ans, end, cp, buf, sizeof(buf)-1)) < 0) {
|
1999-04-17 00:37:12 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
cp += i;
|
1999-05-23 20:32:19 +00:00
|
|
|
add_next_index_string(mx_list, buf, 1);
|
2002-10-18 22:08:23 +00:00
|
|
|
if (need_weight) {
|
1999-05-23 20:32:19 +00:00
|
|
|
add_next_index_long(weight_list, weight);
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-11-19 02:34:13 +00:00
|
|
|
#endif /* HAVE_DNS_FUNCS */
|
|
|
|
#endif /* HAVE_RES_SEARCH && !(defined(__BEOS__)||defined(PHP_WIN32) || defined(NETWARE)) */
|
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2001-09-09 13:29:31 +00:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
1999-04-17 00:37:12 +00:00
|
|
|
*/
|