Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def register_server2(self, params):
self.logger.info("register_server2")
request = ua.RegisterServer2Request()
request.Parameters = params
data = self._uasocket.send_request(request)
response = struct_from_binary(ua.RegisterServer2Response, data)
self.logger.debug(response)
response.ResponseHeader.ServiceResult.check()
return response.ConfigurationResults
def register_nodes(self, nodes):
self.logger.info("register_nodes")
request = ua.RegisterNodesRequest()
request.Parameters.NodesToRegister = nodes
data = self._uasocket.send_request(request)
response = struct_from_binary(ua.RegisterNodesResponse, data)
self.logger.debug(response)
response.ResponseHeader.ServiceResult.check()
return response.Parameters.RegisteredNodeIds
def browse_next(self, parameters):
self.logger.info("browse next")
request = ua.BrowseNextRequest()
request.Parameters = parameters
data = self._uasocket.send_request(request)
response = struct_from_binary(ua.BrowseNextResponse, data)
self.logger.debug(response)
response.ResponseHeader.ServiceResult.check()
return response.Parameters.Results
def activate_session(self, parameters):
self.logger.info("activate_session")
request = ua.ActivateSessionRequest()
request.Parameters = parameters
data = self._uasocket.send_request(request)
response = struct_from_binary(ua.ActivateSessionResponse, data)
self.logger.debug(response)
response.ResponseHeader.ServiceResult.check()
return response.Parameters
async def read(self, parameters):
self.logger.info("read")
request = ua.ReadRequest()
request.Parameters = parameters
data = await self.protocol.send_request(request)
response = struct_from_binary(ua.ReadResponse, data)
self.logger.debug(response)
response.ResponseHeader.ServiceResult.check()
# cast to Enum attributes that need to
for idx, rv in enumerate(parameters.NodesToRead):
if rv.AttributeId == ua.AttributeIds.NodeClass:
dv = response.Results[idx]
if dv.StatusCode.is_good():
dv.Value.Value = ua.NodeClass(dv.Value.Value)
elif rv.AttributeId == ua.AttributeIds.ValueRank:
dv = response.Results[idx]
if dv.StatusCode.is_good() and dv.Value.Value in (-3, -2, -1, 0, 1, 2, 3, 4):
dv.Value.Value = ua.ValueRank(dv.Value.Value)
return response.Results
security_header = struct_from_binary(ua.AsymmetricAlgorithmHeader, data)
self.select_policy(security_header.SecurityPolicyURI, security_header.SenderCertificate)
elif header.MessageType in (ua.MessageType.SecureMessage,
ua.MessageType.SecureClose):
data = body.copy(header.body_size)
security_header = struct_from_binary(ua.SymmetricAlgorithmHeader, data)
self._check_sym_header(security_header)
if header.MessageType in (ua.MessageType.SecureMessage,
ua.MessageType.SecureOpen,
ua.MessageType.SecureClose):
chunk = MessageChunk.from_header_and_body(self.security_policy,
header, body)
return self._receive(chunk)
elif header.MessageType == ua.MessageType.Hello:
msg = struct_from_binary(ua.Hello, body)
self._max_chunk_size = msg.ReceiveBufferSize
return msg
elif header.MessageType == ua.MessageType.Acknowledge:
msg = struct_from_binary(ua.Acknowledge, body)
self._max_chunk_size = msg.SendBufferSize
return msg
elif header.MessageType == ua.MessageType.Error:
msg = struct_from_binary(ua.ErrorMessage, body)
logger.warning("Received an error: %s", msg)
return msg
else:
raise ua.UaError("Unsupported message type {0}".format(header.MessageType))
def _create_subscription_callback(self, pub_callback, ready_callback, resp_fut, data_fut):
self.logger.info("_create_subscription_callback")
data = data_fut.result()
response = struct_from_binary(ua.CreateSubscriptionResponse, data)
self.logger.debug(response)
response.ResponseHeader.ServiceResult.check()
if ready_callback:
ready_callback(response)
self._publishcallbacks[response.Parameters.SubscriptionId] = pub_callback
resp_fut.set_result(response.Parameters)
async def open_secure_channel(self, params):
self.logger.info("open_secure_channel")
request = ua.OpenSecureChannelRequest()
request.Parameters = params
future = self._send_request(request, message_type=ua.MessageType.SecureOpen)
await asyncio.wait_for(future, self.timeout)
result = future.result()
# FIXME: we have a race condition here
# we can get a packet with the new token id before we reach to store it..
response = struct_from_binary(ua.OpenSecureChannelResponse, result)
response.ResponseHeader.ServiceResult.check()
self._connection.set_channel(response.Parameters)
return response.Parameters
elif header.MessageType == ua.MessageType.SecureOpen:
security_header = struct_from_binary(ua.AsymmetricAlgorithmHeader, data)
crypto = security_policy.asymmetric_cryptography
else:
raise ua.UaError("Unsupported message type: {0}".format(header.MessageType))
obj = MessageChunk(crypto)
obj.MessageHeader = header
obj.SecurityHeader = security_header
decrypted = crypto.decrypt(data.read(len(data)))
signature_size = crypto.vsignature_size()
if signature_size > 0:
signature = decrypted[-signature_size:]
decrypted = decrypted[:-signature_size]
crypto.verify(header_to_binary(obj.MessageHeader) + struct_to_binary(obj.SecurityHeader) + decrypted, signature)
data = ua.utils.Buffer(crypto.remove_padding(decrypted))
obj.SequenceHeader = struct_from_binary(ua.SequenceHeader, data)
obj.Body = data.read(len(data))
return obj
async def create_session(self, parameters):
self.logger.info("create_session")
request = ua.CreateSessionRequest()
request.Parameters = parameters
data = await self.protocol.send_request(request)
response = struct_from_binary(ua.CreateSessionResponse, data)
self.logger.debug(response)
response.ResponseHeader.ServiceResult.check()
self.protocol.authentication_token = response.Parameters.AuthenticationToken
return response.Parameters