/// <summary>
        /// Create File, named pipe, directory. One transport can only create one file.
        /// </summary>
        /// <param name="fileName">The file, namedpipe, directory name</param>
        /// <param name="desiredAccess">The desired access</param>
        /// <param name="impersonationLevel">The impersonation level</param>
        /// <param name="fileAttribute">The file attribute, this field is only valid when create file.
        /// </param>
        /// <param name="createDisposition">Defines the action the server MUST take if the file that is
        /// specified in the name field already exists</param>
        /// <param name="createOption">Specifies the options to be applied when creating or opening the file</param>
        private void InternalCreate(string fileName, NtTransactDesiredAccess desiredAccess,
                                    NtTransactImpersonationLevel impersonationLevel, SMB_EXT_FILE_ATTR fileAttribute,
                                    NtTransactCreateDisposition createDisposition, NtTransactCreateOptions createOption)
        {
            SmbNtCreateAndxRequestPacket request = smbClient.CreateCreateRequest(
                this.treeId,
                fileName,
                desiredAccess,
                fileAttribute,
                NtTransactShareAccess.NONE,
                createDisposition,
                createOption,
                impersonationLevel,
                CreateFlags.NT_CREATE_REQUEST_EXTENDED_RESPONSE);

            if (this.isSignRequired)
            {
                request.Sign(this.NextSequenceNumber, this.sessionKey);
            }

            uint status = 0;
            SmbNtCreateAndxResponsePacket response =
                this.SendAndExpectSmbPacket(request, internalTimeout, out status) as SmbNtCreateAndxResponsePacket;

            if (status != 0)
            {
                throw new InvalidOperationException("Create Failed. ErrorCode: " + status);
            }

            this.fid = response.SmbParameters.FID;
        }
        /// <summary>
        /// Create a not transaction create request packet for client to create new or open share on server.
        /// </summary>
        /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
        /// <param name = "fileName">A string that represents the name of the file to open or create. </param>
        /// <param name = "desiredAccess">
        /// Access wanted. This value MUST be specified in the ACCESS_MASK format, as specified in [CIFS] section 3.7. 
        /// </param>
        /// <param name = "extFileAttributes">Extended attributes and flags for this file or directory. </param>
        /// <param name = "shareAccess">Type of shared access requested for this file or directory. </param>
        /// <param name = "createDisposition">The action to take if a file does or does not exist. </param>
        /// <param name = "createOptions">The options to use if creating the file or directory. </param>
        /// <param name = "impersonationLevel">
        /// This field specifies the information given to the server about the client and how the server MUST   
        /// represent, or impersonate, the client. 
        /// </param>
        /// <param name = "createFlags">
        /// A 32-bit field containing a set of options that specify the security tracking mode. 
        /// </param>
        /// <param name="securityDescriptor">The security descriptor to use when requesting access to the file</param>
        /// <param name="extendedAttributes">
        /// The list of extended attributes that SHOULD be applied to the new file.
        /// </param>
        /// <returns>a nt transaction create request packet</returns>
        public virtual SmbNtTransactCreateRequestPacket CreateNtTransCreateRequest(
            ushort treeId,
            string fileName,
            NtTransactDesiredAccess desiredAccess,
            SMB_EXT_FILE_ATTR extFileAttributes,
            NtTransactShareAccess shareAccess,
            NtTransactCreateDisposition createDisposition,
            NtTransactCreateOptions createOptions,
            NtTransactImpersonationLevel impersonationLevel,
            CreateFlags createFlags,
            RawSecurityDescriptor securityDescriptor,
            FILE_FULL_EA_INFORMATION[] extendedAttributes)
        {
            Cifs.SmbNtTransactCreateRequestPacket request = this.cifsClient.CreateNtTransactCreateRequest(
                this.MessageId, this.GetSessionIdByTreeId(treeId), treeId,
                (SmbFlags)this.capability.Flag, (SmbFlags2)this.capability.Flags2,
                this.capability.MaxSetupCount, this.capability.MaxParameterCount, this.capability.MaxDataCount,
                (NtTransactFlags)createFlags,
                0, // No root directory
                desiredAccess,
                0, // AllocationSize should be 0
                extFileAttributes, shareAccess, createDisposition, (Cifs.NtTransactCreateOptions)createOptions,
                impersonationLevel,
                (NtTransactSecurityFlags)0x00,
                fileName,
                securityDescriptor, // No security descriptor
                extendedAttributes // No extended attributes
                );

            return new SmbNtTransactCreateRequestPacket(request);
        }
 /// <summary>
 /// Create a create request packet for client to create new or open share on server. 
 /// </summary>
 /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
 /// <param name = "fileName">A string that represents the name of the file to open or create. </param>
 /// <param name = "desiredAccess">
 /// Access wanted. This value MUST be specified in the ACCESS_MASK format, as specified in [CIFS] section 3.7. 
 /// </param>
 /// <param name = "extFileAttributes">Extended attributes and flags for this file or directory. </param>
 /// <param name = "shareAccess">Type of shared access requested for this file or directory. </param>
 /// <param name = "createDisposition">The action to take if a file does or does not exist. </param>
 /// <param name = "createOptions">The options to use if creating the file or directory. </param>
 /// <param name = "impersonationLevel">
 /// This field specifies the information given to the server about the client and how the server MUST   
 /// represent, or impersonate, the client. 
 /// </param>
 /// <param name = "createFlags">
 /// A 32-bit field containing a set of options that specify the security tracking mode. 
 /// </param>
 /// <returns>a create packet </returns>
 public SmbNtCreateAndxRequestPacket CreateCreateRequest(
     ushort treeId,
     string fileName,
     NtTransactDesiredAccess desiredAccess,
     SMB_EXT_FILE_ATTR extFileAttributes,
     NtTransactShareAccess shareAccess,
     NtTransactCreateDisposition createDisposition,
     NtTransactCreateOptions createOptions,
     NtTransactImpersonationLevel impersonationLevel,
     CreateFlags createFlags
     )
 {
     return CreateCreateRequest(this.MessageId,
         this.GetSessionIdByTreeId(treeId), treeId,
        this.capability.Flag, this.capability.Flags2, fileName,
         desiredAccess, extFileAttributes, shareAccess, createDisposition, createOptions,
         impersonationLevel, createFlags);
 }
 /// <summary>
 /// Create a create request packet for client to create new or open share on server. 
 /// </summary>
 /// <param name = "messageId">the id of message, used to identity the request and the server response. </param>
 /// <param name = "sessionUid">the valid session id, must be response by server of the session setup request. </param>
 /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
 /// <param name = "flags">
 /// The Flags field contains individual flags, as specified in [CIFS] sections 2.4.2 and 3.1.1. 
 /// </param>
 /// <param name = "flags2">
 /// The Flags2 field contains individual bit flags that, depending on the negotiated SMB dialect, indicate   
 /// various client and server capabilities. 
 /// </param>
 /// <param name = "fileName">A string that represents the name of the file to open or create. </param>
 /// <param name = "desiredAccess">
 /// Access wanted. This value MUST be specified in the ACCESS_MASK format, as specified in [CIFS] section 3.7. 
 /// </param>
 /// <param name = "extFileAttributes">Extended attributes and flags for this file or directory. </param>
 /// <param name = "shareAccess">Type of shared access requested for this file or directory. </param>
 /// <param name = "createDisposition">The action to take if a file does or does not exist. </param>
 /// <param name = "createOptions">The options to use if creating the file or directory. </param>
 /// <param name = "impersonationLevel">
 /// This field specifies the information given to the server about the client and how the server MUST   
 /// represent, or impersonate, the client. 
 /// </param>
 /// <param name = "createFlags">
 /// A 32-bit field containing a set of options that specify the security tracking mode. 
 /// </param>
 /// <returns>a create packet </returns>
 private SmbNtCreateAndxRequestPacket CreateCreateRequest(
     ushort messageId,
     ushort sessionUid,
     ushort treeId,
     SmbHeader_Flags_Values flags,
     SmbHeader_Flags2_Values flags2,
     string fileName,
     NtTransactDesiredAccess desiredAccess,
     SMB_EXT_FILE_ATTR extFileAttributes,
     NtTransactShareAccess shareAccess,
     NtTransactCreateDisposition createDisposition,
     NtTransactCreateOptions createOptions,
     NtTransactImpersonationLevel impersonationLevel,
     CreateFlags createFlags)
 {
     return new SmbNtCreateAndxRequestPacket(
         this.cifsClient.CreateNtCreateAndxRequest(
         messageId, sessionUid, treeId, (SmbFlags)flags, (SmbFlags2)flags2, (NtTransactFlags)createFlags,
         0, desiredAccess, 0, extFileAttributes, shareAccess, createDisposition,
         (Cifs.NtTransactCreateOptions)createOptions, impersonationLevel, (NtTransactSecurityFlags)0x00,
         fileName, null));
 }
 /// <summary>
 /// Constructor
 /// </summary>
 internal SmbServerOpen()
 {
     this.grantedAccess = (NtTransactDesiredAccess)0x00;
 }
 /// <summary>
 /// to create a NtTransactCreate request packet.
 /// </summary>
 /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
 /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
 /// accessing.</param>
 /// <param name="ntTransParametersFlags">A 32-bit field containing a set of flags that modify the client
 /// request. Unused bits SHOULD be set to 0 by the client when sending a message and MUST be ignored when
 /// received by the server. </param>
 /// <param name="rootDirectoryFID">If nonzero, this value is the FID of an opened root directory, and the Name
 /// field MUST be handled as relative to the directory specified by this FID. If this value is zero the Name
 /// field MUST be handled as relative to the root of the share (the TID). The FID MUST have been acquired in a
 /// previous message exchange</param>
 /// <param name="desiredAccess">A 32-bit field containing standard, specific, and generic access rights</param>
 /// <param name="allocationSize">The client MUST set this value to the initial allocation size of the file in
 /// bytes</param>
 /// <param name="extFileAttributes">A 32-bit field containing encoded file attribute values and file access
 /// behavior flag values.</param>
 /// <param name="shareAccess">A 32-bit field that specifies how the file SHOULD be shared with other processes.
 /// </param>
 /// <param name="createDisposition">A 32-bit value that represents the action to take if the file already
 /// exists or if the file is a new file and does not already exist.</param>
 /// <param name="createOptions">ULONG A 32-bit field containing flag options to use if creating the file or
 /// directory. This field MUST be set to 0 or a combination of the following possible values</param>
 /// <param name="impersonationLevel">ULONG A value that indicates what security context the server SHOULD use
 /// when executing the command on behalf of the client</param>
 /// <param name="securityFlags">A 32-bit field containing a set of options that specify the security tracking
 /// mode.</param>
 /// <param name="name">The name of the file; not null-terminated.  </param>
 /// <param name="securityDescriptor">The security descriptor to use when requesting access to the file</param>
 /// <param name="extendedAttributes">The list of extended attributes that SHOULD be applied to the new file.
 /// </param>
 /// <returns>a NtTransactCreate request packet</returns>
 /// <exception cref="System.NullReferenceException">There is no connection in context. </exception>
 public SmbNtTransactCreateRequestPacket CreateNtTransactCreateRequest(
     ushort uid,
     ushort treeId,
     NtTransactFlags ntTransParametersFlags,
     uint rootDirectoryFID,
     NtTransactDesiredAccess desiredAccess,
     ulong allocationSize,
     SMB_EXT_FILE_ATTR extFileAttributes,
     NtTransactShareAccess shareAccess,
     NtTransactCreateDisposition createDisposition,
     NtTransactCreateOptions createOptions,
     NtTransactImpersonationLevel impersonationLevel,
     NtTransactSecurityFlags securityFlags,
     string name,
     RawSecurityDescriptor securityDescriptor,
     FILE_FULL_EA_INFORMATION[] extendedAttributes)
 {
     return this.CreateNtTransactCreateRequest(this.Context.GetMessageId(this.connectionId),
         uid, treeId, this.defaultParameters.Flag, this.defaultParameters.Flag2, this.defaultParameters.MaxSetupCount,
         this.defaultParameters.MaxParameterCount, this.defaultParameters.MaxDataCount, ntTransParametersFlags,
         rootDirectoryFID, desiredAccess, allocationSize, extFileAttributes, shareAccess,
         createDisposition, createOptions, impersonationLevel, securityFlags, name,
         securityDescriptor, extendedAttributes);
 }
        /// <summary>
        /// to create a NtTransactCreate request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <param name="ntTransParametersFlags">A 32-bit field containing a set of flags that modify the client
        /// request. Unused bits SHOULD be set to 0 by the client when sending a message and MUST be ignored when
        /// received by the server. </param>
        /// <param name="rootDirectoryFID">If nonzero, this value is the FID of an opened root directory, and the Name
        /// field MUST be handled as relative to the directory specified by this FID. If this value is zero the Name
        /// field MUST be handled as relative to the root of the share (the TID). The FID MUST have been acquired in a
        /// previous message exchange</param>
        /// <param name="desiredAccess">A 32-bit field containing standard, specific, and generic access rights</param>
        /// <param name="allocationSize">The client MUST set this value to the initial allocation size of the file in
        /// bytes</param>
        /// <param name="extFileAttributes">A 32-bit field containing encoded file attribute values and file access
        /// behavior flag values.</param>
        /// <param name="shareAccess">A 32-bit field that specifies how the file SHOULD be shared with other processes.
        /// </param>
        /// <param name="createDisposition">A 32-bit value that represents the action to take if the file already
        /// exists or if the file is a new file and does not already exist.</param>
        /// <param name="createOptions">ULONG A 32-bit field containing flag options to use if creating the file or
        /// directory. This field MUST be set to 0 or a combination of the following possible values</param>
        /// <param name="impersonationLevel">ULONG A value that indicates what security context the server SHOULD use
        /// when executing the command on behalf of the client</param>
        /// <param name="securityFlags">A 32-bit field containing a set of options that specify the security tracking mode.</param>
        /// <param name="name">The name of the file; not null-terminated.  </param>
        /// <param name="securityDescriptor">The security descriptor to use when requesting access to the file</param>
        /// <param name="extendedAttributes">The list of extended attributes that SHOULD be applied to the new file.
        /// </param>
        /// <returns>a NtTransactCreate request packet</returns>
        public SmbNtTransactCreateRequestPacket CreateNtTransactCreateRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            byte maxSetupCount,
            uint maxParameterCount,
            uint maxDataCount,
            NtTransactFlags ntTransParametersFlags,
            uint rootDirectoryFID,
            NtTransactDesiredAccess desiredAccess,
            ulong allocationSize,
            SMB_EXT_FILE_ATTR extFileAttributes,
            NtTransactShareAccess shareAccess,
            NtTransactCreateDisposition createDisposition,
            NtTransactCreateOptions createOptions,
            NtTransactImpersonationLevel impersonationLevel,
            NtTransactSecurityFlags securityFlags,
            string name,
            RawSecurityDescriptor securityDescriptor,
            FILE_FULL_EA_INFORMATION[] extendedAttributes)
        {
            if (name == null)
            {
                name = string.Empty;
            }
            if (extendedAttributes == null)
            {
                extendedAttributes = new FILE_FULL_EA_INFORMATION[0];
            }

            SmbNtTransactCreateRequestPacket packet = new SmbNtTransactCreateRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_NT_TRANSACT,
                messageId, uid, treeId, flags, flags2);

            // Set Smb_Parameters
            SMB_COM_NT_TRANSACT_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_TRANSACT_Request_SMB_Parameters();
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.SetupCount = 0; // the correct count in word of the Setup is always 0.
            smbParameters.Function = NtTransSubCommand.NT_TRANSACT_CREATE;
            smbParameters.Setup = new ushort[0];
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_NT_TRANSACT_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb_Data
            SMB_COM_NT_TRANSACT_Request_SMB_Data smbData = new SMB_COM_NT_TRANSACT_Request_SMB_Data();

            // Set NT_TransParameters
            NT_TRANSACT_CREATE_Request_NT_Trans_Parameters ntTransParameters =
                new NT_TRANSACT_CREATE_Request_NT_Trans_Parameters();
            ntTransParameters.Flags = ntTransParametersFlags;
            ntTransParameters.RootDirectoryFID = rootDirectoryFID;
            ntTransParameters.DesiredAccess = desiredAccess;
            ntTransParameters.AllocationSize = allocationSize;
            if (createDisposition == NtTransactCreateDisposition.FILE_OPEN
                || createDisposition == NtTransactCreateDisposition.NONE
                || (extFileAttributes & SMB_EXT_FILE_ATTR.ATTR_DIRECTORY)
                == SMB_EXT_FILE_ATTR.ATTR_DIRECTORY)
            {
                ntTransParameters.AllocationSize = 0;
            }

            ntTransParameters.ExtFileAttributes = extFileAttributes;
            ntTransParameters.ShareAccess = shareAccess;
            ntTransParameters.CreateDisposition = createDisposition;
            ntTransParameters.CreateOptions = createOptions;
            ntTransParameters.SecurityDescriptorLength = securityDescriptor == null
                ? 0 : (uint)securityDescriptor.BinaryLength;
            ntTransParameters.ImpersonationLevel = impersonationLevel;
            ntTransParameters.SecurityFlags = securityFlags;

            if ((flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE)
            {
                ntTransParameters.Name = Encoding.Unicode.GetBytes(name);
            }
            else
            {
                ntTransParameters.Name = Encoding.ASCII.GetBytes(name);
            }
            ntTransParameters.NameLength = (uint)ntTransParameters.Name.Length;

            // The size of the preceding SmbParameters part plus Header is an odd number for all cifs messages
            // Use ntTransParameters field to judge whether needs to add one 16-bits align pad.
            if ((flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE &&
                CifsMessageUtils.GetSize<NT_TRANSACT_CREATE_Request_NT_Trans_Parameters>(ntTransParameters) % twoBytesAlign == 0)
            {
                // pad 1 byte for 16-bits align:
                smbData.Pad2 = new byte[1];
            }
            else
            {
                smbData.Pad2 = new byte[0];
            }

            // Set NT_TransData
            NT_TRANSACT_CREATE_Request_NT_Trans_Data ntTransData = new NT_TRANSACT_CREATE_Request_NT_Trans_Data();
            ntTransData.ExtendedAttributes = extendedAttributes;

            for (int i = 0; i < ntTransData.ExtendedAttributes.Length; i++)
            {
                int eaNameLength = ntTransData.ExtendedAttributes[i].EaName == null
                    ? 0 : ntTransData.ExtendedAttributes[i].EaName.Length;
                int eaValueLength = ntTransData.ExtendedAttributes[i].EaValue == null
                    ? 0 : ntTransData.ExtendedAttributes[i].EaValue.Length;
                uint currentLength = (uint)(EA.FULL_EA_FIXED_SIZE + eaNameLength + eaValueLength);
                uint alignPad = (fourBytesAlign - currentLength % fourBytesAlign) % fourBytesAlign;

                if (i != ntTransData.ExtendedAttributes.Length - 1)
                {
                    currentLength += alignPad;
                    ntTransData.ExtendedAttributes[i].NextEntryOffset = currentLength;
                }
                ntTransParameters.EALength += currentLength;
            }

            ntTransData.SecurityDescriptor = securityDescriptor;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.NtTransParameters = ntTransParameters;
            packet.NtTransData = ntTransData;
            packet.UpdateCountAndOffset();

            return packet;
        }
 /// <summary>
 /// to create a NtCreateAndx request packet.
 /// </summary>
 /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
 /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
 /// accessing.</param>
 /// <param name="smbParametersFlags">A 32-bit field containing a set of flags that modify the client
 /// request</param>
 /// <param name="rootDirectoryFID">If nonzero, this value is the File ID of an opened root directory, and the
 /// FileName field MUST be handled as relative to the directory specified by this RootDirectoryFID.</param>
 /// <param name="desiredAccess">A 32-bit field of flags that indicate standard, specific, and generic access
 /// rights. </param>
 /// <param name="allocationSize">The client MUST set this value to the initial allocation size of the file in
 /// bytes.</param>
 /// <param name="extFileAttributes">A 32-bit field containing encoded file attribute values and file access
 /// behavior flag values</param>
 /// <param name="shareAccess">A 32-bit field that specifies how the file SHOULD be shared with other
 /// processes.</param>
 /// <param name="createDisposition">A 32-bit value that represents the action to take if the file already
 /// exists or if the file is a new file and does not already exist</param>
 /// <param name="createOptions">A 32-bit field containing flag options to use if creating the file or
 /// directory</param>
 /// <param name="impersonationLevel">A value that indicates what security context the server SHOULD use when
 /// executing the command on behalf of the client. Value names are provided for convenience only</param>
 /// <param name="securityFlags">A 32-bit field containing a set of options that specify the security tracking
 /// mode.</param>
 /// <param name="fileName">A string that represents the fully qualified name of the file relative to the
 /// supplied TID to create or truncate on the server.</param>
 /// <param name="andxPacket">the andx packet.</param>
 /// <returns>a NtCreateAndx request packet</returns>
 /// <exception cref="System.NullReferenceException">There is no connection in context. </exception>
 public SmbNtCreateAndxRequestPacket CreateNtCreateAndxRequest(
     ushort uid,
     ushort treeId,
     NtTransactFlags smbParametersFlags,
     uint rootDirectoryFID,
     NtTransactDesiredAccess desiredAccess,
     ulong allocationSize,
     SMB_EXT_FILE_ATTR extFileAttributes,
     NtTransactShareAccess shareAccess,
     NtTransactCreateDisposition createDisposition,
     NtTransactCreateOptions createOptions,
     NtTransactImpersonationLevel impersonationLevel,
     NtTransactSecurityFlags securityFlags,
     string fileName,
     SmbPacket andxPacket)
 {
     return this.CreateNtCreateAndxRequest(this.Context.GetMessageId(this.connectionId),
         uid, treeId, this.defaultParameters.Flag, this.defaultParameters.Flag2, smbParametersFlags, rootDirectoryFID,
         desiredAccess, allocationSize, extFileAttributes, shareAccess, createDisposition,
         createOptions, impersonationLevel, securityFlags, fileName, andxPacket);
 }
        /// <summary>
        /// to create a NtCreateAndx request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="smbParametersFlags">A 32-bit field containing a set of flags that modify the client
        /// request</param>
        /// <param name="rootDirectoryFID">If nonzero, this value is the File ID of an opened root directory, and the
        /// FileName field MUST be handled as relative to the directory specified by this RootDirectoryFID.</param>
        /// <param name="desiredAccess">A 32-bit field of flags that indicate standard, specific, and generic access
        /// rights. </param>
        /// <param name="allocationSize">The client MUST set this value to the initial allocation size of the file in
        /// bytes.</param>
        /// <param name="extFileAttributes">A 32-bit field containing encoded file attribute values and file access
        /// behavior flag values</param>
        /// <param name="shareAccess">A 32-bit field that specifies how the file SHOULD be shared with other
        /// processes.</param>
        /// <param name="createDisposition">A 32-bit value that represents the action to take if the file already
        /// exists or if the file is a new file and does not already exist</param>
        /// <param name="createOptions">A 32-bit field containing flag options to use if creating the file or
        /// directory</param>
        /// <param name="impersonationLevel">A value that indicates what security context the server SHOULD use when
        /// executing the command on behalf of the client. Value names are provided for convenience only</param>
        /// <param name="securityFlags">A 32-bit field containing a set of options that specify the security tracking
        /// mode.</param>
        /// <param name="fileName">A string that represents the fully qualified name of the file relative to the
        /// supplied TID to create or truncate on the server.</param>
        /// <param name="andxPacket">the andx packet.</param>
        /// <returns>a NtCreateAndx request packet</returns>
        public SmbNtCreateAndxRequestPacket CreateNtCreateAndxRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            NtTransactFlags smbParametersFlags,
            uint rootDirectoryFID,
            NtTransactDesiredAccess desiredAccess,
            ulong allocationSize,
            SMB_EXT_FILE_ATTR extFileAttributes,
            NtTransactShareAccess shareAccess,
            NtTransactCreateDisposition createDisposition,
            NtTransactCreateOptions createOptions,
            NtTransactImpersonationLevel impersonationLevel,
            NtTransactSecurityFlags securityFlags,
            string fileName,
            SmbPacket andxPacket)
        {
            if (fileName == null)
            {
                fileName = string.Empty;
            }

            SmbNtCreateAndxRequestPacket packet = new SmbNtCreateAndxRequestPacket();

            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_NT_CREATE_ANDX,
                messageId, uid, treeId, flags, flags2);

            SMB_COM_NT_CREATE_ANDX_Request_SMB_Parameters smbParameters =
                new SMB_COM_NT_CREATE_ANDX_Request_SMB_Parameters();
            if (andxPacket == null)
            {
                smbParameters.AndXCommand = SmbCommand.SMB_COM_NO_ANDX_COMMAND;
            }
            else
            {
                smbParameters.AndXCommand = andxPacket.SmbHeader.Command;
            }
            smbParameters.AndXReserved = 0;
            smbParameters.Reserved = 0;
            smbParameters.Flags = (uint)smbParametersFlags;
            smbParameters.RootDirectoryFID = rootDirectoryFID;
            smbParameters.DesiredAccess = (uint)desiredAccess;
            smbParameters.AllocationSize = allocationSize;
            smbParameters.ExtFileAttributes = (uint)extFileAttributes;
            smbParameters.ShareAccess = (uint)shareAccess;
            smbParameters.CreateDisposition = (uint)createDisposition;
            smbParameters.CreateOptions = (uint)createOptions;
            smbParameters.ImpersonationLevel = (uint)impersonationLevel;
            smbParameters.SecurityFlags = (byte)securityFlags;
            smbParameters.WordCount = (byte)(Marshal.SizeOf(smbParameters) / NumBytesOfWord);

            SMB_COM_NT_CREATE_ANDX_Request_SMB_Data smbData = new SMB_COM_NT_CREATE_ANDX_Request_SMB_Data();

            // The size of the preceding SmbParameters part plus Header part is an odd number for all cifs messages
            // Use Name field to judge whether needs to add one 16-bits align pad.
            if ((flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE)
            {
                // if Unicode, add 1 byte pad for align on 16-bits.
                smbData.Pad = new byte[1];
                smbData.FileName = CifsMessageUtils.ToSmbStringBytes(fileName, true);
                smbParameters.NameLength = (ushort)(smbData.FileName.Length);
            }
            else
            {
                smbData.Pad = new byte[0];
                smbData.FileName = CifsMessageUtils.ToSmbStringBytes(fileName, false);
                smbParameters.NameLength = (ushort)smbData.FileName.Length;
            }

            smbData.ByteCount = (ushort)(smbData.Pad.Length + smbData.FileName.Length);

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.AndxPacket = andxPacket;

            return packet;
        }
 /// <summary>
 /// Constructor
 /// </summary>
 internal SmbServerOpen()
 {
     this.grantedAccess = (NtTransactDesiredAccess)0x00;
 }
        /// <summary>
        /// Create File, named pipe, directory. One transport can only create one file.
        /// </summary>
        /// <param name="fileName">The file, namedpipe, directory name</param>
        /// <param name="desiredAccess">The desired access</param>
        /// <param name="impersonationLevel">The impersonation level</param>
        /// <param name="fileAttribute">The file attribute, this field is only valid when create file.
        /// </param>
        /// <param name="createDisposition">Defines the action the server MUST take if the file that is
        /// specified in the name field already exists</param>
        /// <param name="createOption">Specifies the options to be applied when creating or opening the file</param>
        private void InternalCreate(string fileName, NtTransactDesiredAccess desiredAccess,
            NtTransactImpersonationLevel impersonationLevel, SMB_EXT_FILE_ATTR fileAttribute,
            NtTransactCreateDisposition createDisposition, NtTransactCreateOptions createOption)
        {
            SmbNtCreateAndxRequestPacket request = smbClient.CreateCreateRequest(
                this.treeId,
                fileName,
                desiredAccess,
                fileAttribute,
                NtTransactShareAccess.NONE,
                createDisposition,
                createOption,
                impersonationLevel,
                CreateFlags.NT_CREATE_REQUEST_EXTENDED_RESPONSE);

            if (this.isSignRequired)
            {
                request.Sign(this.NextSequenceNumber, this.sessionKey);
            }

            uint status = 0;
            SmbNtCreateAndxResponsePacket response =
                this.SendAndExpectSmbPacket(request, internalTimeout, out status) as SmbNtCreateAndxResponsePacket;

            if (status != 0)
            {
                throw new InvalidOperationException("Create Failed. ErrorCode: " + status);
            }

            this.fid = response.SmbParameters.FID;
        }