Add a "bytes_padding" parameter to smb_splice_chain
authorVolker Lendecke <vl@samba.org>
Sun, 16 Nov 2008 17:02:17 +0000 (18:02 +0100)
committerVolker Lendecke <vl@samba.org>
Mon, 8 Dec 2008 21:09:33 +0000 (22:09 +0100)
For example open&x and write&x needs the bytes to be aligned relative to the
SMB header. In particular for write&x we should not have to move stuff around.

source3/include/async_smb.h
source3/libsmb/async_smb.c

index 24ea6649d952555360676f946bf7be1efb726336..25fd353632aa58af9c57ae1f58570246f25d1c62 100644 (file)
@@ -127,6 +127,7 @@ void cli_chain_uncork(struct cli_state *cli);
 bool cli_in_chain(struct cli_state *cli);
 bool smb_splice_chain(char **poutbuf, uint8_t smb_command,
                      uint8_t wct, const uint16_t *vwv,
+                     size_t bytes_alignment,
                      uint16_t num_bytes, const uint8_t *bytes);
 
 NTSTATUS cli_pull_reply(struct async_req *req,
index cf6886ff190e5ab03a0de72a027fa2062f5a005b..fd2fe930f84639c7ecfe190e240383fb950fcf55 100644 (file)
@@ -223,6 +223,7 @@ static bool find_andx_cmd_ofs(char *buf, size_t *pofs)
  * @param[in] smb_command      The command that we want to issue
  * @param[in] wct              How many words?
  * @param[in] vwv              The words, already in network order
+ * @param[in] bytes_alignment  How shall we align "bytes"?
  * @param[in] num_bytes                How many bytes?
  * @param[in] bytes            The data the request ships
  *
@@ -232,12 +233,14 @@ static bool find_andx_cmd_ofs(char *buf, size_t *pofs)
 
 bool smb_splice_chain(char **poutbuf, uint8_t smb_command,
                      uint8_t wct, const uint16_t *vwv,
+                     size_t bytes_alignment,
                      uint16_t num_bytes, const uint8_t *bytes)
 {
        char *outbuf;
        size_t old_size, new_size;
        size_t ofs;
-       size_t padding = 0;
+       size_t chain_padding = 0;
+       size_t bytes_padding = 0;
        bool first_request;
 
        old_size = talloc_get_size(*poutbuf);
@@ -251,17 +254,25 @@ bool smb_splice_chain(char **poutbuf, uint8_t smb_command,
 
        if (!first_request && ((old_size % 4) != 0)) {
                /*
-                * Align subsequent requests to a 4-byte boundary
+                * Align the wct field of subsequent requests to a 4-byte
+                * boundary
                 */
-               padding = 4 - (old_size % 4);
+               chain_padding = 4 - (old_size % 4);
        }
 
        /*
-        * We need space for the wct field, the words, the byte count field
-        * and the bytes themselves.
+        * After the old request comes the new wct field (1 byte), the vwv's
+        * and the num_bytes field. After at we might need to align the bytes
+        * given to us to "bytes_alignment", increasing the num_bytes value.
         */
-       new_size = old_size + padding
-               + 1 + wct * sizeof(uint16_t) + 2 + num_bytes;
+
+       new_size = old_size + chain_padding + 1 + wct * sizeof(uint16_t) + 2;
+
+       if ((bytes_alignment != 0) && ((new_size % bytes_alignment) != 0)) {
+               bytes_padding = bytes_alignment + (new_size % bytes_alignment);
+       }
+
+       new_size += bytes_padding + num_bytes;
 
        if (new_size > 0xffff) {
                DEBUG(1, ("splice_chain: %u bytes won't fit\n",
@@ -288,9 +299,9 @@ bool smb_splice_chain(char **poutbuf, uint8_t smb_command,
                        return false;
                }
 
-               if (padding != 0) {
-                       memset(outbuf + old_size, 0, padding);
-                       old_size += padding;
+               if (chain_padding != 0) {
+                       memset(outbuf + old_size, 0, chain_padding);
+                       old_size += chain_padding;
                }
 
                SCVAL(outbuf, andx_cmd_ofs, smb_command);
@@ -305,9 +316,14 @@ bool smb_splice_chain(char **poutbuf, uint8_t smb_command,
        memcpy(outbuf + ofs, vwv, sizeof(uint16_t) * wct);
        ofs += sizeof(uint16_t) * wct;
 
-       SSVAL(outbuf, ofs, num_bytes);
+       SSVAL(outbuf, ofs, num_bytes + bytes_padding);
        ofs += sizeof(uint16_t);
 
+       if (bytes_padding != 0) {
+               memset(outbuf + ofs, 0, bytes_padding);
+               ofs += bytes_padding;
+       }
+
        memcpy(outbuf + ofs, bytes, num_bytes);
 
        return true;
@@ -407,7 +423,7 @@ static struct async_req *cli_request_chain(TALLOC_CTX *mem_ctx,
                              cli_async_req_destructor);
 
        if (!smb_splice_chain(&req->outbuf, smb_command, wct, vwv,
-                             num_bytes, bytes)) {
+                             0, num_bytes, bytes)) {
                goto fail;
        }