1
1
local base = require " resty.core.base"
2
- base .allows_subsystem (" http" )
2
+ -- base.allows_subsystem("http")
3
3
local debug = require " debug"
4
4
local ffi = require " ffi"
5
+ local subsystem = ngx .config .subsystem
5
6
6
7
7
8
local error = error
@@ -30,9 +31,13 @@ local option_index = {
30
31
[" tcp-nodelay" ] = 3 ,
31
32
[" sndbuf" ] = 4 ,
32
33
[" rcvbuf" ] = 5 ,
34
+ [" ip-transparent" ] = 6 ,
33
35
}
34
36
35
37
38
+ local ngx_lua_ffi_socket_tcp_getoption
39
+ local ngx_lua_ffi_socket_tcp_setoption
40
+ if subsystem == ' http' then
36
41
ffi .cdef [[
37
42
typedef struct ngx_http_lua_socket_tcp_upstream_s
38
43
ngx_http_lua_socket_tcp_upstream_t ;
60
65
ngx_http_lua_ffi_ssl_free_session (void * sess );
61
66
]]
62
67
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
+
63
89
64
90
local output_value_buf = ffi_new (" int[1]" )
65
91
local ERR_BUF_SIZE = 4096
@@ -73,6 +99,7 @@ local FFI_NO_REQ_CTX = base.FFI_NO_REQ_CTX
73
99
local SOCKET_CTX_INDEX = 1
74
100
local SOCKET_CLIENT_CERT_INDEX = 6
75
101
local SOCKET_CLIENT_PKEY_INDEX = 7
102
+ local SOCKET_IP_TRANSPARENT_INDEX = 9
76
103
77
104
78
105
local function get_tcp_socket (cosocket )
@@ -84,7 +111,6 @@ local function get_tcp_socket(cosocket)
84
111
return tcp_socket
85
112
end
86
113
87
-
88
114
local function getoption (cosocket , option )
89
115
local tcp_socket = get_tcp_socket (cosocket )
90
116
@@ -100,11 +126,11 @@ local function getoption(cosocket, option)
100
126
local errlen = get_size_ptr ()
101
127
errlen [0 ] = ERR_BUF_SIZE
102
128
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 )
108
134
if rc ~= FFI_OK then
109
135
return nil , ffi_str (err , errlen [0 ])
110
136
end
@@ -113,8 +139,8 @@ local function getoption(cosocket, option)
113
139
end
114
140
115
141
142
+
116
143
local function setoption (cosocket , option , value )
117
- local tcp_socket = get_tcp_socket (cosocket )
118
144
119
145
if option == nil then
120
146
return nil , ' missing the "option" argument'
@@ -124,6 +150,12 @@ local function setoption(cosocket, option, value)
124
150
return nil , ' missing the "value" argument'
125
151
end
126
152
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 )
127
159
if option_index [option ] == nil then
128
160
return nil , " unsupported option " .. tostring (option )
129
161
end
@@ -132,11 +164,11 @@ local function setoption(cosocket, option, value)
132
164
local errlen = get_size_ptr ()
133
165
errlen [0 ] = ERR_BUF_SIZE
134
166
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 )
140
172
if rc ~= FFI_OK then
141
173
return nil , ffi_str (err , errlen [0 ])
142
174
end
@@ -145,6 +177,7 @@ local function setoption(cosocket, option, value)
145
177
end
146
178
147
179
180
+ if subsystem == ' http' then
148
181
local errmsg = base .get_errmsg_ptr ()
149
182
local session_ptr = ffi_new (" void *[1]" )
150
183
local server_name_str = ffi_new (" ngx_str_t[1]" )
268
301
method_table .sslhandshake = sslhandshake
269
302
end
270
303
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
271
311
272
312
return { version = base .version }
0 commit comments