Split up async_req into a generic and a NTSTATUS specific part
[tprouty/samba.git] / source3 / libsmb / clientgen.c
index ff01b6798f9a382f698709c07bbf469ff0882ba8..0382ef5fae7223dd19d11d740ede61356357e3ab 100644 (file)
@@ -50,12 +50,21 @@ unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
  Change the port number used to call on.
 ****************************************************************************/
 
-int cli_set_port(struct cli_state *cli, int port)
+void cli_set_port(struct cli_state *cli, int port)
 {
        cli->port = port;
-       return port;
 }
 
+/****************************************************************************
+ convenience routine to find if we negotiated ucs2
+****************************************************************************/
+
+bool cli_ucs2(struct cli_state *cli)
+{
+       return ((cli->capabilities & CAP_UNICODE) != 0);
+}
+
+
 /****************************************************************************
  Read an smb from a fd ignoring all keepalive packets.
  The timeout is in milliseconds
@@ -315,7 +324,7 @@ bool cli_send_smb_direct_writeX(struct cli_state *cli,
        /* First length to send is the offset to the data. */
        size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
        size_t nwritten=0;
-       ssize_t ret;
+       struct iovec iov[2];
 
        /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
        if (cli->fd == -1) {
@@ -327,33 +336,19 @@ bool cli_send_smb_direct_writeX(struct cli_state *cli,
                return false;
        }
 
-       while (nwritten < len) {
-               ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
-               if (ret <= 0) {
-                       close(cli->fd);
-                       cli->fd = -1;
-                       cli->smb_rw_error = SMB_WRITE_ERROR;
-                       DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
-                               (int)len,(int)ret, strerror(errno) ));
-                       return false;
-               }
-               nwritten += ret;
-       }
+       iov[0].iov_base = cli->outbuf;
+       iov[0].iov_len = len;
+       iov[1].iov_base = CONST_DISCARD(char *, p);
+       iov[1].iov_len = extradata;
 
-       /* Now write the extra data. */
-       nwritten=0;
-       while (nwritten < extradata) {
-               ret = write_socket(cli->fd,p+nwritten,extradata - nwritten);
-               if (ret <= 0) {
-                       close(cli->fd);
-                       cli->fd = -1;
-                       cli->smb_rw_error = SMB_WRITE_ERROR;
-                       DEBUG(0,("Error writing %d extradata "
-                               "bytes to client. %d (%s)\n",
-                               (int)extradata,(int)ret, strerror(errno) ));
-                       return false;
-               }
-               nwritten += ret;
+       nwritten = write_data_iov(cli->fd, iov, 2);
+       if (nwritten < (len + extradata)) {
+               close(cli->fd);
+               cli->fd = -1;
+               cli->smb_rw_error = SMB_WRITE_ERROR;
+               DEBUG(0,("Error writing %d bytes to client. (%s)\n",
+                        (int)(len+extradata), strerror(errno)));
+               return false;
        }
 
        /* Increment the mid so we can tell between responses. */
@@ -655,7 +650,7 @@ static void cli_echo_recv_helper(struct async_req *req)
 
        status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               async_req_nterror(req, status);
                return;
        }
 
@@ -664,7 +659,7 @@ static void cli_echo_recv_helper(struct async_req *req)
        if ((num_bytes != cli_req->data.echo.data.length)
            || (memcmp(cli_req->data.echo.data.data, bytes,
                       num_bytes) != 0)) {
-               async_req_error(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               async_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
                return;
        }
 
@@ -732,7 +727,7 @@ struct async_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
 
 NTSTATUS cli_echo_recv(struct async_req *req)
 {
-       return async_req_simple_recv(req);
+       return async_req_simple_recv_ntstatus(req);
 }
 
 /**
@@ -780,3 +775,28 @@ NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
        TALLOC_FREE(frame);
        return status;
 }
+
+/**
+ * Is the SMB command able to hold an AND_X successor
+ * @param[in] cmd      The SMB command in question
+ * @retval Can we add a chained request after "cmd"?
+ */
+bool is_andx_req(uint8_t cmd)
+{
+       switch (cmd) {
+       case SMBtconX:
+       case SMBlockingX:
+       case SMBopenX:
+       case SMBreadX:
+       case SMBwriteX:
+       case SMBsesssetupX:
+       case SMBulogoffX:
+       case SMBntcreateX:
+               return true;
+               break;
+       default:
+               break;
+       }
+
+       return false;
+}