11use std:: ffi:: { c_int, c_void} ;
2- use std:: ptr:: addr_of;
2+ use std:: ptr:: { addr_of, NonNull } ;
33
44use ngx:: core;
55use ngx:: ffi:: {
@@ -47,29 +47,33 @@ impl NgxHttpOrigDstCtx {
4747 }
4848
4949 pub unsafe fn bind_addr ( & self , v : * mut ngx_variable_value_t ) {
50+ let mut v = NonNull :: new ( v) . unwrap ( ) ;
51+ let v = unsafe { v. as_mut ( ) } ;
5052 if self . orig_dst_addr . len == 0 {
51- ( * v ) . set_not_found ( 1 ) ;
53+ v . set_not_found ( 1 ) ;
5254 return ;
5355 }
5456
55- ( * v ) . set_valid ( 1 ) ;
56- ( * v ) . set_no_cacheable ( 0 ) ;
57- ( * v ) . set_not_found ( 0 ) ;
58- ( * v ) . set_len ( self . orig_dst_addr . len as u32 ) ;
59- ( * v ) . data = self . orig_dst_addr . data ;
57+ v . set_valid ( 1 ) ;
58+ v . set_no_cacheable ( 0 ) ;
59+ v . set_not_found ( 0 ) ;
60+ v . set_len ( self . orig_dst_addr . len as u32 ) ;
61+ v . data = self . orig_dst_addr . data ;
6062 }
6163
6264 pub unsafe fn bind_port ( & self , v : * mut ngx_variable_value_t ) {
65+ let mut v = NonNull :: new ( v) . unwrap ( ) ;
66+ let v = unsafe { v. as_mut ( ) } ;
6367 if self . orig_dst_port . len == 0 {
64- ( * v ) . set_not_found ( 1 ) ;
68+ v . set_not_found ( 1 ) ;
6569 return ;
6670 }
6771
68- ( * v ) . set_valid ( 1 ) ;
69- ( * v ) . set_no_cacheable ( 0 ) ;
70- ( * v ) . set_not_found ( 0 ) ;
71- ( * v ) . set_len ( self . orig_dst_port . len as u32 ) ;
72- ( * v ) . data = self . orig_dst_port . data ;
72+ v . set_valid ( 1 ) ;
73+ v . set_no_cacheable ( 0 ) ;
74+ v . set_not_found ( 0 ) ;
75+ v . set_len ( self . orig_dst_port . len as u32 ) ;
76+ v . data = self . orig_dst_port . data ;
7377 }
7478}
7579
@@ -129,19 +133,21 @@ unsafe fn ngx_get_origdst(
129133) -> Result < ( String , in_port_t ) , core:: Status > {
130134 let c = request. connection ( ) ;
131135
132- if ( * c) . type_ != libc:: SOCK_STREAM {
136+ if unsafe { ( * c) . type_ } != libc:: SOCK_STREAM {
133137 ngx_log_debug_http ! ( request, "httporigdst: connection is not type SOCK_STREAM" ) ;
134138 return Err ( core:: Status :: NGX_DECLINED ) ;
135139 }
136140
137- if ngx_connection_local_sockaddr ( c, std:: ptr:: null_mut ( ) , 0 ) != core:: Status :: NGX_OK . into ( ) {
141+ if unsafe { ngx_connection_local_sockaddr ( c, std:: ptr:: null_mut ( ) , 0 ) }
142+ != core:: Status :: NGX_OK . into ( )
143+ {
138144 ngx_log_debug_http ! ( request, "httporigdst: no local sockaddr from connection" ) ;
139145 return Err ( core:: Status :: NGX_ERROR ) ;
140146 }
141147
142148 let level: c_int ;
143149 let optname: c_int ;
144- match ( * ( * c) . local_sockaddr ) . sa_family as i32 {
150+ match unsafe { ( * ( * c) . local_sockaddr ) . sa_family } as i32 {
145151 libc:: AF_INET => {
146152 level = libc:: SOL_IP ;
147153 optname = libc:: SO_ORIGINAL_DST ;
@@ -152,15 +158,17 @@ unsafe fn ngx_get_origdst(
152158 }
153159 }
154160
155- let mut addr: sockaddr_storage = { std:: mem:: zeroed ( ) } ;
161+ let mut addr: sockaddr_storage = unsafe { std:: mem:: zeroed ( ) } ;
156162 let mut addrlen: libc:: socklen_t = std:: mem:: size_of_val ( & addr) as libc:: socklen_t ;
157- let rc = libc:: getsockopt (
158- ( * c) . fd ,
159- level,
160- optname,
161- & mut addr as * mut _ as * mut _ ,
162- & mut addrlen as * mut u32 ,
163- ) ;
163+ let rc = unsafe {
164+ libc:: getsockopt (
165+ ( * c) . fd ,
166+ level,
167+ optname,
168+ & mut addr as * mut _ as * mut _ ,
169+ & mut addrlen as * mut u32 ,
170+ )
171+ } ;
164172 if rc == -1 {
165173 ngx_log_debug_http ! ( request, "httporigdst: getsockopt failed" ) ;
166174 return Err ( core:: Status :: NGX_DECLINED ) ;
@@ -192,10 +200,11 @@ unsafe fn ngx_get_origdst(
192200http_variable_get ! (
193201 ngx_http_orig_dst_addr_variable,
194202 |request: & mut http:: Request , v: * mut ngx_variable_value_t, _: usize | {
195- let ctx = request. get_module_ctx:: <NgxHttpOrigDstCtx >( & * addr_of!( ngx_http_orig_dst_module) ) ;
203+ let ctx = request
204+ . get_module_ctx:: <NgxHttpOrigDstCtx >( unsafe { & * addr_of!( ngx_http_orig_dst_module) } ) ;
196205 if let Some ( obj) = ctx {
197206 ngx_log_debug_http!( request, "httporigdst: found context and binding variable" , ) ;
198- obj. bind_addr( v) ;
207+ unsafe { obj. bind_addr( v) } ;
199208 return core:: Status :: NGX_OK ;
200209 }
201210 // lazy initialization:
@@ -204,7 +213,7 @@ http_variable_get!(
204213 // set context
205214 // bind address
206215 ngx_log_debug_http!( request, "httporigdst: context not found, getting address" ) ;
207- let r = ngx_get_origdst( request) ;
216+ let r = unsafe { ngx_get_origdst( request) } ;
208217 match r {
209218 Err ( e) => {
210219 return e;
@@ -226,10 +235,11 @@ http_variable_get!(
226235 ip,
227236 port,
228237 ) ;
229- ( * new_ctx) . save( & ip, port, & request. pool( ) ) ;
230- ( * new_ctx) . bind_addr( v) ;
231- request
232- . set_module_ctx( new_ctx as * mut c_void, & * addr_of!( ngx_http_orig_dst_module) ) ;
238+ unsafe { ( * new_ctx) . save( & ip, port, & request. pool( ) ) } ;
239+ unsafe { ( * new_ctx) . bind_addr( v) } ;
240+ request. set_module_ctx( new_ctx as * mut c_void, unsafe {
241+ & * addr_of!( ngx_http_orig_dst_module)
242+ } ) ;
233243 }
234244 }
235245 core:: Status :: NGX_OK
@@ -239,10 +249,11 @@ http_variable_get!(
239249http_variable_get ! (
240250 ngx_http_orig_dst_port_variable,
241251 |request: & mut http:: Request , v: * mut ngx_variable_value_t, _: usize | {
242- let ctx = request. get_module_ctx:: <NgxHttpOrigDstCtx >( & * addr_of!( ngx_http_orig_dst_module) ) ;
252+ let ctx = request
253+ . get_module_ctx:: <NgxHttpOrigDstCtx >( unsafe { & * addr_of!( ngx_http_orig_dst_module) } ) ;
243254 if let Some ( obj) = ctx {
244255 ngx_log_debug_http!( request, "httporigdst: found context and binding variable" , ) ;
245- obj. bind_port( v) ;
256+ unsafe { obj. bind_port( v) } ;
246257 return core:: Status :: NGX_OK ;
247258 }
248259 // lazy initialization:
@@ -251,7 +262,7 @@ http_variable_get!(
251262 // set context
252263 // bind port
253264 ngx_log_debug_http!( request, "httporigdst: context not found, getting address" ) ;
254- let r = ngx_get_origdst( request) ;
265+ let r = unsafe { ngx_get_origdst( request) } ;
255266 match r {
256267 Err ( e) => {
257268 return e;
@@ -273,10 +284,11 @@ http_variable_get!(
273284 ip,
274285 port,
275286 ) ;
276- ( * new_ctx) . save( & ip, port, & request. pool( ) ) ;
277- ( * new_ctx) . bind_port( v) ;
278- request
279- . set_module_ctx( new_ctx as * mut c_void, & * addr_of!( ngx_http_orig_dst_module) ) ;
287+ unsafe { ( * new_ctx) . save( & ip, port, & request. pool( ) ) } ;
288+ unsafe { ( * new_ctx) . bind_port( v) } ;
289+ request. set_module_ctx( new_ctx as * mut c_void, unsafe {
290+ & * addr_of!( ngx_http_orig_dst_module)
291+ } ) ;
280292 }
281293 }
282294 core:: Status :: NGX_OK
@@ -292,13 +304,15 @@ impl HttpModule for Module {
292304
293305 // static ngx_int_t ngx_http_orig_dst_add_variables(ngx_conf_t *cf)
294306 unsafe extern "C" fn preconfiguration ( cf : * mut ngx_conf_t ) -> ngx_int_t {
295- for mut v in NGX_HTTP_ORIG_DST_VARS {
296- let var = ngx_http_add_variable ( cf, & mut v. name , v. flags ) ;
297- if var. is_null ( ) {
307+ for mut v in unsafe { NGX_HTTP_ORIG_DST_VARS } {
308+ let var = NonNull :: new ( unsafe { ngx_http_add_variable ( cf, & mut v. name , v. flags ) } ) ;
309+ if var. is_none ( ) {
298310 return core:: Status :: NGX_ERROR . into ( ) ;
299311 }
300- ( * var) . get_handler = v. get_handler ;
301- ( * var) . data = v. data ;
312+ let mut var = var. unwrap ( ) ;
313+ let var = unsafe { var. as_mut ( ) } ;
314+ var. get_handler = v. get_handler ;
315+ var. data = v. data ;
302316 }
303317 core:: Status :: NGX_OK . into ( )
304318 }
0 commit comments