From e8ecaaef80fd6351a3eee11fd0be694d643cc692 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 30 May 2010 20:56:13 +0000 Subject: added debug shell (not finished yet) remove echo server --- src/Makefile | 2 +- src/debug_shell.lua | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/echo_server.lua | 78 ----------------------------------- src/main_loop.lua | 17 ++++---- 4 files changed, 124 insertions(+), 88 deletions(-) create mode 100644 src/debug_shell.lua delete mode 100644 src/echo_server.lua diff --git a/src/Makefile b/src/Makefile index cbd0900..93386ca 100644 --- a/src/Makefile +++ b/src/Makefile @@ -49,7 +49,7 @@ C_OBJS := log.o \ C_SRCS := $(C_OBJS:%.o=%.c) LUA_SRCS := main_loop.lua \ - echo_server.lua + debug_shell.lua LUA_BYTECODE := $(EXECUTABLE).lc LUA_BYTECODE_OBJ := $(EXECUTABLE)_lua_bytecode.o 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 +-- Christian Pointner +-- +-- 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 . +-- + +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 diff --git a/src/echo_server.lua b/src/echo_server.lua deleted file mode 100644 index 6770421..0000000 --- a/src/echo_server.lua +++ /dev/null @@ -1,78 +0,0 @@ --- --- 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 --- Christian Pointner --- --- 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 . --- - -socket = require("socket") - -echo_server = {} - -echo_server.init = function(host, port) - local ip, err = socket.dns.toip(host) - if(ip == nil) then - log.printf(log.ERROR, "can't resolve %s: %s", host, err) - return nil - end - - local udp, err = socket.udp() - if(udp == nil) then - log.printf(log.ERROR, "can't create udp socket") - return nil - end - - local ret, err = udp:setsockname(ip, port) - if(ret == nil) then - log.printf(log.ERROR, "setsockname(%s,%s) failed: %s", ip, port, err) - return nil - end - - log.printf(log.NOTICE, "echo server listening on %s:%s", ip, port); - - return udp -end - -echo_server.handle = function(udp) - local dgrm, from_ip, from_port = udp:receivefrom() - if(dgrm == nil) then - log.printf(log.ERROR, "receivefrom(%s,%s) failed: %s", ip, port, from_ip) - return -1 - end - - local ret, err = udp:sendto(dgrm, from_ip, from_port) - if(ret == nil) then - log.printf(log.ERROR, "sendto(%s,%s) failed: %s", ip, port, err) - return -1 - end - - if(string.gsub(dgrm, "^(%w+)%s*%c$", "%1") == "quit") then - return 2 - end - - return 0 -end diff --git a/src/main_loop.lua b/src/main_loop.lua index f7f09e9..cb74170 100644 --- a/src/main_loop.lua +++ b/src/main_loop.lua @@ -35,16 +35,12 @@ function main_loop(opt) log.printf(log.NOTICE, "main_loop started") local sig = signal.init() - local sock = {} - sock[1] = echo_server.init("localhost", 9000) - if(sock[1] == nil) then return -1 end - - sock[2] = echo_server.init("localhost", 10000) - if(sock[2] == nil) then return -1 end + local ret = debug_shell.init("localhost", 9000) + if(ret == nil) then return -1 end local return_value = 0 while return_value == 0 do - local readable, _, err = socket.select({ sig , sock[1], sock[2] }, nil) + local readable, _, err = socket.select({ sig , unpack(debug_shell.socks) } , nil) if(err) then log.printf(log.ERROR, "select returned with error: %s", err) return_value = -1 @@ -53,9 +49,11 @@ function main_loop(opt) if(input == sig) then return_value = signal.handle() if(return_value == 1) then break end - else - return_value = echo_server.handle(input) + elseif(input == debug_shell.socks[1] or input == debug_shell.socks[2]) then + return_value = debug_shell.handle(input) if(return_value == 2) then break end + else + log.printf(log.ERROR, "select returned unknown file descriptor!?") end end end @@ -63,6 +61,7 @@ function main_loop(opt) if(return_value == 2) then return_value = 0 end + debug_shell.close(); signal.stop() return return_value end -- cgit v1.2.3