@@ -118,56 +118,45 @@ ngx_http_modsecurity_rewrite_handler_internal(ngx_http_request_t *r)
118
118
static ngx_int_t
119
119
ngx_http_modsecurity_process_connection (ngx_http_request_t * r )
120
120
{
121
+ in_port_t client_port , server_port ;
121
122
ngx_int_t rc ;
123
+ ngx_str_t client_addr , server_addr ;
122
124
ngx_pool_t * old_pool ;
123
125
ngx_connection_t * c ;
124
126
ngx_http_modsecurity_ctx_t * ctx ;
127
+ u_char addr [NGX_SOCKADDR_STRLEN + 1 ];
125
128
126
129
ctx = ngx_http_get_module_ctx (r , ngx_http_modsecurity_module );
127
130
if (ctx == NULL ) {
128
131
return NGX_ERROR ;
129
132
}
130
133
131
134
c = r -> connection ;
132
- /**
133
- * FIXME: We may want to use struct sockaddr instead of addr_text.
134
- *
135
- */
136
- ngx_str_t addr_text = c -> addr_text ;
137
135
138
- /**
139
- * FIXME: Check if it is possible to hook on nginx on a earlier phase.
140
- *
141
- * At this point we are doing an late connection process. Maybe
142
- * we have to hook into NGX_HTTP_FIND_CONFIG_PHASE, it seems to be the
143
- * erliest phase that nginx allow us to attach those kind of hooks.
144
- *
145
- */
146
- int client_port = ngx_inet_get_port (c -> sockaddr );
147
- int server_port = ngx_inet_get_port (c -> local_sockaddr );
136
+ client_addr = c -> addr_text ;
137
+ client_port = ngx_inet_get_port (c -> sockaddr );
148
138
149
- const char * client_addr = ngx_str_to_char (addr_text , r -> pool );
150
- if (client_addr == (char * )-1 ) {
151
- return NGX_HTTP_INTERNAL_SERVER_ERROR ;
139
+ if (client_addr .len < c -> listening -> addr_text_max_len ) {
140
+ client_addr .data [client_addr .len ] = 0 ;
141
+ } else {
142
+ client_addr .data = (u_char * )ngx_str_to_char (client_addr , r -> pool );
152
143
}
153
144
154
- ngx_str_t s ;
155
- u_char addr [NGX_SOCKADDR_STRLEN ];
156
- s .len = NGX_SOCKADDR_STRLEN ;
157
- s .data = addr ;
158
- if (ngx_connection_local_sockaddr (c , & s , 0 ) != NGX_OK ) {
159
- return NGX_HTTP_INTERNAL_SERVER_ERROR ;
160
- }
145
+ // fill c->local_sockaddr
146
+ ngx_connection_local_sockaddr (c , NULL , 0 );
161
147
162
- const char * server_addr = ngx_str_to_char (s , r -> pool );
163
- if (server_addr == (char * )-1 ) {
164
- return NGX_HTTP_INTERNAL_SERVER_ERROR ;
165
- }
148
+ server_addr .data = addr ;
149
+ server_addr .len = NGX_SOCKADDR_STRLEN ;
150
+
151
+ server_addr .len = ngx_sock_ntop (c -> local_sockaddr , c -> local_socklen ,
152
+ server_addr .data , server_addr .len , 0 );
153
+ server_addr .data [server_addr .len ] = 0 ;
154
+ server_port = ngx_inet_get_port (c -> local_sockaddr );
166
155
167
156
old_pool = ngx_http_modsecurity_pcre_malloc_init (r -> pool );
168
157
rc = msc_process_connection (ctx -> modsec_transaction ,
169
- client_addr , client_port ,
170
- server_addr , server_port );
158
+ ( char * ) client_addr . data , client_port ,
159
+ ( char * ) server_addr . data , server_port );
171
160
ngx_http_modsecurity_pcre_malloc_done (old_pool );
172
161
if (rc != 1 ){
173
162
dd ("Was not able to extract connection information." );
0 commit comments