Lua Socket: Difference between revisions

From Spring
Jump to navigationJump to search
Abma (talk | contribs)
Flozi (talk | contribs)
→config: 98.0 - allow tcp connections by luasocket as default (udp is still restricted)
 
(16 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==config settings==
LuaSockets allow a widget to use tcp/udp sockets. With luasockets you can create a client or a server connecting to a server and transfering some data.
 
There are many possibilties what could be done with luasockets in spring, some ideas:
 
* webserver that shows all files in VFS
* telnet console, that allows control of spring
* statistics webpage
* ingame widget updater
* ingame news pages
* ...
 
==config==


All LuaSocket related config is readonly for LuaWidgets. They can be only changed through unitsync.
All LuaSocket related config is readonly for LuaWidgets. They can be only changed through unitsync.


As default, socket access is restricted, as default no connections are allowed. To allow a connection / sending a packet, the destination has to the corresponding allow rule. For example, if a lua widget wants to connect to springrts.com at port 80,
<code><pre>TCPAllowConnect springrts.com:80</pre></code>
has to be set.
Since spring 97.0 lobby.springrts.com:8200 is allowed as default.
Since Spring 98.0 all tcp connections are allowed by default (udp is still restricted).
Creating a server socket needs no config value to be set.
===possible config values===


{|
{|
|-
|-
|LuaSocketEnabled = 1
|LuaSocketEnabled = 1
|Default is 0 means, all Socket functions are disabled
|Default is 1 means, all Socket functions are enabled, 0 disables access to sockets.
|-
|-
|TCPAllowConnect
|TCPAllowConnect
|allow TCP-connections to the listed hosts, for example: "springrts.com:80 springfiles.com:80" which allows luasockets to connect to springrts.com at port 80 and springfiles.com at port 80.
|allow TCP-connections to the listed hosts, for example: "springrts.com:80 springfiles.com:80" which allows luasockets to connect to springrts.com at port 80 and springfiles.com at port 80.
|-
|TCPAllowListen
|Server TCP-sockets are allowed on this port, for example: "localhost:1234 0.0.0.0:12345"
|-
|-
|UDPAllowConnect
|UDPAllowConnect
|see TCPAllowConnect
|see TCPAllowConnect
|-
|UDPAllowConnect
|see TCPAllowListen
|-
|-
|}
|}
Line 30: Line 47:
==Notes / known issues / TODO ==
==Notes / known issues / TODO ==


* only ipv4 is supported (could be solved by using unstable luasocket2: http://code.matthewwild.co.uk/luasocket2-hg/)
* only ipv4 is supported (could be solved by using unstable luasocket2: http://code.matthewwild.co.uk/luasocket2-hg/) or https://github.com/diegonehab/luasocket (seems to be the more official)
* dns resolving is blocking, solutions: worker thread or some lib:
* dns resolving is blocking, solutions: worker thread or some lib:
** adns http://www.chiark.greenend.org.uk/~ian/adns/
** adns http://www.chiark.greenend.org.uk/~ian/adns/
** dns.c http://25thandclement.com/~william/projects/dns.c.html
** dns.c http://25thandclement.com/~william/projects/dns.c.html
** tadns http://adns.sourceforge.net/
** tadns http://adns.sourceforge.net/
** or a lua implementation? biggest problem would be to find the local dns-server
** or a lua implementation? biggest problem would be to [http://stackoverflow.com/questions/2916675/programmatically-obtain-dns-servers-of-host find the local dns-server]
* most lua scripts from luasocket won't work as they are blocking! rewriting them non-blocking is required! (should be in most cases few work, as parsing stuff can be reused)
* most lua scripts from luasocket won't work as they are blocking! rewriting them non-blocking is required! (should be in most cases few work, as parsing stuff can be reused)
* config file should be better protected, see https://github.com/spring/spring/blob/develop/rts/System/Config/ConfigHandler.cpp#L289 + https://github.com/spring/spring/commit/948d6fb9d1c95b99b5cc7929cf3e0de0f9d6d93e maybe just locking the file is best?! see also http://springrts.com/mantis/view.php?id=2891 (private report)
* config file should be better protected, see https://github.com/spring/spring/blob/develop/rts/System/Config/ConfigHandler.cpp#L289 + https://github.com/spring/spring/commit/948d6fb9d1c95b99b5cc7929cf3e0de0f9d6d93e maybe just locking the file is best?! see also http://springrts.com/mantis/view.php?id=2891 (private report)
* access to autohost interface / game host is denied
* access to autohost interface / game host is denied
* If you get this error:
<code><pre>
Error in Initialize(): [string "LuaUI/Widgets/luasocket.lua"]:65:
attempt to index upvalue 'socket' (a nil value)</pre></code>
you maybe need to add to system.lua: socket = socket,


==documentation==
==documentation==
Line 47: Line 69:


==links==
==links==
[http://springrts.com/dl/buildbot/default/luasocket/ compiled Spring test version]
{{forumlink|topic=27526|title=lua sockets in spring}} - original thread
{{forumlink|topic=27526|title=forum thread}}
{{forumlink|topic=28071|title=Lua Lobby / Socket}} - some code pieces for connecting to spring lobby server & protocol


[[Category: Development]]
[[Category: Lua]]
[[Category: Lua]]

Latest revision as of 17:50, 11 October 2014

LuaSockets allow a widget to use tcp/udp sockets. With luasockets you can create a client or a server connecting to a server and transfering some data.

There are many possibilties what could be done with luasockets in spring, some ideas:

  • webserver that shows all files in VFS
  • telnet console, that allows control of spring
  • statistics webpage
  • ingame widget updater
  • ingame news pages
  • ...

config

All LuaSocket related config is readonly for LuaWidgets. They can be only changed through unitsync.

As default, socket access is restricted, as default no connections are allowed. To allow a connection / sending a packet, the destination has to the corresponding allow rule. For example, if a lua widget wants to connect to springrts.com at port 80,

TCPAllowConnect springrts.com:80

has to be set.

Since spring 97.0 lobby.springrts.com:8200 is allowed as default.

Since Spring 98.0 all tcp connections are allowed by default (udp is still restricted).

Creating a server socket needs no config value to be set.


possible config values

LuaSocketEnabled = 1 Default is 1 means, all Socket functions are enabled, 0 disables access to sockets.
TCPAllowConnect allow TCP-connections to the listed hosts, for example: "springrts.com:80 springfiles.com:80" which allows luasockets to connect to springrts.com at port 80 and springfiles.com at port 80.
UDPAllowConnect see TCPAllowConnect

example code

Notes / known issues / TODO

Error in Initialize(): [string "LuaUI/Widgets/luasocket.lua"]:65:
attempt to index upvalue 'socket' (a nil value)

you maybe need to add to system.lua: socket = socket,

documentation

links

lua sockets in spring - original thread Lua Lobby / Socket - some code pieces for connecting to spring lobby server & protocol