-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_proc_daytime_module.c
264 lines (200 loc) · 6.25 KB
/
ngx_proc_daytime_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
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
#include <ngx_event.h>
#include <ngx_core.h>
#include <ngx_config.h>
static void *ngx_proc_daytime_create_conf(ngx_conf_t *cf);
static char *ngx_proc_daytime_merge_conf(ngx_conf_t *cf, void *parent,
void *child);
static ngx_int_t ngx_proc_daytime_prepare(ngx_cycle_t *cycle);
static ngx_int_t ngx_proc_daytime_process_init(ngx_cycle_t *cycle);
static ngx_int_t ngx_proc_daytime_loop(ngx_cycle_t *cycle);
static void ngx_proc_daytime_exit_process(ngx_cycle_t *cycle);
static void ngx_proc_daytime_accept(ngx_event_t *ev);
static char *week[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday" };
static char *months[] = { "January", "February", "March", "April", "May",
"June", "July", "August", "Semptember", "October",
"November", "December" };
typedef struct {
ngx_flag_t enable;
ngx_uint_t port;
ngx_socket_t fd;
} ngx_proc_daytime_conf_t;
static ngx_command_t ngx_proc_daytime_commands[] = {
{ ngx_string("listen"),
NGX_PROC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_PROC_CONF_OFFSET,
offsetof(ngx_proc_daytime_conf_t, port),
NULL },
{ ngx_string("daytime"),
NGX_PROC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_PROC_CONF_OFFSET,
offsetof(ngx_proc_daytime_conf_t, enable),
NULL },
ngx_null_command
};
static ngx_proc_module_t ngx_proc_daytime_module_ctx = {
ngx_string("daytime"),
NULL,
NULL,
ngx_proc_daytime_create_conf,
ngx_proc_daytime_merge_conf,
ngx_proc_daytime_prepare,
ngx_proc_daytime_process_init,
ngx_proc_daytime_loop,
ngx_proc_daytime_exit_process
};
ngx_module_t ngx_proc_daytime_module = {
NGX_MODULE_V1,
&ngx_proc_daytime_module_ctx,
ngx_proc_daytime_commands,
NGX_PROC_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static void *
ngx_proc_daytime_create_conf(ngx_conf_t *cf)
{
ngx_proc_daytime_conf_t *pbcf;
pbcf = ngx_pcalloc(cf->pool, sizeof(ngx_proc_daytime_conf_t));
if (pbcf == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"daytime create proc conf error");
return NULL;
}
pbcf->enable = NGX_CONF_UNSET;
pbcf->port = NGX_CONF_UNSET_UINT;
return pbcf;
}
static char *
ngx_proc_daytime_merge_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_proc_daytime_conf_t *prev = parent;
ngx_proc_daytime_conf_t *conf = child;
ngx_conf_merge_uint_value(conf->port, prev->port, 0);
ngx_conf_merge_off_value(conf->enable, prev->enable, 0);
return NGX_CONF_OK;
}
static ngx_int_t
ngx_proc_daytime_prepare(ngx_cycle_t *cycle)
{
ngx_proc_daytime_conf_t *pbcf;
pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_daytime_module);
if (!pbcf->enable) {
return NGX_DECLINED;
}
if (pbcf->port == 0) {
return NGX_DECLINED;
}
return NGX_OK;
}
static ngx_int_t
ngx_proc_daytime_process_init(ngx_cycle_t *cycle)
{
int reuseaddr;
ngx_event_t *rev;
ngx_socket_t fd;
ngx_connection_t *c;
struct sockaddr_in sin;
ngx_proc_daytime_conf_t *pbcf;
pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_daytime_module);
fd = ngx_socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "daytime socket error");
return NGX_ERROR;
}
reuseaddr = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(const void *) &reuseaddr, sizeof(int))
== -1)
{
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
"daytime setsockopt(SO_REUSEADDR) failed");
ngx_close_socket(fd);
return NGX_ERROR;
}
if (ngx_nonblocking(fd) == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
"daytime nonblocking failed");
ngx_close_socket(fd);
return NGX_ERROR;
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(pbcf->port);
if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "daytime bind error");
return NGX_ERROR;
}
if (listen(fd, 20) == -1) {
ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "daytime listen error");
return NGX_ERROR;
}
c = ngx_get_connection(fd, cycle->log);
if (c == NULL) {
ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "daytime no connection");
return NGX_ERROR;
}
c->log = cycle->log;
rev = c->read;
rev->log = c->log;
rev->accept = 1;
rev->handler = ngx_proc_daytime_accept;
if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
return NGX_ERROR;
}
pbcf->fd = fd;
return NGX_OK;
}
static ngx_int_t
ngx_proc_daytime_loop(ngx_cycle_t *cycle)
{
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "daytime %V",
&ngx_cached_http_time);
return NGX_OK;
}
static void
ngx_proc_daytime_exit_process(ngx_cycle_t *cycle)
{
ngx_proc_daytime_conf_t *pbcf;
pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_daytime_module);
ngx_close_socket(pbcf->fd);
}
static void
ngx_proc_daytime_accept(ngx_event_t *ev)
{
u_char sa[NGX_SOCKADDRLEN], buf[256], *p;
socklen_t socklen;
ngx_socket_t s;
ngx_connection_t *lc;
socklen = NGX_SOCKADDRLEN;
lc = ev->data;
s = accept(lc->fd, (struct sockaddr *) sa, &socklen);
if (s == -1) {
return;
}
if (ngx_nonblocking(s) == -1) {
goto finish;
}
/*
Daytime Protocol
http://tools.ietf.org/html/rfc867
Weekday, Month Day, Year Time-Zone
*/
p = ngx_sprintf(buf, "%s, %s, %d, %d, %d:%d:%d-%s",
week[ngx_cached_tm->tm_wday],
months[ngx_cached_tm->tm_mon],
ngx_cached_tm->tm_mday, ngx_cached_tm->tm_year,
ngx_cached_tm->tm_hour, ngx_cached_tm->tm_min,
ngx_cached_tm->tm_sec, ngx_cached_tm->tm_zone);
ngx_write_fd(s, buf, p - buf);
finish:
ngx_close_socket(s);
}