-
Notifications
You must be signed in to change notification settings - Fork 3
/
rdns.c
395 lines (337 loc) · 12.1 KB
/
rdns.c
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
+----------------------------------------------------------------------+
| Copyright (c) 2014 weheartwebsites UG |
+----------------------------------------------------------------------+
| This source file is subject to version 2 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
+----------------------------------------------------------------------+
| Authors: Alexander Solovets <[email protected]> |
| Eduardo Silva <[email protected]> |
| Gunter Grodotzki <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <pthread.h>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_rdns.h"
#include "rdns.h"
#include "rdns_ev.h"
#define RDNS_INIT_VARS \
zval *object = getThis(); \
php_rdns_t *i_obj = NULL;
#define RDNS_FETCH_OBJECT \
i_obj = (php_rdns_t *) zend_object_store_get_object( object TSRMLS_CC );
typedef struct {
zend_object obj;
zval *result;
struct ev_loop *loop;
struct rdns_resolver *resolver;
pthread_mutex_t mutex;
size_t nreq;
} php_rdns_t;
typedef struct {
php_rdns_t *rdns;
size_t index;
} php_rdns_context_t;
static zend_class_entry *rdns_ce = NULL;
static PHP_METHOD(RDNS, __construct);
static PHP_METHOD(RDNS, addServer);
static PHP_METHOD(RDNS, addRequest);
static PHP_METHOD(RDNS, getReplies);
ZEND_BEGIN_ARG_INFO_EX(arginfo_addServer, 0, 0, 1)
ZEND_ARG_INFO(0, server)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, prio)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_addRequest, 0, 0, 3)
ZEND_ARG_INFO(0, hostname)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, timeout)
ZEND_END_ARG_INFO()
static zend_function_entry rdns_class_methods[] = {
PHP_ME(RDNS, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(RDNS, addServer, arginfo_addServer, ZEND_ACC_PUBLIC)
PHP_ME(RDNS, addRequest, arginfo_addRequest, ZEND_ACC_PUBLIC)
PHP_ME(RDNS, getReplies, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/* }}} */
static void php_rdns_free(php_rdns_t *i_obj TSRMLS_DC)
{
zend_object_std_dtor(&i_obj->obj TSRMLS_CC);
rdns_resolver_release(i_obj->resolver);
ev_loop_destroy(i_obj->loop);
efree(i_obj);
}
static zend_object_value
php_rdns_new(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
php_rdns_t *i_obj;
zval *tmp;
i_obj = ecalloc(1, sizeof(*i_obj));
zend_object_std_init(&i_obj->obj, ce TSRMLS_CC );
retval.handle = zend_objects_store_put(i_obj,
(zend_objects_store_dtor_t)
zend_objects_destroy_object,
(zend_objects_free_object_storage_t)
php_rdns_free, NULL TSRMLS_CC);
retval.handlers = zend_get_std_object_handlers();
return retval;
}
/* {{{ PHP_MINIT_FUNCTION */
static PHP_MINIT_FUNCTION(rdns)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, PHP_RDNS_EXTNAME, rdns_class_methods);
rdns_ce = zend_register_internal_class(&ce TSRMLS_CC);
rdns_ce->create_object = php_rdns_new;
#define RDNS_TYPE_CONST(TYPE) \
REGISTER_LONG_CONSTANT("RDNS_"#TYPE, RDNS_REQUEST_##TYPE, \
CONST_CS | CONST_PERSISTENT);
RDNS_TYPE_CONST(A);
RDNS_TYPE_CONST(NS);
RDNS_TYPE_CONST(PTR);
RDNS_TYPE_CONST(MX);
RDNS_TYPE_CONST(TXT);
RDNS_TYPE_CONST(SRV);
RDNS_TYPE_CONST(AAAA);
RDNS_TYPE_CONST(SOA);
#undef RDNS_TYPE_CONST
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
static PHP_MINFO_FUNCTION(rdns)
{
php_info_print_table_start();
php_info_print_table_header(2, "RDNS Support", "enabled");
php_info_print_table_row(2, "RDNS Version", PHP_RDNS_VERSION);
php_info_print_table_row(2, "RDNS GitHub", "https://github.com/weheartwebsites/php-rdns");
php_info_print_table_row(2, "RDNS librdns", "$Id: 429e2100724fa709e64011951edee8d1e747d04e $");
php_info_print_table_end();
}
/* }}} */
// the following code creates an entry for the module and registers it with Zend.
zend_module_entry rdns_module_entry = {
#if ZEND_MODULE_API_NO >= 20050922
STANDARD_MODULE_HEADER_EX,
NULL,
NULL,
#else
STANDARD_MODULE_HEADER,
#endif
PHP_RDNS_EXTNAME,
NULL,
PHP_MINIT(rdns),
NULL, // SHUTDOWN
NULL, // name of the RINIT function or NULL if not applicable
NULL, // name of the RSHUTDOWN function or NULL if not applicable
PHP_MINFO(rdns),
PHP_RDNS_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_RDNS
ZEND_GET_MODULE(rdns)
#endif
static void rdns_log_cb(
void *data,
enum rdns_log_level level,
const char *function,
const char *format,
va_list args
)
{
/* TODO: implement */
}
/* {{{ RDNS::__construct() */
static PHP_METHOD(RDNS, __construct)
{
zval *object = getThis();
php_rdns_t *i_obj = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
ZVAL_NULL(object);
return;
}
i_obj = (php_rdns_t *) zend_object_store_get_object(object TSRMLS_CC);
if (!(i_obj->resolver = rdns_resolver_new())) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "could not create resolver structure");
}
rdns_resolver_set_logger(i_obj->resolver, rdns_log_cb, NULL);
pthread_mutex_init(&i_obj->mutex, 0);
i_obj->loop = ev_default_loop(0);
i_obj->nreq = 0;
}
/* }}} */
/* {{{ bool RDNS::addServer(string $server [, int $port = 53 [, int $prio = 0 ] ]) */
static PHP_METHOD(RDNS, addServer)
{
char *server;
size_t server_len;
long port = 53, prio = 0;
RDNS_INIT_VARS;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll",
&server, &server_len, &port, &prio) == FAILURE) {
return;
}
RDNS_FETCH_OBJECT;
RETURN_BOOL(rdns_resolver_add_server(i_obj->resolver,
server, port, prio, 8));
}
/* }}} */
static void
rdns_reply_callback(struct rdns_reply *reply, void *arg)
{
struct rdns_reply_entry *entry;
int ipstr_len = INET6_ADDRSTRLEN + 1;
char *ipstr;
zval *result, *result_item;
php_rdns_context_t *context = (php_rdns_context_t *) arg;
struct rdns_request_name const *name =
rdns_request_get_name(reply->request, NULL);
MAKE_STD_ZVAL(result);
array_init(result);
if (reply->code == RDNS_RC_NOERROR) {
entry = reply->entries;
ipstr = malloc(ipstr_len);
while (entry != NULL) {
MAKE_STD_ZVAL(result_item);
array_init(result_item);
add_assoc_string_ex(result_item, ZEND_STRS("host"),
name->name, 1);
add_assoc_string_ex(result_item, ZEND_STRS("class"), "IN", 1);
add_assoc_long(result_item, "ttl", entry->ttl);
if (entry->type == RDNS_REQUEST_A) {
add_assoc_string_ex(result_item, ZEND_STRS("type"), "A", 1);
inet_ntop (AF_INET, &entry->content.a.addr, ipstr, ipstr_len);
add_assoc_string_ex(result_item, ZEND_STRS("ip"), ipstr, 1);
} else if (entry->type == RDNS_REQUEST_AAAA) {
add_assoc_string_ex(result_item, ZEND_STRS("type"), "AAAA", 1);
inet_ntop (AF_INET6, &entry->content.aaa.addr, ipstr, ipstr_len);
add_assoc_string_ex(result_item, ZEND_STRS("ipv6"), ipstr, 1);
} else if (entry->type == RDNS_REQUEST_MX) {
add_assoc_string_ex(result_item, ZEND_STRS("type"), "MX", 1);
add_assoc_long(result_item, "pri", entry->content.mx.priority);
add_assoc_string_ex(result_item, ZEND_STRS("target"),
entry->content.mx.name, 1);
} else if (entry->type == RDNS_REQUEST_NS) {
add_assoc_string_ex(result_item, ZEND_STRS("type"), "NS", 1);
add_assoc_string_ex(result_item, ZEND_STRS("target"),
entry->content.ns.name, 1);
} else if (entry->type == RDNS_REQUEST_PTR) {
add_assoc_string_ex(result_item, ZEND_STRS("type"), "PTR", 1);
add_assoc_string_ex(result_item, ZEND_STRS("target"),
entry->content.ptr.name, 1);
} else if (entry->type == RDNS_REQUEST_TXT) {
add_assoc_string_ex(result_item, ZEND_STRS("type"), "TXT", 1);
add_assoc_string_ex(result_item, ZEND_STRS("txt"),
entry->content.txt.data, 1);
} else if (entry->type == RDNS_REQUEST_SRV) {
add_assoc_string_ex(result_item, ZEND_STRS("type"), "SRV", 1);
add_assoc_string_ex(result_item, ZEND_STRS("target"),
entry->content.srv.target, 1);
add_assoc_long(result_item, "pri", entry->content.srv.priority);
add_assoc_long(result_item, "weight", entry->content.srv.weight);
add_assoc_long(result_item, "port", entry->content.srv.port);
} else if (entry->type == RDNS_REQUEST_SOA) {
add_assoc_string_ex(result_item, ZEND_STRS("type"), "SOA", 1);
add_assoc_string_ex(result_item, ZEND_STRS("mname"),
entry->content.soa.mname, 1);
add_assoc_string_ex(result_item, ZEND_STRS("rname"),
entry->content.soa.admin, 1);
add_assoc_long(result_item, "serial", entry->content.soa.serial);
add_assoc_long(result_item, "refresh", entry->content.soa.refresh);
add_assoc_long(result_item, "retry", entry->content.soa.retry);
add_assoc_long(result_item, "expire", entry->content.soa.expire);
add_assoc_long(result_item, "minimum-ttl",
entry->content.soa.minimum);
}
entry = entry->next;
add_next_index_zval(result, result_item);
}
free(ipstr);
}
add_index_zval(context->rdns->result, context->index, result);
if (--context->rdns->nreq == 0) {
/* rdns_resolver_release(context->rdns->resolver); */
ev_break(context->rdns->loop, EVBREAK_ALL);
}
}
/* {{{ bool RDNS::addRequest(string $hostname, int $type, float $timeout) */
static PHP_METHOD(RDNS, addRequest)
{
php_rdns_context_t *context;
char *hostname;
size_t len;
long type;
double timeout = 5;
RDNS_INIT_VARS;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"sl|d",
&hostname,
&len,
&type,
&timeout) == FAILURE) {
return;
}
RDNS_FETCH_OBJECT;
context = (php_rdns_context_t *) malloc(sizeof *context);
context->rdns = i_obj;
pthread_mutex_lock(&i_obj->mutex);
context->index = i_obj->nreq++;
pthread_mutex_unlock(&i_obj->mutex);
if (context->index == 0) {
rdns_bind_libev(i_obj->resolver, i_obj->loop);
if (!rdns_resolver_init(i_obj->resolver)) {
RETURN_FALSE;
}
}
RETURN_BOOL(rdns_make_request_full(i_obj->resolver, rdns_reply_callback,
context, timeout, 1, 1,
hostname, type));
}
/* }}} */
static int ksort(zval *array)
{
/* zval *params = { array }; */
/* zval *func; */
/* zval *retval; */
/* MAKE_STD_ZVAL(func); */
/* ZVAL_STRING(func, "ksort", 0); */
/* MAKE_STD_ZVAL(retval); */
/* call_user_function( */
/* NULL, */
/* NULL, */
/* func, */
/* retval, */
/* 1, */
/* &array TSRMLS_CC */
/* ); */
/* FREE_ZVAL(func); */
/* FREE_ZVAL(retval); */
return 0;
}
/* {{{ array RDNS::getReplies() */
static PHP_METHOD(RDNS, getReplies)
{
RDNS_INIT_VARS;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
RDNS_FETCH_OBJECT;
array_init(return_value);
i_obj->result = return_value;
pthread_mutex_lock(&i_obj->mutex);
if (i_obj->nreq > 0) {
ev_run(i_obj->loop, 0);
ksort(return_value);
}
pthread_mutex_unlock(&i_obj->mutex);
}
/* }}} */