Skip to content

Commit 91f6470

Browse files
committed
- [DOC] Added a 5th parameter to dns_get_record, a boolean that tells whether to activate
"raw mode". In this mide, $type (2nd parameter) is the numeric type of the record, and the responses are not parsed -- the "type" element will be numeric and there will be a "data" element with the raw data of the response buffer, which the programmer will have to parse. - Fixed bug in the Win32 implementation of dns_get_record, where the 3rd and 4th arguments would only be filled if the 2nd ($type) was DNS_ANY. - [DOC] The 3rd and 4th parameters can now be NULL (changed their arginfo).
1 parent 7aa43a8 commit 91f6470

File tree

3 files changed

+101
-45
lines changed

3 files changed

+101
-45
lines changed

ext/standard/basic_functions.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -996,11 +996,12 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_check_record, 0, 0, 1)
996996
ZEND_END_ARG_INFO()
997997

998998
# if defined(PHP_WIN32) || HAVE_FULL_DNS_FUNCS
999-
ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_record, 1, 0, 1)
999+
ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_record, 0, 0, 1)
10001000
ZEND_ARG_INFO(0, hostname)
10011001
ZEND_ARG_INFO(0, type)
1002-
ZEND_ARG_INFO(1, authns) /* ARRAY_INFO(1, authns, 1) */
1003-
ZEND_ARG_INFO(1, addtl) /* ARRAY_INFO(1, addtl, 1) */
1002+
ZEND_ARG_ARRAY_INFO(1, authns, 1)
1003+
ZEND_ARG_ARRAY_INFO(1, addtl, 1)
1004+
ZEND_ARG_INFO(0, raw)
10041005
ZEND_END_ARG_INFO()
10051006

10061007
ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_mx, 0, 0, 2)

ext/standard/dns.c

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ PHP_FUNCTION(dns_check_record)
403403
#if HAVE_FULL_DNS_FUNCS
404404

405405
/* {{{ php_parserr */
406-
static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int store, zval **subarray)
406+
static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int store, int raw, zval **subarray)
407407
{
408408
u_short type, class, dlen;
409409
u_long ttl;
@@ -439,6 +439,16 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
439439
array_init(*subarray);
440440

441441
add_assoc_string(*subarray, "host", name, 1);
442+
add_assoc_string(*subarray, "class", "IN", 1);
443+
add_assoc_long(*subarray, "ttl", ttl);
444+
445+
if (raw) {
446+
add_assoc_long(*subarray, "type", type);
447+
add_assoc_stringl(*subarray, "data", (char*) cp, (uint) dlen, 1);
448+
cp += dlen;
449+
return cp;
450+
}
451+
442452
switch (type) {
443453
case DNS_T_A:
444454
add_assoc_string(*subarray, "type", "A", 1);
@@ -679,20 +689,12 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
679689
add_assoc_string(*subarray, "replacement", name, 1);
680690
break;
681691
default:
682-
{
683-
char buf[10]; /* max length of short + sizeof(id #) */
684-
snprintf(buf, 10, "id #%hu", type);
685-
buf[10-1] = '\0';
686-
add_assoc_string(*subarray, "type", buf, 1);
687-
add_assoc_stringl(*subarray, "data", (char*) cp, (uint) dlen, 1);
688-
cp += dlen;
689-
break;
690-
}
692+
zval_ptr_dtor(subarray);
693+
*subarray = NULL;
694+
cp += dlen;
695+
break;
691696
}
692697

693-
add_assoc_string(*subarray, "class", "IN", 1);
694-
add_assoc_long(*subarray, "ttl", ttl);
695-
696698
return cp;
697699
}
698700
/* }}} */
@@ -720,8 +722,10 @@ PHP_FUNCTION(dns_get_record)
720722
u_char *cp = NULL, *end = NULL;
721723
int n, qd, an, ns = 0, ar = 0;
722724
int type, first_query = 1, store_results = 1;
725+
zend_bool raw = 0;
723726

724-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzz", &hostname, &hostname_len, &type_param, &authns, &addtl) == FAILURE) {
727+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz!z!b",
728+
&hostname, &hostname_len, &type_param, &authns, &addtl, &raw) == FAILURE) {
725729
return;
726730
}
727731

@@ -735,9 +739,17 @@ PHP_FUNCTION(dns_get_record)
735739
addtl_recs = 1;
736740
}
737741

