unixstream.c 12 KB

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