unix.c 12 KB

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