Update luasocket, embed eventcompat.lua differently
This commit is contained in:
parent
0832615599
commit
92ccb12d70
@ -1,9 +1,3 @@
|
||||
to_array = generator(
|
||||
import('python').find_installation('python3'),
|
||||
output: [ '@BASENAME@.cpp', '@BASENAME@.h' ],
|
||||
arguments: [ join_paths(meson.current_source_dir(), 'to_array.py'), '@BUILD_DIR@', '@OUTPUT0@', '@OUTPUT1@', '@INPUT@', '@EXTRA_ARGS@' ]
|
||||
)
|
||||
|
||||
data_files = files(
|
||||
'hmap.cpp',
|
||||
'icon.cpp',
|
||||
|
@ -2,7 +2,12 @@ project('the-powder-toy', [ 'c', 'cpp' ], version: 'the.cake.is.a.lie', default_
|
||||
'cpp_std=c++11',
|
||||
])
|
||||
|
||||
prog_python3 = import('python').find_installation('python3')
|
||||
to_array = generator(
|
||||
import('python').find_installation('python3'),
|
||||
output: [ '@PLAINNAME@.cpp', '@PLAINNAME@.h' ],
|
||||
arguments: [ join_paths(meson.current_source_dir(), 'to_array.py'), '@BUILD_DIR@', '@OUTPUT0@', '@OUTPUT1@', '@INPUT@', '@EXTRA_ARGS@' ]
|
||||
)
|
||||
|
||||
cpp_compiler = meson.get_compiler('cpp')
|
||||
|
||||
project_c_args = []
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "FontReader.h"
|
||||
|
||||
#include "bzip2/bz2wrap.h"
|
||||
#include "font.h"
|
||||
#include "font.bz2.h"
|
||||
|
||||
unsigned char *font_data = nullptr;
|
||||
unsigned int *font_ptrs = nullptr;
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "../data/font.h"
|
||||
#include "../data/font.bz2.h"
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
|
@ -34,7 +34,6 @@ extern int tptElements; //Table for TPT element names
|
||||
extern int tptParts, tptPartsMeta, tptElementTransitions, tptPartsCData, tptPartMeta, cIndex;
|
||||
extern LuaSmartRef *tptPart;
|
||||
|
||||
void luaopen_eventcompat(lua_State *l);
|
||||
void luacon_hook(lua_State *L, lua_Debug *ar);
|
||||
int luacon_eval(const char *command);
|
||||
String luacon_geterror();
|
||||
|
@ -60,9 +60,10 @@ extern "C"
|
||||
#endif
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include "socket/luasocket.h"
|
||||
#include "luasocket/luasocket.h"
|
||||
}
|
||||
#include "socket/socket.lua.h"
|
||||
#include "socket.lua.h"
|
||||
#include "eventcompat.lua.h"
|
||||
|
||||
GameModel * luacon_model;
|
||||
GameController * luacon_controller;
|
||||
@ -129,17 +130,22 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m):
|
||||
lua_atpanic(l, atPanic);
|
||||
luaL_openlibs(l);
|
||||
luaopen_bit(l);
|
||||
luaopen_socket_core(l);
|
||||
|
||||
// load built-in luasocket and luasec
|
||||
lua_getglobal(l, "package");
|
||||
lua_pushstring(l, "loaded");
|
||||
lua_rawget(l, -2);
|
||||
lua_pushstring(l, "socket");
|
||||
lua_rawget(l, -2);
|
||||
lua_pushstring(l, "socket.core");
|
||||
lua_pushvalue(l, -2);
|
||||
lua_rawset(l, -4);
|
||||
lua_pop(l, 3);
|
||||
luaopen_socket(l);
|
||||
lua_getfield(l, -1, "preload");
|
||||
lua_pushcfunction(l, luaopen_socket_core);
|
||||
lua_setfield(l, -2, "socket.core");
|
||||
if (luaL_loadbuffer(l, (const char *)socket_lua, socket_lua_size, "@[built-in socket.lua]"))
|
||||
{
|
||||
throw std::runtime_error(ByteString("failed to load built-in luasocket: ") + lua_tostring(l, -1));
|
||||
}
|
||||
lua_setfield(l, -2, "socket");
|
||||
lua_pop(l, 2);
|
||||
if (luaL_dostring(l, "socket = require(\"socket\")"))
|
||||
{
|
||||
throw std::runtime_error(ByteString("failed to load built-in luasocket: ") + lua_tostring(l, -1));
|
||||
}
|
||||
|
||||
lua_pushstring(l, "Luacon_ci");
|
||||
lua_pushlightuserdata(l, this);
|
||||
@ -363,7 +369,10 @@ tpt.partsdata = nil");
|
||||
initLegacyProps();
|
||||
|
||||
ui::Engine::Ref().LastTick(Platform::GetTime());
|
||||
luaopen_eventcompat(l);
|
||||
if (luaL_loadbuffer(l, (const char *)eventcompat_lua, eventcompat_lua_size, "@[built-in eventcompat.lua]") || lua_pcall(l, 0, 0, 0))
|
||||
{
|
||||
throw std::runtime_error(ByteString("failed to load built-in eventcompat: ") + lua_tostring(l, -1));
|
||||
}
|
||||
}
|
||||
|
||||
void LuaScriptInterface::Init()
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,27 +0,0 @@
|
||||
#ifdef LUACONSOLE
|
||||
#ifdef _MSC_VER
|
||||
#include "Platform.h"
|
||||
#include "resource.h"
|
||||
#endif
|
||||
#include "lua/LuaCompat.h"
|
||||
|
||||
void luaopen_eventcompat(lua_State *l)
|
||||
{
|
||||
#ifndef _MSC_VER
|
||||
int eventcompat_luac_sz = /*#SIZE*/;
|
||||
const char* eventcompat_luac = /*#DATA*/;
|
||||
luaL_loadbuffer(l, eventcompat_luac, eventcompat_luac_sz, "@eventcompat.lua");
|
||||
lua_pcall(l, 0, 0, 0);
|
||||
#else
|
||||
unsigned int size = 0;
|
||||
const char* data = NULL;
|
||||
Platform::LoadFileInResource(IDI_EVENTCOMPAT, LUASCRIPT, size, data);
|
||||
char *buffer = new char[size+1];
|
||||
::memcpy(buffer, data, size);
|
||||
buffer[size] = 0;
|
||||
luaL_loadbuffer(l, buffer, size, "@eventcompat.lua");
|
||||
lua_pcall(l, 0, 0, 0);
|
||||
delete[] buffer;
|
||||
#endif
|
||||
}
|
||||
#endif
|
@ -1,27 +0,0 @@
|
||||
#file from mniip, https://github.com/mniip/The-Powder-Toy/commit/d46d9f3f815d
|
||||
import sys
|
||||
import re
|
||||
|
||||
def encode(x):
|
||||
x = x.group(0)
|
||||
if x == '\n':
|
||||
return '\\n';
|
||||
if x == '"':
|
||||
return '\\"';
|
||||
if x == '\\':
|
||||
return '\\\\';
|
||||
return '\\{0:03o}'.format(ord(x))
|
||||
|
||||
f = open(sys.argv[2], 'rb')
|
||||
|
||||
data = f.read().decode('utf-8')
|
||||
f.close()
|
||||
size = len(data)
|
||||
data = '"' + re.sub(r'[^ -~]|"|\\', encode, data) + '"';
|
||||
|
||||
i = open(sys.argv[3], 'r')
|
||||
o = open(sys.argv[1], 'w')
|
||||
o.write(i.read().replace('/*#SIZE*/', str(size)).replace('/*#DATA*/', data))
|
||||
i.close()
|
||||
o.close()
|
||||
|
@ -1,3 +1 @@
|
||||
resources_files += files(
|
||||
'eventcompat.lua',
|
||||
)
|
||||
luaconsole_files += to_array.process('eventcompat.lua', extra_args: 'eventcompat_lua')
|
||||
|
20
src/lua/luasocket/LICENSE
Normal file
20
src/lua/luasocket/LICENSE
Normal file
@ -0,0 +1,20 @@
|
||||
LuaSocket 3.0 license
|
||||
Copyright © 2004-2013 Diego Nehab
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
@ -1,17 +1,12 @@
|
||||
/*=========================================================================*\
|
||||
* Auxiliar routines for class hierarchy manipulation
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: auxiliar.c,v 1.14 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "luasocket.h"
|
||||
#include "auxiliar.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "auxiliar.h"
|
||||
|
||||
/*=========================================================================*\
|
||||
* Exported functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes the module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
@ -81,7 +76,7 @@ void auxiliar_add2group(lua_State *L, const char *classname, const char *groupna
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int auxiliar_checkboolean(lua_State *L, int objidx) {
|
||||
if (!lua_isboolean(L, objidx))
|
||||
luaL_typerror(L, objidx, lua_typename(L, LUA_TBOOLEAN));
|
||||
auxiliar_typeerror(L, objidx, lua_typename(L, LUA_TBOOLEAN));
|
||||
return lua_toboolean(L, objidx);
|
||||
}
|
||||
|
||||
@ -145,5 +140,15 @@ void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) {
|
||||
* otherwise
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) {
|
||||
return luaL_checkudata(L, objidx, classname);
|
||||
return luaL_testudata(L, objidx, classname);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Throws error when argument does not have correct type.
|
||||
* Used to be part of lauxlib in Lua 5.1, was dropped from 5.2.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int auxiliar_typeerror (lua_State *L, int narg, const char *tname) {
|
||||
const char *msg = lua_pushfstring(L, "%s expected, got %s", tname,
|
||||
luaL_typename(L, narg));
|
||||
return luaL_argerror(L, narg, msg);
|
||||
}
|
@ -27,21 +27,28 @@
|
||||
*
|
||||
* The mapping from class name to the corresponding metatable and the
|
||||
* reverse mapping are done using lauxlib.
|
||||
*
|
||||
* RCS ID: $Id: auxiliar.h,v 1.9 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
|
||||
#include "../LuaCompat.h"
|
||||
#include "luasocket.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility push(hidden)
|
||||
#endif
|
||||
|
||||
int auxiliar_open(lua_State *L);
|
||||
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_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_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);
|
||||
int auxiliar_checkboolean(lua_State *L, int objidx);
|
||||
int auxiliar_tostring(lua_State *L);
|
||||
void *auxiliar_getclassudata(lua_State *L, const char *groupname, int objidx);
|
||||
int auxiliar_typeerror(lua_State *L, int narg, const char *tname);
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility pop
|
||||
#endif
|
||||
|
||||
#endif /* AUXILIAR_H */
|
@ -1,9 +1,8 @@
|
||||
/*=========================================================================*\
|
||||
* Input/Output interface for Lua programs
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: buffer.c,v 1.28 2007/06/11 23:44:54 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "luasocket.h"
|
||||
#include "buffer.h"
|
||||
|
||||
/*=========================================================================*\
|
||||
@ -39,7 +38,7 @@ int buffer_open(lua_State *L) {
|
||||
* Initializes C structure
|
||||
\*-------------------------------------------------------------------------*/
|
||||
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->tm = tm;
|
||||
buf->received = buf->sent = 0;
|
||||
@ -50,8 +49,8 @@ void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
|
||||
* object:getstats() interface
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int buffer_meth_getstats(lua_State *L, p_buffer buf) {
|
||||
lua_pushnumber(L, buf->received);
|
||||
lua_pushnumber(L, buf->sent);
|
||||
lua_pushnumber(L, (lua_Number) buf->received);
|
||||
lua_pushnumber(L, (lua_Number) buf->sent);
|
||||
lua_pushnumber(L, timeout_gettime() - buf->birthday);
|
||||
return 3;
|
||||
}
|
||||
@ -60,8 +59,8 @@ int buffer_meth_getstats(lua_State *L, p_buffer buf) {
|
||||
* object:setstats() interface
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int buffer_meth_setstats(lua_State *L, p_buffer buf) {
|
||||
buf->received = (long) luaL_optnumber(L, 2, buf->received);
|
||||
buf->sent = (long) luaL_optnumber(L, 3, buf->sent);
|
||||
buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received);
|
||||
buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent);
|
||||
if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4);
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
@ -77,11 +76,7 @@ int buffer_meth_send(lua_State *L, p_buffer buf) {
|
||||
const char *data = luaL_checklstring(L, 2, &size);
|
||||
long start = (long) luaL_optnumber(L, 3, 1);
|
||||
long end = (long) luaL_optnumber(L, 4, -1);
|
||||
#ifdef LUASOCKET_DEBUG
|
||||
p_timeout tm = timeout_markstart(buf->tm);
|
||||
#else
|
||||
timeout_markstart(buf->tm); //not sure if this is needed, but prevent warning anyway
|
||||
#endif
|
||||
timeout_markstart(buf->tm);
|
||||
if (start < 0) start = (long) (size+start+1);
|
||||
if (end < 0) end = (long) (size+end+1);
|
||||
if (start < 1) start = (long) 1;
|
||||
@ -91,15 +86,15 @@ int buffer_meth_send(lua_State *L, p_buffer buf) {
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, buf->io->error(buf->io->ctx, err));
|
||||
lua_pushnumber(L, sent+start-1);
|
||||
lua_pushnumber(L, (lua_Number) (sent+start-1));
|
||||
} else {
|
||||
lua_pushnumber(L, sent+start-1);
|
||||
lua_pushnumber(L, (lua_Number) (sent+start-1));
|
||||
lua_pushnil(L);
|
||||
lua_pushnil(L);
|
||||
}
|
||||
#ifdef LUASOCKET_DEBUG
|
||||
/* push time elapsed during operation as the last return value */
|
||||
lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm));
|
||||
lua_pushnumber(L, timeout_gettime() - timeout_getstart(buf->tm));
|
||||
#endif
|
||||
return lua_gettop(L) - top;
|
||||
}
|
||||
@ -112,11 +107,7 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) {
|
||||
luaL_Buffer b;
|
||||
size_t size;
|
||||
const char *part = luaL_optlstring(L, 3, "", &size);
|
||||
#ifdef LUASOCKET_DEBUG
|
||||
p_timeout tm = timeout_markstart(buf->tm);
|
||||
#else
|
||||
timeout_markstart(buf->tm);
|
||||
#endif
|
||||
/* initialize buffer with optional extra prefix
|
||||
* (useful for concatenating previous partial results) */
|
||||
luaL_buffinit(L, &b);
|
||||
@ -127,9 +118,15 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) {
|
||||
if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b);
|
||||
else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b);
|
||||
else luaL_argcheck(L, 0, 2, "invalid receive pattern");
|
||||
/* get a fixed number of bytes (minus what was already partially
|
||||
* received) */
|
||||
} else err = recvraw(buf, (size_t) lua_tonumber(L, 2)-size, &b);
|
||||
/* get a fixed number of bytes (minus what was already partially
|
||||
* received) */
|
||||
} else {
|
||||
double n = lua_tonumber(L, 2);
|
||||
size_t wanted = (size_t) n;
|
||||
luaL_argcheck(L, n >= 0, 2, "invalid receive pattern");
|
||||
if (size == 0 || wanted > size)
|
||||
err = recvraw(buf, wanted-size, &b);
|
||||
}
|
||||
/* check if there was an error */
|
||||
if (err != IO_DONE) {
|
||||
/* we can't push anyting in the stack before pushing the
|
||||
@ -146,7 +143,7 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) {
|
||||
}
|
||||
#ifdef LUASOCKET_DEBUG
|
||||
/* push time elapsed during operation as the last return value */
|
||||
lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm));
|
||||
lua_pushnumber(L, timeout_gettime() - timeout_getstart(buf->tm));
|
||||
#endif
|
||||
return lua_gettop(L) - top;
|
||||
}
|
||||
@ -171,7 +168,7 @@ static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) {
|
||||
size_t total = 0;
|
||||
int err = IO_DONE;
|
||||
while (total < count && err == IO_DONE) {
|
||||
size_t done;
|
||||
size_t done = 0;
|
||||
size_t step = (count-total <= STEPSIZE)? count-total: STEPSIZE;
|
||||
err = io->send(io->ctx, data+total, step, &done, tm);
|
||||
total += done;
|
@ -1,24 +1,21 @@
|
||||
#ifndef BUF_H
|
||||
#define BUF_H
|
||||
#define BUF_H
|
||||
/*=========================================================================*\
|
||||
* Input/Output interface for Lua programs
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* Line patterns require buffering. Reading one character at a time involves
|
||||
* too many system calls and is very slow. This module implements the
|
||||
* LuaSocket interface for input/output on connected objects, as seen by
|
||||
* Lua programs.
|
||||
* LuaSocket interface for input/output on connected objects, as seen by
|
||||
* Lua programs.
|
||||
*
|
||||
* Input is buffered. Output is *not* buffered because there was no simple
|
||||
* way of making sure the buffered output data would ever be sent.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* RCS ID: $Id: buffer.h,v 1.12 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "../LuaCompat.h"
|
||||
|
||||
#include "luasocket.h"
|
||||
#include "io.h"
|
||||
#include "timeout.h"
|
||||
|
||||
@ -31,17 +28,25 @@ typedef struct t_buffer_ {
|
||||
size_t sent, received; /* bytes sent, and bytes received */
|
||||
p_io io; /* IO driver used for this buffer */
|
||||
p_timeout tm; /* timeout management for this buffer */
|
||||
size_t first, last; /* index of first and last bytes of stored data */
|
||||
char data[BUF_SIZE]; /* storage space for buffer data */
|
||||
size_t first, last; /* index of first and last bytes of stored data */
|
||||
char data[BUF_SIZE]; /* storage space for buffer data */
|
||||
} t_buffer;
|
||||
typedef t_buffer *p_buffer;
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility push(hidden)
|
||||
#endif
|
||||
|
||||
int buffer_open(lua_State *L);
|
||||
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_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);
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility pop
|
||||
#endif
|
||||
|
||||
#endif /* BUF_H */
|
39
src/lua/luasocket/compat.c
Normal file
39
src/lua/luasocket/compat.c
Normal file
@ -0,0 +1,39 @@
|
||||
#include "luasocket.h"
|
||||
#include "compat.h"
|
||||
|
||||
#if LUA_VERSION_NUM==501
|
||||
|
||||
/*
|
||||
** Adapted from Lua 5.2
|
||||
*/
|
||||
void luasocket_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
|
||||
luaL_checkstack(L, nup+1, "too many upvalues");
|
||||
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
||||
int i;
|
||||
lua_pushstring(L, l->name);
|
||||
for (i = 0; i < nup; i++) /* copy upvalues to the top */
|
||||
lua_pushvalue(L, -(nup+1));
|
||||
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
|
||||
lua_settable(L, -(nup + 3));
|
||||
}
|
||||
lua_pop(L, nup); /* remove upvalues */
|
||||
}
|
||||
|
||||
/*
|
||||
** Duplicated from Lua 5.2
|
||||
*/
|
||||
void *luasocket_testudata (lua_State *L, int ud, const char *tname) {
|
||||
void *p = lua_touserdata(L, ud);
|
||||
if (p != NULL) { /* value is a userdata? */
|
||||
if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
|
||||
luaL_getmetatable(L, tname); /* get correct metatable */
|
||||
if (!lua_rawequal(L, -1, -2)) /* not the same? */
|
||||
p = NULL; /* value is a userdata with wrong metatable */
|
||||
lua_pop(L, 2); /* remove both metatables */
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return NULL; /* value is not a userdata with a metatable */
|
||||
}
|
||||
|
||||
#endif
|
22
src/lua/luasocket/compat.h
Normal file
22
src/lua/luasocket/compat.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef COMPAT_H
|
||||
#define COMPAT_H
|
||||
|
||||
#if LUA_VERSION_NUM==501
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility push(hidden)
|
||||
#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
|
@ -1,12 +1,19 @@
|
||||
/*=========================================================================*\
|
||||
* Simple exception support
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: except.c,v 1.8 2005/09/29 06:11:41 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "luasocket.h"
|
||||
#include "except.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "except.h"
|
||||
#if LUA_VERSION_NUM < 502
|
||||
#define lua_pcallk(L, na, nr, err, ctx, cont) \
|
||||
(((void)ctx),((void)cont),lua_pcall(L, na, nr, err))
|
||||
#endif
|
||||
|
||||
#if LUA_VERSION_NUM < 503
|
||||
typedef int lua_KContext;
|
||||
#endif
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal function prototypes.
|
||||
@ -28,18 +35,17 @@ static luaL_Reg func[] = {
|
||||
* Try factory
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static void wrap(lua_State *L) {
|
||||
lua_newtable(L);
|
||||
lua_pushnumber(L, 1);
|
||||
lua_pushvalue(L, -3);
|
||||
lua_settable(L, -3);
|
||||
lua_insert(L, -2);
|
||||
lua_pop(L, 1);
|
||||
lua_createtable(L, 1, 0);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushvalue(L, lua_upvalueindex(1));
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
static int finalize(lua_State *L) {
|
||||
if (!lua_toboolean(L, 1)) {
|
||||
lua_pushvalue(L, lua_upvalueindex(1));
|
||||
lua_pcall(L, 0, 0, 0);
|
||||
lua_pushvalue(L, lua_upvalueindex(2));
|
||||
lua_call(L, 0, 0);
|
||||
lua_settop(L, 2);
|
||||
wrap(L);
|
||||
lua_error(L);
|
||||
@ -55,7 +61,9 @@ static int do_nothing(lua_State *L) {
|
||||
static int global_newtry(lua_State *L) {
|
||||
lua_settop(L, 1);
|
||||
if (lua_isnil(L, 1)) lua_pushcfunction(L, do_nothing);
|
||||
lua_pushcclosure(L, finalize, 1);
|
||||
lua_pushvalue(L, lua_upvalueindex(1));
|
||||
lua_insert(L, -2);
|
||||
lua_pushcclosure(L, finalize, 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -63,27 +71,49 @@ static int global_newtry(lua_State *L) {
|
||||
* Protect factory
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int unwrap(lua_State *L) {
|
||||
if (lua_istable(L, -1)) {
|
||||
lua_pushnumber(L, 1);
|
||||
lua_gettable(L, -2);
|
||||
lua_pushnil(L);
|
||||
lua_insert(L, -2);
|
||||
return 1;
|
||||
} else return 0;
|
||||
if (lua_istable(L, -1) && lua_getmetatable(L, -1)) {
|
||||
int r = lua_rawequal(L, -1, lua_upvalueindex(1));
|
||||
lua_pop(L, 1);
|
||||
if (r) {
|
||||
lua_pushnil(L);
|
||||
lua_rawgeti(L, -2, 1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int protected_(lua_State *L) {
|
||||
lua_pushvalue(L, lua_upvalueindex(1));
|
||||
lua_insert(L, 1);
|
||||
if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) {
|
||||
static int protected_finish(lua_State *L, int status, lua_KContext ctx) {
|
||||
(void)ctx;
|
||||
if (status != 0 && status != LUA_YIELD) {
|
||||
if (unwrap(L)) return 2;
|
||||
else lua_error(L);
|
||||
return 0;
|
||||
else return lua_error(L);
|
||||
} else return lua_gettop(L);
|
||||
}
|
||||
|
||||
#if LUA_VERSION_NUM == 502
|
||||
static int protected_cont(lua_State *L) {
|
||||
int ctx = 0;
|
||||
int status = lua_getctx(L, &ctx);
|
||||
return protected_finish(L, status, ctx);
|
||||
}
|
||||
#else
|
||||
#define protected_cont protected_finish
|
||||
#endif
|
||||
|
||||
static int protected_(lua_State *L) {
|
||||
int status;
|
||||
lua_pushvalue(L, lua_upvalueindex(2));
|
||||
lua_insert(L, 1);
|
||||
status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, protected_cont);
|
||||
return protected_finish(L, status, 0);
|
||||
}
|
||||
|
||||
static int global_protect(lua_State *L) {
|
||||
lua_pushcclosure(L, protected_, 1);
|
||||
lua_settop(L, 1);
|
||||
lua_pushvalue(L, lua_upvalueindex(1));
|
||||
lua_insert(L, 1);
|
||||
lua_pushcclosure(L, protected_, 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -91,6 +121,9 @@ static int global_protect(lua_State *L) {
|
||||
* Init module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int except_open(lua_State *L) {
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
lua_newtable(L); /* metatable for wrapped exceptions */
|
||||
lua_pushboolean(L, 0);
|
||||
lua_setfield(L, -2, "__metatable");
|
||||
luaL_setfuncs(L, func, 1);
|
||||
return 0;
|
||||
}
|
46
src/lua/luasocket/except.h
Normal file
46
src/lua/luasocket/except.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef EXCEPT_H
|
||||
#define EXCEPT_H
|
||||
/*=========================================================================*\
|
||||
* Exception control
|
||||
* LuaSocket toolkit (but completely independent from other modules)
|
||||
*
|
||||
* This provides support for simple exceptions in Lua. During the
|
||||
* development of the HTTP/FTP/SMTP support, it became aparent that
|
||||
* error checking was taking a substantial amount of the coding. These
|
||||
* function greatly simplify the task of checking errors.
|
||||
*
|
||||
* The main idea is that functions should return nil as their first return
|
||||
* values when they find an error, and return an error message (or value)
|
||||
* following nil. In case of success, as long as the first value is not nil,
|
||||
* the other values don't matter.
|
||||
*
|
||||
* The idea is to nest function calls with the "try" function. This function
|
||||
* checks the first value, and, if it's falsy, wraps the second value in a
|
||||
* table with metatable and calls "error" on it. Otherwise, it returns all
|
||||
* values it received. Basically, it works like the Lua "assert" function,
|
||||
* but it creates errors targeted specifically at "protect".
|
||||
*
|
||||
* The "newtry" function is a factory for "try" functions that call a
|
||||
* finalizer in protected mode before calling "error".
|
||||
*
|
||||
* The "protect" function returns a new function that behaves exactly like
|
||||
* the function it receives, but the new function catches exceptions thrown
|
||||
* by "try" functions and returns nil followed by the error message instead.
|
||||
*
|
||||
* With these three functions, it's easy to write functions that throw
|
||||
* exceptions on error, but that don't interrupt the user script.
|
||||
\*=========================================================================*/
|
||||
|
||||
#include "luasocket.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility push(hidden)
|
||||
#endif
|
||||
|
||||
int except_open(lua_State *L);
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility pop
|
||||
#endif
|
||||
|
||||
#endif
|
537
src/lua/luasocket/inet.c
Normal file
537
src/lua/luasocket/inet.c
Normal file
@ -0,0 +1,537 @@
|
||||
/*=========================================================================*\
|
||||
* Internet domain functions
|
||||
* LuaSocket toolkit
|
||||
\*=========================================================================*/
|
||||
#include "luasocket.h"
|
||||
#include "inet.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal function prototypes.
|
||||
\*=========================================================================*/
|
||||
static int inet_global_toip(lua_State *L);
|
||||
static int inet_global_getaddrinfo(lua_State *L);
|
||||
static int inet_global_tohostname(lua_State *L);
|
||||
static int inet_global_getnameinfo(lua_State *L);
|
||||
static void inet_pushresolved(lua_State *L, struct hostent *hp);
|
||||
static int inet_global_gethostname(lua_State *L);
|
||||
|
||||
/* DNS functions */
|
||||
static luaL_Reg func[] = {
|
||||
{ "toip", inet_global_toip},
|
||||
{ "getaddrinfo", inet_global_getaddrinfo},
|
||||
{ "tohostname", inet_global_tohostname},
|
||||
{ "getnameinfo", inet_global_getnameinfo},
|
||||
{ "gethostname", inet_global_gethostname},
|
||||
{ NULL, NULL}
|
||||
};
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int inet_open(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, "dns");
|
||||
lua_newtable(L);
|
||||
luaL_setfuncs(L, func, 0);
|
||||
lua_settable(L, -3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Global Lua functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Returns all information provided by the resolver given a host name
|
||||
* or ip address
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int inet_gethost(const char *address, struct hostent **hp) {
|
||||
struct in_addr addr;
|
||||
if (inet_aton(address, &addr))
|
||||
return socket_gethostbyaddr((char *) &addr, sizeof(addr), hp);
|
||||
else
|
||||
return socket_gethostbyname(address, hp);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Returns all information provided by the resolver given a host name
|
||||
* or ip address
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int inet_global_tohostname(lua_State *L) {
|
||||
const char *address = luaL_checkstring(L, 1);
|
||||
struct hostent *hp = NULL;
|
||||
int err = inet_gethost(address, &hp);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_hoststrerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushstring(L, hp->h_name);
|
||||
inet_pushresolved(L, hp);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int inet_global_getnameinfo(lua_State *L) {
|
||||
char hbuf[NI_MAXHOST];
|
||||
char sbuf[NI_MAXSERV];
|
||||
int i, ret;
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *resolved, *iter;
|
||||
const char *host = luaL_optstring(L, 1, NULL);
|
||||
const char *serv = luaL_optstring(L, 2, NULL);
|
||||
|
||||
if (!(host || serv))
|
||||
luaL_error(L, "host and serv cannot be both nil");
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
|
||||
ret = getaddrinfo(host, serv, &hints, &resolved);
|
||||
if (ret != 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_gaistrerror(ret));
|
||||
return 2;
|
||||
}
|
||||
|
||||
lua_newtable(L);
|
||||
for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) {
|
||||
getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen,
|
||||
hbuf, host? (socklen_t) sizeof(hbuf): 0,
|
||||
sbuf, serv? (socklen_t) sizeof(sbuf): 0, 0);
|
||||
if (host) {
|
||||
lua_pushnumber(L, i);
|
||||
lua_pushstring(L, hbuf);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
}
|
||||
freeaddrinfo(resolved);
|
||||
|
||||
if (serv) {
|
||||
lua_pushstring(L, sbuf);
|
||||
return 2;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Returns all information provided by the resolver given a host name
|
||||
* or ip address
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int inet_global_toip(lua_State *L)
|
||||
{
|
||||
const char *address = luaL_checkstring(L, 1);
|
||||
struct hostent *hp = NULL;
|
||||
int err = inet_gethost(address, &hp);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_hoststrerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushstring(L, inet_ntoa(*((struct in_addr *) hp->h_addr)));
|
||||
inet_pushresolved(L, hp);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int inet_optfamily(lua_State* L, int narg, const char* def)
|
||||
{
|
||||
static const char* optname[] = { "unspec", "inet", "inet6", NULL };
|
||||
static int optvalue[] = { AF_UNSPEC, AF_INET, AF_INET6, 0 };
|
||||
|
||||
return optvalue[luaL_checkoption(L, narg, def, optname)];
|
||||
}
|
||||
|
||||
int inet_optsocktype(lua_State* L, int narg, const char* def)
|
||||
{
|
||||
static const char* optname[] = { "stream", "dgram", NULL };
|
||||
static int optvalue[] = { SOCK_STREAM, SOCK_DGRAM, 0 };
|
||||
|
||||
return optvalue[luaL_checkoption(L, narg, def, optname)];
|
||||
}
|
||||
|
||||
static int inet_global_getaddrinfo(lua_State *L)
|
||||
{
|
||||
const char *hostname = luaL_checkstring(L, 1);
|
||||
struct addrinfo *iterator = NULL, *resolved = NULL;
|
||||
struct addrinfo hints;
|
||||
int i = 1, ret = 0;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
ret = getaddrinfo(hostname, NULL, &hints, &resolved);
|
||||
if (ret != 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_gaistrerror(ret));
|
||||
return 2;
|
||||
}
|
||||
lua_newtable(L);
|
||||
for (iterator = resolved; iterator; iterator = iterator->ai_next) {
|
||||
char hbuf[NI_MAXHOST];
|
||||
ret = getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen,
|
||||
hbuf, (socklen_t) sizeof(hbuf), NULL, 0, NI_NUMERICHOST);
|
||||
if (ret){
|
||||
freeaddrinfo(resolved);
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_gaistrerror(ret));
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, i);
|
||||
lua_newtable(L);
|
||||
switch (iterator->ai_family) {
|
||||
case AF_INET:
|
||||
lua_pushliteral(L, "family");
|
||||
lua_pushliteral(L, "inet");
|
||||
lua_settable(L, -3);
|
||||
break;
|
||||
case AF_INET6:
|
||||
lua_pushliteral(L, "family");
|
||||
lua_pushliteral(L, "inet6");
|
||||
lua_settable(L, -3);
|
||||
break;
|
||||
case AF_UNSPEC:
|
||||
lua_pushliteral(L, "family");
|
||||
lua_pushliteral(L, "unspec");
|
||||
lua_settable(L, -3);
|
||||
break;
|
||||
default:
|
||||
lua_pushliteral(L, "family");
|
||||
lua_pushliteral(L, "unknown");
|
||||
lua_settable(L, -3);
|
||||
break;
|
||||
}
|
||||
lua_pushliteral(L, "addr");
|
||||
lua_pushstring(L, hbuf);
|
||||
lua_settable(L, -3);
|
||||
lua_settable(L, -3);
|
||||
i++;
|
||||
}
|
||||
freeaddrinfo(resolved);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Gets the host name
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int inet_global_gethostname(lua_State *L)
|
||||
{
|
||||
char name[257];
|
||||
name[256] = '\0';
|
||||
if (gethostname(name, 256) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_strerror(errno));
|
||||
return 2;
|
||||
} else {
|
||||
lua_pushstring(L, name);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Lua methods
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Retrieves socket peer name
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
|
||||
{
|
||||
int err;
|
||||
struct sockaddr_storage peer;
|
||||
socklen_t peer_len = sizeof(peer);
|
||||
char name[INET6_ADDRSTRLEN];
|
||||
char port[6]; /* 65535 = 5 bytes + 0 to terminate it */
|
||||
if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
err = getnameinfo((struct sockaddr *) &peer, peer_len,
|
||||
name, INET6_ADDRSTRLEN,
|
||||
port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, gai_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushstring(L, name);
|
||||
lua_pushinteger(L, (int) strtol(port, (char **) NULL, 10));
|
||||
switch (family) {
|
||||
case AF_INET: lua_pushliteral(L, "inet"); break;
|
||||
case AF_INET6: lua_pushliteral(L, "inet6"); break;
|
||||
case AF_UNSPEC: lua_pushliteral(L, "unspec"); break;
|
||||
default: lua_pushliteral(L, "unknown"); break;
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Retrieves socket local name
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int inet_meth_getsockname(lua_State *L, p_socket ps, int family)
|
||||
{
|
||||
int err;
|
||||
struct sockaddr_storage peer;
|
||||
socklen_t peer_len = sizeof(peer);
|
||||
char name[INET6_ADDRSTRLEN];
|
||||
char port[6]; /* 65535 = 5 bytes + 0 to terminate it */
|
||||
if (getsockname(*ps, (SA *) &peer, &peer_len) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
err=getnameinfo((struct sockaddr *)&peer, peer_len,
|
||||
name, INET6_ADDRSTRLEN, port, 6, NI_NUMERICHOST | NI_NUMERICSERV);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, gai_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushstring(L, name);
|
||||
lua_pushstring(L, port);
|
||||
switch (family) {
|
||||
case AF_INET: lua_pushliteral(L, "inet"); break;
|
||||
case AF_INET6: lua_pushliteral(L, "inet6"); break;
|
||||
case AF_UNSPEC: lua_pushliteral(L, "unspec"); break;
|
||||
default: lua_pushliteral(L, "unknown"); break;
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Passes all resolver information to Lua as a table
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static void inet_pushresolved(lua_State *L, struct hostent *hp)
|
||||
{
|
||||
char **alias;
|
||||
struct in_addr **addr;
|
||||
int i, resolved;
|
||||
lua_newtable(L); resolved = lua_gettop(L);
|
||||
lua_pushstring(L, "name");
|
||||
lua_pushstring(L, hp->h_name);
|
||||
lua_settable(L, resolved);
|
||||
lua_pushstring(L, "ip");
|
||||
lua_pushstring(L, "alias");
|
||||
i = 1;
|
||||
alias = hp->h_aliases;
|
||||
lua_newtable(L);
|
||||
if (alias) {
|
||||
while (*alias) {
|
||||
lua_pushnumber(L, i);
|
||||
lua_pushstring(L, *alias);
|
||||
lua_settable(L, -3);
|
||||
i++; alias++;
|
||||
}
|
||||
}
|
||||
lua_settable(L, resolved);
|
||||
i = 1;
|
||||
lua_newtable(L);
|
||||
addr = (struct in_addr **) hp->h_addr_list;
|
||||
if (addr) {
|
||||
while (*addr) {
|
||||
lua_pushnumber(L, i);
|
||||
lua_pushstring(L, inet_ntoa(**addr));
|
||||
lua_settable(L, -3);
|
||||
i++; addr++;
|
||||
}
|
||||
}
|
||||
lua_settable(L, resolved);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Tries to create a new inet socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_trycreate(p_socket ps, int family, int type, int protocol) {
|
||||
const char *err = socket_strerror(socket_create(ps, family, type, protocol));
|
||||
if (err == NULL && family == AF_INET6) {
|
||||
int yes = 1;
|
||||
setsockopt(*ps, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&yes, sizeof(yes));
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* "Disconnects" a DGRAM socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm)
|
||||
{
|
||||
switch (family) {
|
||||
case AF_INET: {
|
||||
struct sockaddr_in sin;
|
||||
memset((char *) &sin, 0, sizeof(sin));
|
||||
sin.sin_family = AF_UNSPEC;
|
||||
sin.sin_addr.s_addr = INADDR_ANY;
|
||||
return socket_strerror(socket_connect(ps, (SA *) &sin,
|
||||
sizeof(sin), tm));
|
||||
}
|
||||
case AF_INET6: {
|
||||
struct sockaddr_in6 sin6;
|
||||
struct in6_addr addrany = IN6ADDR_ANY_INIT;
|
||||
memset((char *) &sin6, 0, sizeof(sin6));
|
||||
sin6.sin6_family = AF_UNSPEC;
|
||||
sin6.sin6_addr = addrany;
|
||||
return socket_strerror(socket_connect(ps, (SA *) &sin6,
|
||||
sizeof(sin6), tm));
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Tries to connect to remote address (address, port)
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_tryconnect(p_socket ps, int *family, const char *address,
|
||||
const char *serv, p_timeout tm, struct addrinfo *connecthints)
|
||||
{
|
||||
struct addrinfo *iterator = NULL, *resolved = NULL;
|
||||
const char *err = NULL;
|
||||
int current_family = *family;
|
||||
/* try resolving */
|
||||
err = socket_gaistrerror(getaddrinfo(address, serv,
|
||||
connecthints, &resolved));
|
||||
if (err != NULL) {
|
||||
if (resolved) freeaddrinfo(resolved);
|
||||
return err;
|
||||
}
|
||||
for (iterator = resolved; iterator; iterator = iterator->ai_next) {
|
||||
timeout_markstart(tm);
|
||||
/* create new socket if necessary. if there was no
|
||||
* bind, we need to create one for every new family
|
||||
* that shows up while iterating. if there was a
|
||||
* bind, all families will be the same and we will
|
||||
* not enter this branch. */
|
||||
if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) {
|
||||
socket_destroy(ps);
|
||||
err = inet_trycreate(ps, iterator->ai_family,
|
||||
iterator->ai_socktype, iterator->ai_protocol);
|
||||
if (err) continue;
|
||||
current_family = iterator->ai_family;
|
||||
/* set non-blocking before connect */
|
||||
socket_setnonblocking(ps);
|
||||
}
|
||||
/* try connecting to remote address */
|
||||
err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr,
|
||||
(socklen_t) iterator->ai_addrlen, tm));
|
||||
/* if success or timeout is zero, break out of loop */
|
||||
if (err == NULL || timeout_iszero(tm)) {
|
||||
*family = current_family;
|
||||
break;
|
||||
}
|
||||
}
|
||||
freeaddrinfo(resolved);
|
||||
/* here, if err is set, we failed */
|
||||
return err;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Tries to accept a socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_tryaccept(p_socket server, int family, p_socket client,
|
||||
p_timeout tm) {
|
||||
socklen_t len;
|
||||
t_sockaddr_storage addr;
|
||||
switch (family) {
|
||||
case AF_INET6: len = sizeof(struct sockaddr_in6); break;
|
||||
case AF_INET: len = sizeof(struct sockaddr_in); break;
|
||||
default: len = sizeof(addr); break;
|
||||
}
|
||||
return socket_strerror(socket_accept(server, client, (SA *) &addr,
|
||||
&len, tm));
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Tries to bind socket to (address, port)
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_trybind(p_socket ps, int *family, const char *address,
|
||||
const char *serv, struct addrinfo *bindhints) {
|
||||
struct addrinfo *iterator = NULL, *resolved = NULL;
|
||||
const char *err = NULL;
|
||||
int current_family = *family;
|
||||
/* translate luasocket special values to C */
|
||||
if (strcmp(address, "*") == 0) address = NULL;
|
||||
if (!serv) serv = "0";
|
||||
/* try resolving */
|
||||
err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved));
|
||||
if (err) {
|
||||
if (resolved) freeaddrinfo(resolved);
|
||||
return err;
|
||||
}
|
||||
/* iterate over resolved addresses until one is good */
|
||||
for (iterator = resolved; iterator; iterator = iterator->ai_next) {
|
||||
if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) {
|
||||
socket_destroy(ps);
|
||||
err = inet_trycreate(ps, iterator->ai_family,
|
||||
iterator->ai_socktype, iterator->ai_protocol);
|
||||
if (err) continue;
|
||||
current_family = iterator->ai_family;
|
||||
}
|
||||
/* try binding to local address */
|
||||
err = socket_strerror(socket_bind(ps, (SA *) iterator->ai_addr,
|
||||
(socklen_t) iterator->ai_addrlen));
|
||||
/* keep trying unless bind succeeded */
|
||||
if (err == NULL) {
|
||||
*family = current_family;
|
||||
/* set to non-blocking after bind */
|
||||
socket_setnonblocking(ps);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* cleanup and return error */
|
||||
freeaddrinfo(resolved);
|
||||
/* here, if err is set, we failed */
|
||||
return err;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Some systems do not provide these so that we provide our own.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#ifdef LUASOCKET_INET_ATON
|
||||
int inet_aton(const char *cp, struct in_addr *inp)
|
||||
{
|
||||
unsigned int a = 0, b = 0, c = 0, d = 0;
|
||||
int n = 0, r;
|
||||
unsigned long int addr = 0;
|
||||
r = sscanf(cp, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n);
|
||||
if (r == 0 || n == 0) return 0;
|
||||
cp += n;
|
||||
if (*cp) return 0;
|
||||
if (a > 255 || b > 255 || c > 255 || d > 255) return 0;
|
||||
if (inp) {
|
||||
addr += a; addr <<= 8;
|
||||
addr += b; addr <<= 8;
|
||||
addr += c; addr <<= 8;
|
||||
addr += d;
|
||||
inp->s_addr = htonl(addr);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef LUASOCKET_INET_PTON
|
||||
int inet_pton(int af, const char *src, void *dst)
|
||||
{
|
||||
struct addrinfo hints, *res;
|
||||
int ret = 1;
|
||||
memset(&hints, 0, sizeof(struct addrinfo));
|
||||
hints.ai_family = af;
|
||||
hints.ai_flags = AI_NUMERICHOST;
|
||||
if (getaddrinfo(src, NULL, &hints, &res) != 0) return -1;
|
||||
if (af == AF_INET) {
|
||||
struct sockaddr_in *in = (struct sockaddr_in *) res->ai_addr;
|
||||
memcpy(dst, &in->sin_addr, sizeof(in->sin_addr));
|
||||
} else if (af == AF_INET6) {
|
||||
struct sockaddr_in6 *in = (struct sockaddr_in6 *) res->ai_addr;
|
||||
memcpy(dst, &in->sin6_addr, sizeof(in->sin6_addr));
|
||||
} else {
|
||||
ret = -1;
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
56
src/lua/luasocket/inet.h
Normal file
56
src/lua/luasocket/inet.h
Normal file
@ -0,0 +1,56 @@
|
||||
#ifndef INET_H
|
||||
#define INET_H
|
||||
/*=========================================================================*\
|
||||
* Internet domain functions
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* This module implements the creation and connection of internet domain
|
||||
* sockets, on top of the socket.h interface, and the interface of with the
|
||||
* resolver.
|
||||
*
|
||||
* The function inet_aton is provided for the platforms where it is not
|
||||
* available. The module also implements the interface of the internet
|
||||
* getpeername and getsockname functions as seen by Lua programs.
|
||||
*
|
||||
* The Lua functions toip and tohostname are also implemented here.
|
||||
\*=========================================================================*/
|
||||
#include "luasocket.h"
|
||||
#include "socket.h"
|
||||
#include "timeout.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define LUASOCKET_INET_ATON
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility push(hidden)
|
||||
#endif
|
||||
|
||||
int inet_open(lua_State *L);
|
||||
|
||||
int inet_optfamily(lua_State* L, int narg, const char* def);
|
||||
int inet_optsocktype(lua_State* L, int narg, const char* def);
|
||||
|
||||
int inet_meth_getpeername(lua_State *L, p_socket ps, int family);
|
||||
int inet_meth_getsockname(lua_State *L, p_socket ps, int family);
|
||||
|
||||
const char *inet_trycreate(p_socket ps, int family, int type, int protocol);
|
||||
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
|
||||
int inet_aton(const char *cp, struct in_addr *inp);
|
||||
#endif
|
||||
|
||||
#ifdef LUASOCKET_INET_PTON
|
||||
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
|
||||
int inet_pton(int af, const char *src, void *dst);
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility pop
|
||||
#endif
|
||||
|
||||
#endif /* INET_H */
|
@ -1,14 +1,10 @@
|
||||
/*=========================================================================*\
|
||||
* Input/Output abstraction
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: io.c,v 1.6 2005/09/29 06:11:41 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "luasocket.h"
|
||||
#include "io.h"
|
||||
|
||||
/*=========================================================================*\
|
||||
* Exported functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes C structure
|
||||
\*-------------------------------------------------------------------------*/
|
@ -11,12 +11,8 @@
|
||||
*
|
||||
* The module socket.h implements this interface, and thus the module tcp.h
|
||||
* is very simple.
|
||||
*
|
||||
* RCS ID: $Id: io.h,v 1.11 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include <stdio.h>
|
||||
#include "../LuaCompat.h"
|
||||
|
||||
#include "luasocket.h"
|
||||
#include "timeout.h"
|
||||
|
||||
/* IO error codes */
|
||||
@ -45,7 +41,7 @@ typedef int (*p_send) (
|
||||
/* interface to recv function */
|
||||
typedef int (*p_recv) (
|
||||
void *ctx, /* context needed by recv */
|
||||
char *data, /* pointer to buffer where data will be written */
|
||||
char *data, /* pointer to buffer where data will be writen */
|
||||
size_t count, /* number of bytes to receive into buffer */
|
||||
size_t *got, /* number of bytes received uppon return */
|
||||
p_timeout tm /* timeout control */
|
||||
@ -60,8 +56,15 @@ typedef struct t_io_ {
|
||||
} t_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);
|
||||
const char *io_strerror(int err);
|
||||
|
||||
#endif /* IO_H */
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility pop
|
||||
#endif
|
||||
|
||||
#endif /* IO_H */
|
@ -10,13 +10,8 @@
|
||||
* involved in setting up both client and server connections. The provided
|
||||
* IO routines, however, follow the Lua style, being very similar to the
|
||||
* standard Lua read and write functions.
|
||||
*
|
||||
* RCS ID: $Id: luasocket.c,v 1.53 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
|
||||
/*=========================================================================*\
|
||||
* LuaSocket includes
|
||||
\*=========================================================================*/
|
||||
#include "luasocket.h"
|
||||
#include "auxiliar.h"
|
||||
#include "except.h"
|
||||
@ -59,7 +54,7 @@ static luaL_Reg func[] = {
|
||||
* Skip a few arguments
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int global_skip(lua_State *L) {
|
||||
int amount = luaL_checkint(L, 1);
|
||||
int amount = (int) luaL_checkinteger(L, 1);
|
||||
int ret = lua_gettop(L) - amount - 1;
|
||||
return ret >= 0 ? ret : 0;
|
||||
}
|
||||
@ -79,7 +74,8 @@ static int global_unload(lua_State *L) {
|
||||
static int base_open(lua_State *L) {
|
||||
if (socket_open()) {
|
||||
/* export functions (and leave namespace table on top of stack) */
|
||||
luaL_openlib(L, "socket", func, 0);
|
||||
lua_newtable(L);
|
||||
luaL_setfuncs(L, func, 0);
|
||||
#ifdef LUASOCKET_DEBUG
|
||||
lua_pushstring(L, "_DEBUG");
|
||||
lua_pushboolean(L, 1);
|
@ -5,24 +5,28 @@
|
||||
* Networking support for the Lua language
|
||||
* Diego Nehab
|
||||
* 9/11/1999
|
||||
*
|
||||
* RCS ID: $Id: luasocket.h,v 1.25 2007/06/11 23:44:54 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "../LuaCompat.h"
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
/*-------------------------------------------------------------------------* \
|
||||
* Current socket library version
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#define LUASOCKET_VERSION "LuaSocket 2.0.2"
|
||||
#define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2007 Diego Nehab"
|
||||
#define LUASOCKET_AUTHORS "Diego Nehab"
|
||||
#define LUASOCKET_VERSION "LuaSocket 3.0-rc1"
|
||||
#define LUASOCKET_COPYRIGHT "Copyright (C) 1999-2013 Diego Nehab"
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* This macro prefixes all exported API functions
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#ifndef LUASOCKET_API
|
||||
#define LUASOCKET_API extern
|
||||
#ifdef _WIN32
|
||||
#define LUASOCKET_API __declspec(dllexport)
|
||||
#else
|
||||
#define LUASOCKET_API __attribute__ ((visibility ("default")))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "compat.h"
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes the library.
|
25
src/lua/luasocket/meson.build
Normal file
25
src/lua/luasocket/meson.build
Normal file
@ -0,0 +1,25 @@
|
||||
luaconsole_files += files(
|
||||
'auxiliar.c',
|
||||
'buffer.c',
|
||||
'compat.c',
|
||||
'except.c',
|
||||
'inet.c',
|
||||
'io.c',
|
||||
'luasocket.c',
|
||||
'options.c',
|
||||
'select.c',
|
||||
'tcp.c',
|
||||
'timeout.c',
|
||||
'udp.c',
|
||||
)
|
||||
luaconsole_files += to_array.process('socket.lua', extra_args: 'socket_lua')
|
||||
|
||||
if copt_platform == 'windows'
|
||||
luaconsole_files += files(
|
||||
'wsocket.c',
|
||||
)
|
||||
else
|
||||
luaconsole_files += files(
|
||||
'usocket.c',
|
||||
)
|
||||
endif
|
454
src/lua/luasocket/options.c
Normal file
454
src/lua/luasocket/options.c
Normal file
@ -0,0 +1,454 @@
|
||||
/*=========================================================================*\
|
||||
* Common option interface
|
||||
* LuaSocket toolkit
|
||||
\*=========================================================================*/
|
||||
#include "luasocket.h"
|
||||
#include "auxiliar.h"
|
||||
#include "options.h"
|
||||
#include "inet.h"
|
||||
#include <string.h>
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal functions prototypes
|
||||
\*=========================================================================*/
|
||||
static int opt_setmembership(lua_State *L, p_socket ps, int level, int name);
|
||||
static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name);
|
||||
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name);
|
||||
static int opt_getboolean(lua_State *L, p_socket ps, int level, int name);
|
||||
static int opt_setint(lua_State *L, p_socket ps, int level, int name);
|
||||
static int opt_getint(lua_State *L, p_socket ps, int level, int name);
|
||||
static int opt_set(lua_State *L, p_socket ps, int level, int name,
|
||||
void *val, int len);
|
||||
static int opt_get(lua_State *L, p_socket ps, int level, int name,
|
||||
void *val, int* len);
|
||||
|
||||
/*=========================================================================*\
|
||||
* Exported functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Calls appropriate option handler
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
|
||||
{
|
||||
const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
|
||||
while (opt->name && strcmp(name, opt->name))
|
||||
opt++;
|
||||
if (!opt->func) {
|
||||
char msg[57];
|
||||
sprintf(msg, "unsupported option `%.35s'", name);
|
||||
luaL_argerror(L, 2, msg);
|
||||
}
|
||||
return opt->func(L, ps);
|
||||
}
|
||||
|
||||
int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
|
||||
{
|
||||
const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
|
||||
while (opt->name && strcmp(name, opt->name))
|
||||
opt++;
|
||||
if (!opt->func) {
|
||||
char msg[57];
|
||||
sprintf(msg, "unsupported option `%.35s'", name);
|
||||
luaL_argerror(L, 2, msg);
|
||||
}
|
||||
return opt->func(L, ps);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
/* enables reuse of local address */
|
||||
int opt_set_reuseaddr(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
|
||||
}
|
||||
|
||||
int opt_get_reuseaddr(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
/* enables reuse of local port */
|
||||
int opt_set_reuseport(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
|
||||
}
|
||||
|
||||
int opt_get_reuseport(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
/* disables the Nagle algorithm */
|
||||
int opt_set_tcp_nodelay(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
|
||||
}
|
||||
|
||||
int opt_get_tcp_nodelay(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
#ifdef TCP_KEEPIDLE
|
||||
|
||||
int opt_get_tcp_keepidle(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
|
||||
}
|
||||
|
||||
int opt_set_tcp_keepidle(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------
|
||||
#ifdef TCP_KEEPCNT
|
||||
|
||||
int opt_get_tcp_keepcnt(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
|
||||
}
|
||||
|
||||
int opt_set_tcp_keepcnt(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------
|
||||
#ifdef TCP_KEEPINTVL
|
||||
|
||||
int opt_get_tcp_keepintvl(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
|
||||
}
|
||||
|
||||
int opt_set_tcp_keepintvl(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_keepalive(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
|
||||
}
|
||||
|
||||
int opt_get_keepalive(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_dontroute(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
|
||||
}
|
||||
|
||||
int opt_get_dontroute(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_broadcast(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
|
||||
}
|
||||
|
||||
int opt_get_broadcast(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
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);
|
||||
}
|
||||
|
||||
int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
|
||||
}
|
||||
|
||||
int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_ip_multicast_loop(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
|
||||
}
|
||||
|
||||
int opt_get_ip_multicast_loop(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
|
||||
}
|
||||
|
||||
int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_linger(lua_State *L, p_socket ps)
|
||||
{
|
||||
struct linger li; /* obj, name, table */
|
||||
if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
|
||||
lua_pushstring(L, "on");
|
||||
lua_gettable(L, 3);
|
||||
if (!lua_isboolean(L, -1))
|
||||
luaL_argerror(L, 3, "boolean 'on' field expected");
|
||||
li.l_onoff = (u_short) lua_toboolean(L, -1);
|
||||
lua_pushstring(L, "timeout");
|
||||
lua_gettable(L, 3);
|
||||
if (!lua_isnumber(L, -1))
|
||||
luaL_argerror(L, 3, "number 'timeout' field expected");
|
||||
li.l_linger = (u_short) lua_tonumber(L, -1);
|
||||
return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
|
||||
}
|
||||
|
||||
int opt_get_linger(lua_State *L, p_socket ps)
|
||||
{
|
||||
struct linger li; /* obj, name */
|
||||
int len = sizeof(li);
|
||||
int err = opt_get(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, &len);
|
||||
if (err)
|
||||
return err;
|
||||
lua_newtable(L);
|
||||
lua_pushboolean(L, li.l_onoff);
|
||||
lua_setfield(L, -2, "on");
|
||||
lua_pushinteger(L, li.l_linger);
|
||||
lua_setfield(L, -2, "timeout");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
|
||||
{
|
||||
const char *address = luaL_checkstring(L, 3); /* obj, name, ip */
|
||||
struct in_addr val;
|
||||
val.s_addr = htonl(INADDR_ANY);
|
||||
if (strcmp(address, "*") && !inet_aton(address, &val))
|
||||
luaL_argerror(L, 3, "ip expected");
|
||||
return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_IF,
|
||||
(char *) &val, sizeof(val));
|
||||
}
|
||||
|
||||
int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
|
||||
{
|
||||
struct in_addr val;
|
||||
socklen_t len = sizeof(val);
|
||||
if (getsockopt(*ps, IPPROTO_IP, IP_MULTICAST_IF, (char *) &val, &len) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "getsockopt failed");
|
||||
return 2;
|
||||
}
|
||||
lua_pushstring(L, inet_ntoa(val));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_ip_add_membership(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
|
||||
}
|
||||
|
||||
int opt_set_ip_drop_membersip(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
int opt_set_ip6_add_membership(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP);
|
||||
}
|
||||
|
||||
int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP);
|
||||
}
|
||||
// -------------------------------------------------------
|
||||
int opt_get_ip6_v6only(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
|
||||
}
|
||||
|
||||
int opt_set_ip6_v6only(lua_State *L, p_socket ps)
|
||||
{
|
||||
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
|
||||
\*=========================================================================*/
|
||||
static int opt_setmembership(lua_State *L, p_socket ps, int level, int name)
|
||||
{
|
||||
struct ip_mreq val; /* obj, name, table */
|
||||
if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
|
||||
lua_pushstring(L, "multiaddr");
|
||||
lua_gettable(L, 3);
|
||||
if (!lua_isstring(L, -1))
|
||||
luaL_argerror(L, 3, "string 'multiaddr' field expected");
|
||||
if (!inet_aton(lua_tostring(L, -1), &val.imr_multiaddr))
|
||||
luaL_argerror(L, 3, "invalid 'multiaddr' ip address");
|
||||
lua_pushstring(L, "interface");
|
||||
lua_gettable(L, 3);
|
||||
if (!lua_isstring(L, -1))
|
||||
luaL_argerror(L, 3, "string 'interface' field expected");
|
||||
val.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||
if (strcmp(lua_tostring(L, -1), "*") &&
|
||||
!inet_aton(lua_tostring(L, -1), &val.imr_interface))
|
||||
luaL_argerror(L, 3, "invalid 'interface' ip address");
|
||||
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
|
||||
}
|
||||
|
||||
static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name)
|
||||
{
|
||||
struct ipv6_mreq val; /* obj, opt-name, table */
|
||||
memset(&val, 0, sizeof(val));
|
||||
if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
|
||||
lua_pushstring(L, "multiaddr");
|
||||
lua_gettable(L, 3);
|
||||
if (!lua_isstring(L, -1))
|
||||
luaL_argerror(L, 3, "string 'multiaddr' field expected");
|
||||
if (!inet_pton(AF_INET6, lua_tostring(L, -1), &val.ipv6mr_multiaddr))
|
||||
luaL_argerror(L, 3, "invalid 'multiaddr' ip address");
|
||||
lua_pushstring(L, "interface");
|
||||
lua_gettable(L, 3);
|
||||
/* By default we listen to interface on default route
|
||||
* (sigh). However, interface= can override it. We should
|
||||
* support either number, or name for it. Waiting for
|
||||
* windows port of if_nametoindex */
|
||||
if (!lua_isnil(L, -1)) {
|
||||
if (lua_isnumber(L, -1)) {
|
||||
val.ipv6mr_interface = (unsigned int) lua_tonumber(L, -1);
|
||||
} else
|
||||
luaL_argerror(L, -1, "number 'interface' field expected");
|
||||
}
|
||||
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
|
||||
}
|
||||
|
||||
static
|
||||
int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len)
|
||||
{
|
||||
socklen_t socklen = *len;
|
||||
if (getsockopt(*ps, level, name, (char *) val, &socklen) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "getsockopt failed");
|
||||
return 2;
|
||||
}
|
||||
*len = socklen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static
|
||||
int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len)
|
||||
{
|
||||
if (setsockopt(*ps, level, name, (char *) val, len) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "setsockopt failed");
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int opt_getboolean(lua_State *L, p_socket ps, int level, int name)
|
||||
{
|
||||
int val = 0;
|
||||
int len = sizeof(val);
|
||||
int err = opt_get(L, ps, level, name, (char *) &val, &len);
|
||||
if (err)
|
||||
return err;
|
||||
lua_pushboolean(L, val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
|
||||
{
|
||||
int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */
|
||||
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
|
||||
}
|
||||
|
||||
static int opt_getint(lua_State *L, p_socket ps, int level, int name)
|
||||
{
|
||||
int val = 0;
|
||||
int len = sizeof(val);
|
||||
int err = opt_get(L, ps, level, name, (char *) &val, &len);
|
||||
if (err)
|
||||
return err;
|
||||
lua_pushnumber(L, val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int opt_setint(lua_State *L, p_socket ps, int level, int name)
|
||||
{
|
||||
int val = (int) lua_tonumber(L, 3); /* obj, name, int */
|
||||
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
|
||||
}
|
102
src/lua/luasocket/options.h
Normal file
102
src/lua/luasocket/options.h
Normal file
@ -0,0 +1,102 @@
|
||||
#ifndef OPTIONS_H
|
||||
#define OPTIONS_H
|
||||
/*=========================================================================*\
|
||||
* Common option interface
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* This module provides a common interface to socket options, used mainly by
|
||||
* modules UDP and TCP.
|
||||
\*=========================================================================*/
|
||||
|
||||
#include "luasocket.h"
|
||||
#include "socket.h"
|
||||
|
||||
/* option registry */
|
||||
typedef struct t_opt {
|
||||
const char *name;
|
||||
int (*func)(lua_State *L, p_socket ps);
|
||||
} t_opt;
|
||||
typedef t_opt *p_opt;
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility push(hidden)
|
||||
#endif
|
||||
|
||||
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_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
|
28
src/lua/luasocket/pierror.h
Normal file
28
src/lua/luasocket/pierror.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef PIERROR_H
|
||||
#define PIERROR_H
|
||||
/*=========================================================================*\
|
||||
* Error messages
|
||||
* Defines platform independent error messages
|
||||
\*=========================================================================*/
|
||||
|
||||
#define PIE_HOST_NOT_FOUND "host not found"
|
||||
#define PIE_ADDRINUSE "address already in use"
|
||||
#define PIE_ISCONN "already connected"
|
||||
#define PIE_ACCESS "permission denied"
|
||||
#define PIE_CONNREFUSED "connection refused"
|
||||
#define PIE_CONNABORTED "closed"
|
||||
#define PIE_CONNRESET "closed"
|
||||
#define PIE_TIMEDOUT "timeout"
|
||||
#define PIE_AGAIN "temporary failure in name resolution"
|
||||
#define PIE_BADFLAGS "invalid value for ai_flags"
|
||||
#define PIE_BADHINTS "invalid value for hints"
|
||||
#define PIE_FAIL "non-recoverable failure in name resolution"
|
||||
#define PIE_FAMILY "ai_family not supported"
|
||||
#define PIE_MEMORY "memory allocation failure"
|
||||
#define PIE_NONAME "host or service not provided, or not known"
|
||||
#define PIE_OVERFLOW "argument buffer overflow"
|
||||
#define PIE_PROTOCOL "resolved protocol is unknown"
|
||||
#define PIE_SERVICE "service not supported for socket type"
|
||||
#define PIE_SOCKTYPE "ai_socktype not supported"
|
||||
|
||||
#endif
|
@ -1,22 +1,22 @@
|
||||
/*=========================================================================*\
|
||||
* Select implementation
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: select.c,v 1.22 2005/11/20 07:20:23 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
#include "luasocket.h"
|
||||
|
||||
#include "socket.h"
|
||||
#include "timeout.h"
|
||||
#include "select.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal function prototypes.
|
||||
\*=========================================================================*/
|
||||
static t_socket getfd(lua_State *L);
|
||||
static int dirty(lua_State *L);
|
||||
static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd,
|
||||
int itab, fd_set *set);
|
||||
static void collect_fd(lua_State *L, int tab, int itab,
|
||||
fd_set *set, t_socket *max_fd);
|
||||
static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set);
|
||||
static void return_fd(lua_State *L, fd_set *set, t_socket max_fd,
|
||||
int itab, int tab, int start);
|
||||
@ -29,14 +29,17 @@ static luaL_Reg func[] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/*=========================================================================*\
|
||||
* Exported functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int select_open(lua_State *L) {
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
lua_pushstring(L, "_SETSIZE");
|
||||
lua_pushinteger(L, FD_SETSIZE);
|
||||
lua_rawset(L, -3);
|
||||
lua_pushstring(L, "_SOCKETINVALID");
|
||||
lua_pushinteger(L, SOCKET_INVALID);
|
||||
lua_rawset(L, -3);
|
||||
luaL_setfuncs(L, func, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -48,7 +51,7 @@ int select_open(lua_State *L) {
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int global_select(lua_State *L) {
|
||||
int rtab, wtab, itab, ret, ndirty;
|
||||
t_socket max_fd;
|
||||
t_socket max_fd = SOCKET_INVALID;
|
||||
fd_set rset, wset;
|
||||
t_timeout tm;
|
||||
double t = luaL_optnumber(L, 3, -1);
|
||||
@ -57,12 +60,12 @@ static int global_select(lua_State *L) {
|
||||
lua_newtable(L); itab = lua_gettop(L);
|
||||
lua_newtable(L); rtab = lua_gettop(L);
|
||||
lua_newtable(L); wtab = lua_gettop(L);
|
||||
max_fd = collect_fd(L, 1, SOCKET_INVALID, itab, &rset);
|
||||
collect_fd(L, 1, itab, &rset, &max_fd);
|
||||
collect_fd(L, 2, itab, &wset, &max_fd);
|
||||
ndirty = check_dirty(L, 1, rtab, &rset);
|
||||
t = ndirty > 0? 0.0: t;
|
||||
timeout_init(&tm, t, -1);
|
||||
timeout_markstart(&tm);
|
||||
max_fd = collect_fd(L, 2, max_fd, itab, &wset);
|
||||
ret = socket_select(max_fd+1, &rset, &wset, NULL, &tm);
|
||||
if (ret > 0 || ndirty > 0) {
|
||||
return_fd(L, &rset, max_fd+1, itab, rtab, ndirty);
|
||||
@ -74,7 +77,7 @@ static int global_select(lua_State *L) {
|
||||
lua_pushstring(L, "timeout");
|
||||
return 3;
|
||||
} else {
|
||||
lua_pushstring(L, "error");
|
||||
luaL_error(L, "select failed");
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
@ -89,8 +92,10 @@ static t_socket getfd(lua_State *L) {
|
||||
if (!lua_isnil(L, -1)) {
|
||||
lua_pushvalue(L, -2);
|
||||
lua_call(L, 1, 1);
|
||||
if (lua_isnumber(L, -1))
|
||||
fd = (t_socket) lua_tonumber(L, -1);
|
||||
if (lua_isnumber(L, -1)) {
|
||||
double numfd = lua_tonumber(L, -1);
|
||||
fd = (numfd >= 0.0)? (t_socket) numfd: SOCKET_INVALID;
|
||||
}
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
return fd;
|
||||
@ -109,12 +114,14 @@ static int dirty(lua_State *L) {
|
||||
return is;
|
||||
}
|
||||
|
||||
static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd,
|
||||
int itab, fd_set *set) {
|
||||
int i = 1;
|
||||
if (lua_isnil(L, tab))
|
||||
return max_fd;
|
||||
while (1) {
|
||||
static void collect_fd(lua_State *L, int tab, int itab,
|
||||
fd_set *set, t_socket *max_fd) {
|
||||
int i = 1, n = 0;
|
||||
/* nil is the same as an empty table */
|
||||
if (lua_isnil(L, tab)) return;
|
||||
/* otherwise we need it to be a table */
|
||||
luaL_checktype(L, tab, LUA_TTABLE);
|
||||
for ( ;; ) {
|
||||
t_socket fd;
|
||||
lua_pushnumber(L, i);
|
||||
lua_gettable(L, tab);
|
||||
@ -122,26 +129,37 @@ static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd,
|
||||
lua_pop(L, 1);
|
||||
break;
|
||||
}
|
||||
/* getfd figures out if this is a socket */
|
||||
fd = getfd(L);
|
||||
if (fd != SOCKET_INVALID) {
|
||||
/* make sure we don't overflow the fd_set */
|
||||
#ifdef _WIN32
|
||||
if (n >= FD_SETSIZE)
|
||||
luaL_argerror(L, tab, "too many sockets");
|
||||
#else
|
||||
if (fd >= FD_SETSIZE)
|
||||
luaL_argerror(L, tab, "descriptor too large for set size");
|
||||
#endif
|
||||
FD_SET(fd, set);
|
||||
if (max_fd == SOCKET_INVALID || max_fd < fd)
|
||||
max_fd = fd;
|
||||
lua_pushnumber(L, fd);
|
||||
n++;
|
||||
/* keep track of the largest descriptor so far */
|
||||
if (*max_fd == SOCKET_INVALID || *max_fd < fd)
|
||||
*max_fd = fd;
|
||||
/* make sure we can map back from descriptor to the object */
|
||||
lua_pushnumber(L, (lua_Number) fd);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, itab);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
i = i + 1;
|
||||
}
|
||||
return max_fd;
|
||||
}
|
||||
|
||||
static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) {
|
||||
int ndirty = 0, i = 1;
|
||||
if (lua_isnil(L, tab))
|
||||
return 0;
|
||||
while (1) {
|
||||
for ( ;; ) {
|
||||
t_socket fd;
|
||||
lua_pushnumber(L, i);
|
||||
lua_gettable(L, tab);
|
||||
@ -168,7 +186,7 @@ static void return_fd(lua_State *L, fd_set *set, t_socket max_fd,
|
||||
for (fd = 0; fd < max_fd; fd++) {
|
||||
if (FD_ISSET(fd, set)) {
|
||||
lua_pushnumber(L, ++start);
|
||||
lua_pushnumber(L, fd);
|
||||
lua_pushnumber(L, (lua_Number) fd);
|
||||
lua_gettable(L, itab);
|
||||
lua_settable(L, tab);
|
||||
}
|
||||
@ -178,7 +196,7 @@ static void return_fd(lua_State *L, fd_set *set, t_socket max_fd,
|
||||
static void make_assoc(lua_State *L, int tab) {
|
||||
int i = 1, atab;
|
||||
lua_newtable(L); atab = lua_gettop(L);
|
||||
while (1) {
|
||||
for ( ;; ) {
|
||||
lua_pushnumber(L, i);
|
||||
lua_gettable(L, tab);
|
||||
if (!lua_isnil(L, -1)) {
|
||||
@ -194,4 +212,3 @@ static void make_assoc(lua_State *L, int tab) {
|
||||
i = i+1;
|
||||
}
|
||||
}
|
||||
|
@ -4,14 +4,20 @@
|
||||
* Select implementation
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* Each object that can be passed to the select function has to export
|
||||
* Each object that can be passed to the select function has to export
|
||||
* method getfd() which returns the descriptor to be passed to the
|
||||
* underlying select function. Another method, dirty(), should return
|
||||
* underlying select function. Another method, dirty(), should return
|
||||
* true if there is data ready for reading (required for buffered input).
|
||||
*
|
||||
* RCS ID: $Id: select.h,v 1.7 2004/06/16 01:02:07 diego Exp $
|
||||
\*=========================================================================*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility push(hidden)
|
||||
#endif
|
||||
|
||||
int select_open(lua_State *L);
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility pop
|
||||
#endif
|
||||
|
||||
#endif /* SELECT_H */
|
@ -8,8 +8,6 @@
|
||||
* differences. Also, not all *nix platforms behave the same. This module
|
||||
* (and the associated usocket.h and wsocket.h) factor these differences and
|
||||
* creates a interface compatible with the io.h module.
|
||||
*
|
||||
* RCS ID: $Id: socket.h,v 1.20 2005/11/20 07:20:23 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "io.h"
|
||||
|
||||
@ -26,7 +24,7 @@
|
||||
* The connect and accept functions accept a timeout and their
|
||||
* implementations are somewhat complicated. We chose to move
|
||||
* the timeout control into this module for these functions in
|
||||
* order to simplify the modules that use them.
|
||||
* order to simplify the modules that use them.
|
||||
\*=========================================================================*/
|
||||
#include "timeout.h"
|
||||
|
||||
@ -34,40 +32,44 @@
|
||||
typedef struct sockaddr SA;
|
||||
|
||||
/*=========================================================================*\
|
||||
* Functions bellow implement a comfortable platform independent
|
||||
* Functions bellow implement a comfortable platform independent
|
||||
* interface to sockets
|
||||
\*=========================================================================*/
|
||||
int socket_open(void);
|
||||
int socket_close(void);
|
||||
void socket_destroy(p_socket ps);
|
||||
void socket_shutdown(p_socket ps, int how);
|
||||
int socket_sendto(p_socket ps, const char *data, size_t count,
|
||||
void socket_shutdown(p_socket ps, int how);
|
||||
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,
|
||||
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,
|
||||
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_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_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_accept(p_socket ps, p_socket pa, SA *addr,
|
||||
int socket_accept(p_socket ps, p_socket pa, SA *addr,
|
||||
socklen_t *addr_len, p_timeout tm);
|
||||
|
||||
const char *socket_hoststrerror(int err);
|
||||
const char *socket_gaistrerror(int err);
|
||||
const char *socket_strerror(int err);
|
||||
|
||||
/* these are perfect to use with the io abstraction module
|
||||
/* 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,
|
||||
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_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);
|
||||
const char *socket_ioerror(p_socket ps, int err);
|
||||
|
||||
int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp);
|
@ -1,7 +1,6 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- LuaSocket helper module
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: socket.lua,v 1.22 2005/11/22 08:33:29 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
@ -11,37 +10,52 @@ local base = _G
|
||||
local string = require("string")
|
||||
local math = require("math")
|
||||
local socket = require("socket.core")
|
||||
module("socket")
|
||||
|
||||
local _M = socket
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Exported auxiliar functions
|
||||
-----------------------------------------------------------------------------
|
||||
function connect(address, port, laddress, lport)
|
||||
local sock, err = socket.tcp()
|
||||
if not sock then return nil, err end
|
||||
if laddress then
|
||||
local res, err = sock:bind(laddress, lport, -1)
|
||||
if not res then return nil, err end
|
||||
function _M.connect4(address, port, laddress, lport)
|
||||
return socket.connect(address, port, laddress, lport, "inet")
|
||||
end
|
||||
|
||||
function _M.connect6(address, port, laddress, lport)
|
||||
return socket.connect(address, port, laddress, lport, "inet6")
|
||||
end
|
||||
|
||||
function _M.bind(host, port, backlog)
|
||||
if host == "*" then host = "0.0.0.0" end
|
||||
local addrinfo, err = socket.dns.getaddrinfo(host);
|
||||
if not addrinfo then return nil, err end
|
||||
local sock, res
|
||||
err = "no info on address"
|
||||
for i, alt in base.ipairs(addrinfo) do
|
||||
if alt.family == "inet" then
|
||||
sock, err = socket.tcp4()
|
||||
else
|
||||
sock, err = socket.tcp6()
|
||||
end
|
||||
if not sock then return nil, err end
|
||||
sock:setoption("reuseaddr", true)
|
||||
res, err = sock:bind(alt.addr, port)
|
||||
if not res then
|
||||
sock:close()
|
||||
else
|
||||
res, err = sock:listen(backlog)
|
||||
if not res then
|
||||
sock:close()
|
||||
else
|
||||
return sock
|
||||
end
|
||||
end
|
||||
end
|
||||
local res, err = sock:connect(address, port)
|
||||
if not res then return nil, err end
|
||||
return sock
|
||||
return nil, err
|
||||
end
|
||||
|
||||
function bind(host, port, backlog)
|
||||
local sock, err = socket.tcp()
|
||||
if not sock then return nil, err end
|
||||
sock:setoption("reuseaddr", true)
|
||||
local res, err = sock:bind(host, port)
|
||||
if not res then return nil, err end
|
||||
res, err = sock:listen(backlog)
|
||||
if not res then return nil, err end
|
||||
return sock
|
||||
end
|
||||
_M.try = _M.newtry()
|
||||
|
||||
try = newtry()
|
||||
|
||||
function choose(table)
|
||||
function _M.choose(table)
|
||||
return function(name, opt1, opt2)
|
||||
if base.type(name) ~= "string" then
|
||||
name, opt1, opt2 = "default", name, opt1
|
||||
@ -56,10 +70,11 @@ end
|
||||
-- Socket sources and sinks, conforming to LTN12
|
||||
-----------------------------------------------------------------------------
|
||||
-- create namespaces inside LuaSocket namespace
|
||||
sourcet = {}
|
||||
sinkt = {}
|
||||
local sourcet, sinkt = {}, {}
|
||||
_M.sourcet = sourcet
|
||||
_M.sinkt = sinkt
|
||||
|
||||
BLOCKSIZE = 2048
|
||||
_M.BLOCKSIZE = 2048
|
||||
|
||||
sinkt["close-when-done"] = function(sock)
|
||||
return base.setmetatable({
|
||||
@ -89,7 +104,7 @@ end
|
||||
|
||||
sinkt["default"] = sinkt["keep-open"]
|
||||
|
||||
sink = choose(sinkt)
|
||||
_M.sink = _M.choose(sinkt)
|
||||
|
||||
sourcet["by-length"] = function(sock, length)
|
||||
return base.setmetatable({
|
||||
@ -129,5 +144,6 @@ end
|
||||
|
||||
sourcet["default"] = sourcet["until-closed"]
|
||||
|
||||
source = choose(sourcet)
|
||||
_M.source = _M.choose(sourcet)
|
||||
|
||||
return _M
|
@ -1,10 +1,8 @@
|
||||
/*=========================================================================*\
|
||||
* TCP object
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: tcp.c,v 1.41 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
#include "luasocket.h"
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "socket.h"
|
||||
@ -12,12 +10,18 @@
|
||||
#include "options.h"
|
||||
#include "tcp.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal function prototypes
|
||||
\*=========================================================================*/
|
||||
static int global_create(lua_State *L);
|
||||
static int global_create4(lua_State *L);
|
||||
static int global_create6(lua_State *L);
|
||||
static int global_connect(lua_State *L);
|
||||
static int meth_connect(lua_State *L);
|
||||
static int meth_listen(lua_State *L);
|
||||
static int meth_getfamily(lua_State *L);
|
||||
static int meth_bind(lua_State *L);
|
||||
static int meth_send(lua_State *L);
|
||||
static int meth_getstats(lua_State *L);
|
||||
@ -28,14 +32,16 @@ static int meth_shutdown(lua_State *L);
|
||||
static int meth_receive(lua_State *L);
|
||||
static int meth_accept(lua_State *L);
|
||||
static int meth_close(lua_State *L);
|
||||
static int meth_getoption(lua_State *L);
|
||||
static int meth_setoption(lua_State *L);
|
||||
static int meth_gettimeout(lua_State *L);
|
||||
static int meth_settimeout(lua_State *L);
|
||||
static int meth_getfd(lua_State *L);
|
||||
static int meth_setfd(lua_State *L);
|
||||
static int meth_dirty(lua_State *L);
|
||||
|
||||
/* tcp object methods */
|
||||
static luaL_Reg tcp[] = {
|
||||
static luaL_Reg tcp_methods[] = {
|
||||
{"__gc", meth_close},
|
||||
{"__tostring", auxiliar_tostring},
|
||||
{"accept", meth_accept},
|
||||
@ -43,7 +49,9 @@ static luaL_Reg tcp[] = {
|
||||
{"close", meth_close},
|
||||
{"connect", meth_connect},
|
||||
{"dirty", meth_dirty},
|
||||
{"getfamily", meth_getfamily},
|
||||
{"getfd", meth_getfd},
|
||||
{"getoption", meth_getoption},
|
||||
{"getpeername", meth_getpeername},
|
||||
{"getsockname", meth_getsockname},
|
||||
{"getstats", meth_getstats},
|
||||
@ -56,22 +64,60 @@ static luaL_Reg tcp[] = {
|
||||
{"setpeername", meth_connect},
|
||||
{"setsockname", meth_bind},
|
||||
{"settimeout", meth_settimeout},
|
||||
{"gettimeout", meth_gettimeout},
|
||||
{"shutdown", meth_shutdown},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* socket option handlers */
|
||||
static t_opt opt[] = {
|
||||
{"keepalive", opt_keepalive},
|
||||
{"reuseaddr", opt_reuseaddr},
|
||||
{"tcp-nodelay", opt_tcp_nodelay},
|
||||
{"linger", opt_linger},
|
||||
static t_opt optget[] = {
|
||||
{"keepalive", opt_get_keepalive},
|
||||
{"reuseaddr", opt_get_reuseaddr},
|
||||
{"reuseport", opt_get_reuseport},
|
||||
{"tcp-nodelay", opt_get_tcp_nodelay},
|
||||
#ifdef TCP_KEEPIDLE
|
||||
{"tcp-keepidle", opt_get_tcp_keepidle},
|
||||
#endif
|
||||
#ifdef TCP_KEEPCNT
|
||||
{"tcp-keepcnt", opt_get_tcp_keepcnt},
|
||||
#endif
|
||||
#ifdef TCP_KEEPINTVL
|
||||
{"tcp-keepintvl", opt_get_tcp_keepintvl},
|
||||
#endif
|
||||
{"linger", opt_get_linger},
|
||||
{"error", opt_get_error},
|
||||
{"recv-buffer-size", opt_get_recv_buf_size},
|
||||
{"send-buffer-size", opt_get_send_buf_size},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static t_opt optset[] = {
|
||||
{"keepalive", opt_set_keepalive},
|
||||
{"reuseaddr", opt_set_reuseaddr},
|
||||
{"reuseport", opt_set_reuseport},
|
||||
{"tcp-nodelay", opt_set_tcp_nodelay},
|
||||
#ifdef TCP_KEEPIDLE
|
||||
{"tcp-keepidle", opt_set_tcp_keepidle},
|
||||
#endif
|
||||
#ifdef TCP_KEEPCNT
|
||||
{"tcp-keepcnt", opt_set_tcp_keepcnt},
|
||||
#endif
|
||||
#ifdef TCP_KEEPINTVL
|
||||
{"tcp-keepintvl", opt_set_tcp_keepintvl},
|
||||
#endif
|
||||
{"ipv6-v6only", opt_set_ip6_v6only},
|
||||
{"linger", opt_set_linger},
|
||||
{"recv-buffer-size", opt_set_recv_buf_size},
|
||||
{"send-buffer-size", opt_set_send_buf_size},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* functions in library namespace */
|
||||
static luaL_Reg func[] = {
|
||||
{"tcp", global_create},
|
||||
{"tcp4", global_create4},
|
||||
{"tcp6", global_create6},
|
||||
{"connect", global_connect},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -81,15 +127,15 @@ static luaL_Reg func[] = {
|
||||
int tcp_open(lua_State *L)
|
||||
{
|
||||
/* create classes */
|
||||
auxiliar_newclass(L, "tcp{master}", tcp);
|
||||
auxiliar_newclass(L, "tcp{client}", tcp);
|
||||
auxiliar_newclass(L, "tcp{server}", tcp);
|
||||
auxiliar_newclass(L, "tcp{master}", tcp_methods);
|
||||
auxiliar_newclass(L, "tcp{client}", tcp_methods);
|
||||
auxiliar_newclass(L, "tcp{server}", tcp_methods);
|
||||
/* create class groups */
|
||||
auxiliar_add2group(L, "tcp{master}", "tcp{any}");
|
||||
auxiliar_add2group(L, "tcp{client}", "tcp{any}");
|
||||
auxiliar_add2group(L, "tcp{server}", "tcp{any}");
|
||||
/* define library functions */
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
luaL_setfuncs(L, func, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -122,10 +168,16 @@ static int meth_setstats(lua_State *L) {
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call option handler
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_getoption(lua_State *L)
|
||||
{
|
||||
p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
|
||||
return opt_meth_getoption(L, optget, &tcp->sock);
|
||||
}
|
||||
|
||||
static int meth_setoption(lua_State *L)
|
||||
{
|
||||
p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
|
||||
return opt_meth_setoption(L, opt, &tcp->sock);
|
||||
return opt_meth_setoption(L, optset, &tcp->sock);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -162,22 +214,24 @@ static int meth_accept(lua_State *L)
|
||||
p_tcp server = (p_tcp) auxiliar_checkclass(L, "tcp{server}", 1);
|
||||
p_timeout tm = timeout_markstart(&server->tm);
|
||||
t_socket sock;
|
||||
int err = socket_accept(&server->sock, &sock, NULL, NULL, tm);
|
||||
const char *err = inet_tryaccept(&server->sock, server->family, &sock, tm);
|
||||
/* if successful, push client socket */
|
||||
if (err == IO_DONE) {
|
||||
if (err == NULL) {
|
||||
p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
|
||||
auxiliar_setclass(L, "tcp{client}", -1);
|
||||
/* initialize structure fields */
|
||||
memset(clnt, 0, sizeof(t_tcp));
|
||||
socket_setnonblocking(&sock);
|
||||
clnt->sock = sock;
|
||||
io_init(&clnt->io, (p_send) socket_send, (p_recv) socket_recv,
|
||||
(p_error) socket_ioerror, &clnt->sock);
|
||||
timeout_init(&clnt->tm, -1, -1);
|
||||
buffer_init(&clnt->buf, &clnt->io, &clnt->tm);
|
||||
clnt->family = server->family;
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_strerror(err));
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
@ -185,12 +239,17 @@ static int meth_accept(lua_State *L)
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Binds an object to an address
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_bind(lua_State *L)
|
||||
{
|
||||
static int meth_bind(lua_State *L) {
|
||||
p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1);
|
||||
const char *address = luaL_checkstring(L, 2);
|
||||
unsigned short port = (unsigned short) luaL_checknumber(L, 3);
|
||||
const char *err = inet_trybind(&tcp->sock, address, port);
|
||||
const char *port = luaL_checkstring(L, 3);
|
||||
const char *err;
|
||||
struct addrinfo bindhints;
|
||||
memset(&bindhints, 0, sizeof(bindhints));
|
||||
bindhints.ai_socktype = SOCK_STREAM;
|
||||
bindhints.ai_family = tcp->family;
|
||||
bindhints.ai_flags = AI_PASSIVE;
|
||||
err = inet_trybind(&tcp->sock, &tcp->family, address, port, &bindhints);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
@ -203,13 +262,19 @@ static int meth_bind(lua_State *L)
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Turns a master tcp object into a client object.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_connect(lua_State *L)
|
||||
{
|
||||
static int meth_connect(lua_State *L) {
|
||||
p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
|
||||
const char *address = luaL_checkstring(L, 2);
|
||||
unsigned short port = (unsigned short) luaL_checknumber(L, 3);
|
||||
p_timeout tm = timeout_markstart(&tcp->tm);
|
||||
const char *err = inet_tryconnect(&tcp->sock, address, port, tm);
|
||||
const char *port = luaL_checkstring(L, 3);
|
||||
struct addrinfo connecthints;
|
||||
const char *err;
|
||||
memset(&connecthints, 0, sizeof(connecthints));
|
||||
connecthints.ai_socktype = SOCK_STREAM;
|
||||
/* make sure we try to connect only to the same family */
|
||||
connecthints.ai_family = tcp->family;
|
||||
timeout_markstart(&tcp->tm);
|
||||
err = inet_tryconnect(&tcp->sock, &tcp->family, address, port,
|
||||
&tcp->tm, &connecthints);
|
||||
/* have to set the class even if it failed due to non-blocking connects */
|
||||
auxiliar_setclass(L, "tcp{client}", 1);
|
||||
if (err) {
|
||||
@ -217,7 +282,6 @@ static int meth_connect(lua_State *L)
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
/* turn master object into a client object */
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
@ -233,6 +297,24 @@ static int meth_close(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Returns family as string
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_getfamily(lua_State *L)
|
||||
{
|
||||
p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
|
||||
if (tcp->family == AF_INET6) {
|
||||
lua_pushliteral(L, "inet6");
|
||||
return 1;
|
||||
} else if (tcp->family == AF_INET) {
|
||||
lua_pushliteral(L, "inet4");
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushliteral(L, "inet4");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Puts the sockt in listen mode
|
||||
\*-------------------------------------------------------------------------*/
|
||||
@ -257,27 +339,13 @@ static int meth_listen(lua_State *L)
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_shutdown(lua_State *L)
|
||||
{
|
||||
/* SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, so we can use method index directly */
|
||||
static const char* methods[] = { "receive", "send", "both", NULL };
|
||||
p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1);
|
||||
const char *how = luaL_optstring(L, 2, "both");
|
||||
switch (how[0]) {
|
||||
case 'b':
|
||||
if (strcmp(how, "both")) goto error;
|
||||
socket_shutdown(&tcp->sock, 2);
|
||||
break;
|
||||
case 's':
|
||||
if (strcmp(how, "send")) goto error;
|
||||
socket_shutdown(&tcp->sock, 1);
|
||||
break;
|
||||
case 'r':
|
||||
if (strcmp(how, "receive")) goto error;
|
||||
socket_shutdown(&tcp->sock, 0);
|
||||
break;
|
||||
}
|
||||
int how = luaL_checkoption(L, 2, "both", methods);
|
||||
socket_shutdown(&tcp->sock, how);
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
error:
|
||||
luaL_argerror(L, 2, "invalid shutdown method");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -286,13 +354,13 @@ error:
|
||||
static int meth_getpeername(lua_State *L)
|
||||
{
|
||||
p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
|
||||
return inet_meth_getpeername(L, &tcp->sock);
|
||||
return inet_meth_getpeername(L, &tcp->sock, tcp->family);
|
||||
}
|
||||
|
||||
static int meth_getsockname(lua_State *L)
|
||||
{
|
||||
p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
|
||||
return inet_meth_getsockname(L, &tcp->sock);
|
||||
return inet_meth_getsockname(L, &tcp->sock, tcp->family);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -304,33 +372,100 @@ static int meth_settimeout(lua_State *L)
|
||||
return timeout_meth_settimeout(L, &tcp->tm);
|
||||
}
|
||||
|
||||
static int meth_gettimeout(lua_State *L)
|
||||
{
|
||||
p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
|
||||
return timeout_meth_gettimeout(L, &tcp->tm);
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Library functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Creates a master tcp object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int global_create(lua_State *L)
|
||||
{
|
||||
t_socket sock;
|
||||
const char *err = inet_trycreate(&sock, SOCK_STREAM);
|
||||
/* try to allocate a system socket */
|
||||
if (!err) {
|
||||
/* allocate tcp object */
|
||||
p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
|
||||
/* set its type as master object */
|
||||
auxiliar_setclass(L, "tcp{master}", -1);
|
||||
/* initialize remaining structure fields */
|
||||
socket_setnonblocking(&sock);
|
||||
tcp->sock = sock;
|
||||
io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv,
|
||||
(p_error) socket_ioerror, &tcp->sock);
|
||||
timeout_init(&tcp->tm, -1, -1);
|
||||
buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
|
||||
return 1;
|
||||
} else {
|
||||
static int tcp_create(lua_State *L, int family) {
|
||||
p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
|
||||
memset(tcp, 0, sizeof(t_tcp));
|
||||
/* set its type as master object */
|
||||
auxiliar_setclass(L, "tcp{master}", -1);
|
||||
/* if family is AF_UNSPEC, we leave the socket invalid and
|
||||
* store AF_UNSPEC into family. This will allow it to later be
|
||||
* replaced with an AF_INET6 or AF_INET socket upon first use. */
|
||||
tcp->sock = SOCKET_INVALID;
|
||||
tcp->family = family;
|
||||
io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv,
|
||||
(p_error) socket_ioerror, &tcp->sock);
|
||||
timeout_init(&tcp->tm, -1, -1);
|
||||
buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
|
||||
if (family != AF_UNSPEC) {
|
||||
const char *err = inet_trycreate(&tcp->sock, family, SOCK_STREAM, 0);
|
||||
if (err != NULL) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
socket_setnonblocking(&tcp->sock);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int global_create(lua_State *L) {
|
||||
return tcp_create(L, AF_UNSPEC);
|
||||
}
|
||||
|
||||
static int global_create4(lua_State *L) {
|
||||
return tcp_create(L, AF_INET);
|
||||
}
|
||||
|
||||
static int global_create6(lua_State *L) {
|
||||
return tcp_create(L, AF_INET6);
|
||||
}
|
||||
|
||||
static int global_connect(lua_State *L) {
|
||||
const char *remoteaddr = luaL_checkstring(L, 1);
|
||||
const char *remoteserv = luaL_checkstring(L, 2);
|
||||
const char *localaddr = luaL_optstring(L, 3, NULL);
|
||||
const char *localserv = luaL_optstring(L, 4, "0");
|
||||
int family = inet_optfamily(L, 5, "unspec");
|
||||
p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
|
||||
struct addrinfo bindhints, connecthints;
|
||||
const char *err = NULL;
|
||||
/* initialize tcp structure */
|
||||
memset(tcp, 0, sizeof(t_tcp));
|
||||
io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv,
|
||||
(p_error) socket_ioerror, &tcp->sock);
|
||||
timeout_init(&tcp->tm, -1, -1);
|
||||
buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
|
||||
tcp->sock = SOCKET_INVALID;
|
||||
tcp->family = AF_UNSPEC;
|
||||
/* allow user to pick local address and port */
|
||||
memset(&bindhints, 0, sizeof(bindhints));
|
||||
bindhints.ai_socktype = SOCK_STREAM;
|
||||
bindhints.ai_family = family;
|
||||
bindhints.ai_flags = AI_PASSIVE;
|
||||
if (localaddr) {
|
||||
err = inet_trybind(&tcp->sock, &tcp->family, localaddr,
|
||||
localserv, &bindhints);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
/* try to connect to remote address and port */
|
||||
memset(&connecthints, 0, sizeof(connecthints));
|
||||
connecthints.ai_socktype = SOCK_STREAM;
|
||||
/* make sure we try to connect only to the same family */
|
||||
connecthints.ai_family = tcp->family;
|
||||
err = inet_tryconnect(&tcp->sock, &tcp->family, remoteaddr, remoteserv,
|
||||
&tcp->tm, &connecthints);
|
||||
if (err) {
|
||||
socket_destroy(&tcp->sock);
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
auxiliar_setclass(L, "tcp{client}", -1);
|
||||
return 1;
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
* TCP object
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* The tcp.h module is basically a glue that puts together modules buffer.h,
|
||||
* The tcp.h module is basicly a glue that puts together modules buffer.h,
|
||||
* timeout.h socket.h and inet.h to provide the LuaSocket TCP (AF_INET,
|
||||
* SOCK_STREAM) support.
|
||||
*
|
||||
@ -13,10 +13,8 @@
|
||||
* objects are tcp objects bound to some local address. Client objects are
|
||||
* tcp objects either connected to some address or returned by the accept
|
||||
* method of a server object.
|
||||
*
|
||||
* RCS ID: $Id: tcp.h,v 1.7 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "../LuaCompat.h"
|
||||
#include "luasocket.h"
|
||||
|
||||
#include "buffer.h"
|
||||
#include "timeout.h"
|
||||
@ -27,10 +25,19 @@ typedef struct t_tcp_ {
|
||||
t_io io;
|
||||
t_buffer buf;
|
||||
t_timeout tm;
|
||||
int family;
|
||||
} t_tcp;
|
||||
|
||||
typedef t_tcp *p_tcp;
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility push(hidden)
|
||||
#endif
|
||||
|
||||
int tcp_open(lua_State *L);
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility pop
|
||||
#endif
|
||||
|
||||
#endif /* TCP_H */
|
@ -1,14 +1,16 @@
|
||||
/*=========================================================================*\
|
||||
* Timeout management functions
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: timeout.c,v 1.30 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include <stdio.h>
|
||||
#include "luasocket.h"
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "timeout.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <float.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
@ -141,7 +143,7 @@ double timeout_gettime(void) {
|
||||
* Initializes module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int timeout_open(lua_State *L) {
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
luaL_setfuncs(L, func, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -169,6 +171,16 @@ int timeout_meth_settimeout(lua_State *L, p_timeout tm) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Gets timeout values for IO operations
|
||||
* Lua Output: block, total
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int timeout_meth_gettimeout(lua_State *L, p_timeout tm) {
|
||||
lua_pushnumber(L, tm->block);
|
||||
lua_pushnumber(L, tm->total);
|
||||
return 2;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Test support functions
|
||||
\*=========================================================================*/
|
||||
@ -184,13 +196,23 @@ static int timeout_lua_gettime(lua_State *L)
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Sleep for n seconds.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#ifdef _WIN32
|
||||
int timeout_lua_sleep(lua_State *L)
|
||||
{
|
||||
double n = luaL_checknumber(L, 1);
|
||||
#ifdef _WIN32
|
||||
Sleep((int)(n*1000));
|
||||
if (n < 0.0) n = 0.0;
|
||||
if (n < DBL_MAX/1000.0) n *= 1000.0;
|
||||
if (n > INT_MAX) n = INT_MAX;
|
||||
Sleep((int)n);
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
int timeout_lua_sleep(lua_State *L)
|
||||
{
|
||||
double n = luaL_checknumber(L, 1);
|
||||
struct timespec t, r;
|
||||
if (n < 0.0) n = 0.0;
|
||||
if (n > INT_MAX) n = INT_MAX;
|
||||
t.tv_sec = (int) n;
|
||||
n -= t.tv_sec;
|
||||
t.tv_nsec = (int) (n * 1000000000);
|
||||
@ -199,6 +221,6 @@ int timeout_lua_sleep(lua_State *L)
|
||||
t.tv_sec = r.tv_sec;
|
||||
t.tv_nsec = r.tv_nsec;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
#endif
|
@ -3,10 +3,8 @@
|
||||
/*=========================================================================*\
|
||||
* Timeout management functions
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: timeout.h,v 1.14 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "../LuaCompat.h"
|
||||
#include "luasocket.h"
|
||||
|
||||
/* timeout control structure */
|
||||
typedef struct t_timeout_ {
|
||||
@ -16,14 +14,26 @@ typedef struct t_timeout_ {
|
||||
} t_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);
|
||||
double timeout_get(p_timeout tm);
|
||||
double timeout_getstart(p_timeout tm);
|
||||
double timeout_getretry(p_timeout tm);
|
||||
p_timeout timeout_markstart(p_timeout tm);
|
||||
double timeout_getstart(p_timeout tm);
|
||||
|
||||
double timeout_gettime(void);
|
||||
|
||||
int timeout_open(lua_State *L);
|
||||
|
||||
int timeout_meth_settimeout(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)
|
||||
|
488
src/lua/luasocket/udp.c
Normal file
488
src/lua/luasocket/udp.c
Normal file
@ -0,0 +1,488 @@
|
||||
/*=========================================================================*\
|
||||
* UDP object
|
||||
* LuaSocket toolkit
|
||||
\*=========================================================================*/
|
||||
#include "luasocket.h"
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "socket.h"
|
||||
#include "inet.h"
|
||||
#include "options.h"
|
||||
#include "udp.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* min and max macros */
|
||||
#ifndef MIN
|
||||
#define MIN(x, y) ((x) < (y) ? x : y)
|
||||
#endif
|
||||
#ifndef MAX
|
||||
#define MAX(x, y) ((x) > (y) ? x : y)
|
||||
#endif
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal function prototypes
|
||||
\*=========================================================================*/
|
||||
static int global_create(lua_State *L);
|
||||
static int global_create4(lua_State *L);
|
||||
static int global_create6(lua_State *L);
|
||||
static int meth_send(lua_State *L);
|
||||
static int meth_sendto(lua_State *L);
|
||||
static int meth_receive(lua_State *L);
|
||||
static int meth_receivefrom(lua_State *L);
|
||||
static int meth_getfamily(lua_State *L);
|
||||
static int meth_getsockname(lua_State *L);
|
||||
static int meth_getpeername(lua_State *L);
|
||||
static int meth_gettimeout(lua_State *L);
|
||||
static int meth_setsockname(lua_State *L);
|
||||
static int meth_setpeername(lua_State *L);
|
||||
static int meth_close(lua_State *L);
|
||||
static int meth_setoption(lua_State *L);
|
||||
static int meth_getoption(lua_State *L);
|
||||
static int meth_settimeout(lua_State *L);
|
||||
static int meth_getfd(lua_State *L);
|
||||
static int meth_setfd(lua_State *L);
|
||||
static int meth_dirty(lua_State *L);
|
||||
|
||||
/* udp object methods */
|
||||
static luaL_Reg udp_methods[] = {
|
||||
{"__gc", meth_close},
|
||||
{"__tostring", auxiliar_tostring},
|
||||
{"close", meth_close},
|
||||
{"dirty", meth_dirty},
|
||||
{"getfamily", meth_getfamily},
|
||||
{"getfd", meth_getfd},
|
||||
{"getpeername", meth_getpeername},
|
||||
{"getsockname", meth_getsockname},
|
||||
{"receive", meth_receive},
|
||||
{"receivefrom", meth_receivefrom},
|
||||
{"send", meth_send},
|
||||
{"sendto", meth_sendto},
|
||||
{"setfd", meth_setfd},
|
||||
{"setoption", meth_setoption},
|
||||
{"getoption", meth_getoption},
|
||||
{"setpeername", meth_setpeername},
|
||||
{"setsockname", meth_setsockname},
|
||||
{"settimeout", meth_settimeout},
|
||||
{"gettimeout", meth_gettimeout},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* socket options for setoption */
|
||||
static t_opt optset[] = {
|
||||
{"dontroute", opt_set_dontroute},
|
||||
{"broadcast", opt_set_broadcast},
|
||||
{"reuseaddr", opt_set_reuseaddr},
|
||||
{"reuseport", opt_set_reuseport},
|
||||
{"ip-multicast-if", opt_set_ip_multicast_if},
|
||||
{"ip-multicast-ttl", opt_set_ip_multicast_ttl},
|
||||
{"ip-multicast-loop", opt_set_ip_multicast_loop},
|
||||
{"ip-add-membership", opt_set_ip_add_membership},
|
||||
{"ip-drop-membership", opt_set_ip_drop_membersip},
|
||||
{"ipv6-unicast-hops", opt_set_ip6_unicast_hops},
|
||||
{"ipv6-multicast-hops", opt_set_ip6_unicast_hops},
|
||||
{"ipv6-multicast-loop", opt_set_ip6_multicast_loop},
|
||||
{"ipv6-add-membership", opt_set_ip6_add_membership},
|
||||
{"ipv6-drop-membership", opt_set_ip6_drop_membersip},
|
||||
{"ipv6-v6only", opt_set_ip6_v6only},
|
||||
{"recv-buffer-size", opt_set_recv_buf_size},
|
||||
{"send-buffer-size", opt_set_send_buf_size},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* socket options for getoption */
|
||||
static t_opt optget[] = {
|
||||
{"dontroute", opt_get_dontroute},
|
||||
{"broadcast", opt_get_broadcast},
|
||||
{"reuseaddr", opt_get_reuseaddr},
|
||||
{"reuseport", opt_get_reuseport},
|
||||
{"ip-multicast-if", opt_get_ip_multicast_if},
|
||||
{"ip-multicast-loop", opt_get_ip_multicast_loop},
|
||||
{"error", opt_get_error},
|
||||
{"ipv6-unicast-hops", opt_get_ip6_unicast_hops},
|
||||
{"ipv6-multicast-hops", opt_get_ip6_unicast_hops},
|
||||
{"ipv6-multicast-loop", opt_get_ip6_multicast_loop},
|
||||
{"ipv6-v6only", opt_get_ip6_v6only},
|
||||
{"recv-buffer-size", opt_get_recv_buf_size},
|
||||
{"send-buffer-size", opt_get_send_buf_size},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* functions in library namespace */
|
||||
static luaL_Reg func[] = {
|
||||
{"udp", global_create},
|
||||
{"udp4", global_create4},
|
||||
{"udp6", global_create6},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int udp_open(lua_State *L) {
|
||||
/* create classes */
|
||||
auxiliar_newclass(L, "udp{connected}", udp_methods);
|
||||
auxiliar_newclass(L, "udp{unconnected}", udp_methods);
|
||||
/* create class groups */
|
||||
auxiliar_add2group(L, "udp{connected}", "udp{any}");
|
||||
auxiliar_add2group(L, "udp{unconnected}", "udp{any}");
|
||||
auxiliar_add2group(L, "udp{connected}", "select{able}");
|
||||
auxiliar_add2group(L, "udp{unconnected}", "select{able}");
|
||||
/* define library functions */
|
||||
luaL_setfuncs(L, func, 0);
|
||||
/* export default UDP size */
|
||||
lua_pushliteral(L, "_DATAGRAMSIZE");
|
||||
lua_pushinteger(L, UDP_DATAGRAMSIZE);
|
||||
lua_rawset(L, -3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Lua methods
|
||||
\*=========================================================================*/
|
||||
static const char *udp_strerror(int err) {
|
||||
/* a 'closed' error on an unconnected means the target address was not
|
||||
* accepted by the transport layer */
|
||||
if (err == IO_CLOSED) return "refused";
|
||||
else return socket_strerror(err);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Send data through connected udp socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_send(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1);
|
||||
p_timeout tm = &udp->tm;
|
||||
size_t count, sent = 0;
|
||||
int err;
|
||||
const char *data = luaL_checklstring(L, 2, &count);
|
||||
timeout_markstart(tm);
|
||||
err = socket_send(&udp->sock, data, count, &sent, tm);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, (lua_Number) sent);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Send data through unconnected udp socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_sendto(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
|
||||
size_t count, sent = 0;
|
||||
const char *data = luaL_checklstring(L, 2, &count);
|
||||
const char *ip = luaL_checkstring(L, 3);
|
||||
const char *port = luaL_checkstring(L, 4);
|
||||
p_timeout tm = &udp->tm;
|
||||
int err;
|
||||
struct addrinfo aihint;
|
||||
struct addrinfo *ai;
|
||||
memset(&aihint, 0, sizeof(aihint));
|
||||
aihint.ai_family = udp->family;
|
||||
aihint.ai_socktype = SOCK_DGRAM;
|
||||
aihint.ai_flags = AI_NUMERICHOST;
|
||||
#ifdef AI_NUMERICSERV
|
||||
aihint.ai_flags |= AI_NUMERICSERV;
|
||||
#endif
|
||||
err = getaddrinfo(ip, port, &aihint, &ai);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, gai_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* create socket if on first sendto if AF_UNSPEC was set */
|
||||
if (udp->family == AF_UNSPEC && udp->sock == SOCKET_INVALID) {
|
||||
struct addrinfo *ap;
|
||||
const char *errstr = NULL;
|
||||
for (ap = ai; ap != NULL; ap = ap->ai_next) {
|
||||
errstr = inet_trycreate(&udp->sock, ap->ai_family, SOCK_DGRAM, 0);
|
||||
if (errstr == NULL) {
|
||||
socket_setnonblocking(&udp->sock);
|
||||
udp->family = ap->ai_family;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (errstr != NULL) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, errstr);
|
||||
freeaddrinfo(ai);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
timeout_markstart(tm);
|
||||
err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr,
|
||||
(socklen_t) ai->ai_addrlen, tm);
|
||||
freeaddrinfo(ai);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, (lua_Number) sent);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Receives data from a UDP socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_receive(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
char buf[UDP_DATAGRAMSIZE];
|
||||
size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
|
||||
char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
|
||||
int err;
|
||||
p_timeout tm = &udp->tm;
|
||||
timeout_markstart(tm);
|
||||
if (!dgram) {
|
||||
lua_pushnil(L);
|
||||
lua_pushliteral(L, "out of memory");
|
||||
return 2;
|
||||
}
|
||||
err = socket_recv(&udp->sock, dgram, wanted, &got, tm);
|
||||
/* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */
|
||||
if (err != IO_DONE && err != IO_CLOSED) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
if (wanted > sizeof(buf)) free(dgram);
|
||||
return 2;
|
||||
}
|
||||
lua_pushlstring(L, dgram, got);
|
||||
if (wanted > sizeof(buf)) free(dgram);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Receives data and sender from a UDP socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_receivefrom(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
|
||||
char buf[UDP_DATAGRAMSIZE];
|
||||
size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
|
||||
char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t addr_len = sizeof(addr);
|
||||
char addrstr[INET6_ADDRSTRLEN];
|
||||
char portstr[6];
|
||||
int err;
|
||||
p_timeout tm = &udp->tm;
|
||||
timeout_markstart(tm);
|
||||
if (!dgram) {
|
||||
lua_pushnil(L);
|
||||
lua_pushliteral(L, "out of memory");
|
||||
return 2;
|
||||
}
|
||||
err = socket_recvfrom(&udp->sock, dgram, wanted, &got, (SA *) &addr,
|
||||
&addr_len, tm);
|
||||
/* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */
|
||||
if (err != IO_DONE && err != IO_CLOSED) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
if (wanted > sizeof(buf)) free(dgram);
|
||||
return 2;
|
||||
}
|
||||
err = getnameinfo((struct sockaddr *)&addr, addr_len, addrstr,
|
||||
INET6_ADDRSTRLEN, portstr, 6, NI_NUMERICHOST | NI_NUMERICSERV);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, gai_strerror(err));
|
||||
if (wanted > sizeof(buf)) free(dgram);
|
||||
return 2;
|
||||
}
|
||||
lua_pushlstring(L, dgram, got);
|
||||
lua_pushstring(L, addrstr);
|
||||
lua_pushinteger(L, (int) strtol(portstr, (char **) NULL, 10));
|
||||
if (wanted > sizeof(buf)) free(dgram);
|
||||
return 3;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Returns family as string
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_getfamily(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
if (udp->family == AF_INET6) {
|
||||
lua_pushliteral(L, "inet6");
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushliteral(L, "inet4");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Select support methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_getfd(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
lua_pushnumber(L, (int) udp->sock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* this is very dangerous, but can be handy for those that are brave enough */
|
||||
static int meth_setfd(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
udp->sock = (t_socket) luaL_checknumber(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int meth_dirty(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
(void) udp;
|
||||
lua_pushboolean(L, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call inet methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_getpeername(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1);
|
||||
return inet_meth_getpeername(L, &udp->sock, udp->family);
|
||||
}
|
||||
|
||||
static int meth_getsockname(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
return inet_meth_getsockname(L, &udp->sock, udp->family);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call option handler
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_setoption(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
return opt_meth_setoption(L, optset, &udp->sock);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call option handler
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_getoption(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
return opt_meth_getoption(L, optget, &udp->sock);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call tm methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_settimeout(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
return timeout_meth_settimeout(L, &udp->tm);
|
||||
}
|
||||
|
||||
static int meth_gettimeout(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
return timeout_meth_gettimeout(L, &udp->tm);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Turns a master udp object into a client object.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_setpeername(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
p_timeout tm = &udp->tm;
|
||||
const char *address = luaL_checkstring(L, 2);
|
||||
int connecting = strcmp(address, "*");
|
||||
const char *port = connecting? luaL_checkstring(L, 3): "0";
|
||||
struct addrinfo connecthints;
|
||||
const char *err;
|
||||
memset(&connecthints, 0, sizeof(connecthints));
|
||||
connecthints.ai_socktype = SOCK_DGRAM;
|
||||
/* make sure we try to connect only to the same family */
|
||||
connecthints.ai_family = udp->family;
|
||||
if (connecting) {
|
||||
err = inet_tryconnect(&udp->sock, &udp->family, address,
|
||||
port, tm, &connecthints);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
auxiliar_setclass(L, "udp{connected}", 1);
|
||||
} else {
|
||||
/* we ignore possible errors because Mac OS X always
|
||||
* returns EAFNOSUPPORT */
|
||||
inet_trydisconnect(&udp->sock, udp->family, tm);
|
||||
auxiliar_setclass(L, "udp{unconnected}", 1);
|
||||
}
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Closes socket used by object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_close(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
socket_destroy(&udp->sock);
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Turns a master object into a server object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_setsockname(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
|
||||
const char *address = luaL_checkstring(L, 2);
|
||||
const char *port = luaL_checkstring(L, 3);
|
||||
const char *err;
|
||||
struct addrinfo bindhints;
|
||||
memset(&bindhints, 0, sizeof(bindhints));
|
||||
bindhints.ai_socktype = SOCK_DGRAM;
|
||||
bindhints.ai_family = udp->family;
|
||||
bindhints.ai_flags = AI_PASSIVE;
|
||||
err = inet_trybind(&udp->sock, &udp->family, address, port, &bindhints);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Library functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Creates a master udp object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int udp_create(lua_State *L, int family) {
|
||||
/* allocate udp object */
|
||||
p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp));
|
||||
auxiliar_setclass(L, "udp{unconnected}", -1);
|
||||
/* if family is AF_UNSPEC, we leave the socket invalid and
|
||||
* store AF_UNSPEC into family. This will allow it to later be
|
||||
* replaced with an AF_INET6 or AF_INET socket upon first use. */
|
||||
udp->sock = SOCKET_INVALID;
|
||||
timeout_init(&udp->tm, -1, -1);
|
||||
udp->family = family;
|
||||
if (family != AF_UNSPEC) {
|
||||
const char *err = inet_trycreate(&udp->sock, family, SOCK_DGRAM, 0);
|
||||
if (err != NULL) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
socket_setnonblocking(&udp->sock);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int global_create(lua_State *L) {
|
||||
return udp_create(L, AF_UNSPEC);
|
||||
}
|
||||
|
||||
static int global_create4(lua_State *L) {
|
||||
return udp_create(L, AF_INET);
|
||||
}
|
||||
|
||||
static int global_create6(lua_State *L) {
|
||||
return udp_create(L, AF_INET6);
|
||||
}
|
@ -11,23 +11,29 @@
|
||||
* originally unconnected. They can be "connected" to a given address
|
||||
* with a call to the setpeername function. The same function can be used to
|
||||
* break the connection.
|
||||
*
|
||||
* RCS ID: $Id: udp.h,v 1.10 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "../LuaCompat.h"
|
||||
#include "luasocket.h"
|
||||
|
||||
#include "timeout.h"
|
||||
#include "socket.h"
|
||||
|
||||
/* can't be larger than wsocket.c MAXCHUNK!!! */
|
||||
#define UDP_DATAGRAMSIZE 8192
|
||||
|
||||
typedef struct t_udp_ {
|
||||
t_socket sock;
|
||||
t_timeout tm;
|
||||
int family;
|
||||
} t_udp;
|
||||
typedef t_udp *p_udp;
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility push(hidden)
|
||||
#endif
|
||||
|
||||
int udp_open(lua_State *L);
|
||||
|
||||
#ifndef _WIN32
|
||||
#pragma GCC visibility pop
|
||||
#endif
|
||||
|
||||
#endif /* UDP_H */
|
@ -1,4 +1,3 @@
|
||||
#ifndef WIN
|
||||
/*=========================================================================*\
|
||||
* Socket compatibilization module for Unix
|
||||
* LuaSocket toolkit
|
||||
@ -6,18 +5,19 @@
|
||||
* The code is now interrupt-safe.
|
||||
* The penalty of calling select to avoid busy-wait is only paid when
|
||||
* the I/O call fail in the first place.
|
||||
*
|
||||
* RCS ID: $Id: usocket.c,v 1.38 2007/10/13 23:55:20 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include "luasocket.h"
|
||||
|
||||
#include "socket.h"
|
||||
#include "pierror.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Wait for readable/writable/connected socket with timeout
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#ifdef SOCKET_POLL
|
||||
#ifndef SOCKET_SELECT
|
||||
#include <sys/poll.h>
|
||||
|
||||
#define WAITFD_R POLLIN
|
||||
@ -31,9 +31,9 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
||||
pfd.revents = 0;
|
||||
if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
|
||||
do {
|
||||
int t = (int)(timeout_getretry(tm)*1e3);
|
||||
ret = poll(&pfd, 1, t >= 0? t: -1);
|
||||
} while (ret == -1 && errno == EINTR);
|
||||
int t = (int)(timeout_getretry(tm)*1e3);
|
||||
ret = poll(&pfd, 1, t >= 0? t: -1);
|
||||
} while (ret == -1 && errno == EINTR);
|
||||
if (ret == -1) return errno;
|
||||
if (ret == 0) return IO_TIMEOUT;
|
||||
if (sw == WAITFD_C && (pfd.revents & (POLLIN|POLLERR))) return IO_CLOSED;
|
||||
@ -50,6 +50,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
||||
fd_set rfds, wfds, *rp, *wp;
|
||||
struct timeval tv, *tp;
|
||||
double t;
|
||||
if (*ps >= FD_SETSIZE) return EINVAL;
|
||||
if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
|
||||
do {
|
||||
/* must set bits within loop, because select may have modifed them */
|
||||
@ -77,7 +78,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
||||
* Initializes module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int socket_open(void) {
|
||||
/* instals 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);
|
||||
return 1;
|
||||
}
|
||||
@ -94,7 +95,6 @@ int socket_close(void) {
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void socket_destroy(p_socket ps) {
|
||||
if (*ps != SOCKET_INVALID) {
|
||||
socket_setblocking(ps);
|
||||
close(*ps);
|
||||
*ps = SOCKET_INVALID;
|
||||
}
|
||||
@ -142,9 +142,7 @@ int socket_bind(p_socket ps, SA *addr, socklen_t len) {
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int socket_listen(p_socket ps, int backlog) {
|
||||
int err = IO_DONE;
|
||||
socket_setblocking(ps);
|
||||
if (listen(*ps, backlog)) err = errno;
|
||||
socket_setnonblocking(ps);
|
||||
return err;
|
||||
}
|
||||
|
||||
@ -152,9 +150,7 @@ int socket_listen(p_socket ps, int backlog) {
|
||||
*
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void socket_shutdown(p_socket ps, int how) {
|
||||
socket_setblocking(ps);
|
||||
shutdown(*ps, how);
|
||||
socket_setnonblocking(ps);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -183,11 +179,7 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
|
||||
* Accept with timeout
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
|
||||
SA daddr;
|
||||
socklen_t dlen = sizeof(daddr);
|
||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||
if (!addr) addr = &daddr;
|
||||
if (!len) len = &dlen;
|
||||
for ( ;; ) {
|
||||
int err;
|
||||
if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE;
|
||||
@ -214,14 +206,15 @@ int socket_send(p_socket ps, const char *data, size_t count,
|
||||
for ( ;; ) {
|
||||
long put = (long) send(*ps, data, count, 0);
|
||||
/* if we sent anything, we are done */
|
||||
if (put > 0) {
|
||||
if (put >= 0) {
|
||||
*sent = put;
|
||||
return IO_DONE;
|
||||
}
|
||||
err = errno;
|
||||
/* send can't really return 0, but EPIPE means the connection was
|
||||
closed */
|
||||
if (put == 0 || err == EPIPE) return IO_CLOSED;
|
||||
/* EPIPE means the connection was closed */
|
||||
if (err == EPIPE) return IO_CLOSED;
|
||||
/* EPROTOTYPE means the connection is being closed (on Yosemite!)*/
|
||||
if (err == EPROTOTYPE) continue;
|
||||
/* we call was interrupted, just try again */
|
||||
if (err == EINTR) continue;
|
||||
/* if failed fatal reason, report error */
|
||||
@ -243,13 +236,14 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
|
||||
*sent = 0;
|
||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||
for ( ;; ) {
|
||||
long put = (long) sendto(*ps, data, count, 0, addr, len);
|
||||
if (put > 0) {
|
||||
long put = (long) sendto(*ps, data, count, 0, addr, len);
|
||||
if (put >= 0) {
|
||||
*sent = put;
|
||||
return IO_DONE;
|
||||
}
|
||||
err = errno;
|
||||
if (put == 0 || err == EPIPE) return IO_CLOSED;
|
||||
if (err == EPIPE) return IO_CLOSED;
|
||||
if (err == EPROTOTYPE) continue;
|
||||
if (err == EINTR) continue;
|
||||
if (err != EAGAIN) return err;
|
||||
if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
|
||||
@ -302,6 +296,68 @@ int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
|
||||
return IO_UNKNOWN;
|
||||
}
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Write with timeout
|
||||
*
|
||||
* socket_read and socket_write are cut-n-paste of socket_send and socket_recv,
|
||||
* 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.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int socket_write(p_socket ps, const char *data, size_t count,
|
||||
size_t *sent, p_timeout tm)
|
||||
{
|
||||
int err;
|
||||
*sent = 0;
|
||||
/* avoid making system calls on closed sockets */
|
||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||
/* loop until we send something or we give up on error */
|
||||
for ( ;; ) {
|
||||
long put = (long) write(*ps, data, count);
|
||||
/* if we sent anything, we are done */
|
||||
if (put >= 0) {
|
||||
*sent = put;
|
||||
return IO_DONE;
|
||||
}
|
||||
err = errno;
|
||||
/* EPIPE means the connection was closed */
|
||||
if (err == EPIPE) return IO_CLOSED;
|
||||
/* EPROTOTYPE means the connection is being closed (on Yosemite!)*/
|
||||
if (err == EPROTOTYPE) continue;
|
||||
/* we call was interrupted, just try again */
|
||||
if (err == EINTR) continue;
|
||||
/* if failed fatal reason, report error */
|
||||
if (err != EAGAIN) return err;
|
||||
/* wait until we can send something or we timeout */
|
||||
if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
|
||||
}
|
||||
/* can't reach here */
|
||||
return IO_UNKNOWN;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Read with timeout
|
||||
* See note for socket_write
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
|
||||
int err;
|
||||
*got = 0;
|
||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||
for ( ;; ) {
|
||||
long taken = (long) read(*ps, data, count);
|
||||
if (taken > 0) {
|
||||
*got = taken;
|
||||
return IO_DONE;
|
||||
}
|
||||
err = errno;
|
||||
if (taken == 0) return IO_CLOSED;
|
||||
if (err == EINTR) continue;
|
||||
if (err != EAGAIN) return err;
|
||||
if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
|
||||
}
|
||||
return IO_UNKNOWN;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Put socket into blocking mode
|
||||
\*-------------------------------------------------------------------------*/
|
||||
@ -346,7 +402,7 @@ int socket_gethostbyname(const char *addr, struct hostent **hp) {
|
||||
const char *socket_hoststrerror(int err) {
|
||||
if (err <= 0) return io_strerror(err);
|
||||
switch (err) {
|
||||
case HOST_NOT_FOUND: return "host not found";
|
||||
case HOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
|
||||
default: return hstrerror(err);
|
||||
}
|
||||
}
|
||||
@ -354,14 +410,16 @@ const char *socket_hoststrerror(int err) {
|
||||
const char *socket_strerror(int err) {
|
||||
if (err <= 0) return io_strerror(err);
|
||||
switch (err) {
|
||||
case EADDRINUSE: return "address already in use";
|
||||
case EISCONN: return "already connected";
|
||||
case EACCES: return "permission denied";
|
||||
case ECONNREFUSED: return "connection refused";
|
||||
case ECONNABORTED: return "closed";
|
||||
case ECONNRESET: return "closed";
|
||||
case ETIMEDOUT: return "timeout";
|
||||
default: return strerror(errno);
|
||||
case EADDRINUSE: return PIE_ADDRINUSE;
|
||||
case EISCONN: return PIE_ISCONN;
|
||||
case EACCES: return PIE_ACCESS;
|
||||
case ECONNREFUSED: return PIE_CONNREFUSED;
|
||||
case ECONNABORTED: return PIE_CONNABORTED;
|
||||
case ECONNRESET: return PIE_CONNRESET;
|
||||
case ETIMEDOUT: return PIE_TIMEDOUT;
|
||||
default: {
|
||||
return strerror(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -369,4 +427,28 @@ const char *socket_ioerror(p_socket ps, int err) {
|
||||
(void) ps;
|
||||
return socket_strerror(err);
|
||||
}
|
||||
|
||||
const char *socket_gaistrerror(int err) {
|
||||
if (err == 0) return NULL;
|
||||
switch (err) {
|
||||
case EAI_AGAIN: return PIE_AGAIN;
|
||||
case EAI_BADFLAGS: return PIE_BADFLAGS;
|
||||
#ifdef EAI_BADHINTS
|
||||
case EAI_BADHINTS: return PIE_BADHINTS;
|
||||
#endif
|
||||
case EAI_FAIL: return PIE_FAIL;
|
||||
case EAI_FAMILY: return PIE_FAMILY;
|
||||
case EAI_MEMORY: return PIE_MEMORY;
|
||||
case EAI_NONAME: return PIE_NONAME;
|
||||
#ifdef EAI_OVERFLOW
|
||||
case EAI_OVERFLOW: return PIE_OVERFLOW;
|
||||
#endif
|
||||
#ifdef EAI_PROTOCOL
|
||||
case EAI_PROTOCOL: return PIE_PROTOCOL;
|
||||
#endif
|
||||
case EAI_SERVICE: return PIE_SERVICE;
|
||||
case EAI_SOCKTYPE: return PIE_SOCKTYPE;
|
||||
case EAI_SYSTEM: return strerror(errno);
|
||||
default: return gai_strerror(err);
|
||||
}
|
||||
}
|
@ -3,8 +3,6 @@
|
||||
/*=========================================================================*\
|
||||
* Socket compatibilization module for Unix
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: usocket.h,v 1.7 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
|
||||
/*=========================================================================*\
|
||||
@ -31,9 +29,30 @@
|
||||
#include <arpa/inet.h>
|
||||
/* TCP options (nagle algorithm disable) */
|
||||
#include <netinet/tcp.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#ifndef SO_REUSEPORT
|
||||
#define SO_REUSEPORT SO_REUSEADDR
|
||||
#endif
|
||||
|
||||
/* Some platforms use IPV6_JOIN_GROUP instead if
|
||||
* IPV6_ADD_MEMBERSHIP. The semantics are same, though. */
|
||||
#ifndef IPV6_ADD_MEMBERSHIP
|
||||
#ifdef IPV6_JOIN_GROUP
|
||||
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
|
||||
#endif /* IPV6_JOIN_GROUP */
|
||||
#endif /* !IPV6_ADD_MEMBERSHIP */
|
||||
|
||||
/* Same with IPV6_DROP_MEMBERSHIP / IPV6_LEAVE_GROUP. */
|
||||
#ifndef IPV6_DROP_MEMBERSHIP
|
||||
#ifdef IPV6_LEAVE_GROUP
|
||||
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
|
||||
#endif /* IPV6_LEAVE_GROUP */
|
||||
#endif /* !IPV6_DROP_MEMBERSHIP */
|
||||
|
||||
typedef int t_socket;
|
||||
typedef t_socket *p_socket;
|
||||
typedef struct sockaddr_storage t_sockaddr_storage;
|
||||
|
||||
#define SOCKET_INVALID (-1)
|
||||
|
@ -4,12 +4,13 @@
|
||||
*
|
||||
* The penalty of calling select to avoid busy-wait is only paid when
|
||||
* the I/O call fail in the first place.
|
||||
*
|
||||
* RCS ID: $Id: wsocket.c,v 1.36 2007/06/11 23:44:54 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "luasocket.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "socket.h"
|
||||
#include "pierror.h"
|
||||
|
||||
/* WinSock doesn't have a strerror... */
|
||||
static const char *wstrerror(int err);
|
||||
@ -54,7 +55,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
|
||||
if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
|
||||
if (sw & WAITFD_R) {
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(*ps, &rfds);
|
||||
FD_SET(*ps, &rfds);
|
||||
rp = &rfds;
|
||||
}
|
||||
if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; }
|
||||
@ -132,11 +133,11 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
|
||||
/* we wait until something happens */
|
||||
err = socket_waitfd(ps, WAITFD_C, tm);
|
||||
if (err == IO_CLOSED) {
|
||||
int len = sizeof(err);
|
||||
int elen = sizeof(err);
|
||||
/* give windows time to set the error (yes, disgusting) */
|
||||
Sleep(10);
|
||||
/* find out why we failed */
|
||||
getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len);
|
||||
getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &elen);
|
||||
/* we KNOW there was an error. if 'why' is 0, we will return
|
||||
* "unknown error", but it's not really our fault */
|
||||
return err > 0? err: IO_UNKNOWN;
|
||||
@ -171,11 +172,7 @@ int socket_listen(p_socket ps, int backlog) {
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
|
||||
p_timeout tm) {
|
||||
SA daddr;
|
||||
socklen_t dlen = sizeof(daddr);
|
||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||
if (!addr) addr = &daddr;
|
||||
if (!len) len = &dlen;
|
||||
for ( ;; ) {
|
||||
int err;
|
||||
/* try to get client socket */
|
||||
@ -187,8 +184,6 @@ int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
|
||||
/* call select to avoid busy wait */
|
||||
if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
|
||||
}
|
||||
/* can't reach here */
|
||||
return IO_UNKNOWN;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -207,7 +202,7 @@ int socket_send(p_socket ps, const char *data, size_t count,
|
||||
/* loop until we send something or we give up on error */
|
||||
for ( ;; ) {
|
||||
/* try to send something */
|
||||
int put = send(*ps, data, (int) count, 0);
|
||||
int put = send(*ps, data, (int) count, 0);
|
||||
/* if we sent something, we are done */
|
||||
if (put > 0) {
|
||||
*sent = put;
|
||||
@ -220,8 +215,6 @@ int socket_send(p_socket ps, const char *data, size_t count,
|
||||
/* avoid busy wait */
|
||||
if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
|
||||
}
|
||||
/* can't reach here */
|
||||
return IO_UNKNOWN;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -243,14 +236,15 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
|
||||
if (err != WSAEWOULDBLOCK) return err;
|
||||
if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
|
||||
}
|
||||
return IO_UNKNOWN;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Receive with timeout
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
|
||||
int err;
|
||||
int socket_recv(p_socket ps, char *data, size_t count, size_t *got,
|
||||
p_timeout tm)
|
||||
{
|
||||
int err, prev = IO_DONE;
|
||||
*got = 0;
|
||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||
for ( ;; ) {
|
||||
@ -261,18 +255,25 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm
|
||||
}
|
||||
if (taken == 0) return IO_CLOSED;
|
||||
err = WSAGetLastError();
|
||||
if (err != WSAEWOULDBLOCK) return err;
|
||||
/* On UDP, a connreset simply means the previous send failed.
|
||||
* So we try again.
|
||||
* On TCP, it means our socket is now useless, so the error passes.
|
||||
* (We will loop again, exiting because the same error will happen) */
|
||||
if (err != WSAEWOULDBLOCK) {
|
||||
if (err != WSAECONNRESET || prev == WSAECONNRESET) return err;
|
||||
prev = err;
|
||||
}
|
||||
if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
|
||||
}
|
||||
return IO_UNKNOWN;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Recvfrom with timeout
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
|
||||
SA *addr, socklen_t *len, p_timeout tm) {
|
||||
int err;
|
||||
SA *addr, socklen_t *len, p_timeout tm)
|
||||
{
|
||||
int err, prev = IO_DONE;
|
||||
*got = 0;
|
||||
if (*ps == SOCKET_INVALID) return IO_CLOSED;
|
||||
for ( ;; ) {
|
||||
@ -283,10 +284,16 @@ int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
|
||||
}
|
||||
if (taken == 0) return IO_CLOSED;
|
||||
err = WSAGetLastError();
|
||||
if (err != WSAEWOULDBLOCK) return err;
|
||||
/* On UDP, a connreset simply means the previous send failed.
|
||||
* So we try again.
|
||||
* On TCP, it means our socket is now useless, so the error passes.
|
||||
* (We will loop again, exiting because the same error will happen) */
|
||||
if (err != WSAEWOULDBLOCK) {
|
||||
if (err != WSAECONNRESET || prev == WSAECONNRESET) return err;
|
||||
prev = err;
|
||||
}
|
||||
if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
|
||||
}
|
||||
return IO_UNKNOWN;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -326,7 +333,7 @@ int socket_gethostbyname(const char *addr, struct hostent **hp) {
|
||||
const char *socket_hoststrerror(int err) {
|
||||
if (err <= 0) return io_strerror(err);
|
||||
switch (err) {
|
||||
case WSAHOST_NOT_FOUND: return "host not found";
|
||||
case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
|
||||
default: return wstrerror(err);
|
||||
}
|
||||
}
|
||||
@ -334,26 +341,26 @@ const char *socket_hoststrerror(int err) {
|
||||
const char *socket_strerror(int err) {
|
||||
if (err <= 0) return io_strerror(err);
|
||||
switch (err) {
|
||||
case WSAEADDRINUSE: return "address already in use";
|
||||
case WSAECONNREFUSED: return "connection refused";
|
||||
case WSAEISCONN: return "already connected";
|
||||
case WSAEACCES: return "permission denied";
|
||||
case WSAECONNABORTED: return "closed";
|
||||
case WSAECONNRESET: return "closed";
|
||||
case WSAETIMEDOUT: return "timeout";
|
||||
case WSAEADDRINUSE: return PIE_ADDRINUSE;
|
||||
case WSAECONNREFUSED : return PIE_CONNREFUSED;
|
||||
case WSAEISCONN: return PIE_ISCONN;
|
||||
case WSAEACCES: return PIE_ACCESS;
|
||||
case WSAECONNABORTED: return PIE_CONNABORTED;
|
||||
case WSAECONNRESET: return PIE_CONNRESET;
|
||||
case WSAETIMEDOUT: return PIE_TIMEDOUT;
|
||||
default: return wstrerror(err);
|
||||
}
|
||||
}
|
||||
|
||||
const char *socket_ioerror(p_socket ps, int err) {
|
||||
(void) ps;
|
||||
return socket_strerror(err);
|
||||
(void) ps;
|
||||
return socket_strerror(err);
|
||||
}
|
||||
|
||||
static const char *wstrerror(int err) {
|
||||
switch (err) {
|
||||
case WSAEINTR: return "Interrupted function call";
|
||||
case WSAEACCES: return "Permission denied";
|
||||
case WSAEACCES: return PIE_ACCESS; // "Permission denied";
|
||||
case WSAEFAULT: return "Bad address";
|
||||
case WSAEINVAL: return "Invalid argument";
|
||||
case WSAEMFILE: return "Too many open files";
|
||||
@ -366,24 +373,23 @@ static const char *wstrerror(int err) {
|
||||
case WSAEPROTOTYPE: return "Protocol wrong type for socket";
|
||||
case WSAENOPROTOOPT: return "Bad protocol option";
|
||||
case WSAEPROTONOSUPPORT: return "Protocol not supported";
|
||||
case WSAESOCKTNOSUPPORT: return "Socket type not supported";
|
||||
case WSAESOCKTNOSUPPORT: return PIE_SOCKTYPE; // "Socket type not supported";
|
||||
case WSAEOPNOTSUPP: return "Operation not supported";
|
||||
case WSAEPFNOSUPPORT: return "Protocol family not supported";
|
||||
case WSAEAFNOSUPPORT:
|
||||
return "Address family not supported by protocol family";
|
||||
case WSAEADDRINUSE: return "Address already in use";
|
||||
case WSAEAFNOSUPPORT: return PIE_FAMILY; // "Address family not supported by protocol family";
|
||||
case WSAEADDRINUSE: return PIE_ADDRINUSE; // "Address already in use";
|
||||
case WSAEADDRNOTAVAIL: return "Cannot assign requested address";
|
||||
case WSAENETDOWN: return "Network is down";
|
||||
case WSAENETUNREACH: return "Network is unreachable";
|
||||
case WSAENETRESET: return "Network dropped connection on reset";
|
||||
case WSAECONNABORTED: return "Software caused connection abort";
|
||||
case WSAECONNRESET: return "Connection reset by peer";
|
||||
case WSAECONNRESET: return PIE_CONNRESET; // "Connection reset by peer";
|
||||
case WSAENOBUFS: return "No buffer space available";
|
||||
case WSAEISCONN: return "Socket is already connected";
|
||||
case WSAEISCONN: return PIE_ISCONN; // "Socket is already connected";
|
||||
case WSAENOTCONN: return "Socket is not connected";
|
||||
case WSAESHUTDOWN: return "Cannot send after socket shutdown";
|
||||
case WSAETIMEDOUT: return "Connection timed out";
|
||||
case WSAECONNREFUSED: return "Connection refused";
|
||||
case WSAETIMEDOUT: return PIE_TIMEDOUT; // "Connection timed out";
|
||||
case WSAECONNREFUSED: return PIE_CONNREFUSED; // "Connection refused";
|
||||
case WSAEHOSTDOWN: return "Host is down";
|
||||
case WSAEHOSTUNREACH: return "No route to host";
|
||||
case WSAEPROCLIM: return "Too many processes";
|
||||
@ -392,10 +398,37 @@ static const char *wstrerror(int err) {
|
||||
case WSANOTINITIALISED:
|
||||
return "Successful WSAStartup not yet performed";
|
||||
case WSAEDISCON: return "Graceful shutdown in progress";
|
||||
case WSAHOST_NOT_FOUND: return "Host not found";
|
||||
case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; // "Host not found";
|
||||
case WSATRY_AGAIN: return "Nonauthoritative host not found";
|
||||
case WSANO_RECOVERY: return "Nonrecoverable name lookup error";
|
||||
case WSANO_RECOVERY: return PIE_FAIL; // "Nonrecoverable name lookup error";
|
||||
case WSANO_DATA: return "Valid name, no data record of requested type";
|
||||
default: return "Unknown error";
|
||||
}
|
||||
}
|
||||
|
||||
const char *socket_gaistrerror(int err) {
|
||||
if (err == 0) return NULL;
|
||||
switch (err) {
|
||||
case EAI_AGAIN: return PIE_AGAIN;
|
||||
case EAI_BADFLAGS: return PIE_BADFLAGS;
|
||||
#ifdef EAI_BADHINTS
|
||||
case EAI_BADHINTS: return PIE_BADHINTS;
|
||||
#endif
|
||||
case EAI_FAIL: return PIE_FAIL;
|
||||
case EAI_FAMILY: return PIE_FAMILY;
|
||||
case EAI_MEMORY: return PIE_MEMORY;
|
||||
case EAI_NONAME: return PIE_NONAME;
|
||||
#ifdef EAI_OVERFLOW
|
||||
case EAI_OVERFLOW: return PIE_OVERFLOW;
|
||||
#endif
|
||||
#ifdef EAI_PROTOCOL
|
||||
case EAI_PROTOCOL: return PIE_PROTOCOL;
|
||||
#endif
|
||||
case EAI_SERVICE: return PIE_SERVICE;
|
||||
case EAI_SOCKTYPE: return PIE_SOCKTYPE;
|
||||
#ifdef EAI_SYSTEM
|
||||
case EAI_SYSTEM: return strerror(errno);
|
||||
#endif
|
||||
default: return gai_strerror(err);
|
||||
}
|
||||
}
|
@ -3,19 +3,31 @@
|
||||
/*=========================================================================*\
|
||||
* Socket compatibilization module for Win32
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: wsocket.h,v 1.4 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
|
||||
/*=========================================================================*\
|
||||
* WinSock include files
|
||||
\*=========================================================================*/
|
||||
#include <winsock.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
typedef int socklen_t;
|
||||
typedef SOCKADDR_STORAGE t_sockaddr_storage;
|
||||
typedef SOCKET t_socket;
|
||||
typedef t_socket *p_socket;
|
||||
|
||||
#ifndef IPV6_V6ONLY
|
||||
#define IPV6_V6ONLY 27
|
||||
#endif
|
||||
|
||||
#define SOCKET_INVALID (INVALID_SOCKET)
|
||||
|
||||
#ifndef SO_REUSEPORT
|
||||
#define SO_REUSEPORT SO_REUSEADDR
|
||||
#endif
|
||||
|
||||
#ifndef AI_NUMERICSERV
|
||||
#define AI_NUMERICSERV (0)
|
||||
#endif
|
||||
|
||||
#endif /* WSOCKET_H */
|
@ -8,26 +8,13 @@ luaconsole_files = files(
|
||||
'LuaLabel.cpp',
|
||||
'LuaProgressBar.cpp',
|
||||
'LuaScriptInterface.cpp',
|
||||
'luascripts/eventcompat.lua.cpp',
|
||||
'LuaSlider.cpp',
|
||||
'LuaSmartRef.cpp',
|
||||
'LuaTextbox.cpp',
|
||||
'LuaWindow.cpp',
|
||||
'socket/auxiliar.c',
|
||||
'socket/buffer.c',
|
||||
'socket/except.c',
|
||||
'socket/inet.c',
|
||||
'socket/io.c',
|
||||
'socket/luasocket.c',
|
||||
'socket/options.c',
|
||||
'socket/select.c',
|
||||
'socket/socket.lua.cpp',
|
||||
'socket/tcp.c',
|
||||
'socket/timeout.c',
|
||||
'socket/udp.c',
|
||||
)
|
||||
|
||||
subdir('luasocket')
|
||||
subdir('luascripts')
|
||||
subdir('socket')
|
||||
|
||||
powder_files += luaconsole_files
|
||||
|
@ -1,35 +0,0 @@
|
||||
#ifndef EXCEPT_H
|
||||
#define EXCEPT_H
|
||||
/*=========================================================================*\
|
||||
* Exception control
|
||||
* LuaSocket toolkit (but completely independent from other modules)
|
||||
*
|
||||
* This provides support for simple exceptions in Lua. During the
|
||||
* development of the HTTP/FTP/SMTP support, it became apparent that
|
||||
* error checking was taking a substantial amount of the coding. These
|
||||
* function greatly simplify the task of checking errors.
|
||||
*
|
||||
* The main idea is that functions should return nil as its first return
|
||||
* value when it finds an error, and return an error message (or value)
|
||||
* following nil. In case of success, as long as the first value is not nil,
|
||||
* the other values don't matter.
|
||||
*
|
||||
* The idea is to nest function calls with the "try" function. This function
|
||||
* checks the first value, and calls "error" on the second if the first is
|
||||
* nil. Otherwise, it returns all values it received.
|
||||
*
|
||||
* The protect function returns a new function that behaves exactly like the
|
||||
* function it receives, but the new function doesn't throw exceptions: it
|
||||
* returns nil followed by the error message instead.
|
||||
*
|
||||
* With these two function, it's easy to write functions that throw
|
||||
* exceptions on error, but that don't interrupt the user script.
|
||||
*
|
||||
* RCS ID: $Id: except.h,v 1.2 2005/09/29 06:11:41 diego Exp $
|
||||
\*=========================================================================*/
|
||||
|
||||
#include "../LuaCompat.h"
|
||||
|
||||
int except_open(lua_State *L);
|
||||
|
||||
#endif
|
@ -1,278 +0,0 @@
|
||||
/*=========================================================================*\
|
||||
* Internet domain functions
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: inet.c,v 1.28 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "inet.h"
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal function prototypes.
|
||||
\*=========================================================================*/
|
||||
static int inet_global_toip(lua_State *L);
|
||||
static int inet_global_tohostname(lua_State *L);
|
||||
static void inet_pushresolved(lua_State *L, struct hostent *hp);
|
||||
static int inet_global_gethostname(lua_State *L);
|
||||
|
||||
/* DNS functions */
|
||||
static luaL_Reg func[] = {
|
||||
{ "toip", inet_global_toip },
|
||||
{ "tohostname", inet_global_tohostname },
|
||||
{ "gethostname", inet_global_gethostname},
|
||||
{ NULL, NULL}
|
||||
};
|
||||
|
||||
/*=========================================================================*\
|
||||
* Exported functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int inet_open(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, "dns");
|
||||
lua_newtable(L);
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
lua_settable(L, -3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Global Lua functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Returns all information provided by the resolver given a host name
|
||||
* or ip address
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int inet_gethost(const char *address, struct hostent **hp) {
|
||||
struct in_addr addr;
|
||||
if (inet_aton(address, &addr))
|
||||
return socket_gethostbyaddr((char *) &addr, sizeof(addr), hp);
|
||||
else
|
||||
return socket_gethostbyname(address, hp);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Returns all information provided by the resolver given a host name
|
||||
* or ip address
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int inet_global_tohostname(lua_State *L) {
|
||||
const char *address = luaL_checkstring(L, 1);
|
||||
struct hostent *hp = NULL;
|
||||
int err = inet_gethost(address, &hp);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_hoststrerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushstring(L, hp->h_name);
|
||||
inet_pushresolved(L, hp);
|
||||
return 2;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Returns all information provided by the resolver given a host name
|
||||
* or ip address
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int inet_global_toip(lua_State *L)
|
||||
{
|
||||
const char *address = luaL_checkstring(L, 1);
|
||||
struct hostent *hp = NULL;
|
||||
int err = inet_gethost(address, &hp);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_hoststrerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushstring(L, inet_ntoa(*((struct in_addr *) hp->h_addr)));
|
||||
inet_pushresolved(L, hp);
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Gets the host name
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int inet_global_gethostname(lua_State *L)
|
||||
{
|
||||
char name[257];
|
||||
name[256] = '\0';
|
||||
if (gethostname(name, 256) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "gethostname failed");
|
||||
return 2;
|
||||
} else {
|
||||
lua_pushstring(L, name);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*=========================================================================*\
|
||||
* Lua methods
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Retrieves socket peer name
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int inet_meth_getpeername(lua_State *L, p_socket ps)
|
||||
{
|
||||
struct sockaddr_in peer;
|
||||
socklen_t peer_len = sizeof(peer);
|
||||
if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "getpeername failed");
|
||||
} else {
|
||||
lua_pushstring(L, inet_ntoa(peer.sin_addr));
|
||||
lua_pushnumber(L, ntohs(peer.sin_port));
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Retrieves socket local name
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int inet_meth_getsockname(lua_State *L, p_socket ps)
|
||||
{
|
||||
struct sockaddr_in local;
|
||||
socklen_t local_len = sizeof(local);
|
||||
if (getsockname(*ps, (SA *) &local, &local_len) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "getsockname failed");
|
||||
} else {
|
||||
lua_pushstring(L, inet_ntoa(local.sin_addr));
|
||||
lua_pushnumber(L, ntohs(local.sin_port));
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Passes all resolver information to Lua as a table
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static void inet_pushresolved(lua_State *L, struct hostent *hp)
|
||||
{
|
||||
char **alias;
|
||||
struct in_addr **addr;
|
||||
int i, resolved;
|
||||
lua_newtable(L); resolved = lua_gettop(L);
|
||||
lua_pushstring(L, "name");
|
||||
lua_pushstring(L, hp->h_name);
|
||||
lua_settable(L, resolved);
|
||||
lua_pushstring(L, "ip");
|
||||
lua_pushstring(L, "alias");
|
||||
i = 1;
|
||||
alias = hp->h_aliases;
|
||||
lua_newtable(L);
|
||||
if (alias) {
|
||||
while (*alias) {
|
||||
lua_pushnumber(L, i);
|
||||
lua_pushstring(L, *alias);
|
||||
lua_settable(L, -3);
|
||||
i++; alias++;
|
||||
}
|
||||
}
|
||||
lua_settable(L, resolved);
|
||||
i = 1;
|
||||
lua_newtable(L);
|
||||
addr = (struct in_addr **) hp->h_addr_list;
|
||||
if (addr) {
|
||||
while (*addr) {
|
||||
lua_pushnumber(L, i);
|
||||
lua_pushstring(L, inet_ntoa(**addr));
|
||||
lua_settable(L, -3);
|
||||
i++; addr++;
|
||||
}
|
||||
}
|
||||
lua_settable(L, resolved);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Tries to create a new inet socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_trycreate(p_socket ps, int type) {
|
||||
return socket_strerror(socket_create(ps, AF_INET, type, 0));
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Tries to connect to remote address (address, port)
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_tryconnect(p_socket ps, const char *address,
|
||||
unsigned short port, p_timeout tm)
|
||||
{
|
||||
struct sockaddr_in remote;
|
||||
int err;
|
||||
memset(&remote, 0, sizeof(remote));
|
||||
remote.sin_family = AF_INET;
|
||||
remote.sin_port = htons(port);
|
||||
if (strcmp(address, "*")) {
|
||||
if (!inet_aton(address, &remote.sin_addr)) {
|
||||
struct hostent *hp = NULL;
|
||||
struct in_addr **addr;
|
||||
err = socket_gethostbyname(address, &hp);
|
||||
if (err != IO_DONE) return socket_hoststrerror(err);
|
||||
addr = (struct in_addr **) hp->h_addr_list;
|
||||
memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
|
||||
}
|
||||
} else remote.sin_family = AF_UNSPEC;
|
||||
err = socket_connect(ps, (SA *) &remote, sizeof(remote), tm);
|
||||
return socket_strerror(err);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Tries to bind socket to (address, port)
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_trybind(p_socket ps, const char *address, unsigned short port)
|
||||
{
|
||||
struct sockaddr_in local;
|
||||
int err;
|
||||
memset(&local, 0, sizeof(local));
|
||||
/* address is either wildcard or a valid ip address */
|
||||
local.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
local.sin_port = htons(port);
|
||||
local.sin_family = AF_INET;
|
||||
if (strcmp(address, "*") && !inet_aton(address, &local.sin_addr)) {
|
||||
struct hostent *hp = NULL;
|
||||
struct in_addr **addr;
|
||||
err = socket_gethostbyname(address, &hp);
|
||||
if (err != IO_DONE) return socket_hoststrerror(err);
|
||||
addr = (struct in_addr **) hp->h_addr_list;
|
||||
memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
|
||||
}
|
||||
err = socket_bind(ps, (SA *) &local, sizeof(local));
|
||||
if (err != IO_DONE) socket_destroy(ps);
|
||||
return socket_strerror(err);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Some systems do not provide this so that we provide our own. It's not
|
||||
* marvelously fast, but it works just fine.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#ifdef INET_ATON
|
||||
int inet_aton(const char *cp, struct in_addr *inp)
|
||||
{
|
||||
unsigned int a = 0, b = 0, c = 0, d = 0;
|
||||
int n = 0, r;
|
||||
unsigned long int addr = 0;
|
||||
r = sscanf(cp, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n);
|
||||
if (r == 0 || n == 0) return 0;
|
||||
cp += n;
|
||||
if (*cp) return 0;
|
||||
if (a > 255 || b > 255 || c > 255 || d > 255) return 0;
|
||||
if (inp) {
|
||||
addr += a; addr <<= 8;
|
||||
addr += b; addr <<= 8;
|
||||
addr += c; addr <<= 8;
|
||||
addr += d;
|
||||
inp->s_addr = htonl(addr);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -1,42 +0,0 @@
|
||||
#ifndef INET_H
|
||||
#define INET_H
|
||||
/*=========================================================================*\
|
||||
* Internet domain functions
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* This module implements the creation and connection of internet domain
|
||||
* sockets, on top of the socket.h interface, and the interface of with the
|
||||
* resolver.
|
||||
*
|
||||
* The function inet_aton is provided for the platforms where it is not
|
||||
* available. The module also implements the interface of the internet
|
||||
* getpeername and getsockname functions as seen by Lua programs.
|
||||
*
|
||||
* The Lua functions toip and tohostname are also implemented here.
|
||||
*
|
||||
* RCS ID: $Id: inet.h,v 1.16 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "../LuaCompat.h"
|
||||
#include "socket.h"
|
||||
#include "timeout.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define INET_ATON
|
||||
#endif
|
||||
|
||||
int inet_open(lua_State *L);
|
||||
|
||||
const char *inet_trycreate(p_socket ps, int type);
|
||||
const char *inet_tryconnect(p_socket ps, const char *address,
|
||||
unsigned short port, p_timeout tm);
|
||||
const char *inet_trybind(p_socket ps, const char *address,
|
||||
unsigned short port);
|
||||
|
||||
int inet_meth_getpeername(lua_State *L, p_socket ps);
|
||||
int inet_meth_getsockname(lua_State *L, p_socket ps);
|
||||
|
||||
#ifdef INET_ATON
|
||||
int inet_aton(const char *cp, struct in_addr *inp);
|
||||
#endif
|
||||
|
||||
#endif /* INET_H */
|
@ -1,10 +0,0 @@
|
||||
if copt_platform == 'windows'
|
||||
luaconsole_files += files(
|
||||
'wsocket.c',
|
||||
)
|
||||
else
|
||||
luaconsole_files += files(
|
||||
'unix.c',
|
||||
'usocket.c',
|
||||
)
|
||||
endif
|
@ -1,149 +0,0 @@
|
||||
/*=========================================================================*\
|
||||
* Common option interface
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: options.c,v 1.6 2005/11/20 07:20:23 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "options.h"
|
||||
#include "inet.h"
|
||||
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal functions prototypes
|
||||
\*=========================================================================*/
|
||||
static int opt_setmembership(lua_State *L, p_socket ps, int level, int name);
|
||||
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name);
|
||||
static int opt_set(lua_State *L, p_socket ps, int level, int name,
|
||||
void *val, int len);
|
||||
|
||||
/*=========================================================================*\
|
||||
* Exported functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Calls appropriate option handler
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
|
||||
{
|
||||
const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
|
||||
while (opt->name && strcmp(name, opt->name))
|
||||
opt++;
|
||||
if (!opt->func) {
|
||||
char* msg = (char*)malloc(30+strlen(name));
|
||||
sprintf(msg, "unsupported option `%.35s'", name);
|
||||
luaL_argerror(L, 2, msg);
|
||||
free(msg);
|
||||
}
|
||||
return opt->func(L, ps);
|
||||
}
|
||||
|
||||
/* enables reuse of local address */
|
||||
int opt_reuseaddr(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
|
||||
}
|
||||
|
||||
/* disables the Naggle algorithm */
|
||||
int opt_tcp_nodelay(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
|
||||
}
|
||||
|
||||
int opt_keepalive(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
|
||||
}
|
||||
|
||||
int opt_dontroute(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
|
||||
}
|
||||
|
||||
int opt_broadcast(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
|
||||
}
|
||||
|
||||
int opt_ip_multicast_loop(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
|
||||
}
|
||||
|
||||
int opt_linger(lua_State *L, p_socket ps)
|
||||
{
|
||||
struct linger li; /* obj, name, table */
|
||||
if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE));
|
||||
lua_pushstring(L, "on");
|
||||
lua_gettable(L, 3);
|
||||
if (!lua_isboolean(L, -1))
|
||||
luaL_argerror(L, 3, "boolean 'on' field expected");
|
||||
li.l_onoff = (u_short) lua_toboolean(L, -1);
|
||||
lua_pushstring(L, "timeout");
|
||||
lua_gettable(L, 3);
|
||||
if (!lua_isnumber(L, -1))
|
||||
luaL_argerror(L, 3, "number 'timeout' field expected");
|
||||
li.l_linger = (u_short) lua_tonumber(L, -1);
|
||||
return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
|
||||
}
|
||||
|
||||
int opt_ip_multicast_ttl(lua_State *L, p_socket ps)
|
||||
{
|
||||
int val = (int) luaL_checknumber(L, 3); /* obj, name, int */
|
||||
return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &val, sizeof(val));
|
||||
}
|
||||
|
||||
int opt_ip_add_membership(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
|
||||
}
|
||||
|
||||
int opt_ip_drop_membersip(lua_State *L, p_socket ps)
|
||||
{
|
||||
return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Auxiliar functions
|
||||
\*=========================================================================*/
|
||||
static int opt_setmembership(lua_State *L, p_socket ps, int level, int name)
|
||||
{
|
||||
struct ip_mreq val; /* obj, name, table */
|
||||
if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE));
|
||||
lua_pushstring(L, "multiaddr");
|
||||
lua_gettable(L, 3);
|
||||
if (!lua_isstring(L, -1))
|
||||
luaL_argerror(L, 3, "string 'multiaddr' field expected");
|
||||
if (!inet_aton(lua_tostring(L, -1), &val.imr_multiaddr))
|
||||
luaL_argerror(L, 3, "invalid 'multiaddr' ip address");
|
||||
lua_pushstring(L, "interface");
|
||||
lua_gettable(L, 3);
|
||||
if (!lua_isstring(L, -1))
|
||||
luaL_argerror(L, 3, "string 'interface' field expected");
|
||||
val.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||
if (strcmp(lua_tostring(L, -1), "*") &&
|
||||
!inet_aton(lua_tostring(L, -1), &val.imr_interface))
|
||||
luaL_argerror(L, 3, "invalid 'interface' ip address");
|
||||
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
|
||||
}
|
||||
|
||||
static
|
||||
int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len)
|
||||
{
|
||||
if (setsockopt(*ps, level, name, (char *) val, len) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "setsockopt failed");
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
|
||||
{
|
||||
int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */
|
||||
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
|
||||
}
|
||||
|
@ -1,39 +0,0 @@
|
||||
#ifndef OPTIONS_H
|
||||
#define OPTIONS_H
|
||||
/*=========================================================================*\
|
||||
* Common option interface
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* This module provides a common interface to socket options, used mainly by
|
||||
* modules UDP and TCP.
|
||||
*
|
||||
* RCS ID: $Id: options.h,v 1.4 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
|
||||
#include "../LuaCompat.h"
|
||||
#include "socket.h"
|
||||
|
||||
/* option registry */
|
||||
typedef struct t_opt {
|
||||
const char *name;
|
||||
int (*func)(lua_State *L, p_socket ps);
|
||||
} t_opt;
|
||||
typedef t_opt *p_opt;
|
||||
|
||||
/* supported options */
|
||||
int opt_dontroute(lua_State *L, p_socket ps);
|
||||
int opt_broadcast(lua_State *L, p_socket ps);
|
||||
int opt_reuseaddr(lua_State *L, p_socket ps);
|
||||
int opt_tcp_nodelay(lua_State *L, p_socket ps);
|
||||
int opt_keepalive(lua_State *L, p_socket ps);
|
||||
int opt_linger(lua_State *L, p_socket ps);
|
||||
int opt_reuseaddr(lua_State *L, p_socket ps);
|
||||
int opt_ip_multicast_ttl(lua_State *L, p_socket ps);
|
||||
int opt_ip_multicast_loop(lua_State *L, p_socket ps);
|
||||
int opt_ip_add_membership(lua_State *L, p_socket ps);
|
||||
int opt_ip_drop_membersip(lua_State *L, p_socket ps);
|
||||
|
||||
/* invokes the appropriate option handler */
|
||||
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps);
|
||||
|
||||
#endif
|
@ -1,11 +0,0 @@
|
||||
#include "Config.h"
|
||||
#ifdef LUACONSOLE
|
||||
// socket.lua from luasocket compiled into a cpp file
|
||||
#include "../LuaCompat.h"
|
||||
void luaopen_socket(lua_State *l){
|
||||
int socket_luac_sz=4061;
|
||||
const char* socket_luac="-----------------------------------------------------------------------------\012-- LuaSocket helper module\012-- Author: Diego Nehab\012-- RCS ID: $Id: socket.lua,v 1.22 2005/11/22 08:33:29 diego Exp $\012-----------------------------------------------------------------------------\012\012-----------------------------------------------------------------------------\012-- Declare module and import dependencies\012-----------------------------------------------------------------------------\012local base = _G\012local string = require(\042string\042)\012local math = require(\042math\042)\012local socket = require(\042socket.core\042)\012module(\042socket\042)\012\012-----------------------------------------------------------------------------\012-- Exported auxiliar functions\012-----------------------------------------------------------------------------\012function connect(address, port, laddress, lport)\012 local sock, err = socket.tcp()\012 if not sock then return nil, err end\012 if laddress then\012 local res, err = sock:bind(laddress, lport, -1)\012 if not res then return nil, err end\012 end\012 local res, err = sock:connect(address, port)\012 if not res then return nil, err end\012 return sock\012end\012\012function bind(host, port, backlog)\012 local sock, err = socket.tcp()\012 if not sock then return nil, err end\012 sock:setoption(\042reuseaddr\042, true)\012 local res, err = sock:bind(host, port)\012 if not res then return nil, err end\012 res, err = sock:listen(backlog)\012 if not res then return nil, err end\012 return sock\012end\012\012try = newtry()\012\012function choose(table)\012 return function(name, opt1, opt2)\012 if base.type(name) ~= \042string\042 then\012 name, opt1, opt2 = \042default\042, name, opt1\012 end\012 local f = table[name or \042nil\042]\012 if not f then base.error(\042unknown key (\042.. base.tostring(name) ..\042)\042, 3)\012 else return f(opt1, opt2) end\012 end\012end\012\012-----------------------------------------------------------------------------\012-- Socket sources and sinks, conforming to LTN12\012-----------------------------------------------------------------------------\012-- create namespaces inside LuaSocket namespace\012sourcet = {}\012sinkt = {}\012\012BLOCKSIZE = 2048\012\012sinkt[\042close-when-done\042] = function(sock)\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function(self, chunk, err)\012 if not chunk then\012 sock:close()\012 return 1\012 else return sock:send(chunk) end\012 end\012 })\012end\012\012sinkt[\042keep-open\042] = function(sock)\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function(self, chunk, err)\012 if chunk then return sock:send(chunk)\012 else return 1 end\012 end\012 })\012end\012\012sinkt[\042default\042] = sinkt[\042keep-open\042]\012\012sink = choose(sinkt)\012\012sourcet[\042by-length\042] = function(sock, length)\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function()\012 if length <= 0 then return nil end\012 local size = math.min(socket.BLOCKSIZE, length)\012 local chunk, err = sock:receive(size)\012 if err then return nil, err end\012 length = length - string.len(chunk)\012 return chunk\012 end\012 })\012end\012\012sourcet[\042until-closed\042] = function(sock)\012 local done\012 return base.setmetatable({\012 getfd = function() return sock:getfd() end,\012 dirty = function() return sock:dirty() end\012 }, {\012 __call = function()\012 if done then return nil end\012 local chunk, err, partial = sock:receive(socket.BLOCKSIZE)\012 if not err then return chunk\012 elseif err == \042closed\042 then\012 sock:close()\012 done = 1\012 return partial\012 else return nil, err end\012 end\012 })\012end\012\012\012sourcet[\042default\042] = sourcet[\042until-closed\042]\012\012source = choose(sourcet)\012\012";
|
||||
luaL_loadbuffer(l, socket_luac, socket_luac_sz, "@builtin socket.lua");
|
||||
lua_call(l, 0, 0);
|
||||
}
|
||||
#endif
|
@ -1,2 +0,0 @@
|
||||
#include "../LuaCompat.h"
|
||||
void luaopen_socket(lua_State *l);
|
@ -1,333 +0,0 @@
|
||||
/*=========================================================================*\
|
||||
* UDP object
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: udp.c,v 1.29 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "socket.h"
|
||||
#include "inet.h"
|
||||
#include "options.h"
|
||||
#include "udp.h"
|
||||
|
||||
/* min and max macros */
|
||||
#ifndef MIN
|
||||
#define MIN(x, y) ((x) < (y) ? x : y)
|
||||
#endif
|
||||
#ifndef MAX
|
||||
#define MAX(x, y) ((x) > (y) ? x : y)
|
||||
#endif
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal function prototypes
|
||||
\*=========================================================================*/
|
||||
static int global_create(lua_State *L);
|
||||
static int meth_send(lua_State *L);
|
||||
static int meth_sendto(lua_State *L);
|
||||
static int meth_receive(lua_State *L);
|
||||
static int meth_receivefrom(lua_State *L);
|
||||
static int meth_getsockname(lua_State *L);
|
||||
static int meth_getpeername(lua_State *L);
|
||||
static int meth_setsockname(lua_State *L);
|
||||
static int meth_setpeername(lua_State *L);
|
||||
static int meth_close(lua_State *L);
|
||||
static int meth_setoption(lua_State *L);
|
||||
static int meth_settimeout(lua_State *L);
|
||||
static int meth_getfd(lua_State *L);
|
||||
static int meth_setfd(lua_State *L);
|
||||
static int meth_dirty(lua_State *L);
|
||||
|
||||
/* udp object methods */
|
||||
static luaL_Reg udp[] = {
|
||||
{"__gc", meth_close},
|
||||
{"__tostring", auxiliar_tostring},
|
||||
{"close", meth_close},
|
||||
{"dirty", meth_dirty},
|
||||
{"getfd", meth_getfd},
|
||||
{"getpeername", meth_getpeername},
|
||||
{"getsockname", meth_getsockname},
|
||||
{"receive", meth_receive},
|
||||
{"receivefrom", meth_receivefrom},
|
||||
{"send", meth_send},
|
||||
{"sendto", meth_sendto},
|
||||
{"setfd", meth_setfd},
|
||||
{"setoption", meth_setoption},
|
||||
{"setpeername", meth_setpeername},
|
||||
{"setsockname", meth_setsockname},
|
||||
{"settimeout", meth_settimeout},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* socket options */
|
||||
static t_opt opt[] = {
|
||||
{"dontroute", opt_dontroute},
|
||||
{"broadcast", opt_broadcast},
|
||||
{"reuseaddr", opt_reuseaddr},
|
||||
{"ip-multicast-ttl", opt_ip_multicast_ttl},
|
||||
{"ip-multicast-loop", opt_ip_multicast_loop},
|
||||
{"ip-add-membership", opt_ip_add_membership},
|
||||
{"ip-drop-membership", opt_ip_drop_membersip},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* functions in library namespace */
|
||||
static luaL_Reg func[] = {
|
||||
{"udp", global_create},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int udp_open(lua_State *L)
|
||||
{
|
||||
/* create classes */
|
||||
auxiliar_newclass(L, "udp{connected}", udp);
|
||||
auxiliar_newclass(L, "udp{unconnected}", udp);
|
||||
/* create class groups */
|
||||
auxiliar_add2group(L, "udp{connected}", "udp{any}");
|
||||
auxiliar_add2group(L, "udp{unconnected}", "udp{any}");
|
||||
auxiliar_add2group(L, "udp{connected}", "select{able}");
|
||||
auxiliar_add2group(L, "udp{unconnected}", "select{able}");
|
||||
/* define library functions */
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Lua methods
|
||||
\*=========================================================================*/
|
||||
const char *udp_strerror(int err) {
|
||||
/* a 'closed' error on an unconnected means the target address was not
|
||||
* accepted by the transport layer */
|
||||
if (err == IO_CLOSED) return "refused";
|
||||
else return socket_strerror(err);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Send data through connected udp socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_send(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1);
|
||||
p_timeout tm = &udp->tm;
|
||||
size_t count, sent = 0;
|
||||
int err;
|
||||
const char *data = luaL_checklstring(L, 2, &count);
|
||||
timeout_markstart(tm);
|
||||
err = socket_send(&udp->sock, data, count, &sent, tm);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, sent);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Send data through unconnected udp socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_sendto(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
|
||||
size_t count, sent = 0;
|
||||
const char *data = luaL_checklstring(L, 2, &count);
|
||||
const char *ip = luaL_checkstring(L, 3);
|
||||
unsigned short port = (unsigned short) luaL_checknumber(L, 4);
|
||||
p_timeout tm = &udp->tm;
|
||||
struct sockaddr_in addr;
|
||||
int err;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
if (!inet_aton(ip, &addr.sin_addr))
|
||||
luaL_argerror(L, 3, "invalid ip address");
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
timeout_markstart(tm);
|
||||
err = socket_sendto(&udp->sock, data, count, &sent,
|
||||
(SA *) &addr, sizeof(addr), tm);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, sent);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Receives data from a UDP socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_receive(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
char buffer[UDP_DATAGRAMSIZE];
|
||||
size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer));
|
||||
int err;
|
||||
p_timeout tm = &udp->tm;
|
||||
count = MIN(count, sizeof(buffer));
|
||||
timeout_markstart(tm);
|
||||
err = socket_recv(&udp->sock, buffer, count, &got, tm);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushlstring(L, buffer, got);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Receives data and sender from a UDP socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_receivefrom(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
|
||||
struct sockaddr_in addr;
|
||||
socklen_t addr_len = sizeof(addr);
|
||||
char buffer[UDP_DATAGRAMSIZE];
|
||||
size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer));
|
||||
int err;
|
||||
p_timeout tm = &udp->tm;
|
||||
timeout_markstart(tm);
|
||||
count = MIN(count, sizeof(buffer));
|
||||
err = socket_recvfrom(&udp->sock, buffer, count, &got,
|
||||
(SA *) &addr, &addr_len, tm);
|
||||
if (err == IO_DONE) {
|
||||
lua_pushlstring(L, buffer, got);
|
||||
lua_pushstring(L, inet_ntoa(addr.sin_addr));
|
||||
lua_pushnumber(L, ntohs(addr.sin_port));
|
||||
return 3;
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, udp_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Select support methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_getfd(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
lua_pushnumber(L, (int) udp->sock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* this is very dangerous, but can be handy for those that are brave enough */
|
||||
static int meth_setfd(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
udp->sock = (t_socket) luaL_checknumber(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int meth_dirty(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
(void) udp;
|
||||
lua_pushboolean(L, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call inet methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_getpeername(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1);
|
||||
return inet_meth_getpeername(L, &udp->sock);
|
||||
}
|
||||
|
||||
static int meth_getsockname(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
return inet_meth_getsockname(L, &udp->sock);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call option handler
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_setoption(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
return opt_meth_setoption(L, opt, &udp->sock);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call tm methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_settimeout(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
return timeout_meth_settimeout(L, &udp->tm);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Turns a master udp object into a client object.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_setpeername(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
p_timeout tm = &udp->tm;
|
||||
const char *address = luaL_checkstring(L, 2);
|
||||
int connecting = strcmp(address, "*");
|
||||
unsigned short port = connecting ?
|
||||
(unsigned short) luaL_checknumber(L, 3) :
|
||||
(unsigned short) luaL_optnumber(L, 3, 0);
|
||||
const char *err = inet_tryconnect(&udp->sock, address, port, tm);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
/* change class to connected or unconnected depending on address */
|
||||
if (connecting) auxiliar_setclass(L, "udp{connected}", 1);
|
||||
else auxiliar_setclass(L, "udp{unconnected}", 1);
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Closes socket used by object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_close(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
|
||||
socket_destroy(&udp->sock);
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Turns a master object into a server object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_setsockname(lua_State *L) {
|
||||
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
|
||||
const char *address = luaL_checkstring(L, 2);
|
||||
unsigned short port = (unsigned short) luaL_checknumber(L, 3);
|
||||
const char *err = inet_trybind(&udp->sock, address, port);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Library functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Creates a master udp object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int global_create(lua_State *L) {
|
||||
t_socket sock;
|
||||
const char *err = inet_trycreate(&sock, SOCK_DGRAM);
|
||||
/* try to allocate a system socket */
|
||||
if (!err) {
|
||||
/* allocate tcp object */
|
||||
p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp));
|
||||
auxiliar_setclass(L, "udp{unconnected}", -1);
|
||||
/* initialize remaining structure fields */
|
||||
socket_setnonblocking(&sock);
|
||||
udp->sock = sock;
|
||||
timeout_init(&udp->tm, -1, -1);
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
}
|
@ -1,355 +0,0 @@
|
||||
#ifndef WIN
|
||||
/*=========================================================================*\
|
||||
* Unix domain socket
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id: unix.c,v 1.13 2006/03/13 07:16:39 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "socket.h"
|
||||
#include "options.h"
|
||||
#include "unix.h"
|
||||
#include <sys/un.h>
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal function prototypes
|
||||
\*=========================================================================*/
|
||||
static int global_create(lua_State *L);
|
||||
static int meth_connect(lua_State *L);
|
||||
static int meth_listen(lua_State *L);
|
||||
static int meth_bind(lua_State *L);
|
||||
static int meth_send(lua_State *L);
|
||||
static int meth_shutdown(lua_State *L);
|
||||
static int meth_receive(lua_State *L);
|
||||
static int meth_accept(lua_State *L);
|
||||
static int meth_close(lua_State *L);
|
||||
static int meth_setoption(lua_State *L);
|
||||
static int meth_settimeout(lua_State *L);
|
||||
static int meth_getfd(lua_State *L);
|
||||
static int meth_setfd(lua_State *L);
|
||||
static int meth_dirty(lua_State *L);
|
||||
static int meth_getstats(lua_State *L);
|
||||
static int meth_setstats(lua_State *L);
|
||||
|
||||
static const char *unix_tryconnect(p_unix un, const char *path);
|
||||
static const char *unix_trybind(p_unix un, const char *path);
|
||||
|
||||
/* unix object methods */
|
||||
static luaL_Reg un[] = {
|
||||
{"__gc", meth_close},
|
||||
{"__tostring", auxiliar_tostring},
|
||||
{"accept", meth_accept},
|
||||
{"bind", meth_bind},
|
||||
{"close", meth_close},
|
||||
{"connect", meth_connect},
|
||||
{"dirty", meth_dirty},
|
||||
{"getfd", meth_getfd},
|
||||
{"getstats", meth_getstats},
|
||||
{"setstats", meth_setstats},
|
||||
{"listen", meth_listen},
|
||||
{"receive", meth_receive},
|
||||
{"send", meth_send},
|
||||
{"setfd", meth_setfd},
|
||||
{"setoption", meth_setoption},
|
||||
{"setpeername", meth_connect},
|
||||
{"setsockname", meth_bind},
|
||||
{"settimeout", meth_settimeout},
|
||||
{"shutdown", meth_shutdown},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* socket option handlers */
|
||||
static t_opt opt[] = {
|
||||
{"keepalive", opt_keepalive},
|
||||
{"reuseaddr", opt_reuseaddr},
|
||||
{"linger", opt_linger},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* our socket creation function */
|
||||
static luaL_Reg func[] = {
|
||||
{"unix", global_create},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int luaopen_socket_unix(lua_State *L) {
|
||||
/* create classes */
|
||||
auxiliar_newclass(L, "unix{master}", un);
|
||||
auxiliar_newclass(L, "unix{client}", un);
|
||||
auxiliar_newclass(L, "unix{server}", un);
|
||||
/* create class groups */
|
||||
auxiliar_add2group(L, "unix{master}", "unix{any}");
|
||||
auxiliar_add2group(L, "unix{client}", "unix{any}");
|
||||
auxiliar_add2group(L, "unix{server}", "unix{any}");
|
||||
/* make sure the function ends up in the package table */
|
||||
luaL_openlib(L, "socket", func, 0);
|
||||
/* return the function instead of the 'socket' table */
|
||||
lua_pushstring(L, "unix");
|
||||
lua_gettable(L, -2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Lua methods
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call buffered IO methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_send(lua_State *L) {
|
||||
p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1);
|
||||
return buffer_meth_send(L, &un->buf);
|
||||
}
|
||||
|
||||
static int meth_receive(lua_State *L) {
|
||||
p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1);
|
||||
return buffer_meth_receive(L, &un->buf);
|
||||
}
|
||||
|
||||
static int meth_getstats(lua_State *L) {
|
||||
p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1);
|
||||
return buffer_meth_getstats(L, &un->buf);
|
||||
}
|
||||
|
||||
static int meth_setstats(lua_State *L) {
|
||||
p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1);
|
||||
return buffer_meth_setstats(L, &un->buf);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call option handler
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_setoption(lua_State *L) {
|
||||
p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
|
||||
return opt_meth_setoption(L, opt, &un->sock);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Select support methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_getfd(lua_State *L) {
|
||||
p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
|
||||
lua_pushnumber(L, (int) un->sock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* this is very dangerous, but can be handy for those that are brave enough */
|
||||
static int meth_setfd(lua_State *L) {
|
||||
p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
|
||||
un->sock = (t_socket) luaL_checknumber(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int meth_dirty(lua_State *L) {
|
||||
p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
|
||||
lua_pushboolean(L, !buffer_isempty(&un->buf));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Waits for and returns a client object attempting connection to the
|
||||
* server object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_accept(lua_State *L) {
|
||||
p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1);
|
||||
p_timeout tm = timeout_markstart(&server->tm);
|
||||
t_socket sock;
|
||||
int err = socket_accept(&server->sock, &sock, NULL, NULL, tm);
|
||||
/* if successful, push client socket */
|
||||
if (err == IO_DONE) {
|
||||
p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix));
|
||||
auxiliar_setclass(L, "unix{client}", -1);
|
||||
/* initialize structure fields */
|
||||
socket_setnonblocking(&sock);
|
||||
clnt->sock = sock;
|
||||
io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv,
|
||||
(p_error) socket_ioerror, &clnt->sock);
|
||||
timeout_init(&clnt->tm, -1, -1);
|
||||
buffer_init(&clnt->buf, &clnt->io, &clnt->tm);
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Binds an object to an address
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static const char *unix_trybind(p_unix un, const char *path) {
|
||||
struct sockaddr_un local;
|
||||
size_t len = strlen(path);
|
||||
int err;
|
||||
if (len >= sizeof(local.sun_path)) return "path too long";
|
||||
memset(&local, 0, sizeof(local));
|
||||
strcpy(local.sun_path, path);
|
||||
local.sun_family = AF_UNIX;
|
||||
#ifdef UNIX_HAS_SUN_LEN
|
||||
local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len)
|
||||
+ len + 1;
|
||||
err = socket_bind(&un->sock, (SA *) &local, local.sun_len);
|
||||
|
||||
#else
|
||||
err = socket_bind(&un->sock, (SA *) &local,
|
||||
sizeof(local.sun_family) + len);
|
||||
#endif
|
||||
if (err != IO_DONE) socket_destroy(&un->sock);
|
||||
return socket_strerror(err);
|
||||
}
|
||||
|
||||
static int meth_bind(lua_State *L) {
|
||||
p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1);
|
||||
const char *path = luaL_checkstring(L, 2);
|
||||
const char *err = unix_trybind(un, path);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Turns a master unix object into a client object.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static const char *unix_tryconnect(p_unix un, const char *path)
|
||||
{
|
||||
struct sockaddr_un remote;
|
||||
int err;
|
||||
size_t len = strlen(path);
|
||||
if (len >= sizeof(remote.sun_path)) return "path too long";
|
||||
memset(&remote, 0, sizeof(remote));
|
||||
strcpy(remote.sun_path, path);
|
||||
remote.sun_family = AF_UNIX;
|
||||
timeout_markstart(&un->tm);
|
||||
#ifdef UNIX_HAS_SUN_LEN
|
||||
remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len)
|
||||
+ len + 1;
|
||||
err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm);
|
||||
#else
|
||||
err = socket_connect(&un->sock, (SA *) &remote,
|
||||
sizeof(remote.sun_family) + len, &un->tm);
|
||||
#endif
|
||||
if (err != IO_DONE) socket_destroy(&un->sock);
|
||||
return socket_strerror(err);
|
||||
}
|
||||
|
||||
static int meth_connect(lua_State *L)
|
||||
{
|
||||
p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1);
|
||||
const char *path = luaL_checkstring(L, 2);
|
||||
const char *err = unix_tryconnect(un, path);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
/* turn master object into a client object */
|
||||
auxiliar_setclass(L, "unix{client}", 1);
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Closes socket used by object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_close(lua_State *L)
|
||||
{
|
||||
p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
|
||||
socket_destroy(&un->sock);
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Puts the sockt in listen mode
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_listen(lua_State *L)
|
||||
{
|
||||
p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1);
|
||||
int backlog = (int) luaL_optnumber(L, 2, 32);
|
||||
int err = socket_listen(&un->sock, backlog);
|
||||
if (err != IO_DONE) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
/* turn master object into a server object */
|
||||
auxiliar_setclass(L, "unix{server}", 1);
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Shuts the connection down partially
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_shutdown(lua_State *L)
|
||||
{
|
||||
p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1);
|
||||
const char *how = luaL_optstring(L, 2, "both");
|
||||
switch (how[0]) {
|
||||
case 'b':
|
||||
if (strcmp(how, "both")) goto error;
|
||||
socket_shutdown(&un->sock, 2);
|
||||
break;
|
||||
case 's':
|
||||
if (strcmp(how, "send")) goto error;
|
||||
socket_shutdown(&un->sock, 1);
|
||||
break;
|
||||
case 'r':
|
||||
if (strcmp(how, "receive")) goto error;
|
||||
socket_shutdown(&un->sock, 0);
|
||||
break;
|
||||
}
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
error:
|
||||
luaL_argerror(L, 2, "invalid shutdown method");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call tm methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_settimeout(lua_State *L) {
|
||||
p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
|
||||
return timeout_meth_settimeout(L, &un->tm);
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Library functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Creates a master unix object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int global_create(lua_State *L) {
|
||||
t_socket sock;
|
||||
int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0);
|
||||
/* try to allocate a system socket */
|
||||
if (err == IO_DONE) {
|
||||
/* allocate unix object */
|
||||
p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix));
|
||||
/* set its type as master object */
|
||||
auxiliar_setclass(L, "unix{master}", -1);
|
||||
/* initialize remaining structure fields */
|
||||
socket_setnonblocking(&sock);
|
||||
un->sock = sock;
|
||||
io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv,
|
||||
(p_error) socket_ioerror, &un->sock);
|
||||
timeout_init(&un->tm, -1, -1);
|
||||
buffer_init(&un->buf, &un->io, &un->tm);
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
#endif
|
@ -1,30 +0,0 @@
|
||||
#ifndef WIN
|
||||
#ifndef UNIX_H
|
||||
#define UNIX_H
|
||||
/*=========================================================================*\
|
||||
* Unix domain object
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* This module is just an example of how to extend LuaSocket with a new
|
||||
* domain.
|
||||
*
|
||||
* RCS ID: $Id: unix.h,v 1.9 2006/03/13 07:16:39 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "../LuaCompat.h"
|
||||
|
||||
#include "buffer.h"
|
||||
#include "timeout.h"
|
||||
#include "socket.h"
|
||||
|
||||
typedef struct t_unix_ {
|
||||
t_socket sock;
|
||||
t_io io;
|
||||
t_buffer buf;
|
||||
t_timeout tm;
|
||||
} t_unix;
|
||||
typedef t_unix *p_unix;
|
||||
|
||||
int luaopen_socket_unix(lua_State *L);
|
||||
|
||||
#endif /* UNIX_H */
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user