Skip to content

Commit d84ee96

Browse files
bgrgicakbrandonpayton
authored andcommitted
Move DNS polyfills to a PHP extension and add missing constants (#27)
The original implementation of the DNS polyfill didn't register `DNS_*` constants which are [expected to exist in PHP](https://www.php.net/manual/en/network.constants.php#constant.dns-any). To fix this this PR registered the required DNS constants. The constants were already defined in the polyfill and now we register them as PH constants using `REGISTER_LONG_CONSTANT`. I didn't want to add more code to the `php_wasm.c` file that wasn't directly related to PHP-wasm, so I moved the DNS polyfill code to an extension. - CI
1 parent adb2d94 commit d84ee96

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+259
-149
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dnl config.m4 for extension dns_polyfill
2+
3+
PHP_ARG_ENABLE(dns_polyfill, whether to enable dns_polyfill support,
4+
[ --enable-dns_polyfill Enable dns_polyfill support])
5+
6+
if test "$PHP_WASM_DNS_POLYFILL" != "no"; then
7+
PHP_NEW_EXTENSION(dns_polyfill, dns_polyfill.c, $ext_shared)
8+
fi
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ARG_ENABLE("dns_polyfill", "Enable dns_polyfill support", "no");
2+
3+
if (PHP_WASM_DNS_POLYFILL == "yes") {
4+
EXTENSION("dns_polyfill", "dns_polyfill.c");
5+
}

packages/php-wasm/compile/php/dns_polyfill.c renamed to packages/php-wasm/compile/php-wasm-dns-polyfill/dns_polyfill.c

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "php_network.h"
44
#include "zend_API.h"
55
#include "dns_polyfill.h"
6+
#include "ext/standard/info.h"
67

78
#ifdef HAVE_SYS_SOCKET_H
89
#include <sys/socket.h>
@@ -108,61 +109,11 @@ extern void __res_ndestroy(res_state statp);
108109
#define MAXRESOURCERECORDS 64
109110
#endif /* MAXRESOURCERECORDS */
110111

111-
#ifndef PHP_DNS_A
112-
#define PHP_DNS_A 0x00000001
113-
#endif
114-
#ifndef PHP_DNS_NS
115-
#define PHP_DNS_NS 0x00000002
116-
#endif
117-
#ifndef PHP_DNS_CNAME
118-
#define PHP_DNS_CNAME 0x00000010
119-
#endif
120-
#ifndef PHP_DNS_SOA
121-
#define PHP_DNS_SOA 0x00000020
122-
#endif
123-
#ifndef PHP_DNS_PTR
124-
#define PHP_DNS_PTR 0x00000800
125-
#endif
126-
#ifndef PHP_DNS_HINFO
127-
#define PHP_DNS_HINFO 0x00001000
128-
#endif
129-
#if !defined(PHP_WIN32) && !defined(PHP_DNS_CAA)
130-
#define PHP_DNS_CAA 0x00002000
131-
#endif
132-
#ifndef PHP_DNS_MX
133-
#define PHP_DNS_MX 0x00004000
134-
#endif
135-
#ifndef PHP_DNS_TXT
136-
#define PHP_DNS_TXT 0x00008000
137-
#endif
138-
#ifndef PHP_DNS_A6
139-
#define PHP_DNS_A6 0x01000000
140-
#endif
141-
#ifndef PHP_DNS_SRV
142-
#define PHP_DNS_SRV 0x02000000
143-
#endif
144-
#ifndef PHP_DNS_NAPTR
145-
#define PHP_DNS_NAPTR 0x04000000
146-
#endif
147-
#ifndef PHP_DNS_AAAA
148-
#define PHP_DNS_AAAA 0x08000000
149-
#endif
150-
#ifndef PHP_DNS_ANY
151-
#define PHP_DNS_ANY 0x10000000
152-
#endif
153-
#ifndef PHP_DNS_NUM_TYPES
154-
#define PHP_DNS_NUM_TYPES 13 /* Number of DNS Types Supported by PHP currently */
155-
#endif
156-
#ifndef PHP_DNS_ALL
157-
#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)
158-
#endif
159112
typedef union {
160113
HEADER qb1;
161114
uint8_t qb2[65536];
162115
} querybuf;
163116

164-
#define arginfo_checkdnsrr arginfo_dns_check_record
165-
166117
PHP_FUNCTION(dns_check_record)
167118
{
168119
HEADER *hp;
@@ -232,8 +183,6 @@ PHP_FUNCTION(dns_get_record)
232183

233184
/* {{{ Get MX records corresponding to a given Internet host name */
234185

235-
#define arginfo_getmxrr arginfo_dns_get_mx
236-
237186
PHP_FUNCTION(dns_get_mx)
238187
{
239188
char *hostname;
@@ -271,3 +220,62 @@ PHP_FUNCTION(dns_get_mx)
271220
RETURN_FALSE;
272221
}
273222
/* }}} */
223+
224+
/* {{{ PHP_MINFO_FUNCTION */
225+
PHP_MINFO_FUNCTION(dns_polyfill)
226+
{
227+
php_info_print_table_start();
228+
php_info_print_table_row(2, "dns_polyfill support", "enabled");
229+
php_info_print_table_end();
230+
}
231+
/* }}} */
232+
233+
PHP_MINIT_FUNCTION(dns_polyfill)
234+
{
235+
REGISTER_LONG_CONSTANT("DNS_A", PHP_DNS_A, CONST_CS | CONST_PERSISTENT);
236+
REGISTER_LONG_CONSTANT("DNS_NS", PHP_DNS_NS, CONST_CS | CONST_PERSISTENT);
237+
REGISTER_LONG_CONSTANT("DNS_CNAME", PHP_DNS_CNAME, CONST_CS | CONST_PERSISTENT);
238+
REGISTER_LONG_CONSTANT("DNS_SOA", PHP_DNS_SOA, CONST_CS | CONST_PERSISTENT);
239+
REGISTER_LONG_CONSTANT("DNS_PTR", PHP_DNS_PTR, CONST_CS | CONST_PERSISTENT);
240+
REGISTER_LONG_CONSTANT("DNS_HINFO", PHP_DNS_HINFO, CONST_CS | CONST_PERSISTENT);
241+
REGISTER_LONG_CONSTANT("DNS_CAA", PHP_DNS_CAA, CONST_CS | CONST_PERSISTENT);
242+
REGISTER_LONG_CONSTANT("DNS_MX", PHP_DNS_MX, CONST_CS | CONST_PERSISTENT);
243+
REGISTER_LONG_CONSTANT("DNS_TXT", PHP_DNS_TXT, CONST_CS | CONST_PERSISTENT);
244+
REGISTER_LONG_CONSTANT("DNS_SRV", PHP_DNS_SRV, CONST_CS | CONST_PERSISTENT);
245+
REGISTER_LONG_CONSTANT("DNS_NAPTR", PHP_DNS_NAPTR, CONST_CS | CONST_PERSISTENT);
246+
REGISTER_LONG_CONSTANT("DNS_AAAA", PHP_DNS_AAAA, CONST_CS | CONST_PERSISTENT);
247+
REGISTER_LONG_CONSTANT("DNS_A6", PHP_DNS_A6, CONST_CS | CONST_PERSISTENT);
248+
REGISTER_LONG_CONSTANT("DNS_ANY", PHP_DNS_ANY, CONST_CS | CONST_PERSISTENT);
249+
REGISTER_LONG_CONSTANT("DNS_ALL", PHP_DNS_ALL, CONST_CS | CONST_PERSISTENT);
250+
251+
return SUCCESS;
252+
}
253+
254+
PHP_MSHUTDOWN_FUNCTION(dns_polyfill)
255+
{
256+
return SUCCESS;
257+
}
258+
259+
/* {{{ dns_polyfill_functions[] */
260+
const zend_function_entry dns_polyfill_functions[] = {
261+
ZEND_FE(dns_get_mx, arginfo_dns_get_mx)
262+
ZEND_FALIAS(getmxrr, dns_get_mx, arginfo_getmxrr)
263+
ZEND_FE(dns_check_record, arginfo_dns_check_record)
264+
ZEND_FALIAS(checkdnsrr, dns_check_record, arginfo_checkdnsrr)
265+
ZEND_FE(dns_get_record, arginfo_dns_get_record)
266+
ZEND_FE_END};
267+
/* }}} */
268+
269+
/* {{{ dns_polyfill_module_entry */
270+
zend_module_entry dns_polyfill_module_entry = {
271+
STANDARD_MODULE_HEADER,
272+
"dns_polyfill", /* Extension name */
273+
dns_polyfill_functions, /* zend_function_entry */
274+
PHP_MINIT(dns_polyfill), /* PHP_MINIT - Module initialization */
275+
PHP_MSHUTDOWN(dns_polyfill), /* PHP_MSHUTDOWN - Module shutdown */
276+
NULL, /* PHP_RINIT - Request initialization */
277+
NULL, /* PHP_RSHUTDOWN - Request shutdown */
278+
PHP_MINFO(dns_polyfill), /* PHP_MINFO - Module info */
279+
PHP_DNS_POLYFILL_VERSION, /* Version */
280+
STANDARD_MODULE_PROPERTIES};
281+
/* }}} */
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#ifndef PHP_WASM_DNS_POLYFILL_H
2+
#define PHP_WASM_DNS_POLYFILL_H
3+
4+
#ifndef PHP_DNS_A
5+
#define PHP_DNS_A 0x00000001
6+
#endif
7+
#ifndef PHP_DNS_NS
8+
#define PHP_DNS_NS 0x00000002
9+
#endif
10+
#ifndef PHP_DNS_CNAME
11+
#define PHP_DNS_CNAME 0x00000010
12+
#endif
13+
#ifndef PHP_DNS_SOA
14+
#define PHP_DNS_SOA 0x00000020
15+
#endif
16+
#ifndef PHP_DNS_PTR
17+
#define PHP_DNS_PTR 0x00000800
18+
#endif
19+
#ifndef PHP_DNS_HINFO
20+
#define PHP_DNS_HINFO 0x00001000
21+
#endif
22+
#if !defined(PHP_WIN32) && !defined(PHP_DNS_CAA)
23+
#define PHP_DNS_CAA 0x00002000
24+
#endif
25+
#ifndef PHP_DNS_MX
26+
#define PHP_DNS_MX 0x00004000
27+
#endif
28+
#ifndef PHP_DNS_TXT
29+
#define PHP_DNS_TXT 0x00008000
30+
#endif
31+
#ifndef PHP_DNS_A6
32+
#define PHP_DNS_A6 0x01000000
33+
#endif
34+
#ifndef PHP_DNS_SRV
35+
#define PHP_DNS_SRV 0x02000000
36+
#endif
37+
#ifndef PHP_DNS_NAPTR
38+
#define PHP_DNS_NAPTR 0x04000000
39+
#endif
40+
#ifndef PHP_DNS_AAAA
41+
#define PHP_DNS_AAAA 0x08000000
42+
#endif
43+
#ifndef PHP_DNS_ANY
44+
#define PHP_DNS_ANY 0x10000000
45+
#endif
46+
#ifndef PHP_DNS_NUM_TYPES
47+
#define PHP_DNS_NUM_TYPES 13 /* Number of DNS Types Supported by PHP currently */
48+
#endif
49+
#ifndef PHP_DNS_ALL
50+
#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)
51+
#endif
52+
53+
extern zend_module_entry dns_polyfill_module_entry;
54+
#define phpext_dns_polyfill_ptr &dns_polyfill_module_entry
55+
56+
ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_check_record, 0, 0, 1)
57+
ZEND_ARG_INFO(0, host)
58+
ZEND_ARG_INFO(0, type)
59+
ZEND_END_ARG_INFO()
60+
61+
#define arginfo_checkdnsrr arginfo_dns_check_record
62+
63+
PHP_FUNCTION(dns_check_record);
64+
65+
ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_record, 0, 0, 1)
66+
ZEND_ARG_INFO(0, hostname)
67+
ZEND_ARG_INFO(0, type)
68+
ZEND_ARG_ARRAY_INFO(1, authns, 1)
69+
ZEND_ARG_ARRAY_INFO(1, addtl, 1)
70+
ZEND_ARG_INFO(0, raw)
71+
ZEND_END_ARG_INFO()
72+
73+
PHP_FUNCTION(dns_get_record);
74+
75+
ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_mx, 0, 0, 2)
76+
ZEND_ARG_INFO(0, hostname)
77+
ZEND_ARG_INFO(1, mxhosts) /* ARRAY_INFO(1, mxhosts, 1) */
78+
ZEND_ARG_INFO(1, weight) /* ARRAY_INFO(1, weight, 1) */
79+
ZEND_END_ARG_INFO()
80+
81+
#define arginfo_getmxrr arginfo_dns_get_mx
82+
83+
PHP_FUNCTION(dns_get_mx);
84+
85+
#define PHP_DNS_POLYFILL_VERSION "1.0.0"
86+
87+
#endif // PHP_WASM_DNS_POLYFILL_H

