-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua-api.c
More file actions
340 lines (325 loc) · 10.8 KB
/
Copy pathlua-api.c
File metadata and controls
340 lines (325 loc) · 10.8 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#define PLUGIN_IMPLEMENT 1
#include <dlfcn.h>
#include <antd/plugin.h>
#include <antd/scheduler.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <time.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <sys/stat.h>
#define LUA_HDL_FN "lua_handle"
#define MAX_SOCK_NAME 64
#define SOCKET_NAME "lua.sock"
#define PING_INTERVAL 10u // 10s
#define PROCESS_TIMEOUT 200u //100ms
typedef struct {
plugin_header_t* __plugin__;
int fd;
} lua_thread_data_t;
static pid_t pid = 0;
static char sock_path[108];
static int open_unix_socket()
{
struct sockaddr_un address;
address.sun_family = AF_UNIX;
(void) strncpy(address.sun_path, sock_path, sizeof(address.sun_path));
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if(fd == -1)
{
ERROR( "Unable to create Unix domain socket: %s", strerror(errno));
return -1;
}
if(connect(fd, (struct sockaddr*)(&address), sizeof(address)) == -1)
{
ERROR( "Unable to connect to socket '%s': %s", address.sun_path, strerror(errno));
return -1;
}
LOG( "Socket %s is created successfully", sock_path);
return fd;
}
static int mk_socket()
{
struct sockaddr_un address;
address.sun_family = AF_UNIX;
// create the socket
(void)strncpy(address.sun_path, sock_path, sizeof(address.sun_path));
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1)
{
ERROR("Unable to create Unix domain socket: %s", strerror(errno));
return -1;
}
if (bind(fd, (struct sockaddr *)(&address), sizeof(address)) == -1)
{
ERROR("Unable to bind name: %s to a socket: %s", address.sun_path, strerror(errno));
close(fd);
return -1;
}
// mark the socket as passive mode
if (listen(fd, 500) == -1)
{
ERROR("Unable to listen to socket: %d (%s): %s", fd, sock_path, strerror(errno));
close(fd);
return -1;
}
LOG("Socket %s is created successfully: %d", sock_path, fd);
return fd;
}
static void lua_serve()
{
void* core = NULL;
void* lua_handle = NULL;
void *(*handle_fn)(void*);
char path[BUFFLEN];
char* error;
(void)snprintf(path, BUFFLEN, "%s/lua/core.so", __plugin__.pdir);
core = dlopen(path, RTLD_NOW| RTLD_GLOBAL);
if(!core)
{
ERROR("Cannot load Lua core: %s", dlerror());
return;
}
// now load the handle
(void)snprintf(path, BUFFLEN, "%s/lua/handle.so", __plugin__.pdir);
lua_handle = dlopen(path, RTLD_LAZY);
if(!lua_handle)
{
ERROR("Cannot load lua_handle: %s", dlerror());
return;
}
// find the fn
handle_fn = (void *(*)(void*))dlsym(lua_handle, LUA_HDL_FN);
if ((error = dlerror()) != NULL)
{
ERROR("Problem when finding %s method from handle : %s", LUA_HDL_FN, error);
handle_fn = NULL;
return;
}
int socket = mk_socket();
if(socket != -1)
{
int fd;
if (setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) == -1)
{
ERROR("Unable to set reuse address on %d - setsockopt: %s", socket, strerror(errno));
}
LOG("LUA server online");
/*set log level*/
const char * enable_debug = getenv("ANTD_DEBUG");
int log_level = LOG_ERR;
if(enable_debug)
{
if(atoi(enable_debug))
{
LOG("LUA Debug is enabled");
log_level = LOG_NOTICE;
}
}
setlogmask(LOG_UPTO(log_level));
while((fd = accept(socket, NULL, NULL)) > 0)
{
pthread_t thread;
lua_thread_data_t* data = (lua_thread_data_t*)malloc(sizeof(lua_thread_data_t));
data->__plugin__ = &__plugin__;
data->fd = fd;
//set_nonblock(fd);
if (pthread_create(&thread, NULL, (void *(*)(void*))handle_fn, (void *)data) != 0)
{
ERROR("pthread_create: cannot create lua thread: %s", strerror(errno));
(void)close(fd);
}
else
{
LOG("Serve thread created for %d", fd);
pthread_detach(thread);
}
}
if (fd < 0)
{
ERROR("Unable to accept the new connection: %s", strerror(errno));
}
}
if(core)
(void)dlclose(core);
if(lua_handle)
(void)dlclose(lua_handle);
LOG("lua_serve: stop serve due to error");
}
void init()
{
(void)snprintf(sock_path, sizeof(sock_path), "%s/%s", __plugin__.tmpdir, SOCKET_NAME);
LOG("Lua socket will be stored in %s", sock_path);
pid = fork();
if (pid == 0)
{
// child
lua_serve();
}
LOG("Lua module initialized");
}
static void push_dict_to_socket(antd_client_t* cl, char* name, char* parent_name, dictionary_t d)
{
antd_send(cl,name, strlen(name));
antd_send(cl,"\n", 1);
chain_t as;
if(d)
for_each_assoc(as, d)
{
if(EQU(as->key,"COOKIE") || EQU(as->key,"REQUEST_HEADER") || EQU(as->key,"REQUEST_DATA") )
push_dict_to_socket(cl, as->key, name, (dictionary_t)as->value);
else if(as->value)
{
antd_send(cl,as->key, strlen(as->key));
antd_send(cl,"\n", 1);
antd_send(cl,as->value, strlen(as->value));
antd_send(cl,"\n", 1);
}
}
antd_send(cl,parent_name, strlen(parent_name));
antd_send(cl,"\n", 1);
}
static void *process(void *data)
{
antd_request_t *rq = (antd_request_t *)data;
antd_client_t* cl = (antd_client_t* ) dvalue(rq->request, "LUA_CL_DATA");
struct pollfd pfds[2];
pfds[0].fd = rq->client->sock;
pfds[0].events = POLLIN;
if(rq->client->ssl)
{
pfds[0].events = POLLIN | POLLOUT;
}
pfds[1].fd = cl->sock;
pfds[1].events = POLLIN;
int rc = poll(pfds, 2, PROCESS_TIMEOUT);
antd_task_t* task;
uint8_t buff[BUFFLEN];
int ret;
switch (rc)
{
case -1:
ERROR("Error on poll(): %s", strerror(errno));
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
case 0:
// time out
task = antd_create_task(process, (void *)rq, NULL, time(NULL));
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
antd_task_bind_event(task, cl->sock, 0, TASK_EVT_ON_READABLE);
return task;
// we have data
default:
// If data is on webserver
if ((pfds[0].revents & POLLIN) || (rq->client->ssl && (pfds[0].revents & POLLOUT)) )
{
while((ret = antd_recv_upto(rq->client,buff, BUFFLEN)) > 0)
{
// write data to the other side
if(antd_send(cl,buff, ret) != ret)
{
ERROR("Error on atnd_send(): %s", strerror(errno));
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
}
if(ret < 0)
{
LOG("antd_recv_upto() on %d: %s",rq->client->sock, strerror(errno));
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
}
else if(pfds[0].revents &(POLLERR | POLLHUP))
{
ERROR("POLLERR or POLLHUP received on %d", rq->client->sock);
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
if(pfds[1].revents & POLLIN)
{
while((ret = antd_recv_upto(cl,buff, BUFFLEN)) > 0)
{
// write data to the other side
if(antd_send(rq->client,buff, ret) != ret)
{
ERROR("Error atnd_send(): %s", strerror(errno));
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
}
if(ret < 0)
{
LOG("antd_recv_upto() on %d: %s", cl->sock, strerror(errno));
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
}
else if(pfds[1].revents &(POLLERR | POLLHUP))
{
ERROR("POLLERR or POLLHUP received on %d", cl->sock);
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
task = antd_create_task(process, (void *)rq, NULL, time(NULL));
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
antd_task_bind_event(task, cl->sock, 0, TASK_EVT_ON_READABLE);
return task;
}
}
void* handle(void* data)
{
antd_request_t *rq = (antd_request_t *)data;
// connect to socket
int fd = open_unix_socket();
if(fd < 0)
{
antd_error(rq->client, 503, "Service unavailable");
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
LOG("Connected to lua server at %d", fd);
set_nonblock(fd);
// write all header to lua
antd_client_t* cl = (antd_client_t*) malloc(sizeof(antd_client_t));
(void)memset(cl, 0, sizeof(antd_client_t));
cl->sock = fd;
time(&cl->last_io);
cl->ssl = NULL;
cl->state = ANTD_CLIENT_PLUGIN_EXEC;
cl->z_status = 0;
cl->z_level = ANTD_CNONE;
cl->zstream = NULL;
rq->client->z_level = ANTD_CNONE;
push_dict_to_socket(cl, "request","HTTP_REQUEST", rq->request);
antd_send(cl,"\r\n", 2);
dput(rq->request, "LUA_CL_DATA", cl);
antd_task_t* task = antd_create_task(process, (void *)rq, NULL, time(NULL));
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
antd_task_bind_event(task, fd, 0, TASK_EVT_ON_READABLE);
return task;
}
void destroy()
{
if(pid > 0)
{
kill(pid, SIGHUP);
}
LOG("Exit LUA Handle");
}