-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_vrf_ext_module.c
118 lines (97 loc) · 3.02 KB
/
ngx_http_vrf_ext_module.c
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Copyright (C) 2018 by Green Communications
*
* ngx_vrf_ext NGINX module.
*
* This adds support for variable $request_vrf which contains the name
* of the VRF from which the current request has arrived. This works
* only on Linux kernels supporting VRF. For the default VRF, the
* $request_vrf variable is empty.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <assert.h>
/* Retrieve the value of the $request_vrf variable. */
static ngx_int_t
ngx_http_vrf_get_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v,
uintptr_t data)
{
u_char *name;
socklen_t name_len = IFNAMSIZ;
int ret;
assert(r->connection != NULL);
name = ngx_pnalloc(r->pool, name_len);
if (name == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
"Could not allocate memory for $request_vrf variable.");
return NGX_ERROR;
}
ret = getsockopt(r->connection->fd, SOL_SOCKET, SO_BINDTODEVICE,
name, &name_len);
if (ret == -1) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_socket_errno,
"Could not get interface to which connection socket"
" is bound.");
return NGX_ERROR;
}
v->data = name;
v->len = name_len > 0 && name[name_len - 1] == 0?
name_len - 1: name_len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
"Getting $request_vrf value = \"%s\".", v->data);
return NGX_OK;
}
/* List of supported variables. */
static ngx_http_variable_t ngx_http_vrf_ext_vars[] = {
{ ngx_string("request_vrf"), NULL, ngx_http_vrf_get_variable, 0, 0, 0 },
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
/* Add supported variables during preconfiguration. */
static ngx_int_t
ngx_http_vrf_ext_add_variable(ngx_conf_t *cf)
{
ngx_http_variable_t *var, *v;
for (v = ngx_http_vrf_ext_vars; v->name.len; ++v) {
var = ngx_http_add_variable(cf, &v->name, v->flags);
if (var == NULL)
return NGX_ERROR;
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
/* Module context */
static ngx_http_module_t
ngx_http_vrf_ext_module_ctx = {
ngx_http_vrf_ext_add_variable, /* Preconfiguration */
NULL, /* Postconfiguration */
NULL, /* Create main configuration */
NULL, /* Init main configuration */
NULL, /* Create server configuration */
NULL, /* Merge server configuration */
NULL, /* Create location configuration */
NULL /* Merge location configuration */
};
/* Module definition */
ngx_module_t
ngx_http_vrf_ext_module = {
NGX_MODULE_V1,
&ngx_http_vrf_ext_module_ctx, /* Module context */
NULL, /* Module directives */
NGX_HTTP_MODULE, /* Module type */
NULL, /* Init master */
NULL, /* Init module */
NULL, /* Init process */
NULL, /* Init thread */
NULL, /* Exit thread */
NULL, /* Exit process */
NULL, /* Exit master */
NGX_MODULE_V1_PADDING
};