s3:libsmb: remove unused cli_smb_inbuf()
[metze/samba/wip.git] / source3 / libsmb / async_smb.c
index 2ce641094fae214d3b839396a19cd97b2b96c426..0493a52a0fd1f8cabae66d33783f857af6dfef8a 100644 (file)
 #include "libsmb/nmblib.h"
 #include "read_smb.h"
 
-/**
- * Fetch an error out of a NBT packet
- * @param[in] buf      The SMB packet
- * @retval             The error, converted to NTSTATUS
- */
-
-NTSTATUS cli_pull_error(char *buf)
+static NTSTATUS cli_pull_raw_error(const uint8_t *buf)
 {
        uint32_t flags2 = SVAL(buf, smb_flg2);
 
@@ -41,30 +35,7 @@ NTSTATUS cli_pull_error(char *buf)
                return NT_STATUS(IVAL(buf, smb_rcls));
        }
 
-       return dos_to_ntstatus(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
-}
-
-/**
- * Compatibility helper for the sync APIs: Fake NTSTATUS in cli->inbuf
- * @param[in] cli      The client connection that just received an error
- * @param[in] status   The error to set on "cli"
- */
-
-void cli_set_error(struct cli_state *cli, NTSTATUS status)
-{
-       uint32_t flags2 = SVAL(cli->inbuf, smb_flg2);
-
-       if (NT_STATUS_IS_DOS(status)) {
-               SSVAL(cli->inbuf, smb_flg2,
-                     flags2 & ~FLAGS2_32_BIT_ERROR_CODES);
-               SCVAL(cli->inbuf, smb_rcls, NT_STATUS_DOS_CLASS(status));
-               SSVAL(cli->inbuf, smb_err, NT_STATUS_DOS_CODE(status));
-               return;
-       }
-
-       SSVAL(cli->inbuf, smb_flg2, flags2 | FLAGS2_32_BIT_ERROR_CODES);
-       SIVAL(cli->inbuf, smb_rcls, NT_STATUS_V(status));
-       return;
+       return NT_STATUS_DOS(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
 }
 
 /**
@@ -153,6 +124,14 @@ void cli_smb_req_unset_pending(struct tevent_req *req)
        int num_pending = talloc_array_length(cli->pending);
        int i;
 
+       if (state->mid != 0) {
+               /*
+                * This is a [nt]trans[2] request which waits
+                * for more than one reply.
+                */
+               return;
+       }
+
        if (num_pending == 1) {
                /*
                 * The pending read_smb tevent_req is a child of
@@ -193,6 +172,13 @@ void cli_smb_req_unset_pending(struct tevent_req *req)
 
 static int cli_smb_req_destructor(struct tevent_req *req)
 {
+       struct cli_smb_state *state = tevent_req_data(
+               req, struct cli_smb_state);
+       /*
+        * Make sure we really remove it from
+        * the pending array on destruction.
+        */
+       state->mid = 0;
        cli_smb_req_unset_pending(req);
        return 0;
 }
@@ -255,6 +241,20 @@ void cli_smb_req_set_mid(struct tevent_req *req, uint16_t mid)
        state->mid = mid;
 }
 
+uint32_t cli_smb_req_seqnum(struct tevent_req *req)
+{
+       struct cli_smb_state *state = tevent_req_data(
+               req, struct cli_smb_state);
+       return state->seqnum;
+}
+
+void cli_smb_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
+{
+       struct cli_smb_state *state = tevent_req_data(
+               req, struct cli_smb_state);
+       state->seqnum = seqnum;
+}
+
 static size_t iov_len(const struct iovec *iov, int count)
 {
        size_t result = 0;
@@ -340,7 +340,7 @@ struct tevent_req *cli_smb_req_create(TALLOC_CTX *mem_ctx,
        if (cli->timeout) {
                endtime = timeval_current_ofs_msec(cli->timeout);
                if (!tevent_req_set_endtime(result, ev, endtime)) {
-                       tevent_req_nomem(NULL, result);
+                       tevent_req_oom(result);
                }
        }
        return result;
@@ -627,7 +627,7 @@ static void cli_smb_received(struct tevent_req *subreq)
        if (state->chained_requests == NULL) {
                state->inbuf = talloc_move(state, &inbuf);
                talloc_set_destructor(req, NULL);
-               cli_smb_req_destructor(req);
+               cli_smb_req_unset_pending(req);
                state->chain_num = 0;
                state->chain_length = 1;
                tevent_req_done(req);
@@ -671,7 +671,7 @@ static void cli_smb_received(struct tevent_req *subreq)
        while (talloc_array_length(cli->pending) > 0) {
                req = cli->pending[0];
                talloc_set_destructor(req, NULL);
-               cli_smb_req_destructor(req);
+               cli_smb_req_unset_pending(req);
                tevent_req_nterror(req, status);
        }
 }
@@ -694,6 +694,24 @@ NTSTATUS cli_smb_recv(struct tevent_req *req,
        }
 
        if (state->inbuf == NULL) {
+               if (min_wct != 0) {
+                       return NT_STATUS_INVALID_NETWORK_RESPONSE;
+               }
+               if (pinbuf) {
+                       *pinbuf = NULL;
+               }
+               if (pwct) {
+                       *pwct = 0;
+               }
+               if (pvwv) {
+                       *pvwv = NULL;
+               }
+               if (pnum_bytes) {
+                       *pnum_bytes = 0;
+               }
+               if (pbytes) {
+                       *pbytes = NULL;
+               }
                /* This was a request without a reply */
                return NT_STATUS_OK;
        }
@@ -734,9 +752,20 @@ NTSTATUS cli_smb_recv(struct tevent_req *req,
                cmd = CVAL(state->inbuf, wct_ofs + 1);
        }
 
-       status = cli_pull_error((char *)state->inbuf);
-
-       cli_set_error(state->cli, status);
+       state->cli->raw_status = cli_pull_raw_error(state->inbuf);
+       if (NT_STATUS_IS_DOS(state->cli->raw_status)) {
+               uint8_t eclass = NT_STATUS_DOS_CLASS(state->cli->raw_status);
+               uint16_t ecode = NT_STATUS_DOS_CODE(state->cli->raw_status);
+               /*
+                * TODO: is it really a good idea to do a mapping here?
+                *
+                * The old cli_pull_error() also does it, so I do not change
+                * the behavior yet.
+                */
+               status = dos_to_ntstatus(eclass, ecode);
+       } else {
+               status = state->cli->raw_status;
+       }
 
        if (!have_andx_command((char *)state->inbuf, wct_ofs)) {
 
@@ -921,13 +950,6 @@ NTSTATUS cli_smb_chain_send(struct tevent_req **reqs, int num_reqs)
        return NT_STATUS_OK;
 }
 
-uint8_t *cli_smb_inbuf(struct tevent_req *req)
-{
-       struct cli_smb_state *state = tevent_req_data(
-               req, struct cli_smb_state);
-       return state->inbuf;
-}
-
 bool cli_has_async_calls(struct cli_state *cli)
 {
        return ((tevent_queue_length(cli->outgoing) != 0)