-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathngx_http_lua_resolver.c
More file actions
367 lines (282 loc) · 10.1 KB
/
ngx_http_lua_resolver.c
File metadata and controls
367 lines (282 loc) · 10.1 KB
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
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_contentby.h"
typedef struct ngx_http_lua_resolver_ctx_s {
ngx_http_request_t *request;
u_char *buf;
size_t *buf_size;
ngx_http_lua_co_ctx_t *curr_co_ctx;
ngx_resolver_ctx_t *resolver_ctx;
ngx_int_t exit_code;
unsigned ipv4:1;
unsigned ipv6:1;
} ngx_http_lua_resolver_ctx_t;
static void ngx_http_lua_resolve_handler(ngx_resolver_ctx_t *rctx);
static void ngx_http_lua_resolve_cleanup(void *data);
static void ngx_http_lua_resolve_empty_handler(ngx_resolver_ctx_t *ctx);
static ngx_int_t ngx_http_lua_resolve_resume(ngx_http_request_t *r);
static int ngx_http_lua_resolve_retval(ngx_http_lua_resolver_ctx_t *ctx,
lua_State *L);
int
ngx_http_lua_ffi_resolve(ngx_http_lua_resolver_ctx_t *ctx, u_char *hostname)
{
u_char *buf;
size_t *buf_size;
lua_State *L;
ngx_str_t host;
ngx_http_lua_ctx_t *lctx;
ngx_http_request_t *r;
ngx_resolver_ctx_t *rctx;
ngx_http_lua_co_ctx_t *coctx;
ngx_http_core_loc_conf_t *clcf;
#if (NGX_HAVE_INET6)
struct in6_addr inaddr6;
#endif
buf = ctx->buf;
buf_size = ctx->buf_size;
r = ctx->request;
if (r == NULL) {
*ctx->buf_size = ngx_snprintf(buf, *buf_size, "no request") - buf;
return NGX_ERROR;
}
lctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (lctx == NULL) {
*buf_size = ngx_snprintf(buf, *buf_size, "no lua ctx") - buf;
return NGX_ERROR;
}
L = ngx_http_lua_get_lua_vm(r, lctx);
ngx_http_lua_check_context(L, lctx, NGX_HTTP_LUA_CONTEXT_REWRITE
| NGX_HTTP_LUA_CONTEXT_ACCESS
| NGX_HTTP_LUA_CONTEXT_CONTENT
| NGX_HTTP_LUA_CONTEXT_TIMER
| NGX_HTTP_LUA_CONTEXT_SSL_CERT
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH);
host.data = hostname;
host.len = ngx_strlen(hostname);
if (ngx_inet_addr(host.data, host.len) != INADDR_NONE
#if (NGX_HAVE_INET6)
|| ngx_inet6_addr(host.data, host.len, inaddr6.s6_addr) == NGX_OK
#endif
)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"given hostname contains a network address");
*buf_size = ngx_cpystrn(buf, host.data, host.len + 1) - buf;
return NGX_OK;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
rctx = ngx_resolve_start(clcf->resolver, NULL);
if (rctx == NULL) {
*buf_size = ngx_snprintf(buf, *buf_size, "failed to start the resolver")
- buf;
return NGX_ERROR;
}
if (rctx == NGX_NO_RESOLVER) {
*buf_size = ngx_snprintf(buf, *buf_size, "no resolver defined "
"to resolve \"%s\"", host.data)
- buf;
return NGX_ERROR;
}
rctx->name = host;
rctx->handler = ngx_http_lua_resolve_handler;
rctx->data = ctx;
rctx->timeout = clcf->resolver_timeout;
ctx->resolver_ctx = rctx;
ctx->curr_co_ctx = lctx->cur_co_ctx;
coctx = lctx->cur_co_ctx;
ngx_http_lua_cleanup_pending_operation(coctx);
coctx->cleanup = ngx_http_lua_resolve_cleanup;
coctx->data = ctx;
if (ngx_resolve_name(rctx) != NGX_OK) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua resolver fail to run resolver immediately");
coctx->cleanup = NULL;
coctx->data = NULL;
ctx->resolver_ctx = NULL;
*buf_size = ngx_snprintf(buf, *buf_size, "%s could not be resolved",
host.data)
- buf;
return NGX_ERROR;
}
if (rctx->async) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"resolution is going to be performed asynchronously");
return NGX_DONE;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"resolution was finished with code %d", ctx->exit_code);
return ctx->exit_code;
}
static void
ngx_http_lua_resolve_handler(ngx_resolver_ctx_t *rctx)
{
u_char *buf;
size_t *buf_size;
unsigned async;
ngx_uint_t i;
ngx_connection_t *c;
ngx_http_request_t *r;
ngx_http_lua_ctx_t *lctx;
ngx_resolver_addr_t *addr;
ngx_http_lua_resolver_ctx_t *ctx;
ctx = rctx->data;
buf = ctx->buf;
buf_size = ctx->buf_size;
r = ctx->request;
c = r->connection;
async = rctx->async;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "lua ngx resolve handler");
lctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (lctx == NULL) {
return;
}
lctx->cur_co_ctx = ctx->curr_co_ctx;
ctx->curr_co_ctx->cleanup = NULL;
if (rctx->state) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "resolve error: %s "
"(async:%d)", ngx_resolver_strerror(rctx->state), async);
*buf_size = ngx_snprintf(buf, *buf_size, "%s could not be resolved "
"(%d: %s)", rctx->name.data, (int) rctx->state,
ngx_resolver_strerror(rctx->state))
- buf;
goto failed;
}
#if (NGX_DEBUG)
{
u_char text[NGX_SOCKADDR_STRLEN];
ngx_str_t tmp;
tmp.data = text;
for (i = 0; i < rctx->naddrs; i++) {
tmp.len = ngx_sock_ntop(rctx->addrs[i].sockaddr,
rctx->addrs[i].socklen,
text, NGX_SOCKADDR_STRLEN, 0);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"name was resolved to %V", &tmp);
}
}
#endif
/* NG: On a cache hit, Nginx resolver performs random rotation of
* addrs list on every invocation, so, here we just looking for
* the first one that satisfies the requirements.
* See ngx_resolve_name_locked()/ngx_resolver_export() for details */
for (i = 0; i < rctx->naddrs; i++) {
addr = &rctx->addrs[i];
if ((addr->sockaddr->sa_family == AF_INET && ctx->ipv4)
#if (NGX_HAVE_INET6)
|| (addr->sockaddr->sa_family == AF_INET6 && ctx->ipv6)
#endif
)
{
*buf_size = ngx_sock_ntop(addr->sockaddr, addr->socklen, ctx->buf,
*buf_size, 0);
goto done;
}
}
*buf_size = ngx_snprintf(buf, *buf_size, "no suitable address found for "
"%s out of %d received", rctx->name.data,
rctx->naddrs)
- buf;
failed:
ctx->exit_code = NGX_ERROR;
done:
ngx_resolve_name_done(rctx);
ctx->resolver_ctx = NULL;
ctx->curr_co_ctx = NULL;
if (async) {
if (lctx->entered_content_phase) {
(void) ngx_http_lua_resolve_resume(r);
} else {
lctx->resume_handler = ngx_http_lua_resolve_resume;
ngx_http_core_run_phases(r);
}
ngx_http_run_posted_requests(c);
}
}
static ngx_int_t
ngx_http_lua_resolve_resume(ngx_http_request_t *r)
{
int nret;
lua_State *vm;
ngx_int_t rc;
ngx_uint_t nreqs;
ngx_connection_t *c;
ngx_http_lua_ctx_t *lctx;
ngx_http_lua_co_ctx_t *coctx;
ngx_http_lua_resolver_ctx_t *ctx;
lctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (lctx == NULL) {
return NGX_ERROR;
}
lctx->resume_handler = ngx_http_lua_wev_handler;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua resolve operation is done, resuming lua thread");
coctx = lctx->cur_co_ctx;
dd("coctx: %p", coctx);
ctx = coctx->data;
nret = ngx_http_lua_resolve_retval(ctx, lctx->cur_co_ctx->co);
c = r->connection;
vm = ngx_http_lua_get_lua_vm(r, lctx);
nreqs = c->requests;
rc = ngx_http_lua_run_thread(vm, r, lctx, nret);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua run thread returned %d", rc);
if (rc == NGX_AGAIN) {
return ngx_http_lua_run_posted_threads(c, vm, r, lctx, nreqs);
}
if (rc == NGX_DONE) {
ngx_http_lua_finalize_request(r, NGX_DONE);
return ngx_http_lua_run_posted_threads(c, vm, r, lctx, nreqs);
}
if (lctx->entered_content_phase) {
ngx_http_lua_finalize_request(r, rc);
return NGX_DONE;
}
return rc;
}
static void
ngx_http_lua_resolve_cleanup(void *data)
{
ngx_resolver_ctx_t *rctx;
ngx_http_lua_co_ctx_t *coctx;
ngx_http_lua_resolver_ctx_t *ctx;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua ngx.resolve abort resolver");
coctx = data;
ctx = coctx->data;
rctx = ctx->resolver_ctx;
if (rctx == NULL) {
return;
}
/* just to be safer */
rctx->handler = ngx_http_lua_resolve_empty_handler;
ngx_resolve_name_done(rctx);
}
static void
ngx_http_lua_resolve_empty_handler(ngx_resolver_ctx_t *ctx)
{
/* do nothing */
}
static int
ngx_http_lua_resolve_retval(ngx_http_lua_resolver_ctx_t *ctx, lua_State *L)
{
if (ctx->exit_code == NGX_OK) {
lua_pushlstring(L, (const char *) ctx->buf, *ctx->buf_size);
return 1;
}
lua_pushnil(L);
lua_pushlstring(L, (const char *) ctx->buf, *ctx->buf_size);
return 2;
}
void
ngx_http_lua_ffi_resolver_destroy(ngx_http_lua_resolver_ctx_t *ctx)
{
if (ctx->resolver_ctx == NULL) {
return;
}
ngx_resolve_name_done(ctx->resolver_ctx);
ctx->resolver_ctx = NULL;
}
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */