Skip to content

Commit

Permalink
feat: add handle create session (#43)
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
shoothzj authored Oct 17, 2024
1 parent 10053f5 commit 347bebe
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
50 changes: 49 additions & 1 deletion opcua/service_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opcua

import (
"github.com/protocol-laboratory/opcua-go/opcua/uamsg"
"github.com/protocol-laboratory/opcua-go/opcua/util"
)

func (secChan *SecureChannel) handleOpenSecureChannelRequest(req *uamsg.Message) (*uamsg.Message, error) {
Expand Down Expand Up @@ -37,7 +38,54 @@ func (secChan *SecureChannel) handleCreateSessionRequest(req *uamsg.Message) (*u
if err != nil {
return nil, err
}
return nil, nil
session := newSession(createSessionRequest.SessionName, createSessionRequest.RequestedSessionTimeout, createSessionRequest.MaxResponseMessageSize)
token := getUniqueSessionAuthenticationToken()

rsp := &uamsg.Message{
MessageHeader: &uamsg.MessageHeader{
MessageType: uamsg.MsgMessageType,
SecureChannelId: &secChan.channelId,
},
SecurityHeader: &uamsg.SymmetricSecurityHeader{
TokenId: secChan.getCurrentTokenId(),
},
SequenceHeader: &uamsg.SequenceHeader{
SequenceNumber: secChan.getNextSequenceNumber(),
RequestId: req.RequestId,
},
MessageBody: &uamsg.GenericBody{
TypeId: &uamsg.ExpandedNodeId{
NodeId: &uamsg.ObjectCreateSessionResponse_Encoding_DefaultBinary,
},
Service: &uamsg.CreateSessionResponse{
Header: &uamsg.ResponseHeader{
Timestamp: util.GetCurrentUaTimestamp(),
RequestHandle: createSessionRequest.Header.RequestHandle,
ServiceResult: uint32(uamsg.ErrorCodeGood),
ServiceDiagnostics: &uamsg.DiagnosticInfo{
EncodingMask: 0x00,
},
StringTable: nil,
AdditionalHeader: &uamsg.ExtensionObject{
TypeId: &uamsg.NodeId{
EncodingType: uamsg.TwoByte,
Identifier: byte(0),
},
Encoding: 0x00,
},
},
SessionId: &session.sessionId,
AuthenticationToken: token,
RevisedSessionTimeout: uamsg.Duration(session.requestedSessionTimeout.Milliseconds()),
ServerNonce: session.serverNonce,
MaxRequestMessageSize: session.maxResponseMessageSize,
// TODO endpoints description needed
// ServerEndpoints: make([]*uamsg.EndpointDescription, 0),
},
},
}

return rsp, nil
}

func (secChan *SecureChannel) handleActivateSessionRequest(req *uamsg.Message) (*uamsg.Message, error) {
Expand Down
1 change: 1 addition & 0 deletions opcua/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func getUniqueSessionId() uamsg.NodeId {
return uamsg.NodeId{
EncodingType: uamsg.GuidType,
Namespace: 1,
// TODO should create the opcua-guid from a real uuid
Identifier: uamsg.Guid{
Data1: rand.Uint32(),
Data2: uint16(rand.Uint32() % 65535),
Expand Down

0 comments on commit 347bebe

Please sign in to comment.