-->
Page 1 of 2

SSL failure to connect

PostPosted: Wed Apr 15, 2015 12:44 pm
by netcrusher88
Using the following code:

Code: Select alllocal api_host = 'api.thingspeak.com'
local api_path = '/update'

local get_template = 'GET '..api_path..'?api_key='..api_key

local function update_factory(getstring)
    return function (s)
        if debug then print('Connected!') end
        if debug then print('Sending: '..getstring) end
        s:send(getstring..'\r\n\r\n')
    end
end

local function sendUpdate(getstring)
    local s = net.createConnection(net.TCP, 1)
    if debug then print("socket created...") end
    s:connect(443,api_host)
    if debug then print ("connect requested...") end
    s:on("connection", update_factory(getstring) )
    s:on("receive", function (s, r) if debug then print("Return from API: "..r) s:close() end end)
end

function M.update(data)
    local getstring = get_template
    for i,v in ipairs(data) do
        if i < 9 then
            if debug then print('Field '..i..':'..v) end
            getstring = getstring.."&field"..i.."="..v
        end
    end
    if debug then print('Preparing connection') end
    sendUpdate(getstring)
end


I set debug to true in the global scope, and It gets as far as "connect requested..." but never actually seems to connect. Not in a position where I can get a packet capture right now, any thoughts?

One thing I thought is maybe s falls out of scope and gets GC'd after sendUpdate ends?

Re: SSL failure to connect

PostPosted: Fri Apr 17, 2015 10:51 am
by TerryE
The function bound to the sk:on("connect",func) takes one parameter and that is the socket on which the connection occurs. So the socket will be in scope.

Using upvalues in event action scripts is problematic, and update_factory(getstring) is using one to be bound into the returned function. Dodgy, IMO. Why not just make getstring an old fashioned global and set it to nil when done? Dump the factory and just declare the on routine inline in the s:on() call.

Being clever when you've only got ~20K application RAM to play with tends to get you burnt.

Re: SSL failure to connect

PostPosted: Fri Apr 17, 2015 6:04 pm
by abl
SSL only works (in most cases) with very specific builds of the core SDK; specifically, 0.9.3-patch1 with espressif's libssl from a forum attachment, and supposedly 1.0.1b2 with, again, a different libssl from a different forum attachment. :)

Without those two builds, you're probably only going to be able to connect to very basic SSL servers with small certs - as in 1024-bit self-signed chainless certs.

Re: SSL failure to connect

PostPosted: Sat Apr 18, 2015 5:25 am
by TerryE
Thanks. Good to know.