packages/php-wasm/compile/php/Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ RUN git clone https://github.com/php/php-src.git php-src \
1919
# Work around memory leak due to PHP using Emscripten's incomplete mmap/munmap support
2020
COPY ./php-wasm-memory-storage /root/php-src/ext/wasm_memory_storage
2121

22+
# Polyfill for dns functions
23+
COPY ./php-wasm-dns-polyfill/ /root/php-src/ext/dns_polyfill
24+
2225
RUN cd php-src && ./buildconf --force
2326

2427
# Bring in the libraries
@@ -333,6 +336,7 @@ CURL_LIBS="-I/root/lib/lib -L/root/lib/lib" \
333336
--enable-ctype \
334337
--enable-tokenizer \
335338
--enable-wasm_memory_storage \
339+
--enable-dns_polyfill \
336340
$(cat /root/.php-configure-flags)
337341

338342
# Silence the errors "munmap() failed: [28] Invalid argument"
@@ -391,8 +395,6 @@ RUN cp -v /root/php-src/.libs/libphp*.la /root/lib/libphp.la
391395
RUN cp -v /root/php-src/.libs/libphp*.a /root/lib/libphp.a
392396

393397
COPY ./php/php_wasm.c /root/
394-
COPY ./php/dns_polyfill.c /root/
395-
COPY ./php/dns_polyfill.h /root/
396398
COPY ./php/proc_open* /root/
397399

