Module jcifs

Class ServerMessageBlock2

java.lang.Object
org.codelibs.jcifs.smb.internal.smb2.ServerMessageBlock2
All Implemented Interfaces:
CommonServerMessageBlock, Message
Direct Known Subclasses:
ServerMessageBlock2Request, ServerMessageBlock2Response, Smb2CancelRequest

public abstract class ServerMessageBlock2 extends Object implements CommonServerMessageBlock
Base class for SMB2/SMB3 protocol messages. This abstract class provides common functionality for all SMB2/SMB3 message types including encoding, decoding, and message handling.
Author:
mbechler
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final short
    SMB2 cancel command
    protected static final short
    SMB2 change notify command
    protected static final short
    SMB2 close file command
    protected static final short
    SMB2 create/open file command
    protected static final short
    SMB2 echo/keepalive command
    static final int
    Flag indicating this is an asynchronous command.
    static final int
    Flag indicating the operation is a DFS operation.
    static final int
    Mask for message priority bits.
    static final int
    Flag indicating this operation is related to the previous operation in a compound request.
    static final int
    Flag indicating this is a replay operation.
    static final int
    Flag indicating the message is a response from server to client.
    static final int
    Flag indicating the message is signed.
    protected static final short
    SMB2 flush file command
    protected static final short
    SMB2 IO control command
    protected static final short
    SMB2 lock file command
    protected static final short
    SMB2 logoff command
    protected static final short
    SMB2 negotiate protocol command
    protected static final short
    SMB2 opportunistic lock break notification
    protected static final short
    SMB2 query directory command
    protected static final short
    SMB2 query info command
    protected static final short
    SMB2 read file command
    protected static final short
    SMB2 session setup command
    protected static final short
    SMB2 set info command
    protected static final short
    SMB2 tree connect command
    protected static final short
    SMB2 tree disconnect command
    protected static final short
    SMB2 write file command
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructor for SMB2 message block with configuration.
    protected
    ServerMessageBlock2(Configuration config, int command)
    Constructor for SMB2 message block with configuration and command.
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    addFlags(int flag)
    Adds the specified flags to this message.
    boolean
    Chains another message to this message for compound operations.
    final void
    clearFlags(int flag)
    Clears the specified flags from this message.
    int
    decode(byte[] buffer, int bufferIndex)
    Decode message data from the given byte array
    int
    decode(byte[] buffer, int bufferIndex, boolean compound)
    Decodes the SMB2 message from the buffer.
    int
    encode(byte[] dst, int dstIndex)
    Encodes this message into a byte array.
    boolean
     
    final long
    Gets the asynchronous identifier for this message.
    final int
    Gets the SMB command.
    protected Configuration
    Gets the configuration object for this message.
    final int
    Gets the credit count for this message.
    final int
    Gets the credit charge for this message.
    Gets the signing digest for this message.
    final byte
    Gets the error context count for this message.
    final byte[]
    Gets the error data associated with this message.
    final int
    Gets the flags for this message.
    final int
    Gets the header start position for this message.
    final int
    Gets the total length of this message.
    final long
    Gets the message ID.
    Gets the next message in the compound chain.
    final int
    Gets the offset to the next compound command in the message chain.
    byte[]
    Gets the raw payload of the message.
    Gets the response associated with this message.
    long
    Gets the session identifier for this message.
    final int
    Gets the status code for this message.
    final int
    Gets the tree identifier for this message.
    int
     
    protected void
    haveResponse(byte[] buffer, int start, int len)
    Called when a response has been received and decoded.
    boolean
    Checks whether this message is an asynchronous message.
    protected boolean
    Checks if this message has an error status.
    boolean
    Determines whether to retain the message payload.
    protected final int
    pad8(int dstIndex)
    Calculates padding needed to align to 8-byte boundary from header start.
    protected abstract int
    readBytesWireFormat(byte[] buffer, int bufferIndex)
    Reads the message body from the wire format.
    protected int
    readErrorResponse(byte[] buffer, int bufferIndex)
    Read error response from buffer
    protected int
    readHeaderWireFormat(byte[] buffer, int bufferIndex)
    Reads the SMB2 header from the wire format.
    void
    Resets this message to its initial state.
    void
    Indicate that this message should retain it's raw payload
    final void
    setAsyncId(long asyncId)
    Sets the asynchronous identifier for this message.
    final void
    setCommand(int command)
    Sets the SMB command.
    final void
    setCredit(int credit)
    Sets the credit count for this message.
    void
    Sets the signing digest for this message.
    void
    setExtendedSecurity(boolean extendedSecurity)
    Sets whether extended security is enabled.
    final void
    setMid(long mid)
    Sets the message ID.
    protected void
    Sets the next message in the compound chain.
    void
    setRawPayload(byte[] rawPayload)
    Sets the raw payload of the message.
    void
    setReadSize(int readSize)
    Sets the read size for this message.
    void
    Sets the response for this message.
    final void
    setSessionId(long sessionId)
    Sets the session ID.
    final void
    setTreeId(int treeId)
    Sets the tree identifier for this message.
    void
    setUid(int uid)
    Sets the user ID.
    protected static final int
    size8(int size)
    Rounds up the size to 8-byte alignment.
    protected static final int
    size8(int size, int align)
    Rounds up the size to the specified alignment.
     
    protected abstract int
    writeBytesWireFormat(byte[] dst, int dstIndex)
    Writes the message body to the wire format.
    protected int
    writeHeaderWireFormat(byte[] dst, int dstIndex)
    Writes the SMB2 header to the wire format.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • SMB2_NEGOTIATE

      protected static final short SMB2_NEGOTIATE
      SMB2 negotiate protocol command
      See Also:
    • SMB2_SESSION_SETUP

      protected static final short SMB2_SESSION_SETUP
      SMB2 session setup command
      See Also:
    • SMB2_LOGOFF

      protected static final short SMB2_LOGOFF
      SMB2 logoff command
      See Also:
    • SMB2_TREE_CONNECT

      protected static final short SMB2_TREE_CONNECT
      SMB2 tree connect command
      See Also:
    • SMB2_TREE_DISCONNECT

      protected static final short SMB2_TREE_DISCONNECT
      SMB2 tree disconnect command
      See Also:
    • SMB2_CREATE

      protected static final short SMB2_CREATE
      SMB2 create/open file command
      See Also:
    • SMB2_CLOSE

      protected static final short SMB2_CLOSE
      SMB2 close file command
      See Also:
    • SMB2_FLUSH

      protected static final short SMB2_FLUSH
      SMB2 flush file command
      See Also:
    • SMB2_READ

      protected static final short SMB2_READ
      SMB2 read file command
      See Also:
    • SMB2_WRITE

      protected static final short SMB2_WRITE
      SMB2 write file command
      See Also:
    • SMB2_LOCK

      protected static final short SMB2_LOCK
      SMB2 lock file command
      See Also:
    • SMB2_IOCTL

      protected static final short SMB2_IOCTL
      SMB2 IO control command
      See Also:
    • SMB2_CANCEL

      protected static final short SMB2_CANCEL
      SMB2 cancel command
      See Also:
    • SMB2_ECHO

      protected static final short SMB2_ECHO
      SMB2 echo/keepalive command
      See Also:
    • SMB2_QUERY_DIRECTORY

      protected static final short SMB2_QUERY_DIRECTORY
      SMB2 query directory command
      See Also:
    • SMB2_CHANGE_NOTIFY

      protected static final short SMB2_CHANGE_NOTIFY
      SMB2 change notify command
      See Also:
    • SMB2_QUERY_INFO

      protected static final short SMB2_QUERY_INFO
      SMB2 query info command
      See Also:
    • SMB2_SET_INFO

      protected static final short SMB2_SET_INFO
      SMB2 set info command
      See Also:
    • SMB2_OPLOCK_BREAK

      protected static final short SMB2_OPLOCK_BREAK
      SMB2 opportunistic lock break notification
      See Also:
    • SMB2_FLAGS_SERVER_TO_REDIR

      public static final int SMB2_FLAGS_SERVER_TO_REDIR
      Flag indicating the message is a response from server to client.
      See Also:
    • SMB2_FLAGS_ASYNC_COMMAND

      public static final int SMB2_FLAGS_ASYNC_COMMAND
      Flag indicating this is an asynchronous command.
      See Also:
    • SMB2_FLAGS_SIGNED

      public static final int SMB2_FLAGS_SIGNED
      Flag indicating the message is signed.
      See Also:
    • SMB2_FLAGS_PRIORITY_MASK

      public static final int SMB2_FLAGS_PRIORITY_MASK
      Mask for message priority bits.
      See Also:
    • SMB2_FLAGS_DFS_OPERATIONS

      public static final int SMB2_FLAGS_DFS_OPERATIONS
      Flag indicating the operation is a DFS operation.
      See Also:
    • SMB2_FLAGS_REPLAY_OPERATION

      public static final int SMB2_FLAGS_REPLAY_OPERATION
      Flag indicating this is a replay operation.
      See Also:
  • Constructor Details

    • ServerMessageBlock2

      protected ServerMessageBlock2(Configuration config)
      Constructor for SMB2 message block with configuration.
      Parameters:
      config - the configuration object
    • ServerMessageBlock2

      protected ServerMessageBlock2(Configuration config, int command)
      Constructor for SMB2 message block with configuration and command.
      Parameters:
      config - the configuration object
      command - the SMB2 command code
  • Method Details

    • getConfig

      protected Configuration getConfig()
      Gets the configuration object for this message.
      Returns:
      the configuration object
    • reset

      public void reset()
      Description copied from interface: CommonServerMessageBlock
      Resets this message to its initial state.
      Specified by:
      reset in interface CommonServerMessageBlock
    • getCommand

      public final int getCommand()
      Description copied from interface: CommonServerMessageBlock
      Gets the SMB command.
      Specified by:
      getCommand in interface CommonServerMessageBlock
      Returns:
      the command
    • getNextCommandOffset

      public final int getNextCommandOffset()
      Gets the offset to the next compound command in the message chain.
      Returns:
      offset to next compound command
    • setReadSize

      public void setReadSize(int readSize)
      Sets the read size for this message.
      Parameters:
      readSize - the readSize to set
    • isAsync

      public boolean isAsync()
      Checks whether this message is an asynchronous message.
      Returns:
      the async
    • setCommand

      public final void setCommand(int command)
      Description copied from interface: CommonServerMessageBlock
      Sets the SMB command.
      Specified by:
      setCommand in interface CommonServerMessageBlock
      Parameters:
      command - the command to set
    • getTreeId

      public final int getTreeId()
      Gets the tree identifier for this message.
      Returns:
      the treeId
    • setTreeId

      public final void setTreeId(int treeId)
      Sets the tree identifier for this message.
      Parameters:
      treeId - the treeId to set
    • getAsyncId

      public final long getAsyncId()
      Gets the asynchronous identifier for this message.
      Returns:
      the asyncId
    • setAsyncId

      public final void setAsyncId(long asyncId)
      Sets the asynchronous identifier for this message.
      Parameters:
      asyncId - the asyncId to set
    • getCredit

      public final int getCredit()
      Gets the credit count for this message.
      Returns:
      the credit
    • setCredit

      public final void setCredit(int credit)
      Sets the credit count for this message.
      Parameters:
      credit - the credit to set
    • getCreditCharge

      public final int getCreditCharge()
      Gets the credit charge for this message.
      Returns:
      the creditCharge
    • retainPayload

      public void retainPayload()
      Description copied from interface: Message
      Indicate that this message should retain it's raw payload
      Specified by:
      retainPayload in interface Message
    • isRetainPayload

      public boolean isRetainPayload()
      Description copied from interface: Message
      Determines whether to retain the message payload.
      Specified by:
      isRetainPayload in interface Message
      Returns:
      whether to retain the message payload
    • getRawPayload

      public byte[] getRawPayload()
      Description copied from interface: Message
      Gets the raw payload of the message.
      Specified by:
      getRawPayload in interface Message
      Returns:
      the raw response message
    • setRawPayload

      public void setRawPayload(byte[] rawPayload)
      Description copied from interface: Message
      Sets the raw payload of the message.
      Specified by:
      setRawPayload in interface Message
      Parameters:
      rawPayload - the raw message payload to set
    • getDigest

      public Smb2SigningDigest getDigest()
      Gets the signing digest for this message.
      Specified by:
      getDigest in interface CommonServerMessageBlock
      Returns:
      the signing digest
      See Also:
    • setDigest

      public void setDigest(SMBSigningDigest digest)
      Sets the signing digest for this message.
      Specified by:
      setDigest in interface CommonServerMessageBlock
      Parameters:
      digest - the signing digest to set
      See Also:
    • getStatus

      public final int getStatus()
      Gets the status code for this message.
      Returns:
      the status
    • getSessionId

      public long getSessionId()
      Gets the session identifier for this message.
      Returns:
      the sessionId
    • setSessionId

      public final void setSessionId(long sessionId)
      Description copied from interface: CommonServerMessageBlock
      Sets the session ID.
      Specified by:
      setSessionId in interface CommonServerMessageBlock
      Parameters:
      sessionId - the sessionId to set
    • setExtendedSecurity

      public void setExtendedSecurity(boolean extendedSecurity)
      Sets whether extended security is enabled.
      Specified by:
      setExtendedSecurity in interface CommonServerMessageBlock
      Parameters:
      extendedSecurity - true to enable extended security
      See Also:
    • setUid

      public void setUid(int uid)
      Sets the user ID.
      Specified by:
      setUid in interface CommonServerMessageBlock
      Parameters:
      uid - the user ID to set
      See Also:
    • getFlags

      public final int getFlags()
      Gets the flags for this message.
      Returns:
      the flags
    • addFlags

      public final void addFlags(int flag)
      Adds the specified flags to this message.
      Parameters:
      flag - the flags to add
    • clearFlags

      public final void clearFlags(int flag)
      Clears the specified flags from this message.
      Parameters:
      flag - the flags to clear
    • getMid

      public final long getMid()
      Description copied from interface: CommonServerMessageBlock
      Gets the message ID.
      Specified by:
      getMid in interface CommonServerMessageBlock
      Returns:
      the mid
    • setMid

      public final void setMid(long mid)
      Description copied from interface: CommonServerMessageBlock
      Sets the message ID.
      Specified by:
      setMid in interface CommonServerMessageBlock
      Parameters:
      mid - the mid to set
    • chain

      public boolean chain(ServerMessageBlock2 n)
      Chains another message to this message for compound operations.
      Parameters:
      n - the message to chain
      Returns:
      whether chaining was successful
    • getNext

      protected ServerMessageBlock2 getNext()
      Gets the next message in the compound chain.
      Returns:
      the next message or null if this is the last message
    • setNext

      protected void setNext(ServerMessageBlock2 n)
      Sets the next message in the compound chain.
      Parameters:
      n - the next message
    • getResponse

      public ServerMessageBlock2Response getResponse()
      Description copied from interface: CommonServerMessageBlock
      Gets the response associated with this message.
      Specified by:
      getResponse in interface CommonServerMessageBlock
      Returns:
      the response
    • setResponse

      public void setResponse(CommonServerMessageBlockResponse msg)
      Sets the response for this message.
      Specified by:
      setResponse in interface CommonServerMessageBlock
      Parameters:
      msg - the response message to set
      See Also:
    • getErrorData

      public final byte[] getErrorData()
      Gets the error data associated with this message.
      Returns:
      the errorData
    • getErrorContextCount

      public final byte getErrorContextCount()
      Gets the error context count for this message.
      Returns:
      the errorContextCount
    • getHeaderStart

      public final int getHeaderStart()
      Gets the header start position for this message.
      Returns:
      the headerStart
    • getLength

      public final int getLength()
      Gets the total length of this message.
      Returns:
      the length
    • encode

      public int encode(byte[] dst, int dstIndex)
      Description copied from interface: CommonServerMessageBlock
      Encodes this message into a byte array.
      Specified by:
      encode in interface CommonServerMessageBlock
      Parameters:
      dst - the destination byte array
      dstIndex - the starting index in the destination array
      Returns:
      message length
    • size8

      protected static final int size8(int size)
      Rounds up the size to 8-byte alignment.
      Parameters:
      size - the size to align
      Returns:
      the aligned size
    • size8

      protected static final int size8(int size, int align)
      Rounds up the size to the specified alignment.
      Parameters:
      size - the size to align
      align - the alignment boundary
      Returns:
      the aligned size
    • pad8

      protected final int pad8(int dstIndex)
      Calculates padding needed to align to 8-byte boundary from header start.
      Parameters:
      dstIndex - the current destination index
      Returns:
      number of padding bytes needed
    • decode

      public int decode(byte[] buffer, int bufferIndex) throws SMBProtocolDecodingException
      Description copied from interface: CommonServerMessageBlock
      Decode message data from the given byte array
      Specified by:
      decode in interface CommonServerMessageBlock
      Parameters:
      buffer - the byte array containing the message data
      bufferIndex - the starting index in the buffer
      Returns:
      message length
      Throws:
      SMBProtocolDecodingException - if decoding fails
    • decode

      public int decode(byte[] buffer, int bufferIndex, boolean compound) throws SMBProtocolDecodingException
      Decodes the SMB2 message from the buffer.
      Parameters:
      buffer - the buffer containing the message
      bufferIndex - the starting position in the buffer
      compound - whether this is part of a compound chain
      Returns:
      decoded length
      Throws:
      SMBProtocolDecodingException - if decoding fails
    • isErrorResponseStatus

      protected boolean isErrorResponseStatus()
      Checks if this message has an error status.
      Returns:
      true if the message has an error status
    • haveResponse

      protected void haveResponse(byte[] buffer, int start, int len) throws SMBProtocolDecodingException
      Called when a response has been received and decoded.
      Parameters:
      buffer - the buffer containing the response
      start - the starting position in the buffer
      len - the length of the response
      Throws:
      SMBProtocolDecodingException - if processing fails
    • readErrorResponse

      protected int readErrorResponse(byte[] buffer, int bufferIndex) throws SMBProtocolDecodingException
      Read error response from buffer
      Parameters:
      buffer - the buffer to read from
      bufferIndex - the starting index in the buffer
      Returns:
      the number of bytes read
      Throws:
      SMBProtocolDecodingException - if decoding fails
    • writeHeaderWireFormat

      protected int writeHeaderWireFormat(byte[] dst, int dstIndex)
      Writes the SMB2 header to the wire format.
      Parameters:
      dst - the destination buffer
      dstIndex - the starting position in the buffer
      Returns:
      number of bytes written
    • readHeaderWireFormat

      protected int readHeaderWireFormat(byte[] buffer, int bufferIndex)
      Reads the SMB2 header from the wire format.
      Parameters:
      buffer - the buffer to read from
      bufferIndex - the starting position in the buffer
      Returns:
      number of bytes read
    • writeBytesWireFormat

      protected abstract int writeBytesWireFormat(byte[] dst, int dstIndex)
      Writes the message body to the wire format.
      Parameters:
      dst - the destination buffer
      dstIndex - the starting position in the buffer
      Returns:
      number of bytes written
    • readBytesWireFormat

      protected abstract int readBytesWireFormat(byte[] buffer, int bufferIndex) throws SMBProtocolDecodingException
      Reads the message body from the wire format.
      Parameters:
      buffer - the buffer to read from
      bufferIndex - the starting position in the buffer
      Returns:
      number of bytes read
      Throws:
      SMBProtocolDecodingException - if decoding fails
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object