aboutsummaryrefslogtreecommitdiff
path: root/src/unixtcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/unixtcp.c')
-rw-r--r--src/unixtcp.c338
1 files changed, 338 insertions, 0 deletions
diff --git a/src/unixtcp.c b/src/unixtcp.c
new file mode 100644
index 0000000..88baac0
--- /dev/null
+++ b/src/unixtcp.c
@@ -0,0 +1,338 @@
1/*=========================================================================*\
2* Unix domain socket tcp sub module
3* LuaSocket toolkit
4\*=========================================================================*/
5#include <string.h>
6
7#include "lua.h"
8#include "lauxlib.h"
9
10#include "auxiliar.h"
11#include "socket.h"
12#include "options.h"
13#include "unixtcp.h"
14#include <sys/un.h>
15
16/*=========================================================================*\
17* Internal function prototypes
18\*=========================================================================*/
19static int global_create(lua_State *L);
20static int meth_connect(lua_State *L);
21static int meth_listen(lua_State *L);
22static int meth_bind(lua_State *L);
23static int meth_send(lua_State *L);
24static int meth_shutdown(lua_State *L);
25static int meth_receive(lua_State *L);
26static int meth_accept(lua_State *L);
27static int meth_close(lua_State *L);
28static int meth_setoption(lua_State *L);
29static int meth_settimeout(lua_State *L);
30static int meth_getfd(lua_State *L);
31static int meth_setfd(lua_State *L);
32static int meth_dirty(lua_State *L);
33static int meth_getstats(lua_State *L);
34static int meth_setstats(lua_State *L);
35
36static const char *unixtcp_tryconnect(p_unix un, const char *path);
37static const char *unixtcp_trybind(p_unix un, const char *path);
38
39/* unixtcp object methods */
40static luaL_Reg unixtcp_methods[] = {
41 {"__gc", meth_close},
42 {"__tostring", auxiliar_tostring},
43 {"accept", meth_accept},
44 {"bind", meth_bind},
45 {"close", meth_close},
46 {"connect", meth_connect},
47 {"dirty", meth_dirty},
48 {"getfd", meth_getfd},
49 {"getstats", meth_getstats},
50 {"setstats", meth_setstats},
51 {"listen", meth_listen},
52 {"receive", meth_receive},
53 {"send", meth_send},
54 {"setfd", meth_setfd},
55 {"setoption", meth_setoption},
56 {"setpeername", meth_connect},
57 {"setsockname", meth_bind},
58 {"settimeout", meth_settimeout},
59 {"shutdown", meth_shutdown},
60 {NULL, NULL}
61};
62
63/* socket option handlers */
64static t_opt optset[] = {
65 {"keepalive", opt_set_keepalive},
66 {"reuseaddr", opt_set_reuseaddr},
67 {"linger", opt_set_linger},
68 {NULL, NULL}
69};
70
71/* functions in library namespace */
72static luaL_Reg func[] = {
73 {"tcp", global_create},
74 {NULL, NULL}
75};
76
77/*-------------------------------------------------------------------------*\
78* Initializes module
79\*-------------------------------------------------------------------------*/
80int unixtcp_open(lua_State *L)
81{
82 /* create classes */
83 auxiliar_newclass(L, "unixtcp{master}", unixtcp_methods);
84 auxiliar_newclass(L, "unixtcp{client}", unixtcp_methods);
85 auxiliar_newclass(L, "unixtcp{server}", unixtcp_methods);
86
87 /* create class groups */
88 auxiliar_add2group(L, "unixtcp{master}", "unixtcp{any}");
89 auxiliar_add2group(L, "unixtcp{client}", "unixtcp{any}");
90 auxiliar_add2group(L, "unixtcp{server}", "unixtcp{any}");
91
92 luaL_setfuncs(L, func, 0);
93 return 1;
94}
95
96/*=========================================================================*\
97* Lua methods
98\*=========================================================================*/
99/*-------------------------------------------------------------------------*\
100* Just call buffered IO methods
101\*-------------------------------------------------------------------------*/
102static int meth_send(lua_State *L) {
103 p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{client}", 1);
104 return buffer_meth_send(L, &un->buf);
105}
106
107static int meth_receive(lua_State *L) {
108 p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{client}", 1);
109 return buffer_meth_receive(L, &un->buf);
110}
111
112static int meth_getstats(lua_State *L) {
113 p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{client}", 1);
114 return buffer_meth_getstats(L, &un->buf);
115}
116
117static int meth_setstats(lua_State *L) {
118 p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{client}", 1);
119 return buffer_meth_setstats(L, &un->buf);
120}
121
122/*-------------------------------------------------------------------------*\
123* Just call option handler
124\*-------------------------------------------------------------------------*/
125static int meth_setoption(lua_State *L) {
126 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1);
127 return opt_meth_setoption(L, optset, &un->sock);
128}
129
130/*-------------------------------------------------------------------------*\
131* Select support methods
132\*-------------------------------------------------------------------------*/
133static int meth_getfd(lua_State *L) {
134 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1);
135 lua_pushnumber(L, (int) un->sock);
136 return 1;
137}
138
139/* this is very dangerous, but can be handy for those that are brave enough */
140static int meth_setfd(lua_State *L) {
141 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1);
142 un->sock = (t_socket) luaL_checknumber(L, 2);
143 return 0;
144}
145
146static int meth_dirty(lua_State *L) {
147 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1);
148 lua_pushboolean(L, !buffer_isempty(&un->buf));
149 return 1;
150}
151
152/*-------------------------------------------------------------------------*\
153* Waits for and returns a client object attempting connection to the
154* server object
155\*-------------------------------------------------------------------------*/
156static int meth_accept(lua_State *L) {
157 p_unix server = (p_unix) auxiliar_checkclass(L, "unixtcp{server}", 1);
158 p_timeout tm = timeout_markstart(&server->tm);
159 t_socket sock;
160 int err = socket_accept(&server->sock, &sock, NULL, NULL, tm);
161 /* if successful, push client socket */
162 if (err == IO_DONE) {
163 p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix));
164 auxiliar_setclass(L, "unixtcp{client}", -1);
165 /* initialize structure fields */
166 socket_setnonblocking(&sock);
167 clnt->sock = sock;
168 io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv,
169 (p_error) socket_ioerror, &clnt->sock);
170 timeout_init(&clnt->tm, -1, -1);
171 buffer_init(&clnt->buf, &clnt->io, &clnt->tm);
172 return 1;
173 } else {
174 lua_pushnil(L);
175 lua_pushstring(L, socket_strerror(err));
176 return 2;
177 }
178}
179
180/*-------------------------------------------------------------------------*\
181* Binds an object to an address
182\*-------------------------------------------------------------------------*/
183static const char *unixtcp_trybind(p_unix un, const char *path) {
184 struct sockaddr_un local;
185 size_t len = strlen(path);
186 int err;
187 if (len >= sizeof(local.sun_path)) return "path too long";
188 memset(&local, 0, sizeof(local));
189 strcpy(local.sun_path, path);
190 local.sun_family = AF_UNIX;
191#ifdef UNIX_HAS_SUN_LEN
192 local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len)
193 + len + 1;
194 err = socket_bind(&un->sock, (SA *) &local, local.sun_len);
195
196#else
197 err = socket_bind(&un->sock, (SA *) &local,
198 sizeof(local.sun_family) + len);
199#endif
200 if (err != IO_DONE) socket_destroy(&un->sock);
201 return socket_strerror(err);
202}
203
204static int meth_bind(lua_State *L) {
205 p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{master}", 1);
206 const char *path = luaL_checkstring(L, 2);
207 const char *err = unixtcp_trybind(un, path);
208 if (err) {
209 lua_pushnil(L);
210 lua_pushstring(L, err);
211 return 2;
212 }
213 lua_pushnumber(L, 1);
214 return 1;
215}
216
217/*-------------------------------------------------------------------------*\
218* Turns a master unixtcp object into a client object.
219\*-------------------------------------------------------------------------*/
220static const char *unixtcp_tryconnect(p_unix un, const char *path)
221{
222 struct sockaddr_un remote;
223 int err;
224 size_t len = strlen(path);
225 if (len >= sizeof(remote.sun_path)) return "path too long";
226 memset(&remote, 0, sizeof(remote));
227 strcpy(remote.sun_path, path);
228 remote.sun_family = AF_UNIX;
229 timeout_markstart(&un->tm);
230#ifdef UNIX_HAS_SUN_LEN
231 remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len)
232 + len + 1;
233 err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm);
234#else
235 err = socket_connect(&un->sock, (SA *) &remote,
236 sizeof(remote.sun_family) + len, &un->tm);
237#endif
238 if (err != IO_DONE) socket_destroy(&un->sock);
239 return socket_strerror(err);
240}
241
242static int meth_connect(lua_State *L)
243{
244 p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{master}", 1);
245 const char *path = luaL_checkstring(L, 2);
246 const char *err = unixtcp_tryconnect(un, path);
247 if (err) {
248 lua_pushnil(L);
249 lua_pushstring(L, err);
250 return 2;
251 }
252 /* turn master object into a client object */
253 auxiliar_setclass(L, "unixtcp{client}", 1);
254 lua_pushnumber(L, 1);
255 return 1;
256}
257
258/*-------------------------------------------------------------------------*\
259* Closes socket used by object
260\*-------------------------------------------------------------------------*/
261static int meth_close(lua_State *L)
262{
263 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1);
264 socket_destroy(&un->sock);
265 lua_pushnumber(L, 1);
266 return 1;
267}
268
269/*-------------------------------------------------------------------------*\
270* Puts the sockt in listen mode
271\*-------------------------------------------------------------------------*/
272static int meth_listen(lua_State *L)
273{
274 p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{master}", 1);
275 int backlog = (int) luaL_optnumber(L, 2, 32);
276 int err = socket_listen(&un->sock, backlog);
277 if (err != IO_DONE) {
278 lua_pushnil(L);
279 lua_pushstring(L, socket_strerror(err));
280 return 2;
281 }
282 /* turn master object into a server object */
283 auxiliar_setclass(L, "unixtcp{server}", 1);
284 lua_pushnumber(L, 1);
285 return 1;
286}
287
288/*-------------------------------------------------------------------------*\
289* Shuts the connection down partially
290\*-------------------------------------------------------------------------*/
291static int meth_shutdown(lua_State *L)
292{
293 /* SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, so we can use method index directly */
294 static const char* methods[] = { "receive", "send", "both", NULL };
295 p_unix tcp = (p_unix) auxiliar_checkclass(L, "unixtcp{client}", 1);
296 int how = luaL_checkoption(L, 2, "both", methods);
297 socket_shutdown(&tcp->sock, how);
298 lua_pushnumber(L, 1);
299 return 1;
300}
301
302/*-------------------------------------------------------------------------*\
303* Just call tm methods
304\*-------------------------------------------------------------------------*/
305static int meth_settimeout(lua_State *L) {
306 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1);
307 return timeout_meth_settimeout(L, &un->tm);
308}
309
310/*=========================================================================*\
311* Library functions
312\*=========================================================================*/
313/*-------------------------------------------------------------------------*\
314* Creates a master unixtcp object
315\*-------------------------------------------------------------------------*/
316static int global_create(lua_State *L) {
317 t_socket sock;
318 int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0);
319 /* try to allocate a system socket */
320 if (err == IO_DONE) {
321 /* allocate unixtcp object */
322 p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix));
323 /* set its type as master object */
324 auxiliar_setclass(L, "unixtcp{master}", -1);
325 /* initialize remaining structure fields */
326 socket_setnonblocking(&sock);
327 un->sock = sock;
328 io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv,
329 (p_error) socket_ioerror, &un->sock);
330 timeout_init(&un->tm, -1, -1);
331 buffer_init(&un->buf, &un->io, &un->tm);
332 return 1;
333 } else {
334 lua_pushnil(L);
335 lua_pushstring(L, socket_strerror(err));
336 return 2;
337 }
338}