398400
RUN if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]]; then \
@@ -964,7 +966,7 @@ RUN set -euxo pipefail; \
964966
else \
965967
export ASYNCIFY_FLAGS=" -s ASYNCIFY=1 -s ASYNCIFY_IGNORE_INDIRECT=1 -s EXPORTED_RUNTIME_METHODS=ccall,PROXYFS,wasmExports $(cat /root/.emcc-php-asyncify-flags) "; \
966968
echo '' > /root/php_wasm_asyncify.h; \
967-
fi; \
969+
fi; \
968970
export EXPORTED_FUNCTIONS=$'["_exit", \n\
969971
"UTF8ToString", \n\
970972
"stringToUTF8", \n\
@@ -1024,7 +1026,6 @@ RUN set -euxo pipefail; \
10241026
/root/lib/libphp.a \
10251027
/root/proc_open.c \
10261028
/root/php_wasm.c \
1027-
/root/dns_polyfill.c \
10281029
$(cat /root/.emcc-php-wasm-sources) \
10291030
-s ENVIRONMENT=$EMSCRIPTEN_ENVIRONMENT \
10301031
-s FORCE_FILESYSTEM=1 \
@@ -1123,7 +1124,7 @@ RUN set -euxo pipefail; \
11231124
# In case of WordPress Playground, the worker in which the PHP
11241125
# runs will typically exit after the PHP program finishes, so
11251126
# we don't have to worry about memory leaks.
1126-
/root/replace-across-lines.sh 's/(class\s+ExitStatus\s*\{\s*name\s*=\s*"ExitStatus";\s*constructor\(\s*status\s*\)\s*\{\s*this\.message\s*=\s*`Program terminated with exit\(\$\{status\}\)`;\s*this.status\s*=\s*status;?\s*\}\s*\})/$1\nExitStatus = class PHPExitStatus extends Error {\nconstructor(status) {\n super(status);\n this.name = "ExitStatus";\n this.message = "Program terminated with exit(" + status + ")";\n this.status = status;\n }\n};/' /root/output/php.js; \
1127+
/root/replace-across-lines.sh 's/(class\s+ExitStatus\s*\{\s*name\s*=\s*"ExitStatus";\s*constructor\(\s*status\s*\)\s*\{\s*this\.message\s*=\s*`Program terminated with exit\(\$\{status\}\)`;\s*this.status\s*=\s*status;?\s*\}\s*\})/$1\nExitStatus = class PHPExitStatus extends Error {\nconstructor(status) {\n super(status);\n this.name = "ExitStatus";\n this.message = "Program terminated with exit(" + status + ")";\n this.status = status;\n }\n};/' /root/output/php.js; \
11271128
# Restore quit handling to Emscripten
11281129
# Quit callback customization was removed from Emscripten by:
11291130
# https://github.com/emscripten-core/emscripten/pull/22371

