-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsrc_core.patch
319 lines (300 loc) · 8.76 KB
/
src_core.patch
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
diff -uNr nginx-1.4.1/src/core/ngx_log.c nginx-tcp-lua-module/src/core/ngx_log.c
--- nginx-1.4.1/src/core/ngx_log.c 2013-05-06 18:26:50.000000000 +0800
+++ nginx-tcp-lua-module/src/core/ngx_log.c 2019-05-07 18:11:48.945019529 +0800
@@ -8,9 +8,9 @@
#include <ngx_config.h>
#include <ngx_core.h>
-
static char *ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
-
+static char *ngx_nlog(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);//NLOG
+static void ngx_clean_nlog_sock(void* data);//NLOG
static ngx_command_t ngx_errlog_commands[] = {
@@ -20,7 +20,14 @@
0,
0,
NULL},
-
+/*------------------------NLOG------------------------*/
+ {ngx_string("nlog"),
+ NGX_MAIN_CONF|NGX_CONF_TAKE2,
+ ngx_nlog,
+ 0,
+ 0,
+ NULL},
+/*------------------------NLOG------------------------*/
ngx_null_command
};
@@ -67,7 +74,7 @@
static const char *debug_levels[] = {
"debug_core", "debug_alloc", "debug_mutex", "debug_event",
- "debug_http", "debug_mail", "debug_mysql"
+ "debug_http", "debug_mail", "debug_mysql","debug_tcp"
};
@@ -139,8 +146,16 @@
}
ngx_linefeed(p);
-
- (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
+/*------------------------NLOG------------------------*/
+ if (log->s_nlog == 0) {
+/*------------------------NLOG------------------------*/
+ (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
+/*------------------------NLOG------------------------*/
+ }
+ else {
+ (void) send(log->s_nlog, errstr, p - errstr, 0);
+ }
+/*------------------------NLOG------------------------*/
if (!ngx_use_stderr
|| level > NGX_LOG_WARN
@@ -434,6 +449,8 @@
return "is duplicate";
}
+ //cf->cycle->new_log.fd = -1;//NLOG
+
value = cf->args->elts;
if (ngx_strcmp(value[1].data, "stderr") == 0) {
@@ -457,3 +474,213 @@
return ngx_log_set_levels(cf, &cf->cycle->new_log);
}
+
+/*------------------------NLOG------------------------*/
+//char *ngx_nlog(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+//{
+// ngx_str_t *value, localaddr, remoteaddress;
+// char* pos;
+// char rip[16] = {0};
+// int val = 1;
+// unsigned short lport,rport;
+// struct sockaddr_in laddr;
+//
+// //need set file first
+// if (NULL == cf->cycle->new_log.file) {
+// return "need set error_log first";
+// }
+//
+// value = cf->args->elts;
+//
+// localaddr = value[1];
+// remoteaddress = value[2];
+//
+// /* nlog 127.0.0.1:5050 192.168.0.25:5151 */
+// pos = strchr((char*)localaddr.data,':');
+// if (NULL == pos) {
+// return NGX_CONF_ERROR;
+// }
+// lport = (unsigned short)atoi(pos + 1);
+//
+// pos = strchr((char*)remoteaddress.data,':');
+// if (NULL == pos) {
+// return NGX_CONF_ERROR;
+// }
+// rport = (unsigned short)atoi(pos + 1);
+//
+// int len = pos - (char*)remoteaddress.data;
+// if (len <= 0 || len >= 16) {
+// return NGX_CONF_ERROR;
+// }
+//
+// memcpy(rip,remoteaddress.data,len);
+// rip[len] = '\0';
+//
+// //printf("local port:%u, remote port:%u, remote ip:%s\n",lport,rport,rip);
+//
+// laddr.sin_family = AF_INET;
+// laddr.sin_port = htons(lport);
+// laddr.sin_addr.s_addr = INADDR_ANY;
+//
+// remoteaddr.sin_family = AF_INET;
+// remoteaddr.sin_port = htons(rport);
+// remoteaddr.sin_addr.s_addr = inet_addr (rip);
+//
+// if ((sock = socket (PF_INET, SOCK_DGRAM, 0)) < 0)
+// {
+// //perror("socket");
+// return NGX_CONF_ERROR;
+// }
+//
+// //printf("sock %d\n",sock);
+//
+// if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof (val)) != 0)
+// {
+// //printf ("error!setsockopt failed! ");
+// return NGX_CONF_ERROR;
+// }
+//
+// if (bind (sock, (struct sockaddr *) &laddr, sizeof (laddr)) < 0)
+// {
+// //perror("bind");
+// return NGX_CONF_ERROR;
+// }
+//
+// return NGX_CONF_OK;
+//}
+
+char *ngx_nlog(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+ ngx_str_t *value;
+ ngx_url_t u_l;
+ ngx_url_t u_r;
+ ngx_socket_t s;
+ int reuseaddr;
+ ngx_pool_cleanup_t *cln;
+
+ //need set file first
+ if (NULL == cf->cycle->new_log.file) {
+ return "need set error_log first";
+ }
+
+ if (cf->cycle->new_log.s_nlog > 0) {
+ return "is duplicate";
+ }
+
+ value = cf->args->elts;
+
+ ngx_memzero(&u_l, sizeof(ngx_url_t));
+ u_l.url = value[1];
+ u_l.default_port = (in_port_t) 0;
+ u_l.no_resolve = 1;
+
+ if (ngx_parse_url(cf->pool, &u_l) != NGX_OK) {
+ if (u_l.err) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "%s in \"%V\" of the \"nlog\" directive",
+ u_l.err, &u_l.url);
+ }
+
+ return NGX_CONF_ERROR;
+ }
+
+ if (u_l.no_port || u_l.family != AF_INET) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "no valid port or no valid ipv4 address");
+
+ return NGX_CONF_ERROR;
+ }
+
+ ngx_memzero(&u_r, sizeof(ngx_url_t));
+ u_r.url = value[2];
+ u_r.default_port = (in_port_t) 0;
+ u_r.no_resolve = 1;
+
+ if (ngx_parse_url(cf->pool, &u_r) != NGX_OK) {
+ if (u_r.err) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "%s in \"%V\" of the \"nlog\" directive",
+ u_r.err, &u_r.url);
+ }
+
+ return NGX_CONF_ERROR;
+ }
+
+ if (u_r.no_port || u_r.family != AF_INET) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "no valid port or no valid ipv4 address");
+
+ return NGX_CONF_ERROR;
+ }
+
+ s = ngx_socket(AF_INET, SOCK_DGRAM, 0);
+
+ ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0,"nlog create udp socket %d",s);
+
+ if (s == -1) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "socket() failed, %d", ngx_socket_errno);
+
+ return NGX_CONF_ERROR;
+ }
+
+ if (s == 0) {
+ ngx_close_socket(s);
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "s_nlog == 0, disable nlog");
+ return NGX_CONF_OK;
+ }
+
+ if (ngx_nonblocking(s) == -1) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "ngx_nonblocking() failed, %d", ngx_socket_errno);
+
+ goto failed;
+ }
+
+ reuseaddr = 1;
+ if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
+ (const void *) &reuseaddr, sizeof(int))
+ == -1) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "setsockopt() failed, %d", ngx_socket_errno);
+
+ goto failed;
+ }
+
+ if (bind(s, (struct sockaddr_in*) &u_l.sockaddr, u_l.socklen) == -1) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "bind() failed, %d", ngx_socket_errno);
+
+ goto failed;
+ }
+
+ if (connect(s, (struct sockaddr_in*) &u_r.sockaddr, u_r.socklen) == -1) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "connect() failed, %d", ngx_socket_errno);
+
+ goto failed;
+ }
+
+ cf->cycle->new_log.s_nlog = s;
+
+ cln = ngx_pool_cleanup_add(cf->pool, 0);
+ cln->data = &cf->cycle->new_log;
+ cln->handler = ngx_clean_nlog_sock;
+
+ return NGX_CONF_OK;
+
+failed:
+
+ if (ngx_close_socket(s) == -1) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "ngx_close_socket() failed, %d", ngx_socket_errno);
+ }
+
+ return NGX_CONF_ERROR;
+}
+
+static void ngx_clean_nlog_sock(void* data)
+{
+ ngx_log_t *log;
+
+ log = data;
+ if (log->s_nlog > 0) {
+ ngx_close_socket(log->s_nlog);
+ log->s_nlog = 0;
+ }
+}
+/*------------------------NLOG------------------------*/
diff -uNr nginx-1.4.1/src/core/ngx_log.h nginx-tcp-lua-module/src/core/ngx_log.h
--- nginx-1.4.1/src/core/ngx_log.h 2013-05-06 18:26:50.000000000 +0800
+++ nginx-tcp-lua-module/src/core/ngx_log.h 2019-05-07 18:11:48.972019509 +0800
@@ -30,6 +30,7 @@
#define NGX_LOG_DEBUG_HTTP 0x100
#define NGX_LOG_DEBUG_MAIL 0x200
#define NGX_LOG_DEBUG_MYSQL 0x400
+#define NGX_LOG_DEBUG_TCP 0x800
/*
* do not forget to update debug_levels[] in src/core/ngx_log.c
@@ -37,7 +38,7 @@
*/
#define NGX_LOG_DEBUG_FIRST NGX_LOG_DEBUG_CORE
-#define NGX_LOG_DEBUG_LAST NGX_LOG_DEBUG_MYSQL
+#define NGX_LOG_DEBUG_LAST NGX_LOG_DEBUG_TCP
#define NGX_LOG_DEBUG_CONNECTION 0x80000000
#define NGX_LOG_DEBUG_ALL 0x7ffffff0
@@ -48,7 +49,6 @@
struct ngx_log_s {
ngx_uint_t log_level;
ngx_open_file_t *file;
-
ngx_atomic_uint_t connection;
ngx_log_handler_pt handler;
@@ -61,6 +61,8 @@
*/
char *action;
+
+ ngx_socket_t s_nlog;//NLOG
};