738-
if (type_param & ~PHP_DNS_ALL && type_param != PHP_DNS_ANY) {
739-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%ld' not supported", type_param);
740-
RETURN_FALSE;
742+
if (!raw) {
743+
if ((type_param & ~PHP_DNS_ALL) && (type_param != PHP_DNS_ANY)) {
744+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%ld' not supported", type_param);
745+
RETURN_FALSE;
746+
}
747+
} else {
748+
if ((type_param < 1) || (type_param > 0xFFFF)) {
749+
php_error_docref(NULL TSRMLS_CC, E_WARNING,
750+
"Numeric DNS record type must be between 1 and 65535, '%ld' given", type_param);
751+
RETURN_FALSE;
752+
}
741753
}
742754

743755
/* Initialize the return array */
@@ -748,13 +760,29 @@ PHP_FUNCTION(dns_get_record)
748760
* store_results is used to skip storing the results retrieved in step
749761
* NUMTYPES+1 when results were already fetched.
750762
* - In case of PHP_DNS_ANY we use the directly fetch DNS_T_ANY. (step NUMTYPES+1 )
763+
* - In case of raw mode, we query only the requestd type instead of looping type by type
764+
* before going with the additional info stuff.
751765
*/
752-
for (type = (type_param == PHP_DNS_ANY ? (PHP_DNS_NUM_TYPES + 1) : 0);
766+
767+
if (raw) {
768+
type = -1;
769+
} else if (type_param == PHP_DNS_ANY) {
770+
type = PHP_DNS_NUM_TYPES + 1;
771+
} else {
772+
type = 0;
773+
}
774+
775+
for ( ;
753776
type < (addtl_recs ? (PHP_DNS_NUM_TYPES + 2) : PHP_DNS_NUM_TYPES) || first_query;
754777
type++
755778
) {
756779
first_query = 0;
757780
switch (type) {
781+
case -1: /* raw */
782+
type_to_fetch = type_param;
783+
/* skip over the rest and go directly to additional records */
784+
type = PHP_DNS_NUM_TYPES - 1;
785+
break;
758786
case 0:
759787
type_to_fetch = type_param&PHP_DNS_A ? DNS_T_A : 0;
760788
break;
@@ -846,7 +874,7 @@ PHP_FUNCTION(dns_get_record)
846874
while (an-- && cp && cp < end) {
847875
zval *retval;
848876

849-
cp = php_parserr(cp, &answer, type_to_fetch, store_results, &retval);
877+
cp = php_parserr(cp, &answer, type_to_fetch, store_results, raw, &retval);
850878
if (retval != NULL && store_results) {
851879
add_next_index_zval(return_value, retval);
852880
}
@@ -859,7 +887,7 @@ PHP_FUNCTION(dns_get_record)
859887
while (ns-- > 0 && cp && cp < end) {
860888
zval *retval = NULL;
861889

862-
cp = php_parserr(cp, &answer, DNS_T_ANY, authns != NULL, &retval);
890+
cp = php_parserr(cp, &answer, DNS_T_ANY, authns != NULL, raw, &retval);
863891
if (retval != NULL) {
864892
add_next_index_zval(authns, retval);
865893
}
@@ -871,7 +899,7 @@ PHP_FUNCTION(dns_get_record)
871899
while (ar-- > 0 && cp && cp < end) {
872900
zval *retval = NULL;
873901

874-
cp = php_parserr(cp, &answer, DNS_T_ANY, 1, &retval);
902+
cp = php_parserr(cp, &answer, DNS_T_ANY, 1, raw, &retval);
875903
if (retval != NULL) {
876904
add_next_index_zval(addtl, retval);
877905
}

ext/standard/dns_win32.c

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ PHP_FUNCTION(dns_check_record)
133133
/* }}} */
134134

135135
/* {{{ php_parserr */
136-
static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, zval **subarray)
136+
static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, int raw, zval **subarray)
137137
{
138138
int type;
139139
u_long ttl;
@@ -153,6 +153,15 @@ static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, zval **s
153153
array_init(*subarray);
154154

155155
add_assoc_string(*subarray, "host", pRec->pName, 1);
156+
add_assoc_string(*subarray, "class", "IN", 1);
157+
add_assoc_long(*subarray, "ttl", ttl);
158+
159+
if (raw) {
160+
add_assoc_long(*subarray, "type", type);
161+
add_assoc_stringl(*subarray, "data", (char*) &pRec->Data, (uint) pRec->wDataLength, 1);
162+
return;
163+
}
164+
156165
switch (type) {
157166
case DNS_TYPE_A: {
158167
IN_ADDR ipaddr;
@@ -303,9 +312,9 @@ static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, zval **s
303312
}
304313
break;
305314

315+
#if _MSC_VER >= 1500
306316
case DNS_TYPE_NAPTR:
307317
{
308-
#if _MSC_VER >= 1500
309318
DNS_NAPTR_DATA * data_naptr = &pRec->Data.Naptr;
310319

311320
add_assoc_string(*subarray, "type", "NAPTR", 1);
@@ -315,23 +324,17 @@ static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, zval **s
315324
add_assoc_string(*subarray, "services", data_naptr->pService, 1);
316325
add_assoc_string(*subarray, "regex", data_naptr->pRegularExpression, 1);
317326
add_assoc_string(*subarray, "replacement", data_naptr->pReplacement, 1);
318-
#endif
319327
}
320328
break;
329+
#endif
321330

322331
default:
323-
{
324-
char buf[10]; /* max length of short + sizeof(id #) */
325-
snprintf(buf, 10, "id #%hu", (unsigned short) type);
326-
buf[10-1] = '\0';
327-
add_assoc_string(*subarray, "type", buf, 1);
328-
add_assoc_stringl(*subarray, "data", (char*) &pRec->Data, (uint) pRec->wDataLength, 1);
329-
break;
330-
}
332+
/* unkown type */
333+
zval_ptr_dtor(subarray);
334+
*subarray = NULL;
335+
return;
331336
}
332337

333-
add_assoc_string(*subarray, "class", "IN", 1);
334-
add_assoc_long(*subarray, "ttl", ttl);
335338
}
336339
/* }}} */
337340

@@ -345,8 +348,10 @@ PHP_FUNCTION(dns_get_record)
345348
zval *authns = NULL, *addtl = NULL;
346349
int type, type_to_fetch, first_query = 1, store_results = 1;
347350
int addtl_recs = 0;
351+
zend_bool raw = 0;
348352

349-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzz", &hostname, &hostname_len, &type_param, &authns, &addtl) == FAILURE) {
353+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz!z!b",
354+
&hostname, &hostname_len, &type_param, &authns, &addtl, &raw) == FAILURE) {
350355
return;
351356
}
352357

@@ -357,17 +362,34 @@ PHP_FUNCTION(dns_get_record)
357362
if (addtl) {
358363
zval_dtor(addtl);
359364
array_init(addtl);
365+
addtl_recs = 1;
360366
}
361367

362-
if (type_param & ~PHP_DNS_ALL && type_param != PHP_DNS_ANY) {
363-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%ld' not supported", type_param);
364-
RETURN_FALSE;
368+
if (!raw) {
369+
if ((type_param & ~PHP_DNS_ALL) && (type_param != PHP_DNS_ANY)) {
370+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type '%ld' not supported", type_param);
371+
RETURN_FALSE;
372+
}
373+
} else {
374+
if ((type_param < 1) || (type_param > 0xFFFF)) {
375+
php_error_docref(NULL TSRMLS_CC, E_WARNING,
376+
"Numeric DNS record type must be between 1 and 65535, '%ld' given", type_param);
377+
RETURN_FALSE;
378+
}
365379
}
366380

367381
/* Initialize the return array */
368382
array_init(return_value);
369383

370-
for (type = (type_param == PHP_DNS_ANY ? (PHP_DNS_NUM_TYPES + 1) : 0);
384+
if (raw) {
385+
type = -1;
386+
} else if (type_param == PHP_DNS_ANY) {
387+
type = PHP_DNS_NUM_TYPES + 1;
388+
} else {
389+
type = 0;
390+
}
391+
392+
for ( ;
371393
type < (addtl_recs ? (PHP_DNS_NUM_TYPES + 2) : PHP_DNS_NUM_TYPES) || first_query;
372394
type++
373395
) {
@@ -376,6 +398,11 @@ PHP_FUNCTION(dns_get_record)
376398

377399
first_query = 0;
378400
switch (type) {
401+
case -1: /* raw */
402+
type_to_fetch = type_param;
403+
/* skip over the rest and go directly to additional records */
404+
type = PHP_DNS_NUM_TYPES - 1;
405+
break;
379406
case 0:
380407
type_to_fetch = type_param&PHP_DNS_A ? DNS_TYPE_A : 0;
381408
break;
@@ -439,15 +466,15 @@ PHP_FUNCTION(dns_get_record)
439466
zval *retval = NULL;
440467

441468
if (pRec->Flags.S.Section == DnsSectionAnswer) {
442-
php_parserr(pRec, type_to_fetch, store_results, &retval);
469+
php_parserr(pRec, type_to_fetch, store_results, raw, &retval);
443470
if (retval != NULL && store_results) {
444471
add_next_index_zval(return_value, retval);
445472
}
446473
}
447474

448475
if (authns && pRec->Flags.S.Section == DnsSectionAuthority) {
449476

450-
php_parserr(pRec, type_to_fetch, store_results, &retval);
477+
php_parserr(pRec, type_to_fetch, 1, raw, &retval);
451478
if (retval != NULL) {
452479
add_next_index_zval(authns, retval);
453480
}
@@ -462,7 +489,7 @@ PHP_FUNCTION(dns_get_record)
462489
# endif
463490
#endif
464491
if (addtl && pRec->Flags.S.Section == DnsSectionAdditional) {
465-
php_parserr(pRec, type_to_fetch, store_results, &retval);
492+
php_parserr(pRec, type_to_fetch, 1, raw, &retval);
466493
if (retval != NULL) {
467494
add_next_index_zval(addtl, retval);
468495
}

0 commit comments

Comments
 (0)