packages/php-wasm/compile/php/dns_polyfill.h

Lines changed: 0 additions & 33 deletions
This file was deleted.

packages/php-wasm/compile/php/php_wasm.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "rfc1867.h"
2626
#include "SAPI.h"
2727
#include "proc_open.h"
28-
#include "dns_polyfill.h"
2928

3029
// Created by Dockerfile:
3130
#include "php_wasm_asyncify.h"
@@ -790,18 +789,11 @@ EMSCRIPTEN_KEEPALIVE int __wrap_select(int max_fd, fd_set *read_fds, fd_set *wri
790789
extern int wasm_shutdown(int sockfd, int how);
791790
extern int wasm_close(int sockfd);
792791

793-
794792
static const zend_function_entry additional_functions[] = {
795793
ZEND_FE(dl, arginfo_dl)
796-
ZEND_FE(dns_get_mx, arginfo_dns_get_mx)
797-
ZEND_FALIAS(getmxrr, dns_get_mx, arginfo_getmxrr)
798-
ZEND_FALIAS(checkdnsrr, dns_check_record, arginfo_checkdnsrr)
799-
ZEND_FE(dns_check_record, arginfo_dns_check_record)
800-
ZEND_FE(dns_get_record, arginfo_dns_get_record)
801-
PHP_FE(cli_set_process_title, arginfo_cli_set_process_title)
802-
PHP_FE(cli_get_process_title, arginfo_cli_get_process_title)
803-
PHP_FE(post_message_to_js, arginfo_post_message_to_js){NULL, NULL, NULL}
804-
};
794+
PHP_FE(cli_set_process_title, arginfo_cli_set_process_title)
795+
PHP_FE(cli_get_process_title, arginfo_cli_get_process_title)
796+
PHP_FE(post_message_to_js, arginfo_post_message_to_js){NULL, NULL, NULL}};
805797

806798
typedef struct wasm_cli_arg
807799
{
@@ -845,13 +837,7 @@ int run_cli()
845837
#else
846838
static const zend_function_entry additional_functions[] = {
847839
ZEND_FE(dl, arginfo_dl)
848-
ZEND_FE(dns_get_mx, arginfo_dns_get_mx)
849-
ZEND_FALIAS(getmxrr, dns_get_mx, arginfo_getmxrr)
850-
ZEND_FALIAS(checkdnsrr, dns_check_record, arginfo_checkdnsrr)
851-
ZEND_FE(dns_check_record, arginfo_dns_check_record)
852-
ZEND_FE(dns_get_record, arginfo_dns_get_record)
853-
PHP_FE(post_message_to_js, arginfo_post_message_to_js){NULL, NULL, NULL}
854-
};
840+
PHP_FE(post_message_to_js, arginfo_post_message_to_js){NULL, NULL, NULL}};
855841
#endif
856842

857843
#if !defined(TSRMLS_DC)

0 commit comments

Comments
 (0)