0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Maxim Dounin
|
|
4 */
|
|
5
|
|
6 #include <ngx_config.h>
|
|
7 #include <ngx_core.h>
|
|
8 #include <ngx_http.h>
|
|
9
|
|
10
|
|
11 typedef struct {
|
|
12 ngx_flag_t enable;
|
|
13 ngx_uint_t tos;
|
|
14 } ngx_http_ip_tos_conf_t;
|
|
15
|
|
16
|
|
17 static void *ngx_http_ip_tos_create_conf(ngx_conf_t *cf);
|
|
18 static char *ngx_http_ip_tos_merge_conf(ngx_conf_t *cf, void *parent,
|
|
19 void *child);
|
|
20 static char *ngx_http_ip_tos(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
21 static ngx_int_t ngx_http_ip_tos_init(ngx_conf_t *cf);
|
|
22
|
|
23
|
|
24 static ngx_command_t ngx_http_ip_tos_commands[] = {
|
|
25
|
|
26 { ngx_string("ip_tos"),
|
|
27 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
28 ngx_http_ip_tos,
|
|
29 NGX_HTTP_LOC_CONF_OFFSET,
|
|
30 0,
|
|
31 NULL },
|
|
32
|
|
33 ngx_null_command
|
|
34 };
|
|
35
|
|
36
|
|
37 static ngx_http_module_t ngx_http_ip_tos_module_ctx = {
|
|
38 NULL, /* preconfiguration */
|
|
39 ngx_http_ip_tos_init, /* postconfiguration */
|
|
40
|
|
41 NULL, /* create main configuration */
|
|
42 NULL, /* init main configuration */
|
|
43
|
|
44 NULL, /* create server configuration */
|
|
45 NULL, /* merge server configuration */
|
|
46
|
|
47 ngx_http_ip_tos_create_conf, /* create location configuration */
|
|
48 ngx_http_ip_tos_merge_conf /* merge location configuration */
|
|
49 };
|
|
50
|
|
51
|
|
52 ngx_module_t ngx_http_ip_tos_filter_module = {
|
|
53 NGX_MODULE_V1,
|
|
54 &ngx_http_ip_tos_module_ctx, /* module context */
|
|
55 ngx_http_ip_tos_commands, /* module directives */
|
|
56 NGX_HTTP_MODULE, /* module type */
|
|
57 NULL, /* init master */
|
|
58 NULL, /* init module */
|
|
59 NULL, /* init process */
|
|
60 NULL, /* init thread */
|
|
61 NULL, /* exit thread */
|
|
62 NULL, /* exit process */
|
|
63 NULL, /* exit master */
|
|
64 NGX_MODULE_V1_PADDING
|
|
65 };
|
|
66
|
|
67
|
|
68 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
69
|
|
70
|
|
71 static ngx_int_t
|
|
72 ngx_http_ip_tos_header_filter(ngx_http_request_t *r)
|
|
73 {
|
|
74 int tos;
|
|
75 ngx_http_ip_tos_conf_t *conf;
|
|
76
|
|
77 if (r != r->main) {
|
|
78 return ngx_http_next_header_filter(r);
|
|
79 }
|
|
80
|
|
81 conf = ngx_http_get_module_loc_conf(r, ngx_http_ip_tos_filter_module);
|
|
82
|
|
83 if (!conf->enable) {
|
|
84 return ngx_http_next_header_filter(r);
|
|
85 }
|
|
86
|
|
87 tos = conf->tos;
|
|
88
|
|
89 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
90 "ip tos: 0x%02Xi", tos);
|
|
91
|
|
92 if (setsockopt(r->connection->fd, IPPROTO_IP, IP_TOS,
|
|
93 (const void *) &tos, sizeof(tos)) == -1)
|
|
94 {
|
|
95 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_socket_errno,
|
|
96 "setsockopt(IP_TOS) failed");
|
|
97 }
|
|
98
|
|
99 return ngx_http_next_header_filter(r);
|
|
100 }
|
|
101
|
|
102
|
|
103 static void *
|
|
104 ngx_http_ip_tos_create_conf(ngx_conf_t *cf)
|
|
105 {
|
|
106 ngx_http_ip_tos_conf_t *conf;
|
|
107
|
|
108 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ip_tos_conf_t));
|
|
109 if (conf == NULL) {
|
|
110 return NULL;
|
|
111 }
|
|
112
|
|
113 conf->enable = NGX_CONF_UNSET;
|
|
114 conf->tos = NGX_CONF_UNSET_UINT;
|
|
115
|
|
116 return conf;
|
|
117 }
|
|
118
|
|
119
|
|
120 static char *
|
|
121 ngx_http_ip_tos_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
122 {
|
|
123 ngx_http_ip_tos_conf_t *prev = parent;
|
|
124 ngx_http_ip_tos_conf_t *conf = child;
|
|
125
|
|
126 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
127 ngx_conf_merge_uint_value(conf->tos, prev->tos, 0);
|
|
128
|
|
129 return NGX_CONF_OK;
|
|
130 }
|
|
131
|
|
132
|
|
133 static char *
|
|
134 ngx_http_ip_tos(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
135 {
|
|
136 ngx_http_ip_tos_conf_t *itcf = conf;
|
|
137
|
|
138 ngx_int_t n;
|
|
139 ngx_str_t *value;
|
|
140
|
|
141 if (itcf->enable != NGX_CONF_UNSET) {
|
|
142 return "is duplicate";
|
|
143 }
|
|
144
|
|
145 value = cf->args->elts;
|
|
146
|
|
147 if (ngx_strcasecmp(value[1].data, (u_char *) "off") == 0) {
|
|
148 itcf->enable = 0;
|
|
149 return NGX_CONF_OK;
|
|
150 }
|
|
151
|
|
152 if (value[1].len != 4 || value[1].data[0] != '0' ||
|
|
153 (value[1].data[1] != 'x' && value[1].data[1] != 'X'))
|
|
154 {
|
|
155 return "invalid argument 1";
|
|
156 }
|
|
157
|
|
158 n = ngx_hextoi(value[1].data + 2, value[1].len - 2);
|
|
159 if (n == NGX_ERROR || n < 0 || n > 255) {
|
|
160 return "invalid argument 2";
|
|
161 }
|
|
162
|
|
163 itcf->enable = 1;
|
|
164 itcf->tos = n;
|
|
165
|
|
166 return NGX_CONF_OK;
|
|
167 }
|
|
168
|
|
169
|
|
170 static ngx_int_t
|
|
171 ngx_http_ip_tos_init(ngx_conf_t *cf)
|
|
172 {
|
|
173 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
174 ngx_http_top_header_filter = ngx_http_ip_tos_header_filter;
|
|
175
|
|
176 return NGX_OK;
|
|
177 }
|