-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Polyfill dns_check_record(), dns_get_record(), and dns_get_mx() #1067
Merged
Merged
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2b10aa2
Polyfill dns_check_record(), dns_get_record(), and dns_get_mx()
adamziel fd68bc6
Add DNS constants registration
adamziel 0a5c539
Improve indentation, add missing alias for checkdnsrr, emit warnings …
adamziel fe82bd6
Remove .only from unit tests
adamziel 40c876e
Merge branch 'trunk' into polyfill_get_dns_record
bgrgicak 845c005
Fix tests
bgrgicak e5a8cf7
Rebuild web
bgrgicak 94da0ab
Move DNS consts to sns_polyfill
bgrgicak cb3a59b
Remove DNS_NS test
bgrgicak a03538e
Rebuild node
bgrgicak 3525045
Merge branch 'trunk' into polyfill_get_dns_record
bgrgicak 9c2f305
Update packages/php-wasm/compile/php/dns_polyfill.c
bgrgicak a51fe85
Add header file
bgrgicak 3c1ac8f
Rebuild web
bgrgicak e23b38c
Merge branch 'trunk' into polyfill_get_dns_record
bgrgicak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,253 @@ | ||
/* {{{ includes */ | ||
#include "php.h" | ||
#include "php_network.h" | ||
#include "zend_API.h" | ||
|
||
#ifdef HAVE_SYS_SOCKET_H | ||
#include <sys/socket.h> | ||
#endif | ||
|
||
#include <netinet/in.h> | ||
#ifdef HAVE_ARPA_INET_H | ||
#include <arpa/inet.h> | ||
#endif | ||
#include <netdb.h> | ||
#ifdef _OSD_POSIX | ||
#undef STATUS | ||
#undef T_UNSPEC | ||
#endif | ||
#ifdef HAVE_ARPA_NAMESER_H | ||
#ifdef DARWIN | ||
# define BIND_8_COMPAT 1 | ||
#endif | ||
#include <arpa/nameser.h> | ||
#endif | ||
#ifdef HAVE_RESOLV_H | ||
#include <resolv.h> | ||
#if defined(__HAIKU__) | ||
extern void __res_ndestroy(res_state statp); | ||
#define res_ndestroy __res_ndestroy | ||
#endif | ||
#endif | ||
#ifdef HAVE_DNS_H | ||
#include <dns.h> | ||
#endif | ||
|
||
#ifndef MAXHOSTNAMELEN | ||
#define MAXHOSTNAMELEN 255 | ||
#endif | ||
|
||
/* For the local hostname obtained via gethostname which is different from the | ||
dns-related MAXHOSTNAMELEN constant above */ | ||
#ifndef HOST_NAME_MAX | ||
#define HOST_NAME_MAX 255 | ||
#endif | ||
|
||
/* type compat */ | ||
#ifndef DNS_T_A | ||
#define DNS_T_A 1 | ||
#endif | ||
#ifndef DNS_T_NS | ||
#define DNS_T_NS 2 | ||
#endif | ||
#ifndef DNS_T_CNAME | ||
#define DNS_T_CNAME 5 | ||
#endif | ||
#ifndef DNS_T_SOA | ||
#define DNS_T_SOA 6 | ||
#endif | ||
#ifndef DNS_T_PTR | ||
#define DNS_T_PTR 12 | ||
#endif | ||
#ifndef DNS_T_HINFO | ||
#define DNS_T_HINFO 13 | ||
#endif | ||
#ifndef DNS_T_MINFO | ||
#define DNS_T_MINFO 14 | ||
#endif | ||
#ifndef DNS_T_MX | ||
#define DNS_T_MX 15 | ||
#endif | ||
#ifndef DNS_T_TXT | ||
#define DNS_T_TXT 16 | ||
#endif | ||
#ifndef DNS_T_AAAA | ||
#define DNS_T_AAAA 28 | ||
#endif | ||
#ifndef DNS_T_SRV | ||
#define DNS_T_SRV 33 | ||
#endif | ||
#ifndef DNS_T_NAPTR | ||
#define DNS_T_NAPTR 35 | ||
#endif | ||
#ifndef DNS_T_A6 | ||
#define DNS_T_A6 38 | ||
#endif | ||
#ifndef DNS_T_CAA | ||
#define DNS_T_CAA 257 | ||
#endif | ||
|
||
#ifndef DNS_T_ANY | ||
#define DNS_T_ANY 255 | ||
#endif | ||
/* }}} */ | ||
|
||
#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 */ | ||
|
||
#undef MAXHOSTNAMELEN | ||
#define MAXHOSTNAMELEN 1024 | ||
|
||
#ifndef MAXRESOURCERECORDS | ||
#define MAXRESOURCERECORDS 64 | ||
#endif /* MAXRESOURCERECORDS */ | ||
|
||
#define PHP_DNS_A 0x00000001 | ||
#define PHP_DNS_NS 0x00000002 | ||
#define PHP_DNS_CNAME 0x00000010 | ||
#define PHP_DNS_SOA 0x00000020 | ||
#define PHP_DNS_PTR 0x00000800 | ||
#define PHP_DNS_HINFO 0x00001000 | ||
#if !defined(PHP_WIN32) | ||
# define PHP_DNS_CAA 0x00002000 | ||
#endif | ||
#define PHP_DNS_MX 0x00004000 | ||
#define PHP_DNS_TXT 0x00008000 | ||
#define PHP_DNS_A6 0x01000000 | ||
#define PHP_DNS_SRV 0x02000000 | ||
#define PHP_DNS_NAPTR 0x04000000 | ||
#define PHP_DNS_AAAA 0x08000000 | ||
#define PHP_DNS_ANY 0x10000000 | ||
#define PHP_DNS_NUM_TYPES 13 /* Number of DNS Types Supported by PHP currently */ | ||
#define PHP_DNS_ALL (PHP_DNS_A|PHP_DNS_NS|PHP_DNS_CNAME|PHP_DNS_SOA|PHP_DNS_PTR|PHP_DNS_HINFO|PHP_DNS_CAA|PHP_DNS_MX|PHP_DNS_TXT|PHP_DNS_A6|PHP_DNS_SRV|PHP_DNS_NAPTR|PHP_DNS_AAAA) | ||
|
||
typedef union { | ||
HEADER qb1; | ||
uint8_t qb2[65536]; | ||
} querybuf; | ||
|
||
ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_check_record, 0, 0, 1) | ||
ZEND_ARG_INFO(0, host) | ||
ZEND_ARG_INFO(0, type) | ||
ZEND_END_ARG_INFO() | ||
|
||
PHP_FUNCTION(dns_check_record) | ||
{ | ||
HEADER *hp; | ||
querybuf answer = {0}; | ||
char *hostname; | ||
size_t hostname_len; | ||
size_t rectype_len = 0; | ||
zend_string *rectype = NULL; | ||
int type = DNS_T_MX, i; | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &hostname, &hostname_len, &rectype, &rectype_len) == FAILURE) { | ||
return; | ||
} | ||
|
||
if (hostname_len == 0) { | ||
php_error_docref(NULL, E_WARNING, "Host cannot be empty"); | ||
RETURN_FALSE; | ||
} | ||
|
||
RETURN_FALSE; | ||
} | ||
|
||
/* {{{ Get any Resource Record corresponding to a given Internet host name */ | ||
ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_record, 0, 0, 1) | ||
ZEND_ARG_INFO(0, hostname) | ||
ZEND_ARG_INFO(0, type) | ||
ZEND_ARG_ARRAY_INFO(1, authns, 1) | ||
ZEND_ARG_ARRAY_INFO(1, addtl, 1) | ||
ZEND_ARG_INFO(0, raw) | ||
ZEND_END_ARG_INFO() | ||
|
||
|
||
PHP_FUNCTION(dns_get_record) | ||
{ | ||
char *hostname; | ||
size_t hostname_len; | ||
zend_long type_param = PHP_DNS_ANY; | ||
zval *authns = NULL, *addtl = NULL; | ||
int type_to_fetch; | ||
int dns_errno; | ||
HEADER *hp; | ||
querybuf answer = {0}; | ||
uint8_t *cp = NULL, *end = NULL; | ||
int n, qd, an, ns = 0, ar = 0; | ||
int type, first_query = 1, store_results = 1; | ||
zend_bool raw = 0; | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lz!z!b", | ||
&hostname, &hostname_len, &type_param, &authns, &addtl, &raw) == FAILURE) { | ||
return; | ||
} | ||
|
||
if (authns) { | ||
array_init(authns); | ||
if (!authns) { | ||
RETURN_FALSE; | ||
} | ||
} | ||
if (addtl) { | ||
array_init(addtl); | ||
if (!addtl) { | ||
RETURN_FALSE; | ||
} | ||
} | ||
|
||
/* Initialize the return array */ | ||
array_init(return_value); | ||
} | ||
|
||
/* }}} */ | ||
|
||
/* {{{ Get MX records corresponding to a given Internet host name */ | ||
ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_mx, 0, 0, 2) | ||
ZEND_ARG_INFO(0, hostname) | ||
ZEND_ARG_INFO(1, mxhosts) /* ARRAY_INFO(1, mxhosts, 1) */ | ||
ZEND_ARG_INFO(1, weight) /* ARRAY_INFO(1, weight, 1) */ | ||
ZEND_END_ARG_INFO() | ||
|
||
#define arginfo_getmxrr arginfo_dns_get_mx | ||
|
||
PHP_FUNCTION(dns_get_mx) | ||
{ | ||
char *hostname; | ||
size_t hostname_len; | ||
zval *mx_list, *weight_list = NULL; | ||
int count, qdc; | ||
u_short type, weight; | ||
querybuf answer = {0}; | ||
char buf[MAXHOSTNAMELEN] = {0}; | ||
HEADER *hp; | ||
uint8_t *cp, *end; | ||
int i; | ||
|
||
ZEND_PARSE_PARAMETERS_START(2, 3) | ||
Z_PARAM_STRING(hostname, hostname_len) | ||
Z_PARAM_ZVAL(mx_list) | ||
Z_PARAM_OPTIONAL | ||
Z_PARAM_ZVAL(weight_list) | ||
ZEND_PARSE_PARAMETERS_END(); | ||
|
||
array_init(mx_list); | ||
if (!mx_list) { | ||
RETURN_FALSE; | ||
} | ||
|
||
if (weight_list) { | ||
array_init(weight_list); | ||
if (!weight_list) { | ||
RETURN_FALSE; | ||
} | ||
} | ||
|
||
RETURN_FALSE; | ||
} | ||
/* }}} */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include
ing a*.c
file seems a bit unorthodox. Doesn't this leave us open to having issues with double definitions down the track?I feel it would be more typical to ensure
dns_polyfill.c
gets compiled separately and then linked into the final output? i.e. including it in the build command here:wordpress-playground/packages/php-wasm/compile/php/Dockerfile
Line 932 in 3525045
We'd need another header file to declare the functions so they can be included in the
additional_functions
array. But still, that seems a bit more conventional.But I might be missing something. Was there a reason why this needed to be included as a C file? Could the DNS functions simply be added to the giant
php_wasm.c
file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bgrgicak I gave this a try on a branch of my own and it seems to be working: p-jackson@e331922
I'm not familiar with all of PHP's C macros, but chatgpt said the same
PHP_FUNCTION
macro can be used for declaring functions too. I also had to move the arginfo declarations into the header file since that's important info that needs to be part of the ccall to register the functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Thank you for working on it. I will add these changes to the PR later today and if everything works we can merge it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the header file in a51fe85.
Thank you for helping me!