Skip to content

Commit eac2a10

Browse files
feature: add bind support for the stream subsystem.
1 parent e846dc4 commit eac2a10

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

lib/resty/core.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ if subsystem == 'http' then
1919
require "resty.core.response"
2020
require "resty.core.phase"
2121
require "resty.core.ndk"
22-
require "resty.core.socket"
2322
require "resty.core.coroutine"
2423
require "resty.core.param"
2524
end
2625

27-
26+
require "resty.core.socket"
2827
require "resty.core.misc"
2928
require "resty.core.ctx"
3029

lib/resty/core/socket.lua

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
local base = require "resty.core.base"
2-
base.allows_subsystem("http")
2+
-- base.allows_subsystem("http")
33
local debug = require "debug"
44
local ffi = require "ffi"
5+
local subsystem = ngx.config.subsystem
56

67

78
local error = error
@@ -30,9 +31,13 @@ local option_index = {
3031
["tcp-nodelay"] = 3,
3132
["sndbuf"] = 4,
3233
["rcvbuf"] = 5,
34+
["ip-transparent"] = 6,
3335
}
3436

3537

38+
local ngx_lua_ffi_socket_tcp_getoption
39+
local ngx_lua_ffi_socket_tcp_setoption
40+
if subsystem == 'http' then
3641
ffi.cdef[[
3742
typedef struct ngx_http_lua_socket_tcp_upstream_s
3843
ngx_http_lua_socket_tcp_upstream_t;
@@ -60,6 +65,27 @@ void
6065
ngx_http_lua_ffi_ssl_free_session(void *sess);
6166
]]
6267

68+
ngx_lua_ffi_socket_tcp_getoption = C.ngx_http_lua_ffi_socket_tcp_getoption
69+
ngx_lua_ffi_socket_tcp_setoption = C.ngx_http_lua_ffi_socket_tcp_setoption
70+
71+
elseif subsystem == 'stream' then
72+
73+
ffi.cdef[[
74+
typedef struct ngx_stream_lua_socket_tcp_upstream_s
75+
ngx_stream_lua_socket_tcp_upstream_t;
76+
77+
int
78+
ngx_stream_lua_ffi_socket_tcp_getoption(ngx_stream_lua_socket_tcp_upstream_t *u,
79+
int option, int *val, unsigned char *err, size_t *errlen);
80+
int
81+
ngx_stream_lua_ffi_socket_tcp_setoption(ngx_stream_lua_socket_tcp_upstream_t *u,
82+
int opt, int val, unsigned char *err, size_t *errlen);
83+
]]
84+
85+
ngx_lua_ffi_socket_tcp_getoption = C.ngx_stream_lua_ffi_socket_tcp_getoption
86+
ngx_lua_ffi_socket_tcp_setoption = C.ngx_stream_lua_ffi_socket_tcp_setoption
87+
end
88+
6389

6490
local output_value_buf = ffi_new("int[1]")
6591
local ERR_BUF_SIZE = 4096
@@ -73,6 +99,7 @@ local FFI_NO_REQ_CTX = base.FFI_NO_REQ_CTX
7399
local SOCKET_CTX_INDEX = 1
74100
local SOCKET_CLIENT_CERT_INDEX = 6
75101
local SOCKET_CLIENT_PKEY_INDEX = 7
102+
local SOCKET_IP_TRANSPARENT_INDEX = 9
76103

77104

78105
local function get_tcp_socket(cosocket)
@@ -84,7 +111,6 @@ local function get_tcp_socket(cosocket)
84111
return tcp_socket
85112
end
86113

87-
88114
local function getoption(cosocket, option)
89115
local tcp_socket = get_tcp_socket(cosocket)
90116

@@ -100,11 +126,11 @@ local function getoption(cosocket, option)
100126
local errlen = get_size_ptr()
101127
errlen[0] = ERR_BUF_SIZE
102128

103-
local rc = C.ngx_http_lua_ffi_socket_tcp_getoption(tcp_socket,
104-
option_index[option],
105-
output_value_buf,
106-
err,
107-
errlen)
129+
local rc = ngx_lua_ffi_socket_tcp_getoption(tcp_socket,
130+
option_index[option],
131+
output_value_buf,
132+
err,
133+
errlen)
108134
if rc ~= FFI_OK then
109135
return nil, ffi_str(err, errlen[0])
110136
end
@@ -113,8 +139,8 @@ local function getoption(cosocket, option)
113139
end
114140

115141

142+
116143
local function setoption(cosocket, option, value)
117-
local tcp_socket = get_tcp_socket(cosocket)
118144

119145
if option == nil then
120146
return nil, 'missing the "option" argument'
@@ -124,6 +150,12 @@ local function setoption(cosocket, option, value)
124150
return nil, 'missing the "value" argument'
125151
end
126152

153+
if option == "ip-transparent" then
154+
cosocket[SOCKET_IP_TRANSPARENT_INDEX] = value
155+
return true
156+
end
157+
158+
local tcp_socket = get_tcp_socket(cosocket)
127159
if option_index[option] == nil then
128160
return nil, "unsupported option " .. tostring(option)
129161
end
@@ -132,11 +164,11 @@ local function setoption(cosocket, option, value)
132164
local errlen = get_size_ptr()
133165
errlen[0] = ERR_BUF_SIZE
134166

135-
local rc = C.ngx_http_lua_ffi_socket_tcp_setoption(tcp_socket,
136-
option_index[option],
137-
value,
138-
err,
139-
errlen)
167+
local rc = ngx_lua_ffi_socket_tcp_setoption(tcp_socket,
168+
option_index[option],
169+
value,
170+
err,
171+
errlen)
140172
if rc ~= FFI_OK then
141173
return nil, ffi_str(err, errlen[0])
142174
end
@@ -145,6 +177,7 @@ local function setoption(cosocket, option, value)
145177
end
146178

147179

180+
if subsystem == 'http' then
148181
local errmsg = base.get_errmsg_ptr()
149182
local session_ptr = ffi_new("void *[1]")
150183
local server_name_str = ffi_new("ngx_str_t[1]")
@@ -268,5 +301,12 @@ do
268301
method_table.sslhandshake = sslhandshake
269302
end
270303

304+
elseif subsystem == 'stream' then
305+
do
306+
local method_table = registry.__tcp_cosocket_mt
307+
method_table.getoption = getoption
308+
method_table.setoption = setoption
309+
end
310+
end
271311

272312
return { version = base.version }

0 commit comments

Comments
 (0)