Skip to content

Commit 52e3b4a

Browse files
author
foobar
committed
Cleanup. Also make gethostnamel() to return FALSE on error and not just empty array.
1 parent 9e9e47f commit 52e3b4a

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

ext/standard/dns.c

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,45 +63,52 @@
6363
#include "dns.h"
6464
/* }}} */
6565

66-
char *php_gethostbyaddr(char *ip);
67-
char *php_gethostbyname(char *name);
66+
static char *php_gethostbyaddr(char *ip);
67+
static char *php_gethostbyname(char *name);
6868

6969
/* {{{ proto string gethostbyaddr(string ip_address)
7070
Get the Internet host name corresponding to a given IP address */
7171
PHP_FUNCTION(gethostbyaddr)
7272
{
73-
pval **arg;
73+
zval **arg;
74+
char *addr;
7475

7576
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
76-
WRONG_PARAM_COUNT;
77+
ZEND_WRONG_PARAM_COUNT();
7778
}
79+
7880
convert_to_string_ex(arg);
81+
82+
addr = php_gethostbyaddr(Z_STRVAL_PP(arg));
7983

80-
RETVAL_STRING(php_gethostbyaddr(Z_STRVAL_PP(arg)), 0);
84+
if(addr == NULL) {
85+
php_error(E_WARNING, "Address is not in a.b.c.d form");
86+
RETVAL_FALSE;
87+
} else {
88+
RETVAL_STRING(addr, 0);
89+
}
8190
}
8291
/* }}} */
8392

8493
/* {{{ php_gethostbyaddr
8594
*/
86-
char *php_gethostbyaddr(char *ip)
95+
static char *php_gethostbyaddr(char *ip)
8796
{
8897
struct in_addr addr;
8998
struct hostent *hp;
9099

91100
addr.s_addr = inet_addr(ip);
101+
92102
if (addr.s_addr == -1) {
93-
#if PHP_DEBUG
94-
php_error(E_WARNING, "address not in a.b.c.d form");
95-
#endif
96-
return estrdup(ip);
103+
return NULL;
97104
}
105+
98106
hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
107+
99108
if (!hp) {
100-
#if PHP_DEBUG
101-
php_error(E_WARNING, "Unable to resolve %s\n", ip);
102-
#endif
103109
return estrdup(ip);
104110
}
111+
105112
return estrdup(hp->h_name);
106113
}
107114
/* }}} */
@@ -110,11 +117,12 @@ char *php_gethostbyaddr(char *ip)
110117
Get the IP address corresponding to a given Internet host name */
111118
PHP_FUNCTION(gethostbyname)
112119
{
113-
pval **arg;
120+
zval **arg;
114121

115122
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
116-
WRONG_PARAM_COUNT;
123+
ZEND_WRONG_PARAM_COUNT();
117124
}
125+
118126
convert_to_string_ex(arg);
119127

120128
RETVAL_STRING(php_gethostbyname(Z_STRVAL_PP(arg)), 0);
@@ -125,13 +133,13 @@ PHP_FUNCTION(gethostbyname)
125133
Return a list of IP addresses that a given hostname resolves to. */
126134
PHP_FUNCTION(gethostbynamel)
127135
{
128-
pval **arg;
136+
zval **arg;
129137
struct hostent *hp;
130138
struct in_addr in;
131139
int i;
132140

133141
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
134-
WRONG_PARAM_COUNT;
142+
ZEND_WRONG_PARAM_COUNT();
135143
}
136144
convert_to_string_ex(arg);
137145

@@ -141,36 +149,31 @@ PHP_FUNCTION(gethostbynamel)
141149

142150
hp = gethostbyname(Z_STRVAL_PP(arg));
143151
if (hp == NULL || hp->h_addr_list == NULL) {
144-
#if PHP_DEBUG
145-
php_error(E_WARNING, "Unable to resolve %s\n", Z_STRVAL_PP(arg));
146-
#endif
147-
return;
152+
RETURN_FALSE;
148153
}
149154

150155
for (i = 0 ; hp->h_addr_list[i] != 0 ; i++) {
151156
in = *(struct in_addr *) hp->h_addr_list[i];
152157
add_next_index_string(return_value, inet_ntoa(in), 1);
153158
}
154-
155-
return;
156159
}
157160
/* }}} */
158161

159162
/* {{{ php_gethostbyname
160163
*/
161-
char *php_gethostbyname(char *name)
164+
static char *php_gethostbyname(char *name)
162165
{
163166
struct hostent *hp;
164167
struct in_addr in;
165168

166169
hp = gethostbyname(name);
170+
167171
if (!hp || !hp->h_addr_list) {
168-
#if PHP_DEBUG
169-
php_error(E_WARNING, "Unable to resolve %s\n", name);
170-
#endif
171172
return estrdup(name);
172173
}
174+
173175
memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr));
176+
174177
return estrdup(inet_ntoa(in));
175178
}
176179
/* }}} */
@@ -181,7 +184,7 @@ char *php_gethostbyname(char *name)
181184
Check DNS records corresponding to a given Internet host name or IP address */
182185
PHP_FUNCTION(checkdnsrr)
183186
{
184-
pval **arg1,**arg2;
187+
zval **arg1,**arg2;
185188
int type,i;
186189
#ifndef MAXPACKET
187190
#define MAXPACKET 8192 /* max packet size used internally by BIND */

0 commit comments

Comments
 (0)