Skip to content

Commit

Permalink
added opc security interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mascarenhas committed Mar 23, 2012
1 parent ea50993 commit 73ec168
Show file tree
Hide file tree
Showing 14 changed files with 4,439 additions and 5 deletions.
11 changes: 10 additions & 1 deletion Makefile.win
Expand Up @@ -45,6 +45,15 @@ opcae_w.obj: opcae_w.cpp
opcae.dll: opcae.obj opcae_w.obj
link /dll opcae.obj opcae_w.obj $(LUA_LIB) $(OLE_LIB) $(OLEAUT_LIB) /def:opcae.def

opcsec.cpp: gen_opc.lua
$(LUA) -l luarocks.require gen_opc.lua

opcsec.obj: opcsec.cpp
cl /c /W3 /D_CRT_SECURE_NO_WARNINGS /I$(LUA_INCLUDE_DIR) /Iopc opcsec.cpp

opcsec.dll: opcsec.obj
link /dll opcsec.obj $(LUA_LIB) $(OLE_LIB) $(OLEAUT_LIB) /def:opcsec.def

connpoint.cpp: gen_connpoint.lua
$(LUA) -l luarocks.require gen_connpoint.lua

Expand All @@ -54,7 +63,7 @@ connpoint.obj: connpoint.cpp
connpoint.dll: connpoint.obj
link /dll connpoint.obj $(LUA_LIB) $(OLE_LIB) $(OLEAUT_LIB) /def:connpoint.def

all: comgen.dll rectpointlib.dll opclib.dll connpoint.dll opcae.dll
all: comgen.dll rectpointlib.dll opclib.dll connpoint.dll opcae.dll opcsec.dll

clean:
del *.dll *.obj *.lib *.exp rectpointlib.* opclib.*
11 changes: 11 additions & 0 deletions comgen.cpp
Expand Up @@ -223,10 +223,21 @@ int comgen_messagestep(lua_State *L) {
return 1;
}

int comgen_raiseprivacy(lua_State *L) {
IUnknown *p = comgen_checkinterface(L, 1);
HRESULT hr = CoSetProxyBlanket(p, RPC_C_AUTHN_DEFAULT, RPC_C_AUTHN_DEFAULT, NULL,
RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_DEFAULT,
NULL, EOAC_DEFAULT);
if(!SUCCEEDED(hr))
return comgen_error(L, hr);
return 1;
}

static luaL_Reg comgen_functions[] = {
{ "CreateInstance", comgen_createinstance },
{ "MessageLoop", comgen_messageloop },
{ "MessageStep", comgen_messagestep },
{ "RaisePrivacy", comgen_raiseprivacy },
{ NULL, NULL }
};

Expand Down
38 changes: 38 additions & 0 deletions gen_opc.lua
Expand Up @@ -272,6 +272,31 @@ local IOPCDataCallback = {
}
}

local IOPCSecurityPrivate = {
name = "IOPCSecurityPrivate",
iid = "{7AA83A02-6C77-11D3-84F9-00008630A38B}",
parent = IUnknown,
methods = {
{
name = "IsAvailablePriv",
parameters = {
{ type = types.bool, attributes = { out = true }, name = "pbAvailable" }
}
},
{
name = "Logon",
parameters = {
{ type = types.wstring, attributes = { ["in"] = true }, name = "szUserID" },
{ type = types.wstring, attributes = { ["in"] = true }, name = "szPassword" }
}
},
{
name = "Logoff",
parameters = {}
}
}
}

local OPCEVENTSERVERSTATE = {
name = "OPCEVENTSERVERSTATE",
fields = {
Expand Down Expand Up @@ -509,6 +534,14 @@ local opcae = {
enums = { OPCEVENTSERVERSTATE, HRESULT, types.vartype }
}

local opcsec = {
modname = "opcsec",
header = "opcsec",
interfaces = { IOPCSecurityPrivate },
wrappers = {},
enums = { HRESULT }
}

local source, def, wrap = generator.compile(opcda)

generator.writefile("opclib.cpp", source)
Expand All @@ -520,3 +553,8 @@ local source, def, wrap = generator.compile(opcae)
generator.writefile("opcae.cpp", source)
generator.writefile("opcae.def", def)
generator.writefile("opcae_w.cpp", wrap)

local source, def, wrap = generator.compile(opcsec)

generator.writefile("opcsec.cpp", source)
generator.writefile("opcsec.def", def)
21 changes: 20 additions & 1 deletion mpa/opc.lua
Expand Up @@ -2,6 +2,7 @@
require "comgen"
require "opclib"
require "connpoint"
pcall(require, "opcsec")

module("opc", package.seeall)

Expand All @@ -14,9 +15,27 @@ methods_v2.__index = methods_v2
local methods_cb = {}
methods_cb.__index = methods_cb

function open(server, host, use_v2, async)
function open(server, host, use_v2, async, user, pass)
if type(server) == "table" then
host = server.host
use_v2 = server.use_v2
async = server.async
user = server.user
pass = server.pass
server = server.server
end
local ok, server = pcall(comgen.CreateInstance, server, opclib.IOPCServer, host)
if not ok then return nil, "server not found, com error: " .. server end
if user then
local ok, sec = pcall(server.QueryInterface, server, opcsec.IOPCSecurityPrivate)
if not ok then return nil, "OPC Security interface not available: " .. server end
if not sec:IsAvailablePriv() then
return nil, "OPC Security interface not available: " .. server
end
comgen.RaisePrivacy(sec)
local ok = pcall(sec.Logon, sec, user, pass)
if not ok then return nil, "login to server failed: " .. server end
end
local ok, itemio = pcall(server.QueryInterface, server, opclib.IOPCItemIO)
if ok and (not use_v2) and (not async) then
return setmetatable({ server = server, itemio = itemio },
Expand Down

0 comments on commit 73ec168

Please sign in to comment.