lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great greatwolf once stated:
> Diego Nehab-3 wrote
> > I don't remember seeing a full patch, but I blieve it would
> > be easy to create a module in the flavour of the unix domain
> > socket support that ships with LuaSocket as a sample.
> 
> It looks like raw socket support has been requested a few times around the
> internet already. But checking the repo  here
> <https://github.com/diegonehab/luasocket/tree/unstable>   shows no
> indication that this feature has ever been incorporated.
> 
> Is there any hope of adding this in soon so we don't have to hack together
> our own ad-hoc solution to this?

  Hmmm ... I know my own socket module [1] supports IPv4, IPv6 and Unix
sockets.  I don't have code for HTTP, FTP or what have you, but I wanted a
decent low level implementation of sockets under Lua.  The :read() and
:write() functions on sockets don't work the same as they do for files
though (they should have been named :recvfrom() and :sendto()).

  I also have support for select, which uses epoll() (if on Linux), poll()
(for the rest of the Unix universe) or select() (if you *really really*
want it) with the same API (the implementation is picked at compile time).  

  Here's a sample daytime server (I use this at work [2][3]) to get a feel
for how this works:

	syslog = require "org.conman.syslog"
	errno  = require "org.conman.errno"
	net    = require "org.conman.net"

	syslog.open('daytime','local1')

	local addr = net.address('0.0.0.0',40960,'udp')
	local sock = net.socket(addr.family,'udp')
	sock.reuseaddr = true
	sock:bind(addr)

	local function daytime()
	  local remaddr,data,err = sock:read()
	  if err ~= 0 then
	    syslog('err',"sock:read() = %s",errno[err])
	    return daytime()
	  end
	  syslog('notice',"request=%s",tostring(remaddr))
	  sock:write(remaddr,tostring(os.time())
	  return daytime()
	end

  Change the address, and this can run over IPv6:

	local addr = net.address('fc00::dead:beef',40960,'udp')

or even a Unix socket:

	local addr = net.address('/tmp/daytime','udp')

without chaning the rest of the code (it just all works).  The client is
just as simple:

	syslog = require "org.conman.syslog"
	net    = require "org.conman.net"

	local server = net.address('192.168.1.10',40960,'udp')
	local socket = net.socket(server.family,'udp')
	
	socket:write(server,'just-to-trigger-a-response')
	local remaddr,data,err = socket:read(5.3) -- timeout in 5.3 seconds
	
	if err ~= 0 then
	  syslog('err',"%s not responding, abort!",tostring(server))
	  return false
	end
	
	return true,tonumber(data)

  -spc (If there's interest, I can provide an example that uses select ... )

[1]	https://github.com/spc476/lua-conmanorg/blob/master/src/net.c

[2]	My job is to test the backend server components, which require the
	servers to be in sync, time wise.  I have this running on each of
	the test servers so when the test code runs, it can check the time
	of the servers to ensure they're in-sync.

[3]	"org.conman.syslog" is my syslog interface, and "org.conman.errno"
	is pretty much a wrapper for strerror().

> 
> 
> --
> View this message in context: http://lua.2524044.n2.nabble.com/Raw-socket-patches-for-LuaSocket-tp7628258p7649717.html
> Sent from the Lua-l mailing list archive at Nabble.com.