@@ -24,6 +24,7 @@ local ngx_lua_ffi_balancer_set_more_tries
24
24
local ngx_lua_ffi_balancer_get_last_failure
25
25
local ngx_lua_ffi_balancer_set_timeouts -- used by both stream and http
26
26
local ngx_lua_ffi_balancer_set_upstream_tls
27
+ local ngx_lua_ffi_balancer_bind_to_local_addr
27
28
28
29
29
30
if subsystem == ' http' then
@@ -48,8 +49,13 @@ if subsystem == 'http' then
48
49
49
50
int ngx_http_lua_ffi_balancer_recreate_request (ngx_http_request_t * r ,
50
51
char ** err );
52
+
51
53
int ngx_http_lua_ffi_balancer_set_upstream_tls (ngx_http_request_t * r ,
52
54
int on , char ** err );
55
+
56
+ int ngx_http_lua_ffi_balancer_bind_to_local_addr (ngx_http_request_t * r ,
57
+ const u_char * addr , size_t addr_len ,
58
+ u_char * errbuf , size_t * errbuf_size );
53
59
]]
54
60
55
61
ngx_lua_ffi_balancer_set_current_peer =
@@ -70,6 +76,9 @@ if subsystem == 'http' then
70
76
ngx_lua_ffi_balancer_set_upstream_tls =
71
77
C .ngx_http_lua_ffi_balancer_set_upstream_tls
72
78
79
+ ngx_lua_ffi_balancer_bind_to_local_addr =
80
+ C .ngx_http_lua_ffi_balancer_bind_to_local_addr
81
+
73
82
elseif subsystem == ' stream' then
74
83
ffi .cdef [[
75
84
int ngx_stream_lua_ffi_balancer_set_current_peer (
@@ -353,5 +362,38 @@ if subsystem == 'http' then
353
362
end
354
363
end
355
364
365
+ if subsystem == " http" then
366
+ function _M .bind_to_local_addr (addr )
367
+ local r = get_request ()
368
+ if not r then
369
+ error (" no request found" )
370
+ end
371
+
372
+ if type (addr ) ~= " string" then
373
+ error (" bad argument #1 to 'bind_to_local_addr' "
374
+ .. " (string expected, got " .. type (addr ) .. " )" )
375
+ end
376
+
377
+ local errbuf_size = 1024
378
+ local errbuf = get_string_buf (errbuf_size )
379
+ local sizep = get_size_ptr ()
380
+ sizep [0 ] = errbuf_size
381
+ local rc = ngx_lua_ffi_balancer_bind_to_local_addr (r , addr , # addr ,
382
+ errbuf ,
383
+ sizep )
384
+ if rc == FFI_OK then
385
+ return true
386
+ end
387
+
388
+ return nil , ffi_str (errbuf , sizep [0 ])
389
+ end
390
+
391
+ else
392
+ function _M .bind_to_local_addr (addr )
393
+ error (" 'bind_to_local_addr' not yet implemented in " .. subsystem ..
394
+ " subsystem" , 2 )
395
+ end
396
+ end
397
+
356
398
357
399
return _M
0 commit comments