| 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 |
|
| 15 |
/*=========================================================================*\ |
| 16 |
* Standard include files |
| 17 |
\*=========================================================================*/ |
| 18 |
#include "lua.h" |
| 19 |
#include "lauxlib.h" |
| 20 |
#include "compat.h" |
| 21 |
|
| 22 |
/*=========================================================================*\ |
| 23 |
* LuaSocket includes |
| 24 |
\*=========================================================================*/ |
| 25 |
#include "luasocket.h" |
| 26 |
#include "auxiliar.h" |
| 27 |
#include "except.h" |
| 28 |
#include "timeout.h" |
| 29 |
#include "buffer.h" |
| 30 |
#include "inet.h" |
| 31 |
#include "tcp.h" |
| 32 |
#include "udp.h" |
| 33 |
#include "select.h" |
| 34 |
|
| 35 |
/*-------------------------------------------------------------------------*\ |
| 36 |
* Internal function prototypes |
| 37 |
\*-------------------------------------------------------------------------*/ |
| 38 |
static int global_skip(lua_State *L); |
| 39 |
static int global_unload(lua_State *L); |
| 40 |
static int base_open(lua_State *L); |
| 41 |
|
| 42 |
/*-------------------------------------------------------------------------*\ |
| 43 |
* Modules and functions |
| 44 |
\*-------------------------------------------------------------------------*/ |
| 45 |
static const luaL_Reg mod[] = { |
| 46 |
{"auxiliar", auxiliar_open}, |
| 47 |
{"except", except_open}, |
| 48 |
{"timeout", timeout_open}, |
| 49 |
{"buffer", buffer_open}, |
| 50 |
{"inet", inet_open}, |
| 51 |
{"tcp", tcp_open}, |
| 52 |
{"udp", udp_open}, |
| 53 |
{"select", select_open}, |
| 54 |
{NULL, NULL} |
| 55 |
}; |
| 56 |
|
| 57 |
static luaL_Reg func[] = { |
| 58 |
{"skip", global_skip}, |
| 59 |
{"__unload", global_unload}, |
| 60 |
{NULL, NULL} |
| 61 |
}; |
| 62 |
|
| 63 |
/*-------------------------------------------------------------------------*\ |
| 64 |
* Skip a few arguments |
| 65 |
\*-------------------------------------------------------------------------*/ |
| 66 |
static int global_skip(lua_State *L) { |
| 67 |
int amount = luaL_checkinteger(L, 1); |
| 68 |
int ret = lua_gettop(L) - amount - 1; |
| 69 |
return ret >= 0 ? ret : 0; |
| 70 |
} |
| 71 |
|
| 72 |
/*-------------------------------------------------------------------------*\ |
| 73 |
* Unloads the library |
| 74 |
\*-------------------------------------------------------------------------*/ |
| 75 |
static int global_unload(lua_State *L) { |
| 76 |
(void) L; |
| 77 |
socket_close(); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
/*-------------------------------------------------------------------------*\ |
| 82 |
* Setup basic stuff. |
| 83 |
\*-------------------------------------------------------------------------*/ |
| 84 |
static int base_open(lua_State *L) { |
| 85 |
if (socket_open()) { |
| 86 |
/* export functions (and leave namespace table on top of stack) */ |
| 87 |
lua_newtable(L); |
| 88 |
luaL_setfuncs(L, func, 0); |
| 89 |
#ifdef LUASOCKET_DEBUG |
| 90 |
lua_pushstring(L, "_DEBUG"); |
| 91 |
lua_pushboolean(L, 1); |
| 92 |
lua_rawset(L, -3); |
| 93 |
#endif |
| 94 |
/* make version string available to scripts */ |
| 95 |
lua_pushstring(L, "_VERSION"); |
| 96 |
lua_pushstring(L, LUASOCKET_VERSION); |
| 97 |
lua_rawset(L, -3); |
| 98 |
return 1; |
| 99 |
} else { |
| 100 |
lua_pushstring(L, "unable to initialize library"); |
| 101 |
lua_error(L); |
| 102 |
return 0; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/*-------------------------------------------------------------------------*\ |
| 107 |
* Initializes all library modules. |
| 108 |
\*-------------------------------------------------------------------------*/ |
| 109 |
LUASOCKET_API int luaopen_socket_core(lua_State *L) { |
| 110 |
int i; |
| 111 |
base_open(L); |
| 112 |
for (i = 0; mod[i].name; i++) mod[i].func(L); |
| 113 |
return 1; |
| 114 |
} |