1 /*=========================================================================*\
2 * LuaSocket toolkit
3 * Networking support for the Lua language
4 * Diego Nehab
5 * 26/11/1999
6 *
7 * This library is part of an  effort to progressively increase the network
8 * connectivity  of  the Lua  language.  The  Lua interface  to  networking
9 * functions follows the Sockets API  closely, trying to simplify all tasks
10 * involved in setting up both  client and server connections. The provided
11 * IO routines, however, follow the Lua  style, being very similar  to the
12 * standard Lua read and write functions.
13 *
14 * RCS ID: $Id: luasocket.c,v 1.53 2005/10/07 04:40:59 diego Exp $
15 \*=========================================================================*/
16 
17 /*=========================================================================*\
18 * LuaSocket includes
19 \*=========================================================================*/
20 #include "luasocket.h"
21 #include "auxiliar.h"
22 #include "except.h"
23 #include "timeout.h"
24 #include "buffer.h"
25 #include "inet.h"
26 #include "tcp.h"
27 #include "udp.h"
28 #include "select.h"
29 
30 /*-------------------------------------------------------------------------*\
31 * Internal function prototypes
32 \*-------------------------------------------------------------------------*/
33 static int global_skip(lua_State *L);
34 static int global_unload(lua_State *L);
35 static int base_open(lua_State *L);
36 
37 /*-------------------------------------------------------------------------*\
38 * Modules and functions
39 \*-------------------------------------------------------------------------*/
40 static const luaL_Reg mod[] = {
41     {"auxiliar", auxiliar_open},
42     {"except", except_open},
43     {"timeout", timeout_open},
44     {"buffer", buffer_open},
45     {"inet", inet_open},
46     {"tcp", tcp_open},
47     {"udp", udp_open},
48     {"select", select_open},
49     {NULL, NULL}
50 };
51 
52 static luaL_Reg func[] = {
53     {"skip",      global_skip},
54     {"__unload",  global_unload},
55     {NULL,        NULL}
56 };
57 
58 /*-------------------------------------------------------------------------*\
59 * Skip a few arguments
60 \*-------------------------------------------------------------------------*/
global_skip(lua_State * L)61 static int global_skip(lua_State *L) {
62     int amount = luaL_checkint(L, 1);
63     int ret = lua_gettop(L) - amount - 1;
64     return ret >= 0 ? ret : 0;
65 }
66 
67 /*-------------------------------------------------------------------------*\
68 * Unloads the library
69 \*-------------------------------------------------------------------------*/
global_unload(lua_State * L)70 static int global_unload(lua_State *L) {
71     (void) L;
72     socket_close();
73     return 0;
74 }
75 
76 /*-------------------------------------------------------------------------*\
77 * Setup basic stuff.
78 \*-------------------------------------------------------------------------*/
base_open(lua_State * L)79 static int base_open(lua_State *L) {
80     if (socket_open()) {
81         /* export functions (and leave namespace table on top of stack) */
82         luaL_openlib(L, "socket", func, 0);
83 #ifdef LUASOCKET_DEBUG
84         lua_pushstring(L, "_DEBUG");
85         lua_pushboolean(L, 1);
86         lua_rawset(L, -3);
87 #endif
88         /* make version string available to scripts */
89         lua_pushstring(L, "_VERSION");
90         lua_pushstring(L, LUASOCKET_VERSION);
91         lua_rawset(L, -3);
92         return 1;
93     } else {
94         lua_pushstring(L, "unable to initialize library");
95         lua_error(L);
96         return 0;
97     }
98 }
99 
100 /*-------------------------------------------------------------------------*\
101 * Initializes all library modules.
102 \*-------------------------------------------------------------------------*/
luaopen_socket_core(lua_State * L)103 LUASOCKET_API int luaopen_socket_core(lua_State *L) {
104     int i;
105     base_open(L);
106     for (i = 0; mod[i].name; i++) mod[i].func(L);
107     return 1;
108 }
109