forked from xHasKx/luamqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluasocket_ssl.lua
56 lines (45 loc) · 1.42 KB
/
luasocket_ssl.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- DOC: http://w3.impa.br/~diego/software/luasocket/tcp.html
-- module table
local luasocket_ssl = {}
local type = type
local assert = assert
local luasocket = require("mqtt.luasocket")
-- Open network connection to .host and .port in conn table
-- Store opened socket to conn table
-- Returns true on success, or false and error text on failure
function luasocket_ssl.connect(conn)
assert(type(conn.secure_params) == "table", "expecting .secure_params to be a table")
-- open usual TCP connection
local ok, err = luasocket.connect(conn)
if not ok then
return false, "luasocket connect failed: "..err
end
local wrapped
-- load right ssl module
local ssl = require(conn.ssl_module or "ssl")
-- TLS/SSL initialization
wrapped, err = ssl.wrap(conn.sock, conn.secure_params)
if not wrapped then
conn.sock:shutdown()
return false, "ssl.wrap() failed: "..err
end
ok = wrapped:dohandshake()
if not ok then
conn.sock:shutdown()
return false, "ssl dohandshake failed"
end
-- replace sock in connection table with wrapped secure socket
conn.sock = wrapped
return true
end
-- Shutdown network connection
function luasocket_ssl.shutdown(conn)
conn.sock:close()
end
-- Copy original methods from mqtt.luasocket module
luasocket_ssl.send = luasocket.send
luasocket_ssl.receive = luasocket.receive
luasocket_ssl.settimeout = luasocket.settimeout
-- export module table
return luasocket_ssl
-- vim: ts=4 sts=4 sw=4 noet ft=lua