-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngx_module_runtime.c
More file actions
82 lines (76 loc) · 2.42 KB
/
ngx_module_runtime.c
File metadata and controls
82 lines (76 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright (C) 2023 Jean-Luc Barriere
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ngx_module.h"
/*#include <netinet/in.h>*/
void
ngx_http_ipfilter_data_parse(ngx_http_request_ctx_t* ctx,
ngx_http_request_t* r,
ngx_http_ipfilter_loc_conf_t* cf)
{
ipf_cidr_address cidr;
ngx_addr_t addr;
in_addr_t inaddr;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
u_char *p;
struct in6_addr *inaddr6;
#endif
if (!cf || !ctx)
{
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
IPFILTER_TAG " unable to parse data.");
return;
}
addr.sockaddr = r->connection->sockaddr;
addr.socklen = r->connection->socklen;
switch (addr.sockaddr->sa_family)
{
#if (NGX_HAVE_INET6)
case AF_INET6:
inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr;
p = inaddr6->s6_addr;
memcpy(cidr.addr, p, 16);
cidr.prefix = 128;
break;
#endif
case AF_INET:
sin = (struct sockaddr_in *) addr.sockaddr;
inaddr = ntohl(sin->sin_addr.s_addr);
ipf_init_address_ipv4_mapped(&cidr);
cidr.addr[12] = (inaddr >> 24) & 0xff;
cidr.addr[13] = (inaddr >> 16) & 0xff;
cidr.addr[14] = (inaddr >> 8) & 0xff;
cidr.addr[15] = inaddr & 0xff;
break;
default:
ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
IPFILTER_TAG " Unable to process address family=%d.",
r->connection->sockaddr->sa_family);
return;
}
ctx->response = ipf_query(cf->db_instance, &cidr);
}
ngx_int_t
ngx_http_output_forbidden_page(ngx_http_request_ctx_t* ctx,
ngx_http_request_t* r,
ngx_http_ipfilter_loc_conf_t* cf)
{
ngx_str_t empty = ngx_string("");
ngx_http_internal_redirect(r, cf->denied_url, &empty);
return (NGX_HTTP_OK);
}