mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-06 05:21:22 +03:00
improvements to Lua sample
git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@109 9a5d90b5-6617-0410-8a86-bb477d3ed2e3
This commit is contained in:
parent
c8cb14d9de
commit
9d7c8e79dc
@ -248,7 +248,6 @@ function do_server(build_mode)
|
|||||||
|
|
||||||
-- Create socket for incoming connections
|
-- Create socket for incoming connections
|
||||||
local server_sock = socket.try(socket.bind("*", port))
|
local server_sock = socket.try(socket.bind("*", port))
|
||||||
local connected = false
|
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
if not quiet then print("ACCEPT") end
|
if not quiet then print("ACCEPT") end
|
||||||
@ -256,16 +255,15 @@ function do_server(build_mode)
|
|||||||
local ssl = axtlsl.ssl_server_new(ssl_ctx, client_sock:getfd())
|
local ssl = axtlsl.ssl_server_new(ssl_ctx, client_sock:getfd())
|
||||||
|
|
||||||
-- do the actual SSL handshake
|
-- do the actual SSL handshake
|
||||||
|
local connected = false
|
||||||
local res
|
local res
|
||||||
local buf
|
local buf
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
|
|
||||||
socket.select({client_sock}, nil)
|
socket.select({client_sock}, nil)
|
||||||
res, buf = axtlsl.ssl_read(ssl)
|
res, buf = axtlsl.ssl_read(ssl)
|
||||||
|
|
||||||
if res == axtlsl.SSL_OK then -- connection established and ok
|
if res == axtlsl.SSL_OK then -- connection established and ok
|
||||||
-- check when the connection has been established
|
|
||||||
if axtlsl.ssl_handshake_status(ssl) == axtlsl.SSL_OK then
|
if axtlsl.ssl_handshake_status(ssl) == axtlsl.SSL_OK then
|
||||||
if not quiet and not connected then
|
if not quiet and not connected then
|
||||||
display_session_id(ssl)
|
display_session_id(ssl)
|
||||||
@ -282,14 +280,13 @@ function do_server(build_mode)
|
|||||||
elseif res < axtlsl.SSL_OK then
|
elseif res < axtlsl.SSL_OK then
|
||||||
if not quiet then
|
if not quiet then
|
||||||
axtlsl.ssl_display_error(res)
|
axtlsl.ssl_display_error(res)
|
||||||
print("CONNECTION CLOSED")
|
|
||||||
end
|
end
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- client was disconnected or the handshake failed.
|
-- client was disconnected or the handshake failed.
|
||||||
|
print("CONNECTION CLOSED")
|
||||||
axtlsl.ssl_free(ssl)
|
axtlsl.ssl_free(ssl)
|
||||||
client_sock:close()
|
client_sock:close()
|
||||||
end
|
end
|
||||||
@ -304,7 +301,8 @@ function do_client(build_mode)
|
|||||||
local i = 2
|
local i = 2
|
||||||
local v
|
local v
|
||||||
local port = 4433
|
local port = 4433
|
||||||
local options = bit.bor(axtlsl.SSL_SERVER_VERIFY_LATER, SSL_DISPLAY_CERTS)
|
local options =
|
||||||
|
bit.bor(axtlsl.SSL_SERVER_VERIFY_LATER, axtlsl.SSL_DISPLAY_CERTS)
|
||||||
local private_key_file = nil
|
local private_key_file = nil
|
||||||
local reconnect = 0
|
local reconnect = 0
|
||||||
local quiet = false
|
local quiet = false
|
||||||
@ -324,10 +322,11 @@ function do_client(build_mode)
|
|||||||
end
|
end
|
||||||
|
|
||||||
i = i + 1
|
i = i + 1
|
||||||
-- TODO
|
local t = string.find(arg[i], ":")
|
||||||
--(host, port) = split(':', arg[i])
|
host = string.sub(arg[i], 1, t-1)
|
||||||
|
port = string.sub(arg[i], t+1)
|
||||||
elseif arg[i] == "-cert" then
|
elseif arg[i] == "-cert" then
|
||||||
if i >= #arg >= cert_size-1 then
|
if i >= #arg or #cert >= cert_size then
|
||||||
print_client_options(build_mode, arg[i])
|
print_client_options(build_mode, arg[i])
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -342,7 +341,7 @@ function do_client(build_mode)
|
|||||||
private_key_file = arg[i]
|
private_key_file = arg[i]
|
||||||
options = bit.bor(options, axtlsl.SSL_NO_DEFAULT_KEY)
|
options = bit.bor(options, axtlsl.SSL_NO_DEFAULT_KEY)
|
||||||
elseif arg[i] == "-CAfile" then
|
elseif arg[i] == "-CAfile" then
|
||||||
if i >= #arg >= ca_cert_size-1 then
|
if i >= #arg or #ca_cert >= ca_cert_size then
|
||||||
print_client_options(build_mode, arg[i])
|
print_client_options(build_mode, arg[i])
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -380,16 +379,6 @@ function do_client(build_mode)
|
|||||||
i = i + 1
|
i = i + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local client_sock = assert(socket.connect(host, port))
|
|
||||||
local ssl
|
|
||||||
local res
|
|
||||||
|
|
||||||
if not quiet then print("CONNECTED") end
|
|
||||||
|
|
||||||
---------------------------------------------------------------------------
|
|
||||||
-- This is where the interesting stuff happens. Up until now we've
|
|
||||||
-- just been setting up sockets etc. Now we do the SSL handshake.
|
|
||||||
---------------------------------------------------------------------------
|
|
||||||
local ssl_ctx = axtlsl.ssl_ctx_new(options, axtlsl.SSL_DEFAULT_CLNT_SESS)
|
local ssl_ctx = axtlsl.ssl_ctx_new(options, axtlsl.SSL_DEFAULT_CLNT_SESS)
|
||||||
|
|
||||||
if ssl_ctx == nil then
|
if ssl_ctx == nil then
|
||||||
@ -427,12 +416,23 @@ function do_client(build_mode)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
-- This is where the interesting stuff happens. Up until now we've
|
||||||
|
-- just been setting up sockets etc. Now we do the SSL handshake.
|
||||||
|
---------------------------------------------------------------------------
|
||||||
|
local client_sock = assert(socket.connect(host, port))
|
||||||
|
local ssl
|
||||||
|
local res
|
||||||
|
|
||||||
|
if not quiet then print("CONNECTED") end
|
||||||
|
|
||||||
-- Try session resumption?
|
-- Try session resumption?
|
||||||
if reconnect then
|
if reconnect > 0 then
|
||||||
local session_id = nil
|
local session_id = nil
|
||||||
while reconnect do
|
while reconnect do
|
||||||
reconnect = reconnect - 1
|
reconnect = reconnect - 1
|
||||||
ssl = axtlsl.ssl_client_new(ssl_ctx, client_sock:getfd(), session_id)
|
ssl = axtlsl.ssl_client_new(ssl_ctx,
|
||||||
|
client_sock:getfd(), session_id)
|
||||||
|
|
||||||
res = ssl_handshake_status(ssl)
|
res = ssl_handshake_status(ssl)
|
||||||
if res ~= axtlsl.SSL_OK then
|
if res ~= axtlsl.SSL_OK then
|
||||||
@ -456,6 +456,7 @@ function do_client(build_mode)
|
|||||||
|
|
||||||
-- check the return status
|
-- check the return status
|
||||||
res = axtlsl.ssl_handshake_status(ssl)
|
res = axtlsl.ssl_handshake_status(ssl)
|
||||||
|
print("RES: "..res)
|
||||||
if res ~= axtlsl.SSL_OK then
|
if res ~= axtlsl.SSL_OK then
|
||||||
if not quiet then axtlsl.ssl_display_error(res) end
|
if not quiet then axtlsl.ssl_display_error(res) end
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
@ -473,10 +474,10 @@ function do_client(build_mode)
|
|||||||
display_cipher(ssl)
|
display_cipher(ssl)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- while <STDIN> do
|
|
||||||
while true do
|
while true do
|
||||||
local cstring = pack("a*x", _) -- add null terminator
|
local x = { 65, 66, 67, 10, 0 }
|
||||||
res = axtlsl.ssl_write(ssl, cstring, length(cstring))
|
local line = io.read()
|
||||||
|
res = axtlsl.ssl_write(ssl, x, #x)
|
||||||
if res < axtlsl.SSL_OK then
|
if res < axtlsl.SSL_OK then
|
||||||
if not quiet then axtlsl.ssl_display_error(res) end
|
if not quiet then axtlsl.ssl_display_error(res) end
|
||||||
break
|
break
|
||||||
|
Loading…
x
Reference in New Issue
Block a user