diff options
Diffstat (limited to 'src/debug_shell.lua')
-rw-r--r-- | src/debug_shell.lua | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/debug_shell.lua b/src/debug_shell.lua new file mode 100644 index 0000000..2589552 --- /dev/null +++ b/src/debug_shell.lua @@ -0,0 +1,115 @@ +-- +-- anylike +-- +-- anylike is an IKEv2 Implementation written in Lua and C. It's main +-- design goal is to provide anytun and uanytun or any other SATP +-- implementation with a key exchange mechanism but it should also be +-- possible to use anylike as key exchange daemon for IPSec security +-- associations. The use of Lua guarantees that anylike is easily +-- portable to many platforms including very small ones like wireless +-- routers. +-- +-- +-- Copyright (C) 2007-2008 Markus Grueneis <gimpf@anylike.org> +-- Christian Pointner <equinox@anylike.org> +-- +-- This file is part of anylike. +-- +-- anylike is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- any later version. +-- +-- anylike is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with anylike. If not, see <http://www.gnu.org/licenses/>. +-- + +socket = require("socket") + +debug_shell = {} + +debug_shell.init = function(host, port) + local ip, err = socket.dns.toip(host) + if(ip == nil) then + log.printf(log.ERROR, "debug shell: can't resolve %s: %s", host, err) + return nil + end + + debug_shell.socks = {}; + debug_shell.socks[1], err = socket.tcp() + if(debug_shell.socks[1] == nil) then + log.printf(log.ERROR, "debug shell: can't create tcp socket") + return nil + end + + debug_shell.socks[1]:setoption('reuseaddr', true); + + local ret, err = debug_shell.socks[1]:bind(ip, port) + if(ret == nil) then + log.printf(log.ERROR, "debug shell: bind(%s,%s) failed: %s", ip, port, err) + return nil + end + + local ret, err = debug_shell.socks[1]:listen() + if(ret == nil) then + log.printf(log.ERROR, "debug shell: listen() failed: %s", ip, port, err) + return nil + end + + debug_shell.socks[1]:settimeout(0); + debug_shell.buffer = ""; + + log.printf(log.NOTICE, "debug shell: listening on %s:%s", ip, port); + + return debug_shell.socks[1] +end + +debug_shell.close = function() + debug_shell.socks[1]:close() + if(debug_shell.socks[2]) then debug_shell.socks[2]:close() end +end + +debug_shell.handle = function(sock) + if(sock == debug_shell.socks[1]) then + local newclient, err = debug_shell.socks[1]:accept() + if(newclient == nil) then + log.printf(log.ERROR, "debug shell: accept() failed: %s", newclient, err) + end + local ip, port = newclient:getpeername(); + if(debug_shell.socks[2]) then + log.printf(log.INFO, "debug shell: refusing connection from %s:%s, already connected", ip, port) + newclient:close(); + else + log.printf(log.INFO, "debug shell: connection from %s:%s accepted", ip, port) + debug_shell.socks[2] = newclient + debug_shell.socks[2]:settimeout(0); + debug_shell.socks[2]:setoption('tcp-nodelay', true); + end + else + local data, err, partial = debug_shell.socks[2]:receive('*l') + if(data == nil) then + if(err == 'closed') then + log.printf(log.INFO, "debug shell: connection closed by peer", ip, port) + debug_shell.socks[2]:close() + debug_shell.socks[2] = nil + elseif(err == 'timeout') then + debug_shell.buffer = debug_shell.buffer .. partial + else + log.printf(log.INFO, "debug shell: connection error: %s", err) + debug_shell.socks[2]:close() + debug_shell.socks[2] = nil + end + else + debug_shell.buffer = debug_shell.buffer .. data + log.printf(log.DEBUG, "debug shell: received string: '%s'", debug_shell.buffer) + debug_shell.buffer = "" + end + end + + return 0 +end |