2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
29 #include <arpa/inet.h>
36 #include <lib/signal.h>
37 #include <lib/luaplugin.h>
40 #include <sys/sendfile.h>
46 static const char *plugin_path = NULL;
47 static struct luaplugin_ctx ctx;
48 static struct luaplugin_entry *e;
49 static struct timeval timeout;
51 static void load_config(void)
55 static struct uci_context* uci = 0;
56 uci = ucix_init("lucittpd");
59 plugin_path = ucix_get_option(uci, "lucittpd", "lucittpd", "path");
60 port = ucix_get_option_int(uci, "lucittpd", "lucittpd", "port", 80);
61 timeout.tv_sec = ucix_get_option_int(uci, "lucittpd", "lucittpd", "timeout", 90);
67 plugin_path = strdup("/usr/lib/lucittpd/plugins/");
69 // ToDo: Check why below command segfaults in uci_free_context
73 static int webuci_read(lua_State *L)
75 int len = luaL_checkinteger(L, 1);
77 return luaL_argerror(L, 1, "too low");
80 char *buffer = malloc(len);
82 return luaL_error(L, "malloc() failed");
85 int sockfd = lua_tointeger(L, lua_upvalueindex(1));
87 len = read(sockfd, buffer, len);
89 lua_pushlstring(L, buffer, len);
94 lua_pushinteger(L, (len == 0) ? 0 : errno);
98 /* fprintf(stderr, "%s:%s[%d] %d %d\n", __FILE__, __func__, __LINE__, sockfd, len); */
103 static int webuci_close(lua_State *L)
105 int sockfd = lua_tointeger(L, lua_upvalueindex(1));
106 int result = shutdown(sockfd, SHUT_RDWR);
108 /*log_printf("%s:%s[%d] %d %d\n", __FILE__, __func__, __LINE__, sockfd, result);*/
112 lua_pushinteger(L, errno);
115 lua_pushboolean(L, 1);
120 static int webuci_write(lua_State *L)
122 luaL_checktype(L, 1, LUA_TSTRING);
125 const char *data = lua_tolstring(L, 1, &len);
126 int sockfd = lua_tointeger(L, lua_upvalueindex(1));
128 len = send(sockfd, data, len, 0);
129 /*log_printf("%s:%s[%d] %d %d - %s\n", __FILE__, __func__, __LINE__, sockfd, len, data);*/
132 lua_pushinteger(L, errno);
135 lua_pushinteger(L, len);
140 static int webuci_sendfile(lua_State *L)
142 FILE **fp = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE);
144 return luaL_error(L, "attempt to use a closed file");
147 off_t offset = luaL_checkinteger(L, 2);
148 size_t size = luaL_checkinteger(L, 3);
150 int sockfd = lua_tointeger(L, lua_upvalueindex(1));
153 setsockopt(sockfd, SOL_TCP, TCP_CORK, &cork, sizeof(cork));
156 // uclibc is teh sux, it does not implement sendfile correctly
158 size_t c, toread = size, oldpos = ftell(*fp);
160 fseek(*fp, offset, SEEK_SET);
162 while(toread > 0 && (c = fread(tmp, 1, (toread < 1024) ? toread : 1024, *fp)) > 0)
166 write(sockfd, tmp, c);
169 fseek(*fp, oldpos, SEEK_SET);
171 size = sendfile(sockfd, fileno(*fp), &offset, size);
172 /*log_printf("%s:%s[%d] %d %d - %d\n", __FILE__, __func__, __LINE__, sockfd, fileno(*fp), size);*/
176 setsockopt(sockfd, SOL_TCP, TCP_CORK, &cork, sizeof(cork));
180 lua_pushinteger(L, errno);
182 lua_pushinteger(L, size);
183 lua_pushinteger(L, offset);
190 static void load_luci(const char *plugindir)
192 luaplugin_init(&ctx, plugindir);
193 luaplugin_scan(&ctx);
195 list_for_each_entry(e, &ctx.entries, list)
197 lua_pushstring(ctx.L, "initialize");
198 luaplugin_call(e, 0);
201 list_for_each_entry(e, &ctx.entries, list)
203 lua_pushstring(ctx.L, "register");
204 luaplugin_call(e, 0);
207 list_for_each_entry(e, &ctx.entries, list)
209 lua_pushstring(ctx.L, "filter");
210 luaplugin_call(e, 0);
214 static void run_luci(int sockfd)
216 lua_pushinteger(ctx.L, sockfd);
217 lua_pushcclosure(ctx.L, webuci_read, 1);
218 lua_setfield(ctx.L, LUA_GLOBALSINDEX, "webuci_read");
220 lua_pushinteger(ctx.L, sockfd);
221 lua_pushcclosure(ctx.L, webuci_write, 1);
222 lua_setfield(ctx.L, LUA_GLOBALSINDEX, "webuci_write");
224 lua_pushinteger(ctx.L, sockfd);
225 lua_pushcclosure(ctx.L, webuci_close, 1);
226 lua_setfield(ctx.L, LUA_GLOBALSINDEX, "webuci_close");
228 lua_pushinteger(ctx.L, sockfd);
229 lua_pushcclosure(ctx.L, webuci_sendfile, 1);
230 lua_setfield(ctx.L, LUA_GLOBALSINDEX, "webuci_sendfile");
232 list_for_each_entry(e, &ctx.entries, list)
234 lua_pushstring(ctx.L, "accept");
235 luaplugin_call(e, 0);
239 static void cleanup_luci(void)
241 luaplugin_done(&ctx);
244 int main(int argc, char **argv)
247 struct sockaddr_storage their_addr;
250 struct sockaddr_in myaddr;
258 /* used by sdk to override plugin dir */
261 load_luci(plugin_path);
267 myaddr.sin_family = AF_INET;
268 myaddr.sin_port = htons(port);
269 //inet_pton(AF_INET, "63.161.169.137", &myaddr.sin_addr.s_addr);
270 myaddr.sin_addr.s_addr = INADDR_ANY;
272 sockfd = socket(PF_INET, SOCK_STREAM, 0);
276 perror("server: socket");
280 if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
282 perror("setsockopt");
286 if(bind(sockfd, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1)
289 perror("server: bind");
293 if(listen(sockfd, BACKLOG) == -1)
299 /*log_printf("server: waiting for connections...\n");*/
303 sin_size = sizeof their_addr;
304 new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
311 /*inet_ntop(their_addr.ss_family,
312 (void*)&((struct sockaddr_in*)&their_addr)->sin_addr, s, sizeof s);
313 log_printf("server: got connection from %s\n", s);*/
320 setsockopt(new_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
321 setsockopt(new_fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));