1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
+----------------------------------------------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2000 PHP Development Team (See Credits file) |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of one of the following licenses: |
| |
| A) the GNU General Public License as published by the Free Software |
| Foundation; either version 2 of the License, or (at your option) |
| any later version. |
| |
| B) the PHP License as published by the PHP Development Team and |
| included in the distribution in the file: LICENSE |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of both licenses referred to here. |
| If you did not, or have any questions about PHP licensing, please |
| contact core@php.net. |
+----------------------------------------------------------------------+
| Authors: |
| |
+----------------------------------------------------------------------+
*/
/* $Id: dns.c,v 1.59 2000/01/01 04:31:14 sas Exp $ */
#include "php.h"
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if WIN32|WINNT
#if HAVE_BINDLIB
#ifndef WINNT
#define WINNT 1
#endif
#include "arpa/inet.h"
#include "netdb.h"
#include "arpa/nameser.h"
#include "resolv.h"
#endif
#include <winsock.h>
#else
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef _OSD_POSIX
#undef STATUS
#undef T_UNSPEC
#endif
#include <arpa/nameser.h>
#include <resolv.h>
#endif
#include "internal_functions.h"
#include "dns.h"
char *_php3_gethostbyaddr(char *ip);
char *_php3_gethostbyname(char *name);
/* {{{ proto string gethostbyaddr(string ip_address)
Get the Internet host name corresponding to a given IP address */
void php3_gethostbyaddr(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg);
return_value->value.str.val = _php3_gethostbyaddr(arg->value.str.val);
return_value->value.str.len = strlen(return_value->value.str.val);
return_value->type = IS_STRING;
}
/* }}} */
char *_php3_gethostbyaddr(char *ip)
{
struct hostent *hp;
struct in_addr in;
if (!inet_aton(ip, &in)) {
#if DEBUG
php3_error(E_WARNING, "address not in a.b.c.d form");
#endif
return estrdup(ip);
}
hp = gethostbyaddr((char *) &in, sizeof(in), AF_INET);
if (!hp) {
#if DEBUG
php3_error(E_WARNING, "Unable to resolve %s\n", ip);
#endif
return estrdup(ip);
}
return estrdup(hp->h_name);
}
/* {{{ proto string gethostbyname(string hostname)
Get the IP address corresponding to a given Internet host name */
void php3_gethostbyname(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg);
return_value->value.str.val = _php3_gethostbyname(arg->value.str.val);
return_value->value.str.len = strlen(return_value->value.str.val);
return_value->type = IS_STRING;
}
/* }}} */
/* {{{ proto array gethostbynamel(string hostname)
Return a list of IP addresses that a given hostname resolves to. */
void php3_gethostbynamel(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
struct hostent *hp;
struct in_addr in;
int i;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg);
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
hp = gethostbyname(arg->value.str.val);
if (hp == NULL || hp->h_addr_list == NULL) {
#if DEBUG
php3_error(E_WARNING, "Unable to resolve %s\n", arg->value.str.val);
#endif
return;
}
for (i = 0 ; hp->h_addr_list[i] != 0 ; i++) {
memcpy(&in.s_addr, hp->h_addr_list[i], sizeof(in.s_addr));
add_next_index_string(return_value, inet_ntoa(in), 1);
}
return;
}
/* }}} */
char *_php3_gethostbyname(char *name)
{
struct hostent *hp;
struct in_addr in;
hp = gethostbyname(name);
if (!hp || !hp->h_addr_list) {
#if DEBUG
php3_error(E_WARNING, "Unable to resolve %s\n", name);
#endif
return estrdup(name);
}
memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr));
return estrdup(inet_ntoa(in));
}
#if !(WIN32|WINNT|OS2)||HAVE_BINDLIB
/* {{{ proto int checkdnsrr(string host [, string type])
Check DNS records corresponding to a given Internet host name or IP address */
void php3_checkdnsrr(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg1,*arg2;
int type,i;
#ifndef MAXPACKET
#define MAXPACKET 8192 /* max packet size used internally by BIND */
#endif
u_char ans[MAXPACKET];
TLS_VARS;
switch (ARG_COUNT(ht)) {
case 1:
if (getParameters(ht, 1, &arg1) == FAILURE) {
WRONG_PARAM_COUNT;
}
type = T_MX;
convert_to_string(arg1);
break;
case 2:
if (getParameters(ht, 2, &arg1, &arg2) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg1);
convert_to_string(arg2);
if ( !strcasecmp("A",arg2->value.str.val) ) type = T_A;
else if ( !strcasecmp("NS",arg2->value.str.val) ) type = T_NS;
else if ( !strcasecmp("MX",arg2->value.str.val) ) type = T_MX;
else if ( !strcasecmp("PTR",arg2->value.str.val) ) type = T_PTR;
else if ( !strcasecmp("ANY",arg2->value.str.val) ) type = T_ANY;
else if ( !strcasecmp("SOA",arg2->value.str.val) ) type = T_SOA;
else if ( !strcasecmp("CNAME",arg2->value.str.val) ) type = T_CNAME;
else {
php3_error(E_WARNING,"Type '%s' not supported",arg2->value.str.val);
RETURN_FALSE;
}
break;
default:
WRONG_PARAM_COUNT;
}
i = res_search(arg1->value.str.val,C_IN,type,ans,sizeof(ans));
if ( i < 0 ) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#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 */
/* {{{ proto int getmxrr(string hostname, array mxhosts [, array weight])
Get MX records corresponding to a given Internet host name */
void php3_getmxrr(INTERNAL_FUNCTION_PARAMETERS)
{
pval *host, *mx_list, *weight_list;
pval tmp1,tmp2;
int need_weight = 0;
int count,qdc;
u_short type,weight;
u_char ans[MAXPACKET];
char buf[MAXHOSTNAMELEN];
HEADER *hp;
u_char *cp,*end;
int i;
switch(ARG_COUNT(ht)) {
case 2:
if (getParameters(ht, 2, &host, &mx_list) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (!ParameterPassedByReference(ht, 2)) {
php3_error(E_WARNING, "Array to be filled with values must be passed by reference.");
RETURN_FALSE;
}
break;
case 3:
if (getParameters(ht, 3, &host, &mx_list, &weight_list) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (!ParameterPassedByReference(ht, 2) || !ParameterPassedByReference(ht, 3)) {
php3_error(E_WARNING, "Array to be filled with values must be passed by reference.");
RETURN_FALSE;
}
need_weight = 1;
pval_destructor(weight_list _INLINE_TLS); /* start with clean array */
if ( array_init(weight_list) == FAILURE ) {
RETURN_FALSE;
}
break;
default:
WRONG_PARAM_COUNT;
}
convert_to_string( host );
pval_destructor(mx_list _INLINE_TLS); /* start with clean array */
if ( array_init(mx_list) == FAILURE ) {
RETURN_FALSE;
}
/* Go! */
i = res_search(host->value.str.val,C_IN,T_MX,(u_char *)&ans,sizeof(ans));
if ( i < 0 ) {
RETURN_FALSE;
}
if ( i > sizeof(ans) ) i = sizeof(ans);
hp = (HEADER *)&ans;
cp = (u_char *)&ans + HFIXEDSZ;
end = (u_char *)&ans +i;
for ( qdc = ntohs((unsigned short)hp->qdcount); qdc--; cp += i + QFIXEDSZ) {
if ( (i = dn_skipname(cp,end)) < 0 ) {
RETURN_FALSE;
}
}
count = ntohs((unsigned short)hp->ancount);
while ( --count >= 0 && cp < end ) {
if ( (i = dn_skipname(cp,end)) < 0 ) {
RETURN_FALSE;
}
cp += i;
GETSHORT(type,cp);
cp += INT16SZ + INT32SZ;
GETSHORT(i,cp);
if ( type != T_MX ) {
cp += i;
continue;
}
GETSHORT(weight,cp);
if ( (i = dn_expand(ans,end,cp,buf,sizeof(buf)-1)) < 0 ) {
RETURN_FALSE;
}
cp += i;
tmp1.value.str.len = strlen(buf);
tmp1.value.str.val = estrndup(buf,tmp1.value.str.len);
tmp1.type = IS_STRING;
_php3_hash_next_index_insert(mx_list->value.ht, (void *)&tmp1, sizeof(pval), NULL);
if ( need_weight ) {
tmp2.value.lval = (long)weight;
tmp2.type = IS_LONG;
_php3_hash_next_index_insert(weight_list->value.ht, (void *)&tmp2, sizeof(pval), NULL);
}
}
RETURN_TRUE;
}
/* }}} */
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|