summaryrefslogtreecommitdiffhomepage
path: root/plop.c
blob: a1de2d0e042b5da029669fa09477287c56785eed (plain)
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
#include "plop.h"

#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>

#include "connection.h"
#include "stream.h"

struct plop plop = {
	.handler = PLOP_DEFAULT_HANDLER,
	.L = NULL,
	.fds = NULL,
	.data = NULL,
	.nfds = 0,
};

/// Initializes new Lua state for the server.
/// \return Lua state
lua_State * plop_initialize_lua(void)
{
	lua_State * L = luaL_newstate();

	if (NULL == L)
	{
		return NULL;
	}

	luaL_openlibs(L);

	return L;
}

/// Tries to create, bind and start listening on INET server socket.
/// \param node Hostname
/// \param service Port
/// \return File descriptor of the socket or -1 in case of an error
/// \see getaddrinfo(3)
// TODO: Handle UNIX sockets
int open_server(const char * node, const char * service)
{
	struct addrinfo hints = {
		.ai_family = AF_UNSPEC,
		.ai_socktype = SOCK_STREAM,
		.ai_flags = AI_PASSIVE,
	};

	struct addrinfo * result;
	struct addrinfo * it;

	if (0 != getaddrinfo(node, service, &hints, &result))
	{
		return -1; // TODO: Handle errors properly
	}

	int server;
	int optval = 1;

	for (it = result; it != NULL; it = it->ai_next)
	{
		server = socket(it->ai_family, it->ai_socktype, it->ai_protocol);

		if (-1 == server)
			continue;

		if (-1 == setsockopt(server, SOL_SOCKET, SO_REUSEADDR, (char *) &optval, sizeof(optval)))
		{
			close(server);
			continue;
		}

		if (-1 == fcntl(server, F_SETFL, (fcntl(server, F_GETFL, 0) | O_NONBLOCK)))
		{
			close(server);
			continue;
		}

		if (0 == bind(server, it->ai_addr,it->ai_addrlen))
			break;

		close(server);
	}

	if (it == NULL)
	{
		server = -1; // TODO: Handle errors properly
	}

	freeaddrinfo(result);

	if (-1 == server || -1 == listen(server, 5))
	{
		return -1; // TODO: Handle errors properly
	}

	return server;
}

/// Loads a file in path, executes it, then puts first result into package.loaded table and into global table.
/// \param L Lua state to load the handler into
/// \param path Path the script that returns handle is located at
/// \param LUA_OK if successful, appropriate Lua error otherwise
int plop_load_handler(lua_State * L, const char * path)
{
	lua_getglobal(L, "package");
	lua_pushliteral(L, "loaded");
	int result = lua_rawget(L, -2);

	if (LUA_TTABLE != result)
	{
		lua_pop(L, 2);
		return LUA_ERRRUN;
	}

	result = luaL_loadfile(L, path) || lua_pcall(L, 0, 1, 0);

	if (LUA_OK != result)
	{
		lua_pop(L, 3);
		return result;
	}

	lua_pushliteral(L, "handler");
	lua_pushvalue(L, -2);
	lua_rawset(L, -4);
	lua_setglobal(L, "handler");
	lua_pop(L, 1);

	return LUA_OK;
}

/// Drops Lua thread referenced by *ref* in the registry of main Lua state.
void plop_drop_thread(const int ref)
{
	if (LUA_NOREF != ref)
		luaL_unref(plop.L, LUA_REGISTRYINDEX, ref);
}

/// Handles client events.
/// \param c Connection associated with the client
/// \return -1 if an error occured, 1 if expecting to continue
int plop_handle_client(struct connection * c)
{
	int nargs = 0;

	if (c->push)
	{
		lua_getglobal(c->L, "handler");
		stream_push_new(c->L, c->fd);
		nargs = 1;
		c->push = 0;
	}

	int nresults = 0;
	int result = lua_resume(c->L, NULL, nargs, &nresults);

	switch (result)
	{
		case LUA_OK:
		{
			// TODO: If keep-alive, then reset c->push?
			plop_drop_thread(c->ref);
			connection_free(c);
			return 0;
		}
		case LUA_YIELD:
		{
			return 1;
		}
		default:
		{
			const char * err = lua_tostring(c->L, -1);

			if (NULL == err)
			{
				err = "Unknown error ocurred";
			}

			dprintf(2, "%s\n", err);
			lua_pop(c->L, 1);
			plop_drop_thread(c->ref);
			connection_free(c);
			return -1;
		}
	}
}

/// Accepts awaiting connections if any.
/// \param server File descriptor for server socket
/// \return -1 if an error occured
int plop_handle_server(const int server)
{
	int i = 1;  // Always skip server descriptor.
	for (; i <= plop.nfds; ++i)
	{
		if (-1 == plop.fds[i].fd)
			break;
		if (plop.nfds == i)
			return 0;
	}

	const int client = accept(server, NULL, NULL);
	if (-1 == client)
	{
		switch (errno)
		{
			case EAGAIN:
#if EWOULDBLOCK != EAGAIN
			case EWOULDBLOCK:
#endif
			case ENETDOWN:
			case EPROTO:
			case ENOPROTOOPT:
			case EHOSTDOWN:
			case ENONET:
			case EHOSTUNREACH:
			case EOPNOTSUPP:
			case ENETUNREACH:
			case ECONNABORTED:
			case EMFILE:
			case ENFILE:
			case ENOBUFS:
			case ENOMEM:
			{
				return 0;
			}
			default:
			{
				return -1;
			}
		}
	}

	struct connection * connection = connection_new(client);
	if (NULL == connection)
		return -1;
	connection->L = lua_newthread(plop.L);
	if (NULL == connection->L)
		return -1;  // TODO: Revisit error handling.
	connection->ref = luaL_ref(plop.L, LUA_REGISTRYINDEX);
	connection->fd = client;
	connection->push = 1;
	plop.fds[i].fd = client;
	plop.data[i] = connection;

	return 0;
}