/// <summary>
 /// Create Trans2GetDFSReferral request for client to get the dfs referral 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 = "transactOptions">
 /// A set of bit flags that alter the behavior of the requested operation. Unused bit fields MUST be set to  
 /// zero by the client sending the request, and MUST be ignored by the server receiving the request. The 
 /// client MAY set either or both of the following bit flags 
 /// </param>
 /// <param name = "timeOut">
 /// The maximum amount of time in milliseconds to wait for the operation to complete. The client SHOULD set  
 /// this to 0 to indicate that no time-out is given. If the operation does not complete within the specified  
 /// time, the server MAY abort the request and send a failure response. 
 /// </param>
 /// <param name = "dfsPathName">use to indicate the REQ_GET_DFS_REFERRAL([MS-DFSC] section 2.2.2). </param>
 /// <param name = "referralRequest">This field MUST be a properly formatted DFS referral request </param>
 /// <returns>a get dfs referral request packet </returns>
 private SmbTrans2GetDfsReferralRequestPacket CreateTrans2GetDFSReferralRequest(
     ushort messageId,
     ushort sessionUid,
     ushort treeId,
     SmbHeader_Flags_Values flags,
     SmbHeader_Flags2_Values flags2,
     Trans2SmbParametersFlags transactOptions,
     uint timeOut,
     string dfsPathName,
     REQ_GET_DFS_REFERRAL referralRequest)
 {
     return new SmbTrans2GetDfsReferralRequestPacket(
         this.cifsClient.CreateTrans2GetDfsReferalRequest(
         messageId, sessionUid, treeId, (SmbFlags)flags, (SmbFlags2)flags2,
         this.capability.MaxParameterCount, this.capability.MaxDataCount, this.capability.MaxSetupCount,
         transactOptions, timeOut, dfsPathName, referralRequest));
 }
 /// <summary>
 /// Create Trans2GetDFSReferral request for client to get the Dfs referral on server. 
 /// </summary>
 /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
 /// <param name = "transactOptions">
 /// A set of bit flags that alter the behavior of the requested operation. Unused bit fields MUST be set to  
 /// zero by the client sending the request, and MUST be ignored by the server receiving the request. The 
 /// client MAY set either or both of the following bit flags 
 /// </param>
 /// <param name = "dfsPathName">use to indicate the REQ_GET_DFS_REFERRAL([MS-DFSC] section 2.2.2). </param>
 /// <param name = "referralRequest">This field MUST be a properly formatted DFS referral request </param>
 /// <returns>a get dfs referral request packet </returns>
 public virtual SmbTrans2GetDfsReferralRequestPacket CreateTrans2GetDFSReferralRequest(
     ushort treeId,
     Trans2SmbParametersFlags transactOptions,
     string dfsPathName,
     REQ_GET_DFS_REFERRAL referralRequest)
 {
     return CreateTrans2GetDFSReferralRequest(
         this.MessageId, this.GetSessionIdByTreeId(treeId), treeId,
        this.capability.Flag, this.capability.Flags2,
         transactOptions, this.capability.Timeout,
         dfsPathName, referralRequest);
 }
        /// <summary>
        /// Send a Dfs request to server
        /// 1. Decode payload from byte array to Dfsc REQ_GET_DFS_REFERRAL payload defined in Cifs
        /// 2. Create cifs packet with payload and send out.
        /// </summary>
        /// <param name="payload">REQ_GET_DFS_REFERRAL structure in byte array</param>
        /// <param name="isEX"> 
        /// Boolean indicating whether payload contains REQ_GET_DFS_REFFERAL_EX message.
        /// MUST be false for CIFS transport
        /// </param>
        /// <exception cref="System.ArgumentNullException">the payload to be sent is null.</exception>
        /// <exception cref="ArgumentException">extended request packet cannot be in payload</exception>
        /// <exception cref="System.InvalidOperationException">The transport is not connected</exception>
        public override void SendDfscPayload(byte[] payload, bool isEX)
        {
            if (isEX)
            {
                throw new ArgumentException("REQ_DFS_GET_REFERRAL_EX message not supported over CIFS transport");
            }

            if (this.cifsClient == null)
            {
                throw new InvalidOperationException("The transport is not connected.");
            }

            if (payload == null)
            {
                throw new ArgumentNullException("payload");
            }
            REQ_GET_DFS_REFERRAL referral = new REQ_GET_DFS_REFERRAL();

            // Put payload[0] into the lower byte of MaxReferralLevel
            // Put payload[1] into the upper byte of MaxReferralLevel
            // Sample: payload[0] = 0x0B; payload[1] = 0x0A
            // MaxReferralLevel = 0x0A0B;
            referral.MaxReferralLevel = (ushort)(payload[1] << 8 | payload[0]);
            referral.RequestFileName = new byte[payload.Length - sizeofMaxReferralLevel];

            Array.Copy(payload, sizeofMaxReferralLevel, referral.RequestFileName, 0, referral.RequestFileName.Length
                - sizeofMaxReferralLevel);

            SmbPacket request = this.cifsClient.CreateTrans2GetDfsReferalRequest(
                this.uid,
                this.treeId,
                "",
                referral);
            this.cifsClient.SendPacket(request);
        }
 /// <summary>
 /// to create a Trans2GetDfsReferal 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="referralRequest">This field MUST be a properly formatted DFS referral request</param>
 /// <param name="name">This field is present but not used in SMB_COM_TRANSACTION2 requests. If Unicode support
 /// has been negotiated, then this field MUST be aligned to a 16-bit boundary and MUST consist of two null bytes
 /// (a null-terminator). If Unicode support has not been negotiated this field will contain only one null
 /// byte.</param>
 /// <returns>a Trans2GetDfsReferal request packet</returns>
 /// <exception cref="System.NullReferenceException">There is no connection in context. </exception>
 public SmbTrans2GetDfsReferalRequestPacket CreateTrans2GetDfsReferalRequest(
     ushort uid,
     ushort treeId,
     string name,
     REQ_GET_DFS_REFERRAL referralRequest)
 {
     return this.CreateTrans2GetDfsReferalRequest(this.Context.GetMessageId(this.connectionId),
         uid, treeId, this.defaultParameters.Flag, this.defaultParameters.Flag2, this.defaultParameters.MaxParameterCount,
         this.defaultParameters.MaxDataCount, this.defaultParameters.MaxSetupCount, this.defaultParameters.Trans2SmbParametersFlags,
         this.defaultParameters.Timeout, name, referralRequest);
 }
        /// <summary>
        /// to create a Trans2GetDfsReferal 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="smbParametersFlags">A set of bit flags that alter the behavior of the requested operation.
        /// Unused bit fields MUST be set to zero by the client sending the request, and MUST be ignored by the server
        /// receiving the request. The client MAY set either or both of the following bit flags</param>
        /// <param name="timeout">The number of milliseconds the server SHOULD wait for completion of the transaction
        /// before generating a timeout. A value of zero indicates that the operation MUST NOT block.</param>
        /// <param name="referralRequest">This field MUST be a properly formatted DFS referral request</param>
        /// <param name="name">This field is present but not used in SMB_COM_TRANSACTION2 requests. If Unicode support
        /// has been negotiated, then this field MUST be aligned to a 16-bit boundary and MUST consist of two null bytes
        /// (a null-terminator). If Unicode support has not been negotiated this field will contain only one null
        /// byte.</param>
        /// <returns>a Trans2GetDfsReferal request packet</returns>
        public SmbTrans2GetDfsReferalRequestPacket CreateTrans2GetDfsReferalRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            ushort maxParameterCount,
            ushort maxDataCount,
            byte maxSetupCount,
            Trans2SmbParametersFlags smbParametersFlags,
            uint timeout,
            string name,
            REQ_GET_DFS_REFERRAL referralRequest)
        {
            if (name == null)
            {
                name = string.Empty;
            }

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

            // Set Smb_Parameters
            SMB_COM_TRANSACTION2_Request_SMB_Parameters smbParameters =
                new SMB_COM_TRANSACTION2_Request_SMB_Parameters();
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.Flags = (ushort)smbParametersFlags;
            smbParameters.Timeout = timeout;
            smbParameters.SetupCount = 1; // the correct count in word of the Setup is always 1.
            smbParameters.Setup = new ushort[] { (ushort)Trans2SubCommand.TRANS2_GET_DFS_REFERRAL };
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_TRANSACTION2_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb_Data
            SMB_COM_TRANSACTION2_Request_SMB_Data smbData = new SMB_COM_TRANSACTION2_Request_SMB_Data();
            smbData.Name = CifsMessageUtils.ToSmbStringBytes(name,
                (flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE);

            // Set Trans2_Parameters
            TRANS2_GET_DFS_REFERRAL_Request_Trans2_Parameters trans2Parameters =
                new TRANS2_GET_DFS_REFERRAL_Request_Trans2_Parameters();
            trans2Parameters.ReferralRequest = referralRequest;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.Trans2Parameters = trans2Parameters;
            packet.UpdateCountAndOffset();

            return packet;
        }
        /// <summary>
        /// Send a Dfs request to server
        /// </summary>
        /// <param name="payload">REQ_GET_DFS_REFERRAL structure in byte array</param>
        /// <param name="isEX">
        /// Boolean which indicates whether payload contains DFS_GET_REFERRAL_EX message
        /// MUST be false for SMB transport.
        /// </param>>
        /// <exception cref="System.ArgumentNullException">the payload to be sent is null.</exception>
        /// <exception cref="ArgumentException">extended request packet cannot be in payload</exception>
        /// <exception cref="System.InvalidOperationException">The transport is not connected</exception>
        public override void SendDfscPayload(byte[] payload, bool isEX)
        {
            if (isEX)
            {
                throw new ArgumentException("REQ_DFS_GET_REFERRAL_EX message not supported over SMB transport");
            }
            if (this.smbClient == null)
            {
                throw new InvalidOperationException("The transport is not connected.");
            }

            if (payload == null)
            {
                throw new ArgumentNullException("payload");
            }
            REQ_GET_DFS_REFERRAL referral = new REQ_GET_DFS_REFERRAL();

            // Put payload[0] into the lower byte of MaxReferralLevel
            // Put payload[1] into the upper byte of MaxReferralLevel
            // Sample: payload[0] = 0x0B; payload[1] = 0x0A
            // MaxReferralLevel = 0x0A0B;
            referral.MaxReferralLevel = (ushort)(payload[1] << 8 | payload[0]);
            referral.RequestFileName = new byte[payload.Length - sizeofMaxReferralLevel];

            Array.Copy(payload, sizeofMaxReferralLevel, referral.RequestFileName, 0, referral.RequestFileName.Length);
            SmbTrans2GetDfsReferralRequestPacket request = this.smbClient.CreateTrans2GetDFSReferralRequest(
                this.treeId, Trans2SmbParametersFlags.NONE, "", referral);

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