mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-25 12:08:21 +01:00
Merge pull request #271 from ewestbrook/pragmavisibility
Use visibility pragma around declarations instead of attributes on definitions
This commit is contained in:
commit
c89a931cc3
@ -82,6 +82,7 @@ local function make_plat(plat)
|
|||||||
modules["socket.unix"] = {
|
modules["socket.unix"] = {
|
||||||
sources = {
|
sources = {
|
||||||
"src/buffer.c"
|
"src/buffer.c"
|
||||||
|
, "src/compat.c"
|
||||||
, "src/auxiliar.c"
|
, "src/auxiliar.c"
|
||||||
, "src/options.c"
|
, "src/options.c"
|
||||||
, "src/timeout.c"
|
, "src/timeout.c"
|
||||||
@ -96,6 +97,7 @@ local function make_plat(plat)
|
|||||||
modules["socket.serial"] = {
|
modules["socket.serial"] = {
|
||||||
sources = {
|
sources = {
|
||||||
"src/buffer.c"
|
"src/buffer.c"
|
||||||
|
, "src/compat.c"
|
||||||
, "src/auxiliar.c"
|
, "src/auxiliar.c"
|
||||||
, "src/options.c"
|
, "src/options.c"
|
||||||
, "src/timeout.c"
|
, "src/timeout.c"
|
||||||
|
@ -82,6 +82,7 @@ local function make_plat(plat)
|
|||||||
modules["socket.unix"] = {
|
modules["socket.unix"] = {
|
||||||
sources = {
|
sources = {
|
||||||
"src/buffer.c"
|
"src/buffer.c"
|
||||||
|
, "src/compat.c"
|
||||||
, "src/auxiliar.c"
|
, "src/auxiliar.c"
|
||||||
, "src/options.c"
|
, "src/options.c"
|
||||||
, "src/timeout.c"
|
, "src/timeout.c"
|
||||||
@ -96,6 +97,7 @@ local function make_plat(plat)
|
|||||||
modules["socket.serial"] = {
|
modules["socket.serial"] = {
|
||||||
sources = {
|
sources = {
|
||||||
"src/buffer.c"
|
"src/buffer.c"
|
||||||
|
, "src/compat.c"
|
||||||
, "src/auxiliar.c"
|
, "src/auxiliar.c"
|
||||||
, "src/options.c"
|
, "src/options.c"
|
||||||
, "src/timeout.c"
|
, "src/timeout.c"
|
||||||
|
@ -7,13 +7,10 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
/*=========================================================================*\
|
|
||||||
* Exported functions
|
|
||||||
\*=========================================================================*/
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes the module
|
* Initializes the module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int auxiliar_open(lua_State *L) {
|
int auxiliar_open(lua_State *L) {
|
||||||
(void) L;
|
(void) L;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -22,7 +19,7 @@ LUASOCKET_PRIVATE int auxiliar_open(lua_State *L) {
|
|||||||
* Creates a new class with given methods
|
* Creates a new class with given methods
|
||||||
* Methods whose names start with __ are passed directly to the metatable.
|
* Methods whose names start with __ are passed directly to the metatable.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) {
|
void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) {
|
||||||
luaL_newmetatable(L, classname); /* mt */
|
luaL_newmetatable(L, classname); /* mt */
|
||||||
/* create __index table to place methods */
|
/* create __index table to place methods */
|
||||||
lua_pushstring(L, "__index"); /* mt,"__index" */
|
lua_pushstring(L, "__index"); /* mt,"__index" */
|
||||||
@ -45,7 +42,7 @@ LUASOCKET_PRIVATE void auxiliar_newclass(lua_State *L, const char *classname, lu
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Prints the value of a class in a nice way
|
* Prints the value of a class in a nice way
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int auxiliar_tostring(lua_State *L) {
|
int auxiliar_tostring(lua_State *L) {
|
||||||
char buf[32];
|
char buf[32];
|
||||||
if (!lua_getmetatable(L, 1)) goto error;
|
if (!lua_getmetatable(L, 1)) goto error;
|
||||||
lua_pushstring(L, "__index");
|
lua_pushstring(L, "__index");
|
||||||
@ -66,7 +63,7 @@ error:
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Insert class into group
|
* Insert class into group
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) {
|
void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) {
|
||||||
luaL_getmetatable(L, classname);
|
luaL_getmetatable(L, classname);
|
||||||
lua_pushstring(L, groupname);
|
lua_pushstring(L, groupname);
|
||||||
lua_pushboolean(L, 1);
|
lua_pushboolean(L, 1);
|
||||||
@ -77,7 +74,7 @@ LUASOCKET_PRIVATE void auxiliar_add2group(lua_State *L, const char *classname, c
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Make sure argument is a boolean
|
* Make sure argument is a boolean
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int auxiliar_checkboolean(lua_State *L, int objidx) {
|
int auxiliar_checkboolean(lua_State *L, int objidx) {
|
||||||
if (!lua_isboolean(L, objidx))
|
if (!lua_isboolean(L, objidx))
|
||||||
auxiliar_typeerror(L, objidx, lua_typename(L, LUA_TBOOLEAN));
|
auxiliar_typeerror(L, objidx, lua_typename(L, LUA_TBOOLEAN));
|
||||||
return lua_toboolean(L, objidx);
|
return lua_toboolean(L, objidx);
|
||||||
@ -87,7 +84,7 @@ LUASOCKET_PRIVATE int auxiliar_checkboolean(lua_State *L, int objidx) {
|
|||||||
* Return userdata pointer if object belongs to a given class, abort with
|
* Return userdata pointer if object belongs to a given class, abort with
|
||||||
* error otherwise
|
* error otherwise
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) {
|
void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) {
|
||||||
void *data = auxiliar_getclassudata(L, classname, objidx);
|
void *data = auxiliar_getclassudata(L, classname, objidx);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
char msg[45];
|
char msg[45];
|
||||||
@ -101,7 +98,7 @@ LUASOCKET_PRIVATE void *auxiliar_checkclass(lua_State *L, const char *classname,
|
|||||||
* Return userdata pointer if object belongs to a given group, abort with
|
* Return userdata pointer if object belongs to a given group, abort with
|
||||||
* error otherwise
|
* error otherwise
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) {
|
void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) {
|
||||||
void *data = auxiliar_getgroupudata(L, groupname, objidx);
|
void *data = auxiliar_getgroupudata(L, groupname, objidx);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
char msg[45];
|
char msg[45];
|
||||||
@ -114,7 +111,7 @@ LUASOCKET_PRIVATE void *auxiliar_checkgroup(lua_State *L, const char *groupname,
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Set object class
|
* Set object class
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void auxiliar_setclass(lua_State *L, const char *classname, int objidx) {
|
void auxiliar_setclass(lua_State *L, const char *classname, int objidx) {
|
||||||
luaL_getmetatable(L, classname);
|
luaL_getmetatable(L, classname);
|
||||||
if (objidx < 0) objidx--;
|
if (objidx < 0) objidx--;
|
||||||
lua_setmetatable(L, objidx);
|
lua_setmetatable(L, objidx);
|
||||||
@ -124,7 +121,7 @@ LUASOCKET_PRIVATE void auxiliar_setclass(lua_State *L, const char *classname, in
|
|||||||
* Get a userdata pointer if object belongs to a given group. Return NULL
|
* Get a userdata pointer if object belongs to a given group. Return NULL
|
||||||
* otherwise
|
* otherwise
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) {
|
void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) {
|
||||||
if (!lua_getmetatable(L, objidx))
|
if (!lua_getmetatable(L, objidx))
|
||||||
return NULL;
|
return NULL;
|
||||||
lua_pushstring(L, groupname);
|
lua_pushstring(L, groupname);
|
||||||
@ -142,7 +139,7 @@ LUASOCKET_PRIVATE void *auxiliar_getgroupudata(lua_State *L, const char *groupna
|
|||||||
* Get a userdata pointer if object belongs to a given class. Return NULL
|
* Get a userdata pointer if object belongs to a given class. Return NULL
|
||||||
* otherwise
|
* otherwise
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) {
|
void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) {
|
||||||
return luaL_testudata(L, objidx, classname);
|
return luaL_testudata(L, objidx, classname);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,9 +147,8 @@ LUASOCKET_PRIVATE void *auxiliar_getclassudata(lua_State *L, const char *classna
|
|||||||
* Throws error when argument does not have correct type.
|
* Throws error when argument does not have correct type.
|
||||||
* Used to be part of lauxlib in Lua 5.1, was dropped from 5.2.
|
* Used to be part of lauxlib in Lua 5.1, was dropped from 5.2.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int auxiliar_typeerror (lua_State *L, int narg, const char *tname) {
|
int auxiliar_typeerror (lua_State *L, int narg, const char *tname) {
|
||||||
const char *msg = lua_pushfstring(L, "%s expected, got %s", tname,
|
const char *msg = lua_pushfstring(L, "%s expected, got %s", tname,
|
||||||
luaL_typename(L, narg));
|
luaL_typename(L, narg));
|
||||||
return luaL_argerror(L, narg, msg);
|
return luaL_argerror(L, narg, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,20 +29,26 @@
|
|||||||
* reverse mapping are done using lauxlib.
|
* reverse mapping are done using lauxlib.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
|
||||||
#include "lua.h"
|
#include "luasocket.h"
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
int auxiliar_open(lua_State *L);
|
int auxiliar_open(lua_State *L);
|
||||||
void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func);
|
void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func);
|
||||||
|
int auxiliar_tostring(lua_State *L);
|
||||||
void auxiliar_add2group(lua_State *L, const char *classname, const char *group);
|
void auxiliar_add2group(lua_State *L, const char *classname, const char *group);
|
||||||
void auxiliar_setclass(lua_State *L, const char *classname, int objidx);
|
int auxiliar_checkboolean(lua_State *L, int objidx);
|
||||||
void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx);
|
void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx);
|
||||||
void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx);
|
void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx);
|
||||||
void *auxiliar_getclassudata(lua_State *L, const char *groupname, int objidx);
|
void auxiliar_setclass(lua_State *L, const char *classname, int objidx);
|
||||||
void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx);
|
void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx);
|
||||||
int auxiliar_checkboolean(lua_State *L, int objidx);
|
void *auxiliar_getclassudata(lua_State *L, const char *groupname, int objidx);
|
||||||
int auxiliar_tostring(lua_State *L);
|
|
||||||
int auxiliar_typeerror(lua_State *L, int narg, const char *tname);
|
int auxiliar_typeerror(lua_State *L, int narg, const char *tname);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* AUXILIAR_H */
|
#endif /* AUXILIAR_H */
|
||||||
|
19
src/buffer.c
19
src/buffer.c
@ -3,11 +3,6 @@
|
|||||||
* LuaSocket toolkit
|
* LuaSocket toolkit
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
|
|
||||||
/*=========================================================================*\
|
/*=========================================================================*\
|
||||||
@ -34,7 +29,7 @@ static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent);
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int buffer_open(lua_State *L) {
|
int buffer_open(lua_State *L) {
|
||||||
(void) L;
|
(void) L;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -42,7 +37,7 @@ LUASOCKET_PRIVATE int buffer_open(lua_State *L) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes C structure
|
* Initializes C structure
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
|
void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
|
||||||
buf->first = buf->last = 0;
|
buf->first = buf->last = 0;
|
||||||
buf->io = io;
|
buf->io = io;
|
||||||
buf->tm = tm;
|
buf->tm = tm;
|
||||||
@ -53,7 +48,7 @@ LUASOCKET_PRIVATE void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* object:getstats() interface
|
* object:getstats() interface
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int buffer_meth_getstats(lua_State *L, p_buffer buf) {
|
int buffer_meth_getstats(lua_State *L, p_buffer buf) {
|
||||||
lua_pushnumber(L, (lua_Number) buf->received);
|
lua_pushnumber(L, (lua_Number) buf->received);
|
||||||
lua_pushnumber(L, (lua_Number) buf->sent);
|
lua_pushnumber(L, (lua_Number) buf->sent);
|
||||||
lua_pushnumber(L, timeout_gettime() - buf->birthday);
|
lua_pushnumber(L, timeout_gettime() - buf->birthday);
|
||||||
@ -63,7 +58,7 @@ LUASOCKET_PRIVATE int buffer_meth_getstats(lua_State *L, p_buffer buf) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* object:setstats() interface
|
* object:setstats() interface
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int buffer_meth_setstats(lua_State *L, p_buffer buf) {
|
int buffer_meth_setstats(lua_State *L, p_buffer buf) {
|
||||||
buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received);
|
buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received);
|
||||||
buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent);
|
buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent);
|
||||||
if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4);
|
if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4);
|
||||||
@ -74,7 +69,7 @@ LUASOCKET_PRIVATE int buffer_meth_setstats(lua_State *L, p_buffer buf) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* object:send() interface
|
* object:send() interface
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int buffer_meth_send(lua_State *L, p_buffer buf) {
|
int buffer_meth_send(lua_State *L, p_buffer buf) {
|
||||||
int top = lua_gettop(L);
|
int top = lua_gettop(L);
|
||||||
int err = IO_DONE;
|
int err = IO_DONE;
|
||||||
size_t size = 0, sent = 0;
|
size_t size = 0, sent = 0;
|
||||||
@ -107,7 +102,7 @@ LUASOCKET_PRIVATE int buffer_meth_send(lua_State *L, p_buffer buf) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* object:receive() interface
|
* object:receive() interface
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int buffer_meth_receive(lua_State *L, p_buffer buf) {
|
int buffer_meth_receive(lua_State *L, p_buffer buf) {
|
||||||
int err = IO_DONE, top = lua_gettop(L);
|
int err = IO_DONE, top = lua_gettop(L);
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
size_t size;
|
size_t size;
|
||||||
@ -156,7 +151,7 @@ LUASOCKET_PRIVATE int buffer_meth_receive(lua_State *L, p_buffer buf) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Determines if there is any data in the read buffer
|
* Determines if there is any data in the read buffer
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int buffer_isempty(p_buffer buf) {
|
int buffer_isempty(p_buffer buf) {
|
||||||
return buf->first >= buf->last;
|
return buf->first >= buf->last;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
src/buffer.h
15
src/buffer.h
@ -15,8 +15,7 @@
|
|||||||
* The module is built on top of the I/O abstraction defined in io.h and the
|
* The module is built on top of the I/O abstraction defined in io.h and the
|
||||||
* timeout management is done with the timeout.h interface.
|
* timeout management is done with the timeout.h interface.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "lua.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
|
||||||
@ -34,12 +33,20 @@ typedef struct t_buffer_ {
|
|||||||
} t_buffer;
|
} t_buffer;
|
||||||
typedef t_buffer *p_buffer;
|
typedef t_buffer *p_buffer;
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
int buffer_open(lua_State *L);
|
int buffer_open(lua_State *L);
|
||||||
void buffer_init(p_buffer buf, p_io io, p_timeout tm);
|
void buffer_init(p_buffer buf, p_io io, p_timeout tm);
|
||||||
int buffer_meth_send(lua_State *L, p_buffer buf);
|
|
||||||
int buffer_meth_receive(lua_State *L, p_buffer buf);
|
|
||||||
int buffer_meth_getstats(lua_State *L, p_buffer buf);
|
int buffer_meth_getstats(lua_State *L, p_buffer buf);
|
||||||
int buffer_meth_setstats(lua_State *L, p_buffer buf);
|
int buffer_meth_setstats(lua_State *L, p_buffer buf);
|
||||||
|
int buffer_meth_send(lua_State *L, p_buffer buf);
|
||||||
|
int buffer_meth_receive(lua_State *L, p_buffer buf);
|
||||||
int buffer_isempty(p_buffer buf);
|
int buffer_isempty(p_buffer buf);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* BUF_H */
|
#endif /* BUF_H */
|
||||||
|
@ -2,10 +2,11 @@
|
|||||||
#include "compat.h"
|
#include "compat.h"
|
||||||
|
|
||||||
#if LUA_VERSION_NUM==501
|
#if LUA_VERSION_NUM==501
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Adapted from Lua 5.2
|
** Adapted from Lua 5.2
|
||||||
*/
|
*/
|
||||||
LUASOCKET_PRIVATE void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
|
void luasocket_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
|
||||||
luaL_checkstack(L, nup+1, "too many upvalues");
|
luaL_checkstack(L, nup+1, "too many upvalues");
|
||||||
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
||||||
int i;
|
int i;
|
||||||
@ -21,7 +22,7 @@ LUASOCKET_PRIVATE void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup)
|
|||||||
/*
|
/*
|
||||||
** Duplicated from Lua 5.2
|
** Duplicated from Lua 5.2
|
||||||
*/
|
*/
|
||||||
LUASOCKET_PRIVATE void *luaL_testudata (lua_State *L, int ud, const char *tname) {
|
void *luasocket_testudata (lua_State *L, int ud, const char *tname) {
|
||||||
void *p = lua_touserdata(L, ud);
|
void *p = lua_touserdata(L, ud);
|
||||||
if (p != NULL) { /* value is a userdata? */
|
if (p != NULL) { /* value is a userdata? */
|
||||||
if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
|
if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
|
||||||
|
22
src/compat.h
22
src/compat.h
@ -1,14 +1,22 @@
|
|||||||
#ifndef COMPAT_H
|
#ifndef COMPAT_H
|
||||||
#define COMPAT_H
|
#define COMPAT_H
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
|
|
||||||
#if LUA_VERSION_NUM==501
|
#if LUA_VERSION_NUM==501
|
||||||
#define luaL_setfuncs socket_setfuncs
|
|
||||||
#define luaL_testudata socket_testudata
|
#ifndef _WIN32
|
||||||
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
|
#pragma GCC visibility push(hidden)
|
||||||
void *luaL_testudata ( lua_State *L, int arg, const char *tname);
|
#endif
|
||||||
|
|
||||||
|
void luasocket_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
|
||||||
|
void *luasocket_testudata ( lua_State *L, int arg, const char *tname);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define luaL_setfuncs luasocket_setfuncs
|
||||||
|
#define luaL_testudata luasocket_testudata
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
10
src/except.c
10
src/except.c
@ -3,14 +3,8 @@
|
|||||||
* LuaSocket toolkit
|
* LuaSocket toolkit
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
#include "except.h"
|
#include "except.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#if LUA_VERSION_NUM < 502
|
#if LUA_VERSION_NUM < 502
|
||||||
#define lua_pcallk(L, na, nr, err, ctx, cont) \
|
#define lua_pcallk(L, na, nr, err, ctx, cont) \
|
||||||
@ -126,7 +120,7 @@ static int global_protect(lua_State *L) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Init module
|
* Init module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int except_open(lua_State *L) {
|
int except_open(lua_State *L) {
|
||||||
lua_newtable(L); /* metatable for wrapped exceptions */
|
lua_newtable(L); /* metatable for wrapped exceptions */
|
||||||
lua_pushboolean(L, 0);
|
lua_pushboolean(L, 0);
|
||||||
lua_setfield(L, -2, "__metatable");
|
lua_setfield(L, -2, "__metatable");
|
||||||
|
10
src/except.h
10
src/except.h
@ -31,8 +31,16 @@
|
|||||||
* exceptions on error, but that don't interrupt the user script.
|
* exceptions on error, but that don't interrupt the user script.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
|
||||||
#include "lua.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
int except_open(lua_State *L);
|
int except_open(lua_State *L);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
34
src/inet.c
34
src/inet.c
@ -3,17 +3,12 @@
|
|||||||
* LuaSocket toolkit
|
* LuaSocket toolkit
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
#include "inet.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
#include "inet.h"
|
|
||||||
|
|
||||||
/*=========================================================================*\
|
/*=========================================================================*\
|
||||||
* Internal function prototypes.
|
* Internal function prototypes.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
@ -34,13 +29,10 @@ static luaL_Reg func[] = {
|
|||||||
{ NULL, NULL}
|
{ NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*=========================================================================*\
|
|
||||||
* Exported functions
|
|
||||||
\*=========================================================================*/
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int inet_open(lua_State *L)
|
int inet_open(lua_State *L)
|
||||||
{
|
{
|
||||||
lua_pushstring(L, "dns");
|
lua_pushstring(L, "dns");
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
@ -145,7 +137,7 @@ static int inet_global_toip(lua_State *L)
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int inet_optfamily(lua_State* L, int narg, const char* def)
|
int inet_optfamily(lua_State* L, int narg, const char* def)
|
||||||
{
|
{
|
||||||
static const char* optname[] = { "unspec", "inet", "inet6", NULL };
|
static const char* optname[] = { "unspec", "inet", "inet6", NULL };
|
||||||
static int optvalue[] = { AF_UNSPEC, AF_INET, AF_INET6, 0 };
|
static int optvalue[] = { AF_UNSPEC, AF_INET, AF_INET6, 0 };
|
||||||
@ -153,7 +145,7 @@ LUASOCKET_PRIVATE int inet_optfamily(lua_State* L, int narg, const char* def)
|
|||||||
return optvalue[luaL_checkoption(L, narg, def, optname)];
|
return optvalue[luaL_checkoption(L, narg, def, optname)];
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int inet_optsocktype(lua_State* L, int narg, const char* def)
|
int inet_optsocktype(lua_State* L, int narg, const char* def)
|
||||||
{
|
{
|
||||||
static const char* optname[] = { "stream", "dgram", NULL };
|
static const char* optname[] = { "stream", "dgram", NULL };
|
||||||
static int optvalue[] = { SOCK_STREAM, SOCK_DGRAM, 0 };
|
static int optvalue[] = { SOCK_STREAM, SOCK_DGRAM, 0 };
|
||||||
@ -244,7 +236,7 @@ static int inet_global_gethostname(lua_State *L)
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Retrieves socket peer name
|
* Retrieves socket peer name
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
|
int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
struct sockaddr_storage peer;
|
struct sockaddr_storage peer;
|
||||||
@ -278,7 +270,7 @@ LUASOCKET_PRIVATE int inet_meth_getpeername(lua_State *L, p_socket ps, int famil
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Retrieves socket local name
|
* Retrieves socket local name
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int inet_meth_getsockname(lua_State *L, p_socket ps, int family)
|
int inet_meth_getsockname(lua_State *L, p_socket ps, int family)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
struct sockaddr_storage peer;
|
struct sockaddr_storage peer;
|
||||||
@ -354,7 +346,7 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Tries to create a new inet socket
|
* Tries to create a new inet socket
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE const char *inet_trycreate(p_socket ps, int family, int type, int protocol) {
|
const char *inet_trycreate(p_socket ps, int family, int type, int protocol) {
|
||||||
const char *err = socket_strerror(socket_create(ps, family, type, protocol));
|
const char *err = socket_strerror(socket_create(ps, family, type, protocol));
|
||||||
if (err == NULL && family == AF_INET6) {
|
if (err == NULL && family == AF_INET6) {
|
||||||
int yes = 1;
|
int yes = 1;
|
||||||
@ -366,7 +358,7 @@ LUASOCKET_PRIVATE const char *inet_trycreate(p_socket ps, int family, int type,
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* "Disconnects" a DGRAM socket
|
* "Disconnects" a DGRAM socket
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm)
|
const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm)
|
||||||
{
|
{
|
||||||
switch (family) {
|
switch (family) {
|
||||||
case AF_INET: {
|
case AF_INET: {
|
||||||
@ -393,7 +385,7 @@ LUASOCKET_PRIVATE const char *inet_trydisconnect(p_socket ps, int family, p_time
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Tries to connect to remote address (address, port)
|
* Tries to connect to remote address (address, port)
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE const char *inet_tryconnect(p_socket ps, int *family, const char *address,
|
const char *inet_tryconnect(p_socket ps, int *family, const char *address,
|
||||||
const char *serv, p_timeout tm, struct addrinfo *connecthints)
|
const char *serv, p_timeout tm, struct addrinfo *connecthints)
|
||||||
{
|
{
|
||||||
struct addrinfo *iterator = NULL, *resolved = NULL;
|
struct addrinfo *iterator = NULL, *resolved = NULL;
|
||||||
@ -439,7 +431,7 @@ LUASOCKET_PRIVATE const char *inet_tryconnect(p_socket ps, int *family, const ch
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Tries to accept a socket
|
* Tries to accept a socket
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE const char *inet_tryaccept(p_socket server, int family, p_socket client,
|
const char *inet_tryaccept(p_socket server, int family, p_socket client,
|
||||||
p_timeout tm) {
|
p_timeout tm) {
|
||||||
socklen_t len;
|
socklen_t len;
|
||||||
t_sockaddr_storage addr;
|
t_sockaddr_storage addr;
|
||||||
@ -455,7 +447,7 @@ LUASOCKET_PRIVATE const char *inet_tryaccept(p_socket server, int family, p_sock
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Tries to bind socket to (address, port)
|
* Tries to bind socket to (address, port)
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE const char *inet_trybind(p_socket ps, int *family, const char *address,
|
const char *inet_trybind(p_socket ps, int *family, const char *address,
|
||||||
const char *serv, struct addrinfo *bindhints) {
|
const char *serv, struct addrinfo *bindhints) {
|
||||||
struct addrinfo *iterator = NULL, *resolved = NULL;
|
struct addrinfo *iterator = NULL, *resolved = NULL;
|
||||||
const char *err = NULL;
|
const char *err = NULL;
|
||||||
@ -499,7 +491,7 @@ LUASOCKET_PRIVATE const char *inet_trybind(p_socket ps, int *family, const char
|
|||||||
* Some systems do not provide these so that we provide our own.
|
* Some systems do not provide these so that we provide our own.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
#ifdef LUASOCKET_INET_ATON
|
#ifdef LUASOCKET_INET_ATON
|
||||||
LUASOCKET_PRIVATE int inet_aton(const char *cp, struct in_addr *inp)
|
int inet_aton(const char *cp, struct in_addr *inp)
|
||||||
{
|
{
|
||||||
unsigned int a = 0, b = 0, c = 0, d = 0;
|
unsigned int a = 0, b = 0, c = 0, d = 0;
|
||||||
int n = 0, r;
|
int n = 0, r;
|
||||||
@ -521,7 +513,7 @@ LUASOCKET_PRIVATE int inet_aton(const char *cp, struct in_addr *inp)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef LUASOCKET_INET_PTON
|
#ifdef LUASOCKET_INET_PTON
|
||||||
LUASOCKET_PRIVATE int inet_pton(int af, const char *src, void *dst)
|
int inet_pton(int af, const char *src, void *dst)
|
||||||
{
|
{
|
||||||
struct addrinfo hints, *res;
|
struct addrinfo hints, *res;
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
|
26
src/inet.h
26
src/inet.h
@ -14,7 +14,7 @@
|
|||||||
*
|
*
|
||||||
* The Lua functions toip and tohostname are also implemented here.
|
* The Lua functions toip and tohostname are also implemented here.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "lua.h"
|
#include "luasocket.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
|
||||||
@ -22,21 +22,23 @@
|
|||||||
#define LUASOCKET_INET_ATON
|
#define LUASOCKET_INET_ATON
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
int inet_open(lua_State *L);
|
int inet_open(lua_State *L);
|
||||||
|
|
||||||
const char *inet_trycreate(p_socket ps, int family, int type, int protocol);
|
int inet_optfamily(lua_State* L, int narg, const char* def);
|
||||||
const char *inet_tryconnect(p_socket ps, int *family, const char *address,
|
int inet_optsocktype(lua_State* L, int narg, const char* def);
|
||||||
const char *serv, p_timeout tm, struct addrinfo *connecthints);
|
|
||||||
const char *inet_trybind(p_socket ps, int *family, const char *address,
|
|
||||||
const char *serv, struct addrinfo *bindhints);
|
|
||||||
const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm);
|
|
||||||
const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm);
|
|
||||||
|
|
||||||
int inet_meth_getpeername(lua_State *L, p_socket ps, int family);
|
int inet_meth_getpeername(lua_State *L, p_socket ps, int family);
|
||||||
int inet_meth_getsockname(lua_State *L, p_socket ps, int family);
|
int inet_meth_getsockname(lua_State *L, p_socket ps, int family);
|
||||||
|
|
||||||
int inet_optfamily(lua_State* L, int narg, const char* def);
|
const char *inet_trycreate(p_socket ps, int family, int type, int protocol);
|
||||||
int inet_optsocktype(lua_State* L, int narg, const char* def);
|
const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm);
|
||||||
|
const char *inet_tryconnect(p_socket ps, int *family, const char *address, const char *serv, p_timeout tm, struct addrinfo *connecthints);
|
||||||
|
const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm);
|
||||||
|
const char *inet_trybind(p_socket ps, int *family, const char *address, const char *serv, struct addrinfo *bindhints);
|
||||||
|
|
||||||
#ifdef LUASOCKET_INET_ATON
|
#ifdef LUASOCKET_INET_ATON
|
||||||
int inet_aton(const char *cp, struct in_addr *inp);
|
int inet_aton(const char *cp, struct in_addr *inp);
|
||||||
@ -47,4 +49,8 @@ const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
|
|||||||
int inet_pton(int af, const char *src, void *dst);
|
int inet_pton(int af, const char *src, void *dst);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* INET_H */
|
#endif /* INET_H */
|
||||||
|
7
src/io.c
7
src/io.c
@ -5,13 +5,10 @@
|
|||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
|
||||||
/*=========================================================================*\
|
|
||||||
* Exported functions
|
|
||||||
\*=========================================================================*/
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes C structure
|
* Initializes C structure
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx) {
|
void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx) {
|
||||||
io->send = send;
|
io->send = send;
|
||||||
io->recv = recv;
|
io->recv = recv;
|
||||||
io->error = error;
|
io->error = error;
|
||||||
@ -21,7 +18,7 @@ LUASOCKET_PRIVATE void io_init(p_io io, p_send send, p_recv recv, p_error error,
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* I/O error strings
|
* I/O error strings
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE const char *io_strerror(int err) {
|
const char *io_strerror(int err) {
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case IO_DONE: return NULL;
|
case IO_DONE: return NULL;
|
||||||
case IO_CLOSED: return "closed";
|
case IO_CLOSED: return "closed";
|
||||||
|
13
src/io.h
13
src/io.h
@ -12,9 +12,7 @@
|
|||||||
* The module socket.h implements this interface, and thus the module tcp.h
|
* The module socket.h implements this interface, and thus the module tcp.h
|
||||||
* is very simple.
|
* is very simple.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include <stdio.h>
|
#include "luasocket.h"
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
|
||||||
/* IO error codes */
|
/* IO error codes */
|
||||||
@ -58,8 +56,15 @@ typedef struct t_io_ {
|
|||||||
} t_io;
|
} t_io;
|
||||||
typedef t_io *p_io;
|
typedef t_io *p_io;
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx);
|
void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx);
|
||||||
const char *io_strerror(int err);
|
const char *io_strerror(int err);
|
||||||
|
|
||||||
#endif /* IO_H */
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* IO_H */
|
||||||
|
@ -12,16 +12,6 @@
|
|||||||
* standard Lua read and write functions.
|
* standard Lua read and write functions.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
|
||||||
/*=========================================================================*\
|
|
||||||
* Standard include files
|
|
||||||
\*=========================================================================*/
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
/*=========================================================================*\
|
|
||||||
* LuaSocket includes
|
|
||||||
\*=========================================================================*/
|
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
#include "auxiliar.h"
|
#include "auxiliar.h"
|
||||||
#include "except.h"
|
#include "except.h"
|
||||||
|
@ -6,9 +6,8 @@
|
|||||||
* Diego Nehab
|
* Diego Nehab
|
||||||
* 9/11/1999
|
* 9/11/1999
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------* \
|
||||||
* Current socket library version
|
* Current socket library version
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
#define LUASOCKET_VERSION "LuaSocket 3.0-rc1"
|
#define LUASOCKET_VERSION "LuaSocket 3.0-rc1"
|
||||||
@ -25,13 +24,9 @@
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef LUASOCKET_PRIVATE
|
#include "lua.h"
|
||||||
#ifdef _WIN32
|
#include "lauxlib.h"
|
||||||
#define LUASOCKET_PRIVATE
|
#include "compat.h"
|
||||||
#else
|
|
||||||
#define LUASOCKET_PRIVATE __attribute__ ((visibility ("hidden")))
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes the library.
|
* Initializes the library.
|
||||||
|
@ -336,6 +336,7 @@ UNIX_OBJS=\
|
|||||||
#
|
#
|
||||||
SERIAL_OBJS=\
|
SERIAL_OBJS=\
|
||||||
buffer.$(O) \
|
buffer.$(O) \
|
||||||
|
compat.$(O) \
|
||||||
auxiliar.$(O) \
|
auxiliar.$(O) \
|
||||||
options.$(O) \
|
options.$(O) \
|
||||||
timeout.$(O) \
|
timeout.$(O) \
|
||||||
|
@ -2,15 +2,11 @@
|
|||||||
* MIME support functions
|
* MIME support functions
|
||||||
* LuaSocket toolkit
|
* LuaSocket toolkit
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
#include "luasocket.h"
|
||||||
|
#include "mime.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
#include "mime.h"
|
|
||||||
|
|
||||||
/*=========================================================================*\
|
/*=========================================================================*\
|
||||||
* Don't want to trust escape character constants
|
* Don't want to trust escape character constants
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
* provide a higher level interface to this functionality.
|
* provide a higher level interface to this functionality.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Current MIME library version
|
* Current MIME library version
|
||||||
|
176
src/options.c
176
src/options.c
@ -3,7 +3,6 @@
|
|||||||
* LuaSocket toolkit
|
* LuaSocket toolkit
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "auxiliar.h"
|
#include "auxiliar.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "inet.h"
|
#include "inet.h"
|
||||||
@ -29,7 +28,7 @@ static int opt_get(lua_State *L, p_socket ps, int level, int name,
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Calls appropriate option handler
|
* Calls appropriate option handler
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
|
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
|
||||||
{
|
{
|
||||||
const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
|
const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
|
||||||
while (opt->name && strcmp(name, opt->name))
|
while (opt->name && strcmp(name, opt->name))
|
||||||
@ -42,7 +41,7 @@ LUASOCKET_PRIVATE int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
|
|||||||
return opt->func(L, ps);
|
return opt->func(L, ps);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
|
int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
|
||||||
{
|
{
|
||||||
const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
|
const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
|
||||||
while (opt->name && strcmp(name, opt->name))
|
while (opt->name && strcmp(name, opt->name))
|
||||||
@ -55,166 +54,188 @@ LUASOCKET_PRIVATE int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
|
|||||||
return opt->func(L, ps);
|
return opt->func(L, ps);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
/* enables reuse of local address */
|
/* enables reuse of local address */
|
||||||
LUASOCKET_PRIVATE int opt_set_reuseaddr(lua_State *L, p_socket ps)
|
int opt_set_reuseaddr(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
|
return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_reuseaddr(lua_State *L, p_socket ps)
|
int opt_get_reuseaddr(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
|
return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
/* enables reuse of local port */
|
/* enables reuse of local port */
|
||||||
LUASOCKET_PRIVATE int opt_set_reuseport(lua_State *L, p_socket ps)
|
int opt_set_reuseport(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
|
return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_reuseport(lua_State *L, p_socket ps)
|
int opt_get_reuseport(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
|
return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* disables the Naggle algorithm */
|
// -------------------------------------------------------
|
||||||
LUASOCKET_PRIVATE int opt_set_tcp_nodelay(lua_State *L, p_socket ps)
|
/* disables the Nagle algorithm */
|
||||||
|
int opt_set_tcp_nodelay(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
|
return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_tcp_nodelay(lua_State *L, p_socket ps)
|
int opt_get_tcp_nodelay(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
|
return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
#ifdef TCP_KEEPIDLE
|
#ifdef TCP_KEEPIDLE
|
||||||
LUASOCKET_PRIVATE int opt_get_tcp_keepidle(lua_State *L, p_socket ps)
|
|
||||||
|
int opt_get_tcp_keepidle(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
|
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_tcp_keepidle(lua_State *L, p_socket ps)
|
int opt_set_tcp_keepidle(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
|
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
#ifdef TCP_KEEPCNT
|
#ifdef TCP_KEEPCNT
|
||||||
LUASOCKET_PRIVATE int opt_get_tcp_keepcnt(lua_State *L, p_socket ps)
|
|
||||||
|
int opt_get_tcp_keepcnt(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
|
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_tcp_keepcnt(lua_State *L, p_socket ps)
|
int opt_set_tcp_keepcnt(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
|
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
#ifdef TCP_KEEPINTVL
|
#ifdef TCP_KEEPINTVL
|
||||||
LUASOCKET_PRIVATE int opt_get_tcp_keepintvl(lua_State *L, p_socket ps)
|
|
||||||
|
int opt_get_tcp_keepintvl(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
|
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_tcp_keepintvl(lua_State *L, p_socket ps)
|
int opt_set_tcp_keepintvl(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
|
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_keepalive(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_keepalive(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
|
return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_keepalive(lua_State *L, p_socket ps)
|
int opt_get_keepalive(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
|
return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_dontroute(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_dontroute(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
|
return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_dontroute(lua_State *L, p_socket ps)
|
int opt_get_dontroute(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
|
return opt_getboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_broadcast(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_broadcast(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
|
return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_recv_buf_size(lua_State *L, p_socket ps)
|
int opt_get_broadcast(lua_State *L, p_socket ps)
|
||||||
{
|
|
||||||
return opt_setint(L, ps, SOL_SOCKET, SO_RCVBUF);
|
|
||||||
}
|
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_recv_buf_size(lua_State *L, p_socket ps)
|
|
||||||
{
|
|
||||||
return opt_getint(L, ps, SOL_SOCKET, SO_RCVBUF);
|
|
||||||
}
|
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_send_buf_size(lua_State *L, p_socket ps)
|
|
||||||
{
|
|
||||||
return opt_getint(L, ps, SOL_SOCKET, SO_SNDBUF);
|
|
||||||
}
|
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_send_buf_size(lua_State *L, p_socket ps)
|
|
||||||
{
|
|
||||||
return opt_setint(L, ps, SOL_SOCKET, SO_SNDBUF);
|
|
||||||
}
|
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_broadcast(lua_State *L, p_socket ps)
|
|
||||||
{
|
{
|
||||||
return opt_getboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
|
return opt_getboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_recv_buf_size(lua_State *L, p_socket ps)
|
||||||
|
{
|
||||||
|
return opt_setint(L, ps, SOL_SOCKET, SO_RCVBUF);
|
||||||
|
}
|
||||||
|
|
||||||
|
int opt_get_recv_buf_size(lua_State *L, p_socket ps)
|
||||||
|
{
|
||||||
|
return opt_getint(L, ps, SOL_SOCKET, SO_RCVBUF);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
|
int opt_get_send_buf_size(lua_State *L, p_socket ps)
|
||||||
|
{
|
||||||
|
return opt_getint(L, ps, SOL_SOCKET, SO_SNDBUF);
|
||||||
|
}
|
||||||
|
|
||||||
|
int opt_set_send_buf_size(lua_State *L, p_socket ps)
|
||||||
|
{
|
||||||
|
return opt_setint(L, ps, SOL_SOCKET, SO_SNDBUF);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
|
int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
|
return opt_setint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps)
|
int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
|
return opt_getint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
|
return opt_setint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps)
|
int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
|
return opt_getint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip_multicast_loop(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_ip_multicast_loop(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
|
return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_ip_multicast_loop(lua_State *L, p_socket ps)
|
int opt_get_ip_multicast_loop(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
|
return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
|
return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps)
|
int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
|
return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_linger(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_linger(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
struct linger li; /* obj, name, table */
|
struct linger li; /* obj, name, table */
|
||||||
if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
|
if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
|
||||||
@ -231,7 +252,7 @@ LUASOCKET_PRIVATE int opt_set_linger(lua_State *L, p_socket ps)
|
|||||||
return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
|
return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_linger(lua_State *L, p_socket ps)
|
int opt_get_linger(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
struct linger li; /* obj, name */
|
struct linger li; /* obj, name */
|
||||||
int len = sizeof(li);
|
int len = sizeof(li);
|
||||||
@ -246,12 +267,14 @@ LUASOCKET_PRIVATE int opt_get_linger(lua_State *L, p_socket ps)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL);
|
return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
const char *address = luaL_checkstring(L, 3); /* obj, name, ip */
|
const char *address = luaL_checkstring(L, 3); /* obj, name, ip */
|
||||||
struct in_addr val;
|
struct in_addr val;
|
||||||
@ -262,7 +285,7 @@ LUASOCKET_PRIVATE int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
|
|||||||
(char *) &val, sizeof(val));
|
(char *) &val, sizeof(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
|
int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
struct in_addr val;
|
struct in_addr val;
|
||||||
socklen_t len = sizeof(val);
|
socklen_t len = sizeof(val);
|
||||||
@ -275,36 +298,52 @@ LUASOCKET_PRIVATE int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip_add_membership(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_ip_add_membership(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
|
return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip_drop_membersip(lua_State *L, p_socket ps)
|
int opt_set_ip_drop_membersip(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
|
return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip6_add_membership(lua_State *L, p_socket ps)
|
// -------------------------------------------------------
|
||||||
|
int opt_set_ip6_add_membership(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP);
|
return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps)
|
int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP);
|
return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP);
|
||||||
}
|
}
|
||||||
|
// -------------------------------------------------------
|
||||||
LUASOCKET_PRIVATE int opt_get_ip6_v6only(lua_State *L, p_socket ps)
|
int opt_get_ip6_v6only(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
|
return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_set_ip6_v6only(lua_State *L, p_socket ps)
|
int opt_set_ip6_v6only(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
|
return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
|
int opt_get_error(lua_State *L, p_socket ps)
|
||||||
|
{
|
||||||
|
int val = 0;
|
||||||
|
socklen_t len = sizeof(val);
|
||||||
|
if (getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *) &val, &len) < 0) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, "getsockopt failed");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
lua_pushstring(L, socket_strerror(val));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/*=========================================================================*\
|
/*=========================================================================*\
|
||||||
* Auxiliar functions
|
* Auxiliar functions
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
@ -391,19 +430,6 @@ static int opt_getboolean(lua_State *L, p_socket ps, int level, int name)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int opt_get_error(lua_State *L, p_socket ps)
|
|
||||||
{
|
|
||||||
int val = 0;
|
|
||||||
socklen_t len = sizeof(val);
|
|
||||||
if (getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *) &val, &len) < 0) {
|
|
||||||
lua_pushnil(L);
|
|
||||||
lua_pushstring(L, "getsockopt failed");
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
lua_pushstring(L, socket_strerror(val));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
|
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
|
||||||
{
|
{
|
||||||
int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */
|
int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */
|
||||||
|
136
src/options.h
136
src/options.h
@ -8,7 +8,7 @@
|
|||||||
* modules UDP and TCP.
|
* modules UDP and TCP.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
|
||||||
#include "lua.h"
|
#include "luasocket.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
|
|
||||||
/* option registry */
|
/* option registry */
|
||||||
@ -18,67 +18,85 @@ typedef struct t_opt {
|
|||||||
} t_opt;
|
} t_opt;
|
||||||
typedef t_opt *p_opt;
|
typedef t_opt *p_opt;
|
||||||
|
|
||||||
/* supported options for setoption */
|
#ifndef _WIN32
|
||||||
int opt_set_dontroute(lua_State *L, p_socket ps);
|
#pragma GCC visibility push(hidden)
|
||||||
int opt_set_broadcast(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_tcp_nodelay(lua_State *L, p_socket ps);
|
|
||||||
#ifdef TCP_KEEPIDLE
|
|
||||||
int opt_set_tcp_keepidle(lua_State *L, p_socket ps);
|
|
||||||
#endif
|
#endif
|
||||||
#ifdef TCP_KEEPCNT
|
|
||||||
int opt_set_tcp_keepcnt(lua_State *L, p_socket ps);
|
|
||||||
#endif
|
|
||||||
#ifdef TCP_KEEPINTVL
|
|
||||||
int opt_set_tcp_keepintvl(lua_State *L, p_socket ps);
|
|
||||||
#endif
|
|
||||||
int opt_set_keepalive(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_linger(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_reuseaddr(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_reuseport(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip_multicast_if(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip_multicast_loop(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip_add_membership(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip_drop_membersip(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip6_add_membership(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_ip6_v6only(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_recv_buf_size(lua_State *L, p_socket ps);
|
|
||||||
int opt_set_send_buf_size(lua_State *L, p_socket ps);
|
|
||||||
|
|
||||||
/* supported options for getoption */
|
|
||||||
int opt_get_dontroute(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_broadcast(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_reuseaddr(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_reuseport(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_tcp_nodelay(lua_State *L, p_socket ps);
|
|
||||||
#ifdef TCP_KEEPIDLE
|
|
||||||
int opt_get_tcp_keepidle(lua_State *L, p_socket ps);
|
|
||||||
#endif
|
|
||||||
#ifdef TCP_KEEPCNT
|
|
||||||
int opt_get_tcp_keepcnt(lua_State *L, p_socket ps);
|
|
||||||
#endif
|
|
||||||
#ifdef TCP_KEEPINTVL
|
|
||||||
int opt_get_tcp_keepintvl(lua_State *L, p_socket ps);
|
|
||||||
#endif
|
|
||||||
int opt_get_keepalive(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_linger(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_ip_multicast_loop(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_ip_multicast_if(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_error(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_ip6_v6only(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_reuseport(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_recv_buf_size(lua_State *L, p_socket ps);
|
|
||||||
int opt_get_send_buf_size(lua_State *L, p_socket ps);
|
|
||||||
|
|
||||||
/* invokes the appropriate option handler */
|
|
||||||
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps);
|
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps);
|
||||||
int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps);
|
int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_reuseaddr(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_reuseaddr(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_reuseport(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_reuseport(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_tcp_nodelay(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_tcp_nodelay(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
#ifdef TCP_KEEPIDLE
|
||||||
|
int opt_set_tcp_keepidle(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_tcp_keepidle(lua_State *L, p_socket ps);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef TCP_KEEPCNT
|
||||||
|
int opt_set_tcp_keepcnt(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_tcp_keepcnt(lua_State *L, p_socket ps);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef TCP_KEEPINTVL
|
||||||
|
int opt_set_tcp_keepintvl(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_tcp_keepintvl(lua_State *L, p_socket ps);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int opt_set_keepalive(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_keepalive(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_dontroute(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_dontroute(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_broadcast(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_broadcast(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_recv_buf_size(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_recv_buf_size(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_send_buf_size(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_send_buf_size(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_ip_multicast_loop(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_ip_multicast_loop(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_linger(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_linger(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_ip_multicast_if(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_ip_multicast_if(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_ip_add_membership(lua_State *L, p_socket ps);
|
||||||
|
int opt_set_ip_drop_membersip(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_ip6_add_membership(lua_State *L, p_socket ps);
|
||||||
|
int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_set_ip6_v6only(lua_State *L, p_socket ps);
|
||||||
|
int opt_get_ip6_v6only(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
int opt_get_error(lua_State *L, p_socket ps);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
10
src/select.c
10
src/select.c
@ -4,10 +4,6 @@
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
#include "select.h"
|
#include "select.h"
|
||||||
@ -33,13 +29,10 @@ static luaL_Reg func[] = {
|
|||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*=========================================================================*\
|
|
||||||
* Exported functions
|
|
||||||
\*=========================================================================*/
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int select_open(lua_State *L) {
|
int select_open(lua_State *L) {
|
||||||
lua_pushstring(L, "_SETSIZE");
|
lua_pushstring(L, "_SETSIZE");
|
||||||
lua_pushinteger(L, FD_SETSIZE);
|
lua_pushinteger(L, FD_SETSIZE);
|
||||||
lua_rawset(L, -3);
|
lua_rawset(L, -3);
|
||||||
@ -219,4 +212,3 @@ static void make_assoc(lua_State *L, int tab) {
|
|||||||
i = i+1;
|
i = i+1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,14 @@
|
|||||||
* true if there is data ready for reading (required for buffered input).
|
* true if there is data ready for reading (required for buffered input).
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
int select_open(lua_State *L);
|
int select_open(lua_State *L);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* SELECT_H */
|
#endif /* SELECT_H */
|
||||||
|
@ -4,15 +4,12 @@
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
|
|
||||||
#include "auxiliar.h"
|
#include "auxiliar.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "unix.h"
|
#include "unix.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
55
src/socket.h
55
src/socket.h
@ -28,51 +28,46 @@
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
|
||||||
/* we are lazy... */
|
/* convenient shorthand */
|
||||||
typedef struct sockaddr SA;
|
typedef struct sockaddr SA;
|
||||||
|
|
||||||
/*=========================================================================*\
|
/*=========================================================================*\
|
||||||
* Functions bellow implement a comfortable platform independent
|
* Functions bellow implement a comfortable platform independent
|
||||||
* interface to sockets
|
* interface to sockets
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int socket_waitfd(p_socket ps, int sw, p_timeout tm);
|
||||||
int socket_open(void);
|
int socket_open(void);
|
||||||
int socket_close(void);
|
int socket_close(void);
|
||||||
void socket_destroy(p_socket ps);
|
void socket_destroy(p_socket ps);
|
||||||
void socket_shutdown(p_socket ps, int how);
|
int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_timeout tm);
|
||||||
int socket_sendto(p_socket ps, const char *data, size_t count,
|
|
||||||
size_t *sent, SA *addr, socklen_t addr_len, p_timeout tm);
|
|
||||||
int socket_recvfrom(p_socket ps, char *data, size_t count,
|
|
||||||
size_t *got, SA *addr, socklen_t *addr_len, p_timeout tm);
|
|
||||||
|
|
||||||
void socket_setnonblocking(p_socket ps);
|
|
||||||
void socket_setblocking(p_socket ps);
|
|
||||||
|
|
||||||
int socket_waitfd(p_socket ps, int sw, p_timeout tm);
|
|
||||||
int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
|
|
||||||
p_timeout tm);
|
|
||||||
|
|
||||||
int socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm);
|
|
||||||
int socket_create(p_socket ps, int domain, int type, int protocol);
|
int socket_create(p_socket ps, int domain, int type, int protocol);
|
||||||
int socket_bind(p_socket ps, SA *addr, socklen_t addr_len);
|
int socket_bind(p_socket ps, SA *addr, socklen_t addr_len);
|
||||||
int socket_listen(p_socket ps, int backlog);
|
int socket_listen(p_socket ps, int backlog);
|
||||||
int socket_accept(p_socket ps, p_socket pa, SA *addr,
|
void socket_shutdown(p_socket ps, int how);
|
||||||
socklen_t *addr_len, p_timeout tm);
|
int socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm);
|
||||||
|
int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *addr_len, p_timeout tm);
|
||||||
const char *socket_hoststrerror(int err);
|
int socket_send(p_socket ps, const char *data, size_t count, size_t *sent, p_timeout tm);
|
||||||
const char *socket_gaistrerror(int err);
|
int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, SA *addr, socklen_t addr_len, p_timeout tm);
|
||||||
const char *socket_strerror(int err);
|
|
||||||
|
|
||||||
/* these are perfect to use with the io abstraction module
|
|
||||||
and the buffered input module */
|
|
||||||
int socket_send(p_socket ps, const char *data, size_t count,
|
|
||||||
size_t *sent, p_timeout tm);
|
|
||||||
int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm);
|
int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm);
|
||||||
int socket_write(p_socket ps, const char *data, size_t count,
|
int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, SA *addr, socklen_t *addr_len, p_timeout tm);
|
||||||
size_t *sent, p_timeout tm);
|
int socket_write(p_socket ps, const char *data, size_t count, size_t *sent, p_timeout tm);
|
||||||
int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm);
|
int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm);
|
||||||
const char *socket_ioerror(p_socket ps, int err);
|
void socket_setblocking(p_socket ps);
|
||||||
|
void socket_setnonblocking(p_socket ps);
|
||||||
int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp);
|
int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp);
|
||||||
int socket_gethostbyname(const char *addr, struct hostent **hp);
|
int socket_gethostbyname(const char *addr, struct hostent **hp);
|
||||||
|
const char *socket_hoststrerror(int err);
|
||||||
|
const char *socket_strerror(int err);
|
||||||
|
const char *socket_ioerror(p_socket ps, int err);
|
||||||
|
const char *socket_gaistrerror(int err);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* SOCKET_H */
|
#endif /* SOCKET_H */
|
||||||
|
@ -4,12 +4,7 @@
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
|
|
||||||
#include "compat.h"
|
|
||||||
#include "auxiliar.h"
|
#include "auxiliar.h"
|
||||||
|
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "inet.h"
|
#include "inet.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
@ -129,7 +124,7 @@ static luaL_Reg func[] = {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int tcp_open(lua_State *L)
|
int tcp_open(lua_State *L)
|
||||||
{
|
{
|
||||||
/* create classes */
|
/* create classes */
|
||||||
auxiliar_newclass(L, "tcp{master}", tcp_methods);
|
auxiliar_newclass(L, "tcp{master}", tcp_methods);
|
||||||
|
10
src/tcp.h
10
src/tcp.h
@ -14,7 +14,7 @@
|
|||||||
* tcp objects either connected to some address or returned by the accept
|
* tcp objects either connected to some address or returned by the accept
|
||||||
* method of a server object.
|
* method of a server object.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "lua.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
@ -30,6 +30,14 @@ typedef struct t_tcp_ {
|
|||||||
|
|
||||||
typedef t_tcp *p_tcp;
|
typedef t_tcp *p_tcp;
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
int tcp_open(lua_State *L);
|
int tcp_open(lua_State *L);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* TCP_H */
|
#endif /* TCP_H */
|
||||||
|
@ -4,10 +4,6 @@
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
#include "auxiliar.h"
|
#include "auxiliar.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
|
||||||
@ -48,7 +44,7 @@ static luaL_Reg func[] = {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initialize structure
|
* Initialize structure
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void timeout_init(p_timeout tm, double block, double total) {
|
void timeout_init(p_timeout tm, double block, double total) {
|
||||||
tm->block = block;
|
tm->block = block;
|
||||||
tm->total = total;
|
tm->total = total;
|
||||||
}
|
}
|
||||||
@ -61,7 +57,7 @@ LUASOCKET_PRIVATE void timeout_init(p_timeout tm, double block, double total) {
|
|||||||
* Returns
|
* Returns
|
||||||
* the number of ms left or -1 if there is no time limit
|
* the number of ms left or -1 if there is no time limit
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE double timeout_get(p_timeout tm) {
|
double timeout_get(p_timeout tm) {
|
||||||
if (tm->block < 0.0 && tm->total < 0.0) {
|
if (tm->block < 0.0 && tm->total < 0.0) {
|
||||||
return -1;
|
return -1;
|
||||||
} else if (tm->block < 0.0) {
|
} else if (tm->block < 0.0) {
|
||||||
@ -82,7 +78,7 @@ LUASOCKET_PRIVATE double timeout_get(p_timeout tm) {
|
|||||||
* Returns
|
* Returns
|
||||||
* start field of structure
|
* start field of structure
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE double timeout_getstart(p_timeout tm) {
|
double timeout_getstart(p_timeout tm) {
|
||||||
return tm->start;
|
return tm->start;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +90,7 @@ LUASOCKET_PRIVATE double timeout_getstart(p_timeout tm) {
|
|||||||
* Returns
|
* Returns
|
||||||
* the number of ms left or -1 if there is no time limit
|
* the number of ms left or -1 if there is no time limit
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE double timeout_getretry(p_timeout tm) {
|
double timeout_getretry(p_timeout tm) {
|
||||||
if (tm->block < 0.0 && tm->total < 0.0) {
|
if (tm->block < 0.0 && tm->total < 0.0) {
|
||||||
return -1;
|
return -1;
|
||||||
} else if (tm->block < 0.0) {
|
} else if (tm->block < 0.0) {
|
||||||
@ -114,7 +110,7 @@ LUASOCKET_PRIVATE double timeout_getretry(p_timeout tm) {
|
|||||||
* Input
|
* Input
|
||||||
* tm: timeout control structure
|
* tm: timeout control structure
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE p_timeout timeout_markstart(p_timeout tm) {
|
p_timeout timeout_markstart(p_timeout tm) {
|
||||||
tm->start = timeout_gettime();
|
tm->start = timeout_gettime();
|
||||||
return tm;
|
return tm;
|
||||||
}
|
}
|
||||||
@ -125,7 +121,7 @@ LUASOCKET_PRIVATE p_timeout timeout_markstart(p_timeout tm) {
|
|||||||
* time in s.
|
* time in s.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
LUASOCKET_PRIVATE double timeout_gettime(void) {
|
double timeout_gettime(void) {
|
||||||
FILETIME ft;
|
FILETIME ft;
|
||||||
double t;
|
double t;
|
||||||
GetSystemTimeAsFileTime(&ft);
|
GetSystemTimeAsFileTime(&ft);
|
||||||
@ -135,7 +131,7 @@ LUASOCKET_PRIVATE double timeout_gettime(void) {
|
|||||||
return (t - 11644473600.0);
|
return (t - 11644473600.0);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
LUASOCKET_PRIVATE double timeout_gettime(void) {
|
double timeout_gettime(void) {
|
||||||
struct timeval v;
|
struct timeval v;
|
||||||
gettimeofday(&v, (struct timezone *) NULL);
|
gettimeofday(&v, (struct timezone *) NULL);
|
||||||
/* Unix Epoch time (time since January 1, 1970 (UTC)) */
|
/* Unix Epoch time (time since January 1, 1970 (UTC)) */
|
||||||
@ -146,7 +142,7 @@ LUASOCKET_PRIVATE double timeout_gettime(void) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int timeout_open(lua_State *L) {
|
int timeout_open(lua_State *L) {
|
||||||
luaL_setfuncs(L, func, 0);
|
luaL_setfuncs(L, func, 0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -157,7 +153,7 @@ LUASOCKET_PRIVATE int timeout_open(lua_State *L) {
|
|||||||
* time: time out value in seconds
|
* time: time out value in seconds
|
||||||
* mode: "b" for block timeout, "t" for total timeout. (default: b)
|
* mode: "b" for block timeout, "t" for total timeout. (default: b)
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int timeout_meth_settimeout(lua_State *L, p_timeout tm) {
|
int timeout_meth_settimeout(lua_State *L, p_timeout tm) {
|
||||||
double t = luaL_optnumber(L, 2, -1);
|
double t = luaL_optnumber(L, 2, -1);
|
||||||
const char *mode = luaL_optstring(L, 3, "b");
|
const char *mode = luaL_optstring(L, 3, "b");
|
||||||
switch (*mode) {
|
switch (*mode) {
|
||||||
@ -179,7 +175,7 @@ LUASOCKET_PRIVATE int timeout_meth_settimeout(lua_State *L, p_timeout tm) {
|
|||||||
* Gets timeout values for IO operations
|
* Gets timeout values for IO operations
|
||||||
* Lua Output: block, total
|
* Lua Output: block, total
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int timeout_meth_gettimeout(lua_State *L, p_timeout tm) {
|
int timeout_meth_gettimeout(lua_State *L, p_timeout tm) {
|
||||||
lua_pushnumber(L, tm->block);
|
lua_pushnumber(L, tm->block);
|
||||||
lua_pushnumber(L, tm->total);
|
lua_pushnumber(L, tm->total);
|
||||||
return 2;
|
return 2;
|
||||||
@ -201,7 +197,7 @@ static int timeout_lua_gettime(lua_State *L)
|
|||||||
* Sleep for n seconds.
|
* Sleep for n seconds.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
LUASOCKET_PRIVATE int timeout_lua_sleep(lua_State *L)
|
int timeout_lua_sleep(lua_State *L)
|
||||||
{
|
{
|
||||||
double n = luaL_checknumber(L, 1);
|
double n = luaL_checknumber(L, 1);
|
||||||
if (n < 0.0) n = 0.0;
|
if (n < 0.0) n = 0.0;
|
||||||
@ -211,7 +207,7 @@ LUASOCKET_PRIVATE int timeout_lua_sleep(lua_State *L)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
LUASOCKET_PRIVATE int timeout_lua_sleep(lua_State *L)
|
int timeout_lua_sleep(lua_State *L)
|
||||||
{
|
{
|
||||||
double n = luaL_checknumber(L, 1);
|
double n = luaL_checknumber(L, 1);
|
||||||
struct timespec t, r;
|
struct timespec t, r;
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
* Timeout management functions
|
* Timeout management functions
|
||||||
* LuaSocket toolkit
|
* LuaSocket toolkit
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "lua.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
/* timeout control structure */
|
/* timeout control structure */
|
||||||
typedef struct t_timeout_ {
|
typedef struct t_timeout_ {
|
||||||
@ -14,16 +14,27 @@ typedef struct t_timeout_ {
|
|||||||
} t_timeout;
|
} t_timeout;
|
||||||
typedef t_timeout *p_timeout;
|
typedef t_timeout *p_timeout;
|
||||||
|
|
||||||
int timeout_open(lua_State *L);
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
void timeout_init(p_timeout tm, double block, double total);
|
void timeout_init(p_timeout tm, double block, double total);
|
||||||
double timeout_get(p_timeout tm);
|
double timeout_get(p_timeout tm);
|
||||||
|
double timeout_getstart(p_timeout tm);
|
||||||
double timeout_getretry(p_timeout tm);
|
double timeout_getretry(p_timeout tm);
|
||||||
p_timeout timeout_markstart(p_timeout tm);
|
p_timeout timeout_markstart(p_timeout tm);
|
||||||
double timeout_getstart(p_timeout tm);
|
|
||||||
double timeout_gettime(void);
|
double timeout_gettime(void);
|
||||||
|
|
||||||
|
int timeout_open(lua_State *L);
|
||||||
|
|
||||||
int timeout_meth_settimeout(lua_State *L, p_timeout tm);
|
int timeout_meth_settimeout(lua_State *L, p_timeout tm);
|
||||||
int timeout_meth_gettimeout(lua_State *L, p_timeout tm);
|
int timeout_meth_gettimeout(lua_State *L, p_timeout tm);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#define timeout_iszero(tm) ((tm)->block == 0.0)
|
#define timeout_iszero(tm) ((tm)->block == 0.0)
|
||||||
|
|
||||||
#endif /* TIMEOUT_H */
|
#endif /* TIMEOUT_H */
|
||||||
|
@ -4,10 +4,6 @@
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
#include "auxiliar.h"
|
#include "auxiliar.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "inet.h"
|
#include "inet.h"
|
||||||
@ -124,7 +120,7 @@ static luaL_Reg func[] = {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int udp_open(lua_State *L) {
|
int udp_open(lua_State *L) {
|
||||||
/* create classes */
|
/* create classes */
|
||||||
auxiliar_newclass(L, "udp{connected}", udp_methods);
|
auxiliar_newclass(L, "udp{connected}", udp_methods);
|
||||||
auxiliar_newclass(L, "udp{unconnected}", udp_methods);
|
auxiliar_newclass(L, "udp{unconnected}", udp_methods);
|
||||||
|
10
src/udp.h
10
src/udp.h
@ -12,7 +12,7 @@
|
|||||||
* with a call to the setpeername function. The same function can be used to
|
* with a call to the setpeername function. The same function can be used to
|
||||||
* break the connection.
|
* break the connection.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "lua.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
@ -26,6 +26,14 @@ typedef struct t_udp_ {
|
|||||||
} t_udp;
|
} t_udp;
|
||||||
typedef t_udp *p_udp;
|
typedef t_udp *p_udp;
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
int udp_open(lua_State *L);
|
int udp_open(lua_State *L);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* UDP_H */
|
#endif /* UDP_H */
|
||||||
|
@ -4,9 +4,6 @@
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
|
|
||||||
#include "unixstream.h"
|
#include "unixstream.h"
|
||||||
#include "unixdgram.h"
|
#include "unixdgram.h"
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
* domain.
|
* domain.
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
|
@ -4,10 +4,6 @@
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
#include "auxiliar.h"
|
#include "auxiliar.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
@ -26,6 +22,7 @@
|
|||||||
((size_t) (((struct sockaddr_un *) 0)->sun_path) \
|
((size_t) (((struct sockaddr_un *) 0)->sun_path) \
|
||||||
+ strlen ((ptr)->sun_path))
|
+ strlen ((ptr)->sun_path))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*=========================================================================*\
|
/*=========================================================================*\
|
||||||
* Internal function prototypes
|
* Internal function prototypes
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
@ -86,7 +83,7 @@ static luaL_Reg func[] = {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int unixdgram_open(lua_State *L)
|
int unixdgram_open(lua_State *L)
|
||||||
{
|
{
|
||||||
/* create classes */
|
/* create classes */
|
||||||
auxiliar_newclass(L, "unixdgram{connected}", unixdgram_methods);
|
auxiliar_newclass(L, "unixdgram{connected}", unixdgram_methods);
|
||||||
|
@ -15,6 +15,14 @@
|
|||||||
|
|
||||||
#include "unix.h"
|
#include "unix.h"
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
int unixdgram_open(lua_State *L);
|
int unixdgram_open(lua_State *L);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* UNIXDGRAM_H */
|
#endif /* UNIXDGRAM_H */
|
||||||
|
@ -4,10 +4,6 @@
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "luasocket.h"
|
#include "luasocket.h"
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
#include "auxiliar.h"
|
#include "auxiliar.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
@ -82,7 +78,7 @@ static luaL_Reg func[] = {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int unixstream_open(lua_State *L)
|
int unixstream_open(lua_State *L)
|
||||||
{
|
{
|
||||||
/* create classes */
|
/* create classes */
|
||||||
auxiliar_newclass(L, "unixstream{master}", unixstream_methods);
|
auxiliar_newclass(L, "unixstream{master}", unixstream_methods);
|
||||||
|
@ -16,6 +16,14 @@
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include "unix.h"
|
#include "unix.h"
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility push(hidden)
|
||||||
|
#endif
|
||||||
|
|
||||||
int unixstream_open(lua_State *L);
|
int unixstream_open(lua_State *L);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#pragma GCC visibility pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* UNIXSTREAM_H */
|
#endif /* UNIXSTREAM_H */
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
#define WAITFD_R POLLIN
|
#define WAITFD_R POLLIN
|
||||||
#define WAITFD_W POLLOUT
|
#define WAITFD_W POLLOUT
|
||||||
#define WAITFD_C (POLLIN|POLLOUT)
|
#define WAITFD_C (POLLIN|POLLOUT)
|
||||||
LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
||||||
int ret;
|
int ret;
|
||||||
struct pollfd pfd;
|
struct pollfd pfd;
|
||||||
pfd.fd = *ps;
|
pfd.fd = *ps;
|
||||||
@ -45,7 +45,7 @@ LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
|||||||
#define WAITFD_W 2
|
#define WAITFD_W 2
|
||||||
#define WAITFD_C (WAITFD_R|WAITFD_W)
|
#define WAITFD_C (WAITFD_R|WAITFD_W)
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
||||||
int ret;
|
int ret;
|
||||||
fd_set rfds, wfds, *rp, *wp;
|
fd_set rfds, wfds, *rp, *wp;
|
||||||
struct timeval tv, *tp;
|
struct timeval tv, *tp;
|
||||||
@ -77,7 +77,7 @@ LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_open(void) {
|
int socket_open(void) {
|
||||||
/* installs a handler to ignore sigpipe or it will crash us */
|
/* installs a handler to ignore sigpipe or it will crash us */
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
return 1;
|
return 1;
|
||||||
@ -86,14 +86,14 @@ LUASOCKET_PRIVATE int socket_open(void) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Close module
|
* Close module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_close(void) {
|
int socket_close(void) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Close and inutilize socket
|
* Close and inutilize socket
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
|
void socket_destroy(p_socket ps) {
|
||||||
if (*ps != SOCKET_INVALID) {
|
if (*ps != SOCKET_INVALID) {
|
||||||
close(*ps);
|
close(*ps);
|
||||||
*ps = SOCKET_INVALID;
|
*ps = SOCKET_INVALID;
|
||||||
@ -103,7 +103,7 @@ LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Select with timeout control
|
* Select with timeout control
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
|
int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
|
||||||
p_timeout tm) {
|
p_timeout tm) {
|
||||||
int ret;
|
int ret;
|
||||||
do {
|
do {
|
||||||
@ -120,7 +120,7 @@ LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_s
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Creates and sets up a socket
|
* Creates and sets up a socket
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) {
|
int socket_create(p_socket ps, int domain, int type, int protocol) {
|
||||||
*ps = socket(domain, type, protocol);
|
*ps = socket(domain, type, protocol);
|
||||||
if (*ps != SOCKET_INVALID) return IO_DONE;
|
if (*ps != SOCKET_INVALID) return IO_DONE;
|
||||||
else return errno;
|
else return errno;
|
||||||
@ -129,7 +129,7 @@ LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int proto
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Binds or returns error message
|
* Binds or returns error message
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
|
int socket_bind(p_socket ps, SA *addr, socklen_t len) {
|
||||||
int err = IO_DONE;
|
int err = IO_DONE;
|
||||||
socket_setblocking(ps);
|
socket_setblocking(ps);
|
||||||
if (bind(*ps, addr, len) < 0) err = errno;
|
if (bind(*ps, addr, len) < 0) err = errno;
|
||||||
@ -140,7 +140,7 @@ LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
*
|
*
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
|
int socket_listen(p_socket ps, int backlog) {
|
||||||
int err = IO_DONE;
|
int err = IO_DONE;
|
||||||
if (listen(*ps, backlog)) err = errno;
|
if (listen(*ps, backlog)) err = errno;
|
||||||
return err;
|
return err;
|
||||||
@ -149,14 +149,14 @@ LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
*
|
*
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
|
void socket_shutdown(p_socket ps, int how) {
|
||||||
shutdown(*ps, how);
|
shutdown(*ps, how);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Connects or returns error message
|
* Connects or returns error message
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
|
int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
|
||||||
int err;
|
int err;
|
||||||
/* avoid calling on closed sockets */
|
/* avoid calling on closed sockets */
|
||||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||||
@ -178,7 +178,7 @@ LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tim
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Accept with timeout
|
* Accept with timeout
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
|
int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
|
||||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||||
for ( ;; ) {
|
for ( ;; ) {
|
||||||
int err;
|
int err;
|
||||||
@ -195,7 +195,7 @@ LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Send with timeout
|
* Send with timeout
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
|
int socket_send(p_socket ps, const char *data, size_t count,
|
||||||
size_t *sent, p_timeout tm)
|
size_t *sent, p_timeout tm)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
@ -229,7 +229,7 @@ LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Sendto with timeout
|
* Sendto with timeout
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
|
int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
|
||||||
SA *addr, socklen_t len, p_timeout tm)
|
SA *addr, socklen_t len, p_timeout tm)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
@ -254,7 +254,7 @@ LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count,
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Receive with timeout
|
* Receive with timeout
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
|
int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
|
||||||
int err;
|
int err;
|
||||||
*got = 0;
|
*got = 0;
|
||||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||||
@ -276,7 +276,7 @@ LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Recvfrom with timeout
|
* Recvfrom with timeout
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
|
int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
|
||||||
SA *addr, socklen_t *len, p_timeout tm) {
|
SA *addr, socklen_t *len, p_timeout tm) {
|
||||||
int err;
|
int err;
|
||||||
*got = 0;
|
*got = 0;
|
||||||
@ -304,7 +304,7 @@ LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, siz
|
|||||||
* with send/recv replaced with write/read. We can't just use write/read
|
* with send/recv replaced with write/read. We can't just use write/read
|
||||||
* in the socket version, because behaviour when size is zero is different.
|
* in the socket version, because behaviour when size is zero is different.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_write(p_socket ps, const char *data, size_t count,
|
int socket_write(p_socket ps, const char *data, size_t count,
|
||||||
size_t *sent, p_timeout tm)
|
size_t *sent, p_timeout tm)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
@ -339,7 +339,7 @@ LUASOCKET_PRIVATE int socket_write(p_socket ps, const char *data, size_t count,
|
|||||||
* Read with timeout
|
* Read with timeout
|
||||||
* See note for socket_write
|
* See note for socket_write
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
|
int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
|
||||||
int err;
|
int err;
|
||||||
*got = 0;
|
*got = 0;
|
||||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||||
@ -361,7 +361,7 @@ LUASOCKET_PRIVATE int socket_read(p_socket ps, char *data, size_t count, size_t
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Put socket into blocking mode
|
* Put socket into blocking mode
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
|
void socket_setblocking(p_socket ps) {
|
||||||
int flags = fcntl(*ps, F_GETFL, 0);
|
int flags = fcntl(*ps, F_GETFL, 0);
|
||||||
flags &= (~(O_NONBLOCK));
|
flags &= (~(O_NONBLOCK));
|
||||||
fcntl(*ps, F_SETFL, flags);
|
fcntl(*ps, F_SETFL, flags);
|
||||||
@ -370,7 +370,7 @@ LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Put socket into non-blocking mode
|
* Put socket into non-blocking mode
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
|
void socket_setnonblocking(p_socket ps) {
|
||||||
int flags = fcntl(*ps, F_GETFL, 0);
|
int flags = fcntl(*ps, F_GETFL, 0);
|
||||||
flags |= O_NONBLOCK;
|
flags |= O_NONBLOCK;
|
||||||
fcntl(*ps, F_SETFL, flags);
|
fcntl(*ps, F_SETFL, flags);
|
||||||
@ -379,7 +379,7 @@ LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* DNS helpers
|
* DNS helpers
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
|
int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
|
||||||
*hp = gethostbyaddr(addr, len, AF_INET);
|
*hp = gethostbyaddr(addr, len, AF_INET);
|
||||||
if (*hp) return IO_DONE;
|
if (*hp) return IO_DONE;
|
||||||
else if (h_errno) return h_errno;
|
else if (h_errno) return h_errno;
|
||||||
@ -387,7 +387,7 @@ LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, stru
|
|||||||
else return IO_UNKNOWN;
|
else return IO_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) {
|
int socket_gethostbyname(const char *addr, struct hostent **hp) {
|
||||||
*hp = gethostbyname(addr);
|
*hp = gethostbyname(addr);
|
||||||
if (*hp) return IO_DONE;
|
if (*hp) return IO_DONE;
|
||||||
else if (h_errno) return h_errno;
|
else if (h_errno) return h_errno;
|
||||||
@ -399,7 +399,7 @@ LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp
|
|||||||
* Error translation functions
|
* Error translation functions
|
||||||
* Make sure important error messages are standard
|
* Make sure important error messages are standard
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
|
const char *socket_hoststrerror(int err) {
|
||||||
if (err <= 0) return io_strerror(err);
|
if (err <= 0) return io_strerror(err);
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case HOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
|
case HOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
|
||||||
@ -407,7 +407,7 @@ LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE const char *socket_strerror(int err) {
|
const char *socket_strerror(int err) {
|
||||||
if (err <= 0) return io_strerror(err);
|
if (err <= 0) return io_strerror(err);
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case EADDRINUSE: return PIE_ADDRINUSE;
|
case EADDRINUSE: return PIE_ADDRINUSE;
|
||||||
@ -423,12 +423,12 @@ LUASOCKET_PRIVATE const char *socket_strerror(int err) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) {
|
const char *socket_ioerror(p_socket ps, int err) {
|
||||||
(void) ps;
|
(void) ps;
|
||||||
return socket_strerror(err);
|
return socket_strerror(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
|
const char *socket_gaistrerror(int err) {
|
||||||
if (err == 0) return NULL;
|
if (err == 0) return NULL;
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case EAI_AGAIN: return PIE_AGAIN;
|
case EAI_AGAIN: return PIE_AGAIN;
|
||||||
@ -452,4 +452,3 @@ LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
|
|||||||
default: return gai_strerror(err);
|
default: return gai_strerror(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ static const char *wstrerror(int err);
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_open(void) {
|
int socket_open(void) {
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
WORD wVersionRequested = MAKEWORD(2, 0);
|
WORD wVersionRequested = MAKEWORD(2, 0);
|
||||||
int err = WSAStartup(wVersionRequested, &wsaData );
|
int err = WSAStartup(wVersionRequested, &wsaData );
|
||||||
@ -34,7 +34,7 @@ LUASOCKET_PRIVATE int socket_open(void) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Close module
|
* Close module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_close(void) {
|
int socket_close(void) {
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ LUASOCKET_PRIVATE int socket_close(void) {
|
|||||||
#define WAITFD_E 4
|
#define WAITFD_E 4
|
||||||
#define WAITFD_C (WAITFD_E|WAITFD_W)
|
#define WAITFD_C (WAITFD_E|WAITFD_W)
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
||||||
int ret;
|
int ret;
|
||||||
fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
|
fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
|
||||||
struct timeval tv, *tp = NULL;
|
struct timeval tv, *tp = NULL;
|
||||||
@ -75,7 +75,7 @@ LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Select with int timeout in ms
|
* Select with int timeout in ms
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
|
int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
|
||||||
p_timeout tm) {
|
p_timeout tm) {
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
double t = timeout_get(tm);
|
double t = timeout_get(tm);
|
||||||
@ -90,7 +90,7 @@ LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_s
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Close and inutilize socket
|
* Close and inutilize socket
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
|
void socket_destroy(p_socket ps) {
|
||||||
if (*ps != SOCKET_INVALID) {
|
if (*ps != SOCKET_INVALID) {
|
||||||
socket_setblocking(ps); /* close can take a long time on WIN32 */
|
socket_setblocking(ps); /* close can take a long time on WIN32 */
|
||||||
closesocket(*ps);
|
closesocket(*ps);
|
||||||
@ -101,7 +101,7 @@ LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
*
|
*
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
|
void socket_shutdown(p_socket ps, int how) {
|
||||||
socket_setblocking(ps);
|
socket_setblocking(ps);
|
||||||
shutdown(*ps, how);
|
shutdown(*ps, how);
|
||||||
socket_setnonblocking(ps);
|
socket_setnonblocking(ps);
|
||||||
@ -110,7 +110,7 @@ LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Creates and sets up a socket
|
* Creates and sets up a socket
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) {
|
int socket_create(p_socket ps, int domain, int type, int protocol) {
|
||||||
*ps = socket(domain, type, protocol);
|
*ps = socket(domain, type, protocol);
|
||||||
if (*ps != SOCKET_INVALID) return IO_DONE;
|
if (*ps != SOCKET_INVALID) return IO_DONE;
|
||||||
else return WSAGetLastError();
|
else return WSAGetLastError();
|
||||||
@ -119,7 +119,7 @@ LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int proto
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Connects or returns error message
|
* Connects or returns error message
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
|
int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
|
||||||
int err;
|
int err;
|
||||||
/* don't call on closed socket */
|
/* don't call on closed socket */
|
||||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||||
@ -148,7 +148,7 @@ LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tim
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Binds or returns error message
|
* Binds or returns error message
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
|
int socket_bind(p_socket ps, SA *addr, socklen_t len) {
|
||||||
int err = IO_DONE;
|
int err = IO_DONE;
|
||||||
socket_setblocking(ps);
|
socket_setblocking(ps);
|
||||||
if (bind(*ps, addr, len) < 0) err = WSAGetLastError();
|
if (bind(*ps, addr, len) < 0) err = WSAGetLastError();
|
||||||
@ -159,7 +159,7 @@ LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
*
|
*
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
|
int socket_listen(p_socket ps, int backlog) {
|
||||||
int err = IO_DONE;
|
int err = IO_DONE;
|
||||||
socket_setblocking(ps);
|
socket_setblocking(ps);
|
||||||
if (listen(*ps, backlog) < 0) err = WSAGetLastError();
|
if (listen(*ps, backlog) < 0) err = WSAGetLastError();
|
||||||
@ -170,7 +170,7 @@ LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Accept with timeout
|
* Accept with timeout
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
|
int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
|
||||||
p_timeout tm) {
|
p_timeout tm) {
|
||||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||||
for ( ;; ) {
|
for ( ;; ) {
|
||||||
@ -192,7 +192,7 @@ LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_
|
|||||||
* this can take an awful lot of time and we will end up blocked.
|
* this can take an awful lot of time and we will end up blocked.
|
||||||
* Therefore, whoever calls this function should not pass a huge buffer.
|
* Therefore, whoever calls this function should not pass a huge buffer.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
|
int socket_send(p_socket ps, const char *data, size_t count,
|
||||||
size_t *sent, p_timeout tm)
|
size_t *sent, p_timeout tm)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
@ -220,7 +220,7 @@ LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Sendto with timeout
|
* Sendto with timeout
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
|
int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
|
||||||
SA *addr, socklen_t len, p_timeout tm)
|
SA *addr, socklen_t len, p_timeout tm)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
@ -241,7 +241,7 @@ LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count,
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Receive with timeout
|
* Receive with timeout
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got,
|
int socket_recv(p_socket ps, char *data, size_t count, size_t *got,
|
||||||
p_timeout tm)
|
p_timeout tm)
|
||||||
{
|
{
|
||||||
int err, prev = IO_DONE;
|
int err, prev = IO_DONE;
|
||||||
@ -270,7 +270,7 @@ LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Recvfrom with timeout
|
* Recvfrom with timeout
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
|
int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
|
||||||
SA *addr, socklen_t *len, p_timeout tm)
|
SA *addr, socklen_t *len, p_timeout tm)
|
||||||
{
|
{
|
||||||
int err, prev = IO_DONE;
|
int err, prev = IO_DONE;
|
||||||
@ -299,7 +299,7 @@ LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, siz
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Put socket into blocking mode
|
* Put socket into blocking mode
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
|
void socket_setblocking(p_socket ps) {
|
||||||
u_long argp = 0;
|
u_long argp = 0;
|
||||||
ioctlsocket(*ps, FIONBIO, &argp);
|
ioctlsocket(*ps, FIONBIO, &argp);
|
||||||
}
|
}
|
||||||
@ -307,7 +307,7 @@ LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Put socket into non-blocking mode
|
* Put socket into non-blocking mode
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
|
void socket_setnonblocking(p_socket ps) {
|
||||||
u_long argp = 1;
|
u_long argp = 1;
|
||||||
ioctlsocket(*ps, FIONBIO, &argp);
|
ioctlsocket(*ps, FIONBIO, &argp);
|
||||||
}
|
}
|
||||||
@ -315,13 +315,13 @@ LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* DNS helpers
|
* DNS helpers
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
|
int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
|
||||||
*hp = gethostbyaddr(addr, len, AF_INET);
|
*hp = gethostbyaddr(addr, len, AF_INET);
|
||||||
if (*hp) return IO_DONE;
|
if (*hp) return IO_DONE;
|
||||||
else return WSAGetLastError();
|
else return WSAGetLastError();
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) {
|
int socket_gethostbyname(const char *addr, struct hostent **hp) {
|
||||||
*hp = gethostbyname(addr);
|
*hp = gethostbyname(addr);
|
||||||
if (*hp) return IO_DONE;
|
if (*hp) return IO_DONE;
|
||||||
else return WSAGetLastError();
|
else return WSAGetLastError();
|
||||||
@ -330,7 +330,7 @@ LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Error translation functions
|
* Error translation functions
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
|
const char *socket_hoststrerror(int err) {
|
||||||
if (err <= 0) return io_strerror(err);
|
if (err <= 0) return io_strerror(err);
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
|
case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
|
||||||
@ -338,7 +338,7 @@ LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE const char *socket_strerror(int err) {
|
const char *socket_strerror(int err) {
|
||||||
if (err <= 0) return io_strerror(err);
|
if (err <= 0) return io_strerror(err);
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case WSAEADDRINUSE: return PIE_ADDRINUSE;
|
case WSAEADDRINUSE: return PIE_ADDRINUSE;
|
||||||
@ -352,12 +352,12 @@ LUASOCKET_PRIVATE const char *socket_strerror(int err) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) {
|
const char *socket_ioerror(p_socket ps, int err) {
|
||||||
(void) ps;
|
(void) ps;
|
||||||
return socket_strerror(err);
|
return socket_strerror(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE static const char *wstrerror(int err) {
|
static const char *wstrerror(int err) {
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case WSAEINTR: return "Interrupted function call";
|
case WSAEINTR: return "Interrupted function call";
|
||||||
case WSAEACCES: return PIE_ACCESS; // "Permission denied";
|
case WSAEACCES: return PIE_ACCESS; // "Permission denied";
|
||||||
@ -406,7 +406,7 @@ LUASOCKET_PRIVATE static const char *wstrerror(int err) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
|
const char *socket_gaistrerror(int err) {
|
||||||
if (err == 0) return NULL;
|
if (err == 0) return NULL;
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case EAI_AGAIN: return PIE_AGAIN;
|
case EAI_AGAIN: return PIE_AGAIN;
|
||||||
@ -432,4 +432,3 @@ LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
|
|||||||
default: return gai_strerror(err);
|
default: return gai_strerror(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user